gecko-dev/mobile/android/modules/BrowserActions.jsm
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

143 lines
4.2 KiB
JavaScript

/* 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 {EventDispatcher} = ChromeUtils.import("resource://gre/modules/Messaging.jsm");
var EXPORTED_SYMBOLS = ["BrowserActions"];
var BrowserActions = {
_browserActions: {},
_browserActionTitles: {},
_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() {
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 UUID.
*/
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) {
// This was probably meant for the NativeWindow menu handler.
return;
}
browserAction.onClicked();
},
/**
* Registers a new browser action.
* @param {Object} browserAction The browser action to add.
*/
register(browserAction) {
EventDispatcher.instance.sendRequest({
type: "Menu:Add",
uuid: browserAction.uuid,
name: browserAction.defaults.name,
});
this._browserActions[browserAction.uuid] = browserAction;
this._browserActionTitles[browserAction.uuid] = browserAction.defaults.name;
this._maybeRegisterListeners();
},
/**
* Updates the browser action with the specified UUID.
* @param {string} uuid The UUID of the browser action.
* @param {Object} options The properties to update.
*/
update(uuid, options) {
if (options.name) {
EventDispatcher.instance.sendRequest({
type: "Menu:Update",
uuid,
options,
});
this._browserActionTitles[uuid] = options.name;
}
},
/**
* Retrieves the name currently used for the browser action with the
* specified UUID. Used for testing only.
* @param {string} uuid The UUID of the browser action.
* @returns {string} the name currently used for the browser action.
*/
getNameForActiveTab(uuid) {
return this._browserActionTitles[uuid];
},
/**
* Checks to see if the browser action is shown. Used for testing only.
* @param {string} uuid The UUID of the browser action.
* @returns {boolean} true if the browser action is shown; false otherwise.
*/
isShown(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(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 UUID.
* @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:Remove",
uuid,
});
delete this._browserActions[uuid];
delete this._browserActionTitles[uuid];
this._maybeUnregisterListeners();
},
};