Bug 1287007 - Move background page API logic to child r=billm
authorRob Wu <rob@robwu.nl>
Mon, 05 Sep 2016 22:34:44 -0700
changeset 428707 1ed5b81ab606d9c1c9cf737b9d6ffbb50378799f
parent 428706 cb7fa050a6bf4cf499a165b2b9ebe7f33c06f518
child 428708 f86e5930ef497a5cdce73f04d3f16fa73871d144
push id33405
push userbcampen@mozilla.com
push dateMon, 24 Oct 2016 15:32:53 +0000
reviewersbillm
bugs1287007
milestone52.0a1
Bug 1287007 - Move background page API logic to child r=billm MozReview-Commit-ID: iGROahhkhn
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/ExtensionChild.jsm
toolkit/components/extensions/ext-backgroundPage.js
toolkit/components/extensions/ext-c-backgroundPage.js
toolkit/components/extensions/extensions-toolkit.manifest
toolkit/components/extensions/jar.mn
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -719,54 +719,29 @@ GlobalManager = {
       // pop-ups.
       viewType = "popup";
     }
 
     let extension = this.extensionMap.get(id);
     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;
 
     let onUnload = subject => {
       let windowId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
       if (windowId == innerWindowID) {
         Services.obs.removeObserver(onUnload, "inner-window-destroyed");
         context.unload();
       }
     };
     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
 // in a directory or a zip file, which may or may not be installed.
 // This class implements the functionality of the Extension class,
 // primarily related to manifest parsing and localization, which is
 // useful prior to extension installation or initialization.
 //
--- a/toolkit/components/extensions/ExtensionChild.jsm
+++ b/toolkit/components/extensions/ExtensionChild.jsm
@@ -173,16 +173,20 @@ this.ExtensionContext = class extends Ba
     let chromeApiWrapper = Object.create(this.childManager);
     chromeApiWrapper.isChromeCompat = true;
 
     let browserObj = Cu.createObjectIn(contentWindow, {defineAs: "browser"});
     let chromeObj = Cu.createObjectIn(contentWindow, {defineAs: "chrome"});
     Schemas.inject(browserObj, this.childManager);
     Schemas.inject(chromeObj, chromeApiWrapper);
 
+    if (viewType == "background") {
+      apiManager.global.initializeBackgroundPage(contentWindow);
+    }
+
     if (this.externallyVisible) {
       this.extension.views.add(this);
     }
   }
 
   get cloneScope() {
     return this.contentWindow;
   }
--- a/toolkit/components/extensions/ext-backgroundPage.js
+++ b/toolkit/components/extensions/ext-backgroundPage.js
@@ -138,25 +138,8 @@ extensions.on("manifest_background", (ty
 
 extensions.on("shutdown", (type, extension) => {
   if (backgroundPagesMap.has(extension)) {
     backgroundPagesMap.get(extension).shutdown();
     backgroundPagesMap.delete(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);
-      },
-    },
-  };
-});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/ext-c-backgroundPage.js
@@ -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());
+      },
+    },
+  };
+});
--- a/toolkit/components/extensions/extensions-toolkit.manifest
+++ b/toolkit/components/extensions/extensions-toolkit.manifest
@@ -18,16 +18,17 @@ category webextension-scripts topSites c
 # scripts specific for content process.
 category webextension-scripts-content extension chrome://extensions/content/ext-c-extension.js
 category webextension-scripts-content i18n chrome://extensions/content/ext-i18n.js
 category webextension-scripts-content runtime chrome://extensions/content/ext-c-runtime.js
 category webextension-scripts-content test chrome://extensions/content/ext-c-test.js
 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
 category webextension-scripts-addon test chrome://extensions/content/ext-c-test.js
 category webextension-scripts-addon storage chrome://extensions/content/ext-c-storage.js
 
 # schemas
 category webextension-schemas alarms chrome://extensions/content/schemas/alarms.json
--- a/toolkit/components/extensions/jar.mn
+++ b/toolkit/components/extensions/jar.mn
@@ -15,12 +15,13 @@ toolkit.jar:
     content/extensions/ext-idle.js
     content/extensions/ext-webRequest.js
     content/extensions/ext-webNavigation.js
     content/extensions/ext-runtime.js
     content/extensions/ext-extension.js
     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
     content/extensions/ext-c-test.js