1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
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/. */
10 #include "mozilla/dom/HistoryBinding.h"
12 #include "nsPIDOMWindow.h"
13 #include "nsIDocument.h"
14 #include "nsIPresShell.h"
15 #include "nsPresContext.h"
16 #include "nsIDocShell.h"
17 #include "nsIWebNavigation.h"
19 #include "nsIInterfaceRequestorUtils.h"
20 #include "nsReadableUtils.h"
21 #include "nsContentUtils.h"
22 #include "nsISHistory.h"
23 #include "nsISHistoryInternal.h"
24 #include "mozilla/Preferences.h"
26 using namespace mozilla
;
27 using namespace mozilla::dom
;
29 static const char* sAllowPushStatePrefStr
=
30 "browser.history.allowPushState";
31 static const char* sAllowReplaceStatePrefStr
=
32 "browser.history.allowReplaceState";
35 // History class implementation
37 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsHistory
)
38 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHistory
)
39 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHistory
)
40 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHistory
)
41 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
42 NS_INTERFACE_MAP_ENTRY(nsISupports
)
43 NS_INTERFACE_MAP_ENTRY(nsIDOMHistory
) // Empty, needed for extension compat
46 nsHistory::nsHistory(nsPIDOMWindow
* aInnerWindow
)
47 : mInnerWindow(do_GetWeakReference(aInnerWindow
))
52 nsHistory::~nsHistory()
57 nsHistory::GetParentObject() const
59 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
64 nsHistory::WrapObject(JSContext
* aCx
)
66 return HistoryBinding::Wrap(aCx
, this);
70 nsHistory::GetLength(ErrorResult
& aRv
) const
72 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
73 if (!win
|| !win
->HasActiveDocument()) {
74 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
79 // Get session History from docshell
80 nsCOMPtr
<nsISHistory
> sHistory
= GetSessionHistory();
82 aRv
.Throw(NS_ERROR_FAILURE
);
88 nsresult rv
= sHistory
->GetCount(&len
);
96 return len
>= 0 ? len
: 0;
100 nsHistory::GetState(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aResult
,
101 ErrorResult
& aRv
) const
103 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
105 aRv
.Throw(NS_ERROR_NOT_AVAILABLE
);
109 if (!win
->HasActiveDocument()) {
110 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
114 nsCOMPtr
<nsIDocument
> doc
=
115 do_QueryInterface(win
->GetExtantDoc());
117 aRv
.Throw(NS_ERROR_NOT_AVAILABLE
);
121 nsCOMPtr
<nsIVariant
> variant
;
122 doc
->GetStateObject(getter_AddRefs(variant
));
125 aRv
= variant
->GetAsJSVal(aResult
);
131 if (!JS_WrapValue(aCx
, aResult
)) {
132 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
142 nsHistory::Go(int32_t aDelta
, ErrorResult
& aRv
)
144 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
145 if (!win
|| !win
->HasActiveDocument()) {
146 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
152 nsCOMPtr
<nsPIDOMWindow
> window
;
153 if (nsIDocShell
* docShell
= GetDocShell()) {
154 window
= docShell
->GetWindow();
157 if (window
&& window
->IsHandlingResizeEvent()) {
158 // history.go(0) (aka location.reload()) was called on a window
159 // that is handling a resize event. Sites do this since Netscape
160 // 4.x needed it, but we don't, and it's a horrible experience
161 // for nothing. In stead of reloading the page, just clear
162 // style data and reflow the page since some sites may use this
163 // trick to work around gecko reflow bugs, and this should have
166 nsCOMPtr
<nsIDocument
> doc
= window
->GetExtantDoc();
170 if (doc
&& (shell
= doc
->GetShell()) && (pcx
= shell
->GetPresContext())) {
171 pcx
->RebuildAllStyleData(NS_STYLE_HINT_REFLOW
);
178 nsCOMPtr
<nsISHistory
> session_history
= GetSessionHistory();
179 nsCOMPtr
<nsIWebNavigation
> webnav(do_QueryInterface(session_history
));
181 aRv
.Throw(NS_ERROR_FAILURE
);
186 int32_t curIndex
= -1;
188 session_history
->GetIndex(&curIndex
);
189 session_history
->GetCount(&len
);
191 int32_t index
= curIndex
+ aDelta
;
192 if (index
> -1 && index
< len
)
193 webnav
->GotoIndex(index
);
195 // Ignore the return value from GotoIndex(), since returning errors
196 // from GotoIndex() can lead to exceptions and a possible leak
201 nsHistory::Back(ErrorResult
& aRv
)
203 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
204 if (!win
|| !win
->HasActiveDocument()) {
205 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
210 nsCOMPtr
<nsISHistory
> sHistory
= GetSessionHistory();
211 nsCOMPtr
<nsIWebNavigation
> webNav(do_QueryInterface(sHistory
));
213 aRv
.Throw(NS_ERROR_FAILURE
);
222 nsHistory::Forward(ErrorResult
& aRv
)
224 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
225 if (!win
|| !win
->HasActiveDocument()) {
226 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
231 nsCOMPtr
<nsISHistory
> sHistory
= GetSessionHistory();
232 nsCOMPtr
<nsIWebNavigation
> webNav(do_QueryInterface(sHistory
));
234 aRv
.Throw(NS_ERROR_FAILURE
);
243 nsHistory::PushState(JSContext
* aCx
, JS::Handle
<JS::Value
> aData
,
244 const nsAString
& aTitle
, const nsAString
& aUrl
,
247 PushOrReplaceState(aCx
, aData
, aTitle
, aUrl
, aRv
, false);
251 nsHistory::ReplaceState(JSContext
* aCx
, JS::Handle
<JS::Value
> aData
,
252 const nsAString
& aTitle
, const nsAString
& aUrl
,
255 PushOrReplaceState(aCx
, aData
, aTitle
, aUrl
, aRv
, true);
259 nsHistory::PushOrReplaceState(JSContext
* aCx
, JS::Handle
<JS::Value
> aData
,
260 const nsAString
& aTitle
, const nsAString
& aUrl
,
261 ErrorResult
& aRv
, bool aReplace
)
263 nsCOMPtr
<nsPIDOMWindow
> win(do_QueryReferent(mInnerWindow
));
265 aRv
.Throw(NS_ERROR_NOT_AVAILABLE
);
270 if (!win
->HasActiveDocument()) {
271 aRv
.Throw(NS_ERROR_DOM_SECURITY_ERR
);
276 // Check that PushState hasn't been pref'ed off.
277 if (!Preferences::GetBool(aReplace
? sAllowReplaceStatePrefStr
:
278 sAllowPushStatePrefStr
, false)) {
282 // AddState might run scripts, so we need to hold a strong reference to the
283 // docShell here to keep it from going away.
284 nsCOMPtr
<nsIDocShell
> docShell
= win
->GetDocShell();
287 aRv
.Throw(NS_ERROR_FAILURE
);
292 // The "replace" argument tells the docshell to whether to add a new
293 // history entry or modify the current one.
295 aRv
= docShell
->AddState(aData
, aTitle
, aUrl
, aReplace
, aCx
);
299 nsHistory::GetDocShell() const
301 nsCOMPtr
<nsPIDOMWindow
> win
= do_QueryReferent(mInnerWindow
);
305 return win
->GetDocShell();
308 already_AddRefed
<nsISHistory
>
309 nsHistory::GetSessionHistory() const
311 nsIDocShell
*docShell
= GetDocShell();
312 NS_ENSURE_TRUE(docShell
, nullptr);
314 // Get the root DocShell from it
315 nsCOMPtr
<nsIDocShellTreeItem
> root
;
316 docShell
->GetSameTypeRootTreeItem(getter_AddRefs(root
));
317 nsCOMPtr
<nsIWebNavigation
> webNav(do_QueryInterface(root
));
318 NS_ENSURE_TRUE(webNav
, nullptr);
320 nsCOMPtr
<nsISHistory
> shistory
;
322 // Get SH from nsIWebNavigation
323 webNav
->GetSessionHistory(getter_AddRefs(shistory
));
325 return shistory
.forget();