2017-06-07 23:43:57 +03:00
|
|
|
/* 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";
|
|
|
|
|
2019-01-30 00:41:48 +03:00
|
|
|
const {actionCreators: ac, actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/Actions.jsm");
|
|
|
|
const {Prefs} = ChromeUtils.import("resource://activity-stream/lib/ActivityStreamPrefs.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2017-09-19 22:01:48 +03:00
|
|
|
|
2018-03-01 21:23:11 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
|
|
|
|
"resource://gre/modules/PrivateBrowsingUtils.jsm");
|
|
|
|
|
2018-04-20 06:05:20 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "AppConstants",
|
|
|
|
"resource://gre/modules/AppConstants.jsm");
|
2017-06-07 23:43:57 +03:00
|
|
|
|
|
|
|
this.PrefsFeed = class PrefsFeed {
|
2017-06-23 21:10:59 +03:00
|
|
|
constructor(prefMap) {
|
|
|
|
this._prefMap = prefMap;
|
2017-06-07 23:43:57 +03:00
|
|
|
this._prefs = new Prefs();
|
|
|
|
}
|
2017-09-06 00:17:01 +03:00
|
|
|
|
2017-06-07 23:43:57 +03:00
|
|
|
onPrefChanged(name, value) {
|
2019-02-16 01:36:37 +03:00
|
|
|
const prefItem = this._prefMap.get(name);
|
|
|
|
if (prefItem) {
|
|
|
|
this.store.dispatch(ac[prefItem.skipBroadcast ? "OnlyToMain" : "BroadcastToContent"]({type: at.PREF_CHANGED, data: {name, value}}));
|
2017-06-23 21:10:59 +03:00
|
|
|
}
|
2017-06-07 23:43:57 +03:00
|
|
|
}
|
2017-09-06 00:17:01 +03:00
|
|
|
|
2018-03-29 21:05:31 +03:00
|
|
|
init() {
|
2017-06-23 21:10:59 +03:00
|
|
|
this._prefs.observeBranch(this);
|
2018-04-20 19:18:44 +03:00
|
|
|
this._storage = this.store.dbStorage.getDbTable("sectionPrefs");
|
2017-06-07 23:43:57 +03:00
|
|
|
|
2017-06-23 21:10:59 +03:00
|
|
|
// Get the initial value of each activity stream pref
|
|
|
|
const values = {};
|
|
|
|
for (const name of this._prefMap.keys()) {
|
2017-06-07 23:43:57 +03:00
|
|
|
values[name] = this._prefs.get(name);
|
|
|
|
}
|
|
|
|
|
2018-04-20 06:05:20 +03:00
|
|
|
// These are not prefs, but are needed to determine stuff in content that can only be
|
|
|
|
// computed in main process
|
2018-03-01 21:23:11 +03:00
|
|
|
values.isPrivateBrowsingEnabled = PrivateBrowsingUtils.enabled;
|
2018-04-20 06:05:20 +03:00
|
|
|
values.platform = AppConstants.platform;
|
2018-03-01 21:23:11 +03:00
|
|
|
|
2018-07-13 18:38:19 +03:00
|
|
|
// Get the firefox accounts url for links and to send firstrun metrics to.
|
|
|
|
values.fxa_endpoint = Services.prefs.getStringPref(
|
|
|
|
"browser.newtabpage.activity-stream.fxaccounts.endpoint", "https://accounts.firefox.com");
|
|
|
|
|
2018-10-01 22:57:19 +03:00
|
|
|
// Read the pref for search shortcuts top sites experiment from firefox.js and store it
|
|
|
|
// in our interal list of prefs to watch
|
|
|
|
let searchTopSiteExperimentPrefValue = Services.prefs.getBoolPref(
|
|
|
|
"browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts");
|
|
|
|
values["improvesearch.topSiteSearchShortcuts"] = searchTopSiteExperimentPrefValue;
|
2019-02-19 03:09:35 +03:00
|
|
|
this._prefMap.set("improvesearch.topSiteSearchShortcuts", {value: searchTopSiteExperimentPrefValue});
|
2018-10-01 22:57:19 +03:00
|
|
|
|
2019-01-11 17:11:03 +03:00
|
|
|
// Read the pref for search hand-off from firefox.js and store it
|
|
|
|
// in our interal list of prefs to watch
|
|
|
|
let handoffToAwesomebarPrefValue = Services.prefs.getBoolPref(
|
|
|
|
"browser.newtabpage.activity-stream.improvesearch.handoffToAwesomebar");
|
|
|
|
values["improvesearch.handoffToAwesomebar"] = handoffToAwesomebarPrefValue;
|
2019-02-19 03:09:35 +03:00
|
|
|
this._prefMap.set("improvesearch.handoffToAwesomebar", {value: handoffToAwesomebarPrefValue});
|
2019-01-11 17:11:03 +03:00
|
|
|
|
2017-06-07 23:43:57 +03:00
|
|
|
// Set the initial state of all prefs in redux
|
|
|
|
this.store.dispatch(ac.BroadcastToContent({type: at.PREFS_INITIAL_VALUES, data: values}));
|
|
|
|
}
|
2018-01-19 03:28:26 +03:00
|
|
|
|
2017-06-07 23:43:57 +03:00
|
|
|
removeListeners() {
|
2017-06-23 21:10:59 +03:00
|
|
|
this._prefs.ignoreBranch(this);
|
2017-06-07 23:43:57 +03:00
|
|
|
}
|
2018-01-19 03:28:26 +03:00
|
|
|
|
2018-03-28 22:39:36 +03:00
|
|
|
async _setIndexedDBPref(id, value) {
|
|
|
|
const name = id === "topsites" ? id : `feeds.section.${id}`;
|
2018-04-20 19:18:44 +03:00
|
|
|
try {
|
|
|
|
await this._storage.set(name, value);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError("Could not set section preferences.");
|
|
|
|
}
|
2018-03-28 22:39:36 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 23:43:57 +03:00
|
|
|
onAction(action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case at.INIT:
|
|
|
|
this.init();
|
|
|
|
break;
|
|
|
|
case at.UNINIT:
|
|
|
|
this.removeListeners();
|
|
|
|
break;
|
|
|
|
case at.SET_PREF:
|
|
|
|
this._prefs.set(action.data.name, action.data.value);
|
|
|
|
break;
|
2018-03-28 22:39:36 +03:00
|
|
|
case at.UPDATE_SECTION_PREFS:
|
|
|
|
this._setIndexedDBPref(action.data.id, action.data.value);
|
|
|
|
break;
|
2017-06-07 23:43:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-01 22:08:44 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["PrefsFeed"];
|