ActivityStreamPrefs.jsm 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "use strict";
  5. const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
  6. const {Preferences} = ChromeUtils.import("resource://gre/modules/Preferences.jsm");
  7. const ACTIVITY_STREAM_PREF_BRANCH = "browser.newtabpage.activity-stream.";
  8. this.Prefs = class Prefs extends Preferences {
  9. /**
  10. * Prefs - A wrapper around Preferences that always sets the branch to
  11. * ACTIVITY_STREAM_PREF_BRANCH
  12. */
  13. constructor(branch = ACTIVITY_STREAM_PREF_BRANCH) {
  14. super({branch});
  15. this._branchObservers = new Map();
  16. }
  17. ignoreBranch(listener) {
  18. const observer = this._branchObservers.get(listener);
  19. this._prefBranch.removeObserver("", observer);
  20. this._branchObservers.delete(listener);
  21. }
  22. observeBranch(listener) {
  23. const observer = (subject, topic, pref) => {
  24. listener.onPrefChanged(pref, this.get(pref));
  25. };
  26. this._prefBranch.addObserver("", observer);
  27. this._branchObservers.set(listener, observer);
  28. }
  29. };
  30. this.DefaultPrefs = class DefaultPrefs extends Preferences {
  31. /**
  32. * DefaultPrefs - A helper for setting and resetting default prefs for the add-on
  33. *
  34. * @param {Map} config A Map with {string} key of the pref name and {object}
  35. * value with the following pref properties:
  36. * {string} .title (optional) A description of the pref
  37. * {bool|string|number} .value The default value for the pref
  38. * @param {string} branch (optional) The pref branch (defaults to ACTIVITY_STREAM_PREF_BRANCH)
  39. */
  40. constructor(config, branch = ACTIVITY_STREAM_PREF_BRANCH) {
  41. super({
  42. branch,
  43. defaultBranch: true,
  44. });
  45. this._config = config;
  46. }
  47. /**
  48. * init - Set default prefs for all prefs in the config
  49. */
  50. init() {
  51. // Local developer builds (with the default mozconfig) aren't OFFICIAL
  52. const IS_UNOFFICIAL_BUILD = !AppConstants.MOZILLA_OFFICIAL;
  53. for (const pref of this._config.keys()) {
  54. try {
  55. // Avoid replacing existing valid default pref values, e.g., those set
  56. // via Autoconfig or policy
  57. if (this.get(pref) !== undefined) {
  58. continue;
  59. }
  60. } catch (ex) {
  61. // We get NS_ERROR_UNEXPECTED for prefs that have a user value (causing
  62. // default branch to believe there's a type) but no actual default value
  63. }
  64. const prefConfig = this._config.get(pref);
  65. let value;
  66. if (IS_UNOFFICIAL_BUILD && "value_local_dev" in prefConfig) {
  67. value = prefConfig.value_local_dev;
  68. } else {
  69. value = prefConfig.value;
  70. }
  71. try {
  72. this.set(pref, value);
  73. } catch (ex) {
  74. // Potentially the user somehow set an unexpected value type, so we fail
  75. // to set a default of our expected type
  76. }
  77. }
  78. }
  79. };
  80. const EXPORTED_SYMBOLS = ["DefaultPrefs", "Prefs"];