/src/mozilla-central/layout/base/nsLayoutHistoryState.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* |
8 | | * container for information saved in session history when the document |
9 | | * is not |
10 | | */ |
11 | | |
12 | | #include "nsILayoutHistoryState.h" |
13 | | #include "nsWeakReference.h" |
14 | | #include "nsClassHashtable.h" |
15 | | #include "mozilla/PresState.h" |
16 | | #include "mozilla/Attributes.h" |
17 | | #include "mozilla/UniquePtr.h" |
18 | | |
19 | | using namespace mozilla; |
20 | | |
21 | | class nsLayoutHistoryState final : public nsILayoutHistoryState, |
22 | | public nsSupportsWeakReference |
23 | | { |
24 | | public: |
25 | | nsLayoutHistoryState() |
26 | | : mScrollPositionOnly(false) |
27 | 0 | { |
28 | 0 | } |
29 | | |
30 | | NS_DECL_ISUPPORTS |
31 | | NS_DECL_NSILAYOUTHISTORYSTATE |
32 | | |
33 | | private: |
34 | 0 | ~nsLayoutHistoryState() {} |
35 | | bool mScrollPositionOnly; |
36 | | |
37 | | nsDataHashtable<nsCStringHashKey, UniquePtr<PresState>> mStates; |
38 | | }; |
39 | | |
40 | | |
41 | | already_AddRefed<nsILayoutHistoryState> |
42 | | NS_NewLayoutHistoryState() |
43 | 0 | { |
44 | 0 | RefPtr<nsLayoutHistoryState> state = new nsLayoutHistoryState(); |
45 | 0 | return state.forget(); |
46 | 0 | } |
47 | | |
48 | | NS_IMPL_ISUPPORTS(nsLayoutHistoryState, |
49 | | nsILayoutHistoryState, |
50 | | nsISupportsWeakReference) |
51 | | |
52 | | NS_IMETHODIMP |
53 | | nsLayoutHistoryState::GetHasStates(bool* aHasStates) |
54 | 0 | { |
55 | 0 | *aHasStates = HasStates(); |
56 | 0 | return NS_OK; |
57 | 0 | } |
58 | | |
59 | | NS_IMETHODIMP |
60 | | nsLayoutHistoryState::GetKeys(uint32_t* aCount, char*** aKeys) |
61 | 0 | { |
62 | 0 | if (!HasStates()) { |
63 | 0 | return NS_ERROR_FAILURE; |
64 | 0 | } |
65 | 0 | |
66 | 0 | char** keys = |
67 | 0 | static_cast<char**>(moz_xmalloc(sizeof(char*) * mStates.Count())); |
68 | 0 | *aCount = mStates.Count(); |
69 | 0 | *aKeys = keys; |
70 | 0 |
|
71 | 0 | for (auto iter = mStates.Iter(); !iter.Done(); iter.Next()) { |
72 | 0 | *keys = ToNewCString(iter.Key()); |
73 | 0 | keys++; |
74 | 0 | } |
75 | 0 |
|
76 | 0 | return NS_OK; |
77 | 0 | } |
78 | | |
79 | | NS_IMETHODIMP |
80 | | nsLayoutHistoryState::GetPresState(const nsACString& aKey, |
81 | | float* aScrollX, float* aScrollY, |
82 | | bool* aAllowScrollOriginDowngrade, |
83 | | float* aRes, bool* aScaleToRes) |
84 | 0 | { |
85 | 0 | PresState* state = GetState(nsCString(aKey)); |
86 | 0 |
|
87 | 0 | if (!state) { |
88 | 0 | return NS_ERROR_FAILURE; |
89 | 0 | } |
90 | 0 | |
91 | 0 | *aScrollX = state->scrollState().x; |
92 | 0 | *aScrollY = state->scrollState().y; |
93 | 0 | *aAllowScrollOriginDowngrade = state->allowScrollOriginDowngrade(); |
94 | 0 | *aRes = state->resolution(); |
95 | 0 | *aScaleToRes = state->scaleToResolution(); |
96 | 0 |
|
97 | 0 | return NS_OK; |
98 | 0 | } |
99 | | |
100 | | NS_IMETHODIMP |
101 | | nsLayoutHistoryState::AddNewPresState(const nsACString& aKey, |
102 | | float aScrollX, float aScrollY, |
103 | | bool aAllowScrollOriginDowngrade, |
104 | | float aRes, bool aScaleToRes) |
105 | 0 | { |
106 | 0 | UniquePtr<PresState> newState = NewPresState(); |
107 | 0 | newState->scrollState() = nsPoint(aScrollX, aScrollY); |
108 | 0 | newState->allowScrollOriginDowngrade() = aAllowScrollOriginDowngrade; |
109 | 0 | newState->resolution() = aRes; |
110 | 0 | newState->scaleToResolution() = aScaleToRes; |
111 | 0 |
|
112 | 0 | mStates.Put(nsCString(aKey), std::move(newState)); |
113 | 0 |
|
114 | 0 | return NS_OK; |
115 | 0 | } |
116 | | |
117 | | void |
118 | | nsLayoutHistoryState::AddState(const nsCString& aStateKey, UniquePtr<PresState> aState) |
119 | 0 | { |
120 | 0 | mStates.Put(aStateKey, std::move(aState)); |
121 | 0 | } |
122 | | |
123 | | PresState* |
124 | | nsLayoutHistoryState::GetState(const nsCString& aKey) |
125 | 0 | { |
126 | 0 | UniquePtr<PresState>* statePtr = mStates.GetValue(aKey); |
127 | 0 | if (!statePtr) { |
128 | 0 | return nullptr; |
129 | 0 | } |
130 | 0 | PresState* state = statePtr->get(); |
131 | 0 |
|
132 | 0 | if (mScrollPositionOnly) { |
133 | 0 | // Ensure any state that shouldn't be restored is removed |
134 | 0 | state->contentData() = void_t(); |
135 | 0 | state->disabledSet() = false; |
136 | 0 | } |
137 | 0 |
|
138 | 0 | return state; |
139 | 0 | } |
140 | | |
141 | | void |
142 | | nsLayoutHistoryState::RemoveState(const nsCString& aKey) |
143 | 0 | { |
144 | 0 | mStates.Remove(aKey); |
145 | 0 | } |
146 | | |
147 | | bool |
148 | | nsLayoutHistoryState::HasStates() |
149 | 0 | { |
150 | 0 | return mStates.Count() != 0; |
151 | 0 | } |
152 | | |
153 | | void |
154 | | nsLayoutHistoryState::SetScrollPositionOnly(const bool aFlag) |
155 | 0 | { |
156 | 0 | mScrollPositionOnly = aFlag; |
157 | 0 | } |
158 | | |
159 | | void |
160 | | nsLayoutHistoryState::ResetScrollState() |
161 | 0 | { |
162 | 0 | for (auto iter = mStates.Iter(); !iter.Done(); iter.Next()) { |
163 | 0 | PresState* state = iter.Data().get(); |
164 | 0 | if (state) { |
165 | 0 | state->scrollState() = nsPoint(0, 0); |
166 | 0 | } |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | namespace mozilla { |
171 | | UniquePtr<PresState> |
172 | | NewPresState() |
173 | 0 | { |
174 | 0 | return MakeUnique<PresState>( |
175 | 0 | /* contentData */ mozilla::void_t(), |
176 | 0 | /* scrollState */ nsPoint(0, 0), |
177 | 0 | /* allowScrollOriginDowngrade */ true, |
178 | 0 | /* resolution */ 1.0, |
179 | 0 | /* scaleToResolution */ false, |
180 | 0 | /* disabledSet */ false, |
181 | 0 | /* disabled */ false, |
182 | 0 | /* droppedDown */ false); |
183 | 0 | } |
184 | | } // namespace mozilla |