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
|
/* 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";
// React & Redux
const {
Component,
createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const {
connect,
} = require("resource://devtools/client/shared/vendor/react-redux.js");
const targetActions = require("resource://devtools/shared/commands/target/actions/targets.js");
const webconsoleActions = require("resource://devtools/client/webconsole/actions/index.js");
const {
l10n,
} = require("resource://devtools/client/webconsole/utils/messages.js");
const targetSelectors = require("resource://devtools/shared/commands/target/selectors/targets.js");
loader.lazyGetter(this, "TARGET_TYPES", function () {
return require("resource://devtools/shared/commands/target/target-command.js")
.TYPES;
});
// Additional Components
const MenuButton = createFactory(
require("resource://devtools/client/shared/components/menu/MenuButton.js")
);
loader.lazyGetter(this, "MenuItem", function () {
return createFactory(
require("resource://devtools/client/shared/components/menu/MenuItem.js")
);
});
loader.lazyGetter(this, "MenuList", function () {
return createFactory(
require("resource://devtools/client/shared/components/menu/MenuList.js")
);
});
class EvaluationContextSelector extends Component {
static get propTypes() {
return {
selectTarget: PropTypes.func.isRequired,
onContextChange: PropTypes.func.isRequired,
selectedTarget: PropTypes.object,
lastTargetRefresh: PropTypes.number,
targets: PropTypes.array,
webConsoleUI: PropTypes.object.isRequired,
};
}
shouldComponentUpdate(nextProps) {
if (this.props.selectedTarget !== nextProps.selectedTarget) {
return true;
}
if (this.props.lastTargetRefresh !== nextProps.lastTargetRefresh) {
return true;
}
if (this.props.targets.length !== nextProps.targets.length) {
return true;
}
for (let i = 0; i < nextProps.targets.length; i++) {
const target = this.props.targets[i];
const nextTarget = nextProps.targets[i];
if (target.url != nextTarget.url || target.name != nextTarget.name) {
return true;
}
}
return false;
}
componentDidUpdate(prevProps) {
if (this.props.selectedTarget !== prevProps.selectedTarget) {
this.props.onContextChange();
}
}
getIcon(target) {
if (target.targetType === TARGET_TYPES.FRAME) {
return "chrome://devtools/content/debugger/images/globe-small.svg";
}
if (
target.targetType === TARGET_TYPES.WORKER ||
target.targetType === TARGET_TYPES.SHARED_WORKER ||
target.targetType === TARGET_TYPES.SERVICE_WORKER
) {
return "chrome://devtools/content/debugger/images/worker.svg";
}
if (target.targetType === TARGET_TYPES.PROCESS) {
return "chrome://devtools/content/debugger/images/window.svg";
}
return null;
}
renderMenuItem(target) {
const { selectTarget, selectedTarget } = this.props;
const label = target.isTopLevel
? l10n.getStr("webconsole.input.selector.top")
: target.name;
return MenuItem({
key: `webconsole-evaluation-selector-item-${target.actorID}`,
className: "menu-item webconsole-evaluation-selector-item",
type: "checkbox",
checked: selectedTarget ? selectedTarget == target : target.isTopLevel,
label,
tooltip: target.url || target.name,
icon: this.getIcon(target),
onClick: () => selectTarget(target.actorID),
});
}
renderMenuItems() {
const { targets } = this.props;
// Let's sort the targets (using "numeric" so Content processes are ordered by PID).
const collator = new Intl.Collator("en", { numeric: true });
targets.sort((a, b) => collator.compare(a.name, b.name));
let mainTarget;
const sections = {
[TARGET_TYPES.FRAME]: [],
[TARGET_TYPES.WORKER]: [],
[TARGET_TYPES.SHARED_WORKER]: [],
[TARGET_TYPES.SERVICE_WORKER]: [],
};
// When in Browser Toolbox, we want to display the process targets with the frames
// in the same process as a group
// e.g.
// |------------------------------|
// | Top |
// | -----------------------------|
// | (pid 1234) priviledgedabout |
// | New Tab |
// | -----------------------------|
// | (pid 5678) web |
// | cnn.com |
// | -----------------------------|
// | RemoteSettingWorker.js |
// |------------------------------|
//
// This object will be keyed by PID, and each property will be an object with a
// `process` property (for the process target item), and a `frames` property (and array
// for all the frame target items).
const processes = {};
const { webConsoleUI } = this.props;
const handleProcessTargets =
webConsoleUI.isBrowserConsole || webConsoleUI.isBrowserToolboxConsole;
for (const target of targets) {
const menuItem = this.renderMenuItem(target);
if (target.isTopLevel) {
mainTarget = menuItem;
} else if (target.targetType == TARGET_TYPES.PROCESS) {
if (!processes[target.processID]) {
processes[target.processID] = { frames: [] };
}
processes[target.processID].process = menuItem;
} else if (
target.targetType == TARGET_TYPES.FRAME &&
handleProcessTargets &&
target.processID
) {
// The associated process target might not have been handled yet, so make sure
// to create it.
if (!processes[target.processID]) {
processes[target.processID] = { frames: [] };
}
processes[target.processID].frames.push(menuItem);
} else {
sections[target.targetType].push(menuItem);
}
}
// Note that while debugging popups, we might have a small period
// of time where we don't have any top level target when we reload
// the original tab
const items = mainTarget ? [mainTarget] : [];
// Handle PROCESS targets sections first, as we want to display the associated frames
// below the process to group them.
if (processes) {
for (const [pid, { process, frames }] of Object.entries(processes)) {
items.push(dom.hr({ role: "menuseparator", key: `${pid}-separator` }));
if (process) {
items.push(process);
}
if (frames) {
items.push(...frames);
}
}
}
for (const [targetType, menuItems] of Object.entries(sections)) {
if (menuItems.length) {
items.push(
dom.hr({ role: "menuseparator", key: `${targetType}-separator` }),
...menuItems
);
}
}
return MenuList(
{ id: "webconsole-console-evaluation-context-selector-menu-list" },
items
);
}
getLabel() {
const { selectedTarget } = this.props;
if (!selectedTarget || selectedTarget.isTopLevel) {
return l10n.getStr("webconsole.input.selector.top");
}
return selectedTarget.name;
}
render() {
const { webConsoleUI, targets, selectedTarget } = this.props;
// Don't render if there's only one target.
// Also bail out if the console is being destroyed (where WebConsoleUI.wrapper gets
// nullified).
if (targets.length <= 1 || !webConsoleUI.wrapper) {
return null;
}
const doc = webConsoleUI.document;
const { toolbox } = webConsoleUI.wrapper;
return MenuButton(
{
menuId: "webconsole-input-evaluationsButton",
toolboxDoc: toolbox ? toolbox.doc : doc,
label: this.getLabel(),
className:
"webconsole-evaluation-selector-button devtools-button devtools-dropdown-button" +
(selectedTarget && !selectedTarget.isTopLevel ? " checked" : ""),
title: l10n.getStr("webconsole.input.selector.tooltip"),
},
// We pass the children in a function so we don't require the MenuItem and MenuList
// components until we need to display them (i.e. when the button is clicked).
() => this.renderMenuItems()
);
}
}
const toolboxConnected = connect(
state => ({
targets: targetSelectors.getToolboxTargets(state),
selectedTarget: targetSelectors.getSelectedTarget(state),
lastTargetRefresh: targetSelectors.getLastTargetRefresh(state),
}),
dispatch => ({
selectTarget: actorID => dispatch(targetActions.selectTarget(actorID)),
}),
undefined,
{ storeKey: "target-store" }
)(EvaluationContextSelector);
module.exports = connect(
state => state,
dispatch => ({
onContextChange: () => {
dispatch(
webconsoleActions.updateInstantEvaluationResultForCurrentExpression()
);
dispatch(webconsoleActions.autocompleteClear());
},
})
)(toolboxConnected);
|