2008-07-15 16:37:48 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Geolocation.
|
|
|
|
*
|
2009-12-21 13:50:30 -08:00
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation
|
2008-07-15 16:37:48 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@meer.net> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* 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 "nsGeolocation.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2009-07-10 17:04:39 -07:00
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch2.h"
|
2008-09-10 08:52:46 -07:00
|
|
|
#include "nsIJSContextStack.h"
|
2008-08-14 09:35:49 -07:00
|
|
|
|
|
|
|
#include <math.h>
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-03-24 20:55:17 -07:00
|
|
|
#ifdef WINCE_WINDOWS_MOBILE
|
2009-03-14 21:10:35 -07:00
|
|
|
#include "WinMobileLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2010-04-02 00:56:22 -07:00
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
#include "MaemoLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocument.h"
|
2008-08-14 09:35:49 -07:00
|
|
|
|
2008-11-12 08:00:37 -08:00
|
|
|
// Some limit to the number of get or watch geolocation requests
|
|
|
|
// that a window can make.
|
|
|
|
#define MAX_GEO_REQUESTS_PER_WINDOW 1500
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// nsDOMGeoPositionError
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsDOMGeoPositionError : public nsIDOMGeoPositionError
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOMGEOPOSITIONERROR
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsDOMGeoPositionError(PRInt16 aCode);
|
|
|
|
void NotifyCallback(nsIDOMGeoPositionErrorCallback* callback);
|
2008-08-14 09:35:49 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsDOMGeoPositionError();
|
|
|
|
PRInt16 mCode;
|
|
|
|
};
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(GeoPositionError, nsDOMGeoPositionError)
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMGeoPositionError)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionError)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionError)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(GeoPositionError)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsDOMGeoPositionError)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsDOMGeoPositionError)
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsDOMGeoPositionError::nsDOMGeoPositionError(PRInt16 aCode)
|
|
|
|
: mCode(aCode)
|
2008-08-14 09:35:49 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMGeoPositionError::~nsDOMGeoPositionError(){}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMGeoPositionError::GetCode(PRInt16 *aCode)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCode);
|
|
|
|
*aCode = mCode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
void
|
|
|
|
nsDOMGeoPositionError::NotifyCallback(nsIDOMGeoPositionErrorCallback* aCallback)
|
|
|
|
{
|
|
|
|
if (!aCallback)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure that the proper context is on the stack (bug 452762)
|
|
|
|
nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1"));
|
|
|
|
if (!stack || NS_FAILED(stack->Push(nsnull)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
aCallback->HandleEvent(this);
|
|
|
|
|
|
|
|
// remove the stack
|
|
|
|
JSContext* cx;
|
|
|
|
stack->Pop(&cx);
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// nsGeolocationRequest
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationRequest::nsGeolocationRequest(nsGeolocation* aLocator,
|
|
|
|
nsIDOMGeoPositionCallback* aCallback,
|
|
|
|
nsIDOMGeoPositionErrorCallback* aErrorCallback,
|
|
|
|
nsIDOMGeoPositionOptions* aOptions)
|
|
|
|
: mAllowed(PR_FALSE),
|
|
|
|
mCleared(PR_FALSE),
|
2010-02-23 23:11:18 -08:00
|
|
|
mHasSentData(PR_FALSE),
|
2008-10-20 12:37:10 -07:00
|
|
|
mCallback(aCallback),
|
|
|
|
mErrorCallback(aErrorCallback),
|
|
|
|
mOptions(aOptions),
|
|
|
|
mLocator(aLocator)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGeolocationRequest::~nsGeolocationRequest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
nsresult
|
|
|
|
nsGeolocationRequest::Init()
|
|
|
|
{
|
|
|
|
// This method is called before the user has given permission for this request.
|
|
|
|
|
|
|
|
// check to see if we have a geolocation provider, if not, notify an error and bail.
|
|
|
|
nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
|
|
|
|
if (!geoService->HasGeolocationProvider()) {
|
2008-11-12 08:31:25 -08:00
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
2009-08-28 10:40:27 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-11-12 07:59:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsGeolocationRequest)
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGeolocationRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGeolocationRequest)
|
2008-10-20 12:37:10 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsGeolocationRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsGeolocationRequest)
|
|
|
|
|
2009-06-11 17:46:46 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_4(nsGeolocationRequest, mCallback, mErrorCallback, mOptions, mLocator)
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::NotifyError(PRInt16 errorCode)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsDOMGeoPositionError> positionError = new nsDOMGeoPositionError(errorCode);
|
|
|
|
if (!positionError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
positionError->NotifyCallback(mErrorCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
// If we haven't gotten an answer from the geolocation
|
|
|
|
// provider yet, cancel the request. Same logic as
|
|
|
|
// ::Cancel, just a different error
|
|
|
|
|
2010-02-23 23:11:18 -08:00
|
|
|
if (!mHasSentData) {
|
|
|
|
NotifyError(nsIDOMGeoPositionError::TIMEOUT);
|
|
|
|
// remove ourselves from the locator's callback lists.
|
|
|
|
mLocator->RemoveRequest(this);
|
|
|
|
}
|
2008-10-20 12:37:10 -07:00
|
|
|
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetRequestingURI(nsIURI * *aRequestingURI)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingURI);
|
2009-07-10 17:02:32 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri = mLocator->GetURI();
|
|
|
|
uri.forget(aRequestingURI);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetRequestingWindow(nsIDOMWindow * *aRequestingWindow)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingWindow);
|
2009-07-10 17:02:32 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_QueryReferent(mLocator->GetOwner());
|
|
|
|
window.forget(aRequestingWindow);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Cancel()
|
|
|
|
{
|
2008-11-12 08:31:25 -08:00
|
|
|
NotifyError(nsIDOMGeoPositionError::PERMISSION_DENIED);
|
2008-10-20 12:37:10 -07:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// remove ourselves from the locators callback lists.
|
|
|
|
mLocator->RemoveRequest(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Allow()
|
|
|
|
{
|
2008-08-14 09:35:49 -07:00
|
|
|
nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
|
2008-12-05 08:29:43 -08:00
|
|
|
|
|
|
|
// Kick off the geo device, if it isn't already running
|
2008-08-14 09:35:49 -07:00
|
|
|
nsresult rv = geoService->StartDevice();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2008-10-20 12:37:10 -07:00
|
|
|
// Location provider error
|
2008-11-12 08:31:25 -08:00
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
2008-10-20 12:37:10 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-01-07 09:41:57 -08:00
|
|
|
|
|
|
|
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) {
|
2009-01-07 09:43:56 -08:00
|
|
|
PRInt32 tempAge;
|
2009-01-07 09:41:57 -08:00
|
|
|
nsresult rv = mOptions->GetMaximumAge(&tempAge);
|
2009-01-07 09:43:56 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-14 16:12:09 -07:00
|
|
|
if (tempAge >= 0)
|
2009-01-07 09:43:56 -08:00
|
|
|
maximumAge = tempAge;
|
|
|
|
}
|
2009-01-07 09:41:57 -08:00
|
|
|
}
|
|
|
|
|
2010-04-02 14:49:38 -07:00
|
|
|
if (lastPosition && maximumAge > 0 &&
|
|
|
|
( (PR_Now() / PR_USEC_PER_MSEC) - maximumAge <=
|
|
|
|
PRTime(cachedPositionTime) )) {
|
2009-01-07 09:41:57 -08:00
|
|
|
// okay, we can return a cached position
|
|
|
|
mAllowed = PR_TRUE;
|
|
|
|
|
|
|
|
// send the cached location
|
|
|
|
SendLocation(lastPosition);
|
|
|
|
}
|
2008-08-14 09:35:49 -07:00
|
|
|
|
2009-01-07 09:43:56 -08:00
|
|
|
PRInt32 timeout;
|
2008-10-20 12:37:10 -07:00
|
|
|
if (mOptions && NS_SUCCEEDED(mOptions->GetTimeout(&timeout)) && timeout > 0) {
|
2009-01-07 09:41:57 -08:00
|
|
|
|
|
|
|
if (timeout < 10)
|
|
|
|
timeout = 10;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-01-07 09:41:57 -08:00
|
|
|
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
2010-02-23 23:11:18 -08:00
|
|
|
|
|
|
|
mAllowed = PR_TRUE;
|
|
|
|
return NS_OK;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::MarkCleared()
|
|
|
|
{
|
|
|
|
mCleared = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-08-01 14:09:34 -07:00
|
|
|
if (mCleared || !mAllowed)
|
2008-07-15 16:37:48 -07:00
|
|
|
return;
|
|
|
|
|
2008-11-12 08:05:32 -08:00
|
|
|
// we should not pass null back to the DOM.
|
|
|
|
if (!aPosition) {
|
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-10 08:52:46 -07:00
|
|
|
// Ensure that the proper context is on the stack (bug 452762)
|
|
|
|
nsCOMPtr<nsIJSContextStack> stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1"));
|
|
|
|
if (!stack || NS_FAILED(stack->Push(nsnull)))
|
|
|
|
return; // silently fail
|
|
|
|
|
2008-11-12 08:04:18 -08:00
|
|
|
mCallback->HandleEvent(aPosition);
|
2008-09-10 08:52:46 -07:00
|
|
|
|
|
|
|
// remove the stack
|
|
|
|
JSContext* cx;
|
|
|
|
stack->Pop(&cx);
|
2008-10-20 12:37:10 -07:00
|
|
|
|
2010-02-23 23:11:18 -08:00
|
|
|
mHasSentData = PR_TRUE;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::Shutdown()
|
|
|
|
{
|
|
|
|
mCleared = PR_TRUE;
|
|
|
|
mCallback = nsnull;
|
2008-08-14 09:35:49 -07:00
|
|
|
mErrorCallback = nsnull;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
2008-08-14 09:35:49 -07:00
|
|
|
// nsGeolocationService
|
2008-07-15 16:37:48 -07:00
|
|
|
////////////////////////////////////////////////////
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsGeolocationService)
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGeolocationUpdate)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGeolocationUpdate)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsGeolocationService)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsGeolocationService)
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-05-06 18:25:44 -07:00
|
|
|
|
|
|
|
static PRBool sGeoEnabled = PR_TRUE;
|
|
|
|
static int
|
|
|
|
GeoEnabledChangedCallback(const char *aPrefName, void *aClosure)
|
|
|
|
{
|
|
|
|
sGeoEnabled = nsContentUtils::GetBoolPref("geo.enabled", PR_TRUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-28 10:40:27 -07:00
|
|
|
nsresult nsGeolocationService::Init()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
mTimeout = nsContentUtils::GetIntPref("geo.timeout", 6000);
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2009-05-06 18:25:44 -07:00
|
|
|
nsContentUtils::RegisterPrefCallback("geo.enabled",
|
|
|
|
GeoEnabledChangedCallback,
|
|
|
|
nsnull);
|
|
|
|
|
|
|
|
GeoEnabledChangedCallback("geo.enabled", nsnull);
|
|
|
|
|
2010-02-07 07:52:43 -08:00
|
|
|
if (!sGeoEnabled)
|
2009-08-28 10:40:27 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-04-14 09:10:20 -07:00
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
nsCOMPtr<nsIGeolocationProvider> provider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
|
|
|
|
|
|
|
// look up any providers that were registered via the category manager
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
|
|
|
|
if (!catMan)
|
2009-08-28 10:40:27 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-07-10 17:04:39 -07:00
|
|
|
|
2009-10-25 22:50:16 -07:00
|
|
|
// geolocation service can be enabled -> now register observer
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (!obs)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
obs->AddObserver(this, "quit-application", false);
|
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> geoproviders;
|
|
|
|
catMan->EnumerateCategory("geolocation-provider", getter_AddRefs(geoproviders));
|
|
|
|
if (geoproviders) {
|
|
|
|
|
|
|
|
PRBool hasMore;
|
|
|
|
while (NS_SUCCEEDED(geoproviders->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> elem;
|
|
|
|
geoproviders->GetNext(getter_AddRefs(elem));
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> elemString = do_QueryInterface(elem);
|
|
|
|
|
|
|
|
nsCAutoString name;
|
|
|
|
elemString->GetData(name);
|
|
|
|
|
|
|
|
nsXPIDLCString spec;
|
|
|
|
catMan->GetCategoryEntry("geolocation-provider", name.get(), getter_Copies(spec));
|
|
|
|
|
|
|
|
provider = do_GetService(spec);
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we should move these providers outside of this file! dft
|
|
|
|
|
2009-03-24 20:55:17 -07:00
|
|
|
#ifdef WINCE_WINDOWS_MOBILE
|
2009-07-10 17:04:39 -07:00
|
|
|
provider = new WinMobileLocationProvider();
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
2009-03-14 21:10:35 -07:00
|
|
|
#endif
|
2010-04-02 00:56:22 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
provider = new MaemoLocationProvider();
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
|
|
|
#endif
|
2009-08-28 10:40:27 -07:00
|
|
|
return NS_OK;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService::~nsGeolocationService()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationService::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
if (!strcmp("quit-application", aTopic))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "quit-application");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
|
|
|
|
mGeolocators[i]->Shutdown();
|
|
|
|
|
|
|
|
StopDevice();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp("timer-callback", aTopic))
|
|
|
|
{
|
|
|
|
// decide if we can close down the service.
|
|
|
|
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
|
|
|
|
if (mGeolocators[i]->HasActiveCallbacks())
|
|
|
|
{
|
|
|
|
SetDisconnectTimer();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay to close up.
|
|
|
|
StopDevice();
|
|
|
|
Update(nsnull);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationService::Update(nsIDOMGeoPosition *aSomewhere)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2009-07-10 17:04:39 -07:00
|
|
|
// here we have to determine this aSomewhere is a "better"
|
|
|
|
// position than any previously recv'ed.
|
|
|
|
|
|
|
|
if (!IsBetterPosition(aSomewhere))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
SetCachedPosition(aSomewhere);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
|
2008-10-20 12:37:10 -07:00
|
|
|
mGeolocators[i]->Update(aSomewhere);
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
PRBool
|
|
|
|
nsGeolocationService::IsBetterPosition(nsIDOMGeoPosition *aSomewhere)
|
2010-02-23 23:11:18 -08:00
|
|
|
{
|
2009-07-10 17:04:39 -07:00
|
|
|
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
|
2009-09-10 07:47:32 -07:00
|
|
|
// 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;
|
2009-07-10 17:04:39 -07:00
|
|
|
|
2009-09-10 07:47:32 -07:00
|
|
|
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;
|
2009-07-10 17:04:39 -07:00
|
|
|
|
|
|
|
// The threshold is when the distance between the two positions exceeds the
|
|
|
|
// worse (larger value) of the two accuracies.
|
2010-02-20 05:59:07 -08:00
|
|
|
double max_accuracy = NS_MAX(oldAccuracy, newAccuracy);
|
2009-07-10 17:04:39 -07:00
|
|
|
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;
|
|
|
|
}
|
2008-12-05 08:29:43 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationService::SetCachedPosition(nsIDOMGeoPosition* aPosition)
|
|
|
|
{
|
|
|
|
mLastPosition = aPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMGeoPosition*
|
|
|
|
nsGeolocationService::GetCachedPosition()
|
|
|
|
{
|
|
|
|
return mLastPosition;
|
|
|
|
}
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
PRBool
|
|
|
|
nsGeolocationService::HasGeolocationProvider()
|
|
|
|
{
|
2009-07-10 17:04:39 -07:00
|
|
|
return mProviders.Count() > 0;
|
2008-11-12 07:59:38 -08:00
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
nsresult
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService::StartDevice()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2010-02-07 07:52:43 -08:00
|
|
|
if (!sGeoEnabled)
|
2009-05-06 18:25:44 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
if (!HasGeolocationProvider())
|
2008-11-12 07:59:38 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
// if we have one, start it up.
|
2009-07-10 17:04:39 -07:00
|
|
|
|
|
|
|
// Start them up!
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
for (PRUint32 i = mProviders.Count() - 1; i != PRUint32(-1); --i) {
|
|
|
|
// If any provder gets started without error, go ahead
|
|
|
|
// and proceed without error
|
|
|
|
nsresult temp = mProviders[i]->Startup();
|
|
|
|
if (NS_SUCCEEDED(temp)) {
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
mProviders[i]->Watch(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2009-04-14 09:10:20 -07:00
|
|
|
// we do not want to keep the geolocation devices online
|
|
|
|
// indefinitely. Close them down after a reasonable period of
|
|
|
|
// inactivivity
|
|
|
|
SetDisconnectTimer();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService::SetDisconnectTimer()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
if (!mDisconnectTimer)
|
|
|
|
mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
else
|
|
|
|
mDisconnectTimer->Cancel();
|
|
|
|
|
|
|
|
mDisconnectTimer->Init(this,
|
|
|
|
mTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService::StopDevice()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2009-07-10 17:04:39 -07:00
|
|
|
for (PRUint32 i = mProviders.Count() - 1; i != PRUint32(-1); --i) {
|
|
|
|
mProviders[i]->Shutdown();
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mDisconnectTimer) {
|
|
|
|
mDisconnectTimer->Cancel();
|
|
|
|
mDisconnectTimer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService* nsGeolocationService::gService = nsnull;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService*
|
|
|
|
nsGeolocationService::GetInstance()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-08-14 09:35:49 -07:00
|
|
|
if (!nsGeolocationService::gService) {
|
|
|
|
nsGeolocationService::gService = new nsGeolocationService();
|
2008-10-20 12:37:10 -07:00
|
|
|
NS_ASSERTION(nsGeolocationService::gService, "null nsGeolocationService.");
|
2009-08-28 10:40:27 -07:00
|
|
|
|
|
|
|
if (nsGeolocationService::gService) {
|
|
|
|
if (NS_FAILED(nsGeolocationService::gService->Init())) {
|
|
|
|
delete nsGeolocationService::gService;
|
|
|
|
nsGeolocationService::gService = nsnull;
|
|
|
|
}
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
2008-08-14 09:35:49 -07:00
|
|
|
return nsGeolocationService::gService;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService*
|
|
|
|
nsGeolocationService::GetGeolocationService()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService* inst = nsGeolocationService::GetInstance();
|
2009-08-28 10:40:27 -07:00
|
|
|
NS_IF_ADDREF(inst);
|
2008-07-15 16:37:48 -07:00
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationService::AddLocator(nsGeolocation* aLocator)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-10-20 12:37:10 -07:00
|
|
|
mGeolocators.AppendElement(aLocator);
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocationService::RemoveLocator(nsGeolocation* aLocator)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-10-20 12:37:10 -07:00
|
|
|
mGeolocators.RemoveElement(aLocator);
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
2008-08-14 09:35:49 -07:00
|
|
|
// nsGeolocation
|
2008-07-15 16:37:48 -07:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(GeoGeolocation, nsGeolocation)
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsGeolocation)
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoGeolocation)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMGeoGeolocation)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(GeoGeolocation)
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsGeolocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsGeolocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsGeolocation)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGeolocation)
|
|
|
|
tmp->mPendingCallbacks.Clear();
|
|
|
|
tmp->mWatchingCallbacks.Clear();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsGeolocation)
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < tmp->mPendingCallbacks.Length(); ++i)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mPendingCallbacks[i], nsIGeolocationRequest)
|
|
|
|
|
|
|
|
for (i = 0; i < tmp->mWatchingCallbacks.Length(); ++i)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mWatchingCallbacks[i], nsIGeolocationRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsGeolocation::nsGeolocation()
|
2008-07-15 16:37:48 -07:00
|
|
|
: mUpdateInProgress(PR_FALSE)
|
2009-11-12 07:14:45 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGeolocation::~nsGeolocation()
|
|
|
|
{
|
|
|
|
if (mService)
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGeolocation::Init(nsIDOMWindow* aContentDom)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
// Remember the window
|
2009-11-12 07:14:45 -08:00
|
|
|
if (aContentDom) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContentDom);
|
|
|
|
if (!window)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
mOwner = do_GetWeakReference(window->GetCurrentInnerWindow());
|
2009-11-12 07:14:45 -08:00
|
|
|
if (!mOwner)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Grab the uri of the document
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
aContentDom->GetDocument(getter_AddRefs(domdoc));
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
|
|
|
|
if (!doc)
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
doc->NodePrincipal()->GetURI(getter_AddRefs(mURI));
|
2009-11-12 07:14:45 -08:00
|
|
|
|
|
|
|
if (!mURI)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
// If no aContentDom was passed into us, we are being used
|
|
|
|
// by chrome/c++ and have no mOwner, no mURI, and no need
|
|
|
|
// to prompt.
|
2008-08-14 09:35:49 -07:00
|
|
|
mService = nsGeolocationService::GetInstance();
|
2008-07-15 16:37:48 -07:00
|
|
|
if (mService)
|
|
|
|
mService->AddLocator(this);
|
2009-11-12 09:17:21 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::Shutdown()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
// Shutdown and release all callbacks
|
2008-10-20 12:37:10 -07:00
|
|
|
for (PRUint32 i = 0; i< mPendingCallbacks.Length(); i++)
|
2008-07-15 16:37:48 -07:00
|
|
|
mPendingCallbacks[i]->Shutdown();
|
|
|
|
mPendingCallbacks.Clear();
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
for (PRUint32 i = 0; i< mWatchingCallbacks.Length(); i++)
|
2008-07-15 16:37:48 -07:00
|
|
|
mWatchingCallbacks[i]->Shutdown();
|
|
|
|
mWatchingCallbacks.Clear();
|
|
|
|
|
|
|
|
if (mService)
|
|
|
|
mService->RemoveLocator(this);
|
|
|
|
|
|
|
|
mService = nsnull;
|
|
|
|
mURI = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::HasActiveCallbacks()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-10-20 12:37:10 -07:00
|
|
|
return mWatchingCallbacks.Length() != 0;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocation::RemoveRequest(nsGeolocationRequest* aRequest)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-10-20 12:37:10 -07:00
|
|
|
mPendingCallbacks.RemoveElement(aRequest);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// if it is in the mWatchingCallbacks, we can't do much
|
|
|
|
// since we passed back the position in the array to who
|
|
|
|
// ever called WatchPosition() and we do not want to mess
|
|
|
|
// around with the ordering of the array. Instead, just
|
|
|
|
// mark the request as "cleared".
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
aRequest->MarkCleared();
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
// This method calls out to objects which may spin and
|
|
|
|
// event loop which may add new location objects into
|
|
|
|
// mPendingCallbacks, and mWatchingCallbacks. Since this
|
|
|
|
// function can only be called on the primary thread, we
|
|
|
|
// can lock this method with a member var.
|
|
|
|
|
|
|
|
if (mUpdateInProgress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mUpdateInProgress = PR_TRUE;
|
2008-12-05 08:29:43 -08:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
if (!WindowOwnerStillExists())
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify anyone that has been waiting
|
2008-10-20 12:37:10 -07:00
|
|
|
for (PRUint32 i = 0; i< mPendingCallbacks.Length(); i++)
|
|
|
|
mPendingCallbacks[i]->SendLocation(aSomewhere);
|
2008-07-15 16:37:48 -07:00
|
|
|
mPendingCallbacks.Clear();
|
|
|
|
|
|
|
|
// notify everyone that is watching
|
2008-10-20 12:37:10 -07:00
|
|
|
for (PRUint32 i = 0; i< mWatchingCallbacks.Length(); i++)
|
|
|
|
mWatchingCallbacks[i]->SendLocation(aSomewhere);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
mUpdateInProgress = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::GetCurrentPosition(nsIDOMGeoPositionCallback *callback,
|
|
|
|
nsIDOMGeoPositionErrorCallback *errorCallback,
|
|
|
|
nsIDOMGeoPositionOptions *options)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2009-07-16 10:03:49 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(callback);
|
|
|
|
|
2010-02-07 07:52:43 -08:00
|
|
|
if (!sGeoEnabled)
|
2009-05-06 18:25:44 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2008-11-12 08:00:37 -08:00
|
|
|
if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, callback, errorCallback, options);
|
|
|
|
if (!request)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
if (NS_FAILED(request->Init()))
|
2009-11-12 07:14:45 -08:00
|
|
|
return NS_ERROR_FAILURE; // this as OKAY. not sure why we wouldn't throw. xxx dft
|
|
|
|
|
|
|
|
if (mOwner) {
|
|
|
|
nsCOMPtr<nsIGeolocationPrompt> prompt = do_GetService(NS_GEOLOCATION_PROMPT_CONTRACTID);
|
|
|
|
if (prompt == nsnull)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
prompt->Prompt(request);
|
|
|
|
|
|
|
|
mPendingCallbacks.AppendElement(request);
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
return NS_OK;
|
2009-11-12 07:14:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsCallerChrome())
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
request->Allow();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
mPendingCallbacks.AppendElement(request);
|
2009-11-12 07:14:45 -08:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-12 07:14:45 -08:00
|
|
|
nsGeolocation::WatchPosition(nsIDOMGeoPositionCallback *callback,
|
|
|
|
nsIDOMGeoPositionErrorCallback *errorCallback,
|
|
|
|
nsIDOMGeoPositionOptions *options,
|
2008-11-12 08:01:40 -08:00
|
|
|
PRInt32 *_retval NS_OUTPARAM)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2009-07-16 10:03:49 -07:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(callback);
|
2009-05-06 18:25:44 -07:00
|
|
|
|
2010-02-07 07:52:43 -08:00
|
|
|
if (!sGeoEnabled)
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW)
|
2008-11-12 08:00:37 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this, callback, errorCallback, options);
|
2008-10-20 12:37:10 -07:00
|
|
|
if (!request)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
if (NS_FAILED(request->Init()))
|
2009-11-12 07:14:45 -08:00
|
|
|
return NS_ERROR_FAILURE; // this as OKAY. not sure why we wouldn't throw. xxx dft
|
|
|
|
|
|
|
|
if (mOwner) {
|
|
|
|
nsCOMPtr<nsIGeolocationPrompt> prompt = do_GetService(NS_GEOLOCATION_PROMPT_CONTRACTID);
|
|
|
|
if (prompt == nsnull)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
prompt->Prompt(request);
|
|
|
|
|
|
|
|
// need to hand back an index/reference.
|
|
|
|
mWatchingCallbacks.AppendElement(request);
|
|
|
|
*_retval = mWatchingCallbacks.Length() - 1;
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
return NS_OK;
|
2009-11-12 07:14:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsContentUtils::IsCallerChrome())
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
request->Allow();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// need to hand back an index/reference.
|
2008-10-20 12:37:10 -07:00
|
|
|
mWatchingCallbacks.AppendElement(request);
|
|
|
|
*_retval = mWatchingCallbacks.Length() - 1;
|
2009-11-12 07:14:45 -08:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-11-12 08:01:40 -08:00
|
|
|
nsGeolocation::ClearWatch(PRInt32 aWatchId)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-11-12 08:01:40 -08:00
|
|
|
PRUint32 count = mWatchingCallbacks.Length();
|
2010-04-02 14:49:38 -07:00
|
|
|
if (aWatchId < 0 || count == 0 || PRUint32(aWatchId) > count)
|
2009-11-13 09:19:45 -08:00
|
|
|
return NS_OK;
|
2008-11-12 08:01:40 -08:00
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
mWatchingCallbacks[aWatchId]->MarkCleared();
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2009-11-12 07:14:45 -08:00
|
|
|
nsGeolocation::WindowOwnerStillExists()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2009-11-12 07:14:45 -08:00
|
|
|
// an owner was never set when nsGeolocation
|
|
|
|
// was created, which means that this object
|
|
|
|
// is being used without a window.
|
|
|
|
if (mOwner == nsnull)
|
|
|
|
return PR_TRUE;
|
2009-07-10 17:02:32 -07:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
if (window)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
PRBool closed = PR_FALSE;
|
2009-07-10 17:02:32 -07:00
|
|
|
window->GetClosed(&closed);
|
2008-07-15 16:37:48 -07:00
|
|
|
if (closed)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsPIDOMWindow* outer = window->GetOuterWindow();
|
|
|
|
if (!outer || outer->GetCurrentInnerWindow() != window)
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2010-01-12 05:08:43 -08:00
|
|
|
|
|
|
|
#ifndef WINCE_WINDOWS_MOBILE
|
|
|
|
DOMCI_DATA(GeoPositionCoords, void)
|
|
|
|
DOMCI_DATA(GeoPosition, void)
|
|
|
|
#endif
|