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