| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 const keyPrefix = "pref:"; | 18 const keyPrefix = "pref:"; |
| 19 | 19 |
| 20 let prefs = Object.create(null); | 20 let defaults = Object.create(null); |
| 21 let listeners = []; | 21 let overrides = Object.create(null); |
| 22 | |
|
kzar
2015/03/19 15:20:37
Maybe these defaults should be stored in their own
| |
| 23 defaults.enabled = true; | |
| 24 defaults.data_directory = ""; | |
| 25 defaults.patternsbackups = 5; | |
| 26 defaults.patternsbackupinterval = 24; | |
| 27 defaults.savestats = false; | |
| 28 defaults.privateBrowsing = false; | |
| 29 defaults.subscriptions_fallbackerrors = 5; | |
| 30 defaults.subscriptions_fallbackurl = "https://adblockplus.org/getSubscription?ve rsion=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus =%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%"; | |
| 31 defaults.subscriptions_autoupdate = true; | |
| 32 defaults.subscriptions_exceptionsurl = "https://easylist-downloads.adblockplus.o rg/exceptionrules.txt"; | |
| 33 defaults.subscriptions_antiadblockurl = "https://easylist-downloads.adblockplus. org/antiadblockfilters.txt"; | |
| 34 defaults.documentation_link = "https://adblockplus.org/redirect?link=%LINK%&lang =%LANG%"; | |
| 35 defaults.notificationdata = {}; | |
| 36 defaults.notificationurl = "https://notification.adblockplus.org/notification.js on"; | |
| 37 defaults.stats_total = {}; | |
| 38 defaults.show_statsinicon = true; | |
| 39 defaults.show_statsinpopup = true; | |
| 40 defaults.shouldShowBlockElementMenu = true; | |
| 41 defaults.hidePlaceholders = true; | |
| 42 defaults.suppress_first_run_page = false; | |
| 22 | 43 |
| 23 let Prefs = exports.Prefs = { | 44 let Prefs = exports.Prefs = { |
| 24 addListener: function(listener) | 45 onChanged: new ext._EventTarget(), |
| 25 { | 46 onLoaded: new ext._EventTarget() |
| 26 if (listeners.indexOf(listener) < 0) | |
| 27 listeners.push(listener); | |
| 28 }, | |
| 29 | |
| 30 removeListener: function(listener) | |
| 31 { | |
| 32 let index = listeners.indexOf(listener); | |
| 33 if (index >= 0) | |
| 34 listeners.splice(index, 1); | |
| 35 } | |
| 36 }; | 47 }; |
| 37 | 48 |
| 38 function keyToPref(key) | 49 function keyToPref(key) |
| 39 { | 50 { |
| 40 if (key.indexOf(keyPrefix) != 0) | 51 if (key.indexOf(keyPrefix) != 0) |
| 41 return null; | 52 return null; |
| 42 | 53 |
| 43 return key.substr(keyPrefix.length); | 54 return key.substr(keyPrefix.length); |
| 44 } | 55 } |
| 45 | 56 |
| 46 function prefToKey(pref) | 57 function prefToKey(pref) |
| 47 { | 58 { |
| 48 return keyPrefix + pref; | 59 return keyPrefix + pref; |
| 49 } | 60 } |
| 50 | 61 |
| 51 function addPreference(pref, defaultValue) | 62 function addPreference(pref) |
| 52 { | 63 { |
| 53 prefs[pref] = undefined; | |
| 54 | |
| 55 Object.defineProperty(Prefs, pref, { | 64 Object.defineProperty(Prefs, pref, { |
| 56 get: function() | 65 get: function() |
| 57 { | 66 { |
| 58 let value = prefs[pref]; | 67 return (pref in overrides ? overrides : defaults)[pref]; |
| 59 | |
| 60 if (typeof value != "undefined") | |
| 61 return value; | |
| 62 | |
| 63 return defaultValue; | |
| 64 }, | 68 }, |
| 65 set: function(value) | 69 set: function(value) |
| 66 { | 70 { |
| 71 let defaultValue = defaults[pref]; | |
| 72 | |
| 67 if (typeof value != typeof defaultValue) | 73 if (typeof value != typeof defaultValue) |
| 68 throw new Error("Attempt to change preference type"); | 74 throw new Error("Attempt to change preference type"); |
| 69 | 75 |
| 70 if (value == defaultValue) | 76 if (value == defaultValue) |
| 71 { | 77 { |
| 72 prefs[pref] = undefined; | 78 delete overrides[pref]; |
| 73 ext.storage.remove(prefToKey(pref)); | 79 ext.storage.remove(prefToKey(pref)); |
| 74 } | 80 } |
| 75 else | 81 else |
| 76 { | 82 { |
| 77 prefs[pref] = value; | 83 overrides[pref] = value; |
| 78 ext.storage.set(prefToKey(pref), value); | 84 ext.storage.set(prefToKey(pref), value); |
| 79 } | 85 } |
| 80 }, | 86 }, |
| 81 enumerable: true | 87 enumerable: true |
| 82 }); | 88 }); |
| 83 } | 89 } |
| 84 | 90 |
| 85 function setPreference(pref, value) | 91 function init() |
| 86 { | 92 { |
| 87 prefs[pref] = value; | 93 let prefs = Object.keys(defaults); |
| 94 prefs.forEach(addPreference); | |
| 88 | 95 |
| 89 for (let listener of listeners) | 96 let localLoaded = false; |
| 90 listener(pref); | 97 let managedLoaded = false; |
| 98 | |
| 99 // Migrate preferences for users updating from old versions. | |
| 100 // TODO: Remove the migration code after a few releases. | |
| 101 ext.storage.migratePrefs({ | |
| 102 map: function(key, value) | |
| 103 { | |
| 104 if (key in defaults) | |
| 105 { | |
| 106 key = prefToKey(key); | |
| 107 try | |
| 108 { | |
| 109 value = JSON.parse(value); | |
| 110 } | |
| 111 catch (e) | |
| 112 { | |
| 113 return null; | |
| 114 } | |
| 115 } | |
| 116 else if (key != "currentVersion") | |
| 117 { | |
| 118 return null; | |
| 119 } | |
| 120 | |
| 121 return {key: key, value: value}; | |
| 122 }, | |
| 123 | |
| 124 done: function() | |
| 125 { | |
| 126 ext.storage.get(prefs.map(prefToKey), function(items) | |
| 127 { | |
| 128 for (let key in items) | |
| 129 overrides[keyToPref(key)] = items[key]; | |
| 130 | |
| 131 ext.storage.onChanged.addListener(function(changes) | |
| 132 { | |
| 133 for (let key in changes) | |
| 134 { | |
| 135 let pref = keyToPref(key); | |
| 136 if (pref && pref in defaults) | |
|
Sebastian Noack
2015/03/20 13:26:12
The key is what's stored in ext.storage (e.g. "pre
| |
| 137 { | |
| 138 let change = changes[key]; | |
| 139 if ("newValue" in change) | |
| 140 overrides[pref] = change.newValue; | |
| 141 else | |
| 142 delete overrides[pref]; | |
| 143 | |
| 144 Prefs.onChanged._dispatch(pref); | |
| 145 } | |
| 146 } | |
| 147 }); | |
| 148 | |
| 149 localLoaded = true; | |
| 150 if (managedLoaded) | |
|
Sebastian Noack
2015/03/20 13:26:12
Done.
| |
| 151 Prefs.onLoaded._dispatch(); | |
| 152 }); | |
| 153 } | |
| 154 }); | |
| 155 | |
| 156 if (require("info").platform == "chromium" && "managed" in chrome.storage) | |
| 157 { | |
| 158 chrome.storage.managed.get(null, function(items) | |
| 159 { | |
| 160 for (let key in items) | |
| 161 defaults[key] = items[key]; | |
| 162 | |
| 163 managedLoaded = true; | |
| 164 if (localLoaded) | |
| 165 Prefs.onLoaded._dispatch(); | |
| 166 }); | |
| 167 } | |
| 168 else | |
| 169 { | |
| 170 managedLoaded = true; | |
|
Wladimir Palant
2015/03/19 16:57:04
This relies on the callbacks to read local prefs t
| |
| 171 } | |
| 91 } | 172 } |
| 92 | 173 |
| 93 addPreference("enabled", true); | 174 init(); |
| 94 addPreference("data_directory", ""); | |
| 95 addPreference("patternsbackups", 5); | |
| 96 addPreference("patternsbackupinterval", 24); | |
| 97 addPreference("savestats", false); | |
| 98 addPreference("privateBrowsing", false); | |
| 99 addPreference("subscriptions_fallbackerrors", 5); | |
| 100 addPreference("subscriptions_fallbackurl", "https://adblockplus.org/getSubscript ion?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channel Status=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%"); | |
| 101 addPreference("subscriptions_autoupdate", true); | |
| 102 addPreference("subscriptions_exceptionsurl", "https://easylist-downloads.adblock plus.org/exceptionrules.txt"); | |
| 103 addPreference("subscriptions_antiadblockurl", "https://easylist-downloads.adbloc kplus.org/antiadblockfilters.txt"); | |
| 104 addPreference("documentation_link", "https://adblockplus.org/redirect?link=%LINK %&lang=%LANG%"); | |
| 105 addPreference("notificationdata", {}); | |
| 106 addPreference("notificationurl", "https://notification.adblockplus.org/notificat ion.json"); | |
| 107 addPreference("stats_total", {}); | |
| 108 addPreference("show_statsinicon", true); | |
| 109 addPreference("show_statsinpopup", true); | |
| 110 addPreference("shouldShowBlockElementMenu", true); | |
| 111 addPreference("hidePlaceholders", true); | |
| 112 | |
| 113 // Migrate preferences for users updating from old versions. | |
| 114 // TODO: Remove the migration code after a few releases. | |
| 115 ext.storage.migratePrefs(function(key, value) | |
| 116 { | |
| 117 if (key in prefs) | |
| 118 { | |
| 119 key = prefToKey(key); | |
| 120 try | |
| 121 { | |
| 122 value = JSON.parse(value); | |
| 123 } | |
| 124 catch (e) | |
| 125 { | |
| 126 return null; | |
| 127 } | |
| 128 } | |
| 129 else if (key != "currentVersion") | |
| 130 { | |
| 131 return null; | |
| 132 } | |
| 133 | |
| 134 return {key: key, value: value}; | |
| 135 }); | |
| 136 | |
| 137 ext.storage.get(Object.keys(prefs).map(prefToKey), function(items) | |
| 138 { | |
| 139 for (let key in items) | |
| 140 setPreference(keyToPref(key), items[key]); | |
| 141 | |
| 142 ext.storage.onChanged.addListener(function(changes) | |
| 143 { | |
| 144 for (let key in changes) | |
| 145 { | |
| 146 let pref = keyToPref(key); | |
| 147 if (pref && pref in prefs) | |
| 148 setPreference(pref, changes[key].newValue); | |
| 149 } | |
| 150 }); | |
| 151 }); | |
| OLD | NEW |