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

Unified Diff: lib/prefs.js

Issue 11159032: Implemented prefs module properly, data is actually being persisted now (Closed)
Patch Set: Created July 19, 2013, 4:12 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/prefs.js
===================================================================
--- a/lib/prefs.js
+++ b/lib/prefs.js
@@ -14,23 +14,76 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
//
// The values are hardcoded for now.
//
-var Prefs = exports.Prefs = {
+let defaults = {
+ __proto__: null,
enabled: true,
patternsfile: "patterns.ini",
patternsbackups: 5,
patternsbackupinterval: 24,
data_directory: "",
savestats: false,
privateBrowsing: false,
subscriptions_fallbackerrors: 5,
subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%",
subscriptions_autoupdate: true,
subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%",
- addListener: function() {}
};
+
+let listeners = [];
+
+function defineProperty(key)
+{
+ Prefs.__defineGetter__(key, function()
+ {
+ if (key in localStorage)
+ {
+ try
+ {
+ return JSON.parse(localStorage[key]);
+ }
+ catch(e)
+ {
+ Cu.reportError(e);
+ }
+ }
+ return defaults[key];
+ });
+ Prefs.__defineSetter__(key, function(value)
+ {
+ if (typeof value != typeof defaults[key])
+ throw new Error("Attempt to change preference type");
Thomas Greiner 2013/07/25 13:20:36 I'd also include a check for the existence of the
Wladimir Palant 2013/07/26 09:44:02 The property handler is only added for properties
+
+ let stringified = JSON.stringify(value);
+ if (stringified != JSON.stringify(defaults[key]))
+ localStorage[key] = stringified;
+ else
+ delete localStorage[key];
+ for each (let listener in listeners)
+ listener(key);
+ });
+}
+
+
+let Prefs = exports.Prefs = {
+ addListener: function(listener)
+ {
+ if (listeners.indexOf(listener) < 0)
+ listeners.push(listener);
+ },
+
+ removeListener: function(listener)
+ {
+ let index = listeners.indexOf(listener);
+ if (index >= 0)
+ listeners.splice(index, 1);
+ },
+};
+
+for (let key in defaults)
+ defineProperty(key);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld