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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
/* 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 Services = require("Services");
const { gDevTools } = require("devtools/client/framework/devtools");
const { LocalizationHelper } = require("devtools/shared/l10n");
const L10N = new LocalizationHelper(
"devtools/client/locales/toolbox.properties"
);
loader.lazyRequireGetter(
this,
"AppConstants",
"resource://gre/modules/AppConstants.jsm",
true
);
loader.lazyRequireGetter(
this,
"openDocLink",
"devtools/client/shared/link",
true
);
exports.OptionsPanel = OptionsPanel;
function GetPref(name) {
const type = Services.prefs.getPrefType(name);
switch (type) {
case Services.prefs.PREF_STRING:
return Services.prefs.getCharPref(name);
case Services.prefs.PREF_INT:
return Services.prefs.getIntPref(name);
case Services.prefs.PREF_BOOL:
return Services.prefs.getBoolPref(name);
default:
throw new Error("Unknown type");
}
}
function SetPref(name, value) {
const type = Services.prefs.getPrefType(name);
switch (type) {
case Services.prefs.PREF_STRING:
return Services.prefs.setCharPref(name, value);
case Services.prefs.PREF_INT:
return Services.prefs.setIntPref(name, value);
case Services.prefs.PREF_BOOL:
return Services.prefs.setBoolPref(name, value);
default:
throw new Error("Unknown type");
}
}
function InfallibleGetBoolPref(key) {
try {
return Services.prefs.getBoolPref(key);
} catch (ex) {
return true;
}
}
/**
* Represents the Options Panel in the Toolbox.
*/
function OptionsPanel(iframeWindow, toolbox) {
this.panelDoc = iframeWindow.document;
this.panelWin = iframeWindow;
this.toolbox = toolbox;
this.telemetry = toolbox.telemetry;
this.isReady = false;
this.setupToolsList = this.setupToolsList.bind(this);
this._prefChanged = this._prefChanged.bind(this);
this._themeRegistered = this._themeRegistered.bind(this);
this._themeUnregistered = this._themeUnregistered.bind(this);
this._disableJSClicked = this._disableJSClicked.bind(this);
this.disableJSNode = this.panelDoc.getElementById(
"devtools-disable-javascript"
);
this._addListeners();
const EventEmitter = require("devtools/shared/event-emitter");
EventEmitter.decorate(this);
}
OptionsPanel.prototype = {
get target() {
return this.toolbox.target;
},
async open() {
this.setupToolsList();
this.setupToolbarButtonsList();
this.setupThemeList();
this.setupAdditionalOptions();
await this.populatePreferences();
this.isReady = true;
this.emit("ready");
return this;
},
_addListeners: function() {
Services.prefs.addObserver("devtools.cache.disabled", this._prefChanged);
Services.prefs.addObserver("devtools.theme", this._prefChanged);
Services.prefs.addObserver(
"devtools.source-map.client-service.enabled",
this._prefChanged
);
gDevTools.on("theme-registered", this._themeRegistered);
gDevTools.on("theme-unregistered", this._themeUnregistered);
// Refresh the tools list when a new tool or webextension has been
// registered to the toolbox.
this.toolbox.on("tool-registered", this.setupToolsList);
this.toolbox.on("webextension-registered", this.setupToolsList);
// Refresh the tools list when a new tool or webextension has been
// unregistered from the toolbox.
this.toolbox.on("tool-unregistered", this.setupToolsList);
this.toolbox.on("webextension-unregistered", this.setupToolsList);
},
_removeListeners: function() {
Services.prefs.removeObserver("devtools.cache.disabled", this._prefChanged);
Services.prefs.removeObserver("devtools.theme", this._prefChanged);
Services.prefs.removeObserver(
"devtools.source-map.client-service.enabled",
this._prefChanged
);
this.toolbox.off("tool-registered", this.setupToolsList);
this.toolbox.off("tool-unregistered", this.setupToolsList);
this.toolbox.off("webextension-registered", this.setupToolsList);
this.toolbox.off("webextension-unregistered", this.setupToolsList);
gDevTools.off("theme-registered", this._themeRegistered);
gDevTools.off("theme-unregistered", this._themeUnregistered);
},
_prefChanged: function(subject, topic, prefName) {
if (prefName === "devtools.cache.disabled") {
const cacheDisabled = GetPref(prefName);
const cbx = this.panelDoc.getElementById("devtools-disable-cache");
cbx.checked = cacheDisabled;
} else if (prefName === "devtools.theme") {
this.updateCurrentTheme();
} else if (prefName === "devtools.source-map.client-service.enabled") {
this.updateSourceMapPref();
}
},
_themeRegistered: function(themeId) {
this.setupThemeList();
},
_themeUnregistered: function(theme) {
const themeBox = this.panelDoc.getElementById("devtools-theme-box");
const themeInput = themeBox.querySelector(`[value=${theme.id}]`);
if (themeInput) {
themeInput.parentNode.remove();
}
},
async setupToolbarButtonsList() {
// Ensure the toolbox is open, and the buttons are all set up.
await this.toolbox.isOpen;
const enabledToolbarButtonsBox = this.panelDoc.getElementById(
"enabled-toolbox-buttons-box"
);
const toolbarButtons = this.toolbox.toolbarButtons;
if (!toolbarButtons) {
console.warn("The command buttons weren't initiated yet.");
return;
}
const onCheckboxClick = checkbox => {
const commandButton = toolbarButtons.filter(
toggleableButton => toggleableButton.id === checkbox.id
)[0];
Services.prefs.setBoolPref(
commandButton.visibilityswitch,
checkbox.checked
);
this.toolbox.updateToolboxButtonsVisibility();
};
const createCommandCheckbox = button => {
const checkboxLabel = this.panelDoc.createElement("label");
const checkboxSpanLabel = this.panelDoc.createElement("span");
checkboxSpanLabel.textContent = button.description;
const checkboxInput = this.panelDoc.createElement("input");
checkboxInput.setAttribute("type", "checkbox");
checkboxInput.setAttribute("id", button.id);
if (Services.prefs.getBoolPref(button.visibilityswitch, true)) {
checkboxInput.setAttribute("checked", true);
}
checkboxInput.addEventListener(
"change",
onCheckboxClick.bind(this, checkboxInput)
);
checkboxLabel.appendChild(checkboxInput);
checkboxLabel.appendChild(checkboxSpanLabel);
return checkboxLabel;
};
for (const button of toolbarButtons) {
if (!button.isTargetSupported(this.toolbox.target)) {
continue;
}
enabledToolbarButtonsBox.appendChild(createCommandCheckbox(button));
}
},
setupToolsList: function() {
const defaultToolsBox = this.panelDoc.getElementById("default-tools-box");
const additionalToolsBox = this.panelDoc.getElementById(
"additional-tools-box"
);
const toolsNotSupportedLabel = this.panelDoc.getElementById(
"tools-not-supported-label"
);
let atleastOneToolNotSupported = false;
// Signal tool registering/unregistering globally (for the tools registered
// globally) and per toolbox (for the tools registered to a single toolbox).
// This event handler expect this to be binded to the related checkbox element.
const onCheckboxClick = function(telemetry, tool) {
// Set the kill switch pref boolean to true
Services.prefs.setBoolPref(tool.visibilityswitch, this.checked);
if (!tool.isWebExtension) {
gDevTools.emit(
this.checked ? "tool-registered" : "tool-unregistered",
tool.id
);
// Record which tools were registered and unregistered.
telemetry.keyedScalarSet(
"devtools.tool.registered",
tool.id,
this.checked
);
}
};
const createToolCheckbox = tool => {
const checkboxLabel = this.panelDoc.createElement("label");
const checkboxInput = this.panelDoc.createElement("input");
checkboxInput.setAttribute("type", "checkbox");
checkboxInput.setAttribute("id", tool.id);
checkboxInput.setAttribute("title", tool.tooltip || "");
const checkboxSpanLabel = this.panelDoc.createElement("span");
if (tool.isTargetSupported(this.target)) {
checkboxSpanLabel.textContent = tool.label;
} else {
atleastOneToolNotSupported = true;
checkboxSpanLabel.textContent = L10N.getFormatStr(
"options.toolNotSupportedMarker",
tool.label
);
checkboxInput.setAttribute("data-unsupported", "true");
checkboxInput.setAttribute("disabled", "true");
}
if (InfallibleGetBoolPref(tool.visibilityswitch)) {
checkboxInput.setAttribute("checked", "true");
}
checkboxInput.addEventListener(
"change",
onCheckboxClick.bind(checkboxInput, this.telemetry, tool)
);
checkboxLabel.appendChild(checkboxInput);
checkboxLabel.appendChild(checkboxSpanLabel);
// We shouldn't have deprecated tools anymore, but we might have one in the future,
// when migrating the storage inspector to the application panel (Bug 1681059).
// Let's keep this code for now so we keep the l10n property around and avoid
// unnecessary translation work if we need it again in the future.
if (tool.deprecated) {
const deprecationURL = this.panelDoc.createElement("a");
deprecationURL.title = deprecationURL.href = tool.deprecationURL;
deprecationURL.textContent = L10N.getStr("options.deprecationNotice");
// Cannot use a real link when we are in the Browser Toolbox.
deprecationURL.addEventListener("click", e => {
e.preventDefault();
openDocLink(tool.deprecationURL, { relatedToCurrent: true });
});
const checkboxSpanDeprecated = this.panelDoc.createElement("span");
checkboxSpanDeprecated.className = "deprecation-notice";
checkboxLabel.appendChild(checkboxSpanDeprecated);
checkboxSpanDeprecated.appendChild(deprecationURL);
}
return checkboxLabel;
};
// Clean up any existent default tools content.
for (const label of defaultToolsBox.querySelectorAll("label")) {
label.remove();
}
// Populating the default tools lists
const toggleableTools = gDevTools.getDefaultTools().filter(tool => {
return tool.visibilityswitch && !tool.hiddenInOptions;
});
const fragment = this.panelDoc.createDocumentFragment();
for (const tool of toggleableTools) {
fragment.appendChild(createToolCheckbox(tool));
}
const toolsNotSupportedLabelNode = this.panelDoc.getElementById(
"tools-not-supported-label"
);
defaultToolsBox.insertBefore(fragment, toolsNotSupportedLabelNode);
// Clean up any existent additional tools content.
for (const label of additionalToolsBox.querySelectorAll("label")) {
label.remove();
}
// Populating the additional tools list.
let atleastOneAddon = false;
for (const tool of gDevTools.getAdditionalTools()) {
atleastOneAddon = true;
additionalToolsBox.appendChild(createToolCheckbox(tool));
}
// Populating the additional tools that came from the installed WebExtension add-ons.
for (const { uuid, name, pref } of this.toolbox.listWebExtensions()) {
atleastOneAddon = true;
additionalToolsBox.appendChild(
createToolCheckbox({
isWebExtension: true,
// Use the preference as the unified webextensions tool id.
id: `webext-${uuid}`,
tooltip: name,
label: name,
// Disable the devtools extension using the given pref name:
// the toolbox options for the WebExtensions are not related to a single
// tool (e.g. a devtools panel created from the extension devtools_page)
// but to the entire devtools part of a webextension which is enabled
// by the Addon Manager (but it may be disabled by its related
// devtools about:config preference), and so the following
visibilityswitch: pref,
// Only local tabs are currently supported as targets.
isTargetSupported: target => target.isLocalTab,
})
);
}
if (!atleastOneAddon) {
additionalToolsBox.style.display = "none";
} else {
additionalToolsBox.style.display = "";
}
if (!atleastOneToolNotSupported) {
toolsNotSupportedLabel.style.display = "none";
} else {
toolsNotSupportedLabel.style.display = "";
}
this.panelWin.focus();
},
setupThemeList: function() {
const themeBox = this.panelDoc.getElementById("devtools-theme-box");
const themeLabels = themeBox.querySelectorAll("label");
for (const label of themeLabels) {
label.remove();
}
const createThemeOption = theme => {
const inputLabel = this.panelDoc.createElement("label");
const inputRadio = this.panelDoc.createElement("input");
inputRadio.setAttribute("type", "radio");
inputRadio.setAttribute("value", theme.id);
inputRadio.setAttribute("name", "devtools-theme-item");
inputRadio.addEventListener("change", function(e) {
SetPref(themeBox.getAttribute("data-pref"), e.target.value);
});
const inputSpanLabel = this.panelDoc.createElement("span");
inputSpanLabel.textContent = theme.label;
inputLabel.appendChild(inputRadio);
inputLabel.appendChild(inputSpanLabel);
return inputLabel;
};
// Populating the default theme list
const themes = gDevTools.getThemeDefinitionArray();
for (const theme of themes) {
themeBox.appendChild(createThemeOption(theme));
}
this.updateCurrentTheme();
},
/**
* Add extra checkbox options bound to a boolean preference.
*/
setupAdditionalOptions: function() {
const prefDefinitions = [];
const isNightly = AppConstants.NIGHTLY_BUILD;
if (isNightly) {
// Labels are hardcoded in english because this checkbox is Nightly only.
prefDefinitions.push({
pref: "devtools.performance.new-panel-enabled",
label: "Enable new performance recorder (then re-open DevTools)",
id: "devtools-new-performance",
parentId: "context-options",
});
}
if (this.target.isParentProcess) {
// The Multiprocess Browser Toolbox is only displayed in the settings
// panel for the Browser Toolbox, or when debugging the main process in
// remote debugging.
prefDefinitions.push({
pref: "devtools.browsertoolbox.fission",
label: L10N.getStr("options.enableMultiProcessToolbox"),
id: "devtools-browsertoolbox-fission",
parentId: "context-options",
// createPreferenceOption already updates the value of the preference
// for the current profile when the checkbox changes. Here we need a
// custom behavior for the Browser Toolbox, so we pass an additional
// onChange callback.
onChange: async checked => {
if (!this.toolbox.isBrowserToolbox()) {
// If we are debugging a parent process, but the toolbox is not a
// Browser Toolbox, it means we are remote debugging another
// browser. In this case, the value of devtools.browsertoolbox.fission
// should not be updated in the target browser.
return;
}
// When setting this preference from the BrowserToolbox, we need to
// update the preference on the debugged Firefox profile as well.
// The devtools.browsertoolbox.fission preference is copied from the
// regular Firefox Profile to the Browser Toolbox profile.
// If the preference is not updated on the regular Firefox profile, the
// new value will be lost on the next Browser Toolbox restart.
const { mainRoot } = this.target.client;
const preferenceFront = await mainRoot.getFront("preference");
preferenceFront.setBoolPref(
"devtools.browsertoolbox.fission",
checked
);
},
});
}
const createPreferenceOption = ({ pref, label, id, onChange }) => {
const inputLabel = this.panelDoc.createElement("label");
const checkbox = this.panelDoc.createElement("input");
checkbox.setAttribute("type", "checkbox");
if (GetPref(pref)) {
checkbox.setAttribute("checked", "checked");
}
checkbox.setAttribute("id", id);
checkbox.addEventListener("change", e => {
SetPref(pref, e.target.checked);
if (onChange) {
onChange(e.target.checked);
}
});
const inputSpanLabel = this.panelDoc.createElement("span");
inputSpanLabel.textContent = label;
inputLabel.appendChild(checkbox);
inputLabel.appendChild(inputSpanLabel);
return inputLabel;
};
for (const prefDefinition of prefDefinitions) {
const parent = this.panelDoc.getElementById(prefDefinition.parentId);
// We want to insert the new definition after the last existing
// definition, but before any other element.
// For example in the "Advanced Settings" column there's indeed a <span>
// text at the end, and we want that it stays at the end.
// The reference element can be `null` if there's no label or if there's
// no element after the last label. But that's OK and it will do what we
// want.
const referenceElement = parent.querySelector("label:last-of-type + *");
parent.insertBefore(
createPreferenceOption(prefDefinition),
referenceElement
);
}
},
async populatePreferences() {
const prefCheckboxes = this.panelDoc.querySelectorAll(
"input[type=checkbox][data-pref]"
);
for (const prefCheckbox of prefCheckboxes) {
if (GetPref(prefCheckbox.getAttribute("data-pref"))) {
prefCheckbox.setAttribute("checked", true);
}
prefCheckbox.addEventListener("change", function(e) {
const checkbox = e.target;
SetPref(checkbox.getAttribute("data-pref"), checkbox.checked);
});
}
// Themes radio inputs are handled in setupThemeList
const prefRadiogroups = this.panelDoc.querySelectorAll(
".radiogroup[data-pref]:not(#devtools-theme-box)"
);
for (const radioGroup of prefRadiogroups) {
const selectedValue = GetPref(radioGroup.getAttribute("data-pref"));
for (const radioInput of radioGroup.querySelectorAll(
"input[type=radio]"
)) {
if (radioInput.getAttribute("value") == selectedValue) {
radioInput.setAttribute("checked", true);
}
radioInput.addEventListener("change", function(e) {
SetPref(radioGroup.getAttribute("data-pref"), e.target.value);
});
}
}
const prefSelects = this.panelDoc.querySelectorAll("select[data-pref]");
for (const prefSelect of prefSelects) {
const pref = GetPref(prefSelect.getAttribute("data-pref"));
const options = [...prefSelect.options];
options.some(function(option) {
const value = option.value;
// non strict check to allow int values.
if (value == pref) {
prefSelect.selectedIndex = options.indexOf(option);
return true;
}
return false;
});
prefSelect.addEventListener("change", function(e) {
const select = e.target;
SetPref(
select.getAttribute("data-pref"),
select.options[select.selectedIndex].value
);
});
}
if (!this.target.chrome) {
this.disableJSNode.checked = !this.target.configureOptions
.javascriptEnabled;
this.disableJSNode.addEventListener("click", this._disableJSClicked);
} else {
// Hide the checkbox and label
this.disableJSNode.parentNode.style.display = "none";
const triggersPageRefreshLabel = this.panelDoc.getElementById(
"triggers-page-refresh-label"
);
triggersPageRefreshLabel.style.display = "none";
}
},
updateCurrentTheme: function() {
const currentTheme = GetPref("devtools.theme");
const themeBox = this.panelDoc.getElementById("devtools-theme-box");
const themeRadioInput = themeBox.querySelector(`[value=${currentTheme}]`);
if (themeRadioInput) {
themeRadioInput.checked = true;
} else {
// If the current theme does not exist anymore, switch to light theme
const lightThemeInputRadio = themeBox.querySelector("[value=light]");
lightThemeInputRadio.checked = true;
}
},
updateSourceMapPref: function() {
const prefName = "devtools.source-map.client-service.enabled";
const enabled = GetPref(prefName);
const box = this.panelDoc.querySelector(`[data-pref="${prefName}"]`);
box.checked = enabled;
},
/**
* Disables JavaScript for the currently loaded tab. We force a page refresh
* here because setting docShell.allowJavascript to true fails to block JS
* execution from event listeners added using addEventListener(), AJAX calls
* and timers. The page refresh prevents these things from being added in the
* first place.
*
* @param {Event} event
* The event sent by checking / unchecking the disable JS checkbox.
*/
_disableJSClicked: function(event) {
const checked = event.target.checked;
const options = {
javascriptEnabled: !checked,
};
this.target.reconfigure({ options });
},
destroy: function() {
if (this.destroyed) {
return;
}
this.destroyed = true;
this._removeListeners();
this.disableJSNode.removeEventListener("click", this._disableJSClicked);
this.panelWin = this.panelDoc = this.disableJSNode = this.toolbox = null;
},
};
|