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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* 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";
loader.lazyRequireGetter(
this,
"EventEmitter",
"resource://devtools/shared/event-emitter.js"
);
/**
* Client-side NodePicker module.
* To be used by inspector front when it needs to select DOM elements.
*
* NodePicker is a proxy for the node picker functionality from WalkerFront instances
* of all available InspectorFronts. It is a single point of entry for the client to:
* - invoke actions to start and stop picking nodes on all walkers
* - listen to node picker events from all walkers and relay them to subscribers
*
*
* @param {Commands} commands
* The commands object with all interfaces defined from devtools/shared/commands/
* @param {Selection} selection
* The global Selection object
*/
class NodePicker extends EventEmitter {
constructor(commands, selection) {
super();
this.commands = commands;
this.targetCommand = commands.targetCommand;
// Whether or not the node picker is active.
this.isPicking = false;
// Whether to focus the top-level frame before picking nodes.
this.doFocus = false;
}
// The set of inspector fronts corresponding to the targets where picking happens.
#currentInspectorFronts = new Set();
/**
* Start/stop the element picker on the debuggee target.
*
* @param {Boolean} doFocus
* Optionally focus the content area once the picker is activated.
* @return Promise that resolves when done
*/
togglePicker = doFocus => {
if (this.isPicking) {
return this.stop({ canceled: true });
}
return this.start(doFocus);
};
/**
* This DOCUMENT_EVENT resource callback is only used for webextension targets
* to workaround the fact that some navigations will not create/destroy any
* target (eg when jumping from a background document to a popup document).
**/
#onWebExtensionDocumentEventAvailable = async resources => {
const { DOCUMENT_EVENT } = this.commands.resourceCommand.TYPES;
for (const resource of resources) {
if (
resource.resourceType == DOCUMENT_EVENT &&
resource.name === "dom-complete" &&
resource.targetFront.isTopLevel &&
// When switching frames for a webextension target, a first dom-complete
// resource is emitted when we start watching the new docshell, in the
// WindowGlobalTargetActor progress listener.
//
// However here, we are expecting the "fake" dom-complete resource
// emitted specifically from the webextension target actor, when the
// new docshell is finally recognized to be linked to the target's
// webextension. This resource is emitted from `_changeTopLevelDocument`
// and is the only one which will have `isFrameSwitching` set to true.
//
// It also emitted after the one for the new docshell, so to avoid
// stopping and starting the node-picker twice, we filter out the first
// resource, which does not have `isFrameSwitching` set.
resource.isFrameSwitching
) {
const inspectorFront = await resource.targetFront.getFront("inspector");
// When a webextension target navigates, it will typically be between
// documents which are not under the same root (fallback-document,
// devtools-panel, popup). Even though we are not switching targets, we
// need to restart the node picker.
await inspectorFront.walker.cancelPick();
await inspectorFront.walker.pick(this.doFocus);
this.emitForTests("node-picker-webextension-target-restarted");
}
}
};
/**
* Tell the walker front corresponding to the given inspector front to enter node
* picking mode (listen for mouse movements over its nodes) and set event listeners
* associated with node picking: hover node, pick node, preview, cancel. See WalkerSpec.
*
* @param {InspectorFront} inspectorFront
* @return {Promise}
*/
#onInspectorFrontAvailable = async inspectorFront => {
this.#currentInspectorFronts.add(inspectorFront);
// watchFront may notify us about inspector fronts that aren't initialized yet,
// so ensure waiting for initialization in order to have a defined `walker` attribute.
await inspectorFront.initialize();
const { walker } = inspectorFront;
walker.on("picker-node-hovered", this.#onHovered);
walker.on("picker-node-picked", this.#onPicked);
walker.on("picker-node-previewed", this.#onPreviewed);
walker.on("picker-node-canceled", this.#onCanceled);
await walker.pick(this.doFocus);
this.emitForTests("inspector-front-ready-for-picker", walker);
};
/**
* Tell the walker front corresponding to the given inspector front to exit the node
* picking mode and remove all event listeners associated with node picking.
*
* @param {InspectorFront} inspectorFront
* @param {Boolean} isDestroyCodePath
* Optional. If true, we assume that's when the toolbox closes
* and we should avoid doing any RDP request.
* @return {Promise}
*/
#onInspectorFrontDestroyed = async (
inspectorFront,
{ isDestroyCodepath } = {}
) => {
this.#currentInspectorFronts.delete(inspectorFront);
const { walker } = inspectorFront;
if (!walker) {
return;
}
walker.off("picker-node-hovered", this.#onHovered);
walker.off("picker-node-picked", this.#onPicked);
walker.off("picker-node-previewed", this.#onPreviewed);
walker.off("picker-node-canceled", this.#onCanceled);
// Only do a RDP request if we stop the node picker from a user action.
// Avoid doing one when we close the toolbox, in this scenario
// the walker actor on the server side will automatically cancel the node picking.
if (!isDestroyCodepath) {
await walker.cancelPick();
}
};
/**
* While node picking, we want each target's walker fronts to listen for mouse
* movements over their nodes and emit events. Walker fronts are obtained from
* inspector fronts so we watch for the creation and destruction of inspector fronts
* in order to add or remove the necessary event listeners.
*
* @param {TargetFront} targetFront
* @return {Promise}
*/
#onTargetAvailable = async ({ targetFront }) => {
targetFront.watchFronts(
"inspector",
this.#onInspectorFrontAvailable,
this.#onInspectorFrontDestroyed
);
};
/**
* Start the element picker.
* This will instruct walker fronts of all available targets (and those of targets
* created while node picking is active) to listen for mouse movements over their nodes
* and trigger events when a node is hovered or picked.
*
* @param {Boolean} doFocus
* Optionally focus the content area once the picker is activated.
*/
start = async doFocus => {
if (this.isPicking) {
return;
}
this.isPicking = true;
this.doFocus = doFocus;
this.emit("picker-starting");
this.targetCommand.watchTargets({
types: this.targetCommand.ALL_TYPES,
onAvailable: this.#onTargetAvailable,
});
if (this.targetCommand.descriptorFront.isWebExtension) {
await this.commands.resourceCommand.watchResources(
[this.commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: this.#onWebExtensionDocumentEventAvailable,
}
);
}
this.emit("picker-started");
};
/**
* Stop the element picker. Note that the picker is automatically stopped when
* an element is picked.
*
* @param {Boolean} isDestroyCodePath
* Optional. If true, we assume that's when the toolbox closes
* and we should avoid doing any RDP request.
* @param {Boolean} canceled
* Optional. If true, emit an additional event to notify that the
* picker was canceled, ie stopped without selecting a node.
*/
stop = async ({ isDestroyCodepath, canceled } = {}) => {
if (!this.isPicking) {
return;
}
this.isPicking = false;
this.doFocus = false;
this.targetCommand.unwatchTargets({
types: this.targetCommand.ALL_TYPES,
onAvailable: this.#onTargetAvailable,
});
if (this.targetCommand.descriptorFront.isWebExtension) {
this.commands.resourceCommand.unwatchResources(
[this.commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: this.#onWebExtensionDocumentEventAvailable,
}
);
}
const promises = [];
for (const inspectorFront of this.#currentInspectorFronts) {
promises.push(
this.#onInspectorFrontDestroyed(inspectorFront, {
isDestroyCodepath,
})
);
}
await Promise.all(promises);
this.#currentInspectorFronts.clear();
this.emit("picker-stopped");
if (canceled) {
this.emit("picker-node-canceled");
}
};
destroy() {
// Do not await for stop as the isDestroy argument will make this method synchronous
// and we want to avoid having an async destroy
this.stop({ isDestroyCodepath: true });
this.targetCommand = null;
this.commands = null;
}
/**
* When a node is hovered by the mouse when the highlighter is in picker mode
*
* @param {Object} data
* Information about the node being hovered
*/
#onHovered = data => {
this.emit("picker-node-hovered", data.node);
// We're going to cleanup references for all the other walkers, so that if we hover
// back the same node, we will receive a new `picker-node-hovered` event.
for (const inspectorFront of this.#currentInspectorFronts) {
if (inspectorFront.walker !== data.node.walkerFront) {
inspectorFront.walker.clearPicker();
}
}
};
/**
* When a node has been picked while the highlighter is in picker mode
*
* @param {Object} data
* Information about the picked node
*/
#onPicked = data => {
this.emit("picker-node-picked", data.node);
return this.stop();
};
/**
* When a node has been shift-clicked (previewed) while the highlighter is in
* picker mode
*
* @param {Object} data
* Information about the picked node
*/
#onPreviewed = data => {
this.emit("picker-node-previewed", data.node);
};
/**
* When the picker is canceled, stop the picker, and make sure the toolbox
* gets the focus.
*/
#onCanceled = data => {
return this.stop({ canceled: true });
};
}
module.exports = NodePicker;
|