Bug 478923 - Maemo Geolocation Provider support to support latest Geolocation specification. r/sr=jst
authorDoug Turner <dougt@meer.net>
Wed, 25 Feb 2009 20:33:24 -0800
changeset 25529 1b9db4b7c6049705e8aad30ef1ecc8e93e1c8a66
parent 25528 d10b74e2d200ba56ec92b4465e44cdddd95b9ed7
child 25530 0fa543fc5ed5ea14bd72cbfa021cf7ebe115a0ba
push id5593
push userdougt@mozilla.com
push dateThu, 26 Feb 2009 04:33:40 +0000
treeherdermozilla-central@1b9db4b7c604 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs478923
milestone1.9.2a1pre
Bug 478923 - Maemo Geolocation Provider support to support latest Geolocation specification. r/sr=jst
dom/public/nsDOMClassInfoID.h
dom/src/base/nsDOMClassInfo.cpp
dom/src/geolocation/MaemoLocationProvider.cpp
dom/src/geolocation/MaemoLocationProvider.h
dom/src/geolocation/nsGeolocation.cpp
--- a/dom/public/nsDOMClassInfoID.h
+++ b/dom/public/nsDOMClassInfoID.h
@@ -428,16 +428,17 @@ enum nsDOMClassInfoID {
 
   // event used for cross-domain message-passing and for server-sent events in
   // HTML5
   eDOMClassInfo_MessageEvent_id,
 
   // Geolocation
   eDOMClassInfo_GeoGeolocation_id,
   eDOMClassInfo_GeoPosition_id,
+  eDOMClassInfo_GeoPositionCoords_id,
   eDOMClassInfo_GeoPositionError_id,
 
   // @font-face in CSS
   eDOMClassInfo_CSSFontFaceRule_id,
   eDOMClassInfo_CSSFontFaceStyleDecl_id,
 
   // WhatWG Video Element
   eDOMClassInfo_HTMLVideoElement_id,
--- a/dom/src/base/nsDOMClassInfo.cpp
+++ b/dom/src/base/nsDOMClassInfo.cpp
@@ -456,16 +456,17 @@
 
 // Offline includes
 #include "nsIDOMLoadStatus.h"
 #include "nsIDOMLoadStatusEvent.h"
 
 // Geolocation
 #include "nsIDOMGeoGeolocation.h"
 #include "nsIDOMGeoPosition.h"
+#include "nsIDOMGeoPositionCoords.h"
 #include "nsIDOMGeoPositionError.h"
 
 // Workers
 #include "nsDOMWorker.h"
 
 #include "nsDOMFile.h"
 #include "nsIDOMFileException.h"
 
@@ -1274,16 +1275,19 @@ static nsDOMClassInfoData sClassInfoData
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
 
   NS_DEFINE_CLASSINFO_DATA(GeoGeolocation, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   
   NS_DEFINE_CLASSINFO_DATA(GeoPosition, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS) 
   
+  NS_DEFINE_CLASSINFO_DATA(GeoPositionCoords, nsDOMGenericSH,
+                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
+
   NS_DEFINE_CLASSINFO_DATA(GeoPositionError, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   
   NS_DEFINE_CLASSINFO_DATA(CSSFontFaceRule, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(CSSFontFaceStyleDecl, nsCSSStyleDeclSH,
                            ARRAY_SCRIPTABLE_FLAGS)
 
@@ -3545,16 +3549,20 @@ nsDOMClassInfo::Init()
   DOM_CLASSINFO_MAP_BEGIN(GeoGeolocation, nsIDOMGeoGeolocation)
      DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoGeolocation)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(GeoPosition, nsIDOMGeoPosition)
      DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoPosition)
   DOM_CLASSINFO_MAP_END
 
+  DOM_CLASSINFO_MAP_BEGIN(GeoPositionCoords, nsIDOMGeoPositionCoords)
+     DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoPositionCoords)
+  DOM_CLASSINFO_MAP_END
+
   DOM_CLASSINFO_MAP_BEGIN(GeoPositionError, nsIDOMGeoPositionError)
      DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoPositionError)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(CSSFontFaceRule, nsIDOMCSSFontFaceRule)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFaceRule)
   DOM_CLASSINFO_MAP_END
 
