Bug 977120 - Enable NetworkGeolocationProvider to collect cellular tower data on Firefox OS. r=jdm
authorDoug Turner <doug.turner@gmail.com>
Mon, 10 Mar 2014 16:10:15 +0000
changeset 189982 20e2ef50fa8b55ce8309355cf0a0432fafe0fd54
parent 189981 efc8568dcf367efca6a4b4aac6846c1bf7cebffc
child 189995 c54b412511887420dfe96623db55b0c366232287
child 190020 232ae3020845218c2e4c642059d7713eea71b6f3
push id3503
push userraliiev@mozilla.com
push dateMon, 28 Apr 2014 18:51:11 +0000
treeherdermozilla-beta@c95ac01e332e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs977120
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 977120 - Enable NetworkGeolocationProvider to collect cellular tower data on Firefox OS. r=jdm
dom/system/NetworkGeolocationProvider.js
--- a/dom/system/NetworkGeolocationProvider.js
+++ b/dom/system/NetworkGeolocationProvider.js
@@ -1,30 +1,31 @@
 /* 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/. */
 
-// See https://developers.google.com/maps/documentation/business/geolocation/
-
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 const Ci = Components.interfaces;
 const Cc = Components.classes;
 
 const POSITION_UNAVAILABLE = Ci.nsIDOMGeoPositionError.POSITION_UNAVAILABLE;
 
 let gLoggingEnabled = false;
 
 // if we don't see any wifi responses in 5 seconds, send the request.
 let gTimeToWaitBeforeSending = 5000; //ms
 
 let gWifiScanningEnabled = true;
 let gWifiResults;
 
+let gCellScanningEnabled = false;
+let gCellResults;
+
 function LOG(aMsg) {
   if (gLoggingEnabled) {
     aMsg = "*** WIFI GEO: " + aMsg + "\n";
     Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
     dump(aMsg);
   }
 }
 
@@ -32,32 +33,30 @@ function WifiGeoCoordsObject(lat, lon, a
   this.latitude = lat;
   this.longitude = lon;
   this.accuracy = acc;
   this.altitude = alt;
   this.altitudeAccuracy = altacc;
 }
 
 WifiGeoCoordsObject.prototype = {
-
   QueryInterface:  XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionCoords]),
 
   classInfo: XPCOMUtils.generateCI({interfaces: [Ci.nsIDOMGeoPositionCoords],
                                     flags: Ci.nsIClassInfo.DOM_OBJECT,
                                     classDescription: "wifi geo position coords object"}),
 };
 
 function WifiGeoPositionObject(lat, lng, acc) {
   this.coords = new WifiGeoCoordsObject(lat, lng, acc, 0, 0);
   this.address = null;
   this.timestamp = Date.now();
 }
 
 WifiGeoPositionObject.prototype = {
-
   QueryInterface:   XPCOMUtils.generateQI([Ci.nsIDOMGeoPosition]),
 
   // 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"}),
 };
 
@@ -69,16 +68,20 @@ function WifiGeoPositionProvider() {
   try {
     gTimeToWaitBeforeSending = Services.prefs.getIntPref("geo.wifi.timeToWaitBeforeSending");
   } catch (e) {}
 
   try {
     gWifiScanningEnabled = Services.prefs.getBoolPref("geo.wifi.scan");
   } catch (e) {}
 
+  try {
+    gCellScanningEnabled = Services.prefs.getBoolPref("geo.cell.scan");
+  } catch (e) {}
+
   this.wifiService = null;
   this.timeoutTimer = null;
   this.started = false;
 }
 
 WifiGeoPositionProvider.prototype = {
   classID:          Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
   QueryInterface:   XPCOMUtils.generateQI([Ci.nsIGeolocationProvider,
@@ -138,27 +141,54 @@ WifiGeoPositionProvider.prototype = {
     function sort(a, b) {
       return b.signal - a.signal;
     };
 
     function encode(ap) {
       return { 'macAddress': ap.mac, 'signalStrength': ap.signal };
     };
 
-    var data;
     if (accessPoints) {
-      data = JSON.stringify({wifiAccessPoints: accessPoints.filter(isPublic).sort(sort).map(encode)})
+      gWifiResults = accessPoints.filter(isPublic).sort(sort).map(encode);
+    } else {
+      gWifiResults = null;
     }
-    gWifiResults = data;
   },
 
   onError: function (code) {
     LOG("wifi error: " + code);
   },
 
+  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;
+    }
+  },
+
   notify: function (timeoutTimer) {
     let url = Services.urlFormatter.formatURLPref("geo.wifi.uri");
     LOG("Sending request: " + url + "\n");
 
     let xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                         .createInstance(Ci.nsIXMLHttpRequest);
 
     getGeoService().locationUpdatePending();
@@ -186,17 +216,29 @@ WifiGeoPositionProvider.prototype = {
 
       let newLocation = new WifiGeoPositionObject(xhr.response.location.lat,
                                                   xhr.response.location.lng,
                                                   xhr.response.accuracy);
 
       getGeoService().update(newLocation);
     };
 
-    let data = gWifiResults;
+    if (gCellScanningEnabled) {
+      this.updateMobileInfo();
+    }
+
+    let data = {};
+    if (gWifiResults) {
+      data.wifiAccessPoints = gWifiResults;
+    }
+    if (gCellResults) {
+      data.cellTowers = gCellResults;
+    }
+    data = JSON.stringify(data);
+    gWifiResults = gCellResults = null;
     LOG("sending " + data);
     xhr.send(data);
   },
 };
 
 function getGeoService() {
   return Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate);
 }