Bug 643429 - Remove IsBetterLocation functionality. r=jst
authorDoug Turner <dougt@dougt.org>
Wed, 23 Mar 2011 10:27:48 -0700
changeset 63549 7c996666a93016a30fa3bd1750731a1604742500
parent 63548 995bf49d8c54b66b6f9d2e5147aeb0fcc3961524
child 63550 4dcf1c5c0c3fc901cc6a43df0db71d47e3681e65
push idunknown
push userunknown
push dateunknown
reviewersjst
bugs643429
milestone2.2a1pre
Bug 643429 - Remove IsBetterLocation functionality. r=jst
dom/src/geolocation/nsGeolocation.cpp
dom/src/geolocation/nsGeolocation.h
--- a/dom/src/geolocation/nsGeolocation.cpp
+++ b/dom/src/geolocation/nsGeolocation.cpp
@@ -456,26 +456,19 @@ nsGeolocationRequest::SendLocation(nsIDO
   JSContext* cx;
   stack->Pop(&cx);
 
   if (mIsWatchPositionRequest)
     SetTimeoutTimer();
 }
 
 void
-nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition, PRBool isBetter)
+nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition)
 {
-  // Only dispatch callbacks if this is the first position for this request, or
-  // if the accuracy is as good or improving.
-  //
-  // This ensures that all listeners get at least one position callback, particularly
-  // in the case when newly detected positions are all less accurate than the cached one.
-  //
-  // Fixes bug 596481
-  if (mIsFirstUpdate || isBetter) {
+  if (mIsFirstUpdate) {
     mIsFirstUpdate = PR_FALSE;
     nsCOMPtr<nsIRunnable> ev  = new RequestSendLocationEvent(aPosition, this);
     NS_DispatchToMainThread(ev);
   }
 }
 
 void
 nsGeolocationRequest::Shutdown()
@@ -653,110 +646,23 @@ nsGeolocationService::Observe(nsISupport
   }
 
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsGeolocationService::Update(nsIDOMGeoPosition *aSomewhere)
 {
-  // here we have to determine this aSomewhere is a "better"
-  // position than any previously recv'ed.
-
-  PRBool isBetter = IsBetterPosition(aSomewhere);
-  if (isBetter) {
-    SetCachedPosition(aSomewhere);
-  }
+  SetCachedPosition(aSomewhere);
 
   for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
-    mGeolocators[i]->Update(aSomewhere, isBetter);
+    mGeolocators[i]->Update(aSomewhere);
   return NS_OK;
 }
 
-PRBool
-nsGeolocationService::IsBetterPosition(nsIDOMGeoPosition *aSomewhere)
-{
-  if (!aSomewhere)
-    return PR_FALSE;
-
-  nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
-  if (!geoService)
-    return PR_FALSE;
-
-  nsCOMPtr<nsIDOMGeoPosition> lastPosition = geoService->GetCachedPosition();
-  if (!lastPosition)
-    return PR_TRUE;
-
-  nsresult rv;
-  DOMTimeStamp oldTime;
-  rv = lastPosition->GetTimestamp(&oldTime);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  nsCOMPtr<nsIDOMGeoPositionCoords> coords;
-  lastPosition->GetCoords(getter_AddRefs(coords));
-  if (!coords)
-    return PR_FALSE;
-
-  double oldAccuracy;
-  rv = coords->GetAccuracy(&oldAccuracy);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  double oldLat, oldLon;
-  rv = coords->GetLongitude(&oldLon);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  rv = coords->GetLatitude(&oldLat);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-
-  DOMTimeStamp newTime;
-  rv = aSomewhere->GetTimestamp(&newTime);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  aSomewhere->GetCoords(getter_AddRefs(coords));
-  if (!coords)
-    return PR_FALSE;
-
-  double newAccuracy;
-  rv = coords->GetAccuracy(&newAccuracy);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  double newLat, newLon;
-  rv = coords->GetLongitude(&newLon);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  rv = coords->GetLatitude(&newLat);
-  NS_ENSURE_SUCCESS(rv, PR_FALSE);
-
-  // check to see if there has been a large movement
-  // Use spherical law of cosines to calculate difference
-  // Not quite as correct as the Haversine but simpler and cheaper
-  double radsInDeg = 3.14159265 / 180.0;
-
-  double rNewLat = newLat * radsInDeg;
-  double rNewLon = newLon * radsInDeg;
-  double rOldLat = oldLat * radsInDeg;
-  double rOldLon = oldLon * radsInDeg;
-
-  // WGS84 equatorial radius of earth = 6378137m
-  double delta = acos( (sin(rNewLat) * sin(rOldLat)) + (cos(rNewLat) * cos(rOldLat) * cos(rOldLon - rNewLon)) ) * 6378137; 
-
-  // The threshold is when the distance between the two positions exceeds the
-  // worse (larger value) of the two accuracies.
-  double max_accuracy = NS_MAX(oldAccuracy, newAccuracy);
-  if (delta > max_accuracy)
-    return PR_TRUE;
-
-  // check to see if the aSomewhere position is more accurate
-  if (oldAccuracy >= newAccuracy)
-    return PR_TRUE;
-
-  return PR_FALSE;
-}
-
 void
 nsGeolocationService::SetCachedPosition(nsIDOMGeoPosition* aPosition)
 {
   mLastPosition = aPosition;
 }
 
 nsIDOMGeoPosition*
 nsGeolocationService::GetCachedPosition()
@@ -994,29 +900,29 @@ nsGeolocation::RemoveRequest(nsGeolocati
   // ever called WatchPosition() and we do not want to mess
   // around with the ordering of the array.  Instead, just
   // mark the request as "cleared".
 
   aRequest->MarkCleared();
 }
 
 void
-nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere, PRBool isBetter)
+nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere)
 {
   if (!WindowOwnerStillExists())
     return Shutdown();
 
   for (PRUint32 i = 0; i< mPendingCallbacks.Length(); i++) {
-    mPendingCallbacks[i]->Update(aSomewhere, isBetter);
+    mPendingCallbacks[i]->Update(aSomewhere);
   }
   mPendingCallbacks.Clear();
 
   // notify everyone that is watching
   for (PRUint32 i = 0; i< mWatchingCallbacks.Length(); i++) {
-    mWatchingCallbacks[i]->Update(aSomewhere, isBetter);
+    mWatchingCallbacks[i]->Update(aSomewhere);
   }
 }
 
 NS_IMETHODIMP
 nsGeolocation::GetCurrentPosition(nsIDOMGeoPositionCallback *callback,
                                   nsIDOMGeoPositionErrorCallback *errorCallback,
                                   nsIDOMGeoPositionOptions *options)
 {
--- a/dom/src/geolocation/nsGeolocation.h
+++ b/dom/src/geolocation/nsGeolocation.h
@@ -92,18 +92,17 @@ class nsGeolocationRequest
                        nsIDOMGeoPositionCallback* callback,
                        nsIDOMGeoPositionErrorCallback* errorCallback,
                        nsIDOMGeoPositionOptions* options,
                        PRBool watchPositionRequest = PR_FALSE);
   nsresult Init();
   void Shutdown();
 
   // Called by the geolocation device to notify that a location has changed.
