Bug 982856 - Make Preferences.jsm not use mutable [[Prototype]]. Special thanks to Edouard Oger for debugging the issue with the initial version of this patch. r=Unfocused
authorJeff Walden <jwalden@mit.edu>
Fri, 31 Jan 2014 11:34:43 -0800
changeset 240467 4d3000ff04310a70d410ba2e91e9971e82232d22
parent 240466 141ecbdcdb4527f432e801b6fddbb75976e52d13
child 240468 c023d6805859475919af61b696b406305b31dd1c
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersUnfocused
bugs982856
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 982856 - Make Preferences.jsm not use mutable [[Prototype]]. Special thanks to Edouard Oger for debugging the issue with the initial version of this patch. r=Unfocused
toolkit/modules/Preferences.jsm
--- a/toolkit/modules/Preferences.jsm
+++ b/toolkit/modules/Preferences.jsm
@@ -5,61 +5,62 @@
 this.EXPORTED_SYMBOLS = ["Preferences"];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
 
 // The minimum and maximum integers that can be set as preferences.
 // The range of valid values is narrower than the range of valid JS values
 // because the native preferences code treats integers as NSPR PRInt32s,
 // which are 32-bit signed integers on all platforms.
 const MAX_INT = 0x7FFFFFFF; // Math.pow(2, 31) - 1
 const MIN_INT = -0x80000000;
 
 this.Preferences =
   function Preferences(args) {
+    this._cachedPrefBranch = null;
     if (isObject(args)) {
       if (args.branch)
         this._prefBranch = args.branch;
       if (args.defaultBranch)
         this._defaultBranch = args.defaultBranch;
       if (args.privacyContext)
         this._privacyContext = args.privacyContext;
     }
     else if (args)
       this._prefBranch = args;
-}
+  };
 
