Mypal68/toolkit/components/remotebrowserutils/RemoteWebNavigation.jsm
2024-11-25 17:24:41 +02:00

191 lines
6.0 KiB
JavaScript

// 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;
},
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,
loadFlags: 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", { loadFlags: aReloadFlags });
},
stop(aStopFlags) {
this._sendMessage("WebNavigation:Stop", { loadFlags: 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"];