--- a/dom/src/geolocation/MaemoLocationProvider.cpp
+++ b/dom/src/geolocation/MaemoLocationProvider.cpp
@@ -29,183 +29,261 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-
-
 #include "MaemoLocationProvider.h"
 #include "nsGeolocation.h"
 #include "nsIClassInfo.h"
 #include "nsDOMClassInfoID.h"
 
+////////////////////////////////////////////////////
+// nsGeoPositionCoords
+////////////////////////////////////////////////////
+
+class nsGeoPositionCoords : public nsIDOMGeoPositionCoords
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIDOMGEOPOSITIONCOORDS
+  
+  nsGeoPositionCoords(double aLat, double aLong,
+                      double aAlt, double aHError,
+                      double aVError, double aHeading,
+                      double aSpeed)
+    : mLat(aLat),
+      mLong(aLong),
+      mAlt(aAlt),
+      mHError(aHError),
+      mVError(aVError),
+      mHeading(aHeading),
+      mSpeed(aSpeed)
+  {
+  }
+  
+  
+  ~nsGeoPositionCoords(){}
+
+private:
+  double mLat, mLong, mAlt, mHError, mVError, mHeading, mSpeed;
+};
+
+NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords)
+NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords)
+NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords)
+NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(GeoPositionCoords)
+NS_INTERFACE_MAP_END
+
+NS_IMPL_ADDREF(nsGeoPositionCoords)
+NS_IMPL_RELEASE(nsGeoPositionCoords)
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetLatitude(double *aLatitude)
+{
+  *aLatitude = mLat;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetLongitude(double *aLongitude)
+{
+  *aLongitude = mLong;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetAltitude(double *aAltitude)
+{
+  *aAltitude = mAlt;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetAccuracy(double *aAccuracy)
+{
+  *aAccuracy = mHError;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetAltitudeAccuracy(double *aAltitudeAccuracy)
+{
+  *aAltitudeAccuracy = mVError;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetHeading(double *aHeading)
+{
+  *aHeading = mHeading;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGeoPositionCoords::GetSpeed(double *aSpeed)
+{
+  *aSpeed = mSpeed;
+  return NS_OK;
+}
 
 ////////////////////////////////////////////////////
 // nsGeoPosition
 ////////////////////////////////////////////////////
 
-/**
- * Simple object that holds a single point in space.
- */ 
 class nsGeoPosition : public nsIDOMGeoPosition
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIDOMGEOPOSITION
 
-    nsGeoPosition(double aLat, double aLong, double aAlt, double aHError, double aVError, double aHeading, double aSpeed, long long aTimestamp)
-    : mLat(aLat), mLong(aLong), mAlt(aAlt), mHError(aHError), mVError(aVError), mHeading(aHeading), mSpeed(aSpeed), mTimestamp(aTimestamp){};
+    nsGeoPosition(double aLat, double aLong,
+                  double aAlt, double aHError,
+                  double aVError, double aHeading,
+                  double aSpeed, long long aTimestamp)
+      : mLat(aLat),
+        mLong(aLong),
+        mAlt(aAlt),
+        mHError(aHError),
+        mVError(aVError),
+        mHeading(aHeading),
+        mSpeed(aSpeed),
+        mTimestamp(aTimestamp)
+  {
+  }
 
 private:
-  ~nsGeoPosition(){}
+  ~nsGeoPosition()
+  {
+  }
   double mLat, mLong, mAlt, mHError, mVError, mHeading, mSpeed;
   long long mTimestamp;
+  nsRefPtr<nsGeoPositionCoords> mCoords;
 };
 
 NS_INTERFACE_MAP_BEGIN(nsGeoPosition)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition)
   NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(GeoPosition)
 NS_INTERFACE_MAP_END
 
-NS_IMPL_THREADSAFE_ADDREF(nsGeoPosition)
-NS_IMPL_THREADSAFE_RELEASE(nsGeoPosition)
-
-NS_IMETHODIMP
-nsGeoPosition::GetLatitude(double *aLatitude)
-{
-  *aLatitude = mLat;
-  return NS_OK;
-}
+NS_IMPL_ADDREF(nsGeoPosition)
+NS_IMPL_RELEASE(nsGeoPosition)
 
 NS_IMETHODIMP
-nsGeoPosition::GetLongitude(double *aLongitude)
+nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords * *aCoords)
 {
-  *aLongitude = mLong;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsGeoPosition::GetAltitude(double *aAltitude)
-{
-  *aAltitude = mAlt;
+  if (mCoords == nsnull)
+    mCoords = new nsGeoPositionCoords(mLat,
+                                      mLong,
+                                      mAlt,
+                                      mHError,
+                                      mVError,
+                                      mHeading,
+                                      mSpeed);
+  NS_IF_ADDREF(*aCoords = mCoords);
   return NS_OK;
 }
 
-NS_IMETHODIMP
-nsGeoPosition::GetAccuracy(double *aAccuracy)
-{
-  *aAccuracy = mHError;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsGeoPosition::GetAltitudeAccuracy(double *aAltitudeAccuracy)
-{
-  *aAltitudeAccuracy = mVError;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsGeoPosition::GetHeading(double *aHeading)
-{
-  *aHeading = mHeading;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsGeoPosition::GetSpeed(double *aSpeed)
-{
-  *aSpeed = mSpeed;
-  return NS_OK;
-}
 
 NS_IMETHODIMP
 nsGeoPosition::GetTimestamp(DOMTimeStamp* aTimestamp)
 {
   *aTimestamp = mTimestamp;
   return NS_OK;
 }
 
 
 NS_IMPL_ISUPPORTS1(MaemoLocationProvider, nsIGeolocationProvider)
 
 MaemoLocationProvider::MaemoLocationProvider()
-: mGPSDevice(nsnull), mLocationCallbackHandle(0), mHasSeenLocation(PR_FALSE)
+: mGPSDevice(nsnull),
+  mHasSeenLocation(PR_FALSE),
+  mLastSeenTime(0)
 {
 }
 
 MaemoLocationProvider::~MaemoLocationProvider()
 {
 }
 
-void location_changed (LocationGPSDevice *device, gpointer userdata)
+static void location_changed(LocationGPSDevice *device, gpointer userdata)
 {
+  if (!userdata || !device || !device->fix) {
+    return;
+  }
+
+  //  printf ("Latitude: %.5f\nLongitude: %.5f\nAltitude: %.2f\n",
+  //	  device->fix->latitude, device->fix->longitude, device->fix->altitude);
+
   MaemoLocationProvider* provider = (MaemoLocationProvider*) userdata;
   nsRefPtr<nsGeoPosition> somewhere = new nsGeoPosition(device->fix->latitude,
                                                         device->fix->longitude,
                                                         device->fix->altitude,
-                                                        device->fix->eph,
-                                                        device->fix->epv,
-                                                        0,0,
-							device->fix->time);
+                                                        0,
+                                                        0,
+                                                        0,
+                                                        0,
+                                                        PR_Now());
   provider->Update(somewhere);
 }
 
 NS_IMETHODIMP MaemoLocationProvider::Startup()
 {
   if (!mGPSDevice)
   {
-    // if we are already started, don't do anything
-    memset(&mGPSBT, 0, sizeof(gpsbt_t));
-    int result = gpsbt_start(NULL, 0, 0, 0, NULL, 0, 0, &mGPSBT);
-    if (result <0)
-      return NS_ERROR_NOT_AVAILABLE;
-    
     mGPSDevice = (LocationGPSDevice*) g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
-    mLocationCallbackHandle = g_signal_connect (mGPSDevice, "changed", G_CALLBACK (location_changed), this->mCallback);
+
+    mCallbackChanged = g_signal_connect(mGPSDevice, 
+                                        "changed",
+                                        G_CALLBACK(location_changed),
+                                        this);
   }
+
   return NS_OK;
 }
 
 NS_IMETHODIMP MaemoLocationProvider::IsReady(PRBool *_retval NS_OUTPARAM)
 {
   *_retval = mHasSeenLocation;
   return NS_OK;
 }
 
 NS_IMETHODIMP MaemoLocationProvider::Watch(nsIGeolocationUpdate *callback)
 {
-  mCallback = callback; // weak ref
-  return NS_OK;
-}
+  if (mCallback)
+    return NS_OK;
 
-/* readonly attribute nsIDOMGeoPosition currentPosition; */
-NS_IMETHODIMP MaemoLocationProvider::GetCurrentPosition(nsIDOMGeoPosition * *aCurrentPosition)
-{
-  NS_IF_ADDREF(*aCurrentPosition = mLastPosition);
+  mCallback = callback;
   return NS_OK;
 }
 
 NS_IMETHODIMP MaemoLocationProvider::Shutdown()
 {
-  if (mGPSDevice && mLocationCallbackHandle) {
-    g_signal_handler_disconnect(mGPSDevice, mLocationCallbackHandle);
+  if (mGPSDevice)
+  {
+    g_signal_handler_disconnect(mGPSDevice, mCallbackChanged);
+
     g_object_unref(mGPSDevice);
-    gpsbt_stop(&mGPSBT);
-    mLocationCallbackHandle = 0;
     mGPSDevice = nsnull;
+
     mHasSeenLocation = PR_FALSE;
+    
+    mCallback = nsnull;
   }
   return NS_OK;
 }
 
 void MaemoLocationProvider::Update(nsIDOMGeoPosition* aPosition)
 {
+  if ((PR_Now() - mLastSeenTime) / PR_USEC_PER_SEC < 5)
+    return;
+
   mHasSeenLocation = PR_TRUE;
-  mLastPosition = aPosition;
+
   if (mCallback)
     mCallback->Update(aPosition);
+
+  mLastSeenTime = PR_Now();
 }
--- a/dom/src/geolocation/MaemoLocationProvider.h
+++ b/dom/src/geolocation/MaemoLocationProvider.h
@@ -31,16 +31,17 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsIGeolocationProvider.h"
 #include "nsIDOMGeoPosition.h"
+
 #include "nsCOMPtr.h"
 
 #include <glib.h>
 #include <errno.h>
 #include <gpsbt.h>
 #include <gpsmgr.h>
 
 extern "C" {
@@ -58,18 +59,18 @@ public:
 
   MaemoLocationProvider();
 
   void Update(nsIDOMGeoPosition* aPosition);
 
 private:
   ~MaemoLocationProvider();
 
-  nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
+  nsCOMPtr<nsIGeolocationUpdate> mCallback;
 
-  nsIGeolocationUpdate* mCallback; // weak reference by contract.
-
+  LocationGPSDevice *mGPSDevice;
 
-  gpsbt_t mGPSBT;
-  LocationGPSDevice *mGPSDevice;
-  gulong mLocationCallbackHandle;
+  gulong mCallbackChanged;
+
   PRBool mHasSeenLocation;
+  PRTime mLastSeenTime;
+
 };
--- a/dom/src/geolocation/nsGeolocation.cpp
+++ b/dom/src/geolocation/nsGeolocation.cpp
@@ -632,17 +632,16 @@ nsGeolocation::Update(nsIDOMGeoPosition 
   mUpdateInProgress = PR_TRUE;
 
   if (aSomewhere)
   {
     nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
     geoService->SetCachedPosition(aSomewhere);
   }
 
-
   if (!OwnerStillExists())
   {
     Shutdown();
     return;
   }
 
   // notify anyone that has been waiting
   for (PRUint32 i = 0; i< mPendingCallbacks.Length(); i++)