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 // | 18 const keyPrefix = "pref:"; |
19 // The values are hardcoded for now. | |
20 // | |
21 | 19 |
22 let defaults = Object.create(null); | 20 let prefs = Object.create(null); |
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 | |
43 let listeners = []; | 21 let listeners = []; |
44 | 22 |
45 function defineProperty(key) | |
46 { | |
47 let value = null; | |
48 Prefs.__defineGetter__(key, function() | |
49 { | |
50 if (value === null) | |
51 { | |
52 if (key in ext.storage) | |
53 { | |
54 try | |
55 { | |
56 value = JSON.parse(ext.storage[key]); | |
57 } | |
58 catch(e) | |
59 { | |
60 Cu.reportError(e); | |
61 } | |
62 } | |
63 | |
64 if (value === null) | |
65 value = JSON.parse(JSON.stringify(defaults[key])); | |
66 } | |
67 return value; | |
68 }); | |
69 Prefs.__defineSetter__(key, function(newValue) | |
70 { | |
71 if (typeof newValue != typeof defaults[key]) | |
72 throw new Error("Attempt to change preference type"); | |
73 | |
74 let stringified = JSON.stringify(newValue); | |
75 if (stringified != JSON.stringify(defaults[key])) | |
76 ext.storage[key] = stringified; | |
77 else | |
78 delete ext.storage[key]; | |
79 | |
80 value = newValue; | |
81 | |
82 for (let listener of listeners) | |
83 listener(key); | |
84 | |
85 return value; | |
86 }); | |
87 } | |
88 | |
89 | |
90 let Prefs = exports.Prefs = { | 23 let Prefs = exports.Prefs = { |
91 addListener: function(listener) | 24 addListener: function(listener) |
92 { | 25 { |
93 if (listeners.indexOf(listener) < 0) | 26 if (listeners.indexOf(listener) < 0) |
94 listeners.push(listener); | 27 listeners.push(listener); |
95 }, | 28 }, |
96 | 29 |
97 removeListener: function(listener) | 30 removeListener: function(listener) |
98 { | 31 { |
99 let index = listeners.indexOf(listener); | 32 let index = listeners.indexOf(listener); |
100 if (index >= 0) | 33 if (index >= 0) |
101 listeners.splice(index, 1); | 34 listeners.splice(index, 1); |
102 }, | 35 } |
103 }; | 36 }; |
104 | 37 |
105 for (let key in defaults) | 38 function keyToPref(key) |
106 defineProperty(key); | 39 { |
40 if (key.indexOf(keyPrefix) != 0) | |
41 return null; | |
42 | |
43 return key.substr(keyPrefix.length); | |
44 } | |
45 | |
46 function prefToKey(pref) | |
47 { | |
48 return keyPrefix + pref; | |
49 } | |
50 | |
51 function addPreference(pref, defaultValue) | |
52 { | |
53 prefs[pref] = undefined; | |
54 | |
55 Object.defineProperty(Prefs, pref, { | |
56 get: function() | |
57 { | |
58 let value = prefs[pref]; | |
59 | |
60 if (typeof value != "undefined") | |
61 return value; | |
62 | |
63 return defaultValue; | |
64 }, | |
65 set: function(value) | |
66 { | |
67 if (typeof value != typeof defaultValue) | |
68 throw new Error("Attempt to change preference type"); | |
69 | |
70 if (value == defaultValue) | |
71 { | |
72 prefs[pref] = undefined; | |
73 ext.storage.remove(prefToKey(pref)); | |
74 } | |
75 else | |
76 { | |
77 prefs[pref] = value; | |
78 ext.storage.set(prefToKey(pref), value); | |
79 } | |
80 }, | |
81 enumerable: true | |
82 }); | |
83 } | |
84 | |
85 function setPreference(pref, value) | |
86 { | |
87 prefs[pref] = value; | |
88 | |
89 for (let listener of listeners) | |
90 listener(pref); | |
91 } | |
Wladimir Palant
2015/03/12 23:05:37
The flow here is rather convoluted, a change to pr
Sebastian Noack
2015/03/12 23:35:29
Currently there is only one case where we external
| |
92 | |
93 addPreference("enabled", true); | |
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(prefs); | |
116 | |
117 ext.storage.get(Object.keys(prefs).map(prefToKey), function(items) | |
118 { | |
119 for (let key in items) | |
120 setPreference(keyToPref(key), items[key]); | |
121 | |
122 ext.storage.onChanged.addListener(function(changes) | |
123 { | |
124 for (let key in changes) | |
125 { | |
126 let pref = keyToPref(key); | |
127 if (pref && pref in prefs) | |
128 setPreference(pref, changes[key].newValue); | |
129 } | |
130 }); | |
131 }); | |
OLD | NEW |