[Pkg-mozext-commits] [requestpolicy] 135/280: dynamically create the "cached" prefs in prefs.jsm

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:30:12 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 a2d1bdfde2ce94cc38e2ef08064c55fc4cb4e5bd
Author: Martin Kimmerle <dev at 256k.de>
Date:   Wed Jan 7 17:45:12 2015 +0100

    dynamically create the "cached" prefs in prefs.jsm
    
    …which is not necessary but a nice feature
---
 src/content/lib/prefs.jsm | 124 ++++++++++++++++++++++++++++++----------------
 1 file changed, 82 insertions(+), 42 deletions(-)

diff --git a/src/content/lib/prefs.jsm b/src/content/lib/prefs.jsm
index 05e6db4..06b8c85 100644
--- a/src/content/lib/prefs.jsm
+++ b/src/content/lib/prefs.jsm
@@ -54,20 +54,72 @@ let Prefs = (function() {
     Services.prefs.savePrefFile(null);
   };
 
-  self.isDefaultAllow = function() {
-    return defaultAllow;
-  };
-  self.isDefaultAllowSameDomain = function() {
-    return defaultAllowSameDomain;
-  };
-  self.isBlockingDisabled = function() {
-    return blockingDisabled;
-  };
-  self.setBlockingDisabled = function(disabled) {
-    blockingDisabled = disabled;
-    rootPrefBranch.setBoolPref('startWithAllowAllEnabled', disabled);
-    self.save();
+
+
+  /**
+   * Define a list of preferences that will be available through
+   * `Prefs.getter_function_name()` and `Prefs.setter_function_name()`.
+   * Those functions will be created subsequently.
+   */
+  let cachedPrefList = {
+    "defaultPolicy.allow": {
+      getter: {name: "isDefaultAllow", fn: rpPrefBranch.getBoolPref}
+    },
+    "defaultPolicy.allowSameDomain": {
+      getter: {name: "isDefaultAllowSameDomain", fn: rpPrefBranch.getBoolPref}
+    },
+    "startWithAllowAllEnabled": {
+      getter: {name: "isBlockingDisabled", fn: rpPrefBranch.getBoolPref},
+      setter: {name: "setBlockingDisabled", fn: rootPrefBranch.setBoolPref}
+    }
   };
+  let cachedPrefs = {};
+
+
+  /**
+   * Dynamically create functions like `isDefaultAllow` or
+   * `setBlockingDisabled`. Also add `update()` functions to elements of
+   * `cachedPrefList` that have a `getter`.
+   */
+  {
+    for (let prefID in cachedPrefList) {
+      let pref = cachedPrefList[prefID];
+
+      if (pref.hasOwnProperty("getter")) {
+        let getterName = pref.getter.name;
+
+        // define the pref's getter function to `self`
+        self[getterName] = function() {
+          return cachedPrefs[getterName];
+        };
+
+        // define the pref's update() function to `cachedPrefList`
+        pref.update = function() {
+          cachedPrefs[prefID] = pref.getter.fn(prefID);
+        };
+
+        // initially call update()
+        pref.update();
+      }
+
+      if (pref.hasOwnProperty("setter")) {
+        let setterName = pref.setter.name;
+
+        // define the pref's getter function to `self`
+        self[setterName] = function(aValue) {
+          // set the pref and save it
+          pref.setter.fn(prefID, aValue);
+          self.save();
+
+          // update the cached value
+          if (typeof pref.update !== 'undefined') {
+            pref.update();
+          }
+        };
+      }
+    }
+  }
+
   self.isPrefetchEnabled = function() {
     // network.dns.disablePrefetch only exists starting in Firefox 3.1
     try {
@@ -78,26 +130,6 @@ let Prefs = (function() {
     }
   };
 
-
-  /**
-   * Take necessary actions when preferences are updated.
-   *
-   * @param {String} prefName name of the preference that was updated.
-   */
-  function updateCachedPref(prefName) {
-    switch (prefName) {
-      case "defaultPolicy.allow" :
-        defaultAllow = rpPrefBranch.getBoolPref("defaultPolicy.allow");
-        break;
-      case "defaultPolicy.allowSameDomain" :
-        defaultAllowSameDomain = rpPrefBranch.getBoolPref(
-            "defaultPolicy.allowSameDomain");
-        break;
-      default:
-        break;
-    }
-  }
-
   function isPrefEmpty(pref) {
     try {
       let value = rpPrefBranch.getComplexValue(pref, Ci.nsISupportsString).data;
@@ -113,10 +145,22 @@ let Prefs = (function() {
              isPrefEmpty('allowedOriginsToDestinations'));
   };
 
-  function init() {
-    defaultAllow = rpPrefBranch.getBoolPref("defaultPolicy.allow");
-    defaultAllowSameDomain = rpPrefBranch.getBoolPref("defaultPolicy.allowSameDomain");
-    blockingDisabled = rpPrefBranch.getBoolPref("startWithAllowAllEnabled");
+
+  /**
+   * This function updates all cached prefs.
+   */
+  function updateCachedPref(prefID) {
+    // first check if this pref is cached
+    if (!cachedPrefList.hasOwnProperty(prefID)) {
+      return;
+    }
+
+    let pref = cachedPrefList[prefID];
+
+    // check if this pref has an update() function
+    if (typeof pref.update === 'function') {
+      pref.update();
+    }
   }
 
   let observePref = function(subject, topic, data) {
@@ -130,13 +174,9 @@ let Prefs = (function() {
     }
   };
 
-  init();
-
   ProcessEnvironment.enqueueStartupFunction(function() {
     ProcessEnvironment.obMan.observeRPPref({
-      "": function() {
-        observePref.apply(null, arguments);
-      }
+      "": observePref
     });
     ProcessEnvironment.obMan.observeRootPref({
       "network.prefetch-next": observePref,

-- 
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