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 ***** */
|
|
|
|
|
2010-09-13 12:49:27 -07:00
|
|
|
#include "nsContentPermissionHelper.h"
|
2010-05-13 10:44:51 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/dom/PBrowserChild.h"
|
|
|
|
#include "mozilla/dom/PBrowserParent.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2010-05-13 10:44:51 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "nsFrameManager.h"
|
|
|
|
#include "nsFrameLoader.h"
|
|
|
|
#include "nsIFrameLoader.h"
|
|
|
|
|
|
|
|
#include "nsIDocShellTreeOwner.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIWebProgressListener2.h"
|
|
|
|
|
|
|
|
#include "nsDOMEventTargetHelper.h"
|
|
|
|
#include "TabChild.h"
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
#include "nsGeolocation.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
#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"
|
2010-08-31 09:15:52 -07:00
|
|
|
#include "nsThreadUtils.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/unused.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2008-08-14 09:35:49 -07:00
|
|
|
|
|
|
|
#include <math.h>
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-04-14 02:42:43 -07:00
|
|
|
#ifdef MOZ_MAEMO_LIBLOCATION
|
2010-04-02 00:56:22 -07:00
|
|
|
#include "MaemoLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-15 17:25:43 -07:00
|
|
|
#ifdef MOZ_ENABLE_QTMOBILITY
|
|
|
|
#include "QTMLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "AndroidLocationProvider.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
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
using mozilla::unused; // <snicker>
|
2011-05-24 23:31:59 -07:00
|
|
|
using namespace mozilla;
|
2010-08-05 15:11:23 -07:00
|
|
|
using namespace mozilla::dom;
|
2010-07-19 11:33:33 -07:00
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
class RequestPromptEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RequestPromptEvent(nsGeolocationRequest* request)
|
|
|
|
: mRequest(request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2010-09-09 21:59:51 -07:00
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt = do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
|
|
|
if (prompt) {
|
2010-08-31 09:15:52 -07:00
|
|
|
prompt->Prompt(mRequest);
|
2010-09-09 21:59:51 -07:00
|
|
|
}
|
2010-08-31 09:15:52 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestAllowEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RequestAllowEvent(int allow, nsGeolocationRequest* request)
|
|
|
|
: mAllow(allow),
|
|
|
|
mRequest(request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
if (mAllow)
|
|
|
|
mRequest->Allow();
|
|
|
|
else
|
|
|
|
mRequest->Cancel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAllow;
|
2010-08-31 09:15:52 -07:00
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestSendLocationEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// a bit funky. if locator is passed, that means this
|
|
|
|
// event should remove the request from it. If we ever
|
|
|
|
// have to do more, then we can change this around.
|
2010-09-09 21:59:51 -07:00
|
|
|
RequestSendLocationEvent(nsIDOMGeoPosition* aPosition,
|
|
|
|
nsGeolocationRequest* aRequest,
|
2011-08-25 12:13:25 -07:00
|
|
|
nsGeolocation* aLocator)
|
2010-08-31 09:15:52 -07:00
|
|
|
: mPosition(aPosition),
|
|
|
|
mRequest(aRequest),
|
|
|
|
mLocator(aLocator)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mRequest->SendLocation(mPosition);
|
|
|
|
if (mLocator)
|
|
|
|
mLocator->RemoveRequest(mRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDOMGeoPosition> mPosition;
|
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
|
|
|
|
|
|
|
nsRefPtr<nsGeolocation> mLocator;
|
|
|
|
};
|
|
|
|
|
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,
|
2011-01-31 14:11:45 -08:00
|
|
|
nsIDOMGeoPositionOptions* aOptions,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aWatchPositionRequest)
|
2008-10-20 12:37:10 -07:00
|
|
|
: mAllowed(PR_FALSE),
|
|
|
|
mCleared(PR_FALSE),
|
2011-01-31 14:11:45 -08:00
|
|
|
mIsWatchPositionRequest(aWatchPositionRequest),
|
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.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsGeolocationRequest)
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
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-08-31 09:15:52 -07:00
|
|
|
// remove ourselves from the locator's callback lists.
|
|
|
|
mLocator->RemoveRequest(this);
|
2010-08-23 21:52:17 -07:00
|
|
|
NotifyError(nsIDOMGeoPositionError::TIMEOUT);
|
2008-10-20 12:37:10 -07:00
|
|
|
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_IMETHODIMP
|
2010-09-09 21:59:51 -07:00
|
|
|
nsGeolocationRequest::GetUri(nsIURI * *aRequestingURI)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
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
|
2010-09-09 21:59:51 -07:00
|
|
|
nsGeolocationRequest::GetType(nsACString & aType)
|
|
|
|
{
|
|
|
|
aType = "geolocation";
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetWindow(nsIDOMWindow * *aRequestingWindow)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
NS_IMETHODIMP
|
2010-09-09 21:59:51 -07:00
|
|
|
nsGeolocationRequest::GetElement(nsIDOMElement * *aRequestingElement)
|
2010-05-13 10:44:51 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingElement);
|
|
|
|
*aRequestingElement = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Cancel()
|
|
|
|
{
|
|
|
|
// remove ourselves from the locators callback lists.
|
|
|
|
mLocator->RemoveRequest(this);
|
2010-08-23 21:52:17 -07:00
|
|
|
|
|
|
|
NotifyError(nsIDOMGeoPositionError::PERMISSION_DENIED);
|
2008-07-15 16:37:48 -07:00
|
|
|
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 &&
|
2010-04-28 13:54:00 -07:00
|
|
|
( PRTime(PR_Now() / PR_USEC_PER_MSEC) - maximumAge <=
|
2010-04-02 14:49:38 -07:00
|
|
|
PRTime(cachedPositionTime) )) {
|
2009-01-07 09:41:57 -08:00
|
|
|
// okay, we can return a cached position
|
|
|
|
mAllowed = PR_TRUE;
|
|
|
|
|
2011-08-25 12:13:25 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new RequestSendLocationEvent(lastPosition, this,
|
|
|
|
mIsWatchPositionRequest ? nsnull : mLocator);
|
2010-08-31 09:15:52 -07:00
|
|
|
NS_DispatchToMainThread(ev);
|
2009-01-07 09:41:57 -08:00
|
|
|
}
|
2008-08-14 09:35:49 -07:00
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
SetTimeoutTimer();
|
|
|
|
|
|
|
|
mAllowed = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::SetTimeoutTimer()
|
|
|
|
{
|
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
}
|
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);
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::MarkCleared()
|
|
|
|
{
|
2011-01-04 13:25:04 -08:00
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
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;
|
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-01-31 14:11:45 -08:00
|
|
|
if (mIsWatchPositionRequest)
|
|
|
|
SetTimeoutTimer();
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 08:44:00 -07:00
|
|
|
bool
|
2011-03-23 10:27:48 -07:00
|
|
|
nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition)
|
2011-01-04 13:25:04 -08:00
|
|
|
{
|
2011-10-03 08:44:00 -07:00
|
|
|
if (!mAllowed)
|
|
|
|
return false;
|
2011-08-25 12:13:25 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new RequestSendLocationEvent(aPosition, this,
|
|
|
|
mIsWatchPositionRequest ? nsnull : mLocator);
|
2011-03-30 21:13:35 -07:00
|
|
|
NS_DispatchToMainThread(ev);
|
2011-10-03 08:44:00 -07:00
|
|
|
return true;
|
2011-01-04 13:25:04 -08:00
|
|
|
}
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
void
|
|
|
|
nsGeolocationRequest::Shutdown()
|
|
|
|
{
|
2010-08-23 19:09:00 -07:00
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
mTimeoutTimer = nsnull;
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
mCleared = PR_TRUE;
|
|
|
|
mCallback = nsnull;
|
2008-08-14 09:35:49 -07:00
|
|
|
mErrorCallback = nsnull;
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
bool nsGeolocationRequest::Recv__delete__(const bool& allow)
|
|
|
|
{
|
|
|
|
if (allow)
|
|
|
|
(void) Allow();
|
|
|
|
else
|
|
|
|
(void) Cancel();
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool sGeoEnabled = true;
|
|
|
|
static bool sGeoIgnoreLocationFilter = false;
|
2010-08-31 09:15:52 -07:00
|
|
|
|
2009-05-06 18:25:44 -07:00
|
|
|
static int
|
|
|
|
GeoEnabledChangedCallback(const char *aPrefName, void *aClosure)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
sGeoEnabled = Preferences::GetBool("geo.enabled", true);
|
2009-05-06 18:25:44 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
static int
|
|
|
|
GeoIgnoreLocationFilterChangedCallback(const char *aPrefName, void *aClosure)
|
|
|
|
{
|
2011-05-24 23:31:59 -07:00
|
|
|
sGeoIgnoreLocationFilter =
|
2011-09-28 23:19:26 -07:00
|
|
|
Preferences::GetBool("geo.ignore.location_filter", true);
|
2010-08-31 09:15:52 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-28 10:40:27 -07:00
|
|
|
nsresult nsGeolocationService::Init()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2011-05-24 23:32:00 -07:00
|
|
|
mTimeout = Preferences::GetInt("geo.timeout", 6000);
|
2008-11-12 07:59:38 -08:00
|
|
|
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::RegisterCallback(GeoIgnoreLocationFilterChangedCallback,
|
|
|
|
"geo.ignore.location_filter");
|
2010-08-31 09:15:52 -07:00
|
|
|
|
|
|
|
GeoIgnoreLocationFilterChangedCallback("geo.ignore.location_filter", nsnull);
|
|
|
|
|
|
|
|
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::RegisterCallback(GeoEnabledChangedCallback, "geo.enabled");
|
2009-05-06 18:25:44 -07:00
|
|
|
|
|
|
|
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
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2009-10-25 22:50:16 -07:00
|
|
|
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) {
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasMore;
|
2009-07-10 17:04:39 -07:00
|
|
|
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
|
|
|
|
|
2010-04-14 02:42:43 -07:00
|
|
|
#ifdef MOZ_MAEMO_LIBLOCATION
|
2010-04-02 00:56:22 -07:00
|
|
|
provider = new MaemoLocationProvider();
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
|
|
|
#endif
|
2010-06-04 14:14:43 -07:00
|
|
|
|
2011-08-15 17:25:43 -07:00
|
|
|
#ifdef MOZ_ENABLE_QTMOBILITY
|
|
|
|
provider = new QTMLocationProvider();
|
|
|
|
if (provider)
|
|
|
|
mProviders.AppendObject(provider);
|
|
|
|
#endif
|
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
provider = new AndroidLocationProvider();
|
|
|
|
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))
|
|
|
|
{
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2008-07-15 16:37:48 -07:00
|
|
|
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
|
|
|
{
|
2011-03-23 10:27:48 -07:00
|
|
|
SetCachedPosition(aSomewhere);
|
2009-07-10 17:04:39 -07:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
for (PRUint32 i = 0; i< mGeolocators.Length(); i++)
|
2011-03-23 10:27:48 -07:00
|
|
|
mGeolocators[i]->Update(aSomewhere);
|
2008-07-15 16:37:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-05 08:29:43 -08:00
|
|
|
void
|
|
|
|
nsGeolocationService::SetCachedPosition(nsIDOMGeoPosition* aPosition)
|
|
|
|
{
|
|
|
|
mLastPosition = aPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMGeoPosition*
|
|
|
|
nsGeolocationService::GetCachedPosition()
|
|
|
|
{
|
|
|
|
return mLastPosition;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2010-09-27 14:23:35 -07:00
|
|
|
// we do not want to keep the geolocation devices online
|
|
|
|
// indefinitely. Close them down after a reasonable period of
|
|
|
|
// inactivivity
|
|
|
|
SetDisconnectTimer();
|
|
|
|
|
2010-09-20 21:16:37 -07:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2010-11-05 10:43:13 -07:00
|
|
|
cpc->SendAddGeolocationListener();
|
2010-09-20 21:16:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-10 17:04:39 -07:00
|
|
|
// Start them up!
|
2010-09-27 14:23:35 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2010-10-14 09:12:57 -07:00
|
|
|
for (PRInt32 i = 0; i < mProviders.Count(); i++) {
|
2010-09-27 14:23:35 -07:00
|
|
|
mProviders[i]->Startup();
|
|
|
|
mProviders[i]->Watch(this);
|
|
|
|
obs->NotifyObservers(mProviders[i],
|
|
|
|
"geolocation-device-events",
|
|
|
|
NS_LITERAL_STRING("starting").get());
|
2009-07-10 17:04:39 -07:00
|
|
|
}
|
2010-10-14 09:12:57 -07: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(mDisconnectTimer) {
|
|
|
|
mDisconnectTimer->Cancel();
|
|
|
|
mDisconnectTimer = nsnull;
|
|
|
|
}
|
2010-09-20 21:16:37 -07:00
|
|
|
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2010-11-05 10:43:13 -07:00
|
|
|
cpc->SendRemoveGeolocationListener();
|
2010-09-20 21:16:37 -07:00
|
|
|
return; // bail early
|
|
|
|
}
|
|
|
|
|
2010-09-27 14:23:35 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs)
|
|
|
|
return;
|
|
|
|
|
2010-10-14 09:12:57 -07:00
|
|
|
for (PRInt32 i = 0; i < mProviders.Count(); i++) {
|
2010-09-20 21:16:37 -07:00
|
|
|
mProviders[i]->Shutdown();
|
2010-09-27 14:23:35 -07:00
|
|
|
obs->NotifyObservers(mProviders[i],
|
|
|
|
"geolocation-device-events",
|
|
|
|
NS_LITERAL_STRING("shutdown").get());
|
2010-09-20 21:16:37 -07:00
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
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)
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mPendingCallbacks[i], nsIContentPermissionRequest)
|
2009-01-14 17:10:46 -08:00
|
|
|
|
|
|
|
for (i = 0; i < tmp->mWatchingCallbacks.Length(); ++i)
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mWatchingCallbacks[i], nsIContentPermissionRequest)
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
nsGeolocation::nsGeolocation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-08-14 09:35:49 -07:00
|
|
|
nsGeolocation::HasActiveCallbacks()
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2010-09-27 14:23:35 -07:00
|
|
|
for (PRUint32 i = 0; i < mWatchingCallbacks.Length(); i++)
|
|
|
|
if (mWatchingCallbacks[i]->IsActive())
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
return PR_FALSE;
|
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
|
2011-03-23 10:27:48 -07:00
|
|
|
nsGeolocation::Update(nsIDOMGeoPosition *aSomewhere)
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
2010-08-04 17:36:54 -07:00
|
|
|
if (!WindowOwnerStillExists())
|
2010-08-31 09:15:52 -07:00
|
|
|
return Shutdown();
|
2010-08-04 17:36:54 -07:00
|
|
|
|
2011-10-03 08:44:00 -07:00
|
|
|
for (PRUint32 i = mPendingCallbacks.Length(); i> 0; i--) {
|
|
|
|
if (mPendingCallbacks[i-1]->Update(aSomewhere))
|
|
|
|
mPendingCallbacks.RemoveElementAt(i-1);
|
2010-08-31 09:15:52 -07:00
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// notify everyone that is watching
|
2010-08-31 09:15:52 -07:00
|
|
|
for (PRUint32 i = 0; i< mWatchingCallbacks.Length(); i++) {
|
2011-03-23 10:27:48 -07:00
|
|
|
mWatchingCallbacks[i]->Update(aSomewhere);
|
2010-08-31 09:15:52 -07:00
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-01-31 14:11:45 -08:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
|
|
|
|
callback,
|
|
|
|
errorCallback,
|
|
|
|
options,
|
|
|
|
PR_FALSE);
|
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) {
|
2011-01-06 09:50:10 -08:00
|
|
|
if (!RegisterRequestWithPrompt(request))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-11-12 07:14:45 -08:00
|
|
|
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
|
|
|
|
2010-08-04 17:36:54 -07:00
|
|
|
mPendingCallbacks.AppendElement(request);
|
2010-08-04 13:58:15 -07:00
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(true, request);
|
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
|
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;
|
|
|
|
|
2011-01-31 14:11:45 -08:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
|
|
|
|
callback,
|
|
|
|
errorCallback,
|
|
|
|
options,
|
|
|
|
PR_TRUE);
|
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) {
|
2011-01-06 09:50:10 -08:00
|
|
|
if (!RegisterRequestWithPrompt(request))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2009-11-12 07:14:45 -08:00
|
|
|
|
|
|
|
// 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-10-19 12:31:15 -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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
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
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool closed = 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
|
|
|
|
2011-01-06 09:50:10 -08:00
|
|
|
bool
|
2010-05-13 10:44:51 -07:00
|
|
|
nsGeolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
if (Preferences::GetBool("geo.prompt.testing", false)) {
|
2011-08-30 12:57:04 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new RequestAllowEvent(Preferences::GetBool("geo.prompt.testing.allow",
|
2011-09-28 23:19:26 -07:00
|
|
|
false), request);
|
2011-08-30 12:57:04 -07:00
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
|
|
|
|
if (!window)
|
2011-01-06 09:50:10 -08:00
|
|
|
return true;
|
2010-05-13 10:44:51 -07:00
|
|
|
|
|
|
|
// because owner implements nsITabChild, we can assume that it is
|
|
|
|
// the one and only TabChild.
|
2010-08-05 15:11:23 -07:00
|
|
|
TabChild* child = GetTabChildFrom(window->GetDocShell());
|
2011-01-06 09:50:10 -08:00
|
|
|
if (!child)
|
|
|
|
return false;
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2010-08-23 11:31:51 -07:00
|
|
|
// Retain a reference so the object isn't deleted without IPDL's knowledge.
|
2010-09-09 22:00:08 -07:00
|
|
|
// Corresponding release occurs in DeallocPContentPermissionRequest.
|
2010-08-23 11:31:51 -07:00
|
|
|
request->AddRef();
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2010-09-09 22:00:08 -07:00
|
|
|
nsCString type = NS_LITERAL_CSTRING("geolocation");
|
|
|
|
child->SendPContentPermissionRequestConstructor(request, type, IPC::URI(mURI));
|
|
|
|
|
|
|
|
request->Sendprompt();
|
2011-01-06 09:50:10 -08:00
|
|
|
return true;
|
2010-05-13 10:44:51 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 09:15:52 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestPromptEvent(request);
|
|
|
|
NS_DispatchToMainThread(ev);
|
2011-01-06 09:50:10 -08:00
|
|
|
return true;
|
2010-05-13 10:44:51 -07:00
|
|
|
}
|
|
|
|
|