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/. */
8 * container for information saved in session history when the document
12 #include "nsILayoutHistoryState.h"
13 #include "nsWeakReference.h"
14 #include "mozilla/PresState.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/UniquePtr.h"
17 #include "nsTHashMap.h"
19 using namespace mozilla
;
21 class nsLayoutHistoryState final
: public nsILayoutHistoryState
,
22 public nsSupportsWeakReference
{
24 nsLayoutHistoryState() : mScrollPositionOnly(false) {}
27 NS_DECL_NSILAYOUTHISTORYSTATE
30 ~nsLayoutHistoryState() = default;
31 bool mScrollPositionOnly
;
33 nsTHashMap
<nsCString
, UniquePtr
<PresState
>> mStates
;
36 already_AddRefed
<nsILayoutHistoryState
> NS_NewLayoutHistoryState() {
37 RefPtr
<nsLayoutHistoryState
> state
= new nsLayoutHistoryState();
38 return state
.forget();
41 NS_IMPL_ISUPPORTS(nsLayoutHistoryState
, nsILayoutHistoryState
,
42 nsISupportsWeakReference
)
45 nsLayoutHistoryState::GetHasStates(bool* aHasStates
) {
46 *aHasStates
= HasStates();
51 nsLayoutHistoryState::GetKeys(nsTArray
<nsCString
>& aKeys
) {
53 return NS_ERROR_FAILURE
;
56 AppendToArray(aKeys
, mStates
.Keys());
62 nsLayoutHistoryState::GetPresState(const nsACString
& aKey
, float* aScrollX
,
64 bool* aAllowScrollOriginDowngrade
,
66 PresState
* state
= GetState(nsCString(aKey
));
69 return NS_ERROR_FAILURE
;
72 *aScrollX
= state
->scrollState().x
;
73 *aScrollY
= state
->scrollState().y
;
74 *aAllowScrollOriginDowngrade
= state
->allowScrollOriginDowngrade();
75 *aRes
= state
->resolution();
81 nsLayoutHistoryState::AddNewPresState(const nsACString
& aKey
, float aScrollX
,
83 bool aAllowScrollOriginDowngrade
,
85 UniquePtr
<PresState
> newState
= NewPresState();
86 newState
->scrollState() = nsPoint(aScrollX
, aScrollY
);
87 newState
->allowScrollOriginDowngrade() = aAllowScrollOriginDowngrade
;
88 newState
->resolution() = aRes
;
90 mStates
.InsertOrUpdate(nsCString(aKey
), std::move(newState
));
95 void nsLayoutHistoryState::AddState(const nsCString
& aStateKey
,
96 UniquePtr
<PresState
> aState
) {
97 mStates
.InsertOrUpdate(aStateKey
, std::move(aState
));
100 PresState
* nsLayoutHistoryState::GetState(const nsCString
& aKey
) {
101 auto statePtr
= mStates
.Lookup(aKey
);
105 PresState
* state
= statePtr
->get();
107 if (mScrollPositionOnly
) {
108 // Ensure any state that shouldn't be restored is removed
109 state
->contentData() = void_t();
110 state
->disabledSet() = false;
116 void nsLayoutHistoryState::RemoveState(const nsCString
& aKey
) {
117 mStates
.Remove(aKey
);
120 bool nsLayoutHistoryState::HasStates() { return mStates
.Count() != 0; }
122 void nsLayoutHistoryState::SetScrollPositionOnly(const bool aFlag
) {
123 mScrollPositionOnly
= aFlag
;
126 void nsLayoutHistoryState::ResetScrollState() {
127 for (const auto& state
: mStates
.Values()) {
129 state
->scrollState() = nsPoint(0, 0);
134 void nsLayoutHistoryState::GetContents(bool* aScrollPositionOnly
,
135 nsTArray
<nsCString
>& aKeys
,
136 nsTArray
<mozilla::PresState
>& aStates
) {
137 *aScrollPositionOnly
= mScrollPositionOnly
;
138 aKeys
.SetCapacity(mStates
.Count());
139 aStates
.SetCapacity(mStates
.Count());
140 for (const auto& entry
: mStates
) {
141 aKeys
.AppendElement(entry
.GetKey());
142 aStates
.AppendElement(*(entry
.GetData().get()));
146 void nsLayoutHistoryState::Reset() {
147 mScrollPositionOnly
= false;
152 UniquePtr
<PresState
> NewPresState() {
153 return MakeUnique
<PresState
>(
154 /* contentData */ mozilla::void_t(),
155 /* scrollState */ nsPoint(0, 0),
156 /* allowScrollOriginDowngrade */ true,
157 /* resolution */ 1.0,
158 /* disabledSet */ false,
159 /* disabled */ false,
160 /* droppedDown */ false);
162 } // namespace mozilla