Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / layout / base / nsLayoutHistoryState.cpp
blobf79545fb3b3ed8612af1a363f73207ab5d64056f
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/. */
7 /*
8 * container for information saved in session history when the document
9 * is not
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 {
23 public:
24 nsLayoutHistoryState() : mScrollPositionOnly(false) {}
26 NS_DECL_ISUPPORTS
27 NS_DECL_NSILAYOUTHISTORYSTATE
29 private:
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)
44 NS_IMETHODIMP
45 nsLayoutHistoryState::GetHasStates(bool* aHasStates) {
46 *aHasStates = HasStates();
47 return NS_OK;
50 NS_IMETHODIMP
51 nsLayoutHistoryState::GetKeys(nsTArray<nsCString>& aKeys) {
52 if (!HasStates()) {
53 return NS_ERROR_FAILURE;
56 AppendToArray(aKeys, mStates.Keys());
58 return NS_OK;
61 NS_IMETHODIMP
62 nsLayoutHistoryState::GetPresState(const nsACString& aKey, float* aScrollX,
63 float* aScrollY,
64 bool* aAllowScrollOriginDowngrade,
65 float* aRes) {
66 PresState* state = GetState(nsCString(aKey));
68 if (!state) {
69 return NS_ERROR_FAILURE;
72 *aScrollX = state->scrollState().x;
73 *aScrollY = state->scrollState().y;
74 *aAllowScrollOriginDowngrade = state->allowScrollOriginDowngrade();
75 *aRes = state->resolution();
77 return NS_OK;
80 NS_IMETHODIMP
81 nsLayoutHistoryState::AddNewPresState(const nsACString& aKey, float aScrollX,
82 float aScrollY,
83 bool aAllowScrollOriginDowngrade,
84 float aRes) {
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));
92 return NS_OK;
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);
102 if (!statePtr) {
103 return nullptr;
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;
113 return state;
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()) {
128 if (state) {
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;
148 mStates.Clear();
151 namespace mozilla {
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