Bug 1254194: Add XPCOMUtils.defineLazyPreferenceGetter. r?billm
MozReview-Commit-ID: JBVelZ0Xg2H
--- a/js/xpconnect/loader/XPCOMUtils.jsm
+++ b/js/xpconnect/loader/XPCOMUtils.jsm
@@ -279,16 +279,73 @@ this.XPCOMUtils = {
Cu.reportError("Failed to load module " + aResource + ".");
throw ex;
}
return temp[aSymbol || aName];
});
},
/**
+ * Defines a getter on a specified object for preference value. The
+ * preference is read the first time that the property is accessed,
+ * and is thereafter kept up-to-date using a preference observer.
+ *
+ * @param aObject
+ * The object to define the lazy getter on.
+ * @param aName
+ * The name of the getter property to define on aObject.
+ * @param aPreference
+ * The name of the preference to read.
+ * @param aDefaultValue
+ * The default value to use, if the preference is not defined.
+ */
+ defineLazyPreferenceGetter: function XPCU_defineLazyPreferenceGetter(
+ aObject, aName, aPreference, aDefaultValue = null)
+ {
+ // Note: We need to keep a reference to this observer alive as long
+ // as aObject is alive. This means that all of our getters need to
+ // explicitly close over the variable that holds the object, and we
+ // cannot define a value in place of a getter after we read the
+ // preference.
+ let observer = {
+ QueryInterface: this.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
+
+ value: undefined,
+
+ observe(subject, topic, data) {
+ if (data == aPreference) {
+ this.value = undefined;
+ }
+ },
+ }
+
+ let defineGetter = get => {
+ Object.defineProperty(aObject, aName, {
+ configurable: true,
+ enumerable: true,
+ get,
+ });
+ };
+
+ function lazyGetter() {
+ if (observer.value === undefined) {
+ observer.value = Preferences.get(aPreference, aDefaultValue);
+ }
+ return observer.value;
+ }
+
+ defineGetter(() => {
+ Services.prefs.addObserver(aPreference, observer, true);
+
+ defineGetter(lazyGetter);
+ return lazyGetter();
+ });
+ },
+
+ /**
* Convenience access to category manager
*/
get categoryManager() {
return Components.classes["@mozilla.org/categorymanager;1"]
.getService(Ci.nsICategoryManager);
},
/**
@@ -377,16 +434,21 @@ this.XPCOMUtils = {
Object.defineProperty(aObj, aName, {
value: aValue,
enumerable: true,
writable: false
});
},
};
+XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
+ "resource://gre/modules/Preferences.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+ "resource://gre/modules/Services.jsm");
+
/**
* Helper for XPCOMUtils.generateQI to avoid leaks - see bug 381651#c1
*/
function makeQI(interfaceNames) {
return function XPCOMUtils_QueryInterface(iid) {
if (iid.equals(Ci.nsISupports))
return this;
if (iid.equals(Ci.nsIClassInfo) && "classInfo" in this)
--- a/js/xpconnect/tests/unit/test_xpcomutils.js
+++ b/js/xpconnect/tests/unit/test_xpcomutils.js
@@ -3,16 +3,17 @@
* 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/. */
/**
* This file tests the methods on XPCOMUtils.jsm.
*/
+Components.utils.import("resource://gre/modules/Preferences.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Cc = Components.classes;
const Ci = Components.interfaces;
////////////////////////////////////////////////////////////////////////////////
//// Tests
@@ -111,16 +112,54 @@ add_test(function test_defineLazyService
for (let prop in obj.service)
do_check_true(prop in service);
for (let prop in service)
do_check_true(prop in obj.service);
run_next_test();
});
+add_test(function test_defineLazyPreferenceGetter()
+{
+ const PREF = "xpcomutils.test.pref";
+
+ let obj = {};
+ XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
+
+
+ equal(obj.pref, "defaultValue", "Should return the default value before pref is set");
+
+ Preferences.set(PREF, "currentValue");
+
+
+ do_print("Create second getter on new object");
+
+ obj = {};
+ XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
+
+
+ equal(obj.pref, "currentValue", "Should return the current value on initial read when pref is already set");
+
+ Preferences.set(PREF, "newValue");
+
+ equal(obj.pref, "newValue", "Should return new value after preference change");
+
+ Preferences.set(PREF, "currentValue");
+
+ equal(obj.pref, "currentValue", "Should return new value after second preference change");
+
+
+ Preferences.reset(PREF);
+
+ equal(obj.pref, "defaultValue", "Should return default value after pref is reset");
+
+ run_next_test();
+});
+
+
add_test(function test_categoryRegistration()
{
const CATEGORY_NAME = "test-cat";
const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}");
// Create a fake app entry for our category registration apps filter.
let tmp = {};