-Preferences.prototype = {
   /**
    * Get the value of a pref, if any; otherwise return the default value.
    *
    * @param   prefName  {String|Array}
    *          the pref to get, or an array of prefs to get
    *
    * @param   defaultValue
    *          the default value, if any, for prefs that don't have one
    *
    * @param   valueType
    *          the XPCOM interface of the pref's complex value type, if any
    *
    * @returns the value of the pref, if any; otherwise the default value
    */
-  get: function(prefName, defaultValue, valueType = Ci.nsISupportsString) {
+  Preferences.get = function(prefName, defaultValue, valueType = Ci.nsISupportsString) {
     if (Array.isArray(prefName))
       return prefName.map(function(v) this.get(v, defaultValue), this);
 
     return this._get(prefName, defaultValue, valueType);
-  },
+  };
 
-  _get: function(prefName, defaultValue, valueType) {
+  Preferences._get = function(prefName, defaultValue, valueType) {
     switch (this._prefSvc.getPrefType(prefName)) {
       case Ci.nsIPrefBranch.PREF_STRING:
         return this._prefSvc.getComplexValue(prefName, valueType).data;
 
       case Ci.nsIPrefBranch.PREF_INT:
         return this._prefSvc.getIntPref(prefName);
 
       case Ci.nsIPrefBranch.PREF_BOOL:
@@ -69,17 +70,17 @@ Preferences.prototype = {
         return defaultValue;
 
       default:
         // This should never happen.
         throw "Error getting pref " + prefName + "; its value's type is " +
               this._prefSvc.getPrefType(prefName) + ", which I don't know " +
               "how to handle.";
     }
-  },
+  };
 
   /**
    * Set a preference to a value.
    *
    * You can set multiple prefs by passing an object as the only parameter.
    * In that case, this method will treat the properties of the object
    * as preferences to set, where each property name is the name of a pref
    * and its corresponding property value is the value of the pref.
@@ -93,27 +94,27 @@ Preferences.prototype = {
    *
    * Note: Preferences cannot store non-integer numbers or numbers outside
    * the signed 32-bit range -(2^31-1) to 2^31-1, If you have such a number,
    * store it as a string by calling toString() on the number before passing
    * it to this method, i.e.:
    *   Preferences.set("pi", 3.14159.toString())
    *   Preferences.set("big", Math.pow(2, 31).toString()).
    */
-  set: function(prefName, prefValue) {
+  Preferences.set = function(prefName, prefValue) {
     if (isObject(prefName)) {
       for (let [name, value] in Iterator(prefName))
         this.set(name, value);
       return;
     }
 
     this._set(prefName, prefValue);
-  },
+  };
 
-  _set: function(prefName, prefValue) {
+  Preferences._set = function(prefName, prefValue) {
     let prefType;
     if (typeof prefValue != "undefined" && prefValue != null)
       prefType = prefValue.constructor.name;
 
     switch (prefType) {
       case "String":
         {
           let string = Cc["@mozilla.org/supports-string;1"].
@@ -145,123 +146,123 @@ Preferences.prototype = {
       case "Boolean":
         this._prefSvc.setBoolPref(prefName, prefValue);
         break;
 
       default:
         throw "can't set pref " + prefName + " to value '" + prefValue +
               "'; it isn't a String, Number, or Boolean";
     }
-  },
+  };
 
   /**
    * 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
    * or a user-set value, while isSet only returns true if the value
    * is a user-set value.
    *
    * @param   prefName  {String|Array}
    *          the pref to check, or an array of prefs to check
    *
    * @returns {Boolean|Array}
    *          whether or not the pref has a value; or, if the caller provided
    *          an array of pref names, an array of booleans indicating whether
    *          or not the prefs have values
    */
-  has: function(prefName) {
+  Preferences.has = function(prefName) {
     if (Array.isArray(prefName))
       return prefName.map(this.has, this);
 
     return this._has(prefName);
-  },
+  };
 
-  _has: function(prefName) {
+  Preferences._has = function(prefName) {
     return (this._prefSvc.getPrefType(prefName) != Ci.nsIPrefBranch.PREF_INVALID);
-  },
+  };
 
   /**
    * 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-
    * set value, while |has| returns true if the value of the pref is a default
    * value or a user-set value.
    *
    * @param   prefName  {String|Array}
    *          the pref to check, or an array of prefs to check
    *
    * @returns {Boolean|Array}
    *          whether or not the pref has a user-set value; or, if the caller
    *          provided an array of pref names, an array of booleans indicating
    *          whether or not the prefs have user-set values
    */
-  isSet: function(prefName) {
+  Preferences.isSet = function(prefName) {
     if (Array.isArray(prefName))
       return prefName.map(this.isSet, this);
 
     return (this.has(prefName) && this._prefSvc.prefHasUserValue(prefName));
   },
 
   /**
    * Whether or not the given pref has a user-set value. Use isSet instead,
    * which is equivalent.
    * @deprecated
    */
-  modified: function(prefName) { return this.isSet(prefName) },
+  Preferences.modified = function(prefName) { return this.isSet(prefName) },
 
-  reset: function(prefName) {
+  Preferences.reset = function(prefName) {
     if (Array.isArray(prefName)) {
       prefName.map(function(v) this.reset(v), this);
       return;
     }
 
     this._prefSvc.clearUserPref(prefName);
-  },
+  };
 
   /**
    * Lock a pref so it can't be changed.
    *
    * @param   prefName  {String|Array}
    *          the pref to lock, or an array of prefs to lock
    */
-  lock: function(prefName) {
+  Preferences.lock = function(prefName) {
     if (Array.isArray(prefName))
       prefName.map(this.lock, this);
 
     this._prefSvc.lockPref(prefName);
-  },
+  };
 
   /**
    * Unlock a pref so it can be changed.
    *
    * @param   prefName  {String|Array}
    *          the pref to lock, or an array of prefs to lock
    */
-  unlock: function(prefName) {
+  Preferences.unlock = function(prefName) {
     if (Array.isArray(prefName))
       prefName.map(this.unlock, this);
 
     this._prefSvc.unlockPref(prefName);
-  },
+  };
 
   /**
    * Whether or not the given pref is locked against changes.
    *
    * @param   prefName  {String|Array}
    *          the pref to check, or an array of prefs to check
    *
    * @returns {Boolean|Array}
    *          whether or not the pref has a user-set value; or, if the caller
    *          provided an array of pref names, an array of booleans indicating
    *          whether or not the prefs have user-set values
    */
-  locked: function(prefName) {
+  Preferences.locked = function(prefName) {
     if (Array.isArray(prefName))
       return prefName.map(this.locked, this);
 
     return this._prefSvc.prefIsLocked(prefName);
-  },
+  };
 
   /**
    * Start observing a pref.
    *
    * The callback can be a function or any object that implements nsIObserver.
    * When the callback is a function and thisObject is provided, it gets called
    * as a method of thisObject.
    *
@@ -271,25 +272,25 @@ Preferences.prototype = {
    * @param   callback    {Function|Object}
    *          the code to notify when the pref changes;
    *
    * @param   thisObject  {Object}  [optional]
    *          the object to use as |this| when calling a Function callback;
    *
    * @returns the wrapped observer
    */
-  observe: function(prefName, callback, thisObject) {
+  Preferences.observe = function(prefName, callback, thisObject) {
     let fullPrefName = this._prefBranch + (prefName || "");
 
     let observer = new PrefObserver(fullPrefName, callback, thisObject);
     Preferences._prefSvc.addObserver(fullPrefName, observer, true);
     observers.push(observer);
 
     return observer;
-  },
+  };
 
   /**
    * Stop observing a pref.
    *
    * You must call this method with the same prefName, callback, and thisObject
    * with which you originally registered the observer.  However, you don't have
    * to call this method on the same exact instance of Preferences; you can call
    * it on any instance.  For example, the following code first starts and then
@@ -303,87 +304,85 @@ Preferences.prototype = {
    *          the name of the pref being observed
    *
    * @param   callback    {Function|Object}
    *          the code being notified when the pref changes
    *
    * @param   thisObject  {Object}  [optional]
    *          the object being used as |this| when calling a Function callback
    */
-  ignore: function(prefName, callback, thisObject) {
+  Preferences.ignore = function(prefName, callback, thisObject) {
     let fullPrefName = this._prefBranch + (prefName || "");
 
     // This seems fairly inefficient, but I'm not sure how much better we can
     // make it.  We could index by fullBranch, but we can't index by callback
     // or thisObject, as far as I know, since the keys to JavaScript hashes
     // (a.k.a. objects) can apparently only be primitive values.
     let [observer] = observers.filter(function(v) v.prefName   == fullPrefName &&
                                                   v.callback   == callback &&
                                                   v.thisObject == thisObject);
 
     if (observer) {
       Preferences._prefSvc.removeObserver(fullPrefName, observer);
       observers.splice(observers.indexOf(observer), 1);
     }
-  },
+  };
 
-  resetBranch: function(prefBranch = "") {
+  Preferences.resetBranch = function(prefBranch = "") {
     try {
       this._prefSvc.resetBranch(prefBranch);
     }
     catch(ex) {
       // The current implementation of nsIPrefBranch in Mozilla
       // doesn't implement resetBranch, so we do it ourselves.
       if (ex.result == Cr.NS_ERROR_NOT_IMPLEMENTED)
         this.reset(this._prefSvc.getChildList(prefBranch, []));
       else
         throw ex;
     }
   },
 
   /**
-   * The branch of the preferences tree to which this instance provides access.
+   * A string identifying the branch of the preferences tree to which this
+   * instance provides access.
    * @private
    */
-  _prefBranch: "",
+  Preferences._prefBranch = "";
 
   /**
-   * Preferences Service
+   * The cached preferences branch object this instance encapsulates, or null.
+   * Do not use!  Use _prefSvc below instead.
+   * @private
+   */
+  Preferences._cachedPrefBranch = null;
+
+  /**
+   * The preferences branch object for this instance.
    * @private
    */
-  get _prefSvc() {
-    let prefSvc = Cc["@mozilla.org/preferences-service;1"]
-                  .getService(Ci.nsIPrefService);
-    if (this._defaultBranch) {
-      prefSvc = prefSvc.getDefaultBranch(this._prefBranch);
-    } else {
-      prefSvc = prefSvc.getBranch(this._prefBranch);
-    }
-
-    this.__defineGetter__("_prefSvc", function() prefSvc);
-    return this._prefSvc;
-  },
+  Object.defineProperty(Preferences, "_prefSvc",
+  {
+    get: function _prefSvc() {
+      if (!this._cachedPrefBranch) {
+        let prefSvc = Services.prefs;
+        this._cachedPrefBranch = this._defaultBranch ?
+                                 prefSvc.getDefaultBranch(this._prefBranch) :
+                                 prefSvc.getBranch(this._prefBranch);
+      }
+      return this._cachedPrefBranch;
+    },
+    enumerable: true,
+    configurable: true
+  });
 
-  /**
-   * 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;
-  }
-
-};
-
-// Give the constructor the same prototype as its instances, so users can access
-// preferences directly via the constructor without having to create an instance
-// first.
-Preferences.__proto__ = Preferences.prototype;
+// Constructor-based access (Preferences.get(...) and set) is preferred over
+// instance-based access (new Preferences().get(...) and set) and when using the
+// root preferences branch, as it's desirable not to allocate the extra object.
+// But both forms are acceptable.
+Preferences.prototype = Preferences;
 
 /**
  * A cache of pref observers.
  *
  * We use this to remove observers when a caller calls Preferences::ignore.
  *
  * All Preferences instances share this object, because we want callers to be
  * able to remove an observer using a different Preferences object than the one