Skip to content

Commit

Permalink
Closes #244 don't override native prefs defaults values if set
Browse files Browse the repository at this point in the history
  • Loading branch information
Quicksaver committed Feb 27, 2016
1 parent 4462285 commit 6016238
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions resource/modules/utils/Prefs.jsm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// VERSION 2.6.2
// VERSION 2.6.3
Modules.UTILS = true;
Modules.BASEUTILS = true;

Expand Down Expand Up @@ -39,44 +39,36 @@ this.Prefs = {
natives: new Map(),
cleaningOnShutdown: false,

setDefaults: function(prefList, branch, trunk) {
get _syncBranch() {
delete this._syncBranch;
this._syncBranch = Services.prefs.getDefaultBranch('services.sync.prefs.sync.');
return this._syncBranch;
},

_getBranch: function(branch, trunk, defaults) {
let str = '';
if(trunk === undefined) {
str = 'extensions.';
} else if(trunk) {
str = trunk+'.';
}
if(!branch) {
branch = objPathString;
}
if(!trunk && trunk !== '') {
trunk = 'extensions';
}
str += branch+'.';

return Services.prefs[defaults ? 'getDefaultBranch' : 'getBranch'](str);
},

setDefaults: function(prefList, branch, trunk) {
// we assume that a Prefs module has been initiated in the main process at least once, so none of this is actually necessary
if(self.isChrome) {
var branchString = ((trunk) ? trunk+'.' : '') +branch+'.';
var defaultBranch = Services.prefs.getDefaultBranch(branchString);
var syncBranch = Services.prefs.getDefaultBranch('services.sync.prefs.sync.');
let defaultBranch = this._getBranch(branch, trunk, true);

for(let pref in prefList) {
if(pref.startsWith('NoSync_')) { continue; }

// When updating from a version with prefs of same name but different type would throw an error and stop.
// In this case, we need to clear it before we can set its default value again.
var savedPrefType = defaultBranch.getPrefType(pref);
var prefType = typeof(prefList[pref]);
var compareType = '';
switch(savedPrefType) {
case defaultBranch.PREF_STRING:
compareType = 'string';
break;
case defaultBranch.PREF_INT:
compareType = 'number';
break;
case defaultBranch.PREF_BOOL:
compareType = 'boolean';
break;
default: break;
}
if(compareType && prefType != compareType) {
defaultBranch.clearUserPref(pref);
}

let prefType = typeof(prefList[pref]);
switch(prefType) {
case 'string':
defaultBranch.setCharPref(pref, prefList[pref]);
Expand All @@ -92,30 +84,31 @@ this.Prefs = {
break;
}

if(trunk == 'extensions' && branch == objPathString && !prefList['NoSync_'+pref]) {
syncBranch.setBoolPref(trunk+'.'+branch+'.'+pref, true);
if(trunk === undefined && branch === undefined && !prefList['NoSync_'+pref]) {
this._syncBranch.setBoolPref('extensions.'+objPathString+'.'+pref, true);
}
}
}

// We do this separate from the process above because we would get errors sometimes:
// setting a pref that has the same string name initially (e.g. "something" and "somethingElse"), it would trigger a change event for "something"
// when set*Pref()'ing "somethingElse"
let userBranch = this._getBranch(branch, trunk);
for(let pref in prefList) {
if(pref.startsWith('NoSync_')) { continue; }

if(!this.instances.has(pref)) {
this._setPref(pref, branch, trunk);
this._setPref(pref, userBranch);
}
}
},

_setPref: function(pref, branch, trunk) {
let instance = {};

instance.listeners = new Set();
instance.branch = Services.prefs.getBranch(((trunk) ? trunk+'.' : '') +branch+'.');
instance.type = instance.branch.getPrefType(pref);
_setPref: function(pref, branch) {
let instance = {
listeners: new Set(),
branch: branch,
type: branch.getPrefType(pref)
};

switch(instance.type) {
case Services.prefs.PREF_STRING:
Expand Down Expand Up @@ -218,7 +211,25 @@ this.Prefs = {
}

// We need to keep this preference accessible also from this object like any of our own preferences.
this.setDefaults({ [nPrefName]: nPrefDefaultValue }, branch, trunk);
// Try to not overwrite a default value if it exists already, only use the passed argument as a backup.
let value = nPrefDefaultValue;
let defaults = this._getBranch(branch, trunk, true);
let type = defaults.getPrefType(nPrefName);
try {
switch(type) {
case Services.prefs.PREF_STRING:
value = defaults.getCharPref(nPrefName);
break;
case Services.prefs.PREF_INT:
value = defaults.getIntPref(nPrefName);
break;
case Services.prefs.PREF_BOOL:
value = defaults.getBoolPref(nPrefName);
break;
}
}
catch(ex) { /* the preference probably doesn't have a default value, ignore */ }
this.setDefaults({ [nPrefName]: value }, branch, trunk);

let handler = {
nPref: nPrefName,
Expand Down

0 comments on commit 6016238

Please sign in to comment.