Bug 1200304 - Move stumbling code from gonkgps to MozStumbler.cpp. r=jdm
authorGarvan Keeley <gkeeley@mozilla.com>
Tue, 08 Sep 2015 21:20:56 -0400
changeset 294064 9838257c96b139b41678757f0ec118b74f9f174f
parent 294063 82e6e2bfbf9c93f11756a8aaffaf111451036d42
child 294065 b542d5f36d6bb87025b0287ed89c7f9edb83ff0c
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs1200304
milestone43.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 1200304 - Move stumbling code from gonkgps to MozStumbler.cpp. r=jdm
dom/system/gonk/GeolocationUtil.cpp
dom/system/gonk/GeolocationUtil.h
dom/system/gonk/GonkGPSGeolocationProvider.cpp
dom/system/gonk/moz.build
dom/system/gonk/mozstumbler/MozStumbler.cpp
dom/system/gonk/mozstumbler/MozStumbler.h
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/GeolocationUtil.cpp
@@ -0,0 +1,28 @@
+/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#include "GeolocationUtil.h"
+
+double CalculateDeltaInMeter(double aLat, double aLon, double aLastLat, double aLastLon)
+{
+  // Use spherical law of cosines to calculate difference
+  // Not quite as correct as the Haversine but simpler and cheaper
+  const double radsInDeg = M_PI / 180.0;
+  const double rNewLat = aLat * radsInDeg;
+  const double rNewLon = aLon * radsInDeg;
+  const double rOldLat = aLastLat * radsInDeg;
+  const double rOldLon = aLastLon * radsInDeg;
+  // WGS84 equatorial radius of earth = 6378137m
+  double cosDelta = (sin(rNewLat) * sin(rOldLat)) +
+                    (cos(rNewLat) * cos(rOldLat) * cos(rOldLon - rNewLon));
+  if (cosDelta > 1.0) {
+    cosDelta = 1.0;
+  } else if (cosDelta < -1.0) {
+    cosDelta = -1.0;
+  }
+  return acos(cosDelta) * 6378137;
+}
+
new file mode 100644
--- /dev/null
+++ b/dom/system/gonk/GeolocationUtil.h
@@ -0,0 +1,13 @@
+/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef GEOLOCATIONUTIL_H
+#define GEOLOCATIONUTIL_H
+
+double CalculateDeltaInMeter(double aLat, double aLon, double aLastLat, double aLastLon);
+
+#endif
+
--- a/dom/system/gonk/GonkGPSGeolocationProvider.cpp
+++ b/dom/system/gonk/GonkGPSGeolocationProvider.cpp
@@ -15,16 +15,18 @@
  */
 
 #include "GonkGPSGeolocationProvider.h"
 #include "mozstumbler/MozStumbler.h"
 
 #include <pthread.h>
 #include <hardware/gps.h>
 
+#include "GeolocationUtil.h"
+#include "mozstumbler/MozStumbler.h"
 #include "mozilla/Constants.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Services.h"
 #include "nsContentUtils.h"
 #include "nsGeoPosition.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsINetworkInterface.h"
 #include "nsIObserverService.h"
