Bug 1319150 - allow function-style observers in Services shim; r?jdescottes draft
authorTom Tromey <tom@tromey.com>
Mon, 21 Nov 2016 11:51:06 -0700
changeset 442113 d5b3d84eef6ea299515a0b70d96a5a3b00c93d26
parent 442112 0273c38ee88e64092aaad696de6fc15f214e11fe
child 537699 6e31fc6e0882664fd19d53903f62990ed1e3c252
push id36585
push userbmo:ttromey@mozilla.com
push dateMon, 21 Nov 2016 18:52:09 +0000
reviewersjdescottes
bugs1319150
milestone53.0a1
Bug 1319150 - allow function-style observers in Services shim; r?jdescottes MozReview-Commit-ID: Zq687zNvyI
devtools/client/shared/shim/Services.js
devtools/client/shared/shim/test/test_service_prefs.html
--- a/devtools/client/shared/shim/Services.js
+++ b/devtools/client/shared/shim/Services.js
@@ -310,18 +310,24 @@ PrefBranch.prototype = {
   _notify: function (relativeName) {
     for (let domain in this._observers) {
       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);
+            if ("observe" in observer) {
+              observer.observe(this, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
+                               relativeName);
+            } else {
+              // Function-style observer -- these aren't mentioned in
+              // the IDL, but they're accepted and devtools uses them.
+              observer(this, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, relativeName);
+            }
           } catch (e) {
             console.error(e);
           }
         }
       }
     }
 
     if (this._parent) {
--- a/devtools/client/shared/shim/test/test_service_prefs.html
+++ b/devtools/client/shared/shim/test/test_service_prefs.html
@@ -220,16 +220,24 @@ function do_tests() {
   isDeeply(notifications, {
     "someotherstring": true
   }, "pref worked");
 
   // Regression test for bug 1296427.
   pref("devtools.hud.loglimit", 1000);
   pref("devtools.hud.loglimit.network", 1000);
 
+  // Regression test for bug 1319150.
+  let seen = false;
+  let fnObserver = () => { seen = true; };
+  branch0.addObserver("devtools.branch1.somestring", fnObserver, false);
+  Services.prefs.setCharPref("devtools.branch1.somestring", "common merganser");
+  ok(seen, "function-style observer was called");
+  branch0.removeObserver("devtools.branch1.somestring", fnObserver);
+
   // Clean up.
   localStorage.clear();
 
   SimpleTest.finish();
 }
 
 SimpleTest.waitForExplicitFinish();
 SpecialPowers.pushPrefEnv(