Bug 1331742 - Part 3 - Create and register ext-browserAction.js r=mixedpuppy
authorMatthew Wein <mwein@mozilla.com>
Wed, 19 Apr 2017 21:44:41 -0400
changeset 354007 31f4919d1f6c44d8a3630260071427f477530f54
parent 354006 c91b14d9ad2a408f45b87cb485211966681e5d8b
child 354008 753e71053540b6845dd6c4174f0c61df9f5e9856
push id31685
push userkwierso@gmail.com
push dateThu, 20 Apr 2017 21:45:29 +0000
treeherdermozilla-central@5e3dc7e1288a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1331742
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1331742 - Part 3 - Create and register ext-browserAction.js r=mixedpuppy MozReview-Commit-ID: CGR4689b6oo
mobile/android/components/extensions/ext-android.js
mobile/android/components/extensions/ext-browserAction.js
mobile/android/components/extensions/jar.mn
--- a/mobile/android/components/extensions/ext-android.js
+++ b/mobile/android/components/extensions/ext-android.js
@@ -42,16 +42,25 @@ extensions.on("page-shutdown", (type, co
       }
     }
   }
 });
 /* eslint-enable mozilla/balanced-listeners */
 
 
 extensions.registerModules({
+  browserAction: {
+    url: "chrome://browser/content/ext-browserAction.js",
+    schema: "chrome://browser/content/schemas/browser_action.json",
+    scopes: ["addon_parent"],
+    manifest: ["browser_action"],
+    paths: [
+      ["browserAction"],
+    ],
+  },
   pageAction: {
     url: "chrome://browser/content/ext-pageAction.js",
     schema: "chrome://browser/content/schemas/page_action.json",
     scopes: ["addon_parent"],
     manifest: ["page_action"],
     paths: [
       ["pageAction"],
     ],
new file mode 100644
--- /dev/null
+++ b/mobile/android/components/extensions/ext-browserAction.js
@@ -0,0 +1,78 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
+                                  "resource://devtools/shared/event-emitter.js");
+
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+                                  "resource://gre/modules/Services.jsm");
+
+// Import the android BrowserActions module.
+XPCOMUtils.defineLazyModuleGetter(this, "BrowserActions",
+                                  "resource://gre/modules/BrowserActions.jsm");
+
+// WeakMap[Extension -> BrowserAction]
+var browserActionMap = new WeakMap();
+
+class BrowserAction {
+  constructor(options, extension) {
+    this.id = `{${extension.uuid}}`;
+    this.name = options.default_title || extension.name;
+    BrowserActions.register(this);
+    EventEmitter.decorate(this);
+  }
+
+  /**
+   * Required by the BrowserActions module. This event will get
+   * called whenever the browser action is clicked on.
+   */
+  onClicked() {
+    this.emit("click", tabTracker.activeTab);
+  }
+
+  /**
+   * Unregister the browser action from the BrowserActions module.
+   */
+  shutdown() {
+    BrowserActions.unregister(this.id);
+  }
+}
+
+this.browserAction = class extends ExtensionAPI {
+  onManifestEntry(entryName) {
+    let {extension} = this;
+    let {manifest} = extension;
+
+    let browserAction = new BrowserAction(manifest.browser_action, extension);
+    browserActionMap.set(extension, browserAction);
+  }
+
+  onShutdown(reason) {
+    let {extension} = this;
+
+    if (browserActionMap.has(extension)) {
+      browserActionMap.get(extension).shutdown();
+      browserActionMap.delete(extension);
+    }
+  }
+
+  getAPI(context) {
+    const {extension} = context;
+    const {tabManager} = extension;
+
+    return {
+      browserAction: {
+        onClicked: new SingletonEventManager(context, "browserAction.onClicked", fire => {
+          let listener = (event, tab) => {
+            fire.async(tabManager.convert(tab));
+          };
+          browserActionMap.get(extension).on("click", listener);
+          return () => {
+            browserActionMap.get(extension).off("click", listener);
+          };
+        }).api(),
+      },
+    };
+  }
+};
--- a/mobile/android/components/extensions/jar.mn
+++ b/mobile/android/components/extensions/jar.mn
@@ -1,11 +1,12 @@
 # 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/.
 
 chrome.jar:
     content/ext-android.js
     content/ext-c-android.js
     content/ext-c-tabs.js
+    content/ext-browserAction.js
     content/ext-pageAction.js
     content/ext-tabs.js
     content/ext-utils.js