2012-05-21 11:12:37 +00: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-09 20:02:39 +00:00
|
|
|
|
2015-01-15 04:28:27 +00:00
|
|
|
"use strict";
|
|
|
|
|
2009-04-14 16:10:20 +00:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2011-08-09 20:02:39 +00:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2009-04-14 16:10:20 +00:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
2014-07-31 20:29:14 +00:00
|
|
|
const Cu = Components.utils;
|
2009-04-14 16:10:20 +00:00
|
|
|
|
2014-03-02 07:46:02 +00:00
|
|
|
const POSITION_UNAVAILABLE = Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE;
|
2014-06-09 05:56:29 +00:00
|
|
|
const SETTINGS_DEBUG_ENABLED = "geolocation.debugging.enabled";
|
|
|
|
const SETTINGS_CHANGED_TOPIC = "mozsettings-changed";
|
|
|
|
const SETTINGS_WIFI_ENABLED = "wifi.enabled";
|
2014-03-02 07:46:02 +00:00
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
let gLoggingEnabled = false;
|
2014-03-02 07:46:02 +00:00
|
|
|
|
2014-06-09 05:58:37 +00: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 05:16:00 +00:00
|
|
|
|
2014-06-09 05:58:37 +00: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.
|
|
|
|
*/
|
|
|
|
|
2014-06-09 05:52:53 +00:00
|
|
|
let gLocationRequestTimeout = 5000;
|
2014-03-02 07:46:02 +00:00
|
|
|
|
|
|
|
let gWifiScanningEnabled = true;
|
2014-03-10 16:10:15 +00:00
|
|
|
let gCellScanningEnabled = false;
|
|
|
|
|
2009-04-14 16:10:20 +00:00
|
|
|
function LOG(aMsg) {
|
2014-03-02 07:46:02 +00:00
|
|
|
if (gLoggingEnabled) {
|
2010-08-31 16:15:52 +00:00
|
|
|
aMsg = "*** WIFI GEO: " + aMsg + "\n";
|
2009-08-14 23:12:09 +00:00
|
|
|
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
|
|
|
|
dump(aMsg);
|
|
|
|
}
|
2009-04-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2014-07-07 05:16:00 +00: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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let gCachedRequest = null;
|
|
|
|
let gDebugCacheReasoning = ""; // for logging the caching logic
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
function isCachedRequestMoreAccurateThanServerRequest(newCell, newWifiList)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
gDebugCacheReasoning +=", Wifi only."
|
|
|
|
return true;
|
|
|
|
} else if (gCachedRequest.isCellAndWifi()) {
|
|
|
|
gDebugCacheReasoning += ", Cache has Cell+Wifi.";
|
|
|
|
if ((hasEqualCells && hasEqualWifis) ||
|
|
|
|
(!newWifiList && hasEqualCells) ||
|
|
|
|
(!newCell && hasEqualWifis))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-14 23:12:09 +00:00
|
|
|
function WifiGeoCoordsObject(lat, lon, acc, alt, altacc) {
|
2011-08-09 20:02:39 +00:00
|
|
|
this.latitude = lat;
|
|
|
|
this.longitude = lon;
|
|
|
|
this.accuracy = acc;
|
|
|
|
this.altitude = alt;
|
|
|
|
this.altitudeAccuracy = altacc;
|
|
|
|
}
|
2009-04-14 16:10:20 +00:00
|
|
|
|
|
|
|
WifiGeoCoordsObject.prototype = {
|
2014-03-25 03:39:23 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionCoords])
|
2009-04-14 16:10:20 +00:00
|
|
|
};
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
function WifiGeoPositionObject(lat, lng, acc) {
|
|
|
|
this.coords = new WifiGeoCoordsObject(lat, lng, acc, 0, 0);
|
|
|
|
this.address = null;
|
|
|
|
this.timestamp = Date.now();
|
|
|
|
}
|
2009-04-14 16:10:20 +00:00
|
|
|
|
|
|
|
WifiGeoPositionObject.prototype = {
|
2014-03-25 03:39:23 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPosition])
|
2009-04-14 16:10:20 +00:00
|
|
|
};
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
function WifiGeoPositionProvider() {
|
|
|
|
try {
|
|
|
|
gLoggingEnabled = Services.prefs.getBoolPref("geo.wifi.logging.enabled");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
try {
|
2014-06-09 05:52:53 +00:00
|
|
|
gLocationRequestTimeout = Services.prefs.getIntPref("geo.wifi.timeToWaitBeforeSending");
|
2011-08-09 20:02:39 +00:00
|
|
|
} catch (e) {}
|
|
|
|
|
2013-01-07 05:57:00 +00:00
|
|
|
try {
|
2014-03-02 07:46:02 +00:00
|
|
|
gWifiScanningEnabled = Services.prefs.getBoolPref("geo.wifi.scan");
|
2013-01-07 05:57:00 +00:00
|
|
|
} catch (e) {}
|
|
|
|
|
2014-03-10 16:10:15 +00:00
|
|
|
try {
|
|
|
|
gCellScanningEnabled = Services.prefs.getBoolPref("geo.cell.scan");
|
|
|
|
} catch (e) {}
|
|
|
|
|
2011-12-01 11:02:51 +00:00
|
|
|
this.wifiService = null;
|
2014-06-09 05:58:37 +00:00
|
|
|
this.timer = null;
|
2011-12-01 11:02:51 +00:00
|
|
|
this.started = false;
|
2011-08-09 20:02:39 +00:00
|
|
|
}
|
2010-02-11 19:59:55 +00:00
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
WifiGeoPositionProvider.prototype = {
|
|
|
|
classID: Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
|
|
|
|
Ci.nsIWifiListener,
|
2014-03-25 04:58:18 +00:00
|
|
|
Ci.nsITimerCallback,
|
|
|
|
Ci.nsIObserver]),
|
2014-03-22 07:42:55 +00:00
|
|
|
listener: null,
|
|
|
|
|
2014-03-25 04:58:18 +00:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2014-06-09 05:56:29 +00:00
|
|
|
if (aTopic != SETTINGS_CHANGED_TOPIC) {
|
2014-03-25 04:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-09-16 17:15:16 +00:00
|
|
|
if ("wrappedJSObject" in aSubject) {
|
|
|
|
aSubject = aSubject.wrappedJSObject;
|
|
|
|
}
|
|
|
|
if (aSubject.key == SETTINGS_DEBUG_ENABLED) {
|
|
|
|
gLoggingEnabled = aSubject.value;
|
|
|
|
} else if (aSubject.key == SETTINGS_WIFI_ENABLED) {
|
|
|
|
gWifiScanningEnabled = aSubject.value;
|
2014-03-25 04:58:18 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-09 05:58:37 +00:00
|
|
|
resetTimer: function() {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
startup: function() {
|
|
|
|
if (this.started)
|
|
|
|
return;
|
2014-03-25 04:58:18 +00:00
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
this.started = true;
|
2014-06-09 05:56:29 +00:00
|
|
|
let self = this;
|
2014-04-23 21:16:24 +00:00
|
|
|
let settingsCallback = {
|
|
|
|
handle: function(name, result) {
|
2014-07-09 13:14:00 +00:00
|
|
|
// Stop the B2G UI setting from overriding the js prefs setting, and turning off logging
|
|
|
|
// If gLoggingEnabled is already on during startup, that means it was set in js prefs.
|
|
|
|
if (name == SETTINGS_DEBUG_ENABLED && !gLoggingEnabled) {
|
2014-06-09 05:56:29 +00:00
|
|
|
gLoggingEnabled = result;
|
|
|
|
} else if (name == SETTINGS_WIFI_ENABLED) {
|
|
|
|
gWifiScanningEnabled = result;
|
|
|
|
if (self.wifiService) {
|
|
|
|
self.wifiService.stopWatching(self);
|
|
|
|
}
|
|
|
|
if (gWifiScanningEnabled) {
|
|
|
|
self.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
|
|
|
|
self.wifiService.startWatching(self);
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 21:16:24 +00:00
|
|
|
},
|
2014-07-07 05:16:00 +00:00
|
|
|
|
2014-04-23 21:16:24 +00:00
|
|
|
handleError: function(message) {
|
|
|
|
gLoggingEnabled = false;
|
|
|
|
LOG("settings callback threw an exception, dropping");
|
|
|
|
}
|
|
|
|
};
|
2012-12-31 16:36:46 +00:00
|
|
|
|
2015-01-15 04:28:27 +00:00
|
|
|
Services.obs.addObserver(this, SETTINGS_CHANGED_TOPIC, false);
|
|
|
|
let settingsService = Cc["@mozilla.org/settingsService;1"];
|
|
|
|
if (settingsService) {
|
|
|
|
let settings = settingsService.getService(Ci.nsISettingsService);
|
2014-06-09 05:56:29 +00:00
|
|
|
settings.createLock().get(SETTINGS_WIFI_ENABLED, settingsCallback);
|
|
|
|
settings.createLock().get(SETTINGS_DEBUG_ENABLED, settingsCallback);
|
2014-03-25 04:58:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:28:56 +00:00
|
|
|
if (gWifiScanningEnabled && Cc["@mozilla.org/wifi/monitor;1"]) {
|
2014-06-09 05:52:53 +00:00
|
|
|
if (this.wifiService) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
}
|
|
|
|
this.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
|
2011-08-09 20:02:39 +00:00
|
|
|
this.wifiService.startWatching(this);
|
2010-02-11 19:59:55 +00:00
|
|
|
}
|
2014-06-09 05:56:29 +00:00
|
|
|
|
2014-06-09 05:58:37 +00:00
|
|
|
this.resetTimer();
|
2014-03-02 07:46:02 +00:00
|
|
|
LOG("startup called.");
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: function(c) {
|
2014-03-22 07:42:55 +00:00
|
|
|
this.listener = c;
|
2011-08-09 20:02:39 +00:00
|
|
|
},
|
2010-02-11 19:59:55 +00:00
|
|
|
|
2014-03-02 07:46:02 +00:00
|
|
|
shutdown: function() {
|
2011-08-09 20:02:39 +00:00
|
|
|
LOG("shutdown called");
|
2014-03-02 07:46:02 +00:00
|
|
|
if (this.started == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 05:16:00 +00: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 05:58:37 +00:00
|
|
|
if (this.timer) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer = null;
|
2014-03-02 07:46:02 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
if(this.wifiService) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
this.wifiService = null;
|
|
|
|
}
|
2014-03-25 04:58:18 +00:00
|
|
|
|
2014-06-09 05:56:29 +00:00
|
|
|
Services.obs.removeObserver(this, SETTINGS_CHANGED_TOPIC);
|
2014-03-25 04:58:18 +00:00
|
|
|
|
2014-03-22 07:42:55 +00:00
|
|
|
this.listener = null;
|
2011-08-09 20:02:39 +00:00
|
|
|
this.started = false;
|
|
|
|
},
|
|
|
|
|
2012-03-21 17:52:35 +00:00
|
|
|
setHighAccuracy: function(enable) {
|
|
|
|
},
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
onChange: function(accessPoints) {
|
2014-06-09 05:58:37 +00:00
|
|
|
|
|
|
|
// we got some wifi data, rearm the timer.
|
|
|
|
this.resetTimer();
|
|
|
|
|
2013-06-13 02:20:07 +00:00
|
|
|
function isPublic(ap) {
|
2014-03-02 07:46:02 +00:00
|
|
|
let mask = "_nomap"
|
|
|
|
let result = ap.ssid.indexOf(mask, ap.ssid.length - mask.length);
|
|
|
|
if (result != -1) {
|
|
|
|
LOG("Filtering out " + ap.ssid + " " + result);
|
|
|
|
}
|
|
|
|
return result;
|
2011-08-09 20:02:39 +00:00
|
|
|
};
|
2009-05-07 17:52:39 +00:00
|
|
|
|
2013-06-13 02:20:07 +00:00
|
|
|
function sort(a, b) {
|
|
|
|
return b.signal - a.signal;
|
2013-06-14 03:20:15 +00:00
|
|
|
};
|
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
function encode(ap) {
|
2014-03-02 07:46:02 +00:00
|
|
|
return { 'macAddress': ap.mac, 'signalStrength': ap.signal };
|
2011-08-09 20:02:39 +00:00
|
|
|
};
|
|
|
|
|
2014-06-09 05:52:53 +00:00
|
|
|
let wifiData = null;
|
2011-08-09 20:02:39 +00:00
|
|
|
if (accessPoints) {
|
2014-06-09 05:52:53 +00:00
|
|
|
wifiData = accessPoints.filter(isPublic).sort(sort).map(encode);
|
2011-08-09 20:02:39 +00:00
|
|
|
}
|
2014-06-09 05:52:53 +00:00
|
|
|
this.sendLocationRequest(wifiData);
|
2014-03-02 07:46:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onError: function (code) {
|
|
|
|
LOG("wifi error: " + code);
|
2014-06-09 05:52:53 +00:00
|
|
|
this.sendLocationRequest(null);
|
2014-03-02 07:46:02 +00:00
|
|
|
},
|
2009-07-11 00:03:48 +00:00
|
|
|
|
2014-06-09 05:52:53 +00:00
|
|
|
getMobileInfo: function() {
|
2014-07-07 05:16:00 +00:00
|
|
|
LOG("getMobileInfo called");
|
2014-03-10 16:10:15 +00:00
|
|
|
try {
|
2014-05-27 13:50:00 +00:00
|
|
|
let radioService = Cc["@mozilla.org/ril;1"]
|
|
|
|
.getService(Ci.nsIRadioInterfaceLayer);
|
2014-08-11 08:15:59 +00:00
|
|
|
let service = Cc["@mozilla.org/mobileconnection/mobileconnectionservice;1"]
|
|
|
|
.getService(Ci.nsIMobileConnectionService);
|
|
|
|
|
2014-05-27 13:50:00 +00:00
|
|
|
let result = [];
|
2014-10-01 11:07:00 +00:00
|
|
|
for (let i = 0; i < service.numItems; i++) {
|
|
|
|
LOG("Looking for SIM in slot:" + i + " of " + service.numItems);
|
2014-09-21 07:24:43 +00:00
|
|
|
let connection = service.getItemByServiceId(i);
|
|
|
|
let voice = connection && connection.voice;
|
|
|
|
let cell = voice && voice.cell;
|
|
|
|
let type = voice && voice.type;
|
|
|
|
let network = voice && voice.network;
|
2014-05-27 13:50:00 +00:00
|
|
|
|
2014-09-09 16:35:00 +00:00
|
|
|
if (network && cell && type) {
|
2014-09-30 11:11:00 +00:00
|
|
|
let radioTechFamily;
|
|
|
|
switch (type) {
|
|
|
|
case "gsm":
|
|
|
|
case "gprs":
|
|
|
|
case "edge":
|
|
|
|
radioTechFamily = "gsm";
|
|
|
|
break;
|
|
|
|
case "umts":
|
|
|
|
case "hsdpa":
|
|
|
|
case "hsupa":
|
|
|
|
case "hspa":
|
|
|
|
case "hspa+":
|
|
|
|
radioTechFamily = "wcdma";
|
|
|
|
break;
|
|
|
|
case "lte":
|
|
|
|
radioTechFamily = "lte";
|
|
|
|
break;
|
|
|
|
// CDMA cases to be handled in bug 1010282
|
|
|
|
};
|
2015-07-21 10:43:00 +00:00
|
|
|
result.push({ radioType: radioTechFamily,
|
2014-09-09 16:35:00 +00:00
|
|
|
mobileCountryCode: voice.network.mcc,
|
|
|
|
mobileNetworkCode: voice.network.mnc,
|
2014-05-27 13:50:00 +00:00
|
|
|
locationAreaCode: cell.gsmLocationAreaCode,
|
|
|
|
cellId: cell.gsmCellId });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2014-03-10 16:10:15 +00:00
|
|
|
} catch (e) {
|
2014-06-09 05:52:53 +00:00
|
|
|
return null;
|
2014-03-10 16:10:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-09 05:58:37 +00:00
|
|
|
notify: function (timer) {
|
|
|
|
this.sendLocationRequest(null);
|
2014-06-09 05:52:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sendLocationRequest: function (wifiData) {
|
2014-12-17 18:44:00 +00:00
|
|
|
let data = { cellTowers: undefined, wifiAccessPoints: undefined };
|
|
|
|
if (wifiData && wifiData.length >= 2) {
|
2014-07-07 05:16:00 +00:00
|
|
|
data.wifiAccessPoints = wifiData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gCellScanningEnabled) {
|
|
|
|
let cellData = this.getMobileInfo();
|
|
|
|
if (cellData && cellData.length > 0) {
|
|
|
|
data.cellTowers = cellData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let useCached = isCachedRequestMoreAccurateThanServerRequest(data.cellTowers,
|
|
|
|
data.wifiAccessPoints);
|
|
|
|
|
|
|
|
LOG("Use request cache:" + useCached + " reason:" + gDebugCacheReasoning);
|
|
|
|
|
|
|
|
if (useCached) {
|
|
|
|
gCachedRequest.location.timestamp = Date.now();
|
2014-07-17 12:35:33 +00:00
|
|
|
this.notifyListener("update", [gCachedRequest.location]);
|
2014-07-07 05:16:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on, do a network geolocation request //
|
2014-03-02 07:46:02 +00:00
|
|
|
let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
|
2014-06-26 02:23:48 +00:00
|
|
|
LOG("Sending request");
|
2009-04-14 16:10:20 +00:00
|
|
|
|
2011-08-09 20:02:39 +00:00
|
|
|
let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
2009-04-14 16:10:20 +00:00
|
|
|
|
2014-07-17 12:35:33 +00:00
|
|
|
this.notifyListener("locationUpdatePending");
|
2014-03-02 07:46:02 +00:00
|
|
|
|
2013-11-04 19:52:53 +00:00
|
|
|
try {
|
2014-03-02 07:46:02 +00:00
|
|
|
xhr.open("POST", url, true);
|
2013-11-04 19:52:53 +00:00
|
|
|
} catch (e) {
|
2014-07-17 12:35:33 +00:00
|
|
|
this.notifyListener("notifyError",
|
|
|
|
[POSITION_UNAVAILABLE]);
|
2014-03-02 07:46:02 +00:00
|
|
|
return;
|
2013-11-04 19:52:53 +00:00
|
|
|
}
|
2013-06-13 02:20:07 +00:00
|
|
|
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
xhr.responseType = "json";
|
2011-08-09 20:02:39 +00:00
|
|
|
xhr.mozBackgroundRequest = true;
|
|
|
|
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
|
2015-08-02 12:45:00 +00:00
|
|
|
xhr.timeout = Services.prefs.getIntPref("geo.wifi.xhr.timeout");
|
|
|
|
xhr.ontimeout = (function() {
|
|
|
|
LOG("Location request XHR timed out.")
|
|
|
|
this.notifyListener("notifyError",
|
|
|
|
[POSITION_UNAVAILABLE]);
|
|
|
|
}).bind(this);
|
2014-07-17 12:35:33 +00:00
|
|
|
xhr.onerror = (function() {
|
|
|
|
this.notifyListener("notifyError",
|
|
|
|
[POSITION_UNAVAILABLE]);
|
|
|
|
}).bind(this);
|
|
|
|
xhr.onload = (function() {
|
2015-01-15 04:28:27 +00:00
|
|
|
LOG("server returned status: " + xhr.status + " --> " + JSON.stringify(xhr.response));
|
2014-03-02 07:46:02 +00:00
|
|
|
if ((xhr.channel instanceof Ci.nsIHttpChannel && xhr.status != 200) ||
|
|
|
|
!xhr.response || !xhr.response.location) {
|
2014-07-17 12:35:33 +00:00
|
|
|
this.notifyListener("notifyError",
|
|
|
|
[POSITION_UNAVAILABLE]);
|
2014-03-02 07:46:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-13 02:20:07 +00:00
|
|
|
|
2014-03-02 07:46:02 +00:00
|
|
|
let newLocation = new WifiGeoPositionObject(xhr.response.location.lat,
|
|
|
|
xhr.response.location.lng,
|
|
|
|
xhr.response.accuracy);
|
|
|
|
|
2014-07-17 12:35:33 +00:00
|
|
|
this.notifyListener("update", [newLocation]);
|
2014-07-07 05:16:00 +00:00
|
|
|
gCachedRequest = new CachedRequest(newLocation, data.cellTowers, data.wifiAccessPoints);
|
2014-07-17 12:35:33 +00:00
|
|
|
}).bind(this);
|
2013-06-13 02:20:07 +00:00
|
|
|
|
2014-07-07 05:16:00 +00:00
|
|
|
var requestData = JSON.stringify(data);
|
|
|
|
LOG("sending " + requestData);
|
|
|
|
xhr.send(requestData);
|
2011-08-09 20:02:39 +00:00
|
|
|
},
|
2014-07-17 12:35:33 +00:00
|
|
|
|
|
|
|
notifyListener: function(listenerFunc, args) {
|
|
|
|
args = args || [];
|
|
|
|
try {
|
|
|
|
this.listener[listenerFunc].apply(this.listener, args);
|
|
|
|
} catch(e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 16:10:20 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);
|