Bug 470072 - Some geolocation providers will not respond with a location for up to 1 minute. This will provide cached location if available. r/sr=jst

This commit is contained in:
Doug Turner 2009-01-07 09:41:57 -08:00
parent 556ecf0d25
commit 16e702e91f
2 changed files with 39 additions and 26 deletions

View File

@ -121,7 +121,7 @@ interface nsIGeolocationProvider : nsISupports {
/**
* shutdown
* Shuts down the location devices.
* Shuts down the location device.
*/
void shutdown();
};

View File

@ -238,27 +238,6 @@ nsGeolocationRequest::Allow()
{
nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
// check to see if we can use a cached value
PRUint32 maximumAge;
if (mOptions && NS_SUCCEEDED(mOptions->GetMaximumAge(&maximumAge)) && maximumAge != 0) {
nsCOMPtr<nsIDOMGeoPosition> lastPosition = geoService->GetCachedPosition();
DOMTimeStamp cachedPositionTime;
lastPosition->GetTimestamp(&cachedPositionTime);
if ( PR_Now() - maximumAge >= cachedPositionTime )
{
// okay, we can return a cached position
mAllowed = PR_TRUE;
// send the cached location
SendLocation(lastPosition);
// remove ourselves from the locators callback lists.
mLocator->RemoveRequest(this);
}
}
// Kick off the geo device, if it isn't already running
nsresult rv = geoService->StartDevice();
@ -267,13 +246,47 @@ nsGeolocationRequest::Allow()
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
return NS_OK;
}
nsCOMPtr<nsIDOMGeoPosition> lastPosition = geoService->GetCachedPosition();
DOMTimeStamp cachedPositionTime;
if (lastPosition)
lastPosition->GetTimestamp(&cachedPositionTime);
// check to see if we can use a cached value
//
// either:
// a) the user has specified a maximumAge which allows us to return a cached value,
// -or-
// b) the cached position time is some reasonable value to return to the user (<30s)
PRUint32 maximumAge = 30 * PR_MSEC_PER_SEC;
if (mOptions) {
PRUint32 tempAge;
nsresult rv = mOptions->GetMaximumAge(&tempAge);
if (NS_SUCCEEDED(rv))
maximumAge = tempAge;
}
if (lastPosition && maximumAge > 0 && ( (PR_Now() / PR_USEC_PER_MSEC ) - maximumAge <= cachedPositionTime) ) {
// okay, we can return a cached position
mAllowed = PR_TRUE;
// send the cached location
SendLocation(lastPosition);
// remove ourselves from the locators callback lists.
mLocator->RemoveRequest(this);
}
PRUint32 timeout;
if (mOptions && NS_SUCCEEDED(mOptions->GetTimeout(&timeout)) && timeout > 0) {
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
}
if (timeout < 10)
timeout = 10;
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
}
mAllowed = PR_TRUE;
return NS_OK;
@ -600,7 +613,7 @@ nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere)
mUpdateInProgress = PR_TRUE;
if (!aSomewhere)
if (aSomewhere)
{
nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
geoService->SetCachedPosition(aSomewhere);