| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 defaults = Object.create(null); | 20 let defaults = Object.create(null); |
| 21 let overrides = Object.create(null); | 21 let overrides = Object.create(null); |
| 22 | 22 |
|
kzar
2015/03/19 15:20:37
Maybe these defaults should be stored in their own
| |
| 23 defaults.enabled = true; | 23 defaults.enabled = true; |
| 24 defaults.currentVersion = ""; | |
| 24 defaults.data_directory = ""; | 25 defaults.data_directory = ""; |
| 25 defaults.patternsbackups = 5; | 26 defaults.patternsbackups = 5; |
| 26 defaults.patternsbackupinterval = 24; | 27 defaults.patternsbackupinterval = 24; |
| 27 defaults.savestats = false; | 28 defaults.savestats = false; |
| 28 defaults.privateBrowsing = false; | 29 defaults.privateBrowsing = false; |
| 29 defaults.subscriptions_fallbackerrors = 5; | 30 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_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_autoupdate = true; |
| 32 defaults.subscriptions_exceptionsurl = "https://easylist-downloads.adblockplus.o rg/exceptionrules.txt"; | 33 defaults.subscriptions_exceptionsurl = "https://easylist-downloads.adblockplus.o rg/exceptionrules.txt"; |
| 33 defaults.subscriptions_antiadblockurl = "https://easylist-downloads.adblockplus. org/antiadblockfilters.txt"; | 34 defaults.subscriptions_antiadblockurl = "https://easylist-downloads.adblockplus. org/antiadblockfilters.txt"; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 } | 90 } |
| 90 | 91 |
| 91 function init() | 92 function init() |
| 92 { | 93 { |
| 93 let prefs = Object.keys(defaults); | 94 let prefs = Object.keys(defaults); |
| 94 prefs.forEach(addPreference); | 95 prefs.forEach(addPreference); |
| 95 | 96 |
| 96 let localLoaded = false; | 97 let localLoaded = false; |
| 97 let managedLoaded = false; | 98 let managedLoaded = false; |
| 98 | 99 |
| 100 let checkLoaded = function() | |
| 101 { | |
| 102 if (!localLoaded || !managedLoaded) | |
| 103 return; | |
| 104 | |
| 105 ext.storage.onChanged.addListener(function(changes) | |
| 106 { | |
| 107 for (let key in changes) | |
| 108 { | |
| 109 let pref = keyToPref(key); | |
| 110 if (pref && pref in defaults) | |
| 111 { | |
| 112 let change = changes[key]; | |
| 113 if ("newValue" in change && change.newValue != defaults[pref]) | |
| 114 overrides[pref] = change.newValue; | |
| 115 else | |
| 116 delete overrides[pref]; | |
| 117 | |
| 118 Prefs.onChanged._dispatch(pref); | |
| 119 } | |
| 120 } | |
| 121 }); | |
| 122 | |
| 123 Prefs.onLoaded._dispatch(); | |
| 124 }; | |
| 125 | |
| 99 // Migrate preferences for users updating from old versions. | 126 // Migrate preferences for users updating from old versions. |
| 100 // TODO: Remove the migration code after a few releases. | 127 // TODO: Remove the migration code after a few releases. |
| 101 ext.storage.migratePrefs({ | 128 ext.storage.migratePrefs({ |
| 102 map: function(key, value) | 129 map: function(key, value) |
| 103 { | 130 { |
| 104 if (key in defaults) | 131 if (key in defaults) |
| 105 { | 132 { |
| 106 key = prefToKey(key); | 133 if (key != "currentVersion") |
| 107 try | |
| 108 { | 134 { |
| 109 value = JSON.parse(value); | 135 try |
| 136 { | |
| 137 value = JSON.parse(value); | |
| 138 } | |
| 139 catch (e) | |
| 140 { | |
| 141 return null; | |
| 142 } | |
| 110 } | 143 } |
| 111 catch (e) | 144 |
| 112 { | 145 return {key: prefToKey(key), value: value}; |
| 113 return null; | |
| 114 } | |
| 115 } | |
| 116 else if (key != "currentVersion") | |
| 117 { | |
| 118 return null; | |
| 119 } | 146 } |
| 120 | 147 |
| 121 return {key: key, value: value}; | 148 return null; |
| 122 }, | 149 }, |
| 123 | 150 |
| 124 done: function() | 151 done: function() |
| 125 { | 152 { |
| 126 ext.storage.get(prefs.map(prefToKey), function(items) | 153 ext.storage.get(prefs.map(prefToKey), function(items) |
| 127 { | 154 { |
| 128 for (let key in items) | 155 for (let key in items) |
| 129 overrides[keyToPref(key)] = items[key]; | 156 overrides[keyToPref(key)] = items[key]; |
| 130 | 157 |
| 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; | 158 localLoaded = true; |
| 150 if (managedLoaded) | 159 checkLoaded(); |
|
Sebastian Noack
2015/03/20 13:26:12
Done.
| |
| 151 Prefs.onLoaded._dispatch(); | |
| 152 }); | 160 }); |
| 153 } | 161 } |
| 154 }); | 162 }); |
| 155 | 163 |
| 156 if (require("info").platform == "chromium" && "managed" in chrome.storage) | 164 if (require("info").platform == "chromium" && "managed" in chrome.storage) |
| 157 { | 165 { |
| 158 chrome.storage.managed.get(null, function(items) | 166 chrome.storage.managed.get(null, function(items) |
| 159 { | 167 { |
| 160 for (let key in items) | 168 for (let key in items) |
| 161 defaults[key] = items[key]; | 169 defaults[key] = items[key]; |
| 162 | 170 |
| 163 managedLoaded = true; | 171 managedLoaded = true; |
| 164 if (localLoaded) | 172 checkLoaded(); |
| 165 Prefs.onLoaded._dispatch(); | |
| 166 }); | 173 }); |
| 167 } | 174 } |
| 168 else | 175 else |
| 169 { | 176 { |
| 170 managedLoaded = true; | 177 managedLoaded = true; |
|
Wladimir Palant
2015/03/19 16:57:04
This relies on the callbacks to read local prefs t
| |
| 178 checkLoaded(); | |
| 171 } | 179 } |
| 172 } | 180 } |
| 173 | 181 |
| 174 init(); | 182 init(); |
| LEFT | RIGHT |