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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* 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/. */
/**
* @file
* This module is used for managing preferences from WebExtension APIs.
* It takes care of the precedence chain and decides whether a preference
* needs to be updated when a change is requested by an API.
*
* It deals with preferences via settings objects, which are objects with
* the following properties:
*
* prefNames: An array of strings, each of which is a preference on
* which the setting depends.
* setCallback: A function that returns an object containing properties and
* values that correspond to the prefs to be set.
*/
export let ExtensionPreferencesManager;
import { Management } from "resource://gre/modules/Extension.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ExtensionCommon: "resource://gre/modules/ExtensionCommon.sys.mjs",
ExtensionSettingsStore:
"resource://gre/modules/ExtensionSettingsStore.sys.mjs",
Preferences: "resource://gre/modules/Preferences.sys.mjs",
});
import { ExtensionUtils } from "resource://gre/modules/ExtensionUtils.sys.mjs";
const { ExtensionError } = ExtensionUtils;
XPCOMUtils.defineLazyGetter(lazy, "defaultPreferences", function () {
return new lazy.Preferences({ defaultBranch: true });
});
/* eslint-disable mozilla/balanced-listeners */
Management.on("uninstall", async (type, { id }) => {
// Ensure managed preferences are cleared if they were
// not cleared at the module level.
await Management.asyncLoadSettingsModules();
return ExtensionPreferencesManager.removeAll(id);
});
Management.on("disable", async (type, id) => {
await Management.asyncLoadSettingsModules();
return ExtensionPreferencesManager.disableAll(id);
});
Management.on("enabling", async (type, id) => {
await Management.asyncLoadSettingsModules();
return ExtensionPreferencesManager.enableAll(id);
});
Management.on("change-permissions", (type, change) => {
// Called for added or removed, but we only care about removed here.
if (!change.removed) {
return;
}
ExtensionPreferencesManager.removeSettingsForPermissions(
change.extensionId,
change.removed.permissions
);
});
/* eslint-enable mozilla/balanced-listeners */
const STORE_TYPE = "prefs";
// Definitions of settings, each of which correspond to a different API.
let settingsMap = new Map();
/**
* This function is passed into the ExtensionSettingsStore to determine the
* initial value of the setting. It reads an array of preference names from
* the this scope, which gets bound to a settings object.
*
* @returns {object}
* An object with one property per preference, which holds the current
* value of that preference.
*/
function initialValueCallback() {
let initialValue = {};
for (let pref of this.prefNames) {
// If there is a prior user-set value, get it.
if (lazy.Preferences.isSet(pref)) {
initialValue[pref] = lazy.Preferences.get(pref);
}
}
return initialValue;
}
/**
* Updates the initialValue stored to exclude any values that match
* default preference values.
*
* @param {object} initialValue Initial Value data from settings store.
* @returns {object}
* The initialValue object after updating the values.
*/
function settingsUpdate(initialValue) {
for (let pref of this.prefNames) {
try {
if (
initialValue[pref] !== undefined &&
initialValue[pref] === lazy.defaultPreferences.get(pref)
) {
initialValue[pref] = undefined;
}
} catch (e) {
// Exception thrown if a default value doesn't exist. We
// presume that this pref had a user-set value initially.
}
}
return initialValue;
}
/**
* Loops through a set of prefs, either setting or resetting them.
*
* @param {string} name
* The api name of the setting.
* @param {object} setting
* An object that represents a setting, which will have a setCallback
* property. If a onPrefsChanged function is provided it will be called
* with item when the preferences change.
* @param {object} item
* An object that represents an item handed back from the setting store
* from which the new pref values can be calculated.
*/
function setPrefs(name, setting, item) {
let prefs = item.initialValue || setting.setCallback(item.value);
let changed = false;
for (let pref of setting.prefNames) {
if (prefs[pref] === undefined) {
if (lazy.Preferences.isSet(pref)) {
changed = true;
lazy.Preferences.reset(pref);
}
} else if (lazy.Preferences.get(pref) != prefs[pref]) {
lazy.Preferences.set(pref, prefs[pref]);
changed = true;
}
}
if (changed && typeof setting.onPrefsChanged == "function") {
setting.onPrefsChanged(item);
}
Management.emit(`extension-setting-changed:${name}`);
}
/**
* Commits a change to a setting and conditionally sets preferences.
*
* If the change to the setting causes a different extension to gain
* control of the pref (or removes all extensions with control over the pref)
* then the prefs should be updated, otherwise they should not be.
* In addition, if the current value of any of the prefs does not
* match what we expect the value to be (which could be the result of a
* user manually changing the pref value), then we do not change any
* of the prefs.
*
* @param {string} id
* The id of the extension for which a setting is being modified. Also
* see selectSetting.
* @param {string} name
* The name of the setting being processed.
* @param {string} action
* The action that is being performed. Will be one of disable, enable
* or removeSetting.
* @returns {Promise}
* Resolves to true if preferences were set as a result and to false
* if preferences were not set.
*/
async function processSetting(id, name, action) {
await lazy.ExtensionSettingsStore.initialize();
let expectedItem = lazy.ExtensionSettingsStore.getSetting(STORE_TYPE, name);
let item = lazy.ExtensionSettingsStore[action](id, STORE_TYPE, name);
if (item) {
let setting = settingsMap.get(name);
let expectedPrefs =
expectedItem.initialValue || setting.setCallback(expectedItem.value);
if (
Object.keys(expectedPrefs).some(
pref =>
expectedPrefs[pref] &&
lazy.Preferences.get(pref) != expectedPrefs[pref]
)
) {
return false;
}
setPrefs(name, setting, item);
return true;
}
return false;
}
ExtensionPreferencesManager = {
/**
* Adds a setting to the settingsMap. This is how an API tells the
* preferences manager what its setting object is. The preferences
* manager needs to know this when settings need to be removed
* automatically.
*
* @param {string} name The unique id of the setting.
* @param {object} setting
* A setting object that should have properties for
* prefNames, getCallback and setCallback.
*/
addSetting(name, setting) {
settingsMap.set(name, setting);
},
/**
* Gets the default value for a preference.
*
* @param {string} prefName The name of the preference.
*
* @returns {string|number|boolean} The default value of the preference.
*/
getDefaultValue(prefName) {
return lazy.defaultPreferences.get(prefName);
},
/**
* Returns a map of prefName to setting Name for use in about:config, about:preferences or
* other areas of Firefox that need to know whether a specific pref is controlled by an
* extension.
*
* Given a prefName, you can get the settingName. Call EPM.getSetting(settingName) to
* get the details of the setting, including which id if any is in control of the
* setting.
*
* @returns {Promise}
* Resolves to a Map of prefName->settingName
*/
async getManagedPrefDetails() {
await Management.asyncLoadSettingsModules();
let prefs = new Map();
settingsMap.forEach((setting, name) => {
for (let prefName of setting.prefNames) {
prefs.set(prefName, name);
}
});
return prefs;
},
/**
* Indicates that an extension would like to change the value of a previously
* defined setting.
*
* @param {string} id
* The id of the extension for which a setting is being set.
* @param {string} name
* The unique id of the setting.
* @param {any} value
* The value to be stored in the settings store for this
* group of preferences.
*
* @returns {Promise}
* Resolves to true if the preferences were changed and to false if
* the preferences were not changed.
*/
async setSetting(id, name, value) {
let setting = settingsMap.get(name);
await lazy.ExtensionSettingsStore.initialize();
let item = await lazy.ExtensionSettingsStore.addSetting(
id,
STORE_TYPE,
name,
value,
initialValueCallback.bind(setting),
name,
settingsUpdate.bind(setting)
);
if (item) {
setPrefs(name, setting, item);
return true;
}
return false;
},
/**
* Indicates that this extension wants to temporarily cede control over the
* given setting.
*
* @param {string} id
* The id of the extension for which a preference setting is being disabled.
* @param {string} name
* The unique id of the setting.
*
* @returns {Promise}
* Resolves to true if the preferences were changed and to false if
* the preferences were not changed.
*/
disableSetting(id, name) {
return processSetting(id, name, "disable");
},
/**
* Enable a setting that has been disabled.
*
* @param {string} id
* The id of the extension for which a setting is being enabled.
* @param {string} name
* The unique id of the setting.
*
* @returns {Promise}
* Resolves to true if the preferences were changed and to false if
* the preferences were not changed.
*/
enableSetting(id, name) {
return processSetting(id, name, "enable");
},
/**
* Specifically select an extension, the user, or the precedence order that will
* be in control of this setting.
*
* @param {string | null} id
* The id of the extension for which a setting is being selected, or
* ExtensionSettingStore.SETTING_USER_SET (null).
* @param {string} name
* The unique id of the setting.
*
* @returns {Promise}
* Resolves to true if the preferences were changed and to false if
* the preferences were not changed.
*/
selectSetting(id, name) {
return processSetting(id, name, "select");
},
/**
* Indicates that this extension no longer wants to set the given setting.
*
* @param {string} id
* The id of the extension for which a preference setting is being removed.
* @param {string} name
* The unique id of the setting.
*
* @returns {Promise}
* Resolves to true if the preferences were changed and to false if
* the preferences were not changed.
*/
removeSetting(id, name) {
return processSetting(id, name, "removeSetting");
},
/**
* Disables all previously set settings for an extension. This can be called when
* an extension is being disabled, for example.
*
* @param {string} id
* The id of the extension for which all settings are being unset.
*/
async disableAll(id) {
await lazy.ExtensionSettingsStore.initialize();
let settings = lazy.ExtensionSettingsStore.getAllForExtension(
id,
STORE_TYPE
);
let disablePromises = [];
for (let name of settings) {
disablePromises.push(this.disableSetting(id, name));
}
await Promise.all(disablePromises);
},
/**
* Enables all disabled settings for an extension. This can be called when
* an extension has finished updating or is being re-enabled, for example.
*
* @param {string} id
* The id of the extension for which all settings are being enabled.
*/
async enableAll(id) {
await lazy.ExtensionSettingsStore.initialize();
let settings = lazy.ExtensionSettingsStore.getAllForExtension(
id,
STORE_TYPE
);
let enablePromises = [];
for (let name of settings) {
enablePromises.push(this.enableSetting(id, name));
}
await Promise.all(enablePromises);
},
/**
* Removes all previously set settings for an extension. This can be called when
* an extension is being uninstalled, for example.
*
* @param {string} id
* The id of the extension for which all settings are being unset.
*/
async removeAll(id) {
await lazy.ExtensionSettingsStore.initialize();
let settings = lazy.ExtensionSettingsStore.getAllForExtension(
id,
STORE_TYPE
);
let removePromises = [];
for (let name of settings) {
removePromises.push(this.removeSetting(id, name));
}
await Promise.all(removePromises);
},
/**
* Removes a set of settings that are available under certain addon permissions.
*
* @param {string} id
* The extension id.
* @param {Array<string>} permissions
* The permission name from the extension manifest.
* @returns {Promise}
* A promise that resolves when all related settings are removed.
*/
async removeSettingsForPermissions(id, permissions) {
if (!permissions || !permissions.length) {
return;
}
await Management.asyncLoadSettingsModules();
let removePromises = [];
settingsMap.forEach((setting, name) => {
if (permissions.includes(setting.permission)) {
removePromises.push(this.removeSetting(id, name));
}
});
return Promise.all(removePromises);
},
/**
* Return the currently active value for a setting.
*
* @param {string} name
* The unique id of the setting.
*
* @returns {object} The current setting object.
*/
async getSetting(name) {
await lazy.ExtensionSettingsStore.initialize();
return lazy.ExtensionSettingsStore.getSetting(STORE_TYPE, name);
},
/**
* Return the levelOfControl for a setting / extension combo.
* This queries the levelOfControl from the ExtensionSettingsStore and also
* takes into account whether any of the setting's preferences are locked.
*
* @param {string} id
* The id of the extension for which levelOfControl is being requested.
* @param {string} name
* The unique id of the setting.
* @param {string} storeType
* The name of the store in ExtensionSettingsStore.
* Defaults to STORE_TYPE.
*
* @returns {Promise}
* Resolves to the level of control of the extension over the setting.
*/
async getLevelOfControl(id, name, storeType = STORE_TYPE) {
// This could be called for a setting that isn't defined to the PreferencesManager,
// in which case we simply defer to the SettingsStore.
if (storeType === STORE_TYPE) {
let setting = settingsMap.get(name);
if (!setting) {
return "not_controllable";
}
for (let prefName of setting.prefNames) {
if (lazy.Preferences.locked(prefName)) {
return "not_controllable";
}
}
}
await lazy.ExtensionSettingsStore.initialize();
return lazy.ExtensionSettingsStore.getLevelOfControl(id, storeType, name);
},
/**
* Returns an API object with get/set/clear used for a setting.
*
* @param {string|object} extensionId or params object
* @param {string} name
* The unique id of the setting.
* @param {Function} callback
* The function that retreives the current setting from prefs.
* @param {string} storeType
* The name of the store in ExtensionSettingsStore.
* Defaults to STORE_TYPE.
* @param {boolean} readOnly
* @param {Function} validate
* Utility function for any specific validation, such as checking
* for supported platform. Function should throw an error if necessary.
*
* @returns {object} API object with get/set/clear methods
*/
getSettingsAPI(
extensionId,
name,
callback,
storeType,
readOnly = false,
validate
) {
if (arguments.length > 1) {
Services.console.logStringMessage(
`ExtensionPreferencesManager.getSettingsAPI for ${name} should be updated to use a single paramater object.`
);
}
return ExtensionPreferencesManager._getInternalSettingsAPI(
arguments.length === 1
? extensionId
: {
extensionId,
name,
callback,
storeType,
readOnly,
validate,
}
).api;
},
/**
* getPrimedSettingsListener returns a function used to create
* a primed event listener.
*
* If a module overrides onChange then it must provide it's own
* persistent listener logic. See homepage_override in browserSettings
* for an example.
*
* addSetting must be called prior to priming listeners.
*
* @param {object} config see getSettingsAPI
* {Extension} extension, passed through to validate and used for extensionId
* {string} name
* The unique id of the settings api in the module, e.g. "settings"
* @returns {object} prime listener object
*/
getPrimedSettingsListener(config) {
let { name, extension } = config;
if (!name || !extension) {
throw new Error(
`name and extension are required for getPrimedSettingListener`
);
}
if (!settingsMap.get(name)) {
throw new Error(
`addSetting must be called prior to getPrimedSettingListener`
);
}
return ExtensionPreferencesManager._getInternalSettingsAPI({
name,
extension,
}).registerEvent;
},
/**
* Returns an object with a public API containing get/set/clear used for a setting,
* and a registerEvent function used for registering the event listener.
*
* @param {object} params The params object contains the following:
* {BaseContext} context
* {Extension} extension, optional, passed through to validate and used for extensionId
* {string} extensionId, optional to support old API
* {string} module
* The name of the api module, e.g. "proxy"
* {string} name
* The unique id of the settings api in the module, e.g. "settings"
* "name" should match the name given in the addSetting call.
* {Function} callback
* The function that retreives the current setting from prefs.
* {string} storeType
* The name of the store in ExtensionSettingsStore.
* Defaults to STORE_TYPE.
* {boolean} readOnly
* {Function} validate
* Utility function for any specific validation, such as checking
* for supported platform. Function should throw an error if necessary.
*
* @returns {object} internal API object with
* {object} api
* the public api available to extensions
* {Function} registerEvent
* the registration function used for priming events
*/
_getInternalSettingsAPI(params) {
let {
extensionId,
context,
extension,
module,
name,
callback,
storeType,
readOnly = false,
onChange,
validate,
} = params;
if (context) {
extension = context.extension;
}
if (!extensionId && extension) {
extensionId = extension.id;
}
const checkScope = details => {
let { scope } = details;
if (scope && scope !== "regular") {
throw new ExtensionError(
`Firefox does not support the ${scope} settings scope.`
);
}
};
// Check the setting for anything we may need.
let setting = settingsMap.get(name);
readOnly = readOnly || !!setting?.readOnly;
validate = validate || setting?.validate || (() => {});
let getValue = callback || setting?.getCallback;
if (!getValue || typeof getValue !== "function") {
throw new Error(`Invalid get callback for setting ${name} in ${module}`);
}
let settingsAPI = {
async get(details) {
validate(extension);
let levelOfControl = details.incognito
? "not_controllable"
: await ExtensionPreferencesManager.getLevelOfControl(
extensionId,
name,
storeType
);
levelOfControl =
readOnly && levelOfControl === "controllable_by_this_extension"
? "not_controllable"
: levelOfControl;
return {
levelOfControl,
value: await getValue(),
};
},
set(details) {
validate(extension);
checkScope(details);
if (!readOnly) {
return ExtensionPreferencesManager.setSetting(
extensionId,
name,
details.value
);
}
return false;
},
clear(details) {
validate(extension);
checkScope(details);
if (!readOnly) {
return ExtensionPreferencesManager.removeSetting(extensionId, name);
}
return false;
},
onChange,
};
let registerEvent = fire => {
let listener = async () => {
fire.async(await settingsAPI.get({}));
};
Management.on(`extension-setting-changed:${name}`, listener);
return {
unregister: () => {
Management.off(`extension-setting-changed:${name}`, listener);
},
convert(_fire) {
fire = _fire;
},
};
};
// Any caller using the old call signature will not have passed
// context to us. This should only be experimental addons in the
// wild.
if (onChange === undefined && context) {
// Some settings that are read-only may not have called addSetting, in
// which case we have no way to listen on the pref changes.
if (setting) {
settingsAPI.onChange = new lazy.ExtensionCommon.EventManager({
context,
module,
event: name,
name: `${name}.onChange`,
register: fire => {
return registerEvent(fire).unregister;
},
}).api();
} else {
Services.console.logStringMessage(
`ExtensionPreferencesManager API ${name} created but addSetting was not called.`
);
}
}
return { api: settingsAPI, registerEvent };
},
};
|