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

Side by Side Diff: lib/prefs.js

Issue 5693109165883392: Issue 2040 - Replaced localStorage with chrome.storage.local (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « chrome/ext/background.js ('k') | lib/stats.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 defaults = Object.create(null);
21 let overrides = Object.create(null);
22
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";
34 defaults.documentation_link = "https://adblockplus.org/redirect?link=%LINK%&lang =%LANG%"; 35 defaults.documentation_link = "https://adblockplus.org/redirect?link=%LINK%&lang =%LANG%";
35 defaults.notificationdata = {}; 36 defaults.notificationdata = {};
36 defaults.notificationurl = "https://notification.adblockplus.org/notification.js on"; 37 defaults.notificationurl = "https://notification.adblockplus.org/notification.js on";
37 defaults.stats_total = {}; 38 defaults.stats_total = {};
38 defaults.show_statsinicon = true; 39 defaults.show_statsinicon = true;
39 defaults.show_statsinpopup = true; 40 defaults.show_statsinpopup = true;
40 defaults.shouldShowBlockElementMenu = true; 41 defaults.shouldShowBlockElementMenu = true;
41 defaults.hidePlaceholders = true; 42 defaults.hidePlaceholders = true;
42 43
43 let listeners = []; 44 let Prefs = exports.Prefs = {
45 onChanged: new ext._EventTarget(),
46 onLoaded: new ext._EventTarget()
47 };
44 48
45 function defineProperty(key) 49 function keyToPref(key)
46 { 50 {
47 let value = null; 51 if (key.indexOf(keyPrefix) != 0)
48 Object.defineProperty(Prefs, key, { 52 return null;
53
54 return key.substr(keyPrefix.length);
55 }
56
57 function prefToKey(pref)
58 {
59 return keyPrefix + pref;
60 }
61
62 function addPreference(pref)
63 {
64 Object.defineProperty(Prefs, pref, {
49 get: function() 65 get: function()
50 { 66 {
51 if (value === null) 67 return (pref in overrides ? overrides : defaults)[pref];
52 { 68 },
53 if (key in ext.storage) 69 set: function(value)
54 { 70 {
55 try 71 let defaultValue = defaults[pref];
56 {
57 value = JSON.parse(ext.storage[key]);
58 }
59 catch(e)
60 {
61 Cu.reportError(e);
62 }
63 }
64 72
65 if (value === null) 73 if (typeof value != typeof defaultValue)
66 value = JSON.parse(JSON.stringify(defaults[key]));
67 }
68 return value;
69 },
70 set: function(newValue)
71 {
72 if (typeof newValue != typeof defaults[key])
73 throw new Error("Attempt to change preference type"); 74 throw new Error("Attempt to change preference type");
74 75
75 let stringified = JSON.stringify(newValue); 76 if (value == defaultValue)
76 if (stringified != JSON.stringify(defaults[key])) 77 {
77 ext.storage[key] = stringified; 78 delete overrides[pref];
79 ext.storage.remove(prefToKey(pref));
80 }
78 else 81 else
79 delete ext.storage[key]; 82 {
80 83 overrides[pref] = value;
81 value = newValue; 84 ext.storage.set(prefToKey(pref), value);
82 85 }
83 for (let listener of listeners)
84 listener(key);
85
86 return value;
87 }, 86 },
88 enumerable: true 87 enumerable: true
89 }); 88 });
90 } 89 }
91 90
91 function init()
92 {
93 let prefs = Object.keys(defaults);
94 prefs.forEach(addPreference);
92 95
93 let Prefs = exports.Prefs = { 96 // Migrate preferences for users updating from old versions.
94 addListener: function(listener) 97 // TODO: Remove the migration code after a few releases.
95 { 98 ext.storage.migratePrefs({
96 if (listeners.indexOf(listener) < 0) 99 map: function(key, value)
97 listeners.push(listener); 100 {
98 }, 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 }
99 114
100 removeListener: function(listener) 115 return {key: prefToKey(key), value: value};
101 { 116 }
102 let index = listeners.indexOf(listener);
103 if (index >= 0)
104 listeners.splice(index, 1);
105 },
106 };
107 117
108 for (let key in defaults) 118 return null;
109 defineProperty(key); 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 });
150 }
151
152 init();
OLDNEW
« no previous file with comments | « chrome/ext/background.js ('k') | lib/stats.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld