Bug 1287007 - Move background page API logic to child r=billm
MozReview-Commit-ID: iGROahhkhn
This commit is contained in:
@@ -724,9 +724,6 @@ GlobalManager = {
|
||||
let uri = document.documentURIObject;
|
||||
|
||||
let context = new ExtensionContext(extension, {viewType, contentWindow, uri, docShell});
|
||||
if (viewType == "background") {
|
||||
this._initializeBackgroundPage(contentWindow);
|
||||
}
|
||||
|
||||
let innerWindowID = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
||||
.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
||||
@@ -740,28 +737,6 @@ GlobalManager = {
|
||||
};
|
||||
Services.obs.addObserver(onUnload, "inner-window-destroyed", false);
|
||||
},
|
||||
|
||||
_initializeBackgroundPage(contentWindow) {
|
||||
// Override the `alert()` method inside background windows;
|
||||
// we alias it to console.log().
|
||||
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1203394
|
||||
let alertDisplayedWarning = false;
|
||||
let alertOverwrite = text => {
|
||||
if (!alertDisplayedWarning) {
|
||||
require("devtools/client/framework/devtools-browser");
|
||||
|
||||
let hudservice = require("devtools/client/webconsole/hudservice");
|
||||
hudservice.openBrowserConsoleOrFocus();
|
||||
|
||||
contentWindow.console.warn("alert() is not supported in background windows; please use console.log instead.");
|
||||
|
||||
alertDisplayedWarning = true;
|
||||
}
|
||||
|
||||
contentWindow.console.log(text);
|
||||
};
|
||||
Cu.exportFunction(alertOverwrite, contentWindow, {defineAs: "alert"});
|
||||
},
|
||||
};
|
||||
|
||||
// Represents the data contained in an extension, contained either
|
||||
|
||||
@@ -178,6 +178,10 @@ this.ExtensionContext = class extends BaseContext {
|
||||
Schemas.inject(browserObj, this.childManager);
|
||||
Schemas.inject(chromeObj, chromeApiWrapper);
|
||||
|
||||
if (viewType == "background") {
|
||||
apiManager.global.initializeBackgroundPage(contentWindow);
|
||||
}
|
||||
|
||||
if (this.externallyVisible) {
|
||||
this.extension.views.add(this);
|
||||
}
|
||||
|
||||
@@ -143,20 +143,3 @@ extensions.on("shutdown", (type, extension) => {
|
||||
}
|
||||
});
|
||||
/* eslint-enable mozilla/balanced-listeners */
|
||||
|
||||
extensions.registerSchemaAPI("extension", "addon_parent", context => {
|
||||
let {extension} = context;
|
||||
return {
|
||||
extension: {
|
||||
getBackgroundPage: function() {
|
||||
return backgroundPagesMap.get(extension).contentWindow;
|
||||
},
|
||||
},
|
||||
|
||||
runtime: {
|
||||
getBackgroundPage() {
|
||||
return context.cloneScope.Promise.resolve(backgroundPagesMap.get(extension).contentWindow);
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
44
toolkit/components/extensions/ext-c-backgroundPage.js
Normal file
44
toolkit/components/extensions/ext-c-backgroundPage.js
Normal file
@@ -0,0 +1,44 @@
|
||||
"use strict";
|
||||
|
||||
global.initializeBackgroundPage = (contentWindow) => {
|
||||
// Override the `alert()` method inside background windows;
|
||||
// we alias it to console.log().
|
||||
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1203394
|
||||
let alertDisplayedWarning = false;
|
||||
let alertOverwrite = text => {
|
||||
if (!alertDisplayedWarning) {
|
||||
require("devtools/client/framework/devtools-browser");
|
||||
|
||||
let hudservice = require("devtools/client/webconsole/hudservice");
|
||||
hudservice.openBrowserConsoleOrFocus();
|
||||
|
||||
contentWindow.console.warn("alert() is not supported in background windows; please use console.log instead.");
|
||||
|
||||
alertDisplayedWarning = true;
|
||||
}
|
||||
|
||||
contentWindow.console.log(text);
|
||||
};
|
||||
Cu.exportFunction(alertOverwrite, contentWindow, {defineAs: "alert"});
|
||||
};
|
||||
|
||||
extensions.registerSchemaAPI("extension", "addon_child", context => {
|
||||
function getBackgroundPage() {
|
||||
for (let view of context.extension.views) {
|
||||
if (view.viewType == "background") {
|
||||
return view.contentWindow;
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
extension: {
|
||||
getBackgroundPage,
|
||||
},
|
||||
|
||||
runtime: {
|
||||
getBackgroundPage() {
|
||||
return context.cloneScope.Promise.resolve(getBackgroundPage());
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -23,6 +23,7 @@ category webextension-scripts-content test chrome://extensions/content/ext-c-tes
|
||||
category webextension-scripts-content storage chrome://extensions/content/ext-c-storage.js
|
||||
|
||||
# scripts that must run in the same process as addon code.
|
||||
category webextension-scripts-addon backgroundPage chrome://extensions/content/ext-c-backgroundPage.js
|
||||
category webextension-scripts-addon extension chrome://extensions/content/ext-c-extension.js
|
||||
category webextension-scripts-addon i18n chrome://extensions/content/ext-i18n.js
|
||||
category webextension-scripts-addon runtime chrome://extensions/content/ext-c-runtime.js
|
||||
|
||||
@@ -20,6 +20,7 @@ toolkit.jar:
|
||||
content/extensions/ext-storage.js
|
||||
content/extensions/ext-test.js
|
||||
content/extensions/ext-topSites.js
|
||||
content/extensions/ext-c-backgroundPage.js
|
||||
content/extensions/ext-c-extension.js
|
||||
content/extensions/ext-c-runtime.js
|
||||
content/extensions/ext-c-storage.js
|
||||
|
||||
Reference in New Issue
Block a user