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
|
|
|
|
|
|
|
|
|
|
|
// Do not use this API without permission from Google.
|
|
|
|
// See http://www.google.com/support/enterprise/bin/request.py?contact_type=gme&utm_campaign=en-us-ptr-mz
|
|
|
|
// for more information.
|
|
|
|
|
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;
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
let gLoggingEnabled = false;
|
|
|
|
let gTestingEnabled = false;
|
2013-01-06 21:57:00 -08:00
|
|
|
let gUseScanning = true;
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2012-12-06 18:13:15 -08:00
|
|
|
let gPrivateAccessToken = '';
|
|
|
|
let gPrivateAccessTime = 0;
|
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
function LOG(aMsg) {
|
2009-08-14 16:12:09 -07: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
|
|
|
};
|
|
|
|
|
2012-12-06 18:13:15 -08:00
|
|
|
function privateBrowsingObserver(aSubject, aTopic, aData) {
|
|
|
|
gPrivateAccessToken = '';
|
|
|
|
gPrivateAccessTime = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
function WifiGeoPositionProvider() {
|
|
|
|
try {
|
|
|
|
gLoggingEnabled = Services.prefs.getBoolPref("geo.wifi.logging.enabled");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
gTestingEnabled = Services.prefs.getBoolPref("geo.wifi.testing");
|
|
|
|
} catch (e) {}
|
|
|
|
|
2013-01-06 21:57:00 -08:00
|
|
|
try {
|
|
|
|
gUseScanning = Services.prefs.getBoolPref("geo.wifi.scan");
|
|
|
|
} catch (e) {}
|
|
|
|
|
2011-12-01 03:02:51 -08:00
|
|
|
this.wifiService = null;
|
|
|
|
this.timer = null;
|
|
|
|
this.hasSeenWiFi = false;
|
|
|
|
this.started = false;
|
2012-12-06 18:13:15 -08:00
|
|
|
this.lastRequestPrivate = false;
|
|
|
|
|
|
|
|
Services.obs.addObserver(privateBrowsingObserver, "last-pb-context-exited", 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;
|
|
|
|
this.hasSeenWiFi = false;
|
|
|
|
|
|
|
|
LOG("startup called. testing mode is" + gTestingEnabled);
|
|
|
|
|
|
|
|
// if we don't see anything in 5 seconds, kick of one IP geo lookup.
|
|
|
|
// if we are testing, just hammer this callback so that we are more or less
|
|
|
|
// always sending data. It doesn't matter if we have an access point or not.
|
|
|
|
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
if (!gTestingEnabled)
|
|
|
|
this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
|
|
|
|
else
|
|
|
|
this.timer.initWithCallback(this, 200, this.timer.TYPE_REPEATING_SLACK);
|
|
|
|
},
|
|
|
|
|
2012-12-06 18:13:15 -08:00
|
|
|
watch: function(c, requestPrivate) {
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("watch called");
|
2012-12-31 08:36:46 -08:00
|
|
|
|
2013-01-06 21:57:00 -08:00
|
|
|
if (!this.wifiService && gUseScanning) {
|
2011-08-09 13:02:39 -07:00
|
|
|
this.wifiService = Cc["@mozilla.org/wifi/monitor;1"].getService(Components.interfaces.nsIWifiMonitor);
|
|
|
|
this.wifiService.startWatching(this);
|
2012-12-06 18:13:15 -08:00
|
|
|
this.lastRequestPrivate = requestPrivate;
|
2010-02-11 11:59:55 -08:00
|
|
|
}
|
2012-02-29 22:58:37 -08:00
|
|
|
if (this.hasSeenWiFi) {
|
|
|
|
this.hasSeenWiFi = false;
|
2013-01-06 21:57:00 -08:00
|
|
|
if (gUseScanning) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
this.wifiService.startWatching(this);
|
|
|
|
} else {
|
|
|
|
// For testing situations, ensure that we always trigger an update.
|
|
|
|
this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
|
|
|
|
}
|
2012-12-06 18:13:15 -08:00
|
|
|
this.lastRequestPrivate = requestPrivate;
|
2012-02-29 22:58:37 -08:00
|
|
|
}
|
2011-08-09 13:02:39 -07:00
|
|
|
},
|
2010-02-11 11:59:55 -08:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
shutdown: function() {
|
|
|
|
LOG("shutdown called");
|
|
|
|
if(this.wifiService) {
|
|
|
|
this.wifiService.stopWatching(this);
|
|
|
|
this.wifiService = null;
|
|
|
|
}
|
|
|
|
if (this.timer != null) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer = null;
|
2010-02-11 11:59:55 -08:00
|
|
|
}
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
// Although we aren't using cookies, we should err on the side of not
|
|
|
|
// saving any access tokens if the user asked us not to save cookies or
|
|
|
|
// has changed the lifetimePolicy. The access token in these cases is
|
|
|
|
// used and valid for the life of this object (eg. between startup and
|
|
|
|
// shutdown).
|
|
|
|
if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") != 0)
|
|
|
|
Services.prefs.deleteBranch("geo.wifi.access_token.");
|
|
|
|
this.started = false;
|
|
|
|
},
|
|
|
|
|
2012-03-21 10:52:35 -07:00
|
|
|
setHighAccuracy: function(enable) {
|
|
|
|
},
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
getAccessTokenForURL: function(url)
|
|
|
|
{
|
|
|
|
// check to see if we have an access token:
|
|
|
|
let accessToken = "";
|
2009-07-10 17:03:48 -07:00
|
|
|
try {
|
2012-12-06 18:13:15 -08:00
|
|
|
if (this.lastRequestPrivate) {
|
|
|
|
accessToken = gPrivateAccessToken;
|
|
|
|
} else {
|
|
|
|
let accessTokenPrefName = "geo.wifi.access_token." + url;
|
|
|
|
accessToken = Services.prefs.getCharPref(accessTokenPrefName);
|
|
|
|
}
|
2011-08-09 13:02:39 -07:00
|
|
|
|
|
|
|
// check to see if it has expired
|
2012-12-06 18:13:15 -08:00
|
|
|
let accessTokenDate;
|
|
|
|
if (this.lastRequestPrivate) {
|
|
|
|
accessTokenDate = gPrivateAccessTime;
|
|
|
|
} else {
|
|
|
|
Services.prefs.getIntPref(accessTokenPrefName + ".time");
|
|
|
|
}
|
2011-08-09 13:02:39 -07:00
|
|
|
|
|
|
|
let accessTokenInterval = 1209600; // seconds in 2 weeks
|
|
|
|
try {
|
|
|
|
accessTokenInterval = Services.prefs.getIntPref("geo.wifi.access_token.recycle_interval");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if ((Date.now() / 1000) - accessTokenDate > accessTokenInterval)
|
|
|
|
accessToken = "";
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
accessToken = "";
|
|
|
|
}
|
|
|
|
return accessToken;
|
|
|
|
},
|
2009-08-14 16:12:09 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
onChange: function(accessPoints) {
|
|
|
|
LOG("onChange called");
|
|
|
|
this.hasSeenWiFi = true;
|
2009-08-14 16:12:09 -07:00
|
|
|
|
2011-09-27 07:23:49 -07:00
|
|
|
let providerUrlBase = "https://maps.googleapis.com/maps/api/browserlocation/json";
|
|
|
|
try {
|
|
|
|
providerUrlBase = Services.prefs.getCharPref("geo.wifi.uri");
|
|
|
|
} catch (x) {};
|
2011-08-09 13:02:39 -07:00
|
|
|
let providerUrl;
|
2009-07-10 17:03:48 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
let query = providerUrlBase.indexOf("?");
|
|
|
|
if (query == -1)
|
|
|
|
providerUrl = providerUrlBase + "?"
|
|
|
|
else
|
|
|
|
providerUrl = providerUrlBase + "&";
|
|
|
|
providerUrl = providerUrl + "browser=firefox&sensor=true";
|
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
let accessToken = this.getAccessTokenForURL(providerUrlBase);
|
|
|
|
if (accessToken !== "")
|
2011-11-20 03:18:27 -08:00
|
|
|
providerUrl = providerUrl + "&access_token="+accessToken;
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
function sort(a, b) {
|
|
|
|
return b.signal - a.signal;
|
|
|
|
};
|
2009-05-07 10:52:39 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
function encode(ap) {
|
|
|
|
// make sure that the ssid doesn't contain any | chars.
|
|
|
|
ap.ssid = ap.ssid.replace("|", "\\|");
|
|
|
|
// gls service parses the | as fields
|
|
|
|
return "&wifi=mac:"+ap.mac+"|ssid:"+ap.ssid+"|ss:"+ap.signal;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (accessPoints) {
|
2011-11-01 21:04:35 -07:00
|
|
|
providerUrl = providerUrl + accessPoints.sort(sort).map(encode).join("");
|
2011-08-09 13:02:39 -07:00
|
|
|
}
|
2009-07-10 17:03:48 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
providerUrl = encodeURI(providerUrl);
|
2012-01-03 20:13:34 -08:00
|
|
|
|
|
|
|
// max length is 2k. make sure we are under that
|
|
|
|
let x = providerUrl.length - 2000;
|
|
|
|
if (x >= 0) {
|
|
|
|
// we need to trim
|
|
|
|
let doomed = providerUrl.lastIndexOf("&", 2000);
|
|
|
|
LOG("Doomed:"+doomed);
|
|
|
|
providerUrl = providerUrl.substring(0, doomed);
|
|
|
|
}
|
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("************************************* Sending request:\n" + providerUrl + "\n");
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
// send our request to a wifi geolocation network provider:
|
|
|
|
let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
// This is a background load
|
|
|
|
xhr.mozBackgroundRequest = true;
|
2012-01-21 11:28:00 -08:00
|
|
|
xhr.open("GET", providerUrl, true);
|
2011-08-09 13:02:39 -07:00
|
|
|
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
|
2011-09-29 09:06:36 -07:00
|
|
|
xhr.addEventListener("error", function(req) {
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("onerror: " + req);
|
2011-09-29 09:06:36 -07:00
|
|
|
}, false);
|
|
|
|
xhr.addEventListener("load", function (req) {
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("service returned: " + req.target.responseText);
|
|
|
|
response = JSON.parse(req.target.responseText);
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"status": "OK",
|
|
|
|
"accuracy": 150.0,
|
|
|
|
"location": {
|
|
|
|
"lat": -33.85702,
|
|
|
|
"lng": 151.21494
|
|
|
|
},
|
|
|
|
"access_token": "quijibo"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (response.status != "OK")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (response.location) {
|
|
|
|
let newLocation = new WifiGeoPositionObject(response.location.lat,
|
|
|
|
response.location.lng,
|
|
|
|
response.accuracy);
|
|
|
|
|
|
|
|
let update = Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate);
|
|
|
|
update.update(newLocation);
|
|
|
|
}
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
// Check to see if we have a new access token
|
|
|
|
let newAccessToken = response.access_token;
|
|
|
|
if (newAccessToken !== undefined)
|
|
|
|
{
|
|
|
|
let accessToken = "";
|
|
|
|
let accessTokenPrefName = "geo.wifi.access_token." + providerUrlBase;
|
2012-12-06 18:13:15 -08:00
|
|
|
if (this.lastRequestPrivate) {
|
|
|
|
accessTokenPrefName = gPrivateAccessToken;
|
|
|
|
} else {
|
|
|
|
try { accessToken = Services.prefs.getCharPref(accessTokenPrefName); } catch (e) {}
|
|
|
|
}
|
2011-08-09 13:02:39 -07:00
|
|
|
|
|
|
|
if (accessToken != newAccessToken) {
|
|
|
|
// no match, lets cache
|
2012-12-06 18:13:15 -08:00
|
|
|
LOG("New Access Token: " + newAccessToken + "\n" + accessTokenPrefName);
|
|
|
|
if (this.lastRequestPrivate) {
|
|
|
|
gPrivateAccessToken = newAccessToken;
|
|
|
|
gPrivateAccessTime = nowInSeconds();
|
|
|
|
} else {
|
2011-08-09 13:02:39 -07:00
|
|
|
try {
|
|
|
|
Services.prefs.setIntPref(accessTokenPrefName + ".time", nowInSeconds());
|
|
|
|
Services.prefs.setCharPref(accessTokenPrefName, newAccessToken);
|
|
|
|
} catch (x) {
|
|
|
|
// XXX temporary hack for bug 575346 to allow geolocation to function
|
|
|
|
}
|
2012-12-06 18:13:15 -08:00
|
|
|
}
|
2011-08-09 13:02:39 -07:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 09:06:36 -07:00
|
|
|
}, false);
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
LOG("************************************* ------>>>> sending.");
|
|
|
|
xhr.send(null);
|
|
|
|
},
|
2010-08-31 09:15:52 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
onError: function (code) {
|
|
|
|
LOG("wifi error: " + code);
|
|
|
|
},
|
2010-08-31 09:15:52 -07:00
|
|
|
|
2011-08-09 13:02:39 -07:00
|
|
|
notify: function (timer) {
|
2013-01-06 21:57:00 -08:00
|
|
|
if (gTestingEnabled || !gUseScanning) {
|
2011-08-09 13:02:39 -07:00
|
|
|
// if we are testing, timer is repeating
|
|
|
|
this.onChange(null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!this.hasSeenWiFi)
|
2010-08-31 09:15:52 -07:00
|
|
|
this.onChange(null);
|
2011-08-09 13:02:39 -07:00
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
},
|
2009-04-14 09:10:20 -07:00
|
|
|
};
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);
|