Bug 1269347 - Part1 Expose the optional embedded webextension as a builtin SDK module. r=krizsa
authorLuca Greco <lgreco@mozilla.com>
Mon, 19 Sep 2016 13:58:50 +0200
changeset 314412 70c93b940d716bf091c3ba95658aafdeb783386a
parent 314411 2a6c70abee77fd8b967c8e1c4f77ae36619880ff
child 314413 013eb78e6709ad5ccbfb64dfc3980d8b0756081e
push id20574
push usercbook@mozilla.com
push dateTue, 20 Sep 2016 10:05:16 +0000
treeherderfx-team@14705f779a46 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskrizsa
bugs1269347
milestone51.0a1
Bug 1269347 - Part1 Expose the optional embedded webextension as a builtin SDK module. r=krizsa This commit integrates the `webExtension` bootstrap method parameter, which can is set from the XPIProvider when the `hasEmbeddedWebExtension` property is true in the addon install.rdf, into a new "sdk/webextension" SDK module, to make the creation of SDK hybrid addons easier. MozReview-Commit-ID: 4OUPZZyBPJv
addon-sdk/moz.build
addon-sdk/source/lib/sdk/addon/bootstrap.js
addon-sdk/source/lib/sdk/webextension.js
--- a/addon-sdk/moz.build
+++ b/addon-sdk/moz.build
@@ -178,16 +178,20 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] != "gonk
         'source/lib/sdk/windows/tabs-fennec.js',
     ]
 
 EXTRA_JS_MODULES.commonjs += [
     'source/lib/index.js',
     'source/lib/test.js',
 ]
 
+EXTRA_JS_MODULES.commonjs.sdk += [
+    'source/lib/sdk/webextension.js',
+]
+
 EXTRA_JS_MODULES.commonjs.dev += [
     'source/lib/dev/debuggee.js',
     'source/lib/dev/frame-script.js',
     'source/lib/dev/panel.js',
     'source/lib/dev/ports.js',
     'source/lib/dev/theme.js',
     'source/lib/dev/toolbox.js',
     'source/lib/dev/utils.js',
--- a/addon-sdk/source/lib/sdk/addon/bootstrap.js
+++ b/addon-sdk/source/lib/sdk/addon/bootstrap.js
@@ -118,27 +118,30 @@ Bootstrap.prototype = {
         paths: Object.assign({
           "": "resource://gre/modules/commonjs/",
           "devtools/": "resource://devtools/",
           "./": baseURI
         }, readPaths(id)),
         manifest: metadata,
         metadata: metadata,
         modules: {
-          "@test/options": {}
+          "@test/options": {},
         },
         noQuit: prefs.get(`extensions.${id}.sdk.test.no-quit`, false)
       });
       self.loader = loader;
 
       const module = Module("package.json", `${baseURI}package.json`);
       const require = Require(loader, module);
       const main = command === "test" ? "sdk/test/runner" : null;
       const prefsURI = `${baseURI}defaults/preferences/prefs.js`;
 
+      // Init the 'sdk/webextension' module from the bootstrap addon parameter.
+      require("sdk/webextension").initFromBootstrapAddonParam(addon);
+
       const { startup } = require("sdk/addon/runner");
       startup(reason, {loader, main, prefsURI});
     }.bind(this)).catch(error => {
       console.error(`Failed to start ${id} addon`, error);
       throw error;
     });
   },
   shutdown(addon, code) {
new file mode 100644
--- /dev/null
+++ b/addon-sdk/source/lib/sdk/webextension.js
@@ -0,0 +1,43 @@
+/* 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";
+
+module.metadata = {
+  "stability": "experimental"
+};
+
+let webExtension;
+let waitForWebExtensionAPI;
+
+module.exports = {
+  initFromBootstrapAddonParam(data) {
+    if (webExtension) {
+      throw new Error("'sdk/webextension' module has been already initialized");
+    }
+
+    webExtension = data.webExtension;
+  },
+
+  startup() {
+    if (!webExtension) {
+      return Promise.reject(new Error(
+        "'sdk/webextension' module is currently disabled. " +
+        "('hasEmbeddedWebExtension' option is missing or set to false)"
+      ));
+    }
+
+    // NOTE: calling `startup` more than once raises an "Embedded Extension already started"
+    // error, but given that SDK addons are going to have access to the startup method through
+    // an SDK module that can be required in any part of the addon, it will be nicer if any
+    // additional startup calls return the startup promise instead of raising an exception,
+    // so that the SDK addon can access the API object in the other addon modules without the
+    // need to manually pass this promise around.
+    if (!waitForWebExtensionAPI) {
+      waitForWebExtensionAPI = webExtension.startup();
+    }
+
+    return waitForWebExtensionAPI;
+  }
+};