2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-08-10 00:02:39 +04:00
|
|
|
|
2015-01-15 07:28:27 +03:00
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2009-04-14 20:10:20 +04:00
|
|
|
|
2018-05-26 03:02:29 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["XMLHttpRequest"]);
|
2017-09-19 18:47:37 +03:00
|
|
|
|
2018-06-02 05:35:44 +03:00
|
|
|
// PositionError has no interface object, so we can't use that here.
|
|
|
|
const POSITION_UNAVAILABLE = 2;
|
2014-03-02 11:46:02 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gLoggingEnabled = false;
|
2014-03-02 11:46:02 +04:00
|
|
|
|
2014-06-09 09:58:37 +04:00
|
|
|
/*
|
|
|
|
The gLocationRequestTimeout controls how long we wait on receiving an update
|
|
|
|
from the Wifi subsystem. If this timer fires, we believe the Wifi scan has
|
|
|
|
had a problem and we no longer can use Wifi to position the user this time
|
|
|
|
around (we will continue to be hopeful that Wifi will recover).
|
2014-07-07 09:16:00 +04:00
|
|
|
|
2014-06-09 09:58:37 +04:00
|
|
|
This timeout value is also used when Wifi scanning is disabled (see
|
|
|
|
gWifiScanningEnabled). In this case, we use this timer to collect cell/ip
|
|
|
|
data and xhr it to the location server.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gLocationRequestTimeout = 5000;
|
2014-03-02 11:46:02 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gWifiScanningEnabled = true;
|
2014-03-10 20:10:15 +04:00
|
|
|
|
2009-04-14 20:10:20 +04:00
|
|
|
function LOG(aMsg) {
|
2014-03-02 11:46:02 +04:00
|
|
|
if (gLoggingEnabled) {
|
2010-08-31 20:15:52 +04:00
|
|
|
aMsg = "*** WIFI GEO: " + aMsg + "\n";
|
2019-01-18 21:56:46 +03:00
|
|
|
Services.console.logStringMessage(aMsg);
|
2009-08-15 03:12:09 +04:00
|
|
|
dump(aMsg);
|
|
|
|
}
|
2009-04-14 20:10:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-07 09:16:00 +04:00
|
|
|
function CachedRequest(loc, cellInfo, wifiList) {
|
|
|
|
this.location = loc;
|
|
|
|
|
|
|
|
let wifis = new Set();
|
|
|
|
if (wifiList) {
|
|
|
|
for (let i = 0; i < wifiList.length; i++) {
|
|
|
|
wifis.add(wifiList[i].macAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use only these values for equality
|
|
|
|
// (the JSON will contain additional values in future)
|
|
|
|
function makeCellKey(cell) {
|
|
|
|
return "" + cell.radio + ":" + cell.mobileCountryCode + ":" +
|
|
|
|
cell.mobileNetworkCode + ":" + cell.locationAreaCode + ":" +
|
|
|
|
cell.cellId;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cells = new Set();
|
|
|
|
if (cellInfo) {
|
|
|
|
for (let i = 0; i < cellInfo.length; i++) {
|
|
|
|
cells.add(makeCellKey(cellInfo[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hasCells = () => cells.size > 0;
|
|
|
|
|
|
|
|
this.hasWifis = () => wifis.size > 0;
|
|
|
|
|
|
|
|
// if fields match
|
|
|
|
this.isCellEqual = function(cellInfo) {
|
|
|
|
if (!this.hasCells()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let len1 = cells.size;
|
|
|
|
let len2 = cellInfo.length;
|
|
|
|
|
|
|
|
if (len1 != len2) {
|
|
|
|
LOG("cells not equal len");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < len2; i++) {
|
|
|
|
if (!cells.has(makeCellKey(cellInfo[i]))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// if 50% of the SSIDS match
|
|
|
|
this.isWifiApproxEqual = function(wifiList) {
|
|
|
|
if (!this.hasWifis()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if either list is a 50% subset of the other, they are equal
|
|
|
|
let common = 0;
|
|
|
|
for (let i = 0; i < wifiList.length; i++) {
|
|
|
|
if (wifis.has(wifiList[i].macAddress)) {
|
|
|
|
common++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let kPercentMatch = 0.5;
|
|
|
|
return common >= (Math.max(wifis.size, wifiList.length) * kPercentMatch);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isGeoip = function() {
|
|
|
|
return !this.hasCells() && !this.hasWifis();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isCellAndWifi = function() {
|
|
|
|
return this.hasCells() && this.hasWifis();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isCellOnly = function() {
|
|
|
|
return this.hasCells() && !this.hasWifis();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isWifiOnly = function() {
|
|
|
|
return this.hasWifis() && !this.hasCells();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gCachedRequest = null;
|
|
|
|
var gDebugCacheReasoning = ""; // for logging the caching logic
|
2014-07-07 09:16:00 +04:00
|
|
|
|
|
|
|
// This function serves two purposes:
|
|
|
|
// 1) do we have a cached request
|
|
|
|
// 2) is the cached request better than what newCell and newWifiList will obtain
|
|
|
|
// If the cached request exists, and we know it to have greater accuracy
|
|
|
|
// by the nature of its origin (wifi/cell/geoip), use its cached location.
|
|
|
|
//
|
|
|
|
// If there is more source info than the cached request had, return false
|
|
|
|
// In other cases, MLS is known to produce better/worse accuracy based on the
|
|
|
|
// inputs, so base the decision on that.
|
2019-01-18 21:56:46 +03:00
|
|
|
function isCachedRequestMoreAccurateThanServerRequest(newCell, newWifiList) {
|
2014-07-07 09:16:00 +04:00
|
|
|
gDebugCacheReasoning = "";
|
|
|
|
let isNetworkRequestCacheEnabled = true;
|
|
|
|
try {
|
|
|
|
// Mochitest needs this pref to simulate request failure
|
|
|
|
isNetworkRequestCacheEnabled = Services.prefs.getBoolPref("geo.wifi.debug.requestCache.enabled");
|
|
|
|
if (!isNetworkRequestCacheEnabled) {
|
|
|
|
gCachedRequest = null;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (!gCachedRequest || !isNetworkRequestCacheEnabled) {
|
|
|
|
gDebugCacheReasoning = "No cached data";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newCell && !newWifiList) {
|
|
|
|
gDebugCacheReasoning = "New req. is GeoIP.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newCell && newWifiList && (gCachedRequest.isCellOnly() || gCachedRequest.isWifiOnly())) {
|
|
|
|
gDebugCacheReasoning = "New req. is cell+wifi, cache only cell or wifi.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newCell && gCachedRequest.isWifiOnly()) {
|
|
|
|
// In order to know if a cell-only request should trump a wifi-only request
|
|
|
|
// need to know if wifi is low accuracy. >5km would be VERY low accuracy,
|
|
|
|
// it is worth trying the cell
|
|
|
|
var isHighAccuracyWifi = gCachedRequest.location.coords.accuracy < 5000;
|
|
|
|
gDebugCacheReasoning = "Req. is cell, cache is wifi, isHigh:" + isHighAccuracyWifi;
|
|
|
|
return isHighAccuracyWifi;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasEqualCells = false;
|
|
|
|
if (newCell) {
|
|
|
|
hasEqualCells = gCachedRequest.isCellEqual(newCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasEqualWifis = false;
|
|
|
|
if (newWifiList) {
|
|
|
|
hasEqualWifis = gCachedRequest.isWifiApproxEqual(newWifiList);
|
|
|
|
}
|
|
|
|
|
|
|
|
gDebugCacheReasoning = "EqualCells:" + hasEqualCells + " EqualWifis:" + hasEqualWifis;
|
|
|
|
|
|
|
|
if (gCachedRequest.isCellOnly()) {
|
|
|
|
gDebugCacheReasoning += ", Cell only.";
|
|
|
|
if (hasEqualCells) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (gCachedRequest.isWifiOnly() && hasEqualWifis) {
|
2019-01-18 21:56:46 +03:00
|
|
|
gDebugCacheReasoning += ", Wifi only.";
|
2014-07-07 09:16:00 +04:00
|
|
|
return true;
|
|
|
|
} else if (gCachedRequest.isCellAndWifi()) {
|
|
|
|
gDebugCacheReasoning += ", Cache has Cell+Wifi.";
|
|
|
|
if ((hasEqualCells && hasEqualWifis) ||
|
|
|
|
(!newWifiList && hasEqualCells) ||
|
2019-01-18 21:56:46 +03:00
|
|
|
(!newCell && hasEqualWifis)) {
|
2014-07-07 09:16:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:35:16 +03:00
|
|
|
function WifiGeoCoordsObject(lat, lon, acc) {
|
2011-08-10 00:02:39 +04:00
|
|
|
this.latitude = lat;
|
|
|
|
this.longitude = lon;
|
|
|
|
this.accuracy = acc;
|
2018-02-26 10:35:16 +03:00
|
|
|
|
|
|
|
// Neither GLS nor MLS return the following properties, so set them to NaN
|
|
|
|
// here. nsGeoPositionCoords will convert NaNs to null for optional properties
|
|
|
|
// of the JavaScript Coordinates object.
|
|
|
|
this.altitude = NaN;
|
|
|
|
this.altitudeAccuracy = NaN;
|
|
|
|
this.heading = NaN;
|
|
|
|
this.speed = NaN;
|
2011-08-10 00:02:39 +04:00
|
|
|
}
|
2009-04-14 20:10:20 +04:00
|
|
|
|
|
|
|
WifiGeoCoordsObject.prototype = {
|
2019-01-18 21:56:46 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsIDOMGeoPositionCoords]),
|
2009-04-14 20:10:20 +04:00
|
|
|
};
|
|
|
|
|
2011-08-10 00:02:39 +04:00
|
|
|
function WifiGeoPositionObject(lat, lng, acc) {
|
2018-02-26 10:35:16 +03:00
|
|
|
this.coords = new WifiGeoCoordsObject(lat, lng, acc);
|
2011-08-10 00:02:39 +04:00
|
|
|
this.address = null;
|
|
|
|
this.timestamp = Date.now();
|
|
|
|
}
|
2009-04-14 20:10:20 +04:00
|
|
|
|
|
|
|
WifiGeoPositionObject.prototype = {
|
2019-01-18 21:56:46 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsIDOMGeoPosition]),
|
2009-04-14 20:10:20 +04:00
|
|
|
};
|
|
|
|
|
2011-08-10 00:02:39 +04:00
|
|
|
function WifiGeoPositionProvider() {
|
2019-01-18 21:56:46 +03:00
|
|
|
gLoggingEnabled = Services.prefs.getBoolPref("geo.wifi.logging.enabled", false);
|
|
|
|
gLocationRequestTimeout = Services.prefs.getIntPref("geo.wifi.timeToWaitBeforeSending", 5000);
|
|
|
|
gWifiScanningEnabled = Services.prefs.getBoolPref("geo.wifi.scan", true);
|
2013-01-07 09:57:00 +04:00
|
|
|
|
2011-12-01 15:02:51 +04:00
|
|
|
this.wifiService = null;
|
2014-06-09 09:58:37 +04:00
|
|
|
this.timer = null;
|
2011-12-01 15:02:51 +04:00
|
|
|
this.started = false;
|
2011-08-10 00:02:39 +04:00
|
|
|
}
|
2010-02-11 22:59:55 +03:00
|
|
|
|
2011-08-10 00:02:39 +04:00
|
|
|
WifiGeoPositionProvider.prototype = {
|
|
|
|
classID: Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsIGeolocationProvider,
|
|
|
|
Ci.nsIWifiListener,
|
|
|
|
Ci.nsITimerCallback,
|
|
|
|
Ci.nsIObserver]),
|
2014-03-22 11:42:55 +04:00
|
|
|
listener: null,
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
resetTimer() {
|
2014-06-09 09:58:37 +04:00
|
|
|
if (this.timer) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
// wifi thread triggers WifiGeoPositionProvider to proceed, with no wifi, do manual timeout
|
|
|
|
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this.timer.initWithCallback(this,
|
|
|
|
gLocationRequestTimeout,
|
|
|
|
this.timer.TYPE_REPEATING_SLACK);
|
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
startup() {
|
2011-08-10 00:02:39 +04:00
|
|
|
if (this.started)
|
|
|
|
return;
|
2014-03-25 08:58:18 +04:00
|
|
|
|
2011-08-10 00:02:39 +04:00
|
|
|
this.started = true;
|
2014-03-25 08:58:18 +04:00
|
|
|
|
2014-06-07 02:28:56 +04:00
|
|
|
if (gWifiScanningEnabled && Cc["@mozilla.org/wifi/monitor;1"]) {
|
2014-06-09 09:52:53 +04:00
|
|
|
if (this.wifiService) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
}
|
|
|
|
this.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
|
2011-08-10 00:02:39 +04:00
|
|
|
this.wifiService.startWatching(this);
|
2010-02-11 22:59:55 +03:00
|
|
|
}
|
2014-06-09 09:56:29 +04:00
|
|
|
|
2014-06-09 09:58:37 +04:00
|
|
|
this.resetTimer();
|
2014-03-02 11:46:02 +04:00
|
|
|
LOG("startup called.");
|
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
watch(c) {
|
2014-03-22 11:42:55 +04:00
|
|
|
this.listener = c;
|
2011-08-10 00:02:39 +04:00
|
|
|
},
|
2010-02-11 22:59:55 +03:00
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
shutdown() {
|
2011-08-10 00:02:39 +04:00
|
|
|
LOG("shutdown called");
|
2019-01-18 21:56:46 +03:00
|
|
|
if (!this.started) {
|
2014-03-02 11:46:02 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 09:16:00 +04:00
|
|
|
// Without clearing this, we could end up using the cache almost indefinitely
|
|
|
|
// TODO: add logic for cache lifespan, for now just be safe and clear it
|
|
|
|
gCachedRequest = null;
|
|
|
|
|
2014-06-09 09:58:37 +04:00
|
|
|
if (this.timer) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer = null;
|
2014-03-02 11:46:02 +04:00
|
|
|
}
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
if (this.wifiService) {
|
2011-08-10 00:02:39 +04:00
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
this.wifiService = null;
|
|
|
|
}
|
2014-03-25 08:58:18 +04:00
|
|
|
|
2014-03-22 11:42:55 +04:00
|
|
|
this.listener = null;
|
2011-08-10 00:02:39 +04:00
|
|
|
this.started = false;
|
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
setHighAccuracy(enable) {
|
2012-03-21 21:52:35 +04:00
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
onChange(accessPoints) {
|
2014-06-09 09:58:37 +04:00
|
|
|
// we got some wifi data, rearm the timer.
|
|
|
|
this.resetTimer();
|
|
|
|
|
2013-06-13 06:20:07 +04:00
|
|
|
function isPublic(ap) {
|
2019-01-18 21:56:46 +03:00
|
|
|
let mask = "_nomap";
|
2014-03-02 11:46:02 +04:00
|
|
|
let result = ap.ssid.indexOf(mask, ap.ssid.length - mask.length);
|
|
|
|
if (result != -1) {
|
|
|
|
LOG("Filtering out " + ap.ssid + " " + result);
|
2016-02-11 08:23:00 +03:00
|
|
|
return false;
|
2014-03-02 11:46:02 +04:00
|
|
|
}
|
2016-02-11 08:23:00 +03:00
|
|
|
return true;
|
2019-01-18 21:56:46 +03:00
|
|
|
}
|
2009-05-07 21:52:39 +04:00
|
|
|
|
2013-06-13 06:20:07 +04:00
|
|
|
function sort(a, b) {
|
|
|
|
return b.signal - a.signal;
|
2019-01-18 21:56:46 +03:00
|
|
|
}
|
2013-06-14 07:20:15 +04:00
|
|
|
|
2011-08-10 00:02:39 +04:00
|
|
|
function encode(ap) {
|
2019-01-18 21:56:46 +03:00
|
|
|
return { "macAddress": ap.mac, "signalStrength": ap.signal };
|
|
|
|
}
|
2011-08-10 00:02:39 +04:00
|
|
|
|
2014-06-09 09:52:53 +04:00
|
|
|
let wifiData = null;
|
2011-08-10 00:02:39 +04:00
|
|
|
if (accessPoints) {
|
2014-06-09 09:52:53 +04:00
|
|
|
wifiData = accessPoints.filter(isPublic).sort(sort).map(encode);
|
2011-08-10 00:02:39 +04:00
|
|
|
}
|
2014-06-09 09:52:53 +04:00
|
|
|
this.sendLocationRequest(wifiData);
|
2014-03-02 11:46:02 +04:00
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
onError(code) {
|
2014-03-02 11:46:02 +04:00
|
|
|
LOG("wifi error: " + code);
|
2014-06-09 09:52:53 +04:00
|
|
|
this.sendLocationRequest(null);
|
2014-03-02 11:46:02 +04:00
|
|
|
},
|
2009-07-11 04:03:48 +04:00
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
notify(timer) {
|
2014-06-09 09:58:37 +04:00
|
|
|
this.sendLocationRequest(null);
|
2014-06-09 09:52:53 +04:00
|
|
|
},
|
|
|
|
|
2019-01-18 21:56:46 +03:00
|
|
|
sendLocationRequest(wifiData) {
|
2014-12-17 21:44:00 +03:00
|
|
|
let data = { cellTowers: undefined, wifiAccessPoints: undefined };
|
|
|
|
if (wifiData && wifiData.length >= 2) {
|
2014-07-07 09:16:00 +04:00
|
|
|
data.wifiAccessPoints = wifiData;
|
|
|
|
}
|
|
|
|
|
|
|
|
let useCached = isCachedRequestMoreAccurateThanServerRequest(data.cellTowers,
|
|
|
|
data.wifiAccessPoints);
|
|
|
|
|
|
|
|
LOG("Use request cache:" + useCached + " reason:" + gDebugCacheReasoning);
|
|
|
|
|
|
|
|
if (useCached) {
|
|
|
|
gCachedRequest.location.timestamp = Date.now();
|
2018-07-31 16:23:24 +03:00
|
|
|
if (this.listener) {
|
|
|
|
this.listener.update(gCachedRequest.location);
|
|
|
|
}
|
2014-07-07 09:16:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on, do a network geolocation request //
|
2014-03-02 11:46:02 +04:00
|
|
|
let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
|
2014-06-26 06:23:48 +04:00
|
|
|
LOG("Sending request");
|
2009-04-14 20:10:20 +04:00
|
|
|
|
2017-09-19 18:47:37 +03:00
|
|
|
let xhr = new XMLHttpRequest();
|
2013-11-04 23:52:53 +04:00
|
|
|
try {
|
2014-03-02 11:46:02 +04:00
|
|
|
xhr.open("POST", url, true);
|
2016-09-04 01:45:08 +03:00
|
|
|
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
|
2013-11-04 23:52:53 +04:00
|
|
|
} catch (e) {
|
2018-03-10 09:20:50 +03:00
|
|
|
notifyPositionUnavailable(this.listener);
|
2014-03-02 11:46:02 +04:00
|
|
|
return;
|
2013-11-04 23:52:53 +04:00
|
|
|
}
|
2013-06-13 06:20:07 +04:00
|
|
|
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
xhr.responseType = "json";
|
2011-08-10 00:02:39 +04:00
|
|
|
xhr.mozBackgroundRequest = true;
|
2015-08-02 15:45:00 +03:00
|
|
|
xhr.timeout = Services.prefs.getIntPref("geo.wifi.xhr.timeout");
|
2017-04-27 01:25:45 +03:00
|
|
|
xhr.ontimeout = () => {
|
2019-01-18 21:56:46 +03:00
|
|
|
LOG("Location request XHR timed out.");
|
2018-03-10 09:20:50 +03:00
|
|
|
notifyPositionUnavailable(this.listener);
|
2017-04-27 01:25:45 +03:00
|
|
|
};
|
|
|
|
xhr.onerror = () => {
|
2018-03-10 09:20:50 +03:00
|
|
|
notifyPositionUnavailable(this.listener);
|
2017-04-27 01:25:45 +03:00
|
|
|
};
|
|
|
|
xhr.onload = () => {
|
2019-01-18 21:56:46 +03:00
|
|
|
LOG("server returned status: " + xhr.status + " --> " + JSON.stringify(xhr.response));
|
2014-03-02 11:46:02 +04:00
|
|
|
if ((xhr.channel instanceof Ci.nsIHttpChannel && xhr.status != 200) ||
|
|
|
|
!xhr.response || !xhr.response.location) {
|
2018-03-10 09:20:50 +03:00
|
|
|
notifyPositionUnavailable(this.listener);
|
2014-03-02 11:46:02 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-06-13 06:20:07 +04:00
|
|
|
|
2014-03-02 11:46:02 +04:00
|
|
|
let newLocation = new WifiGeoPositionObject(xhr.response.location.lat,
|
|
|
|
xhr.response.location.lng,
|
|
|
|
xhr.response.accuracy);
|
|
|
|
|
2018-03-10 09:20:50 +03:00
|
|
|
if (this.listener) {
|
|
|
|
this.listener.update(newLocation);
|
|
|
|
}
|
2014-07-07 09:16:00 +04:00
|
|
|
gCachedRequest = new CachedRequest(newLocation, data.cellTowers, data.wifiAccessPoints);
|
2017-04-27 01:25:45 +03:00
|
|
|
};
|
2013-06-13 06:20:07 +04:00
|
|
|
|
2014-07-07 09:16:00 +04:00
|
|
|
var requestData = JSON.stringify(data);
|
|
|
|
LOG("sending " + requestData);
|
|
|
|
xhr.send(requestData);
|
2018-03-10 09:20:50 +03:00
|
|
|
|
|
|
|
function notifyPositionUnavailable(listener) {
|
|
|
|
if (listener) {
|
|
|
|
listener.notifyError(POSITION_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:56:46 +03:00
|
|
|
},
|
2009-04-14 20:10:20 +04:00
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);
|