Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsHistory.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
#include "nsHistory.h"
8
9
#include "jsapi.h"
10
#include "nsCOMPtr.h"
11
#include "nsPIDOMWindow.h"
12
#include "nsIDocument.h"
13
#include "nsIPresShell.h"
14
#include "nsPresContext.h"
15
#include "nsIDocShell.h"
16
#include "nsIWebNavigation.h"
17
#include "nsIURI.h"
18
#include "nsIInterfaceRequestorUtils.h"
19
#include "nsReadableUtils.h"
20
#include "nsContentUtils.h"
21
#include "nsISHistory.h"
22
#include "mozilla/Preferences.h"
23
24
using namespace mozilla;
25
using namespace mozilla::dom;
26
27
//
28
//  History class implementation
29
//
30
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsHistory)
31
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHistory)
32
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHistory)
33
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHistory)
34
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
35
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
36
0
NS_INTERFACE_MAP_END
37
38
nsHistory::nsHistory(nsPIDOMWindowInner* aInnerWindow)
39
  : mInnerWindow(do_GetWeakReference(aInnerWindow))
40
0
{
41
0
}
42
43
nsHistory::~nsHistory()
44
0
{
45
0
}
46
47
nsPIDOMWindowInner*
48
nsHistory::GetParentObject() const
49
0
{
50
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
51
0
  return win;
52
0
}
53
54
JSObject*
55
nsHistory::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
56
0
{
57
0
  return History_Binding::Wrap(aCx, this, aGivenProto);
58
0
}
59
60
uint32_t
61
nsHistory::GetLength(ErrorResult& aRv) const
62
0
{
63
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
64
0
  if (!win || !win->HasActiveDocument()) {
65
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
66
0
67
0
    return 0;
68
0
  }
69
0
70
0
  // Get session History from docshell
71
0
  RefPtr<ChildSHistory> sHistory = GetSessionHistory();
72
0
  if (!sHistory) {
73
0
    aRv.Throw(NS_ERROR_FAILURE);
74
0
75
0
    return 0;
76
0
  }
77
0
78
0
  int32_t len = sHistory->Count();;
79
0
  return len >= 0 ? len : 0;
80
0
}
81
82
ScrollRestoration
83
nsHistory::GetScrollRestoration(mozilla::ErrorResult& aRv)
84
0
{
85
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
86
0
  if (!win || !win->HasActiveDocument() || !win->GetDocShell()) {
87
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
88
0
    return mozilla::dom::ScrollRestoration::Auto;
89
0
  }
90
0
91
0
  bool currentScrollRestorationIsManual = false;
92
0
  win->GetDocShell()->
93
0
    GetCurrentScrollRestorationIsManual(&currentScrollRestorationIsManual);
94
0
  return currentScrollRestorationIsManual ?
95
0
    mozilla::dom::ScrollRestoration::Manual :
96
0
    mozilla::dom::ScrollRestoration::Auto;
97
0
}
98
99
void
100
nsHistory::SetScrollRestoration(mozilla::dom::ScrollRestoration aMode,
101
                                mozilla::ErrorResult& aRv)
102
0
{
103
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
104
0
  if (!win || !win->HasActiveDocument() || !win->GetDocShell()) {
105
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
106
0
    return;
107
0
  }
108
0
109
0
  win->GetDocShell()->
110
0
    SetCurrentScrollRestorationIsManual(
111
0
      aMode == mozilla::dom::ScrollRestoration::Manual);
112
0
}
113
114
void
115
nsHistory::GetState(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
116
                    ErrorResult& aRv) const
117
0
{
118
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
119
0
  if (!win) {
120
0
    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
121
0
    return;
122
0
  }
123
0
124
0
  if (!win->HasActiveDocument()) {
125
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
126
0
    return;
127
0
  }
128
0
129
0
  nsCOMPtr<nsIDocument> doc =
130
0
    do_QueryInterface(win->GetExtantDoc());
131
0
  if (!doc) {
132
0
    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
133
0
    return;
134
0
  }
135
0
136
0
  nsCOMPtr<nsIVariant> variant;
137
0
  doc->GetStateObject(getter_AddRefs(variant));
138
0
139
0
  if (variant) {
140
0
    aRv = variant->GetAsJSVal(aResult);
141
0
142
0
    if (aRv.Failed()) {
143
0
      return;
144
0
    }
145
0
146
0
    if (!JS_WrapValue(aCx, aResult)) {
147
0
      aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
148
0
    }
149
0
150
0
    return;
151
0
  }
152
0
153
0
  aResult.setNull();
154
0
}
155
156
void
157
nsHistory::Go(int32_t aDelta, ErrorResult& aRv)
158
0
{
159
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
160
0
  if (!win || !win->HasActiveDocument()) {
161
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
162
0
163
0
    return;
164
0
  }
165
0
166
0
  if (!aDelta) {
167
0
    nsCOMPtr<nsPIDOMWindowOuter> window;
168
0
    if (nsIDocShell* docShell = GetDocShell()) {
169
0
      window = docShell->GetWindow();
170
0
    }
171
0
172
0
    if (window && window->IsHandlingResizeEvent()) {
173
0
      // history.go(0) (aka location.reload()) was called on a window
174
0
      // that is handling a resize event. Sites do this since Netscape
175
0
      // 4.x needed it, but we don't, and it's a horrible experience
176
0
      // for nothing.  In stead of reloading the page, just clear
177
0
      // style data and reflow the page since some sites may use this
178
0
      // trick to work around gecko reflow bugs, and this should have
179
0
      // the same effect.
180
0
181
0
      nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
182
0
183
0
      nsPresContext *pcx;
184
0
      if (doc && (pcx = doc->GetPresContext())) {
185
0
        pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW, eRestyle_Subtree);
186
0
      }
187
0
188
0
      return;
189
0
    }
190
0
  }
