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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/* - 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";
// We attach Preferences to the window object so other contexts (tests, JSMs)
// have access to it.
const Preferences = (window.Preferences = (function() {
const { EventEmitter } = ChromeUtils.import(
"resource://gre/modules/EventEmitter.jsm"
);
const { Services } = ChromeUtils.import(
"resource://gre/modules/Services.jsm"
);
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const lazy = {};
ChromeUtils.defineModuleGetter(
lazy,
"DeferredTask",
"resource://gre/modules/DeferredTask.jsm"
);
function getElementsByAttribute(name, value) {
// If we needed to defend against arbitrary values, we would escape
// double quotes (") and escape characters (\) in them, i.e.:
// ${value.replace(/["\\]/g, '\\$&')}
return value
? document.querySelectorAll(`[${name}="${value}"]`)
: document.querySelectorAll(`[${name}]`);
}
const domContentLoadedPromise = new Promise(resolve => {
window.addEventListener("DOMContentLoaded", resolve, {
capture: true,
once: true,
});
});
const Preferences = {
_all: {},
_add(prefInfo) {
if (this._all[prefInfo.id]) {
throw new Error(`preference with id '${prefInfo.id}' already added`);
}
const pref = new Preference(prefInfo);
this._all[pref.id] = pref;
domContentLoadedPromise.then(() => {
pref.updateElements();
});
return pref;
},
add(prefInfo) {
const pref = this._add(prefInfo);
return pref;
},
addAll(prefInfos) {
prefInfos.map(prefInfo => this._add(prefInfo));
},
get(id) {
return this._all[id] || null;
},
getAll() {
return Object.values(this._all);
},
defaultBranch: Services.prefs.getDefaultBranch(""),
get type() {
return document.documentElement.getAttribute("type") || "";
},
get instantApply() {
// The about:preferences page forces instantApply.
if (this._instantApplyForceEnabled) {
return true;
}
// Dialogs of type="child" are never instantApply.
if (this.type === "child") {
return false;
}
// All other pref windows observe the value of the instantApply
// preference. Note that, as of this writing, the only such windows
// are in tests, so it should be possible to remove the pref
// (and forceEnableInstantApply) in favor of always applying in a parent
// and never applying in a child.
return Services.prefs.getBoolPref("browser.preferences.instantApply");
},
_instantApplyForceEnabled: false,
// Override the computed value of instantApply for this window.
forceEnableInstantApply() {
this._instantApplyForceEnabled = true;
},
observe(subject, topic, data) {
const pref = this._all[data];
if (pref) {
pref.value = pref.valueFromPreferences;
}
},
onDOMContentLoaded() {
// Iterate elements with a "preference" attribute and log an error
// if there isn't a corresponding Preference object in order to catch
// any cases of elements referencing preferences that haven't (yet?)
// been registered.
//
// TODO: remove this code once we determine that there are no such
// elements (or resolve any bugs that cause this behavior).
//
const elements = getElementsByAttribute("preference");
for (const element of elements) {
const id = element.getAttribute("preference");
const pref = this.get(id);
if (!pref) {
console.error(`Missing preference for ID ${id}`);
}
}
},
updateQueued: false,
updateAllElements() {
if (this.updateQueued) {
return;
}
this.updateQueued = true;
Promise.resolve().then(() => {
const preferences = Preferences.getAll();
for (const preference of preferences) {
preference.updateElements();
}
this.updateQueued = false;
});
},
onUnload() {
Services.prefs.removeObserver("", this);
},
QueryInterface: ChromeUtils.generateQI(["nsITimerCallback", "nsIObserver"]),
_deferredValueUpdateElements: new Set(),
writePreferences(aFlushToDisk) {
// Write all values to preferences.
if (this._deferredValueUpdateElements.size) {
this._finalizeDeferredElements();
}
const preferences = Preferences.getAll();
for (const preference of preferences) {
preference.batching = true;
preference.valueFromPreferences = preference.value;
preference.batching = false;
}
if (aFlushToDisk) {
Services.prefs.savePrefFile(null);
}
},
getPreferenceElement(aStartElement) {
let temp = aStartElement;
while (
temp &&
temp.nodeType == Node.ELEMENT_NODE &&
!temp.hasAttribute("preference")
) {
temp = temp.parentNode;
}
return temp && temp.nodeType == Node.ELEMENT_NODE ? temp : aStartElement;
},
_deferredValueUpdate(aElement) {
delete aElement._deferredValueUpdateTask;
const prefID = aElement.getAttribute("preference");
const preference = Preferences.get(prefID);
const prefVal = preference.getElementValue(aElement);
preference.value = prefVal;
this._deferredValueUpdateElements.delete(aElement);
},
_finalizeDeferredElements() {
for (const el of this._deferredValueUpdateElements) {
if (el._deferredValueUpdateTask) {
el._deferredValueUpdateTask.finalize();
}
}
},
userChangedValue(aElement) {
const element = this.getPreferenceElement(aElement);
if (element.hasAttribute("preference")) {
if (element.getAttribute("delayprefsave") != "true") {
const preference = Preferences.get(
element.getAttribute("preference")
);
const prefVal = preference.getElementValue(element);
preference.value = prefVal;
} else {
if (!element._deferredValueUpdateTask) {
element._deferredValueUpdateTask = new lazy.DeferredTask(
this._deferredValueUpdate.bind(this, element),
1000
);
this._deferredValueUpdateElements.add(element);
} else {
// Each time the preference is changed, restart the delay.
element._deferredValueUpdateTask.disarm();
}
element._deferredValueUpdateTask.arm();
}
}
},
onCommand(event) {
// This "command" event handler tracks changes made to preferences by
// the user in this window.
if (event.sourceEvent) {
event = event.sourceEvent;
}
this.userChangedValue(event.target);
},
onChange(event) {
// This "change" event handler tracks changes made to preferences by
// the user in this window.
this.userChangedValue(event.target);
},
onInput(event) {
// This "input" event handler tracks changes made to preferences by
// the user in this window.
this.userChangedValue(event.target);
},
_fireEvent(aEventName, aTarget) {
try {
const event = new CustomEvent(aEventName, {
bubbles: true,
cancelable: true,
});
return aTarget.dispatchEvent(event);
} catch (e) {
Cu.reportError(e);
}
return false;
},
onDialogAccept(event) {
let dialog = document.querySelector("dialog");
if (!this._fireEvent("beforeaccept", dialog)) {
event.preventDefault();
return false;
}
this.writePreferences(true);
return true;
},
close(event) {
if (Preferences.instantApply) {
window.close();
}
event.stopPropagation();
event.preventDefault();
},
handleEvent(event) {
switch (event.type) {
case "change":
return this.onChange(event);
case "command":
return this.onCommand(event);
case "dialogaccept":
return this.onDialogAccept(event);
case "input":
return this.onInput(event);
case "unload":
return this.onUnload(event);
default:
return undefined;
}
},
_syncFromPrefListeners: new WeakMap(),
_syncToPrefListeners: new WeakMap(),
addSyncFromPrefListener(aElement, callback) {
this._syncFromPrefListeners.set(aElement, callback);
// Make sure elements are updated correctly with the listener attached.
let elementPref = aElement.getAttribute("preference");
if (elementPref) {
let pref = this.get(elementPref);
if (pref) {
pref.updateElements();
}
}
},
addSyncToPrefListener(aElement, callback) {
this._syncToPrefListeners.set(aElement, callback);
// Make sure elements are updated correctly with the listener attached.
let elementPref = aElement.getAttribute("preference");
if (elementPref) {
let pref = this.get(elementPref);
if (pref) {
pref.updateElements();
}
}
},
removeSyncFromPrefListener(aElement) {
this._syncFromPrefListeners.delete(aElement);
},
removeSyncToPrefListener(aElement) {
this._syncToPrefListeners.delete(aElement);
},
};
Services.prefs.addObserver("", Preferences);
domContentLoadedPromise.then(result =>
Preferences.onDOMContentLoaded(result)
);
window.addEventListener("change", Preferences);
window.addEventListener("command", Preferences);
window.addEventListener("dialogaccept", Preferences);
window.addEventListener("input", Preferences);
window.addEventListener("select", Preferences);
window.addEventListener("unload", Preferences, { once: true });
class Preference extends EventEmitter {
constructor({ id, type, inverted }) {
super();
this.on("change", this.onChange.bind(this));
this._value = null;
this.readonly = false;
this._useDefault = false;
this.batching = false;
this.id = id;
this.type = type;
this.inverted = !!inverted;
// In non-instant apply mode, we must try and use the last saved state
// from any previous opens of a child dialog instead of the value from
// preferences, to pick up any edits a user may have made.
if (
Preferences.type == "child" &&
window.opener &&
window.opener.Preferences &&
window.opener.document.nodePrincipal.isSystemPrincipal
) {
// Try to find the preference in the parent window.
const preference = window.opener.Preferences.get(this.id);
// Don't use the value setter here, we don't want updateElements to be
// prematurely fired.
this._value = preference ? preference.value : this.valueFromPreferences;
} else {
this._value = this.valueFromPreferences;
}
}
reset() {
// defer reset until preference update
this.value = undefined;
}
_reportUnknownType() {
const msg = `Preference with id=${this.id} has unknown type ${this.type}.`;
Services.console.logStringMessage(msg);
}
setElementValue(aElement) {
if (this.locked) {
aElement.disabled = true;
}
if (!this.isElementEditable(aElement)) {
return;
}
let rv = undefined;
if (Preferences._syncFromPrefListeners.has(aElement)) {
rv = Preferences._syncFromPrefListeners.get(aElement)(aElement);
}
let val = rv;
if (val === undefined) {
val = Preferences.instantApply ? this.valueFromPreferences : this.value;
}
// if the preference is marked for reset, show default value in UI
if (val === undefined) {
val = this.defaultValue;
}
/**
* Initialize a UI element property with a value. Handles the case
* where an element has not yet had a XBL binding attached for it and
* the property setter does not yet exist by setting the same attribute
* on the XUL element using DOM apis and assuming the element's
* constructor or property getters appropriately handle this state.
*/
function setValue(element, attribute, value) {
if (attribute in element) {
element[attribute] = value;
} else if (attribute === "checked") {
// The "checked" attribute can't simply be set to the specified value;
// it has to be set if the value is true and removed if the value
// is false in order to be interpreted correctly by the element.
if (value) {
// In theory we can set it to anything; however xbl implementation
// of `checkbox` only works with "true".
element.setAttribute(attribute, "true");
} else {
element.removeAttribute(attribute);
}
} else {
element.setAttribute(attribute, value);
}
}
if (aElement.localName == "checkbox") {
setValue(aElement, "checked", val);
} else {
setValue(aElement, "value", val);
}
}
getElementValue(aElement) {
if (Preferences._syncToPrefListeners.has(aElement)) {
try {
const rv = Preferences._syncToPrefListeners.get(aElement)(aElement);
if (rv !== undefined) {
return rv;
}
} catch (e) {
Cu.reportError(e);
}
}
/**
* Read the value of an attribute from an element, assuming the
* attribute is a property on the element's node API. If the property
* is not present in the API, then assume its value is contained in
* an attribute, as is the case before a binding has been attached.
*/
function getValue(element, attribute) {
if (attribute in element) {
return element[attribute];
}
return element.getAttribute(attribute);
}
let value;
if (aElement.localName == "checkbox") {
value = getValue(aElement, "checked");
} else {
value = getValue(aElement, "value");
}
switch (this.type) {
case "int":
return parseInt(value, 10) || 0;
case "bool":
return typeof value == "boolean" ? value : value == "true";
}
return value;
}
isElementEditable(aElement) {
switch (aElement.localName) {
case "checkbox":
case "input":
case "radiogroup":
case "textarea":
case "menulist":
return true;
}
return false;
}
updateElements() {
if (!this.id) {
return;
}
// This "change" event handler tracks changes made to preferences by
// sources other than the user in this window.
const elements = getElementsByAttribute("preference", this.id);
for (const element of elements) {
this.setElementValue(element);
}
}
onChange() {
this.updateElements();
}
get value() {
return this._value;
}
set value(val) {
if (this.value !== val) {
this._value = val;
if (Preferences.instantApply) {
this.valueFromPreferences = val;
}
this.emit("change");
}
return val;
}
get locked() {
return Services.prefs.prefIsLocked(this.id);
}
updateControlDisabledState(val) {
if (!this.id) {
return;
}
val = val || this.locked;
const elements = getElementsByAttribute("preference", this.id);
for (const element of elements) {
element.disabled = val;
const labels = getElementsByAttribute("control", element.id);
for (const label of labels) {
label.disabled = val;
}
}
}
get hasUserValue() {
return (
Services.prefs.prefHasUserValue(this.id) && this.value !== undefined
);
}
get defaultValue() {
this._useDefault = true;
const val = this.valueFromPreferences;
this._useDefault = false;
return val;
}
get _branch() {
return this._useDefault ? Preferences.defaultBranch : Services.prefs;
}
get valueFromPreferences() {
try {
// Force a resync of value with preferences.
switch (this.type) {
case "int":
return this._branch.getIntPref(this.id);
case "bool": {
const val = this._branch.getBoolPref(this.id);
return this.inverted ? !val : val;
}
case "wstring":
return this._branch.getComplexValue(
this.id,
Ci.nsIPrefLocalizedString
).data;
case "string":
case "unichar":
return this._branch.getStringPref(this.id);
case "fontname": {
const family = this._branch.getStringPref(this.id);
const fontEnumerator = Cc[
"@mozilla.org/gfx/fontenumerator;1"
].createInstance(Ci.nsIFontEnumerator);
return fontEnumerator.getStandardFamilyName(family);
}
case "file": {
const f = this._branch.getComplexValue(this.id, Ci.nsIFile);
return f;
}
default:
this._reportUnknownType();
}
} catch (e) {}
return null;
}
set valueFromPreferences(val) {
// Exit early if nothing to do.
if (this.readonly || this.valueFromPreferences == val) {
return val;
}
// The special value undefined means 'reset preference to default'.
if (val === undefined) {
Services.prefs.clearUserPref(this.id);
return val;
}
// Force a resync of preferences with value.
switch (this.type) {
case "int":
Services.prefs.setIntPref(this.id, val);
break;
case "bool":
Services.prefs.setBoolPref(this.id, this.inverted ? !val : val);
break;
case "wstring": {
const pls = Cc["@mozilla.org/pref-localizedstring;1"].createInstance(
Ci.nsIPrefLocalizedString
);
pls.data = val;
Services.prefs.setComplexValue(
this.id,
Ci.nsIPrefLocalizedString,
pls
);
break;
}
case "string":
case "unichar":
case "fontname":
Services.prefs.setStringPref(this.id, val);
break;
case "file": {
let lf;
if (typeof val == "string") {
lf = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
lf.persistentDescriptor = val;
if (!lf.exists()) {
lf.initWithPath(val);
}
} else {
lf = val.QueryInterface(Ci.nsIFile);
}
Services.prefs.setComplexValue(this.id, Ci.nsIFile, lf);
break;
}
default:
this._reportUnknownType();
}
if (!this.batching) {
Services.prefs.savePrefFile(null);
}
return val;
}
}
return Preferences;
})());
|