2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-02-01 09:19:35 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-07-15 03:31:34 -07:00
|
|
|
#include "nsHistory.h"
|
2013-09-05 23:43:16 -07:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2013-08-09 23:47:59 -07:00
|
|
|
#include "mozilla/dom/HistoryBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-09-05 23:43:16 -07:00
|
|
|
#include "nsISHistory.h"
|
2009-09-01 09:45:05 -07:00
|
|
|
#include "nsISHistoryInternal.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-08-09 23:47:59 -07:00
|
|
|
using namespace mozilla::dom;
|
2009-09-01 09:45:05 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
static const char* sAllowPushStatePrefStr =
|
2009-09-01 09:45:05 -07:00
|
|
|
"browser.history.allowPushState";
|
|
|
|
static const char* sAllowReplaceStatePrefStr =
|
|
|
|
"browser.history.allowReplaceState";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
2013-08-09 23:47:59 -07:00
|
|
|
// History class implementation
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
2013-08-09 23:47:59 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsHistory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHistory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHistory)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHistory)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMHistory) // Empty, needed for extension compat
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-02-01 09:19:35 -08:00
|
|
|
nsHistory::nsHistory(nsPIDOMWindow* aInnerWindow)
|
|
|
|
: mInnerWindow(do_GetWeakReference(aInnerWindow))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHistory::~nsHistory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsPIDOMWindow*
|
|
|
|
nsHistory::GetParentObject() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-20 08:37:21 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
2013-08-09 23:47:59 -07:00
|
|
|
return win;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsHistory::WrapObject(JSContext* aCx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return HistoryBinding::Wrap(aCx, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
uint32_t
|
|
|
|
nsHistory::GetLength(ErrorResult& aRv) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win || !win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2011-02-01 09:19:35 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get session History from docshell
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
|
|
|
|
if (!sHistory) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
int32_t len;
|
|
|
|
nsresult rv = sHistory->GetCount(&len);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len >= 0 ? len : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
JS::Value
|
|
|
|
nsHistory::GetState(JSContext* aCx, ErrorResult& aRv) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
2012-01-26 03:54:23 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
|
|
|
do_QueryInterface(win->GetExtantDoc());
|
|
|
|
if (!doc) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
doc->GetStateObject(getter_AddRefs(variant));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
if (variant) {
|
|
|
|
JS::Rooted<JS::Value> jsData(aCx);
|
2014-01-09 09:39:36 -08:00
|
|
|
aRv = variant->GetAsJSVal(&jsData);
|
2013-02-20 08:37:21 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(aCx, &jsData)) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return jsData;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-12 13:11:56 -08:00
|
|
|
return JS::NullValue();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
void
|
|
|
|
nsHistory::Go(int32_t aDelta, ErrorResult& aRv)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-20 08:37:21 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win || !win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-02-20 08:37:21 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
if (!aDelta) {
|
2011-02-01 09:19:35 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_GetInterface(GetDocShell()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (window && window->IsHandlingResizeEvent()) {
|
|
|
|
// history.go(0) (aka location.reload()) was called on a window
|
|
|
|
// that is handling a resize event. Sites do this since Netscape
|
|
|
|
// 4.x needed it, but we don't, and it's a horrible experience
|
|
|
|
// for nothing. In stead of reloading the page, just clear
|
|
|
|
// style data and reflow the page since some sites may use this
|
|
|
|
// trick to work around gecko reflow bugs, and this should have
|
|
|
|
// the same effect.
|
|
|
|
|
2013-04-23 21:22:37 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIPresShell *shell;
|
|
|
|
nsPresContext *pcx;
|
2010-06-25 06:59:57 -07:00
|
|
|
if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
|
2008-02-08 11:52:46 -08:00
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsISHistory> session_history = GetSessionHistory();
|
2008-09-05 14:54:53 -07:00
|
|
|
nsCOMPtr<nsIWebNavigation> webnav(do_QueryInterface(session_history));
|
2013-08-09 23:47:59 -07:00
|
|
|
if (!webnav) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2008-09-05 14:54:53 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
int32_t curIndex = -1;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t len = 0;
|
2012-07-27 07:03:28 -07:00
|
|
|
session_history->GetIndex(&curIndex);
|
|
|
|
session_history->GetCount(&len);
|
2008-09-05 14:54:53 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = curIndex + aDelta;
|
2013-08-09 23:47:59 -07:00
|
|
|
if (index > -1 && index < len)
|
2008-09-05 14:54:53 -07:00
|
|
|
webnav->GotoIndex(index);
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
// Ignore the return value from GotoIndex(), since returning errors
|
2008-09-05 14:54:53 -07:00
|
|
|
// from GotoIndex() can lead to exceptions and a possible leak
|
|
|
|
// of history length
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
void
|
|
|
|
nsHistory::Back(ErrorResult& aRv)
|
2009-09-01 09:45:05 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win || !win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
|
|
|
return;
|
2011-05-24 23:31:59 -07:00
|
|
|
}
|
2009-09-01 09:45:05 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
|
|
|
|
if (!webNav) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
webNav->GoBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHistory::Forward(ErrorResult& aRv)
|
|
|
|
{
|
2011-02-01 09:19:35 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win || !win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2011-02-01 09:19:35 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2009-09-01 09:45:05 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsISHistory> sHistory = GetSessionHistory();
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(sHistory));
|
|
|
|
if (!webNav) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2011-02-01 09:19:35 -08:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2009-09-01 09:45:05 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
webNav->GoForward();
|
|
|
|
}
|
2009-09-01 09:45:05 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
void
|
|
|
|
nsHistory::PushState(JSContext* aCx, JS::Handle<JS::Value> aData,
|
|
|
|
const nsAString& aTitle, const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, false);
|
2009-09-01 09:45:05 -07:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
void
|
|
|
|
nsHistory::ReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData,
|
|
|
|
const nsAString& aTitle, const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv)
|
2009-09-01 09:45:05 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
PushOrReplaceState(aCx, aData, aTitle, aUrl, aRv, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-23 17:23:37 -07:00
|
|
|
nsHistory::PushOrReplaceState(JSContext* aCx, JS::Handle<JS::Value> aData,
|
2013-08-09 23:47:59 -07:00
|
|
|
const nsAString& aTitle, const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv, bool aReplace)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-27 08:16:07 -08:00
|
|
|
if (!win->HasActiveDocument()) {
|
2013-08-09 23:47:59 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that PushState hasn't been pref'ed off.
|
|
|
|
if (!Preferences::GetBool(aReplace ? sAllowReplaceStatePrefStr :
|
|
|
|
sAllowPushStatePrefStr, false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddState might run scripts, so we need to hold a strong reference to the
|
|
|
|
// docShell here to keep it from going away.
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = win->GetDocShell();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
if (!docShell) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "replace" argument tells the docshell to whether to add a new
|
|
|
|
// history entry or modify the current one.
|
|
|
|
|
|
|
|
aRv = docShell->AddState(aData, aTitle, aUrl, aReplace, aCx);
|
|
|
|
}
|
|
|
|
|
2013-09-05 23:43:16 -07:00
|
|
|
nsIDocShell*
|
|
|
|
nsHistory::GetDocShell() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mInnerWindow);
|
|
|
|
if (!win) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return win->GetDocShell();
|
|
|
|
}
|
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
already_AddRefed<nsISHistory>
|
|
|
|
nsHistory::GetSessionHistory() const
|
|
|
|
{
|
|
|
|
nsIDocShell *docShell = GetDocShell();
|
|
|
|
NS_ENSURE_TRUE(docShell, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get the root DocShell from it
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> root;
|
2013-08-09 23:47:59 -07:00
|
|
|
docShell->GetSameTypeRootTreeItem(getter_AddRefs(root));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(root));
|
|
|
|
NS_ENSURE_TRUE(webNav, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-09 23:47:59 -07:00
|
|
|
nsCOMPtr<nsISHistory> shistory;
|
|
|
|
|
|
|
|
// Get SH from nsIWebNavigation
|
|
|
|
webNav->GetSessionHistory(getter_AddRefs(shistory));
|
|
|
|
|
|
|
|
return shistory.forget();
|
|
|
|
}
|