Bug 1294190 - allow observation of a preference in shim prefs; r?gregtatum
MozReview-Commit-ID: 1Rzs8F0yJtt
--- a/devtools/client/shared/shim/Services.js
+++ b/devtools/client/shared/shim/Services.js
@@ -244,19 +244,16 @@ PrefBranch.prototype = {
/** @see nsIPrefBranch.prefHasUserValue */
prefHasUserValue: function (prefName) {
let thePref = this._findPref(prefName);
return thePref.hasUserValue;
},
/** @see nsIPrefBranch.addObserver */
addObserver: function (domain, observer, holdWeak) {
- if (domain !== "" && !domain.endsWith(".")) {
- throw new Error("invalid domain to addObserver: " + domain);
- }
if (holdWeak) {
throw new Error("shim prefs only supports strong observers");
}
if (!(domain in this._observers)) {
this._observers[domain] = [];
}
this._observers[domain].push(observer);
@@ -320,17 +317,18 @@ PrefBranch.prototype = {
* changed. This will also notify the parent branch for further
* reporting.
*
* @param {String} relativeName the name of the updated pref,
* relative to this branch
*/
_notify: function (relativeName) {
for (let domain in this._observers) {
- if (relativeName.startsWith(domain)) {
+ if (relativeName === domain || domain === "" ||
+ (domain.endsWith(".") && relativeName.startsWith(domain))) {
// Allow mutation while walking.
let localList = this._observers[domain].slice();
for (let observer of localList) {
try {
observer.observe(this, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
relativeName);
} catch (e) {
console.error(e);
--- a/devtools/client/shared/shim/test/test_service_prefs.html
+++ b/devtools/client/shared/shim/test/test_service_prefs.html
@@ -175,24 +175,17 @@ function do_tests() {
let notifications = {};
let clearNotificationList = () => { notifications = {}; }
let observer = {
observe: function (subject, topic, data) {
notifications[data] = true;
}
};
- try {
- threw = false;
- branch0.addObserver("devtools.branch1", null, null);
- } catch (e) {
- threw = true;
- }
- ok(threw, "invalid branch name to addObserver");
-
+ branch0.addObserver("devtools.branch1", null, null);
branch0.addObserver("devtools.branch1.", observer, false);
branch1.addObserver("", observer, false);
Services.prefs.setCharPref("devtools.branch1.somestring", "elf owl");
isDeeply(notifications, {
"devtools.branch1.somestring": true,
"somestring": true
}, "notifications sent to two listeners");
@@ -203,16 +196,25 @@ function do_tests() {
clearNotificationList();
branch0.removeObserver("devtools.branch1.", observer);
Services.prefs.setCharPref("devtools.branch1.somestring", "tapir");
isDeeply(notifications, {
"somestring": true
}, "removeObserver worked");
+ clearNotificationList();
+ branch0.addObserver("devtools.branch1.somestring", observer, false);
+ Services.prefs.setCharPref("devtools.branch1.somestring", "northern shoveler");
+ isDeeply(notifications, {
+ "devtools.branch1.somestring": true,
+ "somestring": true
+ }, "notifications sent to two listeners");
+ branch0.removeObserver("devtools.branch1.somestring", observer);
+
// Make sure we update if the pref change comes from somewhere else.
clearNotificationList();
pref("devtools.branch1.someotherstring", "lazuli bunting");
isDeeply(notifications, {
"someotherstring": true
}, "pref worked");