2012-05-21 04:12:37 -07: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 13:02:39 -07:00
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2011-08-09 13:02:39 -07:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2009-04-14 09:10:20 -07:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
const POSITION_UNAVAILABLE = Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE;
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
let gLoggingEnabled = false;
|
2014-03-01 23:46:02 -08:00
|
|
|
|
|
|
|
// if we don't see any wifi responses in 5 seconds, send the request.
|
|
|
|
let gTimeToWaitBeforeSending = 5000; //ms
|
|
|
|
|
|
|
|
let gWifiScanningEnabled = true;
|
|
|
|
let gWifiResults;
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2014-03-10 09:10:15 -07:00
|
|
|
let gCellScanningEnabled = false;
|
|
|
|
let gCellResults;
|
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
function LOG(aMsg) {
|
2014-03-01 23:46:02 -08:00
|
|
|
if (gLoggingEnabled) {
|
2010-08-31 09:15:52 -07:00
|
|
|
aMsg = "*** WIFI GEO: " + aMsg + "\n";
|
2009-08-14 16:12:09 -07:00
|
|
|
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
|
|
|
|
dump(aMsg);
|
|
|
|
}
|
2009-04-14 09:10:20 -07:00
|
|
|
}
|
|
|
|
|
2009-08-14 16:12:09 -07:00
|
|
|
function WifiGeoCoordsObject(lat, lon, acc, alt, altacc) {
|
2011-08-09 13:02:39 -07:00
|
|
|
this.latitude = lat;
|
|
|
|
this.longitude = lon;
|
|
|
|
this.accuracy = acc;
|
|
|
|
this.altitude = alt;
|
|
|
|
this.altitudeAccuracy = altacc;
|
|
|
|
}
|
2009-04-14 09:10:20 -07:00
|
|
|
|
|
|
|
WifiGeoCoordsObject.prototype = {
|
2011-08-09 13:02:39 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionCoords]),
|
2009-08-14 16:12:09 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
classInfo: XPCOMUtils.generateCI({interfaces: [Ci.nsIDOMGeoPositionCoords],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "wifi geo position coords object"}),
|
2009-04-14 09:10:20 -07:00
|
|
|
};
|
|
|
|
|
2011-08-09 13:02:39 -07: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 09:10:20 -07:00
|
|
|
|
|
|
|
WifiGeoPositionObject.prototype = {
|
2011-08-09 13:02:39 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPosition]),
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
// Class Info is required to be able to pass objects back into the DOM.
|
|
|
|
classInfo: XPCOMUtils.generateCI({interfaces: [Ci.nsIDOMGeoPosition],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "wifi geo location position object"}),
|
2009-04-14 09:10:20 -07:00
|
|
|
};
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
function WifiGeoPositionProvider() {
|
|
|
|
try {
|
|
|
|
gLoggingEnabled = Services.prefs.getBoolPref("geo.wifi.logging.enabled");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
try {
|
2014-03-01 23:46:02 -08:00
|
|
|
gTimeToWaitBeforeSending = Services.prefs.getIntPref("geo.wifi.timeToWaitBeforeSending");
|
2011-08-09 13:02:39 -07:00
|
|
|
} catch (e) {}
|
|
|
|
|
2013-01-06 21:57:00 -08:00
|
|
|
try {
|
2014-03-01 23:46:02 -08:00
|
|
|
gWifiScanningEnabled = Services.prefs.getBoolPref("geo.wifi.scan");
|
2013-01-06 21:57:00 -08:00
|
|
|
} catch (e) {}
|
|
|
|
|
2014-03-10 09:10:15 -07:00
|
|
|
try {
|
|
|
|
gCellScanningEnabled = Services.prefs.getBoolPref("geo.cell.scan");
|
|
|
|
} catch (e) {}
|
|
|
|
|
2011-12-01 03:02:51 -08:00
|
|
|
this.wifiService = null;
|
2014-03-01 23:46:02 -08:00
|
|
|
this.timeoutTimer = null;
|
2011-12-01 03:02:51 -08:00
|
|
|
this.started = false;
|
2011-08-09 13:02:39 -07:00
|
|
|
}
|
2010-02-11 11:59:55 -08:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
WifiGeoPositionProvider.prototype = {
|
|
|
|
classID: Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
|
|
|
|
Ci.nsIWifiListener,
|
|
|
|
Ci.nsITimerCallback]),
|
|
|
|
startup: function() {
|
|
|
|
if (this.started)
|
|
|
|
return;
|
|
|
|
this.started = true;
|
2012-12-31 08:36:46 -08:00
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
if (gWifiScanningEnabled) {
|
2011-08-09 13:02:39 -07:00
|
|
|
this.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Components.interfaces.nsIWifiMonitor);
|
|
|
|
this.wifiService.startWatching(this);
|
2010-02-11 11:59:55 -08:00
|
|
|
}
|
2014-03-01 23:46:02 -08:00
|
|
|
this.timeoutTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this.timeoutTimer.initWithCallback(this,
|
|
|
|
gTimeToWaitBeforeSending,
|
|
|
|
this.timeoutTimer.TYPE_REPEATING_SLACK);
|
|
|
|
LOG("startup called.");
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: function(c) {
|
2011-08-09 13:02:39 -07:00
|
|
|
},
|
2010-02-11 11:59:55 -08:00
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
shutdown: function() {
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("shutdown called");
|
2014-03-01 23:46:02 -08:00
|
|
|
if (this.started == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.timeoutTimer) {
|
|
|
|
this.timeoutTimer.cancel();
|
|
|
|
this.timeoutTimer = null;
|
|
|
|
}
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
if(this.wifiService) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
this.wifiService = null;
|
|
|
|
}
|
|
|
|
this.started = false;
|
|
|
|
},
|
|
|
|
|
2012-03-21 10:52:35 -07:00
|
|
|
setHighAccuracy: function(enable) {
|
|
|
|
},
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
onChange: function(accessPoints) {
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2013-06-12 19:20:07 -07:00
|
|
|
function isPublic(ap) {
|
2014-03-01 23:46:02 -08: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 13:02:39 -07:00
|
|
|
};
|
2009-05-07 10:52:39 -07:00
|
|
|
|
2013-06-12 19:20:07 -07:00
|
|
|
function sort(a, b) {
|
|
|
|
return b.signal - a.signal;
|
2013-06-13 20:20:15 -07:00
|
|
|
};
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
function encode(ap) {
|
2014-03-01 23:46:02 -08:00
|
|
|
return { 'macAddress': ap.mac, 'signalStrength': ap.signal };
|
2011-08-09 13:02:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (accessPoints) {
|
2014-03-10 09:10:15 -07:00
|
|
|
gWifiResults = accessPoints.filter(isPublic).sort(sort).map(encode);
|
|
|
|
} else {
|
|
|
|
gWifiResults = null;
|
2011-08-09 13:02:39 -07:00
|
|
|
}
|
2014-03-01 23:46:02 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
onError: function (code) {
|
|
|
|
LOG("wifi error: " + code);
|
|
|
|
},
|
2009-07-10 17:03:48 -07:00
|
|
|
|
2014-03-10 09:10:15 -07:00
|
|
|
updateMobileInfo: function() {
|
|
|
|
LOG("updateMobileInfo called");
|
|
|
|
try {
|
|
|
|
let radio = Cc["@mozilla.org/ril;1"]
|
|
|
|
.getService(Ci.nsIRadioInterfaceLayer)
|
|
|
|
.getRadioInterface(0);
|
|
|
|
|
|
|
|
let iccInfo = radio.rilContext.iccInfo;
|
|
|
|
let cell = radio.rilContext.voice.cell;
|
|
|
|
|
|
|
|
LOG("mcc: " + iccInfo.mcc);
|
|
|
|
LOG("mnc: " + iccInfo.mnc);
|
|
|
|
LOG("cid: " + cell.gsmCellId);
|
|
|
|
LOG("lac: " + cell.gsmLocationAreaCode);
|
|
|
|
|
|
|
|
gCellResults = [{
|
|
|
|
"radio": "gsm",
|
|
|
|
"mobileCountryCode": iccInfo.mcc,
|
|
|
|
"mobileNetworkCode": iccInfo.mnc,
|
|
|
|
"locationAreaCode": cell.gsmLocationAreaCode,
|
|
|
|
"cellId": cell.gsmCellId,
|
|
|
|
}];
|
|
|
|
} catch (e) {
|
|
|
|
gCellResults = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
notify: function (timeoutTimer) {
|
|
|
|
let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
|
|
|
|
LOG("Sending request: " + url + "\n");
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
getGeoService().locationUpdatePending();
|
|
|
|
|
2013-11-04 11:52:53 -08:00
|
|
|
try {
|
2014-03-01 23:46:02 -08:00
|
|
|
xhr.open("POST", url, true);
|
2013-11-04 11:52:53 -08:00
|
|
|
} catch (e) {
|
2014-03-01 23:46:02 -08:00
|
|
|
getGeoService().notifyError(POSITION_UNAVAILABLE);
|
|
|
|
return;
|
2013-11-04 11:52:53 -08:00
|
|
|
}
|
2013-06-12 19:20:07 -07:00
|
|
|
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
xhr.responseType = "json";
|
2011-08-09 13:02:39 -07:00
|
|
|
xhr.mozBackgroundRequest = true;
|
|
|
|
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
|
2013-06-12 19:20:07 -07:00
|
|
|
xhr.onerror = function() {
|
2014-03-01 23:46:02 -08:00
|
|
|
getGeoService().notifyError(POSITION_UNAVAILABLE);
|
2013-06-12 19:20:07 -07:00
|
|
|
};
|
2014-03-01 23:46:02 -08:00
|
|
|
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) {
|
|
|
|
getGeoService().notifyError(POSITION_UNAVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-12 19:20:07 -07:00
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
let newLocation = new WifiGeoPositionObject(xhr.response.location.lat,
|
|
|
|
xhr.response.location.lng,
|
|
|
|
xhr.response.accuracy);
|
|
|
|
|
|
|
|
getGeoService().update(newLocation);
|
2013-06-12 19:20:07 -07:00
|
|
|
};
|
|
|
|
|
2014-03-10 09:10:15 -07:00
|
|
|
if (gCellScanningEnabled) {
|
|
|
|
this.updateMobileInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {};
|
|
|
|
if (gWifiResults) {
|
|
|
|
data.wifiAccessPoints = gWifiResults;
|
|
|
|
}
|
|
|
|
if (gCellResults) {
|
|
|
|
data.cellTowers = gCellResults;
|
|
|
|
}
|
|
|
|
data = JSON.stringify(data);
|
|
|
|
gWifiResults = gCellResults = null;
|
2014-03-01 23:46:02 -08:00
|
|
|
LOG("sending " + data);
|
2013-06-12 19:20:07 -07:00
|
|
|
xhr.send(data);
|
2011-08-09 13:02:39 -07:00
|
|
|
},
|
2009-04-14 09:10:20 -07:00
|
|
|
};
|
|
|
|
|
2014-03-01 23:46:02 -08:00
|
|
|
function getGeoService() {
|
|
|
|
return Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate);
|
2013-11-04 11:52:53 -08:00
|
|
|
}
|
2014-03-01 23:46:02 -08:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);
|