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.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Corporation
|
|
|
|
* 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"
|
|
|
|
#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
|
|
|
|
2008-09-12 21:47:45 -07:00
|
|
|
#ifdef NS_MAEMO_LOCATION
|
2008-07-15 16:37:48 -07:00
|
|
|
#include "MaemoLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMGeoPositionError::GetMessage(nsAString & aMessage)
|
|
|
|
{
|
2008-10-20 12:37:10 -07:00
|
|
|
aMessage.Truncate();
|
2008-08-14 09:35:49 -07:00
|
|
|
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),
|
|
|
|
mHasSentData(PR_FALSE),
|
|
|
|
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);
|
2008-11-12 07:59:38 -08:00
|
|
|
return NS_ERROR_FAILURE;;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsGeolocationRequest)
|
|
|
|
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
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsGeolocationRequest)
|
|
|
|
NS_IMPL_RELEASE(nsGeolocationRequest)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (!mHasSentData) {
|
2008-11-12 08:31:25 -08:00
|
|
|
NotifyError(nsIDOMGeoPositionError::TIMEOUT);
|
2008-10-20 12:37:10 -07:00
|
|
|
// remove ourselves from the locator's callback lists.
|
|
|
|
mLocator->RemoveRequest(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetRequestingURI(nsIURI * *aRequestingURI)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingURI);
|
|
|
|
*aRequestingURI = mLocator->GetURI();
|
|
|
|
NS_IF_ADDREF(*aRequestingURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetRequestingWindow(nsIDOMWindow * *aRequestingWindow)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingWindow);
|
|
|
|
*aRequestingWindow = mLocator->GetOwner();
|
|
|
|
NS_IF_ADDREF(*aRequestingWindow);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// Kick off the geo device, if it isn't already running
|
2008-08-14 09:35:49 -07:00
|
|
|
nsRefPtr<nsGeolocationService> geoService = nsGeolocationService::GetInstance();
|
|
|
|
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;
|
|
|
|
}
|
2008-08-14 09:35:49 -07:00
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
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);
|
2008-08-14 09:35:49 -07:00
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
mAllowed = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocationService::nsGeolocationService()
|
2008-11-12 07:59:38 -08:00
|
|
|
: mProviderStarted(PR_FALSE)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "quit-application", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimeout = nsContentUtils::GetIntPref("geo.timeout", 6000);
|
2008-11-12 07:59:38 -08:00
|
|
|
|
|
|
|
mProvider = do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
|
|
|
|
|
|
|
|
// if NS_MAEMO_LOCATION, see if we should try the MAEMO location provider
|
|
|
|
#ifdef NS_MAEMO_LOCATION
|
|
|
|
if (!mProvider)
|
|
|
|
mProvider = new MaemoLocationProvider();
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
PRBool
|
|
|
|
nsGeolocationService::HasGeolocationProvider()
|
|
|
|
{
|
|
|
|
return (mProvider != nsnull);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (!mProvider)
|
2008-11-12 07:59:38 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
if (!mProviderStarted) {
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// if we have one, start it up.
|
|
|
|
nsresult rv = mProvider->Startup();
|
|
|
|
if (NS_FAILED(rv))
|
2008-08-14 09:35:49 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// lets monitor it for any changes.
|
|
|
|
mProvider->Watch(this);
|
2008-11-12 07:59:38 -08:00
|
|
|
|
|
|
|
// remember that we are started up
|
|
|
|
mProviderStarted = PR_TRUE;
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// we do not want to keep the geolocation devices online
|
|
|
|
// indefinitely. Close them down after a reasonable period of
|
|
|
|
// inactivivity
|
|
|
|
SetDisconnectTimer();
|
2008-11-12 07:59:38 -08:00
|
|
|
|
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
|
|
|
{
|
|
|
|
if (mProvider) {
|
|
|
|
mProvider->Shutdown();
|
2008-11-12 07:59:38 -08:00
|
|
|
mProviderStarted = PR_FALSE;
|
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.");
|
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();
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_ADDREF(inst);
|
|
|
|
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
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsGeolocation)
|
|
|
|
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
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
NS_IMPL_ADDREF(nsGeolocation)
|
|
|
|
NS_IMPL_RELEASE(nsGeolocation)
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocation::nsGeolocation(nsIDOMWindow* aContentDom)
|
2008-07-15 16:37:48 -07:00
|
|
|
: mUpdateInProgress(PR_FALSE)
|
|
|
|
{
|
|
|
|
// Remember the window
|
2008-10-20 12:37:10 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContentDom);
|
2008-07-15 16:37:48 -07:00
|
|
|
if (window)
|
|
|
|
mOwner = window->GetCurrentInnerWindow();
|
|
|
|
|
|
|
|
// Grab the uri of the document
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
2008-10-20 12:37:10 -07:00
|
|
|
aContentDom->GetDocument(getter_AddRefs(domdoc));
|
2008-07-15 16:37:48 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
|
|
|
|
if (doc)
|
|
|
|
doc->NodePrincipal()->GetURI(getter_AddRefs(mURI));
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
mService = nsGeolocationService::GetInstance();
|
2008-07-15 16:37:48 -07:00
|
|
|
if (mService)
|
|
|
|
mService->AddLocator(this);
|
|
|
|
}
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::~nsGeolocation()
|
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;
|
|
|
|
mOwner = 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;
|
|
|
|
if (!OwnerStillExists())
|
|
|
|
{
|
|
|
|
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::GetLastPosition(nsIDOMGeoPosition * *aLastPosition)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
// we are advocating that this method be removed.
|
|
|
|
NS_ENSURE_ARG_POINTER(aLastPosition);
|
|
|
|
*aLastPosition = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-10-23 11:01:32 -07:00
|
|
|
nsCOMPtr<nsIGeolocationPrompt> prompt = do_GetService(NS_GEOLOCATION_PROMPT_CONTRACTID);
|
2008-07-15 16:37:48 -07:00
|
|
|
if (prompt == nsnull)
|
|
|
|
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()))
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
prompt->Prompt(request);
|
|
|
|
|
|
|
|
// What if you have a location provider that only sends a location once, then stops.? fix.
|
2008-10-20 12:37:10 -07:00
|
|
|
mPendingCallbacks.AppendElement(request);
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-20 12:37:10 -07:00
|
|
|
nsGeolocation::WatchPosition(nsIDOMGeoPositionCallback *aCallback,
|
|
|
|
nsIDOMGeoPositionErrorCallback *aErrorCallback,
|
|
|
|
nsIDOMGeoPositionOptions *aOptions,
|
2008-11-12 08:01:40 -08:00
|
|
|
PRInt32 *_retval NS_OUTPARAM)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2008-10-23 11:01:32 -07:00
|
|
|
nsCOMPtr<nsIGeolocationPrompt> prompt = do_GetService(NS_GEOLOCATION_PROMPT_CONTRACTID);
|
2008-07-15 16:37:48 -07:00
|
|
|
if (prompt == nsnull)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2008-11-12 08:00:37 -08:00
|
|
|
if (mWatchingCallbacks.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, aCallback, aErrorCallback, aOptions);
|
|
|
|
if (!request)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2008-11-12 07:59:38 -08:00
|
|
|
if (NS_FAILED(request->Init()))
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
prompt->Prompt(request);
|
|
|
|
|
|
|
|
// need to hand back an index/reference.
|
2008-10-20 12:37:10 -07:00
|
|
|
mWatchingCallbacks.AppendElement(request);
|
|
|
|
*_retval = mWatchingCallbacks.Length() - 1;
|
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();
|
2008-11-12 14:52:15 -08:00
|
|
|
if (aWatchId < 0 || count == 0 || aWatchId > count)
|
2008-11-12 08:01:40 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
mWatchingCallbacks[aWatchId]->MarkCleared();
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::OwnerStillExists()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
if (!mOwner)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindowInternal> domWindow(mOwner);
|
|
|
|
if (domWindow)
|
|
|
|
{
|
|
|
|
PRBool closed = PR_FALSE;
|
|
|
|
domWindow->GetClosed(&closed);
|
|
|
|
if (closed)
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* outer = mOwner->GetOuterWindow();
|
|
|
|
if (!outer || outer->GetCurrentInnerWindow() != mOwner)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|