Bug 487467. Support for a Network Geolocation Provider.

Browser bits for sanitizing.  r=gavin
Mochitest. r=ctalbert
Dom bits. r/sr=jst
Provider component. r=gavin, sr=jst
This commit is contained in:
Doug Turner 2009-04-14 09:10:20 -07:00
Родитель cecf89f477
Коммит 35582eed73
16 изменённых файлов: 317 добавлений и 26 удалений

Просмотреть файл

@ -148,6 +148,14 @@ Sanitizer.prototype = {
cookieMgr.removeAll();
}
// clear any network geolocation provider sessions
var psvc = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
try {
var branch = psvc.getBranch("geo.wifi.access_token.");
branch.deleteBranch("");
} catch (e) {}
},
get canClear()

Просмотреть файл

@ -216,6 +216,7 @@ bin/components/nsFilePicker.js
bin/components/nsHelperAppDlg.js
bin/components/nsDownloadManagerUI.js
bin/components/nsProxyAutoConfig.js
bin/components/NetworkGeolocationProvider.js
bin/components/nsSidebar.js
bin/components/nsExtensionManager.js
bin/components/nsBlocklistService.js

Просмотреть файл

@ -213,6 +213,7 @@ bin\components\nsTryToClose.js
bin\components\nsHelperAppDlg.js
bin\components\nsDownloadManagerUI.js
bin\components\nsProxyAutoConfig.js
bin\components\NetworkGeolocationProvider.js
bin\components\nsSearchService.js
bin\components\nsSearchSuggestions.js
bin\components\nsSidebar.js

Просмотреть файл

@ -116,6 +116,15 @@ Sanitizer.prototype = {
var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
.getService(Components.interfaces.nsICookieManager);
cookieMgr.removeAll();
// clear any network geolocation provider sessions
var psvc = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
try {
var branch = psvc.getBranch("geo.wifi.access_token.");
branch.deleteBranch("");
} catch (e) {}
},
get canClear()

Просмотреть файл

@ -89,6 +89,12 @@ ifdef ENABLE_TESTS
DIRS += test
endif
EXTRA_COMPONENTS = \
NetworkGeolocationProvider.js \
$(NULL)
include $(topsrcdir)/config/rules.mk
DEFINES += -D_IMPL_NS_LAYOUT

Просмотреть файл