@@ -83,81 +85,16 @@ NS_IMPL_ISUPPORTS(GonkGPSGeolocationProv
 /* static */ GonkGPSGeolocationProvider* GonkGPSGeolocationProvider::sSingleton = nullptr;
 GpsCallbacks GonkGPSGeolocationProvider::mCallbacks;
 
 #ifdef MOZ_B2G_RIL
 AGpsCallbacks GonkGPSGeolocationProvider::mAGPSCallbacks;
 AGpsRilCallbacks GonkGPSGeolocationProvider::mAGPSRILCallbacks;
 #endif // MOZ_B2G_RIL
 
-double CalculateDeltaInMeter(double aLat, double aLon, double aLastLat, double aLastLon)
-{
-  // Use spherical law of cosines to calculate difference
-  // Not quite as correct as the Haversine but simpler and cheaper
-  const double radsInDeg = M_PI / 180.0;
-  const double rNewLat = aLat * radsInDeg;
-  const double rNewLon = aLon * radsInDeg;
-  const double rOldLat = aLastLat * radsInDeg;
-  const double rOldLon = aLastLon * radsInDeg;
-  // WGS84 equatorial radius of earth = 6378137m
-  double cosDelta = (sin(rNewLat) * sin(rOldLat)) +
-                    (cos(rNewLat) * cos(rOldLat) * cos(rOldLon - rNewLon));
-  if (cosDelta > 1.0) {
-    cosDelta = 1.0;
-  } else if (cosDelta < -1.0) {
-    cosDelta = -1.0;
-  }
-  return acos(cosDelta) * 6378137;
-}
-
-class RequestCellInfoEvent : public nsRunnable {
-  public:
-    RequestCellInfoEvent(StumblerInfo *callback)
-      : mRequestCallback(callback)
-      {}
-
-    NS_IMETHOD Run() {
-      MOZ_ASSERT(NS_IsMainThread());
-      // Get Cell Info
-      nsCOMPtr<nsIMobileConnectionService> service =
-        do_GetService(NS_MOBILE_CONNECTION_SERVICE_CONTRACTID);
-
-      if (!service) {
-        nsContentUtils::LogMessageToConsole("Stumbler-can not get nsIMobileConnectionService \n");
-        return NS_OK;
-      }
-      nsCOMPtr<nsIMobileConnection> connection;
-      uint32_t numberOfRilServices = 1, cellInfoNum = 0;
-
-      service->GetNumItems(&numberOfRilServices);
-      for (uint32_t rilNum = 0; rilNum < numberOfRilServices; rilNum++) {
-        service->GetItemByServiceId(rilNum /* Client Id */, getter_AddRefs(connection));
-        if (!connection) {
-          nsContentUtils::LogMessageToConsole("Stumbler-can not get nsIMobileConnection by ServiceId %d \n", rilNum);
-        } else {
-          cellInfoNum++;
-          connection->GetCellInfoList(mRequestCallback);
-        }
-      }
-      mRequestCallback->SetCellInfoResponsesExpected(cellInfoNum);
-
-      // Get Wifi AP Info
-      nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1");
-      nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir);
-      if (!wifi) {
-        mRequestCallback->SetWifiInfoResponseReceived();
-        nsContentUtils::LogMessageToConsole("Stumbler-can not get nsIWifi interface\n");
-        return NS_OK;
-      }
-      wifi->GetWifiScanResults(mRequestCallback);
-      return NS_OK;
-    }
-  private:
-    nsRefPtr<StumblerInfo> mRequestCallback;
-};
 
 void
 GonkGPSGeolocationProvider::LocationCallback(GpsLocation* location)
 {
   if (gDebug_isGPSLocationIgnored) {
     return;
   }
 
@@ -206,45 +143,17 @@ GonkGPSGeolocationProvider::LocationCall
                                         location->latitude,
                                         location->longitude,
                                         location->accuracy);
   }
 
   nsRefPtr<UpdateLocationEvent> event = new UpdateLocationEvent(somewhere);
   NS_DispatchToMainThread(event);
 
