1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/nsHistory.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,323 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 sw=2 et tw=78: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsHistory.h" 1.11 + 1.12 +#include "jsapi.h" 1.13 +#include "mozilla/dom/HistoryBinding.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsPIDOMWindow.h" 1.16 +#include "nsIDocument.h" 1.17 +#include "nsIPresShell.h" 1.18 +#include "nsPresContext.h" 1.19 +#include "nsIDocShell.h" 1.20 +#include "nsIWebNavigation.h" 1.21 +#include "nsIURI.h" 1.22 +#include "nsIInterfaceRequestorUtils.h" 1.23 +#include "nsReadableUtils.h" 1.24 +#include "nsContentUtils.h" 1.25 +#include "nsISHistory.h" 1.26 +#include "nsISHistoryInternal.h" 1.27 +#include "mozilla/Preferences.h" 1.28 + 1.29 +using namespace mozilla; 1.30 +using namespace mozilla::dom; 1.31 + 1.32 +static const char* sAllowPushStatePrefStr = 1.33 + "browser.history.allowPushState"; 1.34 +static const char* sAllowReplaceStatePrefStr = 1.35 + "browser.history.allowReplaceState"; 1.36 + 1.37 +// 1.38 +// History class implementation 1.39 +// 1.40 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsHistory) 1.41 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHistory) 1.42 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHistory) 1.43 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHistory) 1.44 + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 1.45 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.46 + NS_INTERFACE_MAP_ENTRY(nsIDOMHistory) // Empty, needed for extension compat 1.47 +NS_INTERFACE_MAP_END 1.48 + 1.49 +nsHistory::nsHistory(nsPIDOMWindow* aInnerWindow) 1.50 + : mInnerWindow(do_GetWeakReference(aInnerWindow)) 1.51 +{ 1.52 + SetIsDOMBinding(); 1.53 +} 1.54 + 1.55 +nsHistory::~nsHistory() 1.56 +{ 1.57 +} 1.58 + 1.59 +nsPIDOMWindow* 1.60 +nsHistory::GetParentObject() const 1.61 +{ 1.62 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.63 + return win; 1.64 +} 1.65 + 1.66 +JSObject* 1.67 +nsHistory::WrapObject(JSContext* aCx) 1.68 +{ 1.69 + return HistoryBinding::Wrap(aCx, this); 1.70 +} 1.71 + 1.72 +uint32_t 1.73 +nsHistory::GetLength(ErrorResult& aRv) const 1.74 +{ 1.75 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.76 + if (!win || !win->HasActiveDocument()) { 1.77 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.78 + 1.79 + return 0; 1.80 + } 1.81 + 1.82 + // Get session History from docshell 1.83 + nsCOMPtr<nsISHistory> sHistory = GetSessionHistory(); 1.84 + if (!sHistory) { 1.85 + aRv.Throw(NS_ERROR_FAILURE); 1.86 + 1.87 + return 0; 1.88 + } 1.89 + 1.90 + int32_t len; 1.91 + nsresult rv = sHistory->GetCount(&len); 1.92 + 1.93 + if (NS_FAILED(rv)) { 1.94 + aRv.Throw(rv); 1.95 + 1.96 + return 0; 1.97 + } 1.98 + 1.99 + return len >= 0 ? len : 0; 1.100 +} 1.101 + 1.102 +void 1.103 +nsHistory::GetState(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, 1.104 + ErrorResult& aRv) const 1.105 +{ 1.106 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.107 + if (!win) { 1.108 + aRv.Throw(NS_ERROR_NOT_AVAILABLE); 1.109 + return; 1.110 + } 1.111 + 1.112 + if (!win->HasActiveDocument()) { 1.113 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.114 + return; 1.115 + } 1.116 + 1.117 + nsCOMPtr<nsIDocument> doc = 1.118 + do_QueryInterface(win->GetExtantDoc()); 1.119 + if (!doc) { 1.120 + aRv.Throw(NS_ERROR_NOT_AVAILABLE); 1.121 + return; 1.122 + } 1.123 + 1.124 + nsCOMPtr<nsIVariant> variant; 1.125 + doc->GetStateObject(getter_AddRefs(variant)); 1.126 + 1.127 + if (variant) { 1.128 + aRv = variant->GetAsJSVal(aResult); 1.129 + 1.130 + if (aRv.Failed()) { 1.131 + return; 1.132 + } 1.133 + 1.134 + if (!JS_WrapValue(aCx, aResult)) { 1.135 + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 1.136 + } 1.137 + 1.138 + return; 1.139 + } 1.140 + 1.141 + aResult.setNull(); 1.142 +} 1.143 + 1.144 +void 1.145 +nsHistory::Go(int32_t aDelta, ErrorResult& aRv) 1.146 +{ 1.147 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.148 + if (!win || !win->HasActiveDocument()) { 1.149 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.150 + 1.151 + return; 1.152 + } 1.153 + 1.154 + if (!aDelta) { 1.155 + nsCOMPtr<nsPIDOMWindow> window(do_GetInterface(GetDocShell())); 1.156 + 1.157 + if (window && window->IsHandlingResizeEvent()) { 1.158 + // history.go(0) (aka location.reload()) was called on a window 1.159 + // that is handling a resize event. Sites do this since Netscape 1.160 + // 4.x needed it, but we don't, and it's a horrible experience 1.161 + // for nothing. In stead of reloading the page, just clear 1.162 + // style data and reflow the page since some sites may use this 1.163 + // trick to work around gecko reflow bugs, and this should have 1.164 + // the same effect. 1.165 + 1.166 + nsCOMPtr<nsIDocument> doc = window->GetExtantDoc(); 1.167 + 1.168 + nsIPresShell *shell; 1.169 + nsPresContext *pcx; 1.170 + if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) { 1.171 + pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW); 1.172 + } 1.173 + 1.174 + return; 1.175 + } 1.176 + } 1.177 + 1.178 + nsCOMPtr<nsISHistory> session_history = GetSessionHistory(); 1.179 + nsCOMPtr<nsIWebNavigation> webnav(do_QueryInterface(session_history)); 1.180 + if (!webnav) { 1.181 + aRv.Throw(NS_ERROR_FAILURE); 1.182 + 1.183 + return; 1.184 + } 1.185 + 1.186 + int32_t curIndex = -1; 1.187 + int32_t len = 0; 1.188 + session_history->GetIndex(&curIndex); 1.189 + session_history->GetCount(&len); 1.190 + 1.191 + int32_t index = curIndex + aDelta; 1.192 + if (index > -1 && index < len) 1.193 + webnav->GotoIndex(index); 1.194 + 1.195 + // Ignore the return value from GotoIndex(), since returning errors 1.196 + // from GotoIndex() can lead to exceptions and a possible leak 1.197 + // of history length 1.198 +} 1.199 + 1.200 +void 1.201 +nsHistory::Back(ErrorResult& aRv) 1.202 +{ 1.203 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.204 + if (!win || !win->HasActiveDocument()) { 1.205 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.206 + 1.207 + return; 1.208 + } 1.209 + 1.210 + nsCOMPtr<nsISHistory> sHistory = GetSessionHistory(); 1.211 + nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory)); 1.212 + if (!webNav) { 1.213 + aRv.Throw(NS_ERROR_FAILURE); 1.214 + 1.215 + return; 1.216 + } 1.217 + 1.218 + webNav->GoBack(); 1.219 +} 1.220 + 1.221 +void 1.222 +nsHistory::Forward(ErrorResult& aRv) 1.223 +{ 1.224 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.225 + if (!win || !win->HasActiveDocument()) { 1.226 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.227 + 1.228 + return; 1.229 + } 1.230 + 1.231 + nsCOMPtr<nsISHistory> sHistory = GetSessionHistory(); 1.232 + nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory)); 1.233 + if (!webNav) { 1.234 + aRv.Throw(NS_ERROR_FAILURE); 1.235 + 1.236 + return; 1.237 + } 1.238 + 1.239 + webNav->GoForward(); 1.240 +} 1.241 + 1.242 +void 1.243 +nsHistory::PushState(JSContext* aCx, JS::Handle<JS::Value> aData, 1.244 + const nsAString& aTitle, const nsAString& aUrl, 1.245 + ErrorResult& aRv) 1.246 +{ 1.247 + PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, false); 1.248 +} 1.249 + 1.250 +void 1.251 +nsHistory::ReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData, 1.252 + const nsAString& aTitle, const nsAString& aUrl, 1.253 + ErrorResult& aRv) 1.254 +{ 1.255 + PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, true); 1.256 +} 1.257 + 1.258 +void 1.259 +nsHistory::PushOrReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData, 1.260 + const nsAString& aTitle, const nsAString& aUrl, 1.261 + ErrorResult& aRv, bool aReplace) 1.262 +{ 1.263 + nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow)); 1.264 + if (!win) { 1.265 + aRv.Throw(NS_ERROR_NOT_AVAILABLE); 1.266 + 1.267 + return; 1.268 + } 1.269 + 1.270 + if (!win->HasActiveDocument()) { 1.271 + aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); 1.272 + 1.273 + return; 1.274 + } 1.275 + 1.276 + // Check that PushState hasn't been pref'ed off. 1.277 + if (!Preferences::GetBool(aReplace ? sAllowReplaceStatePrefStr : 1.278 + sAllowPushStatePrefStr, false)) { 1.279 + return; 1.280 + } 1.281 + 1.282 + // AddState might run scripts, so we need to hold a strong reference to the 1.283 + // docShell here to keep it from going away. 1.284 + nsCOMPtr<nsIDocShell> docShell = win->GetDocShell(); 1.285 + 1.286 + if (!docShell) { 1.287 + aRv.Throw(NS_ERROR_FAILURE); 1.288 + 1.289 + return; 1.290 + } 1.291 + 1.292 + // The "replace" argument tells the docshell to whether to add a new 1.293 + // history entry or modify the current one. 1.294 + 1.295 + aRv = docShell->AddState(aData, aTitle, aUrl, aReplace, aCx); 1.296 +} 1.297 + 1.298 +nsIDocShell* 1.299 +nsHistory::GetDocShell() const 1.300 +{ 1.301 + nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mInnerWindow); 1.302 + if (!win) { 1.303 + return nullptr; 1.304 + } 1.305 + return win->GetDocShell(); 1.306 +} 1.307 + 1.308 +already_AddRefed<nsISHistory> 1.309 +nsHistory::GetSessionHistory() const 1.310 +{ 1.311 + nsIDocShell *docShell = GetDocShell(); 1.312 + NS_ENSURE_TRUE(docShell, nullptr); 1.313 + 1.314 + // Get the root DocShell from it 1.315 + nsCOMPtr<nsIDocShellTreeItem> root; 1.316 + docShell->GetSameTypeRootTreeItem(getter_AddRefs(root)); 1.317 + nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(root)); 1.318 + NS_ENSURE_TRUE(webNav, nullptr); 1.319 + 1.320 + nsCOMPtr<nsISHistory> shistory; 1.321 + 1.322 + // Get SH from nsIWebNavigation 1.323 + webNav->GetSessionHistory(getter_AddRefs(shistory)); 1.324 + 1.325 + return shistory.forget(); 1.326 +}