-  // isBetter: the accuracy is as good or better than the previous position. 
-  void Update(nsIDOMGeoPosition* aPosition, PRBool isBetter);
+  void Update(nsIDOMGeoPosition* aPosition);
 
   void SendLocation(nsIDOMGeoPosition* location);
   void MarkCleared();
   PRBool IsActive() {return !mCleared;}
   PRBool Allowed() {return mAllowed;}
   void SetTimeoutTimer();
 
   ~nsGeolocationRequest();
@@ -147,18 +146,16 @@ public:
   nsGeolocationService() {mTimeout = 6000;};
 
   nsresult Init();
 
   // Management of the nsGeolocation objects
   void AddLocator(nsGeolocation* locator);
   void RemoveLocator(nsGeolocation* locator);
 
-  PRBool IsBetterPosition(nsIDOMGeoPosition* aPosition);
-
   void SetCachedPosition(nsIDOMGeoPosition* aPosition);
   nsIDOMGeoPosition* GetCachedPosition();
 
   // Find and startup a geolocation device (gps, nmea, etc.)
   nsresult StartDevice();
 
   // Stop the started geolocation device (gps, nmea, etc.)
   void     StopDevice();
@@ -203,18 +200,17 @@ public:
 
   NS_DECL_CYCLE_COLLECTION_CLASS(nsGeolocation)
 
   nsGeolocation();
 
   nsresult Init(nsIDOMWindow* contentDom=nsnull);
 
   // Called by the geolocation device to notify that a location has changed.
-  // isBetter: the accuracy is as good or better than the previous position. 
-  void Update(nsIDOMGeoPosition* aPosition, PRBool isBetter);
+  void Update(nsIDOMGeoPosition* aPosition);
 
   // Returns true if any of the callbacks are repeating
   PRBool HasActiveCallbacks();
 
   // Remove request from all callbacks arrays
   void RemoveRequest(nsGeolocationRequest* request);
 
   // Shutting down.