diff --git a/src/common/options-store.js b/src/common/options-store.js index 447d8af5..1df85b05 100644 --- a/src/common/options-store.js +++ b/src/common/options-store.js @@ -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; } } }; diff --git a/src/common/utils.js b/src/common/utils.js index 08257186..46dae091 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -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 */ @@ -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; @@ -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"); @@ -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"); @@ -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'){ */ diff --git a/src/firefox/chrome/content/background-services.js b/src/firefox/chrome/content/background-services.js index 75270803..6eec8d95 100644 --- a/src/firefox/chrome/content/background-services.js +++ b/src/firefox/chrome/content/background-services.js @@ -126,16 +126,14 @@ true); // wantsUntrusted -- needed for communication with content scripts // Access the actual Firefox/Thunderbird stored prefs. function prefsAccessRequestHandler(request) { - var extPrefsBranch, supportString, prefKeys, prefsObj, i; + var prefsBranch, prefKeys, prefsObj, i; - extPrefsBranch = Components.classes['@mozilla.org/preferences-service;1'] + prefsBranch = 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 (request.verb === 'get') { - prefKeys = extPrefsBranch.getChildList(''); + prefKeys = prefsBranch.getChildList(''); prefsObj = {}; for (i = 0; i < prefKeys.length; i++) { @@ -143,44 +141,19 @@ function prefsAccessRequestHandler(request) { // 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]); + if (prefsBranch.getPrefType(prefKeys[i]) !== prefsBranch.PREF_STRING) { + prefsBranch.clearUserPref(prefKeys[i]); continue; } - try { - if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) { - prefsObj[prefKeys[i]] = JSON.parse( - extPrefsBranch.getComplexValue( - prefKeys[i], - Components.interfaces.nsISupportsString).data); - } else { - prefsObj[prefKeys[i]] = JSON.parse( - extPrefsBranch.getStringPref( - prefKeys[i])); - } - } - catch(e) { - // Null values and empty strings will result in JSON exceptions - prefsObj[prefKeys[i]] = null; - } + prefsObj[prefKeys[i]] = imports.Utils.getMozJsonPref(prefsBranch, prefKeys[i]); } return prefsObj; } else if (request.verb === 'set') { for (var key in request.obj) { - supportString.data = JSON.stringify(request.obj[key]); - if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) { - extPrefsBranch.setComplexValue( - key, - Components.interfaces.nsISupportsString, - supportString); - } else { - extPrefsBranch.setStringPref( - key, - supportString.data); - } + imports.Utils.setMozJsonPref(prefsBranch, key, request.obj[key]); } return; @@ -191,7 +164,7 @@ function prefsAccessRequestHandler(request) { } for (i = 0; i < request.obj.length; i++) { - extPrefsBranch.clearUserPref(request.obj[i]); + prefsBranch.clearUserPref(request.obj[i]); } return; @@ -230,54 +203,18 @@ catch (ex) { function updateHandler(currVer) { var prefService = Components.classes['@mozilla.org/preferences-service;1'] .getService(Components.interfaces.nsIPrefService); - var extPrefsBranch = prefService.getBranch('extensions.markdown-here.'); + var prefsBranch = prefService.getBranch('extensions.markdown-here.'); var extSyncBranch = prefService.getBranch('services.sync.prefs.sync.extensions.markdown-here.'); - var supportString = Components.classes["@mozilla.org/supports-string;1"] - .createInstance(Components.interfaces.nsISupportsString); - - var lastVersion = ''; - try { - if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) { - lastVersion = JSON.parse( - extPrefsBranch.getComplexValue( - 'last-version', - Components.interfaces.nsISupportsString).data); - } else { - lastVersion = JSON.parse( - extPrefsBranch.getStringPref( - 'last-version')); - } - } - catch (ex) { - } + + var lastVersion = imports.Utils.getMozJsonPref(prefsBranch, 'last-version'); // The presence of this pref indicates that it's not the first run. - var localFirstRun = !extPrefsBranch.prefHasUserValue('local-first-run'); - - supportString.data = JSON.stringify(false); - if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) { - extPrefsBranch.setComplexValue( - 'local-first-run', - Components.interfaces.nsISupportsString, - supportString); - } else { - extPrefsBranch.setStringPref( - 'local-first-run', - supportString.data); - } + var localFirstRun = !prefsBranch.prefHasUserValue('local-first-run'); + + imports.Utils.setMozJsonPref(prefsBranch, 'local-first-run', false); if (currVer !== lastVersion) { - supportString.data = JSON.stringify(currVer); - if ( Services.vc.compare(Services.appinfo.platformVersion, '58') < 0 ) { - extPrefsBranch.setComplexValue( - 'last-version', - Components.interfaces.nsISupportsString, - supportString); - } else { - extPrefsBranch.setStringPref( - 'last-version', - supportString.data); - } + imports.Utils.setMozJsonPref(prefsBranch, 'last-version', currVer); // Set the preference sync flags while we're at it. diff --git a/src/install.rdf b/src/install.rdf index cab41794..ec2d3044 100644 --- a/src/install.rdf +++ b/src/install.rdf @@ -30,7 +30,7 @@ toolkit@mozilla.org 6.0 - 54.* + 60.* @@ -152,7 +152,8 @@ resource://markdown_here_common/images/icon128.png https://github.com/adam-p/markdown-here - chrome://markdown_here/content/options.xul + 3 + resource://markdown_here_common/options.html