Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/prefs.js

Issue 5693109165883392: Issue 2040 - Replaced localStorage with chrome.storage.local (Closed)
Left Patch Set: Rebased Created March 3, 2015, 3:11 p.m.
Right Patch Set: Fixed typo in comment Created April 13, 2015, 10:30 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/ext/background.js ('k') | lib/stats.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
23 defaults.enabled = true;
24 defaults.currentVersion = "";
25 defaults.data_directory = "";
26 defaults.patternsbackups = 5;
27 defaults.patternsbackupinterval = 24;
28 defaults.savestats = false;
29 defaults.privateBrowsing = false;
30 defaults.subscriptions_fallbackerrors = 5;
31 defaults.subscriptions_fallbackurl = "https://adblockplus.org/getSubscription?ve rsion=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus =%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%";
32 defaults.subscriptions_autoupdate = true;
33 defaults.subscriptions_exceptionsurl = "https://easylist-downloads.adblockplus.o rg/exceptionrules.txt";
34 defaults.subscriptions_antiadblockurl = "https://easylist-downloads.adblockplus. org/antiadblockfilters.txt";
35 defaults.documentation_link = "https://adblockplus.org/redirect?link=%LINK%&lang =%LANG%";
36 defaults.notificationdata = {};
37 defaults.notificationurl = "https://notification.adblockplus.org/notification.js on";
38 defaults.stats_total = {};
39 defaults.show_statsinicon = true;
40 defaults.show_statsinpopup = true;
41 defaults.shouldShowBlockElementMenu = true;
42 defaults.hidePlaceholders = true;
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 // Migrate preferences for users updating from old versions.
90 listener(pref); 97 // TODO: Remove the migration code after a few releases.
98 ext.storage.migratePrefs({
99 map: function(key, value)
100 {
101 if (key in defaults)
102 {
103 if (key != "currentVersion")
104 {
105 try
106 {
107 value = JSON.parse(value);
108 }
109 catch (e)
110 {
111 return null;
112 }
113 }
114
115 return {key: prefToKey(key), value: value};
116 }
117
118 return null;
119 },
120
121 done: function()
122 {
123 ext.storage.get(prefs.map(prefToKey), function(items)
124 {
125 for (let key in items)
126 overrides[keyToPref(key)] = items[key];
127
128 ext.storage.onChanged.addListener(function(changes)
129 {
130 for (let key in changes)
131 {
132 let pref = keyToPref(key);
133 if (pref && pref in defaults)
134 {
135 let change = changes[key];
136 if ("newValue" in change && change.newValue != defaults[pref])
137 overrides[pref] = change.newValue;
138 else
139 delete overrides[pref];
140
141 Prefs.onChanged._dispatch(pref);
142 }
143 }
144 });
145
146 Prefs.onLoaded._dispatch();
147 });
148 }
149 });
91 } 150 }
92 151
93 addPreference("enabled", true); 152 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(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 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld