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";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["HiddenFrame"];
|
2015-02-04 08:33:00 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {PromiseUtils} = ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2015-02-04 08:33:00 +03:00
|
|
|
|
2017-07-12 10:53:59 +03:00
|
|
|
const XUL_PAGE = "chrome://global/content/win.xul";
|
2015-02-04 08:33:00 +03:00
|
|
|
|
2017-04-03 23:25:16 +03:00
|
|
|
const gAllHiddenFrames = new WeakSet();
|
|
|
|
|
|
|
|
let cleanupRegistered = false;
|
|
|
|
function ensureCleanupRegistered() {
|
|
|
|
if (!cleanupRegistered) {
|
|
|
|
cleanupRegistered = true;
|
|
|
|
Services.obs.addObserver(function() {
|
|
|
|
for (let hiddenFrame of ChromeUtils.nondeterministicGetWeakSetKeys(gAllHiddenFrames)) {
|
|
|
|
hiddenFrame.destroy();
|
|
|
|
}
|
2017-04-14 22:51:38 +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();
|
2017-03-28 17:24:05 +03:00
|
|
|
this._browser = Services.appShell.createWindowlessBrowser(true);
|
|
|
|
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 = {
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
2017-03-28 17:24:05 +03:00
|
|
|
Ci.nsIWebProgressListener, Ci.nsIWebProgressListener2,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
};
|
|
|
|
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();
|
|
|
|
docShell.createAboutBlankContentViewer(systemPrincipal);
|
2017-04-10 17:55:53 +03:00
|
|
|
docShell.useGlobalHistory = false;
|
2019-01-11 14:44:20 +03:00
|
|
|
let loadURIOptions = {
|
|
|
|
triggeringPrincipal: systemPrincipal,
|
|
|
|
};
|
|
|
|
this._browser.loadURI(XUL_PAGE, loadURIOptions);
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2015-02-04 08:33:00 +03:00
|
|
|
};
|