ActivityStreamPrefs.sys.mjs (3411B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // We use importESModule here instead of static import so that 6 // the Karma test environment won't choke on this module. This 7 // is because the Karma test environment already stubs out 8 // AppConstants, and overrides importESModule to be a no-op (which 9 // can't be done for a static import statement). 10 11 // eslint-disable-next-line mozilla/use-static-import 12 const { AppConstants } = ChromeUtils.importESModule( 13 "resource://gre/modules/AppConstants.sys.mjs" 14 ); 15 // eslint-disable-next-line mozilla/use-static-import 16 const { Preferences } = ChromeUtils.importESModule( 17 "resource://gre/modules/Preferences.sys.mjs" 18 ); 19 20 const ACTIVITY_STREAM_PREF_BRANCH = "browser.newtabpage.activity-stream."; 21 22 export class Prefs extends Preferences { 23 /** 24 * Prefs - A wrapper around Preferences that always sets the branch to 25 * ACTIVITY_STREAM_PREF_BRANCH 26 */ 27 constructor(branch = ACTIVITY_STREAM_PREF_BRANCH) { 28 super({ branch }); 29 this._branchObservers = new Map(); 30 } 31 32 ignoreBranch(listener) { 33 const observer = this._branchObservers.get(listener); 34 this._prefBranch.removeObserver("", observer); 35 this._branchObservers.delete(listener); 36 } 37 38 observeBranch(listener) { 39 const observer = (subject, topic, pref) => { 40 listener.onPrefChanged(pref, this.get(pref)); 41 }; 42 this._prefBranch.addObserver("", observer); 43 this._branchObservers.set(listener, observer); 44 } 45 } 46 47 export class DefaultPrefs extends Preferences { 48 /** 49 * DefaultPrefs - A helper for setting and resetting default prefs for the add-on 50 * 51 * @param {Map} config A Map with {string} key of the pref name and {object} 52 * value with the following pref properties: 53 * {string} .title (optional) A description of the pref 54 * {bool|string|number} .value The default value for the pref 55 * @param {string} branch (optional) The pref branch (defaults to ACTIVITY_STREAM_PREF_BRANCH) 56 */ 57 constructor(config, branch = ACTIVITY_STREAM_PREF_BRANCH) { 58 super({ 59 branch, 60 defaultBranch: true, 61 }); 62 this._config = config; 63 } 64 65 /** 66 * init - Set default prefs for all prefs in the config 67 */ 68 init() { 69 // Local developer builds (with the default mozconfig) aren't OFFICIAL 70 const IS_UNOFFICIAL_BUILD = !AppConstants.MOZILLA_OFFICIAL; 71 72 for (const pref of this._config.keys()) { 73 try { 74 // Avoid replacing existing valid default pref values, e.g., those set 75 // via Autoconfig or policy 76 if (this.get(pref) !== undefined) { 77 continue; 78 } 79 } catch (ex) { 80 // We get NS_ERROR_UNEXPECTED for prefs that have a user value (causing 81 // default branch to believe there's a type) but no actual default value 82 } 83 84 const prefConfig = this._config.get(pref); 85 let value; 86 if (IS_UNOFFICIAL_BUILD && "value_local_dev" in prefConfig) { 87 value = prefConfig.value_local_dev; 88 } else { 89 value = prefConfig.value; 90 } 91 92 try { 93 this.set(pref, value); 94 } catch (ex) { 95 // Potentially the user somehow set an unexpected value type, so we fail 96 // to set a default of our expected type 97 } 98 } 99 } 100 }