mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-10 13:18:45 +02:00
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
142 lines
5.2 KiB
JavaScript
142 lines
5.2 KiB
JavaScript
/* 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";
|
|
|
|
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");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
|
|
"resource://gre/modules/PrivateBrowsingUtils.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "AppConstants",
|
|
"resource://gre/modules/AppConstants.jsm");
|
|
|
|
// 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"],
|
|
["section.topstories.collapsed", "topstories"],
|
|
]),
|
|
};
|
|
|
|
this.PrefsFeed = class PrefsFeed {
|
|
constructor(prefMap) {
|
|
this._prefMap = prefMap;
|
|
this._prefs = new Prefs();
|
|
}
|
|
|
|
// If any of the prefs are set to something other than what the
|
|
// prerendered version of AS expects, we can't use it.
|
|
async _setPrerenderPref() {
|
|
const indexedDBPrefs = await this._storage.getAll();
|
|
const prefsAreValid = PrerenderData.arePrefsValid(pref => this._prefs.get(pref), indexedDBPrefs);
|
|
this._prefs.set("prerender", prefsAreValid);
|
|
}
|
|
|
|
_checkPrerender(name) {
|
|
if (PrerenderData.invalidatingPrefs.includes(name)) {
|
|
this._setPrerenderPref();
|
|
}
|
|
}
|
|
|
|
onPrefChanged(name, value) {
|
|
if (this._prefMap.has(name)) {
|
|
this.store.dispatch(ac.BroadcastToContent({type: at.PREF_CHANGED, data: {name, value}}));
|
|
}
|
|
|
|
this._checkPrerender(name);
|
|
}
|
|
|
|
_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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
init() {
|
|
this._prefs.observeBranch(this);
|
|
this._storage = this.store.dbStorage.getDbTable("sectionPrefs");
|
|
|
|
// Get the initial value of each activity stream pref
|
|
const values = {};
|
|
for (const name of this._prefMap.keys()) {
|
|
values[name] = this._prefs.get(name);
|
|
}
|
|
|
|
// These are not prefs, but are needed to determine stuff in content that can only be
|
|
// computed in main process
|
|
values.isPrivateBrowsingEnabled = PrivateBrowsingUtils.enabled;
|
|
values.platform = AppConstants.platform;
|
|
|
|
// 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");
|
|
|
|
// 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);
|
|
|
|
// 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);
|
|
|
|
// Set the initial state of all prefs in redux
|
|
this.store.dispatch(ac.BroadcastToContent({type: at.PREFS_INITIAL_VALUES, data: values}));
|
|
|
|
this._migratePrefs();
|
|
this._setPrerenderPref();
|
|
}
|
|
|
|
removeListeners() {
|
|
this._prefs.ignoreBranch(this);
|
|
}
|
|
|
|
async _setIndexedDBPref(id, value) {
|
|
const name = id === "topsites" ? id : `feeds.section.${id}`;
|
|
try {
|
|
await this._storage.set(name, value);
|
|
this._setPrerenderPref();
|
|
} catch (e) {
|
|
Cu.reportError("Could not set section preferences.");
|
|
}
|
|
}
|
|
|
|
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;
|
|
case at.UPDATE_SECTION_PREFS:
|
|
this._setIndexedDBPref(action.data.id, action.data.value);
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
const EXPORTED_SYMBOLS = ["PrefsFeed"];
|