forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkGeolocationProvider.js
executable file
·448 lines (367 loc) · 15.5 KB
/
NetworkGeolocationProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Ci = Components.interfaces;
const Cc = Components.classes;
var gLoggingEnabled = false;
var gTestingEnabled = false;
function nowInSeconds()
{
return Date.now() / 1000;
}
function LOG(aMsg) {
if (gLoggingEnabled)
{
aMsg = "*** WIFI GEO: " + aMsg + "\n";
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
dump(aMsg);
}
}
function WifiGeoAddressObject(streetNumber, street, premises, city, county, region, country, countryCode, postalCode) {
this.streetNumber = streetNumber;
this.street = street;
this.premises = premises;
this.city = city;
this.county = county;
this.region = region;
this.country = country;
this.countryCode = countryCode;
this.postalCode = postalCode;
}
WifiGeoAddressObject.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionAddress, Ci.nsIClassInfo]),
getInterfaces: function(countRef) {
var interfaces = [Ci.nsIDOMGeoPositionAddress, Ci.nsIClassInfo, Ci.nsISupports];
countRef.value = interfaces.length;
return interfaces;
},
getHelperForLanguage: function(language) null,
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
};
function WifiGeoCoordsObject(lat, lon, acc, alt, altacc) {
this.latitude = lat;
this.longitude = lon;
this.accuracy = acc;
this.altitude = alt;
this.altitudeAccuracy = altacc;
};
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,
classDescription: "wifi geo position coords object",
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
latitude: 0,
longitude: 0,
accuracy: 0,
altitude: 0,
altitudeAccuracy: 0,
};
function WifiGeoPositionObject(location) {
this.coords = new WifiGeoCoordsObject(location.latitude,
location.longitude,
location.accuracy || 12450, // .5 * circumference of earth.
location.altitude || 0,
location.altitude_accuracy || 0);
if (location.address) {
let address = location.address;
this.address = new WifiGeoAddressObject(address.street_number || null,
address.street || null,
address.premises || null,
address.city || null,
address.county || null,
address.region || null,
address.country || null,
address.country_code || null,
address.postal_code || null);
}
else
this.address = null;
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,
classDescription: "wifi geo location position object",
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
coords: null,
timestamp: 0,
};
function HELD() {};
// For information about the HELD format, see:
// http://tools.ietf.org/html/draft-thomson-geopriv-held-measurements-05
HELD.encode = function(requestObject) {
// XML Header
var requestString = "<locationRequest xmlns=\"urn:ietf:params:xml:ns:geopriv:held\">";
// Measurements
if (requestObject.wifi_towers && requestObject.wifi_towers.length > 0) {
requestString += "<measurements xmlns=\"urn:ietf:params:xml:ns:geopriv:lm\">";
requestString += "<wifi xmlns=\"urn:ietf:params:xml:ns:geopriv:lm:wifi\">";
for (var i=0; i < requestObject.wifi_towers.length; ++i) {
requestString += "<neighbourWap>";
requestString += "<bssid>" + requestObject.wifi_towers[i].mac_address + "</bssid>";
requestString += "<ssid>" + requestObject.wifi_towers[i].ssid + "</ssid>";
requestString += "<rssi>" + requestObject.wifi_towers[i].signal_strength + "</rssi>";
requestString += "</neighbourWap>";
}
// XML Footer
requestString += "</wifi></measurements>";
}
requestString += "</locationRequest>";
return requestString;
};
// Decode a HELD response into a Gears-style object
HELD.decode = function(responseXML) {
// Find a Circle object in PIDF-LO and decode
function nsResolver(prefix) {
var ns = {
'held': 'urn:ietf:params:xml:ns:geopriv:held',
'pres': 'urn:ietf:params:xml:ns:pidf',
'gp': 'urn:ietf:params:xml:ns:pidf:geopriv10',
'gml': 'http://www.opengis.net/gml',
'gs': 'http://www.opengis.net/pidflo/1.0',
};
return ns[prefix] || null;
}
var xpathEval = Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Ci.nsIDOMXPathEvaluator);
// Grab values out of XML via XPath
var pos = xpathEval.evaluate(
'/held:locationResponse/pres:presence/pres:tuple/pres:status/gp:geopriv/gp:location-info/gs:Circle/gml:pos',
responseXML,
nsResolver,
Ci.nsIDOMXPathResult.STRING_TYPE,
null);
var rad = xpathEval.evaluate(
'/held:locationResponse/pres:presence/pres:tuple/pres:status/gp:geopriv/gp:location-info/gs:Circle/gs:radius',
responseXML,
nsResolver,
Ci.nsIDOMXPathResult.NUMBER_TYPE,
null );
var uom = xpathEval.evaluate(
'/held:locationResponse/pres:presence/pres:tuple/pres:status/gp:geopriv/gp:location-info/gs:Circle/gs:radius/@uom',
responseXML,
nsResolver,
Ci.nsIDOMXPathResult.STRING_TYPE,
null);
// Bail if we don't have a valid result (all values && uom==meters)
if ((pos.stringValue == null) ||
(rad.numberValue == null) ||
(uom.stringValue == null) ||
(uom.stringValue != "urn:ogc:def:uom:EPSG::9001")) {
return null;
}
// Split the pos value into lat/long
var coords = pos.stringValue.split(/[ \t\n]+/);
// Fill out the object to return:
var obj = {
location: {
latitude: parseFloat(coords[0]),
longitude: parseFloat(coords[1]),
accuracy: rad.numberValue
}
};
return obj;
}
function WifiGeoPositionProvider() {
this.prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).QueryInterface(Ci.nsIPrefService);
try {
gLoggingEnabled = this.prefService.getBoolPref("geo.wifi.logging.enabled");
} catch (e) {}
try {
gTestingEnabled = this.prefService.getBoolPref("geo.wifi.testing");
} catch (e) {}
};
WifiGeoPositionProvider.prototype = {
classID: Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
Ci.nsIWifiListener,
Ci.nsITimerCallback]),
prefService: null,
wifi_service: null,
timer: null,
hasSeenWiFi: false,
started: false,
startup: function() {
if (this.started == true)
return;
this.started = true;
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.hasSeenWiFi = false;
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
if (gTestingEnabled == false)
this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
else
this.timer.initWithCallback(this, 200, this.timer.TYPE_REPEATING_SLACK);
},
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);
}
},
shutdown: function() {
LOG("shutdown called");
if(this.wifi_service)
this.wifi_service.stopWatching(this);
this.wifi_service = null;
if (this.timer != null) {
this.timer.cancel();
this.timer = null;
}
// 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).e
let prefBranch = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
if (prefBranch.getIntPref("network.cookie.lifetimePolicy") != 0)
prefBranch.deleteBranch("geo.wifi.access_token.");
this.started = false;
},
getAccessTokenForURL: function(url)
{
// check to see if we have an access token:
var accessToken = "";
try {
var accessTokenPrefName = "geo.wifi.access_token." + url;
accessToken = this.prefService.getCharPref(accessTokenPrefName);
// check to see if it has expired
var accessTokenDate = this.prefService.getIntPref(accessTokenPrefName + ".time");
var accessTokenInterval = 1209600; /* seconds in 2 weeks */
try {
accessTokenInterval = this.prefService.getIntPref("geo.wifi.access_token.recycle_interval");
} catch (e) {}
if (nowInSeconds() - accessTokenDate > accessTokenInterval)
accessToken = "";
}
catch (e) {
accessToken = "";
}
return accessToken;
},
onChange: function(accessPoints) {
LOG("onChange called");
this.hasSeenWiFi = true;
// send our request to a wifi geolocation network provider:
var xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Ci.nsIXMLHttpRequest);
// This is a background load
xhr.mozBackgroundRequest = true;
var provider_url = this.prefService.getCharPref("geo.wifi.uri");
var provider_protocol = 0;
try {
provider_protocol = this.prefService.getIntPref("geo.wifi.protocol");
} catch (e) {}
LOG("provider url = " + provider_url);
xhr.open("POST", provider_url, false);
// set something so that we can strip cookies
xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS;
xhr.onerror = function(req) {
LOG("onerror: " + req);
};
xhr.onload = function (req) {
LOG("xhr onload...");
try {
// if we get a bad response, we will throw and never report a location
var response;
switch (provider_protocol) {
case 1:
LOG("service returned: " + req.target.responseXML);
response = HELD.decode(req.target.responseXML);
break;
case 0:
default:
LOG("service returned: " + req.target.responseText);
response = JSON.parse(req.target.responseText);
}
} catch (e) {
LOG("Parse failed");
return;
}
// 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);
try {
prefService.setIntPref(accessTokenPrefName + ".time", nowInSeconds());
prefService.setCharPref(accessTokenPrefName, newAccessToken);
} catch (x) {
// XXX temporary hack for bug 575346 to allow geolocation to function
}
}
}
if (response.location) {
var newLocation = new WifiGeoPositionObject(response.location);
var update = Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate);
update.update(newLocation);
}
};
var accessToken = this.getAccessTokenForURL(provider_url);
var request = {
version: "1.1.0",
request_address: true,
};
if (accessToken != "")
request.access_token = accessToken;
if (accessPoints != null) {
function filterBlankSSIDs(ap) ap.ssid != ""
function deconstruct(ap) ({
mac_address: ap.mac,
ssid: ap.ssid,
signal_strength: ap.signal
})
request.wifi_towers = accessPoints.filter(filterBlankSSIDs).map(deconstruct);
}
var requestString;
switch (provider_protocol) {
case 1:
requestString = HELD.encode(request);
break;
case 0:
default:
requestString = JSON.stringify(request);
}
LOG("client sending: " + requestString);
try {
xhr.send(requestString);
} catch (e) {}
},
onError: function (code) {
LOG("wifi error: " + code);
},
notify: function (timer) {
if (!gTestingEnabled) {
if (this.hasSeenWiFi == false)
this.onChange(null);
this.timer = null;
return;
}
// if we are testing, we need to hammer this.
this.onChange(null);
},
};
var NSGetFactory = XPCOMUtils.generateNSGetFactory([WifiGeoPositionProvider]);