@ -0,0 +1,255 @@
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Ci = Components.interfaces;
const Cc = Components.classes;
function nowInSeconds()
{
return Date.now() / 1000;
}
function LOG(aMsg) {
//aMsg = ("*** WIFI GEO: " + aMsg);
//Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
}
function getAccessTokenForURL(url)
{
// check to see if we have an access token:
var accessToken = "";
try {
var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
var accessTokenPrefName = "geo.wifi.access_token." + url;
accessToken = prefService.getCharPref(accessTokenPrefName);
// check to see if it has expired
var accessTokenDate = prefService.getIntPref(accessTokenPrefName + ".time");
var accessTokenInterval = 1209600; /* seconds in 2 weeks */
try {
accessTokenInterval = prefService.getIntPref("geo.wifi.access_token.recycle_interval");
} catch (e) {}
if (nowInSeconds() - accessTokenDate > accessTokenInterval)
accessToken = "";
}
catch (e) {
accessToken = "";
LOG("Error: "+ e);
}
return accessToken;
}
function WifiGeoCoordsObject(lat, lon, acc) {
this.latitude = lat;
this.longitude = lon;
this.accuracy = acc;
};
WifiGeoCoordsObject.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionCoords, Ci.nsIClassInfo]),
getInterfaces: function(countRef) {
var interfaces = [Ci.nsIDOMGeoPositionCoords, Ci.nsIClassInfo, Ci.nsISupports];
countRef.value = interfaces.length;
return interfaces;
},
getHelperForLanguage: function(language) null,
contractID: "",
classDescription: "wifi geo position coords object",
classID: null,
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
latitude: 0,
longitude: 0,
accuracy: 0,
altitude: 0,
altitudeAccuracy: 0,
heading: 0,
speed: 0,
};
function WifiGeoPositionObject(lat, lon, acc) {
this.coords = new WifiGeoCoordsObject(lat, lon, acc);
this.timestamp = Date.now();
};
WifiGeoPositionObject.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPosition, Ci.nsIClassInfo]),
// Class Info is required to be able to pass objects back into the DOM.
getInterfaces: function(countRef) {
var interfaces = [Ci.nsIDOMGeoPosition, Ci.nsIClassInfo, Ci.nsISupports];
countRef.value = interfaces.length;
return interfaces;
},
getHelperForLanguage: function(language) null,
contractID: "",
classDescription: "wifi geo location position object",
classID: null,
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
coords: null,
timestamp: 0,
};
function WifiGeoPositionProvider() {};
WifiGeoPositionProvider.prototype = {
classDescription: "A component that returns a geolocation based on WIFI",
classID: Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
contractID: "@mozilla.org/geolocation/provider;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationProvider, Ci.nsIWifiListener, Ci.nsITimerCallback]),
provider_url: null,
wifi_service: null,
update: null,
timer: null,
hasSeenWiFi: false,
startup: function() {
LOG("startup called");
var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
this.provider_url = prefService.getComplexValue("geo.wifi.uri", Ci.nsIPrefLocalizedString).data;
LOG("provider url = " + this.provider_url);
// if we don't see anything in 5 seconds, kick of one IP geo lookup.
this.hasSeenWiFi = false;
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
},
isReady: function() {
LOG("isReady called");
return true
},
watch: function(c) {
LOG("watch called");
if (!this.wifi_service)
this.wifi_service = Cc["@mozilla.org/wifi/monitor;1"].getService(Components.interfaces.nsIWifiMonitor);
this.wifi_service.startWatching(this);
this.update = c;
},
shutdown: function() {
LOG("shutdown called");
if(this.wifi_service)
this.wifi_service.stopWatching(this);
this.update = null;
if (this.timer != null) {
this.timer.cancel();
this.timer = null;
}
},
onChange: function(accessPoints) {
LOG("onChange called");
this.hasSeenWiFi = true;
var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
// send our request to a wifi geolocation network provider:
const xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
// This is a background load
xhr.mozBackgroundRequest = true;
xhr.open("POST", this.provider_url, false);
// set something so that we can strip cookies
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
// set something so that we can get back to the update object when onload is called
xhr.channel.QueryInterface(Ci.nsIWritablePropertyBag2).setPropertyAsInterface("moz-geolocation-service", this.update);
xhr.onerror = function(req) {
LOG("onerror: " + req);
};
xhr.onload = function (req) {
LOG("service returned: " + req.target.responseText);
// if we get a bad response, we will throw and never report a location
var response = JSON.parse(req.target.responseText);
// response looks something like:
// {"location":{"latitude":51.5090332,"longitude":-0.1212726,"accuracy":150.0},"access_token":"2:jVhRZJ-j6PiRchH_:RGMrR0W1BiwdZs12"}
// Check to see if we have a new access token
var newAccessToken = response.access_token;
if (newAccessToken != undefined)
{
var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
var accessToken = "";
var accessTokenPrefName = "geo.wifi.access_token." + req.target.channel.URI.spec;
try { accessToken = prefService.getCharPref(accessTokenPrefName); } catch (e) {}
if (accessToken != newAccessToken) {
// no match, lets cache
LOG("New Access Token: " + newAccessToken + "\n" + accessTokenPrefName);
prefService.setIntPref(accessTokenPrefName + ".time", nowInSeconds());
prefService.setCharPref(accessTokenPrefName, newAccessToken);
}
}
var newLocation = new WifiGeoPositionObject(response.location.latitude,
response.location.longitude,
response.location.accuracy);
var update = req.target.channel.QueryInterface(Ci.nsIPropertyBag2).getPropertyAsInterface("moz-geolocation-service", Ci.nsIGeolocationUpdate);
update.update(newLocation);
};
var accessToken = getAccessTokenForURL(this.provider_url);
var request = {
version: "1.1.0",
// request_address: true,
};
if (accessToken != "")
request.access_token = accessToken;
if (accessPoints != null) {
request.wifi_towers = accessPoints.map(function (ap) ({
mac_address: ap.mac,
ssid: ap.ssid,
signal_strength: ap.signal,
}));
}
var jsonString = JSON.stringify(request);
LOG("client sending: " + jsonString);
xhr.send(jsonString);
},
notify: function (timer) {
if (this.hasSeenWiFi == false)
this.onChange(null);
this.timer = null;
},
};
var components = [WifiGeoPositionProvider];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}

