Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsHistory.cpp
blob9ec76e65a70ca128a3bec8c30e1ed516581021ff
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/. */
7 #include "nsHistory.h"
9 #include "jsapi.h"
10 #include "mozilla/dom/HistoryBinding.h"
11 #include "nsCOMPtr.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"
18 #include "nsIURI.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
44 NS_INTERFACE_MAP_END
46 nsHistory::nsHistory(nsPIDOMWindow* aInnerWindow)
47 : mInnerWindow(do_GetWeakReference(aInnerWindow))
49 SetIsDOMBinding();
52 nsHistory::~nsHistory()
56 nsPIDOMWindow*
57 nsHistory::GetParentObject() const
59 nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
60 return win;
63 JSObject*
64 nsHistory::WrapObject(JSContext* aCx)
66 return HistoryBinding::Wrap(aCx, this);
69 uint32_t
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);
76 return 0;
79 // Get session History from docshell
80 nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
81 if (!sHistory) {
82 aRv.Throw(NS_ERROR_FAILURE);
84 return 0;
87 int32_t len;
88 nsresult rv = sHistory->GetCount(&len);
90 if (NS_FAILED(rv)) {
91 aRv.Throw(rv);
93 return 0;
96 return len >= 0 ? len : 0;
99 void
100 nsHistory::GetState(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
101 ErrorResult& aRv) const
103 nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
104 if (!win) {
105 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
106 return;
109 if (!win->HasActiveDocument()) {
110 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
111 return;
114 nsCOMPtr<nsIDocument> doc =
115 do_QueryInterface(win->GetExtantDoc());
116 if (!doc) {
117 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
118 return;
121 nsCOMPtr<nsIVariant> variant;
122 doc->GetStateObject(getter_AddRefs(variant));
124 if (variant) {
125 aRv = variant->GetAsJSVal(aResult);
127 if (aRv.Failed()) {
128 return;
131 if (!JS_WrapValue(aCx, aResult)) {
132 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
135 return;
138 aResult.setNull();
141 void
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);
148 return;
151 if (!aDelta) {
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
164 // the same effect.
166 nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
168 nsIPresShell *shell;
169 nsPresContext *pcx;
170 if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
171 pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW);
174 return;
178 nsCOMPtr<nsISHistory> session_history = GetSessionHistory();
179 nsCOMPtr<nsIWebNavigation> webnav(do_QueryInterface(session_history));
180 if (!webnav) {
181 aRv.Throw(NS_ERROR_FAILURE);
183 return;
186 int32_t curIndex = -1;
187 int32_t len = 0;
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
197 // of history length
200 void
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);
207 return;
210 nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
211 nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
212 if (!webNav) {
213 aRv.Throw(NS_ERROR_FAILURE);
215 return;
218 webNav->GoBack();
221 void
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);
228 return;
231 nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
232 nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
233 if (!webNav) {
234 aRv.Throw(NS_ERROR_FAILURE);
236 return;
239 webNav->GoForward();
242 void
243 nsHistory::PushState(JSContext* aCx, JS::Handle<JS::Value> aData,
244 const nsAString& aTitle, const nsAString& aUrl,
245 ErrorResult& aRv)
247 PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, false);
250 void
251 nsHistory::ReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData,
252 const nsAString& aTitle, const nsAString& aUrl,
253 ErrorResult& aRv)
255 PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, true);
258 void
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));
264 if (!win) {
265 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
267 return;
270 if (!win->HasActiveDocument()) {
271 aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
273 return;
276 // Check that PushState hasn't been pref'ed off.
277 if (!Preferences::GetBool(aReplace ? sAllowReplaceStatePrefStr :
278 sAllowPushStatePrefStr, false)) {
279 return;
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();
286 if (!docShell) {
287 aRv.Throw(NS_ERROR_FAILURE);
289 return;
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);
298 nsIDocShell*
299 nsHistory::GetDocShell() const
301 nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mInnerWindow);
302 if (!win) {
303 return nullptr;
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();