Browse Source

initial support for accessing site-specific preferences in the Preferences module

Myk Melez 15 years ago
parent
commit
d06420992c
2 changed files with 118 additions and 3 deletions
  1. 82 3
      Preferences.js
  2. 36 0
      test/unit/test_Preferences.js

+ 82 - 3
Preferences.js

@@ -51,9 +51,15 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 const MAX_INT = Math.pow(2, 31) - 1;
 const MIN_INT = -MAX_INT;
 
-function Preferences(prefBranch) {
-  if (prefBranch)
-    this._prefBranch = prefBranch;
+function Preferences(args) {
+    if (isObject(args)) {
+      if (args.branch)
+        this._prefBranch = args.branch;
+      if (args.site)
+        this._site = args.site;
+    }
+    else if (args)
+      this._prefBranch = args;
 }
 
 Preferences.prototype = {
@@ -72,6 +78,13 @@ Preferences.prototype = {
     if (isArray(prefName))
       return prefName.map(function(v) this.get(v, defaultValue), this);
 
+    if (this._site)
+      return this._siteGet(prefName, defaultValue);
+    else
+      return this._get(prefName, defaultValue);
+  },
+
+  _get: function(prefName, defaultValue) {
     switch (this._prefSvc.getPrefType(prefName)) {
       case Ci.nsIPrefBranch.PREF_STRING:
         return this._prefSvc.getComplexValue(prefName, Ci.nsISupportsString).data;
@@ -93,6 +106,11 @@ Preferences.prototype = {
     }
   },
 
+  _siteGet: function(prefName, defaultValue) {
+    let value = this._contentPrefSvc.getPref(this._site, this._prefBranch + prefName);
+    return typeof value != "undefined" ? value : defaultValue;
+  },
+
   /**
    * Set a preference to a value.
    *
@@ -122,6 +140,13 @@ Preferences.prototype = {
       return;
     }
 
+    if (this._site)
+      this._siteSet(prefName, prefValue);
+    else
+      this._set(prefName, prefValue);
+  },
+
+  _set: function(prefName, prefValue) {
     let prefType;
     if (typeof prefValue != "undefined" && prefValue != null)
       prefType = prefValue.constructor.name;
@@ -165,6 +190,10 @@ Preferences.prototype = {
     }
   },
 
+  _siteSet: function(prefName, prefValue) {
+    this._contentPrefSvc.setPref(this._site, this._prefBranch + prefName, prefValue);
+  },
+
   /**
    * Whether or not the given pref has a value.  This is different from isSet
    * because it returns true whether the value of the pref is a default value
@@ -183,9 +212,20 @@ Preferences.prototype = {
     if (isArray(prefName))
       return prefName.map(this.has, this);
 
+    if (this._site)
+      return this._siteHas(prefName);
+    else
+      return this._has(prefName);
+  },
+
+  _has: function(prefName) {
     return (this._prefSvc.getPrefType(prefName) != Ci.nsIPrefBranch.PREF_INVALID);
   },
 
+  _siteHas: function(prefName) {
+    return this._contentPrefSvc.hasPref(this._site, this._prefBranch + prefName);
+  },
+
   /**
    * Whether or not the given pref has a user-set value.  This is different
    * from |has| because it returns true only if the value of the pref is a user-
@@ -220,6 +260,13 @@ Preferences.prototype = {
       return;
     }
 
+    if (this._site)
+      this._siteReset(prefName);
+    else
+      this._reset(prefName);
+  },
+  
+  _reset: function(prefName) {
     try {
       this._prefSvc.clearUserPref(prefName);
     }
@@ -236,6 +283,10 @@ Preferences.prototype = {
     }
   },
 
+  _siteReset: function(prefName) {
+    return this._contentPrefSvc.removePref(this._site, this._prefBranch + prefName);
+  },
+
   /**
    * Lock a pref so it can't be changed.
    *
@@ -367,6 +418,12 @@ Preferences.prototype = {
    */
   _prefBranch: "",
 
+  site: function(site) {
+    if (!(site instanceof Ci.nsIURI))
+      site = this._ioSvc.newURI("http://" + site, null, null);
+    return new Preferences({ branch: this._prefBranch, site: site });
+  },
+
   /**
    * Preferences Service
    * @private
@@ -378,6 +435,28 @@ Preferences.prototype = {
                   QueryInterface(Ci.nsIPrefBranch2);
     this.__defineGetter__("_prefSvc", function() prefSvc);
     return this._prefSvc;
+  },
+
+  /**
+   * IO Service
+   * @private
+   */
+  get _ioSvc() {
+    let ioSvc = Cc["@mozilla.org/network/io-service;1"].
+                getService(Ci.nsIIOService);
+    this.__defineGetter__("_ioSvc", function() ioSvc);
+    return this._ioSvc;
+  },
+
+  /**
+   * Site Preferences Service
+   * @private
+   */
+  get _contentPrefSvc() {
+    let contentPrefSvc = Cc["@mozilla.org/content-pref/service;1"].
+                         getService(Ci.nsIContentPrefService);
+    this.__defineGetter__("_contentPrefSvc", function() contentPrefSvc);
+    return this._contentPrefSvc;
   }
 
 };

+ 36 - 0
test/unit/test_Preferences.js

@@ -14,6 +14,17 @@ function test_set_get_pref() {
   Preferences.resetBranch("test_set_get_pref.");
 }
 
+function test_set_get_branch_pref() {
+  let prefs = new Preferences("test_set_get_branch_pref.");
+
+  prefs.set("something", 1);
+  do_check_eq(prefs.get("something"), 1);
+  do_check_false(Preferences.has("something"));
+
+  // Clean up.
+  prefs.reset("something");
+}
+
 function test_set_get_multiple_prefs() {
   Preferences.set({ "test_set_get_multiple_prefs.integer":  1,
                     "test_set_get_multiple_prefs.string":   "foo",
@@ -288,3 +299,28 @@ function test_lock_prefs() {
   // Clean up.
   Preferences.reset("toolkit.defaultChromeURI");
 }
+
+function test_site_prefs() {
+  let prefs = Preferences.site("www.example.com");
+
+  prefs.set("test_site_prefs.integer", 1);
+  do_check_eq(prefs.get("test_site_prefs.integer"), 1);
+  do_check_true(prefs.has("test_site_prefs.integer"));
+  do_check_false(Preferences.has("test_site_prefs.integer"));
+  prefs.reset("test_site_prefs.integer");
+  do_check_false(prefs.has("test_site_prefs.integer"));
+
+  prefs.set("test_site_prefs.string", "foo");
+  do_check_eq(prefs.get("test_site_prefs.string"), "foo");
+  do_check_true(prefs.has("test_site_prefs.string"));
+  do_check_false(Preferences.has("test_site_prefs.string"));
+  prefs.reset("test_site_prefs.string");
+  do_check_false(prefs.has("test_site_prefs.string"));
+
+  prefs.set("test_site_prefs.boolean", true);
+  do_check_eq(prefs.get("test_site_prefs.boolean"), true);
+  do_check_true(prefs.has("test_site_prefs.boolean"));
+  do_check_false(Preferences.has("test_site_prefs.boolean"));
+  prefs.reset("test_site_prefs.boolean");
+  do_check_false(prefs.has("test_site_prefs.boolean"));
+}