PrefsFeed.jsm 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 {actionCreators: ac, actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/Actions.jsm");
  6. const {Prefs} = ChromeUtils.import("resource://activity-stream/lib/ActivityStreamPrefs.jsm");
  7. const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
  8. ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
  9. "resource://gre/modules/PrivateBrowsingUtils.jsm");
  10. ChromeUtils.defineModuleGetter(this, "AppConstants",
  11. "resource://gre/modules/AppConstants.jsm");
  12. this.PrefsFeed = class PrefsFeed {
  13. constructor(prefMap) {
  14. this._prefMap = prefMap;
  15. this._prefs = new Prefs();
  16. }
  17. onPrefChanged(name, value) {
  18. const prefItem = this._prefMap.get(name);
  19. if (prefItem) {
  20. this.store.dispatch(ac[prefItem.skipBroadcast ? "OnlyToMain" : "BroadcastToContent"]({type: at.PREF_CHANGED, data: {name, value}}));
  21. }
  22. }
  23. init() {
  24. this._prefs.observeBranch(this);
  25. this._storage = this.store.dbStorage.getDbTable("sectionPrefs");
  26. // Get the initial value of each activity stream pref
  27. const values = {};
  28. for (const name of this._prefMap.keys()) {
  29. values[name] = this._prefs.get(name);
  30. }
  31. // These are not prefs, but are needed to determine stuff in content that can only be
  32. // computed in main process
  33. values.isPrivateBrowsingEnabled = PrivateBrowsingUtils.enabled;
  34. values.platform = AppConstants.platform;
  35. // Get the firefox accounts url for links and to send firstrun metrics to.
  36. values.fxa_endpoint = Services.prefs.getStringPref(
  37. "browser.newtabpage.activity-stream.fxaccounts.endpoint", "https://accounts.firefox.com");
  38. // Read the pref for search shortcuts top sites experiment from firefox.js and store it
  39. // in our interal list of prefs to watch
  40. let searchTopSiteExperimentPrefValue = Services.prefs.getBoolPref(
  41. "browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts");
  42. values["improvesearch.topSiteSearchShortcuts"] = searchTopSiteExperimentPrefValue;
  43. this._prefMap.set("improvesearch.topSiteSearchShortcuts", {value: searchTopSiteExperimentPrefValue});
  44. // Read the pref for search hand-off from firefox.js and store it
  45. // in our interal list of prefs to watch
  46. let handoffToAwesomebarPrefValue = Services.prefs.getBoolPref(
  47. "browser.newtabpage.activity-stream.improvesearch.handoffToAwesomebar");
  48. values["improvesearch.handoffToAwesomebar"] = handoffToAwesomebarPrefValue;
  49. this._prefMap.set("improvesearch.handoffToAwesomebar", {value: handoffToAwesomebarPrefValue});
  50. // Set the initial state of all prefs in redux
  51. this.store.dispatch(ac.BroadcastToContent({type: at.PREFS_INITIAL_VALUES, data: values}));
  52. }
  53. removeListeners() {
  54. this._prefs.ignoreBranch(this);
  55. }
  56. async _setIndexedDBPref(id, value) {
  57. const name = id === "topsites" ? id : `feeds.section.${id}`;
  58. try {
  59. await this._storage.set(name, value);
  60. } catch (e) {
  61. Cu.reportError("Could not set section preferences.");
  62. }
  63. }
  64. onAction(action) {
  65. switch (action.type) {
  66. case at.INIT:
  67. this.init();
  68. break;
  69. case at.UNINIT:
  70. this.removeListeners();
  71. break;
  72. case at.SET_PREF:
  73. this._prefs.set(action.data.name, action.data.value);
  74. break;
  75. case at.UPDATE_SECTION_PREFS:
  76. this._setIndexedDBPref(action.data.id, action.data.value);
  77. break;
  78. }
  79. }
  80. };
  81. const EXPORTED_SYMBOLS = ["PrefsFeed"];