Files
tubestation/toolkit/components/remotebrowserutils/RemoteWebNavigation.jsm
Jim Porter e104d2ed02 Bug 1552017 - Expand the kinds of URLs that can cancel content JS when navigating; r=smaug
This patch makes several changes to the kinds of URLs where we can cancel
content JS when navigating between them:

 1) When navigating directly to a URL (e.g. by typing something into the
    location bar and hitting Enter), we allow canceling content JS if the URLs
    differ in any way *except* their ref ("#"). To help with this, we also
    attempt to fix up the URL (e.g. by prepending "http://" to it).

 2) When navigating through history, we allow canceling content JS if the
    `prePath` part of the URLs differ. Most notably, this allows canceling
    content JS when one of the URLs is an `about:` page (e.g. when hitting the
    Home button).

 3) We explicitly disallow cancelling content JS if the currently-running JS
    is trusted or if the page being navigated away from is anything but
    http(s): or file:.

 4) We also disallow cancelling content JS for windows that are still being
    created (e.g. when creating a new tab or window via `window.open`). For
    more background on this, see the comments about `mCreatingWindow` in
    dom/ipc/BrowserParent.h.

 5) We ensure that, when attempting to cancel JS, the tab ID of the
    currently-running script matches the original tab that requested the
    cancellation. This avoids a race condition in which a particular JSContext
    has already moved on to executing another tab's JS by the time we hit our
    interrupt callback.

Differential Revision: https://phabricator.services.mozilla.com/D31875
2019-06-04 16:19:27 +00:00

175 lines
6.4 KiB
JavaScript

// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
// 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/.
ChromeUtils.defineModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(this, "Utils",
"resource://gre/modules/sessionstore/Utils.jsm");
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
ChromeUtils.defineModuleGetter(this, "E10SUtils",
"resource://gre/modules/E10SUtils.jsm");
function RemoteWebNavigation() {
this.wrappedJSObject = this;
this._cancelContentJSEpoch = 1;
}
RemoteWebNavigation.prototype = {
classDescription: "nsIWebNavigation for remote browsers",
classID: Components.ID("{4b56964e-cdf3-4bb8-830c-0e2dad3f4ebd}"),
contractID: "@mozilla.org/remote-web-navigation;1",
QueryInterface: ChromeUtils.generateQI([Ci.nsIWebNavigation]),
swapBrowser(aBrowser) {
this._browser = aBrowser;
},
LOAD_FLAGS_MASK: 65535,
LOAD_FLAGS_NONE: 0,
LOAD_FLAGS_IS_REFRESH: 16,
LOAD_FLAGS_IS_LINK: 32,
LOAD_FLAGS_BYPASS_HISTORY: 64,
LOAD_FLAGS_REPLACE_HISTORY: 128,
LOAD_FLAGS_BYPASS_CACHE: 256,
LOAD_FLAGS_BYPASS_PROXY: 512,
LOAD_FLAGS_CHARSET_CHANGE: 1024,
LOAD_FLAGS_STOP_CONTENT: 2048,
LOAD_FLAGS_FROM_EXTERNAL: 4096,
LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP: 8192,
LOAD_FLAGS_FIRST_LOAD: 16384,
LOAD_FLAGS_ALLOW_POPUPS: 32768,
LOAD_FLAGS_BYPASS_CLASSIFIER: 65536,
LOAD_FLAGS_FORCE_ALLOW_COOKIES: 131072,
STOP_NETWORK: 1,
STOP_CONTENT: 2,
STOP_ALL: 3,
canGoBack: false,
canGoForward: false,
goBack() {
let cancelContentJSEpoch = this._cancelContentJSEpoch++;
this._browser.frameLoader.remoteTab.maybeCancelContentJSExecution(
Ci.nsIRemoteTab.NAVIGATE_BACK, {epoch: cancelContentJSEpoch});
this._sendMessage("WebNavigation:GoBack", {cancelContentJSEpoch});
},
goForward() {
let cancelContentJSEpoch = this._cancelContentJSEpoch++;
this._browser.frameLoader.remoteTab.maybeCancelContentJSExecution(
Ci.nsIRemoteTab.NAVIGATE_FORWARD, {epoch: cancelContentJSEpoch});
this._sendMessage("WebNavigation:GoForward", {cancelContentJSEpoch});
},
gotoIndex(aIndex) {
let cancelContentJSEpoch = this._cancelContentJSEpoch++;
this._browser.frameLoader.remoteTab.maybeCancelContentJSExecution(
Ci.nsIRemoteTab.NAVIGATE_INDEX,
{index: aIndex, epoch: cancelContentJSEpoch});
this._sendMessage("WebNavigation:GotoIndex", {index: aIndex,
cancelContentJSEpoch});
},
loadURI(aURI, aLoadURIOptions) {
let uri;
try {
let fixup = Cc["@mozilla.org/docshell/urifixup;1"].getService();
let fixupFlags = fixup.webNavigationFlagsToFixupFlags(
aURI, aLoadURIOptions.loadFlags);
uri = fixup.createFixupURI(aURI, fixupFlags);
// We know the url is going to be loaded, let's start requesting network
// connection before the content process asks.
// Note that we might have already setup the speculative connection in
// some cases, especially when the url is from location bar or its popup
// menu.
if (uri.schemeIs("http") || uri.schemeIs("https")) {
let principal = aLoadURIOptions.triggeringPrincipal;
// We usually have a triggeringPrincipal assigned, but in case we
// don't have one or if it's a SystemPrincipal, let's create it with OA
// inferred from the current context.
if (!principal || principal.isSystemPrincipal) {
let attrs = {
userContextId: this._browser.getAttribute("usercontextid") || 0,
privateBrowsingId: PrivateBrowsingUtils.isBrowserPrivate(this._browser) ? 1 : 0,
};
principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, attrs);
}
Services.io.speculativeConnect(uri, principal, null);
}
} catch (ex) {
// Can't setup speculative connection for this uri string for some
// reason (such as failing to parse the URI), just ignore it.
}
let cancelContentJSEpoch = this._cancelContentJSEpoch++;
this._browser.frameLoader.remoteTab.maybeCancelContentJSExecution(
Ci.nsIRemoteTab.NAVIGATE_URL, {uri, epoch: cancelContentJSEpoch});
this._sendMessage("WebNavigation:LoadURI", {
uri: aURI,
flags: aLoadURIOptions.loadFlags,
referrerInfo: E10SUtils.serializeReferrerInfo(aLoadURIOptions.referrerInfo),
postData: aLoadURIOptions.postData ? Utils.serializeInputStream(aLoadURIOptions.postData) : null,
headers: aLoadURIOptions.headers ? Utils.serializeInputStream(aLoadURIOptions.headers) : null,
baseURI: aLoadURIOptions.baseURI ? aLoadURIOptions.baseURI.spec : null,
triggeringPrincipal: E10SUtils.serializePrincipal(
aLoadURIOptions.triggeringPrincipal || Services.scriptSecurityManager.createNullPrincipal({})),
csp: aLoadURIOptions.csp ? E10SUtils.serializeCSP(aLoadURIOptions.csp) : null,
cancelContentJSEpoch,
});
},
setOriginAttributesBeforeLoading(aOriginAttributes) {
this._sendMessage("WebNavigation:SetOriginAttributes", {
originAttributes: aOriginAttributes,
});
},
reload(aReloadFlags) {
this._sendMessage("WebNavigation:Reload", {flags: aReloadFlags});
},
stop(aStopFlags) {
this._sendMessage("WebNavigation:Stop", {flags: aStopFlags});
},
get document() {
return this._browser.contentDocument;
},
_currentURI: null,
get currentURI() {
if (!this._currentURI) {
this._currentURI = Services.io.newURI("about:blank");
}
return this._currentURI;
},
set currentURI(aURI) {
// Bug 1498600 verify usages of systemPrincipal here
let loadURIOptions = {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
};
this.loadURI(aURI.spec, loadURIOptions);
},
referringURI: null,
// Bug 1233803 - accessing the sessionHistory of remote browsers should be
// done in content scripts.
get sessionHistory() {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
set sessionHistory(aValue) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
_sendMessage(aMessage, aData) {
try {
this._browser.messageManager.sendAsyncMessage(aMessage, aData);
} catch (e) {
Cu.reportError(e);
}
},
};
var EXPORTED_SYMBOLS = ["RemoteWebNavigation"];