[Pkg-mozext-commits] [requestpolicy] 132/280: ObserverManager: add `nsIPrefBranch` observation
David Prévot
taffit at moszumanska.debian.org
Sat May 2 20:30:11 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository requestpolicy.
commit f17b3f56de8f8cf59ae21ccb9be2b2d4b38573d5
Author: Martin Kimmerle <dev at 256k.de>
Date: Wed Jan 7 14:08:46 2015 +0100
ObserverManager: add `nsIPrefBranch` observation
- ObserverManager now has two new methods:
- observeRPPref
- observeRootPref
They take similar arguments like the `observe` method.
- There are now several `*Observer` classes (besides the already
existing class `SinglePrefBranchObserver`. Those classes have
been moved to `lib/utils/observers.jsm`.
---
src/content/lib/logger.jsm | 17 ++--
src/content/lib/observer-manager.jsm | 152 ++++++++++++++++++++++++-----------
src/content/lib/prefs.jsm | 35 +++++---
src/content/lib/utils/observers.jsm | 92 +++++++++++++++++++++
4 files changed, 230 insertions(+), 66 deletions(-)
diff --git a/src/content/lib/logger.jsm b/src/content/lib/logger.jsm
index 59b865a..d66cd3e 100644
--- a/src/content/lib/logger.jsm
+++ b/src/content/lib/logger.jsm
@@ -94,14 +94,6 @@ let Logger = (function() {
types = rpPrefBranch.getIntPref("log.types");
}
- let prefObserver = {
- observe: function(subject, topic, data) {
- if (topic == "nsPref:changed") {
- updateLoggingSettings();
- }
- }
- };
-
/**
* init() is called by doLog() until initialization was successful.
* For the case that nothing is logged at all, init is registered as a
@@ -113,7 +105,14 @@ let Logger = (function() {
return;
}
- rpPrefBranch.addObserver("log", prefObserver, false);
+ // rpPrefBranch is available now.
+ ProcessEnvironment.obMan.observeRPPref({
+ "log": function(subject, topic, data) {
+ if (topic == "nsPref:changed") {
+ updateLoggingSettings();
+ }
+ }
+ });
updateLoggingSettings();
// don't call init() anymore when doLog() is called
diff --git a/src/content/lib/observer-manager.jsm b/src/content/lib/observer-manager.jsm
index 51a08f8..7029aff 100644
--- a/src/content/lib/observer-manager.jsm
+++ b/src/content/lib/observer-manager.jsm
@@ -27,10 +27,19 @@ const Cu = Components.utils;
let EXPORTED_SYMBOLS = ["ObserverManager"];
-Cu.import("resource://gre/modules/Services.jsm");
+let globalScope = this;
+
Cu.import("chrome://requestpolicy/content/lib/script-loader.jsm");
+ScriptLoader.importModules([
+ "lib/utils/observers",
+ "lib/process-environment"
+], globalScope);
+
+ScriptLoader.defineLazyModuleGetters({
+ "lib/prefs": ["rpPrefBranch", "rootPrefBranch"]
+}, globalScope);
+
-let {ProcessEnvironment} = ScriptLoader.importModule("lib/process-environment");
// Load the Logger at startup-time, not at load-time!
// ( On load-time Logger might be null. )
@@ -41,36 +50,6 @@ ProcessEnvironment.enqueueStartupFunction(function() {
-/**
- * An instance of this class registers itself to `nsIObserverService` on behalf
- * of some other object.
- */
-function SingleTopicObserver(aTopic, aFunctionToCall) {
- // save the parameters
- this.topic = aTopic;
- // As the `observe` function, take directly the parameter.
- this.observe = aFunctionToCall;
-
- // currently this obserer is not rgistered yet
- this.isRegistered = false;
-
- // register this observer
- this.register();
-}
-SingleTopicObserver.prototype.register = function() {
- if (!this.isRegistered) {
- Services.obs.addObserver(this, this.topic, false);
- this.isRegistered = true;
- }
-};
-SingleTopicObserver.prototype.unregister = function() {
- if (this.isRegistered) {
- Services.obs.removeObserver(this, this.topic);
- this.isRegistered = false;
- }
-};
-
-
/**
* An ObserverManager provides an interface to `nsIObserverService` which takes
@@ -84,6 +63,7 @@ function ObserverManager(aEnv) {
if (!!aEnv) {
self.environment.pushShutdownFunction(function() {
+ // unregister when the environment shuts down
self.unregisterAllObservers();
});
} else {
@@ -99,24 +79,104 @@ function ObserverManager(aEnv) {
self.observers = [];
}
+
/**
- * This function can be called from anywhere; the caller hands over an object
- * with the keys being the *topic* and the values being the function that
- * should be called when the topic is observed.
+ * Define 'observe' functions. Those function can be called from anywhere;
+ * the caller hands over an object with the keys being the "IDs" and the values
+ * being the function that should be called when the "ID" is observed.
+ *
+ * The "ID" for each function might be something different.
*/
-ObserverManager.prototype.observe = function(aList) {
- let self = this;
- for (let topic in aList) {
- if (aList.hasOwnProperty(topic)) {
- self.observers.push(new SingleTopicObserver(topic, aList[topic]));
+{
+ /**
+ * Call aCallback for each of the object's entries, (key, value) being the
+ * parameters.
+ */
+ let forEach = function(obj, aCallback) {
+ for (let key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ aCallback(key, obj[key]);
+ }
}
- }
-};
+ };
+
+
+ //
+ // functions using nsIObserverService
+ //
+
+ /**
+ * Observe one single topic by using nsIObserverService. Details:
+ * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIObserverService#addObserver%28%29
+ *
+ * @param {string} aTopic - The topic to be observed.
+ * @param {Function} aCallback - The observer's callback function.
+ */
+ ObserverManager.prototype.observeSingleTopic = function(aTopic, aCallback) {
+ let self = this;
+ self.observers.push(new SingleTopicObserver(aTopic, aCallback));
+ };
+
+ // shorthand for binding
+ let observeSingleTopic = ObserverManager.prototype.observeSingleTopic;
+
+ /**
+ * Observe multiple topics.
+ *
+ * @param {Object} aList - A list whereas a key is a "topic" to be observed
+ * and a value is the function to be called when the topic is observed.
+ */
+ ObserverManager.prototype.observe = function(aList) {
+ let self = this;
+ forEach(aList, observeSingleTopic.bind(self));
+ };
+
+ /**
+ * A shorthand for calling observe() with topic "requestpolicy-prefs-changed".
+ */
+ ObserverManager.prototype.observePrefChanges = function(aCallback) {
+ let self = this;
+ self.observeSingleTopic("requestpolicy-prefs-changed", aCallback);
+ };
+
+ //
+ // functions using nsIPrefBranch
+ //
+
+ /**
+ * Observe one single subdomain of a Pref Branch (using nsIPrefBranch).
+ * Details: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIPrefBranch#addObserver%28%29
+ *
+ * @param {string} aTopic - The topic to be observed.
+ * @param {Function} aCallback - The observer's callback function.
+ */
+ ObserverManager.prototype.observeSinglePrefBranch = function(aPrefBranch,
+ aDomain,
+ aCallback) {
+ let self = this;
+ let obs = new SinglePrefBranchObserver(aPrefBranch, aDomain, aCallback);
+ self.observers.push(obs);
+ };
+
+ // shorthand for binding
+ let observeSinglePrefBranch = ObserverManager.prototype.observeSinglePrefBranch;
+
+ /**
+ * Observe a notification directly from nsIObserverService.
+ * ==> In this case, each "ID" is a "topic".
+ * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIObserverService#addObserver%28%29
+ */
+ ObserverManager.prototype.observeRPPref = function(aList) {
+ let self = this;
+ forEach(aList, observeSinglePrefBranch.bind(self, rpPrefBranch));
+ };
+ ObserverManager.prototype.observeRootPref = function(aList) {
+ let self = this;
+ forEach(aList, observeSinglePrefBranch.bind(self, rootPrefBranch));
+ };
+}
+
-ObserverManager.prototype.observePrefChanges = function(aFunctionToCall) {
- let self = this;
- self.observe({"requestpolicy-prefs-changed": aFunctionToCall});
-};
/**
* The function will unregister all registered observers.
diff --git a/src/content/lib/prefs.jsm b/src/content/lib/prefs.jsm
index 7f32da5..05e6db4 100644
--- a/src/content/lib/prefs.jsm
+++ b/src/content/lib/prefs.jsm
@@ -29,6 +29,9 @@ let EXPORTED_SYMBOLS = ['rpPrefBranch', 'rootPrefBranch', 'Prefs'];
Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("chrome://requestpolicy/content/lib/script-loader.jsm");
+ScriptLoader.importModules(["lib/process-environment"], this);
+
let rpPrefBranch = Services.prefs.getBranch("extensions.requestpolicy.")
@@ -81,7 +84,7 @@ let Prefs = (function() {
*
* @param {String} prefName name of the preference that was updated.
*/
- function prefChanged(prefName) {
+ function updateCachedPref(prefName) {
switch (prefName) {
case "defaultPolicy.allow" :
defaultAllow = rpPrefBranch.getBoolPref("defaultPolicy.allow");
@@ -93,7 +96,6 @@ let Prefs = (function() {
default:
break;
}
- Services.obs.notifyObservers(null, "requestpolicy-prefs-changed", null);
}
function isPrefEmpty(pref) {
@@ -117,19 +119,30 @@ let Prefs = (function() {
blockingDisabled = rpPrefBranch.getBoolPref("startWithAllowAllEnabled");
}
- let prefObserver = {
- observe: function(subject, topic, data) {
- if (topic == "nsPref:changed") {
- prefChanged(data);
- }
+ let observePref = function(subject, topic, data) {
+ if (topic == "nsPref:changed") {
+ updateCachedPref(data);
+
+ // Send an observer notification that a pref that affects RP has been
+ // changed.
+ // TODO: also send the pref's name and its branch
+ Services.obs.notifyObservers(null, "requestpolicy-prefs-changed", null);
}
};
- rpPrefBranch.addObserver("", prefObserver, false);
- rootPrefBranch.addObserver("network.prefetch-next", prefObserver, false);
- rootPrefBranch.addObserver("network.dns.disablePrefetch", prefObserver, false);
-
init();
+ ProcessEnvironment.enqueueStartupFunction(function() {
+ ProcessEnvironment.obMan.observeRPPref({
+ "": function() {
+ observePref.apply(null, arguments);
+ }
+ });
+ ProcessEnvironment.obMan.observeRootPref({
+ "network.prefetch-next": observePref,
+ "network.dns.disablePrefetch": observePref
+ });
+ });
+
return self;
}());
diff --git a/src/content/lib/utils/observers.jsm b/src/content/lib/utils/observers.jsm
new file mode 100644
index 0000000..0906362
--- /dev/null
+++ b/src/content/lib/utils/observers.jsm
@@ -0,0 +1,92 @@
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ *
+ * RequestPolicy - A Firefox extension for control over cross-site requests.
+ * Copyright (c) 2008-2012 Justin Samuel
+ * Copyright (c) 2014-2015 Martin Kimmerle
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * ***** END LICENSE BLOCK *****
+ */
+
+const Ci = Components.interfaces;
+const Cc = Components.classes;
+const Cu = Components.utils;
+
+let EXPORTED_SYMBOLS = ["SingleTopicObserver", "SinglePrefBranchObserver"];
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+
+/**
+ * Generic Observer class.
+ */
+function Observer(aCallback) {
+ // As the `observe` function, take directly the parameter.
+ this.observe = aCallback;
+
+ // currently this obserer is not rgistered yet
+ this.isRegistered = false;
+
+ // register this observer
+ this.register();
+}
+Observer.prototype.register = function() {
+ if (!this.isRegistered) {
+ this._register();
+ this.isRegistered = true;
+ }
+};
+Observer.prototype.unregister = function() {
+ if (this.isRegistered) {
+ this._unregister();
+ this.isRegistered = false;
+ }
+};
+
+
+/**
+ * An instance of this class registers itself to `nsIObserverService` on behalf
+ * of some other object.
+ */
+function SingleTopicObserver(aTopic, aCallback) {
+ this.topic = aTopic;
+ Observer.call(this, aCallback);
+}
+SingleTopicObserver.prototype = Object.create(Observer.prototype);
+SingleTopicObserver.prototype.constructor = Observer;
+
+SingleTopicObserver.prototype._register = function() {
+ Services.obs.addObserver(this, this.topic, false);
+};
+SingleTopicObserver.prototype._unregister = function() {
+ Services.obs.removeObserver(this, this.topic);
+};
+
+
+function SinglePrefBranchObserver(aBranch, aDomain, aCallback) {
+ this.branch = aBranch;
+ this.domain = aDomain;
+ Observer.call(this, aCallback);
+}
+SinglePrefBranchObserver.prototype = Object.create(Observer.prototype);
+SinglePrefBranchObserver.prototype.constructor = Observer;
+
+SinglePrefBranchObserver.prototype._register = function() {
+ this.branch.addObserver(this.domain, this, false);
+};
+SinglePrefBranchObserver.prototype._unregister = function() {
+ this.branch.removeObserver(this.domain, this);
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git
More information about the Pkg-mozext-commits
mailing list