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

Unified Diff: lib/prefs.js

Issue 10802049: Functional prefs implementation (Closed)
Patch Set: Cleaned up init.js a bit Created June 5, 2013, 9:44 a.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
@@ -14,23 +14,114 @@
* 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() {}
+ update_url_release: "https://updates.adblockplus.org/%NAME%/update.json",
+ update_url_devbuild: "https://adblockplus.org/devbuilds/%s/update.json",
+ last_update: 0
};
+
+let values = Object.create(defaults);
+let path = _fileSystem.resolve("prefs.json");
+let listeners = [];
+let isDirty = false;
+let isSaving = false;
+
+function defineProperty(key)
+{
+ Prefs.__defineGetter__(key, function() values[key]);
+ Prefs.__defineSetter__(key, function(value)
+ {
+ if (typeof value != typeof defaults[key])
+ throw new Error("Attempt to change preference type");
+
+ if (value == defaults[key])
+ delete values[key];
+ else
+ values[key] = value;
+ save();
+
+ for each (let listener in listeners)
+ listener(key);
+ });
+}
+
+function load()
+{
+ _fileSystem.read(path, function(result)
+ {
+ // prefs.json is expected to be missing, ignore errors reading file
+ if (!result.error)
+ {
+ try
+ {
+ let data = JSON.parse(result.content);
+ for (let key in data)
+ if (key in defaults)
+ values[key] = data[key];
+ }
+ catch (e)
+ {
+ Cu.reportError(e);
+ }
+ }
+
+ if (typeof Prefs._initListener == "function")
+ Prefs._initListener();
+ });
+}
+
+function save()
+{
+ if (isSaving)
+ {
+ isDirty = true;
+ return;
+ }
+
+ isDirty = false;
+ isSaving = true;
+ _fileSystem.write(path, JSON.stringify(values), function()
+ {
+ isSaving = false;
+ if (isDirty)
+ save();
+ });
+}
+
+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);
+
+load();

Powered by Google App Engine
This is Rietveld