Reset prefs for new users on uninstall. Fixes #782

This commit is contained in:
Jonathan Kingston 2017-08-31 15:00:40 -07:00
parent 27b2a4b5f2
commit 10c4395efd

60
bootstrap.js vendored
View file

@ -4,22 +4,26 @@ const PREFS = [
{ {
name: "privacy.userContext.enabled", name: "privacy.userContext.enabled",
value: true, value: true,
type: "bool" type: "bool",
default: false
}, },
{ {
name: "privacy.userContext.longPressBehavior", name: "privacy.userContext.longPressBehavior",
value: 2, value: 2,
type: "int" type: "int",
default: 0
}, },
{ {
name: "privacy.userContext.ui.enabled", name: "privacy.userContext.ui.enabled",
value: true, // Post web ext we will be setting this true value: true, // Post web ext we will be setting this true
type: "bool" type: "bool",
default: true
}, },
{ {
name: "privacy.usercontext.about_newtab_segregation.enabled", name: "privacy.usercontext.about_newtab_segregation.enabled",
value: true, value: true,
type: "bool" type: "bool",
default: false
}, },
]; ];
const Ci = Components.interfaces; const Ci = Components.interfaces;
@ -66,14 +70,27 @@ function filename() {
return storeFile.path; return storeFile.path;
} }
async function getConfig() { async function makeFilepath() {
const bytes = await OS.File.read(filename()); const storeFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
const raw = new TextDecoder().decode(bytes) || ""; storeFile.append(JETPACK_DIR_BASENAME);
let savedConfig = {savedConfiguration: {}}; await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
if (raw) { storeFile.append(EXTENSION_ID);
savedConfig = JSON.parse(raw); await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
} storeFile.append("simple-storage");
await OS.File.makeDir(storeFile.path, { ignoreExisting: true });
}
async function getConfig() {
let savedConfig = {savedConfiguration: {}};
try {
const bytes = await OS.File.read(filename());
const raw = new TextDecoder().decode(bytes) || "";
if (raw) {
savedConfig = JSON.parse(raw);
}
} catch (e) {
// ignore file read errors, sometimes they happen and I'm not sure if we can fix
}
return savedConfig; return savedConfig;
} }
@ -84,14 +101,15 @@ async function initConfig() {
savedConfig.savedConfiguration.prefs = {}; savedConfig.savedConfiguration.prefs = {};
PREFS.forEach((pref) => { PREFS.forEach((pref) => {
if ("int" === pref.type) { if ("int" === pref.type) {
savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getIntPref(pref.name, pref.name); savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getIntPref(pref.name);
} else { } else {
savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getBoolPref(pref.name, pref.value); savedConfig.savedConfiguration.prefs[pref.name] = Services.prefs.getBoolPref(pref.name);
} }
}); });
} }
const serialized = JSON.stringify(savedConfig); const serialized = JSON.stringify(savedConfig);
const bytes = new TextEncoder().encode(serialized) || ""; const bytes = new TextEncoder().encode(serialized) || "";
await makeFilepath();
await OS.File.writeAtomic(filename(), bytes, { }); await OS.File.writeAtomic(filename(), bytes, { });
} }
@ -116,14 +134,16 @@ async function uninstall(aData, aReason) {
if (aReason === ADDON_UNINSTALL if (aReason === ADDON_UNINSTALL
|| aReason === ADDON_DISABLE) { || aReason === ADDON_DISABLE) {
const config = await getConfig(); const config = await getConfig();
const storedPrefs = config.savedConfiguration.prefs; const storedPrefs = config.savedConfiguration.prefs || {};
PREFS.forEach((pref) => { PREFS.forEach((pref) => {
let value = pref.default;
if (pref.name in storedPrefs) { if (pref.name in storedPrefs) {
if ("int" === pref.type) { value = storedPrefs[pref.name];
Services.prefs.setIntPref(pref.name, storedPrefs[pref.name]); }
} else { if ("int" === pref.type) {
Services.prefs.setBoolPref(pref.name, storedPrefs[pref.name]); Services.prefs.setIntPref(pref.name, value);
} } else {
Services.prefs.setBoolPref(pref.name, value);
} }
}); });
} }
@ -138,7 +158,7 @@ function startup({webExtension, resourceURI}) {
loadStyles(resourceURI); loadStyles(resourceURI);
} }
// Reset prefs that may have changed, or are legacy // Reset prefs that may have changed, or are legacy
setPrefs(); install();
// Start the embedded webextension. // Start the embedded webextension.
webExtension.startup(); webExtension.startup();
} }