Skip to content

Commit

Permalink
Refactor Thunderbird prefs getting/setting
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-p committed Sep 3, 2018
1 parent 843dd3a commit c151c4e
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 153 deletions.
120 changes: 50 additions & 70 deletions src/common/options-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,85 +303,65 @@ var MozillaOptionsStore = {
// directly. Unfortunately, this means duplicating some code from the background
// service.
_sendRequest: function(data, callback) { // analogue of chrome.runtime.sendMessage
var extPrefsBranch, supportString, prefKeys, prefsObj, request, sender, i;

try {
extPrefsBranch = window.Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService)
.getBranch('extensions.markdown-here.');
supportString = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);

if (data.verb === 'get') {
prefKeys = extPrefsBranch.getChildList('');
prefsObj = {};

for (i = 0; i < prefKeys.length; i++) {
// All of our legitimate prefs should be strings, but issue #237 suggests
// that things may sometimes get into a bad state. We will check and delete
// and prefs that aren't strings.
// https://github.com/adam-p/markdown-here/issues/237
if (extPrefsBranch.getPrefType(prefKeys[i]) !== extPrefsBranch.PREF_STRING) {
extPrefsBranch.clearUserPref(prefKeys[i]);
continue;
}
var privileged, prefsBranch, prefKeys, prefsObj, i;

try {
if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) {
prefsObj[prefKeys[i]] = window.JSON.parse(
extPrefsBranch.getComplexValue(
prefKeys[i],
Components.interfaces.nsISupportsString).data);
} else {
prefsObj[prefKeys[i]] = window.JSON.parse(
extPrefsBranch.getStringPref(
prefKeys[i]));
}
}
catch(e) {
// Null values and empty strings will result in JSON exceptions
prefsObj[prefKeys[i]] = null;
}
privileged = (typeof(Components) !== 'undefined' && typeof(Components.classes) !== 'undefined');
if (!privileged) {
// This means that this code is being called from a content script.
// We need to send a request from this non-privileged context to the
// privileged background script.
data.action = 'prefs-access';
Utils.makeRequestToPrivilegedScript(
document,
data,
callback);

return;
}

prefsBranch = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService)
.getBranch('extensions.markdown-here.');

if (data.verb === 'get') {
prefKeys = prefsBranch.getChildList('');
prefsObj = {};

for (i = 0; i < prefKeys.length; i++) {
// All of our legitimate prefs should be strings, but issue #237 suggests
// that things may sometimes get into a bad state. We will check and delete
// and prefs that aren't strings.
// https://github.com/adam-p/markdown-here/issues/237
if (prefsBranch.getPrefType(prefKeys[i]) !== prefsBranch.PREF_STRING) {
prefsBranch.clearUserPref(prefKeys[i]);
continue;
}

callback(prefsObj);
return;
prefsObj[prefKeys[i]] = Utils.getMozJsonPref(prefsBranch, prefKeys[i]);
}
else if (data.verb === 'set') {
for (i in data.obj) {
supportString.data = window.JSON.stringify(data.obj[i]);
extPrefsBranch.setComplexValue(
i,
Components.interfaces.nsISupportsString,
supportString);
}

if (callback) callback();
return;
callback(prefsObj);
return;
}
else if (data.verb === 'set') {
for (i in data.obj) {
Utils.setMozJsonPref(prefsBranch, i, data.obj[i]);
}
else if (data.verb === 'clear') {
if (typeof(data.obj) === 'string') {
data.obj = [data.obj];
}

for (i = 0; i < data.obj.length; i++) {
extPrefsBranch.clearUserPref(data.obj[i]);
}
if (callback) callback();
return;
}
else if (data.verb === 'clear') {
if (typeof(data.obj) === 'string') {
data.obj = [data.obj];
}

if (callback) return callback();
return;
for (i = 0; i < data.obj.length; i++) {
prefsBranch.clearUserPref(data.obj[i]);
}
}
catch (ex) {
// This exception was thrown by the Components.classes stuff above, and
// means that this code is being called from a content script.
// We need to send a request from this non-privileged context to the
// privileged background script.
data.action = 'prefs-access';
Utils.makeRequestToPrivilegedScript(
document,
data,
callback);

if (callback) return callback();
return;
}
}
};
Expand Down
91 changes: 88 additions & 3 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,87 @@ function nextTickFn(callback, context) {
}


/*? if(platform==='thunderbird'){ */

/**
* Returns the stored preference string for the given key.
* Must only be called from a privileged Mozilla script.
* @param {nsIPrefBranch} prefsBranch
* @param {string} key
* @returns {?string} The preference value. May be null if the preference is not set
* or is null.
*/
function getMozStringPref(prefsBranch, key) {
try {
if (Services.vc.compare(Services.appinfo.platformVersion, '58') < 0) {
return prefsBranch.getComplexValue(
key,
Components.interfaces.nsISupportsString).data;
}

return prefsBranch.getStringPref(key, null);
}
catch(e) {
// getComplexValue could have thrown an exception because it didn't find the key. As
// with getStringPref, we will default to null.
return null;
}
}

/**
* Get the stored preference object, JSON-parsed, for the given key.
* Must only be called from a privileged Mozilla script.
* @param {nsIPrefBranch} prefsBranch
* @param {string} key
* @returns {?(object|number|boolean|string)} The preference object (any valid JSON
* type). May be null if the preference is not set or is null.
*/
function getMozJsonPref(prefsBranch, key) {
try {
return JSON.parse(getMozStringPref(prefsBranch, key));
}
catch(e) {
return null;
}
}

/**
* Store the preference string for the given key.
* Must only be called from a privileged Mozilla script.
* @param {nsIPrefBranch} prefsBranch
* @param {string} key
* @param {string} value
*/
function setMozStringPref(prefsBranch, key, value) {
var supportString = Components.classes['@mozilla.org/supports-string;1']
.createInstance(Components.interfaces.nsISupportsString);

if (Services.vc.compare(Services.appinfo.platformVersion, '58') < 0) {
supportString.data = value;
prefsBranch.setComplexValue(
key,
Components.interfaces.nsISupportsString,
supportString);
}
else {
prefsBranch.setStringPref(key, value);
}
}

/**
* Store the given object in preferences under the given key.
* Must only be called from a privileged Mozilla script.
* @param {nsIPrefBranch} prefsBranch
* @param {string} key
* @param {?(object|number|boolean|string)} value
*/
function setMozJsonPref(prefsBranch, key, value) {
setMozStringPref(prefsBranch, key, JSON.stringify(value));
}

/*? } */


/*
* i18n/l10n
*/
Expand Down Expand Up @@ -736,7 +817,7 @@ function triggerStringBundleLoadListeners() {
}


// Must only be called from a priviledged Mozilla script
// Must only be called from a privileged Mozilla script
function getMozStringBundle() {
if (typeof(Components) === 'undefined' || typeof(Components.classes) === 'undefined') {
return false;
Expand All @@ -755,7 +836,7 @@ function getMozStringBundle() {

// First load the English fallback strings

stringBundle = window.Components.classes["@mozilla.org/intl/stringbundle;1"]
stringBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
// Notice the explicit locale in this path:
.createBundle("resource://markdown_here_locale/en/strings.properties");
Expand All @@ -768,7 +849,7 @@ function getMozStringBundle() {

// Then load the strings that are overridden for the current locale

stringBundle = window.Components.classes["@mozilla.org/intl/stringbundle;1"]
stringBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://markdown_here/locale/strings.properties");

Expand Down Expand Up @@ -1172,6 +1253,10 @@ Utils.getTopURL = getTopURL;
Utils.nextTick = nextTick;
Utils.nextTickFn = nextTickFn;
/*? if(platform==='thunderbird'){ */
Utils.getMozStringPref = getMozStringPref;
Utils.getMozJsonPref = getMozJsonPref;
Utils.setMozStringPref = setMozStringPref;
Utils.setMozJsonPref = setMozJsonPref;
Utils.getMozStringBundle = getMozStringBundle;
/*? } */
/*? if(platform==='safari'){ */
Expand Down
Loading

0 comments on commit c151c4e

Please sign in to comment.