[Pkg-mozext-commits] [adblock-plus-element-hiding-helper] 377/483: Simplified preference handling, merged pref loader and the actual Prefs object
David Prévot
taffit at moszumanska.debian.org
Thu Jan 22 21:41:58 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository adblock-plus-element-hiding-helper.
commit 770915e3d7591a7baa6e3b29ba68f4998ca2b59a
Author: Wladimir Palant <trev at adblockplus.org>
Date: Sat Jan 21 18:06:48 2012 +0100
Simplified preference handling, merged pref loader and the actual Prefs object
---
bootstrap.js | 4 -
chrome/content/prefLoader.js | 51 ---------
prefs.js | 267 +++++++++++++++++++++++++------------------
3 files changed, 155 insertions(+), 167 deletions(-)
diff --git a/bootstrap.js b/bootstrap.js
index 2a31e44..1b6bb8d 100644
--- a/bootstrap.js
+++ b/bootstrap.js
@@ -24,10 +24,6 @@ function startup(params, reason)
addonData = params;
Services.obs.addObserver(RequireObserver, "elemhidehelper-require", true);
- let scope = {};
- Services.scriptloader.loadSubScript("chrome://elemhidehelper/content/prefLoader.js", scope);
- scope.loadDefaultPrefs(params.installPath);
-
require("appIntegration").AppIntegration.init();
}
diff --git a/chrome/content/prefLoader.js b/chrome/content/prefLoader.js
deleted file mode 100644
index ba5562d..0000000
--- a/chrome/content/prefLoader.js
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * This Source Code is subject to the terms of the Mozilla Public License
- * version 2.0 (the "License"). You can obtain a copy of the License at
- * http://mozilla.org/MPL/2.0/.
- */
-
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-const Cu = Components.utils;
-
-Cu.import("resource://gre/modules/Services.jsm");
-
-function loadDefaultPrefs(installPath)
-{
- try
- {
- let baseURI = Services.io.newFileURI(installPath);
- let uri;
- if (installPath.isDirectory())
- uri = Services.io.newURI("defaults/preferences/prefs.js", null, baseURI).spec;
- else
- uri = "jar:" + baseURI.spec + "!/defaults/preferences/prefs.js";
-
- let branch = Services.prefs.getDefaultBranch("");
- let scope =
- {
- pref: function(pref, value)
- {
- switch (typeof value)
- {
- case "boolean":
- branch.setBoolPref(pref, value);
- break;
- case "number":
- branch.setIntPref(pref, value);
- break;
- case "string":
- let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
- str.data = value;
- branch.setComplexValue(pref, Ci.nsISupportsString, str);
- break;
- }
- }
- };
- Services.scriptloader.loadSubScript(uri, scope);
- }
- catch(e)
- {
- Cu.reportError(e);
- }
-}
diff --git a/prefs.js b/prefs.js
index 80acca0..6e03841 100644
--- a/prefs.js
+++ b/prefs.js
@@ -11,42 +11,164 @@ const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-let prefRoot = null;
-let branch = null;
+let {addonRoot} = require("info");
let Prefs = exports.Prefs =
{
- init: function(root)
+ branch: null,
+ ignorePrefChanges: false,
+
+ init: function(branchName)
{
- if (prefRoot)
+ if (this.branch)
return;
- prefRoot = root;
- branch = Services.prefs.getBranch(prefRoot);
+ this.branch = Services.prefs.getBranch(branchName);
- let defaultBranch = Services.prefs.getDefaultBranch(prefRoot);
- for each (let name in defaultBranch.getChildList("", {}))
+ /**
+ * Sets up getter/setter on Prefs object for preference.
+ */
+ function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
{
- let type = defaultBranch.getPrefType(name);
- switch (type)
+ let value = defaultValue;
+ this["_update_" + name] = function()
{
- case Ci.nsIPrefBranch.PREF_INT:
- defineIntegerProperty(name);
- break;
- case Ci.nsIPrefBranch.PREF_BOOL:
- defineBooleanProperty(name);
- break;
- case Ci.nsIPrefBranch.PREF_STRING:
- defineStringProperty(name);
- break;
- }
- if ("_update_" + name in PrefsPrivate)
- PrefsPrivate["_update_" + name]();
+ try
+ {
+ value = readFunc.call(this);
+ }
+ catch(e)
+ {
+ Cu.reportError(e);
+ }
+ };
+ Prefs.__defineGetter__(name, function() value);
+ Prefs.__defineSetter__(name, function(newValue)
+ {
+ if (value == newValue)
+ return value;
+
+ try
+ {
+ this.ignorePrefChanges = true;
+ writeFunc.call(this, newValue);
+ value = newValue;
+ }
+ catch(e)
+ {
+ Cu.reportError(e);
+ }
+ finally
+ {
+ this.ignorePrefChanges = false;
+ }
+ return value;
+ });
+ this["_update_" + name]();
+ }
+
+ /**
+ * Sets up getter/setter on Prefs object for an integer preference.
+ */
+ function defineIntegerProperty(/**String*/ name)
+ {
+ defineProperty.call(this, name, 0,
+ function() this.branch.getIntPref(name),
+ function(newValue) this.branch.setIntPref(name, newValue));
+ }
+
+ /**
+ * Sets up getter/setter on Prefs object for a boolean preference.
+ */
+ function defineBooleanProperty(/**String*/ name)
+ {
+ defineProperty.call(this, name, false,
+ function() this.branch.getBoolPref(name),
+ function(newValue) this.branch.setBoolPref(name, newValue));
+ }
+
+ /**
+ * Sets up getter/setter on Prefs object for a string preference.
+ */
+ function defineStringProperty(/**String*/ name)
+ {
+ defineProperty.call(this, name, "",
+ function() this.branch.getComplexValue(name, Ci.nsISupportsString).data,
+ function(newValue)
+ {
+ let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ str.data = newValue;
+ this.branch.setComplexValue(name, Ci.nsISupportsString, str);
+ });
+ }
+
+ /**
+ * Sets up getter/setter on Prefs object for a JSON-encoded preference.
+ */
+ function defineJSONProperty(/**String*/ name)
+ {
+ defineProperty.call(this, name, "",
+ function() JSON.parse(this.branch.getComplexValue(name, Ci.nsISupportsString).data),
+ function(newValue)
+ {
+ let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ str.data = JSON.stringify(newValue);
+ this.branch.setComplexValue(name, Ci.nsISupportsString, str);
+ });
}
+ // Load default preferences and set up properties for them
+ let defaultBranch = Services.prefs.getDefaultBranch(branchName);
+ let scope =
+ {
+ pref: function(pref, value)
+ {
+ if (pref.substr(0, branchName.length) != branchName)
+ {
+ Cu.reportError(new Error("Ignoring default preference " + pref + ", wrong branch."));
+ return;
+ }
+ pref = pref.substr(branchName.length);
+
+ switch(typeof value)
+ {
+ case "boolean":
+ {
+ defaultBranch.setBoolPref(pref, value);
+ defineBooleanProperty.call(Prefs, pref);
+ break;
+ }
+ case "number":
+ {
+ defaultBranch.setIntPref(pref, value);
+ defineIntegerProperty.call(Prefs, pref);
+ break;
+ }
+ case "string":
+ {
+ let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ str.data = value;
+ defaultBranch.setComplexValue(pref, Ci.nsISupportsString, str);
+ defineStringProperty.call(Prefs, pref);
+ break;
+ }
+ case "object":
+ {
+ let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ str.data = JSON.stringify(value);
+ defaultBranch.setComplexValue(pref, Ci.nsISupportsString, str);
+ defineJSONProperty.call(Prefs, pref);
+ break;
+ }
+ }
+ }
+ };
+ Services.scriptloader.loadSubScript(addonRoot + "defaults/preferences/prefs.js", scope);
+
+ // Add preference change observer
try
{
- branch.QueryInterface(Ci.nsIPrefBranch2)
- .addObserver("", PrefsPrivate, true);
+ this.branch.QueryInterface(Ci.nsIPrefBranch2)
+ .addObserver("", this, true);
}
catch (e)
{
@@ -69,108 +191,29 @@ let Prefs = exports.Prefs =
shutdown: function()
{
- if (!prefRoot)
+ if (!this.branch)
return;
- prefRoot = null;
try
{
- branch.QueryInterface(Ci.nsIPrefBranch2)
- .removeObserver("", PrefsPrivate);
+ this.branch.QueryInterface(Ci.nsIPrefBranch2)
+ .removeObserver("", this);
}
catch (e)
{
Cu.reportError(e);
}
- branch = null;
- }
-};
-
-let PrefsPrivate =
-{
- ignorePrefChanges: false,
+ this.branch = null;
+ },
observe: function(subject, topic, data)
{
- if (PrefsPrivate.ignorePrefChanges || topic != "nsPref:changed")
+ if (this.ignorePrefChanges || topic != "nsPref:changed")
return;
- if ("_update_" + data in PrefsPrivate)
- PrefsPrivate["_update_" + data]();
+ if ("_update_" + data in this)
+ this["_update_" + data]();
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObserver])
-}
-
-/**
- * Sets up getter/setter on Prefs object for preference.
- */
-function defineProperty(/**String*/ name, defaultValue, /**Function*/ readFunc, /**Function*/ writeFunc)
-{
- let value = defaultValue;
- PrefsPrivate["_update_" + name] = function()
- {
- try
- {
- value = readFunc();
- }
- catch(e)
- {
- Cu.reportError(e);
- }
- }
- Prefs.__defineGetter__(name, function() value);
- Prefs.__defineSetter__(name, function(newValue)
- {
- if (value == newValue)
- return value;
-
- try
- {
- PrefsPrivate.ignorePrefChanges = true;
- writeFunc(newValue);
- value = newValue;
- }
- catch(e)
- {
- Cu.reportError(e);
- }
- finally
- {
- PrefsPrivate.ignorePrefChanges = false;
- }
- return value;
- });
-}
-
-/**
- * Sets up getter/setter on Prefs object for an integer preference.
- */
-function defineIntegerProperty(/**String*/ name)
-{
- defineProperty(name, 0, function() branch.getIntPref(name),
- function(newValue) branch.setIntPref(name, newValue));
-}
-
-/**
- * Sets up getter/setter on Prefs object for a boolean preference.
- */
-function defineBooleanProperty(/**String*/ name)
-{
- defineProperty(name, false, function() branch.getBoolPref(name),
- function(newValue) branch.setBoolPref(name, newValue));
-}
-
-/**
- * Sets up getter/setter on Prefs object for a string preference.
- */
-function defineStringProperty(/**String*/ name)
-{
- defineProperty(name, "", function() branch.getComplexValue(name, Ci.nsISupportsString).data,
- function(newValue)
- {
- let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
- str.data = newValue;
- branch.setComplexValue(name, Ci.nsISupportsString, str);
- });
-}
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus-element-hiding-helper.git
More information about the Pkg-mozext-commits
mailing list