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: Addressed comment Created March 16, 2015, 12:53 p.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/storage/io.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 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 Object.defineProperty(Prefs, key, {
49 get: function()
50 {
51 if (value === null)
52 {
53 if (key in ext.storage)
54 {
55 try
56 {
57 value = JSON.parse(ext.storage[key]);
58 }
59 catch(e)
60 {
61 Cu.reportError(e);
62 }
63 }
64
65 if (value === null)
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
75 let stringified = JSON.stringify(newValue);
76 if (stringified != JSON.stringify(defaults[key]))
77 ext.storage[key] = stringified;
78 else
79 delete ext.storage[key];
80
81 value = newValue;
82
83 for (let listener of listeners)
84 listener(key);
85
86 return value;
87 },
88 enumerable: true
89 });
90 }
91
92
93 let Prefs = exports.Prefs = { 23 let Prefs = exports.Prefs = {
94 addListener: function(listener) 24 addListener: function(listener)
95 { 25 {
96 if (listeners.indexOf(listener) < 0) 26 if (listeners.indexOf(listener) < 0)
97 listeners.push(listener); 27 listeners.push(listener);
98 }, 28 },
99 29
100 removeListener: function(listener) 30 removeListener: function(listener)
101 { 31 {
102 let index = listeners.indexOf(listener); 32 let index = listeners.indexOf(listener);
103 if (index >= 0) 33 if (index >= 0)
104 listeners.splice(index, 1); 34 listeners.splice(index, 1);
105 }, 35 }
106 }; 36 };
107 37
108 for (let key in defaults) 38 function keyToPref(key)
109 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 }
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(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 });
OLDNEW
« no previous file with comments | « chrome/ext/background.js ('k') | lib/storage/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld