nsLayoutHistoryState.cpp 4.72 KB
Newer Older
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
/* 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/. */
6

7
8
9
10
11
/*
 * container for information saved in session history when the document
 * is not
 */

12
13
14
#include "nsILayoutHistoryState.h"
#include "nsWeakReference.h"
#include "nsClassHashtable.h"
15
#include "mozilla/PresState.h"
16
#include "mozilla/Attributes.h"
17
18
19
#include "mozilla/UniquePtr.h"

using namespace mozilla;
20

21
class nsLayoutHistoryState final : public nsILayoutHistoryState,
22
23
24
                                   public nsSupportsWeakReference {
 public:
  nsLayoutHistoryState() : mScrollPositionOnly(false) {}
25
26

  NS_DECL_ISUPPORTS
27
  NS_DECL_NSILAYOUTHISTORYSTATE
28

29
 private:
30
  ~nsLayoutHistoryState() {}
31
  bool mScrollPositionOnly;
32

33
  nsDataHashtable<nsCStringHashKey, UniquePtr<PresState>> mStates;
34
35
};

36
already_AddRefed<nsILayoutHistoryState> NS_NewLayoutHistoryState() {
37
  RefPtr<nsLayoutHistoryState> state = new nsLayoutHistoryState();
38
  return state.forget();
39
40
}

41
NS_IMPL_ISUPPORTS(nsLayoutHistoryState, nsILayoutHistoryState,
42
                  nsISupportsWeakReference)
43

44
NS_IMETHODIMP
45
nsLayoutHistoryState::GetHasStates(bool* aHasStates) {
46
47
48
49
50
  *aHasStates = HasStates();
  return NS_OK;
}

NS_IMETHODIMP
51
nsLayoutHistoryState::GetKeys(nsTArray<nsCString>& aKeys) {
52
53
54
55
  if (!HasStates()) {
    return NS_ERROR_FAILURE;
  }

56
  aKeys.SetCapacity(mStates.Count());
57
  for (auto iter = mStates.Iter(); !iter.Done(); iter.Next()) {
58
    aKeys.AppendElement(iter.Key());
59
60
61
62
63
64
  }

  return NS_OK;
}

NS_IMETHODIMP
65
66
nsLayoutHistoryState::GetPresState(const nsACString& aKey, float* aScrollX,
                                   float* aScrollY,
67
                                   bool* aAllowScrollOriginDowngrade,
68
                                   float* aRes) {
69
  PresState* state = GetState(nsCString(aKey));
70
71
72
73
74

  if (!state) {
    return NS_ERROR_FAILURE;
  }

75
76
77
78
  *aScrollX = state->scrollState().x;
  *aScrollY = state->scrollState().y;
  *aAllowScrollOriginDowngrade = state->allowScrollOriginDowngrade();
  *aRes = state->resolution();
79
80
81
82
83

  return NS_OK;
}

NS_IMETHODIMP
84
85
nsLayoutHistoryState::AddNewPresState(const nsACString& aKey, float aScrollX,
                                      float aScrollY,
86
                                      bool aAllowScrollOriginDowngrade,
87
                                      float aRes) {
88
89
90
91
  UniquePtr<PresState> newState = NewPresState();
  newState->scrollState() = nsPoint(aScrollX, aScrollY);
  newState->allowScrollOriginDowngrade() = aAllowScrollOriginDowngrade;
  newState->resolution() = aRes;
92

93
  mStates.Put(nsCString(aKey), std::move(newState));
94
95
96
97

  return NS_OK;
}

98
99
void nsLayoutHistoryState::AddState(const nsCString& aStateKey,
                                    UniquePtr<PresState> aState) {
100
  mStates.Put(aStateKey, std::move(aState));
101
102
}

103
PresState* nsLayoutHistoryState::GetState(const nsCString& aKey) {
104
105
106
107
108
  UniquePtr<PresState>* statePtr = mStates.GetValue(aKey);
  if (!statePtr) {
    return nullptr;
  }
  PresState* state = statePtr->get();
109

110
  if (mScrollPositionOnly) {
111
    // Ensure any state that shouldn't be restored is removed
112
113
    state->contentData() = void_t();
    state->disabledSet() = false;
114
115
  }

116
  return state;
117
}
118

119
void nsLayoutHistoryState::RemoveState(const nsCString& aKey) {
120
  mStates.Remove(aKey);
121
}
122

123
bool nsLayoutHistoryState::HasStates() { return mStates.Count() != 0; }
124

125
void nsLayoutHistoryState::SetScrollPositionOnly(const bool aFlag) {
126
127
  mScrollPositionOnly = aFlag;
}
128

129
void nsLayoutHistoryState::ResetScrollState() {
130
  for (auto iter = mStates.Iter(); !iter.Done(); iter.Next()) {
131
    PresState* state = iter.Data().get();
132
    if (state) {
133
      state->scrollState() = nsPoint(0, 0);
134
135
136
    }
  }
}
137

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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();
}

155
namespace mozilla {
156
UniquePtr<PresState> NewPresState() {
157
158
159
160
161
162
163
164
165
  return MakeUnique<PresState>(
      /* contentData */ mozilla::void_t(),
      /* scrollState */ nsPoint(0, 0),
      /* allowScrollOriginDowngrade */ true,
      /* resolution */ 1.0,
      /* disabledSet */ false,
      /* disabled */ false,
      /* droppedDown */ false);
}
166
}  // namespace mozilla