Просмотреть файл

@ -280,9 +280,6 @@ nsGeolocationRequest::Allow()
// send the cached location
SendLocation(lastPosition);
// remove ourselves from the locators callback lists.
mLocator->RemoveRequest(this);
}
PRInt32 timeout;
@ -352,7 +349,6 @@ NS_IMPL_THREADSAFE_ADDREF(nsGeolocationService)
NS_IMPL_THREADSAFE_RELEASE(nsGeolocationService)
nsGeolocationService::nsGeolocationService()
: mProviderStarted(PR_FALSE)
{
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
if (obs) {
@ -361,6 +357,10 @@ nsGeolocationService::nsGeolocationService()
mTimeout = nsContentUtils::GetIntPref("geo.timeout", 6000);
PRBool enabled = nsContentUtils::GetBoolPref("geo.enabled", PR_TRUE);
if (!enabled)
return;
mProvider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
// if NS_MAEMO_LOCATION, see if we should try the MAEMO location provider
@ -453,8 +453,6 @@ nsGeolocationService::StartDevice()
if (!mProvider)
return NS_ERROR_NOT_AVAILABLE;
if (!mProviderStarted) {
// if we have one, start it up.
nsresult rv = mProvider->Startup();
if (NS_FAILED(rv))
@ -463,16 +461,11 @@ nsGeolocationService::StartDevice()
// lets monitor it for any changes.
mProvider->Watch(this);
// remember that we are started up
mProviderStarted = PR_TRUE;
// we do not want to keep the geolocation devices online
// indefinitely. Close them down after a reasonable period of
// inactivivity
SetDisconnectTimer();
}
return NS_OK;
}
@ -494,7 +487,6 @@ nsGeolocationService::StopDevice()
{
if (mProvider) {
mProvider->Shutdown();
mProviderStarted = PR_FALSE;
}
if(mDisconnectTimer) {

Просмотреть файл

@ -148,9 +148,6 @@ private:
// The object providing geo location information to us.
nsCOMPtr<nsIGeolocationProvider> mProvider;
// A flag that lets us know if the mProvider has been started up.
PRBool mProviderStarted;
// mGeolocators are not owned here. Their constructor
// addes them to this list, and their destructor removes
// them from this list.

Просмотреть файл

@ -1,4 +1,19 @@
function ensure_geolocationProvider()
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
const testing_provider_cid = Components.ID("{10F622A4-6D7F-43A1-A938-5FFCBE2B1D1D}");
var testing_factory = Components.manager.getClassObject(testing_provider_cid, Components.interfaces.nsIFactory);
Components.manager.nsIComponentRegistrar.registerFactory(testing_provider_cid,
"Test Geolocation Provider",
"@mozilla.org/geolocation/provider;1",
testing_factory);
}
function stop_geolocationProvider()
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

Просмотреть файл

@ -33,6 +33,7 @@ function accept() {
clickNotificationButton(kAcceptButton);
}
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
// one-shot position requests

Просмотреть файл

@ -39,6 +39,7 @@ function accept() {
/** Test for Bug **/
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
watchID = navigator.geolocation.watchPosition(successCallback, null, null);

Просмотреть файл

@ -32,6 +32,7 @@ function successCallback(position){
/** Test for Bug **/
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
navigator.geolocation.getCurrentPosition(successCallback, failureCallback, null);

Просмотреть файл

@ -34,6 +34,7 @@ function successCallback(position){
/** Test for Bug **/
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
watchID = navigator.geolocation.getCurrentPosition(successCallback, failureCallback, null);

Просмотреть файл

@ -46,6 +46,7 @@ function testAccepted() {
/** Test for Bug **/
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
watchID = navigator.geolocation.watchPosition(successCallback, failureCallback, null);

Просмотреть файл

@ -23,6 +23,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=452566
const NOT_ENOUGH_ARGS = 2153185281;
ensure_geolocationProvider();
ok(navigator.geolocation, "Should have geolocation");
var exception = null;

Просмотреть файл

@ -21,6 +21,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=455327
<script class="testbody" type="text/javascript">
/** Test for Bug **/
ensure_geolocationProvider();
SimpleTest.waitForExplicitFinish();
stop_geolocationProvider();