1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/* 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 EventEmitter = require("resource://devtools/shared/event-emitter.js");
loader.lazyRequireGetter(
this,
"ResponsiveUI",
"resource://devtools/client/responsive/ui.js"
);
loader.lazyRequireGetter(
this,
"startup",
"resource://devtools/client/responsive/utils/window.js",
true
);
loader.lazyRequireGetter(
this,
"showNotification",
"resource://devtools/client/responsive/utils/notification.js",
true
);
loader.lazyRequireGetter(
this,
"l10n",
"resource://devtools/client/responsive/utils/l10n.js"
);
loader.lazyRequireGetter(
this,
"PriorityLevels",
"resource://devtools/client/shared/components/NotificationBox.js",
true
);
loader.lazyRequireGetter(
this,
"gDevTools",
"resource://devtools/client/framework/devtools.js",
true
);
loader.lazyRequireGetter(
this,
"gDevToolsBrowser",
"resource://devtools/client/framework/devtools-browser.js",
true
);
loader.lazyRequireGetter(
this,
"Telemetry",
"resource://devtools/client/shared/telemetry.js"
);
/**
* ResponsiveUIManager is the external API for the browser UI, etc. to use when
* opening and closing the responsive UI.
*/
class ResponsiveUIManager {
constructor() {
this.activeTabs = new Map();
this.handleMenuCheck = this.handleMenuCheck.bind(this);
EventEmitter.decorate(this);
}
get telemetry() {
if (!this._telemetry) {
this._telemetry = new Telemetry();
}
return this._telemetry;
}
/**
* Toggle the responsive UI for a tab.
*
* @param window
* The main browser chrome window.
* @param tab
* The browser tab.
* @param options
* Other options associated with toggling. Currently includes:
* - `trigger`: String denoting the UI entry point, such as:
* - `toolbox`: Toolbox Button
* - `menu`: Browser Tools menu item
* - `shortcut`: Keyboard shortcut
* @return Promise
* Resolved when the toggling has completed. If the UI has opened,
* it is resolved to the ResponsiveUI instance for this tab. If the
* the UI has closed, there is no resolution value.
*/
toggle(window, tab, options = {}) {
const completed = this._toggleForTab(window, tab, options);
completed.catch(console.error);
return completed;
}
_toggleForTab(window, tab, options) {
if (this.isActiveForTab(tab)) {
return this.closeIfNeeded(window, tab, options);
}
return this.openIfNeeded(window, tab, options);
}
/**
* Opens the responsive UI, if not already open.
*
* @param window
* The main browser chrome window.
* @param tab
* The browser tab.
* @param options
* Other options associated with opening. Currently includes:
* - `trigger`: String denoting the UI entry point, such as:
* - `toolbox`: Toolbox Button
* - `menu`: Browser Tools menu item
* - `shortcut`: Keyboard shortcut
* @return Promise
* Resolved to the ResponsiveUI instance for this tab when opening is
* complete.
*/
async openIfNeeded(window, tab, options = {}) {
if (!this.isActiveForTab(tab)) {
await gDevToolsBrowser.loadBrowserStyleSheet(window);
this.initMenuCheckListenerFor(window);
const ui = new ResponsiveUI(this, window, tab);
this.activeTabs.set(tab, ui);
// Explicitly not await on telemetry to avoid delaying RDM opening
this.recordTelemetryOpen(window, tab, options);
await this.setMenuCheckFor(tab, window);
await ui.inited;
this.emit("on", { tab });
}
return this.getResponsiveUIForTab(tab);
}
/**
* Record all telemetry probes related to RDM opening.
*/
async recordTelemetryOpen(window, tab, options) {
// Track whether a toolbox was opened before RDM was opened.
let toolbox;
if (gDevTools.hasToolboxForTab(tab)) {
toolbox = await gDevTools.getToolboxForTab(tab);
}
const hostType = toolbox ? toolbox.hostType : "none";
const hasToolbox = !!toolbox;
if (hasToolbox) {
this.telemetry.scalarAdd("devtools.responsive.toolbox_opened_first", 1);
}
this.telemetry.recordEvent("activate", "responsive_design", null, {
host: hostType,
width: Math.ceil(window.outerWidth / 50) * 50,
});
// Track opens keyed by the UI entry point used.
let { trigger } = options;
if (!trigger) {
trigger = "unknown";
}
this.telemetry.keyedScalarAdd(
"devtools.responsive.open_trigger",
trigger,
1
);
}
/**
* Closes the responsive UI, if not already closed.
*
* @param window
* The main browser chrome window.
* @param tab
* The browser tab.
* @param options
* Other options associated with closing. Currently includes:
* - `trigger`: String denoting the UI entry point, such as:
* - `toolbox`: Toolbox Button
* - `menu`: Browser Tools menu item
* - `shortcut`: Keyboard shortcut
* - `reason`: String detailing the specific cause for closing
* @return Promise
* Resolved (with no value) when closing is complete.
*/
async closeIfNeeded(window, tab, options = {}) {
if (this.isActiveForTab(tab)) {
const ui = this.activeTabs.get(tab);
const destroyed = await ui.destroy(options);
if (!destroyed) {
// Already in the process of destroying, abort.
return;
}
this.activeTabs.delete(tab);
if (!this.isActiveForWindow(window)) {
this.removeMenuCheckListenerFor(window);
}
this.emit("off", { tab });
await this.setMenuCheckFor(tab, window);
// Explicitly not await on telemetry to avoid delaying RDM closing
this.recordTelemetryClose(window, tab);
}
}
async recordTelemetryClose(window, tab) {
let toolbox;
if (gDevTools.hasToolboxForTab(tab)) {
toolbox = await gDevTools.getToolboxForTab(tab);
}
const hostType = toolbox ? toolbox.hostType : "none";
this.telemetry.recordEvent("deactivate", "responsive_design", null, {
host: hostType,
width: Math.ceil(window.outerWidth / 50) * 50,
});
}
/**
* Returns true if responsive UI is active for a given tab.
*
* @param tab
* The browser tab.
* @return boolean
*/
isActiveForTab(tab) {
return this.activeTabs.has(tab);
}
/**
* Returns true if responsive UI is active in any tab in the given window.
*
* @param window
* The main browser chrome window.
* @return boolean
*/
isActiveForWindow(window) {
return [...this.activeTabs.keys()].some(t => t.ownerGlobal === window);
}
/**
* Return the responsive UI controller for a tab.
*
* @param tab
* The browser tab.
* @return ResponsiveUI
* The UI instance for this tab.
*/
getResponsiveUIForTab(tab) {
return this.activeTabs.get(tab);
}
handleMenuCheck({ target }) {
this.setMenuCheckFor(target);
}
initMenuCheckListenerFor(window) {
const { tabContainer } = window.gBrowser;
tabContainer.addEventListener("TabSelect", this.handleMenuCheck);
}
removeMenuCheckListenerFor(window) {
if (window?.gBrowser?.tabContainer) {
const { tabContainer } = window.gBrowser;
tabContainer.removeEventListener("TabSelect", this.handleMenuCheck);
}
}
async setMenuCheckFor(tab, window = tab.ownerGlobal) {
await startup(window);
const menu = window.document.getElementById("menu_responsiveUI");
if (menu) {
menu.setAttribute("checked", this.isActiveForTab(tab));
}
}
showRemoteOnlyNotification(window, tab, { trigger } = {}) {
return showNotification(window, tab, {
toolboxButton: trigger == "toolbox",
msg: l10n.getStr("responsive.remoteOnly"),
priority: PriorityLevels.PRIORITY_CRITICAL_MEDIUM,
});
}
}
module.exports = new ResponsiveUIManager();
|