| Index: lib/prefs.js |
| =================================================================== |
| --- a/lib/prefs.js |
| +++ b/lib/prefs.js |
| @@ -15,81 +15,11 @@ |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| -// |
| -// The values are hardcoded for now. |
| -// |
| +const keyPrefix = "pref:"; |
| -let defaults = Object.create(null); |
| -defaults.enabled = true; |
| -defaults.data_directory = ""; |
| -defaults.patternsbackups = 5; |
| -defaults.patternsbackupinterval = 24; |
| -defaults.savestats = false; |
| -defaults.privateBrowsing = false; |
| -defaults.subscriptions_fallbackerrors = 5; |
| -defaults.subscriptions_fallbackurl = "https://adblockplus.org/getSubscription?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%"; |
| -defaults.subscriptions_autoupdate = true; |
| -defaults.subscriptions_exceptionsurl = "https://easylist-downloads.adblockplus.org/exceptionrules.txt"; |
| -defaults.subscriptions_antiadblockurl = "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt"; |
| -defaults.documentation_link = "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%"; |
| -defaults.notificationdata = {}; |
| -defaults.notificationurl = "https://notification.adblockplus.org/notification.json"; |
| -defaults.stats_total = {}; |
| -defaults.show_statsinicon = true; |
| -defaults.show_statsinpopup = true; |
| -defaults.shouldShowBlockElementMenu = true; |
| -defaults.hidePlaceholders = true; |
| - |
| +let prefs = Object.create(null); |
| let listeners = []; |
| -function defineProperty(key) |
| -{ |
| - let value = null; |
| - Object.defineProperty(Prefs, key, { |
| - get: function() |
| - { |
| - if (value === null) |
| - { |
| - if (key in ext.storage) |
| - { |
| - try |
| - { |
| - value = JSON.parse(ext.storage[key]); |
| - } |
| - catch(e) |
| - { |
| - Cu.reportError(e); |
| - } |
| - } |
| - |
| - if (value === null) |
| - value = JSON.parse(JSON.stringify(defaults[key])); |
| - } |
| - return value; |
| - }, |
| - set: function(newValue) |
| - { |
| - if (typeof newValue != typeof defaults[key]) |
| - throw new Error("Attempt to change preference type"); |
| - |
| - let stringified = JSON.stringify(newValue); |
| - if (stringified != JSON.stringify(defaults[key])) |
| - ext.storage[key] = stringified; |
| - else |
| - delete ext.storage[key]; |
| - |
| - value = newValue; |
| - |
| - for (let listener of listeners) |
| - listener(key); |
| - |
| - return value; |
| - }, |
| - enumerable: true |
| - }); |
| -} |
| - |
| - |
| let Prefs = exports.Prefs = { |
| addListener: function(listener) |
| { |
| @@ -102,8 +32,100 @@ |
| let index = listeners.indexOf(listener); |
| if (index >= 0) |
| listeners.splice(index, 1); |
| - }, |
| + } |
| }; |
| -for (let key in defaults) |
| - defineProperty(key); |
| +function keyToPref(key) |
| +{ |
| + if (key.indexOf(keyPrefix) != 0) |
| + return null; |
| + |
| + return key.substr(keyPrefix.length); |
| +} |
| + |
| +function prefToKey(pref) |
| +{ |
| + return keyPrefix + pref; |
| +} |
| + |
| +function addPreference(pref, defaultValue) |
| +{ |
| + prefs[pref] = undefined; |
| + |
| + Object.defineProperty(Prefs, pref, { |
| + get: function() |
| + { |
| + let value = prefs[pref]; |
| + |
| + if (typeof value != "undefined") |
| + return value; |
| + |
| + return defaultValue; |
| + }, |
| + set: function(value) |
| + { |
| + if (typeof value != typeof defaultValue) |
| + throw new Error("Attempt to change preference type"); |
| + |
| + if (value == defaultValue) |
| + { |
| + prefs[pref] = undefined; |
| + ext.storage.remove(prefToKey(pref)); |
| + } |
| + else |
| + { |
| + prefs[pref] = value; |
| + ext.storage.set(prefToKey(pref), value); |
| + } |
| + }, |
| + enumerable: true |
| + }); |
| +} |
| + |
| +function setPreference(pref, value) |
| +{ |
| + prefs[pref] = value; |
| + |
| + for (let listener of listeners) |
| + listener(pref); |
| +} |
| + |
| +addPreference("enabled", true); |
| +addPreference("data_directory", ""); |
| +addPreference("patternsbackups", 5); |
| +addPreference("patternsbackupinterval", 24); |
| +addPreference("savestats", false); |
| +addPreference("privateBrowsing", false); |
| +addPreference("subscriptions_fallbackerrors", 5); |
| +addPreference("subscriptions_fallbackurl", "https://adblockplus.org/getSubscription?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%"); |
| +addPreference("subscriptions_autoupdate", true); |
| +addPreference("subscriptions_exceptionsurl", "https://easylist-downloads.adblockplus.org/exceptionrules.txt"); |
| +addPreference("subscriptions_antiadblockurl", "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt"); |
| +addPreference("documentation_link", "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%"); |
| +addPreference("notificationdata", {}); |
| +addPreference("notificationurl", "https://notification.adblockplus.org/notification.json"); |
| +addPreference("stats_total", {}); |
| +addPreference("show_statsinicon", true); |
| +addPreference("show_statsinpopup", true); |
| +addPreference("shouldShowBlockElementMenu", true); |
| +addPreference("hidePlaceholders", true); |
| + |
| +// Migrate preferences for users updating from old versions. |
| +// TODO: Remove the migration code after a few releases. |
| +ext.storage.migratePrefs(prefs); |
| + |
| +ext.storage.get(Object.keys(prefs).map(prefToKey), function(items) |
| +{ |
| + for (let key in items) |
| + setPreference(keyToPref(key), items[key]); |
| + |
| + ext.storage.onChanged.addListener(function(changes) |
| + { |
| + for (let key in changes) |
| + { |
| + let pref = keyToPref(key); |
| + if (pref && pref in prefs) |
| + setPreference(pref, changes[key].newValue); |
| + } |
| + }); |
| +}); |