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 {PrerenderData} = ChromeUtils.import("resource://activity-stream/common/PrerenderData.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
|
|
|
|
2018-03-28 22:39:36 +03:00
|
|
|
// List of prefs that require migration to indexedDB.
|
|
|
|
// Object key is the name of the pref in indexedDB, each will contain a
|
|
|
|
// map (key: name of preference to migrate, value: name of component).
|
|
|
|
const PREF_MIGRATION = {
|
|
|
|
collapsed: new Map([
|
|
|
|
["collapseTopSites", "topsites"],
|
|
|
|
["section.highlights.collapsed", "highlights"],
|
2018-09-14 23:18:00 +03:00
|
|
|
["section.topstories.collapsed", "topstories"],
|
|
|
|
]),
|
2018-03-28 22:39:36 +03:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2018-07-06 13:42:58 +03:00
|
|
|
// If any of the prefs are set to something other than what the
|
2018-03-13 18:43:29 +03:00
|
|
|
// prerendered version of AS expects, we can't use it.
|
2018-07-06 13:42:58 +03:00
|
|
|
async _setPrerenderPref() {
|
2018-03-28 22:39:36 +03:00
|
|
|
const indexedDBPrefs = await this._storage.getAll();
|
2018-03-13 18:43:29 +03:00
|
|
|
const prefsAreValid = PrerenderData.arePrefsValid(pref => this._prefs.get(pref), indexedDBPrefs);
|
2018-07-06 13:42:58 +03:00
|
|
|
this._prefs.set("prerender", prefsAreValid);
|
2017-09-06 00:17:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_checkPrerender(name) {
|
|
|
|
if (PrerenderData.invalidatingPrefs.includes(name)) {
|
|
|
|
this._setPrerenderPref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 23:43:57 +03:00
|
|
|
onPrefChanged(name, value) {
|
2017-06-23 21:10:59 +03:00
|
|
|
if (this._prefMap.has(name)) {
|
|
|
|
this.store.dispatch(ac.BroadcastToContent({type: at.PREF_CHANGED, data: {name, value}}));
|
|
|
|
}
|
2017-09-19 22:01:48 +03:00
|
|
|
|
2017-09-12 00:14:33 +03:00
|
|
|
this._checkPrerender(name);
|
2017-06-07 23:43:57 +03:00
|
|
|
}
|
2017-09-06 00:17:01 +03:00
|
|
|
|
2018-03-28 22:39:36 +03:00
|
|
|
_migratePrefs() {
|
|
|
|
for (const indexedDBPref of Object.keys(PREF_MIGRATION)) {
|
|
|
|
for (const migratePref of PREF_MIGRATION[indexedDBPref].keys()) {
|
|
|
|
// Check if pref exists (if the user changed the default)
|
|
|
|
if (this._prefs.get(migratePref, null) === true) {
|
|
|
|
const data = {id: PREF_MIGRATION[indexedDBPref].get(migratePref), value: {}};
|
|
|
|
data.value[indexedDBPref] = true;
|
|
|
|
this.store.dispatch(ac.OnlyToMain({type: at.UPDATE_SECTION_PREFS, data}));
|
|
|
|
this._prefs.reset(migratePref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
this._prefMap.set("improvesearch.topSiteSearchShortcuts", searchTopSiteExperimentPrefValue);
|
|
|
|
|
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;
|
|
|
|
this._prefMap.set("improvesearch.handoffToAwesomebar", handoffToAwesomebarPrefValue);
|
|
|
|
|
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}));
|
2017-09-06 00:17:01 +03:00
|
|
|
|
2018-03-29 21:05:31 +03:00
|
|
|
this._migratePrefs();
|
2017-09-06 00:17:01 +03:00
|
|
|
this._setPrerenderPref();
|
2017-06-07 23:43:57 +03:00
|
|
|
}
|
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);
|
|
|
|
this._setPrerenderPref();
|
|
|
|
} 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"];
|