Bug 1331742 - Part 2 - Create a module for managing browser actions similar to PageActions.jsm r=sebastian

MozReview-Commit-ID: 2epdMD75e84

--HG--
extra : rebase_source : 7ba699ee966f4fb2e495592f45bc4c612e4a6070
This commit is contained in:
Matthew Wein 2017-05-11 14:41:33 -04:00
Родитель d17da2598c
Коммит 3e6613bfb0
3 изменённых файлов: 126 добавлений и 9 удалений

Просмотреть файл

@ -0,0 +1,116 @@
/* 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,
_nextMenuId: 0,
/**
* 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:BrowserActionClicked");
}
},
/**
* 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:BrowserActionClicked");
}
},
/**
* Called when a browser action is clicked on.
* @param {string} event The name of the event, which should always
* be "Menu:BrowserActionClicked".
* @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:BrowserActionClicked") {
throw new Error(`Expected "Menu:BrowserActionClicked" 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:AddBrowserAction",
id: this._nextMenuId++,
uuid: browserAction.uuid,
name: browserAction.name,
});
this._browserActions[browserAction.uuid] = browserAction;
this._maybeRegisterListeners();
},
/**
* Checks to see if the browser action is shown. Used for testing only.
* @param {string} uuid The UUID of the browser action.
* @returns True if the browser action is shown; false otherwise.
*/
isShown: function(uuid) {
return !!this._browserActions[uuid];
},
/**
* Synthesizes a click on the browser action. Used for testing only.
* @param {string} uuid The UUID of the browser action.
*/
synthesizeClick: function(uuid) {
let browserAction = this._browserActions[uuid];
if (!browserAction) {
throw new Error(`No BrowserAction with UUID ${uuid} was found`);
}
browserAction.onClicked();
},
/**
* Unregisters the browser action with the specified ID.
* @param {string} uuid The UUID of the browser action.
*/
unregister(uuid) {
let browserAction = this._browserActions[uuid];
if (!browserAction) {
throw new Error(`No BrowserAction with UUID ${uuid} was found`);
}
EventDispatcher.instance.sendRequest({
type: "Menu:RemoveBrowserAction",
uuid,
});
delete this._browserActions[uuid];
this._maybeUnregisterListeners();
}
}

Просмотреть файл

@ -34,11 +34,11 @@ function resolveGeckoURI(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",
@ -46,9 +46,9 @@ var PageActions = {
}
},
_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",
@ -101,7 +101,7 @@ var PageActions = {
this._items[id].longClickCallback = aOptions.longClickCallback;
}
this._maybeInit();
this._maybeInitialize();
return id;
},
@ -112,6 +112,6 @@ var PageActions = {
});
delete this._items[id];
this._maybeUninit();
this._maybeUninitialize();
}
}

Просмотреть файл

@ -21,6 +21,7 @@ DIRS += ['geckoview']
EXTRA_JS_MODULES += [
'Accounts.jsm',
'BrowserActions.jsm',
'dbg-browser-actors.js',
'DelayedInit.jsm',
'DownloadNotifications.jsm',