-  const double kMinChangeInMeters = 30;
-  static int64_t lastTime_ms = 0;
-  static double sLastLat = 0;
-  static double sLastLon = 0;
-  double delta = -1.0;
-  int64_t timediff = (PR_Now() / PR_USEC_PER_MSEC) - lastTime_ms;
-
-  if (0 != sLastLon || 0 != sLastLat) {
-    delta = CalculateDeltaInMeter(location->latitude, location->longitude, sLastLat, sLastLon);
-  }
-  if (gDebug_isLoggingEnabled) {
-    nsContentUtils::LogMessageToConsole("Stumbler-Location. [%f , %f] time_diff:%lld, delta : %f\n",
-      location->longitude, location->latitude, timediff, delta);
-  }
-
-  // Consecutive GPS locations must be 30 meters and 3 seconds apart
-  if (lastTime_ms == 0 || ((timediff >= STUMBLE_INTERVAL_MS) && (delta > kMinChangeInMeters))){
-    lastTime_ms = (PR_Now() / PR_USEC_PER_MSEC);
-    sLastLat = location->latitude;
-    sLastLon = location->longitude;
-    nsRefPtr<StumblerInfo> requestCallback = new StumblerInfo(somewhere);
-    nsRefPtr<RequestCellInfoEvent> runnable = new RequestCellInfoEvent(requestCallback);
-    NS_DispatchToMainThread(runnable);
-  } else {
-    if (gDebug_isLoggingEnabled) {
-      nsContentUtils::LogMessageToConsole(
-        "Stumbler-GPS locations less than 30 meters and 3 seconds. Ignore!\n");
-    }
-  }
+  MozStumble(somewhere);
 }
 
 void
 GonkGPSGeolocationProvider::StatusCallback(GpsStatus* status)
 {
   if (gDebug_isLoggingEnabled) {
     switch (status->status) {
       case GPS_STATUS_NONE:
--- a/dom/system/gonk/moz.build
+++ b/dom/system/gonk/moz.build
@@ -28,26 +28,28 @@ XPIDL_SOURCES += [
     'nsIVolumeService.idl',
     'nsIVolumeStat.idl',
     'nsIWorkerHolder.idl',
 ]
 
 XPIDL_MODULE = 'dom_system_gonk'
 
 EXPORTS += [
+    'GeolocationUtil.h',
     'GonkGPSGeolocationProvider.h',
     'mozstumbler/MozStumbler.h',
     'nsVolume.h',
     'nsVolumeService.h',
 ]
 UNIFIED_SOURCES += [
     'AudioChannelManager.cpp',
     'AudioManager.cpp',
     'AutoMounter.cpp',
     'AutoMounterSetting.cpp',
+    'GeolocationUtil.cpp',
     'GonkGPSGeolocationProvider.cpp',
     'MozMtpDatabase.cpp',
     'MozMtpServer.cpp',
     'MozMtpStorage.cpp',
     'mozstumbler/MozStumbler.cpp',
     'mozstumbler/StumblerLogging.cpp',
     'mozstumbler/UploadStumbleRunnable.cpp',
     'mozstumbler/WriteStumbleOnThread.cpp',
--- a/dom/system/gonk/mozstumbler/MozStumbler.cpp
+++ b/dom/system/gonk/mozstumbler/MozStumbler.cpp
@@ -1,28 +1,123 @@
 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
 /* vim: set ts=2 et sw=2 tw=80: */
 /* 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/. */
 
 #include "MozStumbler.h"
+#include "nsDataHashtable.h"
 #include "nsGeoPosition.h"
+#include "nsNetCID.h"
 #include "nsPrintfCString.h"
 #include "StumblerLogging.h"
 #include "WriteStumbleOnThread.h"
-#include "nsNetCID.h"
-#include "nsDataHashtable.h"
+#include "../GeolocationUtil.h"
+
+#include "nsIInterfaceRequestor.h"
+#include "nsIInterfaceRequestorUtils.h"
+#include "nsIMobileConnectionInfo.h"
+#include "nsIMobileConnectionService.h"
+#include "nsIMobileCellInfo.h"
+#include "nsIMobileNetworkInfo.h"
+#include "nsINetworkInterface.h"
+#include "nsIRadioInterfaceLayer.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 
 NS_IMPL_ISUPPORTS(StumblerInfo, nsICellInfoListCallback, nsIWifiScanResultsReady)
 
+class RequestCellInfoEvent : public nsRunnable {
+public:
+  RequestCellInfoEvent(StumblerInfo *callback)
+  : mRequestCallback(callback)
+  {}
+
+  NS_IMETHOD Run() {
+    MOZ_ASSERT(NS_IsMainThread());
+    // Get Cell Info
+    nsCOMPtr<nsIMobileConnectionService> service =
+    do_GetService(NS_MOBILE_CONNECTION_SERVICE_CONTRACTID);
+
+    if (!service) {
+      STUMBLER_ERR("Stumbler-can not get nsIMobileConnectionService \n");
+      return NS_OK;
+    }
+    nsCOMPtr<nsIMobileConnection> connection;
+    uint32_t numberOfRilServices = 1, cellInfoNum = 0;
+
+    service->GetNumItems(&numberOfRilServices);
+    for (uint32_t rilNum = 0; rilNum < numberOfRilServices; rilNum++) {
+      service->GetItemByServiceId(rilNum /* Client Id */, getter_AddRefs(connection));
+      if (!connection) {
+        STUMBLER_ERR("Stumbler-can not get nsIMobileConnection by ServiceId %d \n", rilNum);
+      } else {
+        cellInfoNum++;
+        connection->GetCellInfoList(mRequestCallback);
+      }
+    }
+    mRequestCallback->SetCellInfoResponsesExpected(cellInfoNum);
+
+    // Get Wifi AP Info
+    nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1");
+    nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir);
+    if (!wifi) {
+      mRequestCallback->SetWifiInfoResponseReceived();
+      STUMBLER_ERR("Stumbler-can not get nsIWifi interface\n");
+      return NS_OK;
+    }
+    wifi->GetWifiScanResults(mRequestCallback);
+    return NS_OK;
+  }
+private:
+  nsRefPtr<StumblerInfo> mRequestCallback;
+};
+
+void
+MozStumble(nsGeoPosition* position)
+{
+  nsCOMPtr<nsIDOMGeoPositionCoords> coords;
+  position->GetCoords(getter_AddRefs(coords));
+  if (!coords) {
+    return;
+  }
+
+  double latitude, longitude;
+  coords->GetLatitude(&latitude);
+  coords->GetLongitude(&longitude);
+
+  const double kMinChangeInMeters = 30;
+  static int64_t lastTime_ms = 0;
+  static double sLastLat = 0;
+  static double sLastLon = 0;
+  double delta = -1.0;
+  int64_t timediff = (PR_Now() / PR_USEC_PER_MSEC) - lastTime_ms;
+
+  if (0 != sLastLon || 0 != sLastLat) {
+    delta = CalculateDeltaInMeter(latitude, longitude, sLastLat, sLastLon);
+  }
+  STUMBLER_DBG("Stumbler-Location. [%f , %f] time_diff:%lld, delta : %f\n",
+                                      longitude, latitude, timediff, delta);
+
+  // Consecutive GPS locations must be 30 meters and 3 seconds apart
+  if (lastTime_ms == 0 || ((timediff >= STUMBLE_INTERVAL_MS) && (delta > kMinChangeInMeters))){
+    lastTime_ms = (PR_Now() / PR_USEC_PER_MSEC);
+    sLastLat = latitude;
+    sLastLon = longitude;
+    nsRefPtr<StumblerInfo> requestCallback = new StumblerInfo(position);
+    nsRefPtr<RequestCellInfoEvent> runnable = new RequestCellInfoEvent(requestCallback);
+    NS_DispatchToMainThread(runnable);
+  } else {
+    STUMBLER_DBG("Stumbler-GPS locations less than 30 meters and 3 seconds. Ignore!\n");
+  }
+}
+
 void
 StumblerInfo::SetWifiInfoResponseReceived()
 {
   mIsWifiInfoResponseReceived = true;
 
   if (mIsWifiInfoResponseReceived && mCellInfoResponsesReceived == mCellInfoResponsesExpected) {
     STUMBLER_DBG("Call DumpStumblerInfo from SetWifiInfoResponseReceived\n");
     DumpStumblerInfo();
--- a/dom/system/gonk/mozstumbler/MozStumbler.h
+++ b/dom/system/gonk/mozstumbler/MozStumbler.h
@@ -10,16 +10,18 @@
 #include "nsIDOMEventTarget.h"
 #include "nsICellInfo.h"
 #include "nsIWifi.h"
 
 #define STUMBLE_INTERVAL_MS 3000
 
 class nsGeoPosition;
 
+void MozStumble(nsGeoPosition* position);
+
 class StumblerInfo final : public nsICellInfoListCallback,
                            public nsIWifiScanResultsReady
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSICELLINFOLISTCALLBACK
   NS_DECL_NSIWIFISCANRESULTSREADY