Bug 1331742 - Part 2 - Create a module for managing browser actions similar to PageActions.jsm r=sebastian
authorMatthew Wein <mwein@mozilla.com>
Sat, 25 Mar 2017 22:36:05 -0700
changeset 354006 c91b14d9ad2a408f45b87cb485211966681e5d8b
parent 354005 32ab1a3d736f5f14bc6aa5cb0fc033dd3d1e7feb
child 354007 31f4919d1f6c44d8a3630260071427f477530f54
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)
reviewerssebastian
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 2 - Create a module for managing browser actions similar to PageActions.jsm r=sebastian MozReview-Commit-ID: 2epdMD75e84
mobile/android/modules/BrowserActions.jsm
mobile/android/modules/PageActions.jsm
mobile/android/modules/moz.build
new file mode 100644
--- /dev/null
+++ b/mobile/android/modules/BrowserActions.jsm
@@ -0,0 +1,113 @@
+/* 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";
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Messaging.jsm");
+
+this.EXPORTED_SYMBOLS = ["BrowserActions"];
+
+var BrowserActions = {
+  _browserActions: {},
+
+  _initialized: false,
+
+  /**
+   * Registers the listeners only if they have not been initialized
+   * already and there is at least one browser action.
+   */
+  _maybeRegisterListeners() {
+    if (!this._initialized && Object.keys(this._browserActions).length) {
+      this._initialized = true;
+      EventDispatcher.instance.registerListener(this, "Menu:Clicked");
+    }
+  },
+
+  /**
+   * Unregisters the listeners if they are already initizliaed and
+   * all of the browser actions have been removed.
+   */
+  _maybeUnregisterListeners: function() {
+    if (this._initialized && !Object.keys(this._browserActions).length) {
+      this._initialized = false;
+      EventDispatcher.instance.unregisterListener(this, "Menu:Clicked");
+    }
+  },
+
+  /**
+   * Called when a browser action is clicked on.
+   * @param {string} event The name of the event, which should always
+   *    be "Menu:Clicked".
+   * @param {Object} data An object containing information about the
+   *    browser action, which in this case should contain an `item`
+   *    property which is browser action's ID.
+   */
+  onEvent(event, data) {
+    if (event !== "Menu:Clicked") {
+      throw new Error(`Expected "Menu:Clicked" event - received "${event}" instead`);
+    }
+
+    let browserAction = this._browserActions[data.item];
+    if (!browserAction) {
+      throw new Error(`No browser action found with id ${data.item}`);
+    }
+    browserAction.onClicked();
+  },
+
+  /**
+   * Registers a new browser action.
+   * @param {Object} browserAction The browser action to add.
+   */
+  register(browserAction) {
+    EventDispatcher.instance.sendRequest({
+      type: "Menu:Add",
+      id: browserAction.id,
+      name: browserAction.name,
+    });
+
+    this._browserActions[browserAction.id] = browserAction;
+    this._maybeRegisterListeners();
+  },
+
+  /**
+   * Checks to see if the browser action is shown. Used for testing only.
+   * @param {string} id The ID of the browser action.
+   * @returns True if the browser action is shown; false otherwise.
+   */
+  isShown: function(id) {
+    return !!this._browserActions[id];
+  },
+
+  /**
+   * Synthesizes a click on the browser action. Used for testing only.
+   * @param {string} id The ID of the browser action.
+   */
+  synthesizeClick: function(id) {
+    let browserAction = this._browserActions[id];
+    if (!browserAction) {
+      throw new Error(`No browser action found with id ${id}`);
+    }
+    browserAction.onClicked();
+  },
+
+  /**
+   * Unregisters the browser action with the specified ID.
+   * @param {string} id The browser action ID.
+   */
+  unregister(id) {
+    let browserAction = this._browserActions[id];
+    if (!browserAction) {
+      throw new Error(`No BrowserAction with ID ${id} was found`);
+    }
+    EventDispatcher.instance.sendRequest({
+      type: "Menu:Remove",
+      id,
+    });
+    delete this._browserActions[id];
+    this._maybeUnregisterListeners();
+  }
+}
\ No newline at end of file
--- a/mobile/android/modules/PageActions.jsm
+++ b/mobile/android/modules/PageActions.jsm
@@ -29,31 +29,31 @@ function resolveGeckoURI(aURI) {
     return handler.resolveURI(Services.io.newURI(aURI));
   }
   return aURI;
 }
 
 var PageActions = {
   _items: { },
 
-  _inited: false,
+  _initialized: false,
 
-  _maybeInit: function() {
-    if (!this._inited && Object.keys(this._items).length > 0) {
-      this._inited = true;
+  _maybeInitialize: function() {
+    if (!this._initialized && Object.keys(this._items).length) {
+      this._initialized = true;
       EventDispatcher.instance.registerListener(this, [
         "PageActions:Clicked",
         "PageActions:LongClicked",
       ]);
     }
   },
 
-  _maybeUninit: function() {
-    if (this._inited && Object.keys(this._items).length == 0) {
-      this._inited = false;
+  _maybeUninitialize: function() {
+    if (this._initialized && !Object.keys(this._items).length) {
+      this._initialized = false;
       EventDispatcher.instance.unregisterListener(this, [
         "PageActions:Clicked",
         "PageActions:LongClicked",
       ]);
     }
   },
 
   onEvent: function(event, data, callback) {
@@ -96,22 +96,22 @@ var PageActions = {
     if (aOptions.clickCallback) {
       this._items[id].clickCallback = aOptions.clickCallback;
     }
 
     if (aOptions.longClickCallback) {
       this._items[id].longClickCallback = aOptions.longClickCallback;
     }
 
-    this._maybeInit();
+    this._maybeInitialize();
     return id;
   },
 
   remove: function(id) {
     EventDispatcher.instance.sendRequest({
       type: "PageActions:Remove",
       id: id
     });
 
     delete this._items[id];
-    this._maybeUninit();
+    this._maybeUninitialize();
   }
 }
--- a/mobile/android/modules/moz.build
+++ b/mobile/android/modules/moz.build
@@ -16,16 +16,17 @@ with Files('HomeProvider.jsm'):
 
 with Files('geckoview/**'):
     BUG_COMPONENT = ('Firefox for Android', 'GeckoView')
 
 DIRS += ['geckoview']
 
 EXTRA_JS_MODULES += [
     'Accounts.jsm',
+    'BrowserActions.jsm',
     'dbg-browser-actors.js',
     'DelayedInit.jsm',
     'DownloadNotifications.jsm',
     'FxAccountsWebChannel.jsm',
     'HelperApps.jsm',
     'Home.jsm',
     'HomeProvider.jsm',
     'JavaAddonManager.jsm',