191
0
192
0
  RefPtr<ChildSHistory> session_history = GetSessionHistory();
193
0
  if (!session_history) {
194
0
    aRv.Throw(NS_ERROR_FAILURE);
195
0
196
0
    return;
197
0
  }
198
0
199
0
  // Ignore the return value from Go(), since returning errors from Go() can
200
0
  // lead to exceptions and a possible leak of history length
201
0
  session_history->Go(aDelta, IgnoreErrors());
202
0
}
203
204
void
205
nsHistory::Back(ErrorResult& aRv)
206
0
{
207
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
208
0
  if (!win || !win->HasActiveDocument()) {
209
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
210
0
211
0
    return;
212
0
  }
213
0
214
0
  RefPtr<ChildSHistory> sHistory = GetSessionHistory();
215
0
  if (!sHistory) {
216
0
    aRv.Throw(NS_ERROR_FAILURE);
217
0
218
0
    return;
219
0
  }
220
0
221
0
  sHistory->Go(-1, IgnoreErrors());
222
0
}
223
224
void
225
nsHistory::Forward(ErrorResult& aRv)
226
0
{
227
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
228
0
  if (!win || !win->HasActiveDocument()) {
229
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
230
0
231
0
    return;
232
0
  }
233
0
234
0
  RefPtr<ChildSHistory> sHistory = GetSessionHistory();
235
0
  if (!sHistory) {
236
0
    aRv.Throw(NS_ERROR_FAILURE);
237
0
238
0
    return;
239
0
  }
240
0
241
0
  sHistory->Go(1, IgnoreErrors());
242
0
}
243
244
void
245
nsHistory::PushState(JSContext* aCx, JS::Handle<JS::Value> aData,
246
                     const nsAString& aTitle, const nsAString& aUrl,
247
                     ErrorResult& aRv)
248
0
{
249
0
  PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, false);
250
0
}
251
252
void
253
nsHistory::ReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData,
254
                        const nsAString& aTitle, const nsAString& aUrl,
255
                        ErrorResult& aRv)
256
0
{
257
0
  PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, true);
258
0
}
259
260
void
261
nsHistory::PushOrReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData,
262
                              const nsAString& aTitle, const nsAString& aUrl,
263
                              ErrorResult& aRv, bool aReplace)
264
0
{
265
0
  nsCOMPtr<nsPIDOMWindowInner> win(do_QueryReferent(mInnerWindow));
266
0
  if (!win) {
267
0
    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
268
0
269
0
    return;
270
0
  }
271
0
272
0
  if (!win->HasActiveDocument()) {
273
0
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
274
0
275
0
    return;
276
0
  }
277
0
278
0
  // AddState might run scripts, so we need to hold a strong reference to the
279
0
  // docShell here to keep it from going away.
280
0
  nsCOMPtr<nsIDocShell> docShell = win->GetDocShell();
281
0
282
0
  if (!docShell) {
283
0
    aRv.Throw(NS_ERROR_FAILURE);
284
0
285
0
    return;
286
0
  }
287
0
288
0
  // The "replace" argument tells the docshell to whether to add a new
289
0
  // history entry or modify the current one.
290
0
291
0
  aRv = docShell->AddState(aData, aTitle, aUrl, aReplace, aCx);
292
0
}
293
294
nsIDocShell*
295
nsHistory::GetDocShell() const
296
0
{
297
0
  nsCOMPtr<nsPIDOMWindowInner> win = do_QueryReferent(mInnerWindow);
298
0
  if (!win) {
299
0
    return nullptr;
300
0
  }
301
0
  return win->GetDocShell();
302
0
}
303
304
already_AddRefed<ChildSHistory>
305
nsHistory::GetSessionHistory() const
306
0
{
307
0
  nsIDocShell *docShell = GetDocShell();
308
0
  NS_ENSURE_TRUE(docShell, nullptr);
309
0
310
0
  // Get the root DocShell from it
311
0
  nsCOMPtr<nsIDocShellTreeItem> root;
312
0
  docShell->GetSameTypeRootTreeItem(getter_AddRefs(root));
313
0
  nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(root));
314
0
  NS_ENSURE_TRUE(webNav, nullptr);
315
0
316
0
  // Get SH from nsIWebNavigation
317
0
  return webNav->GetSessionHistory();
318
0
}