Bug 1224105 - [webext] Use <browser> element for background page (r=kmag)

This commit is contained in:
Bill McCloskey 2015-11-11 21:08:43 -08:00
Родитель ecabf2935c
Коммит c15d73943a
1 изменённых файлов: 27 добавлений и 7 удалений

Просмотреть файл

@ -13,14 +13,15 @@ function BackgroundPage(options, extension) {
this.scripts = options.scripts || [];
this.page = options.page || null;
this.contentWindow = null;
this.chromeWebNav = null;
this.webNav = null;
this.context = null;
}
BackgroundPage.prototype = {
build() {
let webNav = Services.appShell.createWindowlessBrowser(false);
this.webNav = webNav;
let chromeWebNav = Services.appShell.createWindowlessBrowser(true);
this.chromeWebNav = chromeWebNav;
let url;
if (this.page) {
@ -38,20 +39,35 @@ BackgroundPage.prototype = {
let uri = Services.io.newURI(url, null, null);
let principal = this.extension.createPrincipal(uri);
let interfaceRequestor = webNav.QueryInterface(Ci.nsIInterfaceRequestor);
let docShell = interfaceRequestor.getInterface(Ci.nsIDocShell);
let system = Services.scriptSecurityManager.getSystemPrincipal();
let interfaceRequestor = chromeWebNav.QueryInterface(Ci.nsIInterfaceRequestor);
let chromeShell = interfaceRequestor.getInterface(Ci.nsIDocShell);
chromeShell.createAboutBlankContentViewer(system);
let chromeDoc = chromeWebNav.document;
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
let browser = chromeDoc.createElementNS(XUL_NS, "browser");
browser.setAttribute("type", "content");
browser.setAttribute("disableglobalhistory", "true");
chromeDoc.body.appendChild(browser);
let frameLoader = browser.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader;
let docShell = frameLoader.docShell;
this.context = new ExtensionPage(this.extension, {type: "background", docShell, uri});
GlobalManager.injectInDocShell(docShell, this.extension, this.context);
docShell.createAboutBlankContentViewer(principal);
let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
this.webNav = webNav;
webNav.loadURI(url, 0, null, null, null);
let window = webNav.document.defaultView;
this.contentWindow = window;
this.context.contentWindow = window;
webNav.loadURI(url, 0, null, null, null);
// TODO: Right now we run onStartup after the background page
// finishes. See if this is what Chrome does.
let loadListener = event => {
@ -59,6 +75,7 @@ BackgroundPage.prototype = {
return;
}
event.currentTarget.removeEventListener("load", loadListener, true);
if (this.scripts) {
let doc = window.document;
for (let script of this.scripts) {
@ -80,7 +97,7 @@ BackgroundPage.prototype = {
this.extension.onStartup();
}
};
window.windowRoot.addEventListener("load", loadListener, true);
browser.addEventListener("load", loadListener, true);
},
shutdown() {
@ -88,6 +105,9 @@ BackgroundPage.prototype = {
// setTimeouts or other callbacks.
this.webNav.loadURI("about:blank", 0, null, null, null);
this.webNav = null;
this.chromeWebNav.loadURI("about:blank", 0, null, null, null);
this.chromeWebNav = null;
},
};