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

Unified Diff: lib/prefs.js

Issue 5693109165883392: Issue 2040 - Replaced localStorage with chrome.storage.local (Closed)
Patch Set: Moved canUseChromeNotifications back Created April 9, 2015, 9:35 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
Index: lib/prefs.js
===================================================================
--- a/lib/prefs.js
+++ b/lib/prefs.js
@@ -15,12 +15,13 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-//
-// The values are hardcoded for now.
-//
+const keyPrefix = "pref:";
let defaults = Object.create(null);
+let overrides = Object.create(null);
+
defaults.enabled = true;
+defaults.currentVersion = "";
defaults.data_directory = "";
defaults.patternsbackups = 5;
defaults.patternsbackupinterval = 24;
@@ -40,70 +41,112 @@
defaults.shouldShowBlockElementMenu = true;
defaults.hidePlaceholders = true;
-let listeners = [];
+let Prefs = exports.Prefs = {
+ onChanged: new ext._EventTarget(),
+ onLoaded: new ext._EventTarget()
+};
-function defineProperty(key)
+function keyToPref(key)
{
- let value = null;
- Object.defineProperty(Prefs, key, {
+ if (key.indexOf(keyPrefix) != 0)
+ return null;
+
+ return key.substr(keyPrefix.length);
+}
+
+function prefToKey(pref)
+{
+ return keyPrefix + pref;
+}
+
+function addPreference(pref)
+{
+ Object.defineProperty(Prefs, pref, {
get: function()
{
- if (value === null)
- {
- if (key in ext.storage)
- {
- try
- {
- value = JSON.parse(ext.storage[key]);
- }
- catch(e)
- {
- Cu.reportError(e);
- }
- }
+ return (pref in overrides ? overrides : defaults)[pref];
+ },
+ set: function(value)
+ {
+ let defaultValue = defaults[pref];
- if (value === null)
- value = JSON.parse(JSON.stringify(defaults[key]));
- }
- return value;
- },
- set: function(newValue)
- {
- if (typeof newValue != typeof defaults[key])
+ if (typeof value != typeof defaultValue)
throw new Error("Attempt to change preference type");
- let stringified = JSON.stringify(newValue);
- if (stringified != JSON.stringify(defaults[key]))
- ext.storage[key] = stringified;
+ if (value == defaultValue)
+ {
+ delete overrides[pref];
+ ext.storage.remove(prefToKey(pref));
+ }
else
- delete ext.storage[key];
-
- value = newValue;
-
- for (let listener of listeners)
- listener(key);
-
- return value;
+ {
+ overrides[pref] = value;
+ ext.storage.set(prefToKey(pref), value);
+ }
},
enumerable: true
});
}
+function init()
+{
+ let prefs = Object.keys(defaults);
+ prefs.forEach(addPreference);
-let Prefs = exports.Prefs = {
- addListener: function(listener)
- {
- if (listeners.indexOf(listener) < 0)
- listeners.push(listener);
- },
+ // Migrate preferences for users updating from old versions.
+ // TODO: Remove the migration code after a few releases.
+ ext.storage.migratePrefs({
+ map: function(key, value)
+ {
+ if (key in defaults)
+ {
+ if (key != "currentVersion")
+ {
+ try
+ {
+ value = JSON.parse(value);
+ }
+ catch (e)
+ {
+ return null;
+ }
+ }
- removeListener: function(listener)
- {
- let index = listeners.indexOf(listener);
- if (index >= 0)
- listeners.splice(index, 1);
- },
-};
+ return {key: prefToKey(key), value: value};
+ }
-for (let key in defaults)
- defineProperty(key);
+ return null;
+ },
+
+ done: function()
+ {
+ ext.storage.get(prefs.map(prefToKey), function(items)
+ {
+ for (let key in items)
+ overrides[keyToPref(key)] = items[key];
+
+ ext.storage.onChanged.addListener(function(changes)
+ {
+ for (let key in changes)
+ {
+ let pref = keyToPref(key);
+ if (pref && pref in defaults)
+ {
+ let change = changes[key];
+ if ("newValue" in change && change.newValue != defaults[pref])
+ overrides[pref] = change.newValue;
+ else
+ delete overrides[pref];
+
+ Prefs.onChanged._dispatch(pref);
+ }
+ }
+ });
+
+ Prefs.onLoaded._dispatch();
+ });
+ }
+ });
+}
+
+init();
« chrome/ext/background.js ('K') | « chrome/ext/background.js ('k') | lib/stats.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld