Bug 1252215 - [webext] Add Embedded Extensions helper to LegacyExtensionsUtils. r=kmag, r=aswan, a=ritu
authorLuca Greco <lgreco@mozilla.com>
Mon, 29 Aug 2016 15:56:02 +0200
changeset 350257 bfd7e6f5db590e2b0b65fb0cb2b465cb761fb3df
parent 350256 e9ef4d944309651c3448f81628bfad3a2a8ca053
child 350258 352666cdee72996c02990b0b19bd5d1d77ba54d0
push id1230
push userjlund@mozilla.com
push dateMon, 31 Oct 2016 18:13:35 +0000
treeherdermozilla-release@5e06e3766db2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag, aswan, ritu
bugs1252215
milestone50.0a2
Bug 1252215 - [webext] Add Embedded Extensions helper to LegacyExtensionsUtils. r=kmag, r=aswan, a=ritu This patch introduces helper for the embedding of a webextension (and new related tests). The new exported helpers are going to be integrated in the XPIProvider to provide the Embedded WebExtension to the Legacy Extensions which have enabled it in their install.rdf MozReview-Commit-ID: 7M1DRkXjGat
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/LegacyExtensionsUtils.jsm
toolkit/components/extensions/test/xpcshell/test_ext_legacy_extension_embedding.js
toolkit/components/extensions/test/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -1443,16 +1443,21 @@ Extension.prototype = extend(Object.crea
       }
 
       if (this.hasShutdown) {
         return;
       }
 
       GlobalManager.init(this);
 
+      // The "startup" Management event sent on the extension instance itself
+      // is emitted just before the Management "startup" event,
+      // and it is used to run code that needs to be executed before
+      // any of the "startup" listeners.
+      this.emit("startup", this);
       Management.emit("startup", this);
 
       return this.runManifest(this.manifest);
     }).then(() => {
       Management.emit("ready", this);
     }).catch(e => {
       dump(`Extension error: ${e.message} ${e.filename || e.fileName}:${e.lineNumber} :: ${e.stack || new Error().stack}\n`);
       Cu.reportError(e);
--- a/toolkit/components/extensions/LegacyExtensionsUtils.jsm
+++ b/toolkit/components/extensions/LegacyExtensionsUtils.jsm
@@ -96,9 +96,163 @@ var LegacyExtensionContext = class exten
   /**
    * The LegacyExtensionContext is not a visible context.
    */
   get externallyVisible() {
     return false;
   }
 };
 
-this.LegacyExtensionsUtils = {};
+var EmbeddedExtensionManager;
+
+/**
+ * Instances of this class are used internally by the exported EmbeddedWebExtensionsUtils
+ * to manage the embedded webextension instance and the related LegacyExtensionContext
+ * instance used to exchange messages with it.
+ */
+class EmbeddedExtension {
+  /**
+   * Create a new EmbeddedExtension given the add-on id and the base resource URI of the
+   * container add-on (the webextension resources will be loaded from the "webextension/"
+   * subdir of the base resource URI for the legacy extension add-on).
+   *
+   * @param {Object} containerAddonParams
+   *   An object with the following properties:
+   * @param {string} containerAddonParams.id
+   *   The Add-on id of the Legacy Extension which will contain the embedded webextension.
+   * @param {nsIURI} containerAddonParams.resourceURI
+   *   The nsIURI of the Legacy Extension container add-on.
+   */
+  constructor({id, resourceURI}) {
+    this.addonId = id;
+    this.resourceURI = resourceURI;
+
+    // Setup status flag.
+    this.started = false;
+  }
+
+  /**
+   * Start the embedded webextension.
+   *
+   * @returns {Promise<LegacyContextAPI>} A promise which resolve to the API exposed to the
+   *   legacy context.
+   */
+  startup() {
+    if (this.started) {
+      return Promise.reject(new Error("This embedded extension has already been started"));
+    }
+
+    // Setup the startup promise.
+    this.startupPromise = new Promise((resolve, reject) => {
+      let embeddedExtensionURI = Services.io.newURI("webextension/", null, this.resourceURI);
+
+      // This is the instance of the WebExtension embedded in the hybrid add-on.
+      this.extension = new Extension({
+        id: this.addonId,
+        resourceURI: embeddedExtensionURI,
+      });
+
+      // This callback is register to the "startup" event, emitted by the Extension instance
+      // after the extension manifest.json has been loaded without any errors, but before
+      // starting any of the defined contexts (which give the legacy part a chance to subscribe
+      // runtime.onMessage/onConnect listener before the background page has been loaded).
+      const onBeforeStarted = () => {
+        this.extension.off("startup", onBeforeStarted);
+
+        // Resolve the startup promise and reset the startupError.
+        this.started = true;
+        this.startupPromise = null;
+
+        // Create the legacy extension context, the legacy container addon
+        // needs to use it before the embedded webextension startup,
+        // because it is supposed to be used during the legacy container startup
+        // to subscribe its message listeners (which are supposed to be able to
+        // receive any message that the embedded part can try to send to it
+        // during its startup).
+        this.context = new LegacyExtensionContext(this.extension, {
+          url: this.resourceURI.resolve("/"),
+        });
+
+        // Destroy the LegacyExtensionContext cloneScope when
+        // the embedded webextensions is unloaded.
+        this.extension.callOnClose({
+          close: () => {
+            this.context.unload();
+          },
+        });
+
+        // resolve startupPromise to execute any pending shutdown that has been
+        // chained to it.
+        resolve(this.context.api);
+      };
+
+      this.extension.on("startup", onBeforeStarted);
+
+      // Run ambedded extension startup and catch any error during embedded extension
+      // startup.
+      this.extension.startup().catch((err) => {
+        this.started = false;
+        this.startupPromise = null;
+        this.extension.off("startup", onBeforeStarted);
+
+        reject(err);
+      });
+    });
+
+    return this.startupPromise;
+  }
+
+  /**
+   * Shuts down the embedded webextension.
+   *
+   * @returns {Promise<void>} a promise that is resolved when the shutdown has been done
+   */
+  shutdown() {
+    EmbeddedExtensionManager.untrackEmbeddedExtension(this);
+
+    // If there is a pending startup,  wait to be completed and then shutdown.
+    if (this.startupPromise) {
+      return this.startupPromise.then(() => {
+        this.extension.shutdown();
+      });
+    }
+
+    // Run shutdown now if the embedded webextension has been correctly started
+    if (this.extension && this.started && !this.extension.hasShutdown) {
+      this.extension.shutdown();
+    }
+
+    return Promise.resolve();
+  }
+}
+
+// Keep track on the created EmbeddedExtension instances and destroy
+// them when their container addon is going to be disabled or uninstalled.
+EmbeddedExtensionManager = {
+  // Map of the existent EmbeddedExtensions instances by addon id.
+  embeddedExtensionsByAddonId: new Map(),
+
+  untrackEmbeddedExtension(embeddedExtensionInstance) {
+    // Remove this instance from the tracked embedded extensions
+    let id = embeddedExtensionInstance.addonId;
+    if (this.embeddedExtensionsByAddonId.get(id) == embeddedExtensionInstance) {
+      this.embeddedExtensionsByAddonId.delete(id);
+    }
+  },
+
+  getEmbeddedExtensionFor({id, resourceURI}) {
+    let embeddedExtension = this.embeddedExtensionsByAddonId.get(id);
+
+    if (!embeddedExtension) {
+      embeddedExtension = new EmbeddedExtension({id, resourceURI});
+      // Keep track of the embedded extension instance.
+      this.embeddedExtensionsByAddonId.set(id, embeddedExtension);
+    }
+
+    return embeddedExtension;
+  },
+};
+
+this.LegacyExtensionsUtils = {
+  getEmbeddedExtensionFor: (addon) => {
+    return EmbeddedExtensionManager.getEmbeddedExtensionFor(addon);
+  },
+};
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_legacy_extension_embedding.js
@@ -0,0 +1,190 @@
+"use strict";
+
+/* globals browser */
+
+Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm");
+
+// Import EmbeddedExtensionManager to be able to check that the
+// tacked instances are cleared after the embedded extension shutdown.
+const {
+  EmbeddedExtensionManager,
+} = Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm", {});
+
+/**
+ * This test case ensures that the LegacyExtensionsUtils.EmbeddedExtension:
+ *  - load the embedded webextension resources from a "/webextension/" dir
+ *    inside the XPI.
+ *  - EmbeddedExtension.prototype.api returns an API object which exposes
+ *    a working `runtime.onConnect` event object (e.g. the API can receive a port
+ *    when the embedded webextension is started  and it can exchange messages
+ *    with the background page).
+ *  - EmbeddedExtension.prototype.startup/shutdown methods manage the embedded
+ *    webextension lifecycle as expected.
+ */
+add_task(function* test_embedded_webextension_utils() {
+  function backgroundScript() {
+    let port = browser.runtime.connect();
+
+    port.onMessage.addListener((msg) => {
+      if (msg == "legacy_extension -> webextension") {
+        port.postMessage("webextension -> legacy_extension");
+        port.disconnect();
+      }
+    });
+  }
+
+  const id = "@test.embedded.web.extension";
+
+  // Extensions.generateXPI is used here (and in the other hybrid addons tests in this same
+  // test dir) to be able to generate an xpi with the directory layout that we expect from
+  // an hybrid legacy+webextension addon (where all the embedded webextension resources are
+  // loaded from a 'webextension/' directory).
+  let fakeHybridAddonFile = Extension.generateXPI(id, {
+    files: {
+      "webextension/manifest.json": {
+        applications: {gecko: {id}},
+        name: "embedded webextension name",
+        manifest_version: 2,
+        version: "1.0",
+        background: {
+          scripts: ["bg.js"],
+        },
+      },
+      "webextension/bg.js": `new ${backgroundScript}`,
+    },
+  });
+
+  // Remove the generated xpi file and flush the its jar cache
+  // on cleanup.
+  do_register_cleanup(() => {
+    Services.obs.notifyObservers(fakeHybridAddonFile, "flush-cache-entry", null);
+    fakeHybridAddonFile.remove(false);
+  });
+
+  let fileURI = Services.io.newFileURI(fakeHybridAddonFile);
+  let resourceURI = Services.io.newURI(`jar:${fileURI.spec}!/`, null, null);
+
+  let embeddedExtension = LegacyExtensionsUtils.getEmbeddedExtensionFor({
+    id, resourceURI,
+  });
+
+  ok(embeddedExtension, "Got the embeddedExtension object");
+
+  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 1,
+        "Got the expected number of tracked embedded extension instances");
+
+  do_print("waiting embeddedExtension.startup");
+  let embeddedExtensionAPI = yield embeddedExtension.startup();
+  ok(embeddedExtensionAPI, "Got the embeddedExtensionAPI object");
+
+  let waitConnectPort = new Promise(resolve => {
+    let {browser} = embeddedExtensionAPI;
+    browser.runtime.onConnect.addListener(port => {
+      resolve(port);
+    });
+  });
+
+  let port = yield waitConnectPort;
+
+  ok(port, "Got the Port API object");
+
+  let waitPortMessage = new Promise(resolve => {
+    port.onMessage.addListener((msg) => {
+      resolve(msg);
+    });
+  });
+
+  port.postMessage("legacy_extension -> webextension");
+
+  let msg = yield waitPortMessage;
+
+  equal(msg, "webextension -> legacy_extension",
+     "LegacyExtensionContext received the expected message from the webextension");
+
+  let waitForDisconnect = new Promise(resolve => {
+    port.onDisconnect.addListener(resolve);
+  });
+
+  do_print("Wait for the disconnect port event");
+  yield waitForDisconnect;
+  do_print("Got the disconnect port event");
+
+  yield embeddedExtension.shutdown();
+
+  equal(EmbeddedExtensionManager.embeddedExtensionsByAddonId.size, 0,
+        "EmbeddedExtension instances has been untracked from the EmbeddedExtensionManager");
+});
+
+function* createManifestErrorTestCase(id, xpi, expectedError) {
+  // Remove the generated xpi file and flush the its jar cache
+  // on cleanup.
+  do_register_cleanup(() => {
+    Services.obs.notifyObservers(xpi, "flush-cache-entry", null);
+    xpi.remove(false);
+  });
+
+  let fileURI = Services.io.newFileURI(xpi);
+  let resourceURI = Services.io.newURI(`jar:${fileURI.spec}!/`, null, null);
+
+  let embeddedExtension = LegacyExtensionsUtils.getEmbeddedExtensionFor({
+    id, resourceURI,
+  });
+
+  yield Assert.rejects(embeddedExtension.startup(), expectedError,
+                       "embedded extension startup rejected");
+
+  // Shutdown a "never-started" addon with an embedded webextension should not
+  // raise any exception, and if it does this test will fail.
+  yield embeddedExtension.shutdown();
+}
+
+add_task(function* test_startup_error_empty_manifest() {
+  const id = "empty-manifest@test.embedded.web.extension";
+  const files = {
+    "webextension/manifest.json": ``,
+  };
+  const expectedError = "(NS_BASE_STREAM_CLOSED)";
+
+  let fakeHybridAddonFile = Extension.generateXPI(id, {files});
+
+  yield createManifestErrorTestCase(id, fakeHybridAddonFile, expectedError);
+});
+
+add_task(function* test_startup_error_invalid_json_manifest() {
+  const id = "invalid-json-manifest@test.embedded.web.extension";
+  const files = {
+    "webextension/manifest.json": `{ "name": }`,
+  };
+  const expectedError = "JSON.parse:";
+
+  let fakeHybridAddonFile = Extension.generateXPI(id, {files});
+
+  yield createManifestErrorTestCase(id, fakeHybridAddonFile, expectedError);
+});
+
+add_task(function* test_startup_error_blocking_validation_errors() {
+  const id = "blocking-manifest-validation-error@test.embedded.web.extension";
+  const files = {
+    "webextension/manifest.json": {
+      name: "embedded webextension name",
+      manifest_version: 2,
+      version: "1.0",
+      background: {
+        scripts: {},
+      },
+    },
+  };
+
+  function expectedError(actual) {
+    if (actual.errors && actual.errors.length == 1 &&
+        actual.errors[0].startsWith("Reading manifest:")) {
+      return true;
+    }
+
+    return false;
+  }
+
+  let fakeHybridAddonFile = Extension.generateXPI(id, {files});
+
+  yield createManifestErrorTestCase(id, fakeHybridAddonFile, expectedError);
+});
--- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini
@@ -39,12 +39,13 @@ skip-if = os == "android"
 [test_ext_onmessage_removelistener.js]
 [test_ext_runtime_getPlatformInfo.js]
 [test_ext_runtime_sendMessage.js]
 [test_ext_schemas.js]
 [test_ext_simple.js]
 [test_ext_storage.js]
 [test_getAPILevelForWindow.js]
 [test_ext_legacy_extension_context.js]
+[test_ext_legacy_extension_embedding.js]
 [test_locale_converter.js]
 [test_locale_data.js]
 [test_native_messaging.js]
 skip-if = os == "android"