2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-03 14:40:09 -08:00
|
|
|
|
|
|
|
let EXPORTED_SYMBOLS = ["Preferences"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.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 = Math.pow(2, 31) - 1;
|
|
|
|
const MIN_INT = -MAX_INT;
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
function Preferences(args) {
|
|
|
|
if (isObject(args)) {
|
|
|
|
if (args.branch)
|
|
|
|
this._prefBranch = args.branch;
|
2010-06-16 15:11:40 -07:00
|
|
|
if (args.defaultBranch)
|
|
|
|
this._defaultBranch = args.defaultBranch;
|
2009-05-02 16:20:08 -07:00
|
|
|
if (args.site)
|
|
|
|
this._site = args.site;
|
|
|
|
}
|
|
|
|
else if (args)
|
|
|
|
this._prefBranch = args;
|
2008-11-03 14:40:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Preferences.prototype = {
|
|
|
|
/**
|
2009-03-06 17:58:22 -08:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @returns the value of the pref, if any; otherwise the default value
|
|
|
|
*/
|
2008-11-03 14:40:09 -08:00
|
|
|
get: function(prefName, defaultValue) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
return prefName.map(function(v) this.get(v, defaultValue), this);
|
2008-11-03 14:40:09 -08:00
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
if (this._site)
|
|
|
|
return this._siteGet(prefName, defaultValue);
|
|
|
|
else
|
|
|
|
return this._get(prefName, defaultValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
_get: function(prefName, defaultValue) {
|
2008-11-03 14:40:09 -08:00
|
|
|
switch (this._prefSvc.getPrefType(prefName)) {
|
|
|
|
case Ci.nsIPrefBranch.PREF_STRING:
|
2009-02-19 07:50:04 -08:00
|
|
|
return this._prefSvc.getComplexValue(prefName, Ci.nsISupportsString).data;
|
2008-11-03 14:40:09 -08:00
|
|
|
|
|
|
|
case Ci.nsIPrefBranch.PREF_INT:
|
|
|
|
return this._prefSvc.getIntPref(prefName);
|
|
|
|
|
|
|
|
case Ci.nsIPrefBranch.PREF_BOOL:
|
|
|
|
return this._prefSvc.getBoolPref(prefName);
|
|
|
|
|
|
|
|
case Ci.nsIPrefBranch.PREF_INVALID:
|
|
|
|
return defaultValue;
|
2009-03-06 17:58:22 -08:00
|
|
|
|
|
|
|
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.";
|
2008-11-03 14:40:09 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
_siteGet: function(prefName, defaultValue) {
|
|
|
|
let value = this._contentPrefSvc.getPref(this._site, this._prefBranch + prefName);
|
|
|
|
return typeof value != "undefined" ? value : defaultValue;
|
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param prefName {String|Object}
|
|
|
|
* the name of the pref to set; or an object containing a set
|
|
|
|
* of prefs to set
|
|
|
|
*
|
|
|
|
* @param prefValue {String|Number|Boolean}
|
|
|
|
* the value to which to set the pref
|
|
|
|
*
|
|
|
|
* 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()).
|
|
|
|
*/
|
2008-11-03 14:40:09 -08:00
|
|
|
set: function(prefName, prefValue) {
|
2009-02-19 07:50:04 -08:00
|
|
|
if (isObject(prefName)) {
|
2008-11-03 14:40:09 -08:00
|
|
|
for (let [name, value] in Iterator(prefName))
|
|
|
|
this.set(name, value);
|
2009-02-19 07:50:04 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
if (this._site)
|
|
|
|
this._siteSet(prefName, prefValue);
|
|
|
|
else
|
|
|
|
this._set(prefName, prefValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
_set: function(prefName, prefValue) {
|
2009-03-06 17:58:22 -08:00
|
|
|
let prefType;
|
|
|
|
if (typeof prefValue != "undefined" && prefValue != null)
|
|
|
|
prefType = prefValue.constructor.name;
|
|
|
|
|
|
|
|
switch (prefType) {
|
|
|
|
case "String":
|
|
|
|
{
|
|
|
|
let string = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
string.data = prefValue;
|
|
|
|
this._prefSvc.setComplexValue(prefName, Ci.nsISupportsString, string);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Number":
|
|
|
|
// We throw if the number is outside the range, since the result
|
|
|
|
// will never be what the consumer wanted to store, but we only warn
|
|
|
|
// if the number is non-integer, since the consumer might not mind
|
|
|
|
// the loss of precision.
|
|
|
|
if (prefValue > MAX_INT || prefValue < MIN_INT)
|
|
|
|
throw("you cannot set the " + prefName + " pref to the number " +
|
|
|
|
prefValue + ", as number pref values must be in the signed " +
|
|
|
|
"32-bit integer range -(2^31-1) to 2^31-1. To store numbers " +
|
|
|
|
"outside that range, store them as strings.");
|
2009-02-19 07:50:04 -08:00
|
|
|
this._prefSvc.setIntPref(prefName, prefValue);
|
|
|
|
if (prefValue % 1 != 0)
|
2009-03-06 17:58:22 -08:00
|
|
|
Cu.reportError("Warning: setting the " + prefName + " pref to the " +
|
|
|
|
"non-integer number " + prefValue + " converted it " +
|
|
|
|
"to the integer number " + this.get(prefName) +
|
|
|
|
"; to retain fractional precision, store non-integer " +
|
|
|
|
"numbers as strings.");
|
2009-02-19 07:50:04 -08:00
|
|
|
break;
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
case "Boolean":
|
2009-02-19 07:50:04 -08:00
|
|
|
this._prefSvc.setBoolPref(prefName, prefValue);
|
|
|
|
break;
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
default:
|
|
|
|
throw "can't set pref " + prefName + " to value '" + prefValue +
|
|
|
|
"'; it isn't a String, Number, or Boolean";
|
2008-11-03 14:40:09 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
_siteSet: function(prefName, prefValue) {
|
|
|
|
this._contentPrefSvc.setPref(this._site, this._prefBranch + prefName, prefValue);
|
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
return prefName.map(this.has, this);
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
if (this._site)
|
|
|
|
return this._siteHas(prefName);
|
|
|
|
else
|
|
|
|
return this._has(prefName);
|
|
|
|
},
|
|
|
|
|
|
|
|
_has: function(prefName) {
|
2009-03-06 17:58:22 -08:00
|
|
|
return (this._prefSvc.getPrefType(prefName) != Ci.nsIPrefBranch.PREF_INVALID);
|
|
|
|
},
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
_siteHas: function(prefName) {
|
|
|
|
return this._contentPrefSvc.hasPref(this._site, this._prefBranch + prefName);
|
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
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) },
|
|
|
|
|
2009-02-19 07:50:04 -08:00
|
|
|
reset: function(prefName) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName)) {
|
2009-02-19 07:50:04 -08:00
|
|
|
prefName.map(function(v) this.reset(v), this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
if (this._site)
|
|
|
|
this._siteReset(prefName);
|
|
|
|
else
|
|
|
|
this._reset(prefName);
|
|
|
|
},
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
_reset: function(prefName) {
|
2009-02-19 07:50:04 -08:00
|
|
|
try {
|
|
|
|
this._prefSvc.clearUserPref(prefName);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
// The pref service throws NS_ERROR_UNEXPECTED when the caller tries
|
|
|
|
// to reset a pref that doesn't exist or is already set to its default
|
|
|
|
// value. This interface fails silently in those cases, so callers
|
|
|
|
// can unconditionally reset a pref without having to check if it needs
|
|
|
|
// resetting first or trap exceptions after the fact. It passes through
|
|
|
|
// other exceptions, however, so callers know about them, since we don't
|
|
|
|
// know what other exceptions might be thrown and what they might mean.
|
|
|
|
if (ex.result != Cr.NS_ERROR_UNEXPECTED)
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
_siteReset: function(prefName) {
|
|
|
|
return this._contentPrefSvc.removePref(this._site, this._prefBranch + prefName);
|
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
prefName.map(this.lock, this);
|
2008-11-03 14:40:09 -08:00
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
this._prefSvc.lockPref(prefName);
|
2008-11-03 14:40:09 -08:00
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
prefName.map(this.unlock, this);
|
|
|
|
|
|
|
|
this._prefSvc.unlockPref(prefName);
|
2008-11-03 14:40:09 -08:00
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2008-11-03 14:40:09 -08:00
|
|
|
locked: function(prefName) {
|
2011-08-26 10:26:11 -07:00
|
|
|
if (Array.isArray(prefName))
|
2009-03-06 17:58:22 -08:00
|
|
|
return prefName.map(this.locked, this);
|
|
|
|
|
|
|
|
return this._prefSvc.prefIsLocked(prefName);
|
2008-11-03 14:40:09 -08:00
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param prefName {String}
|
|
|
|
* the name of the pref to observe
|
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
let fullPrefName = this._prefBranch + (prefName || "");
|
|
|
|
|
|
|
|
let observer = new PrefObserver(fullPrefName, callback, thisObject);
|
|
|
|
Preferences._prefSvc.addObserver(fullPrefName, observer, true);
|
|
|
|
observers.push(observer);
|
|
|
|
|
|
|
|
return observer;
|
2008-11-03 14:40:09 -08:00
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* stops observing the "foo.bar.baz" preference:
|
|
|
|
*
|
|
|
|
* let observer = function() {...};
|
|
|
|
* Preferences.observe("foo.bar.baz", observer);
|
|
|
|
* new Preferences("foo.bar.").ignore("baz", observer);
|
|
|
|
*
|
|
|
|
* @param prefName {String}
|
|
|
|
* 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) {
|
|
|
|
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);
|
|
|
|
}
|
2008-11-03 14:40:09 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
resetBranch: function(prefBranch) {
|
2009-02-18 12:03:12 -08:00
|
|
|
try {
|
|
|
|
this._prefSvc.resetBranch(prefBranch);
|
2009-02-19 07:50:04 -08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2009-03-06 17:58:22 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The branch of the preferences tree to which this instance provides access.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_prefBranch: "",
|
|
|
|
|
2009-05-02 16:20:08 -07:00
|
|
|
site: function(site) {
|
|
|
|
if (!(site instanceof Ci.nsIURI))
|
|
|
|
site = this._ioSvc.newURI("http://" + site, null, null);
|
|
|
|
return new Preferences({ branch: this._prefBranch, site: site });
|
|
|
|
},
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* Preferences Service
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
get _prefSvc() {
|
2010-06-16 15:11:40 -07:00
|
|
|
let prefSvc = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefService);
|
|
|
|
if (this._defaultBranch) {
|
|
|
|
prefSvc = prefSvc.getDefaultBranch(this._prefBranch);
|
|
|
|
} else {
|
2012-01-18 02:23:28 -08:00
|
|
|
prefSvc = prefSvc.getBranch(this._prefBranch);
|
2010-06-16 15:11:40 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
this.__defineGetter__("_prefSvc", function() prefSvc);
|
|
|
|
return this._prefSvc;
|
2009-05-02 16:20:08 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2008-11-03 14:40:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
2009-02-19 07:50:04 -08:00
|
|
|
|
2009-03-06 17:58:22 -08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* with which they added it. That means we have to identify the observers
|
|
|
|
* in this object by their complete pref name, not just their name relative to
|
|
|
|
* the root branch of the Preferences object with which they were created.
|
|
|
|
*/
|
|
|
|
let observers = [];
|
|
|
|
|
|
|
|
function PrefObserver(prefName, callback, thisObject) {
|
|
|
|
this.prefName = prefName;
|
|
|
|
this.callback = callback;
|
|
|
|
this.thisObject = thisObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefObserver.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
// The pref service only observes whole branches, but we only observe
|
|
|
|
// individual preferences, so we check here that the pref that changed
|
|
|
|
// is the exact one we're observing (and not some sub-pref on the branch).
|
2010-09-07 09:44:01 -07:00
|
|
|
if (data.indexOf(this.prefName) != 0)
|
2009-03-06 17:58:22 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (typeof this.callback == "function") {
|
2010-09-07 09:44:01 -07:00
|
|
|
let prefValue = Preferences.get(data);
|
2009-03-06 17:58:22 -08:00
|
|
|
|
|
|
|
if (this.thisObject)
|
|
|
|
this.callback.call(this.thisObject, prefValue);
|
|
|
|
else
|
|
|
|
this.callback(prefValue);
|
|
|
|
}
|
|
|
|
else // typeof this.callback == "object" (nsIObserver)
|
|
|
|
this.callback.observe(subject, topic, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-19 07:50:04 -08:00
|
|
|
function isObject(val) {
|
2009-03-06 17:58:22 -08:00
|
|
|
// We can't check for |val.constructor == Object| here, since the value
|
|
|
|
// might be from a different context whose Object constructor is not the same
|
|
|
|
// as ours, so instead we match based on the name of the constructor.
|
|
|
|
return (typeof val != "undefined" && val != null && typeof val == "object" &&
|
|
|
|
val.constructor.name == "Object");
|
2009-02-19 07:50:04 -08:00
|
|
|
}
|