2015-02-04 08:33:00 +03: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["HiddenFrame"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { PromiseUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/PromiseUtils.jsm"
|
|
|
|
);
|
2015-02-04 08:33:00 +03:00
|
|
|
|
2019-12-11 20:44:54 +03:00
|
|
|
const XUL_PAGE = "chrome://global/content/win.xhtml";
|
2015-02-04 08:33:00 +03:00
|
|
|
|
2019-08-16 06:23:28 +03:00
|
|
|
const gAllHiddenFrames = new Set();
|
2017-04-03 23:25:16 +03:00
|
|
|
|
|
|
|
let cleanupRegistered = false;
|
|
|
|
function ensureCleanupRegistered() {
|
|
|
|
if (!cleanupRegistered) {
|
|
|
|
cleanupRegistered = true;
|
|
|
|
Services.obs.addObserver(function() {
|
2020-08-02 22:31:30 +03:00
|
|
|
for (let hiddenFrame of gAllHiddenFrames) {
|
2017-04-03 23:25:16 +03:00
|
|
|
hiddenFrame.destroy();
|
|
|
|
}
|
2017-04-15 00:39:22 +03:00
|
|
|
}, "xpcom-shutdown");
|
2017-04-03 23:25:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:33:00 +03:00
|
|
|
/**
|
2017-03-28 17:24:05 +03:00
|
|
|
* An hidden frame object. It takes care of creating a windowless browser and
|
|
|
|
* passing the window containing a blank XUL <window> back.
|
2015-02-04 08:33:00 +03:00
|
|
|
*/
|
2017-04-03 23:25:16 +03:00
|
|
|
function HiddenFrame() {}
|
2015-02-04 08:33:00 +03:00
|
|
|
|
|
|
|
HiddenFrame.prototype = {
|
|
|
|
_frame: null,
|
2017-03-28 17:24:05 +03:00
|
|
|
_browser: null,
|
|
|
|
_listener: null,
|
|
|
|
_webProgress: null,
|
2015-02-04 08:33:00 +03:00
|
|
|
_deferred: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the |contentWindow| of the hidden frame. Creates the frame if needed.
|
|
|
|
* @returns Promise Returns a promise which is resolved when the hidden frame has finished
|
|
|
|
* loading.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
get() {
|
2015-02-04 08:33:00 +03:00
|
|
|
if (!this._deferred) {
|
|
|
|
this._deferred = PromiseUtils.defer();
|
|
|
|
this._create();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._deferred.promise;
|
|
|
|
},
|
|
|
|
|
2017-04-03 23:25:16 +03:00
|
|
|
/**
|
|
|
|
* Fetch a sync ref to the window inside the frame (needed for the add-on SDK).
|
|
|
|
*/
|
|
|
|
getWindow() {
|
|
|
|
this.get();
|
2018-05-21 04:10:16 +03:00
|
|
|
return this._browser.document.ownerGlobal;
|
2017-04-03 23:25:16 +03:00
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
destroy() {
|
2017-03-28 17:24:05 +03:00
|
|
|
if (this._browser) {
|
|
|
|
if (this._listener) {
|
|
|
|
this._webProgress.removeProgressListener(this._listener);
|
|
|
|
this._listener = null;
|
|
|
|
this._webProgress = null;
|
2015-02-04 08:33:00 +03:00
|
|
|
}
|
|
|
|
this._frame = null;
|
|
|
|
this._deferred = null;
|
|
|
|
|
2017-04-03 23:25:16 +03:00
|
|
|
gAllHiddenFrames.delete(this);
|
2017-03-28 17:24:05 +03:00
|
|
|
this._browser.close();
|
|
|
|
this._browser = null;
|
2015-02-04 08:33:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
_create() {
|
2017-04-03 23:25:16 +03:00
|
|
|
ensureCleanupRegistered();
|
2021-03-02 02:33:14 +03:00
|
|
|
let chromeFlags = Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW;
|
|
|
|
if (Services.appinfo.fissionAutostart) {
|
|
|
|
chromeFlags |= Ci.nsIWebBrowserChrome.CHROME_FISSION_WINDOW;
|
|
|
|
}
|
|
|
|
this._browser = Services.appShell.createWindowlessBrowser(
|
|
|
|
true,
|
|
|
|
chromeFlags
|
|
|
|
);
|
2017-03-28 17:24:05 +03:00
|
|
|
this._browser.QueryInterface(Ci.nsIInterfaceRequestor);
|
2017-04-03 23:25:16 +03:00
|
|
|
gAllHiddenFrames.add(this);
|
2017-03-28 17:24:05 +03:00
|
|
|
this._webProgress = this._browser.getInterface(Ci.nsIWebProgress);
|
|
|
|
this._listener = {
|
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
"nsIWebProgressListener",
|
|
|
|
"nsIWebProgressListener2",
|
|
|
|
"nsISupportsWeakReference",
|
2017-03-28 17:24:05 +03:00
|
|
|
]),
|
|
|
|
};
|
|
|
|
this._listener.onStateChange = (wbp, request, stateFlags, status) => {
|
|
|
|
if (!request) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
|
|
|
|
this._webProgress.removeProgressListener(this._listener);
|
|
|
|
this._listener = null;
|
|
|
|
this._webProgress = null;
|
|
|
|
// Get the window reference via the document.
|
|
|
|
this._frame = this._browser.document.ownerGlobal;
|
|
|
|
this._deferred.resolve(this._frame);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._webProgress.addProgressListener(
|
|
|
|
this._listener,
|
|
|
|
Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT
|
|
|
|
);
|
2018-08-03 03:05:07 +03:00
|
|
|
let docShell = this._browser.docShell;
|
2018-11-05 17:05:19 +03:00
|
|
|
let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
|
2019-06-12 12:07:33 +03:00
|
|
|
docShell.createAboutBlankContentViewer(systemPrincipal, systemPrincipal);
|
2020-05-08 06:28:44 +03:00
|
|
|
let browsingContext = this._browser.browsingContext;
|
|
|
|
browsingContext.useGlobalHistory = false;
|
2019-01-11 14:44:20 +03:00
|
|
|
let loadURIOptions = {
|
|
|
|
triggeringPrincipal: systemPrincipal,
|
|
|
|
};
|
|
|
|
this._browser.loadURI(XUL_PAGE, loadURIOptions);
|
2015-02-04 08:33:00 +03:00
|
|
|
},
|
|
|
|
};
|