bug 1034301 - Guard against bad listener behavior. r=kanru

This commit is contained in:
Ghislain 'Aus' Lacroix 2014-07-17 05:26:25 -07:00
parent a673af86e3
commit 87e597b7d1

View File

@ -453,6 +453,7 @@ WifiGeoPositionProvider.prototype = {
}
}
let l = this.listener;
let useCached = isCachedRequestMoreAccurateThanServerRequest(data.cellTowers,
data.wifiAccessPoints);
@ -460,38 +461,40 @@ WifiGeoPositionProvider.prototype = {
if (useCached) {
gCachedRequest.location.timestamp = Date.now();
this.listener.update(gCachedRequest.location);
this.notifyListener(l.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(l.locationUpdatePending);
try {
xhr.open("POST", url, true);
} catch (e) {
listener.notifyError(POSITION_UNAVAILABLE);
this.notifyListener(l.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(l.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(l.notifyError,
[POSITION_UNAVAILABLE]);
return;
}
@ -499,14 +502,23 @@ WifiGeoPositionProvider.prototype = {
xhr.response.location.lng,
xhr.response.accuracy);
listener.update(newLocation);
this.notifyListener(l.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]);