Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
"use strict";
6
7
const { EventDispatcher } = ChromeUtils.import(
9
);
10
11
var EXPORTED_SYMBOLS = ["BrowserActions"];
12
13
var BrowserActions = {
14
_browserActions: {},
15
_browserActionTitles: {},
16
17
_initialized: false,
18
19
/**
20
* Registers the listeners only if they have not been initialized
21
* already and there is at least one browser action.
22
*/
23
_maybeRegisterListeners() {
24
if (!this._initialized && Object.keys(this._browserActions).length) {
25
this._initialized = true;
26
EventDispatcher.instance.registerListener(this, "Menu:Clicked");
27
}
28
},
29
30
/**
31
* Unregisters the listeners if they are already initizliaed and
32
* all of the browser actions have been removed.
33
*/
34
_maybeUnregisterListeners() {
35
if (this._initialized && !Object.keys(this._browserActions).length) {
36
this._initialized = false;
37
EventDispatcher.instance.unregisterListener(this, "Menu:Clicked");
38
}
39
},
40
41
/**
42
* Called when a browser action is clicked on.
43
* @param {string} event The name of the event, which should always
44
* be "Menu:Clicked".
45
* @param {Object} data An object containing information about the
46
* browser action, which in this case should contain an `item`
47
* property which is browser action's UUID.
48
*/
49
onEvent(event, data) {
50
if (event !== "Menu:Clicked") {
51
throw new Error(
52
`Expected "Menu:Clicked" event - received "${event}" instead`
53
);
54
}
55
56
let browserAction = this._browserActions[data.item];
57
if (!browserAction) {
58
// This was probably meant for the NativeWindow menu handler.
59
return;
60
}
61
browserAction.onClicked();
62
},
63
64
/**
65
* Registers a new browser action.
66
* @param {Object} browserAction The browser action to add.
67
*/
68
register(browserAction) {
69
EventDispatcher.instance.sendRequest({
70
type: "Menu:Add",
71
uuid: browserAction.uuid,
72
name: browserAction.defaults.name,
73
});
74
75
this._browserActions[browserAction.uuid] = browserAction;
76
this._browserActionTitles[browserAction.uuid] = browserAction.defaults.name;
77
78
this._maybeRegisterListeners();
79
},
80
81
/**
82
* Updates the browser action with the specified UUID.
83
* @param {string} uuid The UUID of the browser action.
84
* @param {Object} options The properties to update.
85
*/
86
update(uuid, options) {
87
if (options.name) {
88
EventDispatcher.instance.sendRequest({
89
type: "Menu:Update",
90
uuid,
91
options,
92
});
93
94
this._browserActionTitles[uuid] = options.name;
95
}
96
},
97
98
/**
99
* Retrieves the name currently used for the browser action with the
100
* specified UUID. Used for testing only.
101
* @param {string} uuid The UUID of the browser action.
102
* @returns {string} the name currently used for the browser action.
103
*/
104
getNameForActiveTab(uuid) {
105
return this._browserActionTitles[uuid];
106
},
107
108
/**
109
* Checks to see if the browser action is shown. Used for testing only.
110
* @param {string} uuid The UUID of the browser action.
111
* @returns {boolean} true if the browser action is shown; false otherwise.
112
*/
113
isShown(uuid) {
114
return !!this._browserActions[uuid];
115
},
116
117
/**
118
* Synthesizes a click on the browser action. Used for testing only.
119
* @param {string} uuid The UUID of the browser action.
120
*/
121
synthesizeClick(uuid) {
122
let browserAction = this._browserActions[uuid];
123
if (!browserAction) {
124
throw new Error(`No BrowserAction with UUID ${uuid} was found`);
125
}
126
browserAction.onClicked();
127
},
128
129
/**
130
* Unregisters the browser action with the specified UUID.
131
* @param {string} uuid The UUID of the browser action.
132
*/
133
unregister(uuid) {
134
let browserAction = this._browserActions[uuid];
135
if (!browserAction) {
136
throw new Error(`No BrowserAction with UUID ${uuid} was found`);
137
}
138
EventDispatcher.instance.sendRequest({
139
type: "Menu:Remove",
140
uuid,
141
});
142
delete this._browserActions[uuid];
143
delete this._browserActionTitles[uuid];
144
this._maybeUnregisterListeners();
145
},
146
};