This commit is contained in:
Erica Wright 2019-06-07 14:21:38 -04:00
Родитель f25c04abd1
Коммит c66f42ea56
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: D9F2AF9D67D0AAB7
1 изменённых файлов: 30 добавлений и 36 удалений

Просмотреть файл

@ -2,17 +2,12 @@
Cu.importGlobalProperties(["fetch"]);
ChromeUtils.defineModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(this, "ExtensionCommon",
"resource://gre/modules/ExtensionCommon.jsm");
ChromeUtils.defineModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
ChromeUtils.defineModuleGetter(this, "Preferences",
"resource://gre/modules/Preferences.jsm");
const DefaultPreferences = new Preferences({ defaultBranch: true });
let gExtension;
this.multipreffer = class extends ExtensionAPI {
@ -29,6 +24,10 @@ this.multipreffer = class extends ExtensionAPI {
};
this.FirefoxHooks = {
// Default branch prefs can't be "reset" since they ARE
// (supposed to be) the defaults. So in order to reset
// them at cleanup, we cache the values of our target
// prefs in this object before modifying them.
_oldDefaultValues: {},
async studyReady(studyInfo) {
// Called every time the add-on is loaded.
@ -43,62 +42,57 @@ this.FirefoxHooks = {
const variationName =
Preferences.get(
"extensions.multipreffer.test.variationName", studyInfo.variation.name);
const prefs = this.variations[variationName].prefs;
for (const pref of Object.keys(prefs)) {
let val = Preferences.get(pref);
if (val === undefined) {
// If undefined, save it as an empty string.
// If undefined, save it as a false-y value.
// This is the best we can do to reset it at cleanup
// since there's no way to clear the value of a pref
// on the default branch.
val = "";
switch (typeof prefs[pref]) {
case "string":
val = "";
break;
case "boolean":
val = false;
break;
case "number":
val = 0;
break;
}
}
this._oldDefaultValues[pref] = val;
}
DefaultPreferences.set(prefs);
this.recomputeCategoryPref();
AddonManager.addAddonListener(this);
},
/** Called when the add-on is being removed for any reason. */
cleanup() {
// Called when the add-on is being removed for any reason.
DefaultPreferences.set(this._oldDefaultValues);
this.recomputeCategoryPref();
},
recomputeCategoryPref() {
// BrowserGlue will recompute the category pref when the cookie behavior
// pref changes value, but only if the category pref is unset. So, we
// need to change the cookie behavior pref, which will recompute the
// category pref. Then, clear the newly recomputed category pref, and
// finally restore the cookie behavior pref again which will recompute
// the category pref in the correct state.
const cookieBehaviorValue =
Preferences.get("network.cookie.cookieBehavior");
const tempCookieBehaviorValue = cookieBehaviorValue === 4 ? 0 : 4;
Preferences.set("network.cookie.cookieBehavior", tempCookieBehaviorValue);
Preferences.reset("browser.contentblocking.category");
Preferences.set("network.cookie.cookieBehavior", cookieBehaviorValue);
AddonManager.removeAddonListener(this);
},
onUninstalling(addon) {
this.handleDisableOrUninstall(addon);
},
onDisabled(addon) {
this.handleDisableOrUninstall(addon);
},
async handleDisableOrUninstall(addon) {
if (addon.id !== gExtension.id) {
return;
}
this.cleanup();
AddonManager.removeAddonListener(this);
// This is needed even for onUninstalling, because it nukes the addon
// from UI. If we don't do this, the user has a chance to "undo".
// This is needed because it nukes the addon from about:addons UI.
// If we don't do this, the user has a chance to "undo".
addon.uninstall();
},
onDisabled(addon) {
if (addon.id !== gExtension.id) {
return;
}
this.cleanup();
},
};