mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-10 05:08:36 +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
172 lines
5 KiB
JavaScript
172 lines
5 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 {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const PROVIDER_PREF_BRANCH = "browser.newtabpage.activity-stream.asrouter.providers.";
|
|
const DEVTOOLS_PREF = "browser.newtabpage.activity-stream.asrouter.devtoolsEnabled";
|
|
|
|
const DEFAULT_STATE = {
|
|
_initialized: false,
|
|
_providers: null,
|
|
_providerPrefBranch: PROVIDER_PREF_BRANCH,
|
|
_devtoolsEnabled: null,
|
|
_devtoolsPref: DEVTOOLS_PREF,
|
|
};
|
|
|
|
const USER_PREFERENCES = {
|
|
snippets: "browser.newtabpage.activity-stream.feeds.snippets",
|
|
cfr: "browser.newtabpage.activity-stream.asrouter.userprefs.cfr",
|
|
};
|
|
|
|
const TEST_PROVIDER = {
|
|
id: "snippets_local_testing",
|
|
type: "local",
|
|
localProvider: "SnippetsTestMessageProvider",
|
|
enabled: true,
|
|
};
|
|
|
|
class _ASRouterPreferences {
|
|
constructor() {
|
|
Object.assign(this, DEFAULT_STATE);
|
|
this._callbacks = new Set();
|
|
}
|
|
|
|
_getProviderConfig() {
|
|
const prefList = Services.prefs.getChildList(this._providerPrefBranch);
|
|
return prefList.reduce((filtered, pref) => {
|
|
let value;
|
|
try {
|
|
value = JSON.parse(Services.prefs.getStringPref(pref, ""));
|
|
} catch (e) {
|
|
Cu.reportError(`Could not parse ASRouter preference. Try resetting ${pref} in about:config.`);
|
|
}
|
|
if (value) {
|
|
filtered.push(value);
|
|
}
|
|
return filtered;
|
|
}, []);
|
|
}
|
|
|
|
get providers() {
|
|
if (!this._initialized || this._providers === null) {
|
|
const config = this._getProviderConfig();
|
|
const providers = config.map(provider => Object.freeze(provider));
|
|
if (this.devtoolsEnabled) {
|
|
providers.unshift(TEST_PROVIDER);
|
|
}
|
|
this._providers = Object.freeze(providers);
|
|
}
|
|
|
|
return this._providers;
|
|
}
|
|
|
|
enableOrDisableProvider(id, value) {
|
|
const providers = this._getProviderConfig();
|
|
const config = providers.find(p => p.id === id);
|
|
if (!config) {
|
|
Cu.reportError(`Cannot set enabled state for '${id}' because the pref ${this._providerPrefBranch}${id} does not exist or is not correctly formatted.`);
|
|
return;
|
|
}
|
|
|
|
Services.prefs.setStringPref(this._providerPrefBranch + id, JSON.stringify({...config, enabled: value}));
|
|
}
|
|
|
|
resetProviderPref() {
|
|
for (const pref of Services.prefs.getChildList(this._providerPrefBranch)) {
|
|
Services.prefs.clearUserPref(pref);
|
|
}
|
|
for (const id of Object.keys(USER_PREFERENCES)) {
|
|
Services.prefs.clearUserPref(USER_PREFERENCES[id]);
|
|
}
|
|
}
|
|
|
|
get devtoolsEnabled() {
|
|
if (!this._initialized || this._devtoolsEnabled === null) {
|
|
this._devtoolsEnabled = Services.prefs.getBoolPref(this._devtoolsPref, false);
|
|
}
|
|
return this._devtoolsEnabled;
|
|
}
|
|
|
|
get specialConditions() {
|
|
let allowLegacySnippets = true;
|
|
for (const provider of this.providers) {
|
|
if (provider.id === "snippets" && provider.enabled) {
|
|
allowLegacySnippets = false;
|
|
}
|
|
}
|
|
return {allowLegacySnippets};
|
|
}
|
|
|
|
observe(aSubject, aTopic, aPrefName) {
|
|
if (aPrefName && aPrefName.startsWith(this._providerPrefBranch)) {
|
|
this._providers = null;
|
|
} else if (aPrefName === this._devtoolsPref) {
|
|
this._providers = null;
|
|
this._devtoolsEnabled = null;
|
|
}
|
|
this._callbacks.forEach(cb => cb(aPrefName));
|
|
}
|
|
|
|
getUserPreference(providerId) {
|
|
if (!USER_PREFERENCES[providerId]) {
|
|
return null;
|
|
}
|
|
return Services.prefs.getBoolPref(USER_PREFERENCES[providerId], true);
|
|
}
|
|
|
|
getAllUserPreferences() {
|
|
const values = {};
|
|
for (const id of Object.keys(USER_PREFERENCES)) {
|
|
values[id] = this.getUserPreference(id);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
setUserPreference(providerId, value) {
|
|
if (!USER_PREFERENCES[providerId]) {
|
|
return;
|
|
}
|
|
Services.prefs.setBoolPref(USER_PREFERENCES[providerId], value);
|
|
}
|
|
|
|
addListener(callback) {
|
|
this._callbacks.add(callback);
|
|
}
|
|
|
|
removeListener(callback) {
|
|
this._callbacks.delete(callback);
|
|
}
|
|
|
|
init() {
|
|
if (this._initialized) {
|
|
return;
|
|
}
|
|
Services.prefs.addObserver(this._providerPrefBranch, this);
|
|
Services.prefs.addObserver(this._devtoolsPref, this);
|
|
for (const id of Object.keys(USER_PREFERENCES)) {
|
|
Services.prefs.addObserver(USER_PREFERENCES[id], this);
|
|
}
|
|
this._initialized = true;
|
|
}
|
|
|
|
uninit() {
|
|
if (this._initialized) {
|
|
Services.prefs.removeObserver(this._providerPrefBranch, this);
|
|
Services.prefs.removeObserver(this._devtoolsPref, this);
|
|
for (const id of Object.keys(USER_PREFERENCES)) {
|
|
Services.prefs.removeObserver(USER_PREFERENCES[id], this);
|
|
}
|
|
}
|
|
Object.assign(this, DEFAULT_STATE);
|
|
this._callbacks.clear();
|
|
}
|
|
}
|
|
this._ASRouterPreferences = _ASRouterPreferences;
|
|
|
|
this.ASRouterPreferences = new _ASRouterPreferences();
|
|
this.TEST_PROVIDER = TEST_PROVIDER;
|
|
|
|
const EXPORTED_SYMBOLS = ["_ASRouterPreferences", "ASRouterPreferences", "TEST_PROVIDER"];
|