bug 1034301 - Guard against bad listener behavior. r=kanru
authorGhislain 'Aus' Lacroix <glacroix@mozilla.com>
Thu, 17 Jul 2014 05:35:33 -0700
changeset 216556 de58560fbb42877f5e995823cb4fe796d054df2a
parent 216555 2419a4ce8088ef8022bfd325285d08187969f994
child 216557 a0364999f06451dbac2d4f5c2cdbbc195306273d
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskanru
bugs1034301
milestone33.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 1034301 - Guard against bad listener behavior. r=kanru
dom/system/NetworkGeolocationProvider.js
--- a/dom/system/NetworkGeolocationProvider.js
+++ b/dom/system/NetworkGeolocationProvider.js
@@ -455,58 +455,69 @@ WifiGeoPositionProvider.prototype = {
 
     let useCached = isCachedRequestMoreAccurateThanServerRequest(data.cellTowers,
                                                                  data.wifiAccessPoints);
 
     LOG("Use request cache:" + useCached + " reason:" + gDebugCacheReasoning);
 
     if (useCached) {
       gCachedRequest.location.timestamp = Date.now();
-      this.listener.update(gCachedRequest.location);
+      this.notifyListener("update", [gCachedRequest.location]);
       return;
     }
 
     // From here on, do a network geolocation request //
     let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
-    let listener = this.listener;
     LOG("Sending request: " + url + "\n");
 
     let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                         .createInstance(Ci.nsIXMLHttpRequest);
 
-    listener.locationUpdatePending();
+    this.notifyListener("locationUpdatePending");
 
     try {
       xhr.open("POST", url, true);
     } catch (e) {
-      listener.notifyError(POSITION_UNAVAILABLE);
+      this.notifyListener("notifyError",
+                          [POSITION_UNAVAILABLE]);
       return;
     }
     xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
     xhr.responseType = "json";
     xhr.mozBackgroundRequest = true;
     xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
-    xhr.onerror = function() {
-      listener.notifyError(POSITION_UNAVAILABLE);
-    };
-    xhr.onload = function() {
+    xhr.onerror = (function() {
+      this.notifyListener("notifyError",
+                          [POSITION_UNAVAILABLE]);
+    }).bind(this);
+    xhr.onload = (function() {
       LOG("gls returned status: " + xhr.status + " --> " +  JSON.stringify(xhr.response));
       if ((xhr.channel instanceof Ci.nsIHttpChannel && xhr.status != 200) ||
           !xhr.response || !xhr.response.location) {
-        listener.notifyError(POSITION_UNAVAILABLE);
+        this.notifyListener("notifyError",
+                            [POSITION_UNAVAILABLE]);
         return;
       }
 
       let newLocation = new WifiGeoPositionObject(xhr.response.location.lat,
                                                   xhr.response.location.lng,
                                                   xhr.response.accuracy);
 
-      listener.update(newLocation);
+      this.notifyListener("update", [newLocation]);
       gCachedRequest = new CachedRequest(newLocation, data.cellTowers, data.wifiAccessPoints);
-    };
+    }).bind(this);
 
     var requestData = JSON.stringify(data);
     LOG("sending " + requestData);
     xhr.send(requestData);
   },
+
+  notifyListener: function(listenerFunc, args) {
+    args = args || [];
+    try {
+      this.listener[listenerFunc].apply(this.listener, args);
+    } catch(e) {
+      Cu.reportError(e);
+    }
+  }
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);