Bug 1034301 - Guard against bad listener behavior. r=kanru, a=2.0+
authorGhislain 'Aus' Lacroix <glacroix@mozilla.com>
Thu, 17 Jul 2014 05:35:33 -0700
changeset 209190 0f29e8a22f67ab20f5a2c726c626a7e1819f6cf1
parent 209189 7d1d5ae5d8a6f7d66aff54bf144d7a43565ed239
child 209191 4e93dbd9a87820df41b4795ca06d7671892ffac7
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskanru, 2
bugs1034301
milestone32.0a2
Bug 1034301 - Guard against bad listener behavior. r=kanru, a=2.0+
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]);