2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2010-05-13 21:44:51 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-04-01 21:45:17 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2010-05-13 21:44:51 +04:00
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
#include "nsISettingsService.h"
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
#include "nsGeolocation.h"
|
2014-10-30 23:39:00 +03:00
|
|
|
#include "nsGeoGridFuzzer.h"
|
|
|
|
#include "nsGeolocationSettings.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2008-07-16 03:37:48 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-12-07 02:38:52 +04:00
|
|
|
#include "nsContentPermissionHelper.h"
|
2013-08-16 06:15:39 +04:00
|
|
|
#include "nsIDocument.h"
|
2008-07-16 03:37:48 +04:00
|
|
|
#include "nsIObserverService.h"
|
2013-08-16 06:15:39 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2010-08-31 20:15:52 +04: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 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/unused.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-11-09 03:36:50 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-10-19 00:57:55 +04:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
2014-09-16 21:15:16 +04:00
|
|
|
#include "mozilla/dom/SettingChangeNotificationBinding.h"
|
2008-08-14 20:35:49 +04:00
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "prdtoa.h"
|
|
|
|
|
2013-08-16 06:15:39 +04:00
|
|
|
class nsIPrincipal;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2014-02-21 06:09:02 +04:00
|
|
|
#ifdef MOZ_ENABLE_QT5GEOPOSITION
|
2011-08-16 04:25:43 +04:00
|
|
|
#include "QTMLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2011-11-11 04:17:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2010-06-05 01:14:43 +04:00
|
|
|
#include "AndroidLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-19 18:53:51 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "GonkGPSGeolocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-01 17:43:17 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
#include "CoreLocationLocationProvider.h"
|
|
|
|
#endif
|
|
|
|
|
2008-11-12 19:00:37 +03:00
|
|
|
// Some limit to the number of get or watch geolocation requests
|
|
|
|
// that a window can make.
|
|
|
|
#define MAX_GEO_REQUESTS_PER_WINDOW 1500
|
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
// the geolocation enabled setting
|
|
|
|
#define GEO_SETTINGS_ENABLED "geolocation.enabled"
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2010-07-19 22:33:33 +04:00
|
|
|
using mozilla::unused; // <snicker>
|
2011-05-25 10:31:59 +04:00
|
|
|
using namespace mozilla;
|
2010-08-06 02:11:23 +04:00
|
|
|
using namespace mozilla::dom;
|
2010-07-19 22:33:33 +04:00
|
|
|
|
2014-08-05 21:33:55 +04:00
|
|
|
class nsGeolocationRequest MOZ_FINAL
|
2013-08-28 08:14:57 +04:00
|
|
|
: public nsIContentPermissionRequest
|
|
|
|
, public nsITimerCallback
|
|
|
|
, public nsIGeolocationUpdate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_NSICONTENTPERMISSIONREQUEST
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
NS_DECL_NSIGEOLOCATIONUPDATE
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsGeolocationRequest, nsIContentPermissionRequest)
|
|
|
|
|
|
|
|
nsGeolocationRequest(Geolocation* aLocator,
|
|
|
|
const GeoPositionCallback& aCallback,
|
|
|
|
const GeoPositionErrorCallback& aErrorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions* aOptions,
|
2013-08-28 08:14:57 +04:00
|
|
|
bool aWatchPositionRequest = false,
|
|
|
|
int32_t aWatchId = 0);
|
|
|
|
void Shutdown();
|
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
void SendLocation(nsIDOMGeoPosition* aLocation);
|
2013-10-20 06:14:00 +04:00
|
|
|
bool WantsHighAccuracy() {return !mShutdown && mOptions && mOptions->mEnableHighAccuracy;}
|
2013-08-28 08:14:57 +04:00
|
|
|
void SetTimeoutTimer();
|
2014-01-09 01:04:37 +04:00
|
|
|
void StopTimeoutTimer();
|
2013-11-04 23:52:53 +04:00
|
|
|
void NotifyErrorAndShutdown(uint16_t);
|
2013-08-28 08:14:57 +04:00
|
|
|
nsIPrincipal* GetPrincipal();
|
|
|
|
|
|
|
|
bool IsWatch() { return mIsWatchPositionRequest; }
|
|
|
|
int32_t WatchId() { return mWatchId; }
|
|
|
|
private:
|
2014-08-01 11:22:20 +04:00
|
|
|
virtual ~nsGeolocationRequest();
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
already_AddRefed<nsIDOMGeoPosition> AdjustedLocation(nsIDOMGeoPosition*);
|
|
|
|
|
2013-08-28 08:14:57 +04:00
|
|
|
bool mIsWatchPositionRequest;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimeoutTimer;
|
|
|
|
GeoPositionCallback mCallback;
|
|
|
|
GeoPositionErrorCallback mErrorCallback;
|
2013-09-07 00:44:26 +04:00
|
|
|
nsAutoPtr<PositionOptions> mOptions;
|
2013-08-28 08:14:57 +04:00
|
|
|
|
|
|
|
nsRefPtr<Geolocation> mLocator;
|
|
|
|
|
|
|
|
int32_t mWatchId;
|
|
|
|
bool mShutdown;
|
|
|
|
};
|
|
|
|
|
2013-09-07 00:44:26 +04:00
|
|
|
static PositionOptions*
|
|
|
|
CreatePositionOptionsCopy(const PositionOptions& aOptions)
|
2013-04-12 22:46:36 +04:00
|
|
|
{
|
2013-09-07 00:44:26 +04:00
|
|
|
nsAutoPtr<PositionOptions> geoOptions(new PositionOptions());
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2013-09-07 00:44:26 +04:00
|
|
|
geoOptions->mEnableHighAccuracy = aOptions.mEnableHighAccuracy;
|
|
|
|
geoOptions->mMaximumAge = aOptions.mMaximumAge;
|
|
|
|
geoOptions->mTimeout = aOptions.mTimeout;
|
2013-04-12 22:46:36 +04:00
|
|
|
|
|
|
|
return geoOptions.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
class GeolocationSettingsCallback : public nsISettingsServiceCallback
|
|
|
|
{
|
2014-06-23 23:56:07 +04:00
|
|
|
virtual ~GeolocationSettingsCallback() {
|
|
|
|
MOZ_COUNT_DTOR(GeolocationSettingsCallback);
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
GeolocationSettingsCallback() {
|
|
|
|
MOZ_COUNT_CTOR(GeolocationSettingsCallback);
|
|
|
|
}
|
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2012-11-01 04:19:34 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
if (aName.EqualsASCII(GEO_SETTINGS_ENABLED)) {
|
|
|
|
// The geolocation is enabled by default:
|
|
|
|
bool value = true;
|
|
|
|
if (aResult.isBoolean()) {
|
|
|
|
value = aResult.toBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
GPSLOG("%s set to %s",
|
|
|
|
NS_ConvertUTF16toUTF8(aName).get(),
|
|
|
|
(value ? "ENABLED" : "DISABLED"));
|
|
|
|
MozSettingValue(value);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
nsRefPtr<nsGeolocationSettings> gs = nsGeolocationSettings::GetGeolocationSettings();
|
|
|
|
if (gs) {
|
|
|
|
gs->HandleGeolocationSettingsChange(aName, aResult);
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:46:38 +04:00
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
2012-11-01 04:19:34 +04:00
|
|
|
{
|
2014-10-30 23:39:00 +03:00
|
|
|
if (aName.EqualsASCII(GEO_SETTINGS_ENABLED)) {
|
|
|
|
GPSLOG("Unable to get value for '" GEO_SETTINGS_ENABLED "'");
|
|
|
|
|
|
|
|
// Default it's enabled:
|
|
|
|
MozSettingValue(true);
|
|
|
|
} else {
|
|
|
|
nsRefPtr<nsGeolocationSettings> gs = nsGeolocationSettings::GetGeolocationSettings();
|
|
|
|
if (gs) {
|
|
|
|
gs->HandleGeolocationSettingsError(aName);
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MozSettingValue(const bool aValue)
|
|
|
|
{
|
2012-11-09 03:36:50 +04:00
|
|
|
nsRefPtr<nsGeolocationService> gs = nsGeolocationService::GetGeolocationService();
|
|
|
|
if (gs) {
|
|
|
|
gs->HandleMozsettingValue(aValue);
|
2012-11-01 04:19:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(GeolocationSettingsCallback, nsISettingsServiceCallback)
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2010-08-31 20:15:52 +04:00
|
|
|
class RequestPromptEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-08-01 11:22:20 +04:00
|
|
|
RequestPromptEvent(nsGeolocationRequest* aRequest, nsWeakPtr aWindow)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
, mWindow(aWindow)
|
2010-08-31 20:15:52 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
|
|
|
|
nsContentPermissionUtils::AskPermission(mRequest, window);
|
2010-08-31 20:15:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
2014-08-01 11:22:20 +04:00
|
|
|
nsWeakPtr mWindow;
|
2010-08-31 20:15:52 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class RequestAllowEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RequestAllowEvent(int allow, nsGeolocationRequest* request)
|
|
|
|
: mAllow(allow),
|
|
|
|
mRequest(request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mAllow) {
|
2013-09-10 19:41:59 +04:00
|
|
|
mRequest->Allow(JS::UndefinedHandleValue);
|
2012-07-27 07:33:04 +04:00
|
|
|
} else {
|
2010-08-31 20:15:52 +04:00
|
|
|
mRequest->Cancel();
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2010-08-31 20:15:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAllow;
|
2010-08-31 20:15:52 +04:00
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestSendLocationEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-09-10 08:59:51 +04:00
|
|
|
RequestSendLocationEvent(nsIDOMGeoPosition* aPosition,
|
2013-06-19 23:08:10 +04:00
|
|
|
nsGeolocationRequest* aRequest)
|
2010-08-31 20:15:52 +04:00
|
|
|
: mPosition(aPosition),
|
2013-06-19 23:08:10 +04:00
|
|
|
mRequest(aRequest)
|
2010-08-31 20:15:52 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2013-05-03 10:04:58 +04:00
|
|
|
mRequest->SendLocation(mPosition);
|
2010-08-31 20:15:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-04-12 22:46:36 +04:00
|
|
|
nsCOMPtr<nsIDOMGeoPosition> mPosition;
|
2010-08-31 20:15:52 +04:00
|
|
|
nsRefPtr<nsGeolocationRequest> mRequest;
|
2013-05-03 10:04:58 +04:00
|
|
|
nsRefPtr<Geolocation> mLocator;
|
2010-08-31 20:15:52 +04:00
|
|
|
};
|
|
|
|
|
2008-08-14 20:35:49 +04:00
|
|
|
////////////////////////////////////////////////////
|
2013-04-12 22:46:32 +04:00
|
|
|
// PositionError
|
2008-08-14 20:35:49 +04:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2013-04-12 22:46:32 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PositionError)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2008-08-14 20:35:49 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionError)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionError)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PositionError, mParent)
|
2013-04-12 22:46:32 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PositionError)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PositionError)
|
2008-08-14 20:35:49 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
PositionError::PositionError(Geolocation* aParent, int16_t aCode)
|
2008-10-20 23:37:10 +04:00
|
|
|
: mCode(aCode)
|
2013-04-12 22:46:32 +04:00
|
|
|
, mParent(aParent)
|
2008-08-14 20:35:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:32 +04:00
|
|
|
PositionError::~PositionError(){}
|
2008-08-14 20:35:49 +04:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-12 22:46:32 +04:00
|
|
|
PositionError::GetCode(int16_t *aCode)
|
2008-08-14 20:35:49 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCode);
|
2013-04-12 22:46:32 +04:00
|
|
|
*aCode = Code();
|
2008-08-14 20:35:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:11:29 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PositionError::GetMessage(nsAString& aMessage)
|
|
|
|
{
|
|
|
|
switch (mCode)
|
|
|
|
{
|
|
|
|
case nsIDOMGeoPositionError::PERMISSION_DENIED:
|
|
|
|
aMessage = NS_LITERAL_STRING("User denied geolocation prompt");
|
|
|
|
break;
|
|
|
|
case nsIDOMGeoPositionError::POSITION_UNAVAILABLE:
|
|
|
|
aMessage = NS_LITERAL_STRING("Unknown error acquiring position");
|
|
|
|
break;
|
|
|
|
case nsIDOMGeoPositionError::TIMEOUT:
|
|
|
|
aMessage = NS_LITERAL_STRING("Position acquisition timed out");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation*
|
2013-04-12 22:46:32 +04:00
|
|
|
PositionError::GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
PositionError::WrapObject(JSContext* aCx)
|
2013-04-12 22:46:32 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return PositionErrorBinding::Wrap(aCx, this);
|
2013-04-12 22:46:32 +04:00
|
|
|
}
|
|
|
|
|
2008-10-20 23:37:10 +04:00
|
|
|
void
|
2013-04-12 22:46:36 +04:00
|
|
|
PositionError::NotifyCallback(const GeoPositionErrorCallback& aCallback)
|
2008-10-20 23:37:10 +04:00
|
|
|
{
|
2012-03-31 20:30:13 +04:00
|
|
|
nsAutoMicroTask mt;
|
2013-04-12 22:46:36 +04:00
|
|
|
if (aCallback.HasWebIDLCallback()) {
|
|
|
|
PositionErrorCallback* callback = aCallback.GetWebIDLCallback();
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
ErrorResult err;
|
|
|
|
callback->Call(*this, err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsIDOMGeoPositionErrorCallback* callback = aCallback.GetXPCOMCallback();
|
|
|
|
if (callback) {
|
|
|
|
callback->HandleEvent(this);
|
|
|
|
}
|
|
|
|
}
|
2008-10-20 23:37:10 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// nsGeolocationRequest
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
nsGeolocationRequest::nsGeolocationRequest(Geolocation* aLocator,
|
2013-04-12 22:46:36 +04:00
|
|
|
const GeoPositionCallback& aCallback,
|
|
|
|
const GeoPositionErrorCallback& aErrorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions* aOptions,
|
2012-11-01 04:19:34 +04:00
|
|
|
bool aWatchPositionRequest,
|
|
|
|
int32_t aWatchId)
|
2013-06-19 23:08:10 +04:00
|
|
|
: mIsWatchPositionRequest(aWatchPositionRequest),
|
2008-10-20 23:37:10 +04:00
|
|
|
mCallback(aCallback),
|
|
|
|
mErrorCallback(aErrorCallback),
|
2013-01-07 09:57:00 +04:00
|
|
|
mOptions(aOptions),
|
2012-11-01 04:19:34 +04:00
|
|
|
mLocator(aLocator),
|
2013-06-19 23:08:10 +04:00
|
|
|
mWatchId(aWatchId),
|
|
|
|
mShutdown(false)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGeolocationRequest::~nsGeolocationRequest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-15 04:10:46 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsGeolocationRequest)
|
2010-09-10 08:59:51 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
|
2008-10-20 23:37:10 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGeolocationUpdate)
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2009-01-15 04:10:46 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsGeolocationRequest)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsGeolocationRequest)
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsGeolocationRequest, mCallback, mErrorCallback, mLocator)
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2008-10-20 23:37:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Notify(nsITimer* aTimer)
|
2013-11-04 23:52:53 +04:00
|
|
|
{
|
2014-01-09 01:04:37 +04:00
|
|
|
StopTimeoutTimer();
|
2013-11-04 23:52:53 +04:00
|
|
|
NotifyErrorAndShutdown(nsIDOMGeoPositionError::TIMEOUT);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::NotifyErrorAndShutdown(uint16_t aErrorCode)
|
2008-10-20 23:37:10 +04:00
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
MOZ_ASSERT(!mShutdown, "timeout after shutdown");
|
2012-12-28 10:46:57 +04:00
|
|
|
|
|
|
|
if (!mIsWatchPositionRequest) {
|
2013-06-19 23:08:10 +04:00
|
|
|
Shutdown();
|
2012-12-28 10:46:57 +04:00
|
|
|
mLocator->RemoveRequest(this);
|
2013-09-12 19:08:50 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 23:52:53 +04:00
|
|
|
NotifyError(aErrorCode);
|
2008-10-20 23:37:10 +04:00
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_IMETHODIMP
|
2012-07-30 18:58:26 +04:00
|
|
|
nsGeolocationRequest::GetPrincipal(nsIPrincipal * *aRequestingPrincipal)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-07-30 18:58:26 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
|
2009-07-11 04:02:32 +04:00
|
|
|
|
2012-07-30 18:58:26 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = mLocator->GetPrincipal();
|
|
|
|
principal.forget(aRequestingPrincipal);
|
2009-07-11 04:02:32 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-10 00:34:40 +04:00
|
|
|
nsGeolocationRequest::GetTypes(nsIArray** aTypes)
|
2010-09-10 08:59:51 +04:00
|
|
|
{
|
2013-09-10 19:41:59 +04:00
|
|
|
nsTArray<nsString> emptyOptions;
|
2014-08-01 11:22:20 +04:00
|
|
|
return nsContentPermissionUtils::CreatePermissionArray(NS_LITERAL_CSTRING("geolocation"),
|
|
|
|
NS_LITERAL_CSTRING("unused"),
|
|
|
|
emptyOptions,
|
|
|
|
aTypes);
|
2012-11-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2010-09-10 08:59:51 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::GetWindow(nsIDOMWindow * *aRequestingWindow)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingWindow);
|
2009-07-11 04:02:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_QueryReferent(mLocator->GetOwner());
|
|
|
|
window.forget(aRequestingWindow);
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-13 21:44:51 +04:00
|
|
|
NS_IMETHODIMP
|
2010-09-10 08:59:51 +04:00
|
|
|
nsGeolocationRequest::GetElement(nsIDOMElement * *aRequestingElement)
|
2010-05-13 21:44:51 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequestingElement);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aRequestingElement = nullptr;
|
2010-05-13 21:44:51 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::Cancel()
|
|
|
|
{
|
2014-11-07 18:42:00 +03:00
|
|
|
if (mLocator->ClearPendingRequest(this)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-24 08:52:17 +04:00
|
|
|
NotifyError(nsIDOMGeoPositionError::PERMISSION_DENIED);
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-09-10 19:41:59 +04:00
|
|
|
nsGeolocationRequest::Allow(JS::HandleValue aChoices)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-09-10 19:41:59 +04:00
|
|
|
MOZ_ASSERT(aChoices.isUndefined());
|
|
|
|
|
2014-11-07 18:42:00 +03:00
|
|
|
if (mLocator->ClearPendingRequest(this)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:29:43 +03:00
|
|
|
// Kick off the geo device, if it isn't already running
|
2012-12-07 06:13:15 +04:00
|
|
|
nsRefPtr<nsGeolocationService> gs = nsGeolocationService::GetGeolocationService();
|
2013-06-13 06:20:07 +04:00
|
|
|
nsresult rv = gs->StartDevice(GetPrincipal());
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2008-08-14 20:35:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2008-10-20 23:37:10 +04:00
|
|
|
// Location provider error
|
2008-11-12 19:31:25 +03:00
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
2008-10-20 23:37:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2014-07-07 08:47:00 +04:00
|
|
|
bool canUseCache = false;
|
|
|
|
CachedPositionAndAccuracy lastPosition = gs->GetCachedPosition();
|
|
|
|
if (lastPosition.position) {
|
|
|
|
DOMTimeStamp cachedPositionTime_ms;
|
|
|
|
lastPosition.position->GetTimestamp(&cachedPositionTime_ms);
|
|
|
|
// check to see if we can use a cached value
|
|
|
|
// if the user has specified a maximumAge, return a cached value.
|
|
|
|
if (mOptions && mOptions->mMaximumAge > 0) {
|
|
|
|
uint32_t maximumAge_ms = mOptions->mMaximumAge;
|
|
|
|
bool isCachedWithinRequestedAccuracy = WantsHighAccuracy() <= lastPosition.isHighAccuracy;
|
|
|
|
bool isCachedWithinRequestedTime =
|
|
|
|
DOMTimeStamp(PR_Now() / PR_USEC_PER_MSEC - maximumAge_ms) <= cachedPositionTime_ms;
|
|
|
|
canUseCache = isCachedWithinRequestedAccuracy && isCachedWithinRequestedTime;
|
2009-01-07 20:43:56 +03:00
|
|
|
}
|
2009-01-07 20:41:57 +03:00
|
|
|
}
|
2012-07-27 07:33:04 +04:00
|
|
|
|
2014-07-07 08:47:00 +04:00
|
|
|
gs->UpdateAccuracy(WantsHighAccuracy());
|
2013-06-19 23:08:10 +04:00
|
|
|
if (canUseCache) {
|
|
|
|
// okay, we can return a cached position
|
|
|
|
// getCurrentPosition requests serviced by the cache
|
|
|
|
// will now be owned by the RequestSendLocationEvent
|
2014-07-07 08:47:00 +04:00
|
|
|
Update(lastPosition.position);
|
2013-06-19 23:08:10 +04:00
|
|
|
}
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2013-06-19 23:08:10 +04:00
|
|
|
if (mIsWatchPositionRequest || !canUseCache) {
|
|
|
|
// let the locator know we're pending
|
|
|
|
// we will now be owned by the locator
|
|
|
|
mLocator->NotifyAllowedRequest(this);
|
2009-01-07 20:41:57 +03:00
|
|
|
}
|
2008-08-14 20:35:49 +04:00
|
|
|
|
2010-08-31 20:15:52 +04:00
|
|
|
SetTimeoutTimer();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationRequest::SetTimeoutTimer()
|
|
|
|
{
|
2014-01-09 01:04:37 +04:00
|
|
|
StopTimeoutTimer();
|
2012-07-27 07:33:04 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t timeout;
|
2013-09-07 00:44:26 +04:00
|
|
|
if (mOptions && (timeout = mOptions->mTimeout) != 0) {
|
2012-04-13 11:28:00 +04:00
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (timeout < 0) {
|
2012-04-13 11:28:00 +04:00
|
|
|
timeout = 0;
|
2012-07-27 07:33:04 +04:00
|
|
|
} else if (timeout < 10) {
|
2009-01-07 20:41:57 +03:00
|
|
|
timeout = 10;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2009-01-07 20:41:57 +03:00
|
|
|
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
mTimeoutTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-09 01:04:37 +04:00
|
|
|
void
|
|
|
|
nsGeolocationRequest::StopTimeoutTimer()
|
|
|
|
{
|
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
|
|
|
mTimeoutTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
static already_AddRefed<nsIDOMGeoPosition>
|
|
|
|
SynthesizeLocation(DOMTimeStamp aTimestamp, double aLatitude, double aLongitude)
|
|
|
|
{
|
|
|
|
// return a position at sea level, N heading, 0 speed, 0 error.
|
|
|
|
nsRefPtr<nsGeoPosition> pos = new nsGeoPosition(aLatitude, aLongitude,
|
|
|
|
0.0, 0.0, 0.0, 0.0, 0.0,
|
|
|
|
aTimestamp);
|
|
|
|
return pos.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMGeoPosition>
|
|
|
|
nsGeolocationRequest::AdjustedLocation(nsIDOMGeoPosition *aPosition)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMGeoPosition> pos = aPosition;
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
GPSLOG("child process just copying position");
|
|
|
|
return pos.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the settings cache
|
|
|
|
nsRefPtr<nsGeolocationSettings> gs = nsGeolocationSettings::GetGeolocationSettings();
|
|
|
|
if (!gs) {
|
|
|
|
return pos.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure ALA is enabled
|
|
|
|
if (!gs->IsAlaEnabled()) {
|
|
|
|
GPSLOG("ALA is disabled, returning precise location");
|
|
|
|
return pos.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up the geolocation settings via the watch ID
|
|
|
|
DOMTimeStamp ts(PR_Now() / PR_USEC_PER_MSEC);
|
|
|
|
GeolocationSetting setting = gs->LookupGeolocationSetting(mWatchId);
|
|
|
|
switch (setting.GetType()) {
|
|
|
|
case GEO_ALA_TYPE_PRECISE:
|
|
|
|
GPSLOG("returning precise location watch ID: %d", mWatchId);
|
|
|
|
return pos.forget();
|
|
|
|
#ifdef MOZ_APPROX_LOCATION
|
|
|
|
case GEO_ALA_TYPE_APPROX:
|
|
|
|
GPSLOG("returning approximate location for watch ID: %d", mWatchId);
|
|
|
|
return nsGeoGridFuzzer::FuzzLocation(setting, aPosition);
|
|
|
|
#endif
|
|
|
|
case GEO_ALA_TYPE_FIXED:
|
|
|
|
GPSLOG("returning fixed location for watch ID:: %d", mWatchId);
|
|
|
|
// use "now" as time stamp
|
|
|
|
return SynthesizeLocation(ts, setting.GetFixedLatitude(),
|
|
|
|
setting.GetFixedLongitude());
|
|
|
|
case GEO_ALA_TYPE_NONE:
|
|
|
|
GPSLOG("returning no location for watch ID: %d", mWatchId);
|
|
|
|
// return nullptr so no location callback happens
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
void
|
2013-05-03 10:04:58 +04:00
|
|
|
nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
// Ignore SendLocationEvents issued before we were cleared.
|
2008-07-16 03:37:48 +04:00
|
|
|
return;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2014-08-21 23:26:00 +04:00
|
|
|
if (mOptions && mOptions->mMaximumAge > 0) {
|
|
|
|
DOMTimeStamp positionTime_ms;
|
|
|
|
aPosition->GetTimestamp(&positionTime_ms);
|
|
|
|
const uint32_t maximumAge_ms = mOptions->mMaximumAge;
|
|
|
|
const bool isTooOld =
|
|
|
|
DOMTimeStamp(PR_Now() / PR_USEC_PER_MSEC - maximumAge_ms) > positionTime_ms;
|
|
|
|
if (isTooOld) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 14:24:00 +04:00
|
|
|
nsRefPtr<Position> wrapped;
|
|
|
|
|
|
|
|
if (aPosition) {
|
2013-04-12 22:46:36 +04:00
|
|
|
nsCOMPtr<nsIDOMGeoPositionCoords> coords;
|
|
|
|
aPosition->GetCoords(getter_AddRefs(coords));
|
|
|
|
if (coords) {
|
2014-10-30 23:39:00 +03:00
|
|
|
#ifdef MOZ_GPS_DEBUG
|
|
|
|
double lat = 0.0, lon = 0.0;
|
|
|
|
coords->GetLatitude(&lat);
|
|
|
|
coords->GetLongitude(&lon);
|
|
|
|
GPSLOG("returning coordinates: %f, %f", lat, lon);
|
|
|
|
#endif
|
2013-06-19 23:08:11 +04:00
|
|
|
wrapped = new Position(ToSupports(mLocator), aPosition);
|
2013-04-12 22:46:36 +04:00
|
|
|
}
|
2013-04-12 22:46:27 +04:00
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
if (!wrapped) {
|
2013-04-12 22:46:27 +04:00
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
2008-11-12 19:05:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:08:50 +04:00
|
|
|
if (!mIsWatchPositionRequest) {
|
|
|
|
// Cancel timer and position updates in case the position
|
|
|
|
// callback spins the event loop
|
|
|
|
Shutdown();
|
|
|
|
}
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2012-03-31 20:30:13 +04:00
|
|
|
nsAutoMicroTask mt;
|
2013-04-12 22:46:36 +04:00
|
|
|
if (mCallback.HasWebIDLCallback()) {
|
|
|
|
ErrorResult err;
|
|
|
|
PositionCallback* callback = mCallback.GetWebIDLCallback();
|
|
|
|
|
|
|
|
MOZ_ASSERT(callback);
|
|
|
|
callback->Call(*wrapped, err);
|
|
|
|
} else {
|
|
|
|
nsIDOMGeoPositionCallback* callback = mCallback.GetXPCOMCallback();
|
|
|
|
|
|
|
|
MOZ_ASSERT(callback);
|
|
|
|
callback->HandleEvent(aPosition);
|
|
|
|
}
|
2008-09-10 19:52:46 +04:00
|
|
|
|
2014-01-09 01:04:37 +04:00
|
|
|
StopTimeoutTimer();
|
|
|
|
MOZ_ASSERT(mShutdown || mIsWatchPositionRequest,
|
|
|
|
"non-shutdown getCurrentPosition request after callback!");
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2013-01-03 01:21:37 +04:00
|
|
|
nsIPrincipal*
|
|
|
|
nsGeolocationRequest::GetPrincipal()
|
|
|
|
{
|
|
|
|
if (!mLocator) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mLocator->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-30 07:22:27 +04:00
|
|
|
nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition)
|
2011-01-05 00:25:04 +03:00
|
|
|
{
|
2014-10-30 23:39:00 +03:00
|
|
|
nsCOMPtr<nsIDOMGeoPosition> pos = AdjustedLocation(aPosition);
|
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestSendLocationEvent(pos, this);
|
2012-12-24 07:05:37 +04:00
|
|
|
NS_DispatchToMainThread(ev);
|
2013-06-19 23:08:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-09 01:04:37 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::LocationUpdatePending()
|
|
|
|
{
|
|
|
|
if (!mTimeoutTimer) {
|
|
|
|
SetTimeoutTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationRequest::NotifyError(uint16_t aErrorCode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsRefPtr<PositionError> positionError = new PositionError(mLocator, aErrorCode);
|
|
|
|
positionError->NotifyCallback(mErrorCallback);
|
|
|
|
return NS_OK;
|
2011-01-05 00:25:04 +03:00
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
void
|
|
|
|
nsGeolocationRequest::Shutdown()
|
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
MOZ_ASSERT(!mShutdown, "request shutdown twice");
|
|
|
|
mShutdown = true;
|
|
|
|
|
2010-08-24 06:09:00 +04:00
|
|
|
if (mTimeoutTimer) {
|
|
|
|
mTimeoutTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimeoutTimer = nullptr;
|
2010-08-24 06:09:00 +04:00
|
|
|
}
|
2013-02-26 21:27:31 +04:00
|
|
|
|
2013-10-20 06:14:00 +04:00
|
|
|
// If there are no other high accuracy requests, the geolocation service will
|
|
|
|
// notify the provider to switch to the default accuracy.
|
2013-09-07 00:44:26 +04:00
|
|
|
if (mOptions && mOptions->mEnableHighAccuracy) {
|
2013-02-26 21:27:31 +04:00
|
|
|
nsRefPtr<nsGeolocationService> gs = nsGeolocationService::GetGeolocationService();
|
|
|
|
if (gs) {
|
2013-10-20 06:14:00 +04:00
|
|
|
gs->UpdateAccuracy();
|
2013-02-26 21:27:31 +04:00
|
|
|
}
|
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
2008-08-14 20:35:49 +04:00
|
|
|
// nsGeolocationService
|
2008-07-16 03:37:48 +04:00
|
|
|
////////////////////////////////////////////////////
|
2008-08-14 20:35:49 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsGeolocationService)
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGeolocationUpdate)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGeolocationUpdate)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ADDREF(nsGeolocationService)
|
|
|
|
NS_IMPL_RELEASE(nsGeolocationService)
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2009-05-07 05:25:44 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sGeoEnabled = true;
|
2012-11-01 04:19:34 +04:00
|
|
|
static bool sGeoInitPending = true;
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t sProviderTimeout = 6000; // Time, in milliseconds, to wait for the location provider to spin up.
|
2010-08-31 20:15:52 +04:00
|
|
|
|
2009-08-28 21:40:27 +04:00
|
|
|
nsresult nsGeolocationService::Init()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-01-10 00:27:43 +04:00
|
|
|
Preferences::AddIntVarCache(&sProviderTimeout, "geo.timeout", sProviderTimeout);
|
|
|
|
Preferences::AddBoolVarCache(&sGeoEnabled, "geo.enabled", sGeoEnabled);
|
2009-05-07 05:25:44 +04:00
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!sGeoEnabled) {
|
2009-08-28 21:40:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-04-14 20:10:20 +04:00
|
|
|
|
2012-11-06 22:42:27 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
sGeoInitPending = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
// check if the geolocation service is enable from settings
|
|
|
|
nsCOMPtr<nsISettingsService> settings =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> settingsLock;
|
2014-02-07 15:19:58 +04:00
|
|
|
nsresult rv = settings->CreateLock(nullptr, getter_AddRefs(settingsLock));
|
2012-11-01 04:19:34 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<GeolocationSettingsCallback> callback = new GeolocationSettingsCallback();
|
2014-10-30 23:39:00 +03:00
|
|
|
rv = settingsLock->Get(GEO_SETTINGS_ENABLED, callback);
|
2012-11-01 04:19:34 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-30 23:39:00 +03:00
|
|
|
|
|
|
|
// look up the geolocation settings
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_ENABLED, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_TYPE, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
#ifdef MOZ_APPROX_LOCATION
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_APPROX_DISTANCE, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
#endif
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_FIXED_COORDS, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_APP_SETTINGS, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
callback = new GeolocationSettingsCallback();
|
|
|
|
rv = settingsLock->Get(GEO_ALA_ALWAYS_PRECISE, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
} else {
|
|
|
|
// If we cannot obtain the settings service, we continue
|
|
|
|
// assuming that the geolocation is enabled:
|
|
|
|
sGeoInitPending = false;
|
|
|
|
}
|
|
|
|
|
2009-10-26 08:50:16 +03:00
|
|
|
// geolocation service can be enabled -> now register observer
|
2013-08-28 08:14:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!obs) {
|
2009-10-26 08:50:16 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-10-26 08:50:16 +03:00
|
|
|
|
|
|
|
obs->AddObserver(this, "quit-application", false);
|
2012-07-27 07:19:01 +04:00
|
|
|
obs->AddObserver(this, "mozsettings-changed", false);
|
2009-10-26 08:50:16 +03:00
|
|
|
|
2014-02-21 06:09:02 +04:00
|
|
|
#ifdef MOZ_ENABLE_QT5GEOPOSITION
|
2013-04-30 07:22:27 +04:00
|
|
|
mProvider = new QTMLocationProvider();
|
2011-08-16 04:25:43 +04:00
|
|
|
#endif
|
|
|
|
|
2011-11-11 04:17:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2013-04-30 07:22:27 +04:00
|
|
|
mProvider = new AndroidLocationProvider();
|
2010-06-05 01:14:43 +04:00
|
|
|
#endif
|
2012-01-19 18:53:51 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2014-04-08 11:56:00 +04:00
|
|
|
// GonkGPSGeolocationProvider can be started at boot up time for initialization reasons.
|
|
|
|
// do_getService gets hold of the already initialized component and starts
|
|
|
|
// processing location requests immediately.
|
|
|
|
// do_Createinstance will create multiple instances of the provider which is not right.
|
|
|
|
// bug 993041
|
|
|
|
mProvider = do_GetService(GONK_GPS_GEOLOCATION_PROVIDER_CONTRACTID);
|
2012-01-19 18:53:51 +04:00
|
|
|
#endif
|
|
|
|
|
2013-07-01 17:43:17 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2014-03-17 06:54:04 +04:00
|
|
|
if (Preferences::GetBool("geo.provider.use_corelocation", false)) {
|
2013-10-12 21:56:37 +04:00
|
|
|
mProvider = new CoreLocationLocationProvider();
|
|
|
|
}
|
2013-07-01 17:43:17 +04:00
|
|
|
#endif
|
|
|
|
|
2014-02-26 04:06:01 +04:00
|
|
|
if (Preferences::GetBool("geo.provider.use_mls", false)) {
|
2014-03-22 11:42:55 +04:00
|
|
|
mProvider = do_CreateInstance("@mozilla.org/geolocation/mls-provider;1");
|
2014-02-26 04:06:01 +04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:43:17 +04:00
|
|
|
// Override platform-specific providers with the default (network)
|
|
|
|
// provider while testing. Our tests are currently not meant to exercise
|
|
|
|
// the provider, and some tests rely on the network provider being used.
|
|
|
|
// "geo.provider.testing" is always set for all plain and browser chrome
|
|
|
|
// mochitests, and also for xpcshell tests.
|
|
|
|
if (!mProvider || Preferences::GetBool("geo.provider.testing", false)) {
|
|
|
|
nsCOMPtr<nsIGeolocationProvider> override =
|
|
|
|
do_GetService(NS_GEOLOCATION_PROVIDER_CONTRACTID);
|
|
|
|
|
|
|
|
if (override) {
|
|
|
|
mProvider = override;
|
|
|
|
}
|
2013-04-30 07:22:27 +04:00
|
|
|
}
|
|
|
|
|
2009-08-28 21:40:27 +04:00
|
|
|
return NS_OK;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 20:35:49 +04:00
|
|
|
nsGeolocationService::~nsGeolocationService()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-27 07:19:01 +04:00
|
|
|
void
|
2014-09-16 21:15:16 +04:00
|
|
|
nsGeolocationService::HandleMozsettingChanged(nsISupports* aSubject)
|
2012-07-27 07:19:01 +04:00
|
|
|
{
|
|
|
|
// The string that we're interested in will be a JSON string that looks like:
|
|
|
|
// {"key":"gelocation.enabled","value":true}
|
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
RootedDictionary<SettingChangeNotification> setting(cx);
|
|
|
|
if (!WrappedJSToDictionary(cx, aSubject, setting)) {
|
2012-07-27 07:19:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-10-30 23:39:00 +03:00
|
|
|
if (!setting.mKey.EqualsASCII(GEO_SETTINGS_ENABLED)) {
|
2012-07-27 07:19:01 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-09-16 21:15:16 +04:00
|
|
|
if (!setting.mValue.isBoolean()) {
|
2012-07-27 07:19:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-30 23:39:00 +03:00
|
|
|
GPSLOG("mozsetting changed: %s == %s",
|
|
|
|
NS_ConvertUTF16toUTF8(setting.mKey).get(),
|
|
|
|
(setting.mValue.toBoolean() ? "TRUE" : "FALSE"));
|
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
HandleMozsettingValue(setting.mValue.toBoolean());
|
2012-11-01 04:19:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGeolocationService::HandleMozsettingValue(const bool aValue)
|
|
|
|
{
|
|
|
|
if (!aValue) {
|
2012-12-03 14:25:55 +04:00
|
|
|
// turn things off
|
2012-07-27 07:19:01 +04:00
|
|
|
StopDevice();
|
2012-07-30 18:20:58 +04:00
|
|
|
Update(nullptr);
|
2014-07-07 08:47:00 +04:00
|
|
|
mLastPosition.position = nullptr;
|
2012-07-27 07:19:01 +04:00
|
|
|
sGeoEnabled = false;
|
2012-07-27 07:33:04 +04:00
|
|
|
} else {
|
2012-07-27 07:19:01 +04:00
|
|
|
sGeoEnabled = true;
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
|
|
|
if (sGeoInitPending) {
|
2012-11-14 03:03:06 +04:00
|
|
|
sGeoInitPending = false;
|
2012-11-01 04:19:34 +04:00
|
|
|
for (uint32_t i = 0, length = mGeolocators.Length(); i < length; ++i) {
|
|
|
|
mGeolocators[i]->ServiceReady();
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 07:19:01 +04:00
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_IMETHODIMP
|
2008-10-20 23:37:10 +04:00
|
|
|
nsGeolocationService::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!strcmp("quit-application", aTopic)) {
|
2013-08-28 08:14:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2008-07-16 03:37:48 +04:00
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "quit-application");
|
2012-07-27 07:19:01 +04:00
|
|
|
obs->RemoveObserver(this, "mozsettings-changed");
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i< mGeolocators.Length(); i++) {
|
2008-07-16 03:37:48 +04:00
|
|
|
mGeolocators[i]->Shutdown();
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
StopDevice();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-27 07:19:01 +04:00
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!strcmp("mozsettings-changed", aTopic)) {
|
2014-09-16 21:15:16 +04:00
|
|
|
HandleMozsettingChanged(aSubject);
|
2012-07-27 07:19:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!strcmp("timer-callback", aTopic)) {
|
2008-07-16 03:37:48 +04:00
|
|
|
// decide if we can close down the service.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i< mGeolocators.Length(); i++)
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mGeolocators[i]->HasActiveCallbacks()) {
|
2008-07-16 03:37:48 +04:00
|
|
|
SetDisconnectTimer();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// okay to close up.
|
|
|
|
StopDevice();
|
2012-07-30 18:20:58 +04:00
|
|
|
Update(nullptr);
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-10-20 23:37:10 +04:00
|
|
|
nsGeolocationService::Update(nsIDOMGeoPosition *aSomewhere)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-04-30 07:22:27 +04:00
|
|
|
SetCachedPosition(aSomewhere);
|
2009-07-11 04:04:39 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i< mGeolocators.Length(); i++) {
|
2013-04-30 07:22:27 +04:00
|
|
|
mGeolocators[i]->Update(aSomewhere);
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-09 01:04:37 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationService::LocationUpdatePending()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i< mGeolocators.Length(); i++) {
|
|
|
|
mGeolocators[i]->LocationUpdatePending();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGeolocationService::NotifyError(uint16_t aErrorCode)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mGeolocators.Length(); i++) {
|
|
|
|
mGeolocators[i]->NotifyError(aErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-07 00:34:03 +04:00
|
|
|
|
2008-12-05 19:29:43 +03:00
|
|
|
void
|
|
|
|
nsGeolocationService::SetCachedPosition(nsIDOMGeoPosition* aPosition)
|
|
|
|
{
|
2014-07-07 08:47:00 +04:00
|
|
|
mLastPosition.position = aPosition;
|
|
|
|
mLastPosition.isHighAccuracy = mHigherAccuracy;
|
2008-12-05 19:29:43 +03:00
|
|
|
}
|
|
|
|
|
2014-07-07 08:47:00 +04:00
|
|
|
CachedPositionAndAccuracy
|
2008-12-05 19:29:43 +03:00
|
|
|
nsGeolocationService::GetCachedPosition()
|
|
|
|
{
|
|
|
|
return mLastPosition;
|
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
nsresult
|
2013-06-13 06:20:07 +04:00
|
|
|
nsGeolocationService::StartDevice(nsIPrincipal *aPrincipal)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-11-01 04:19:34 +04:00
|
|
|
if (!sGeoEnabled || sGeoInitPending) {
|
2009-05-07 05:25:44 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-05-07 05:25:44 +04:00
|
|
|
|
2010-09-28 01:23:35 +04:00
|
|
|
// we do not want to keep the geolocation devices online
|
|
|
|
// indefinitely. Close them down after a reasonable period of
|
|
|
|
// inactivivity
|
|
|
|
SetDisconnectTimer();
|
|
|
|
|
2010-09-21 08:16:37 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2013-02-26 21:27:31 +04:00
|
|
|
cpc->SendAddGeolocationListener(IPC::Principal(aPrincipal),
|
|
|
|
HighAccuracyRequested());
|
2010-09-21 08:16:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-11 04:04:39 +04:00
|
|
|
// Start them up!
|
2013-08-28 08:14:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!obs) {
|
2010-09-28 01:23:35 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2010-09-28 01:23:35 +04:00
|
|
|
|
2013-04-30 07:22:27 +04:00
|
|
|
if (!mProvider) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-07-11 04:04:39 +04:00
|
|
|
}
|
2010-10-14 20:12:57 +04:00
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = mProvider->Startup()) ||
|
2013-06-13 06:20:07 +04:00
|
|
|
NS_FAILED(rv = mProvider->Watch(this))) {
|
2013-06-19 23:08:11 +04:00
|
|
|
|
|
|
|
NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-04-30 07:22:27 +04:00
|
|
|
obs->NotifyObservers(mProvider,
|
|
|
|
"geolocation-device-events",
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("starting"));
|
2013-04-30 07:22:27 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-14 20:35:49 +04:00
|
|
|
nsGeolocationService::SetDisconnectTimer()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!mDisconnectTimer) {
|
2008-07-16 03:37:48 +04:00
|
|
|
mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1");
|
2012-07-27 07:33:04 +04:00
|
|
|
} else {
|
2008-07-16 03:37:48 +04:00
|
|
|
mDisconnectTimer->Cancel();
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
mDisconnectTimer->Init(this,
|
2012-01-10 00:27:43 +04:00
|
|
|
sProviderTimeout,
|
2008-07-16 03:37:48 +04:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
bool
|
|
|
|
nsGeolocationService::HighAccuracyRequested()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mGeolocators.Length(); i++) {
|
|
|
|
if (mGeolocators[i]->HighAccuracyRequested()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-21 21:52:35 +04:00
|
|
|
void
|
2013-10-20 06:14:00 +04:00
|
|
|
nsGeolocationService::UpdateAccuracy(bool aForceHigh)
|
2012-03-21 21:52:35 +04:00
|
|
|
{
|
2013-10-20 06:14:00 +04:00
|
|
|
bool highRequired = aForceHigh || HighAccuracyRequested();
|
2013-02-26 21:27:31 +04:00
|
|
|
|
2012-12-31 20:36:46 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2014-10-14 01:28:00 +04:00
|
|
|
if (cpc->IsAlive()) {
|
|
|
|
cpc->SendSetGeolocationHigherAccuracy(highRequired);
|
|
|
|
}
|
2012-12-31 20:36:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
if (!mHigherAccuracy && highRequired) {
|
2013-04-30 07:22:27 +04:00
|
|
|
mProvider->SetHighAccuracy(true);
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
if (mHigherAccuracy && !highRequired) {
|
2013-04-30 07:22:27 +04:00
|
|
|
mProvider->SetHighAccuracy(false);
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2012-03-21 21:52:35 +04:00
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
mHigherAccuracy = highRequired;
|
2012-03-21 21:52:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
void
|
2008-08-14 20:35:49 +04:00
|
|
|
nsGeolocationService::StopDevice()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
if(mDisconnectTimer) {
|
|
|
|
mDisconnectTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mDisconnectTimer = nullptr;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
2010-09-21 08:16:37 +04:00
|
|
|
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2010-11-05 20:43:13 +03:00
|
|
|
cpc->SendRemoveGeolocationListener();
|
2010-09-21 08:16:37 +04:00
|
|
|
return; // bail early
|
|
|
|
}
|
|
|
|
|
2013-08-28 08:14:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!obs) {
|
2010-09-28 01:23:35 +04:00
|
|
|
return;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2010-09-28 01:23:35 +04:00
|
|
|
|
2013-04-30 07:22:27 +04:00
|
|
|
if (!mProvider) {
|
|
|
|
return;
|
2010-09-21 08:16:37 +04:00
|
|
|
}
|
2013-04-30 07:22:27 +04:00
|
|
|
|
2013-04-30 07:22:30 +04:00
|
|
|
mHigherAccuracy = false;
|
|
|
|
|
2013-04-30 07:22:27 +04:00
|
|
|
mProvider->Shutdown();
|
|
|
|
obs->NotifyObservers(mProvider,
|
|
|
|
"geolocation-device-events",
|
2013-12-13 05:50:01 +04:00
|
|
|
MOZ_UTF16("shutdown"));
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
StaticRefPtr<nsGeolocationService> nsGeolocationService::sService;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2012-11-09 03:36:50 +04:00
|
|
|
already_AddRefed<nsGeolocationService>
|
|
|
|
nsGeolocationService::GetGeolocationService()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-11-09 03:36:50 +04:00
|
|
|
nsRefPtr<nsGeolocationService> result;
|
|
|
|
if (nsGeolocationService::sService) {
|
|
|
|
result = nsGeolocationService::sService;
|
|
|
|
return result.forget();
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2012-11-09 03:36:50 +04:00
|
|
|
result = new nsGeolocationService();
|
|
|
|
if (NS_FAILED(result->Init())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ClearOnShutdown(&nsGeolocationService::sService);
|
|
|
|
nsGeolocationService::sService = result;
|
|
|
|
return result.forget();
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
nsGeolocationService::AddLocator(Geolocation* aLocator)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2008-10-20 23:37:10 +04:00
|
|
|
mGeolocators.AppendElement(aLocator);
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
nsGeolocationService::RemoveLocator(Geolocation* aLocator)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2008-10-20 23:37:10 +04:00
|
|
|
mGeolocators.RemoveElement(aLocator);
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
2013-04-12 22:47:47 +04:00
|
|
|
// Geolocation
|
2008-07-16 03:37:48 +04:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Geolocation)
|
2013-04-12 22:46:36 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2008-08-14 20:35:49 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoGeolocation)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMGeoGeolocation)
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGeolocationUpdate)
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Geolocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Geolocation)
|
2009-01-15 04:10:46 +03:00
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Geolocation,
|
|
|
|
mPendingCallbacks,
|
|
|
|
mWatchingCallbacks,
|
|
|
|
mPendingRequests)
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::Geolocation()
|
2012-11-01 04:19:34 +04:00
|
|
|
: mLastWatchId(0)
|
2009-11-12 18:14:45 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::~Geolocation()
|
2009-11-12 18:14:45 +03:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mService) {
|
2009-11-12 18:14:45 +03:00
|
|
|
Shutdown();
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-11-12 18:14:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::Init(nsIDOMWindow* aContentDom)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
// Remember the window
|
2009-11-12 18:14:45 +03:00
|
|
|
if (aContentDom) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContentDom);
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!window) {
|
2009-11-12 18:14:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2009-07-11 04:02:32 +04:00
|
|
|
mOwner = do_GetWeakReference(window->GetCurrentInnerWindow());
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!mOwner) {
|
2009-11-12 18:14:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2012-07-30 18:58:26 +04:00
|
|
|
// Grab the principal of the document
|
2013-07-24 03:39:17 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetDoc();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!doc) {
|
2009-11-12 18:14:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2012-07-30 18:58:26 +04:00
|
|
|
mPrincipal = doc->NodePrincipal();
|
2009-11-12 18:14:45 +03:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2009-11-12 18:14:45 +03:00
|
|
|
// If no aContentDom was passed into us, we are being used
|
2012-07-30 18:58:26 +04:00
|
|
|
// by chrome/c++ and have no mOwner, no mPrincipal, and no need
|
2009-11-12 18:14:45 +03:00
|
|
|
// to prompt.
|
2012-11-09 03:36:50 +04:00
|
|
|
mService = nsGeolocationService::GetGeolocationService();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mService) {
|
2008-07-16 03:37:48 +04:00
|
|
|
mService->AddLocator(this);
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-11-12 20:17:21 +03:00
|
|
|
return NS_OK;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::Shutdown()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
// Release all callbacks
|
2008-07-16 03:37:48 +04:00
|
|
|
mPendingCallbacks.Clear();
|
|
|
|
mWatchingCallbacks.Clear();
|
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mService) {
|
2008-07-16 03:37:48 +04:00
|
|
|
mService->RemoveLocator(this);
|
2013-10-20 06:14:00 +04:00
|
|
|
mService->UpdateAccuracy();
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mService = nullptr;
|
2012-07-30 18:58:26 +04:00
|
|
|
mPrincipal = nullptr;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
nsIDOMWindow*
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::GetParentObject() const {
|
2013-04-12 22:46:36 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
|
|
|
|
return window.get();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::HasActiveCallbacks()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
return mPendingCallbacks.Length() || mWatchingCallbacks.Length();
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
bool
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::HighAccuracyRequested()
|
2013-02-26 21:27:31 +04:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mWatchingCallbacks.Length(); i++) {
|
2013-06-19 23:08:10 +04:00
|
|
|
if (mWatchingCallbacks[i]->WantsHighAccuracy()) {
|
2013-02-26 21:27:31 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPendingCallbacks.Length(); i++) {
|
2013-06-19 23:08:10 +04:00
|
|
|
if (mPendingCallbacks[i]->WantsHighAccuracy()) {
|
2013-02-26 21:27:31 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::RemoveRequest(nsGeolocationRequest* aRequest)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-06-19 23:08:10 +04:00
|
|
|
bool requestWasKnown =
|
|
|
|
(mPendingCallbacks.RemoveElement(aRequest) !=
|
|
|
|
mWatchingCallbacks.RemoveElement(aRequest));
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-06-19 23:08:10 +04:00
|
|
|
unused << requestWasKnown;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-30 07:22:27 +04:00
|
|
|
Geolocation::Update(nsIDOMGeoPosition *aSomewhere)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!WindowOwnerStillExists()) {
|
2013-06-19 23:08:11 +04:00
|
|
|
Shutdown();
|
|
|
|
return NS_OK;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2010-08-05 04:36:54 +04:00
|
|
|
|
2014-04-01 21:45:17 +04:00
|
|
|
if (aSomewhere) {
|
|
|
|
nsCOMPtr<nsIDOMGeoPositionCoords> coords;
|
|
|
|
aSomewhere->GetCoords(getter_AddRefs(coords));
|
|
|
|
if (coords) {
|
|
|
|
double accuracy = -1;
|
|
|
|
coords->GetAccuracy(&accuracy);
|
|
|
|
mozilla::Telemetry::Accumulate(mozilla::Telemetry::GEOLOCATION_ACCURACY, accuracy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:10 +04:00
|
|
|
for (uint32_t i = mPendingCallbacks.Length(); i > 0; i--) {
|
|
|
|
mPendingCallbacks[i-1]->Update(aSomewhere);
|
|
|
|
RemoveRequest(mPendingCallbacks[i-1]);
|
2010-08-31 20:15:52 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
// notify everyone that is watching
|
2013-06-19 23:08:11 +04:00
|
|
|
for (uint32_t i = 0; i < mWatchingCallbacks.Length(); i++) {
|
2013-04-30 07:22:27 +04:00
|
|
|
mWatchingCallbacks[i]->Update(aSomewhere);
|
2010-08-31 20:15:52 +04:00
|
|
|
}
|
2013-06-19 23:08:11 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-09 01:04:37 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Geolocation::LocationUpdatePending()
|
|
|
|
{
|
|
|
|
// this event is only really interesting for watch callbacks
|
|
|
|
for (uint32_t i = 0; i < mWatchingCallbacks.Length(); i++) {
|
|
|
|
mWatchingCallbacks[i]->LocationUpdatePending();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Geolocation::NotifyError(uint16_t aErrorCode)
|
|
|
|
{
|
|
|
|
if (!WindowOwnerStillExists()) {
|
|
|
|
Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:45:17 +04:00
|
|
|
mozilla::Telemetry::Accumulate(mozilla::Telemetry::GEOLOCATION_ERROR, true);
|
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
for (uint32_t i = mPendingCallbacks.Length(); i > 0; i--) {
|
2013-11-04 23:52:53 +04:00
|
|
|
mPendingCallbacks[i-1]->NotifyErrorAndShutdown(aErrorCode);
|
|
|
|
//NotifyErrorAndShutdown() removes the request from the array
|
2013-06-19 23:08:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify everyone that is watching
|
|
|
|
for (uint32_t i = 0; i < mWatchingCallbacks.Length(); i++) {
|
2013-11-04 23:52:53 +04:00
|
|
|
mWatchingCallbacks[i]->NotifyErrorAndShutdown(aErrorCode);
|
2013-06-19 23:08:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
|
|
|
|
2014-11-07 18:42:00 +03:00
|
|
|
bool
|
|
|
|
Geolocation::IsAlreadyCleared(nsGeolocationRequest* aRequest)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0, length = mClearedWatchIDs.Length(); i < length; ++i) {
|
|
|
|
if (mClearedWatchIDs[i] == aRequest->WatchId()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Geolocation::ClearPendingRequest(nsGeolocationRequest* aRequest)
|
|
|
|
{
|
|
|
|
if (aRequest->IsWatch() && this->IsAlreadyCleared(aRequest)) {
|
|
|
|
this->NotifyAllowedRequest(aRequest);
|
|
|
|
this->ClearWatch(aRequest->WatchId());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::GetCurrentPosition(PositionCallback& aCallback,
|
|
|
|
PositionErrorCallback* aErrorCallback,
|
|
|
|
const PositionOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2013-04-12 22:46:36 +04:00
|
|
|
{
|
|
|
|
GeoPositionCallback successCallback(&aCallback);
|
|
|
|
GeoPositionErrorCallback errorCallback(aErrorCallback);
|
|
|
|
|
2013-09-07 00:44:26 +04:00
|
|
|
nsresult rv = GetCurrentPosition(successCallback, errorCallback,
|
|
|
|
CreatePositionOptionsCopy(aOptions));
|
2013-04-12 22:46:36 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2013-01-07 09:57:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::GetCurrentPosition(nsIDOMGeoPositionCallback* aCallback,
|
|
|
|
nsIDOMGeoPositionErrorCallback* aErrorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions* aOptions)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-04-12 22:46:36 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
|
|
|
GeoPositionCallback successCallback(aCallback);
|
|
|
|
GeoPositionErrorCallback errorCallback(aErrorCallback);
|
2009-07-16 21:03:49 +04:00
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
return GetCurrentPosition(successCallback, errorCallback, aOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::GetCurrentPosition(GeoPositionCallback& callback,
|
|
|
|
GeoPositionErrorCallback& errorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions *options)
|
2013-04-12 22:46:36 +04:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mPendingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) {
|
2008-11-12 19:00:37 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-11-12 19:00:37 +03:00
|
|
|
|
2011-02-01 01:11:45 +03:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
|
2012-07-27 07:33:04 +04:00
|
|
|
callback,
|
|
|
|
errorCallback,
|
2013-01-07 09:57:00 +04:00
|
|
|
options,
|
2012-07-27 07:33:04 +04:00
|
|
|
false);
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2012-07-27 07:19:01 +04:00
|
|
|
if (!sGeoEnabled) {
|
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(false, request);
|
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
if (!mOwner && !nsContentUtils::IsCallerChrome()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sGeoInitPending) {
|
2013-09-09 16:57:37 +04:00
|
|
|
mPendingRequests.AppendElement(request);
|
2012-11-01 04:19:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCurrentPositionReady(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::GetCurrentPositionReady(nsGeolocationRequest* aRequest)
|
2012-11-01 04:19:34 +04:00
|
|
|
{
|
2009-11-12 18:14:45 +03:00
|
|
|
if (mOwner) {
|
2012-11-01 04:19:34 +04:00
|
|
|
if (!RegisterRequestWithPrompt(aRequest)) {
|
2011-01-06 20:50:10 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2008-11-12 18:59:38 +03:00
|
|
|
return NS_OK;
|
2009-11-12 18:14:45 +03:00
|
|
|
}
|
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
2009-11-12 18:14:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-11-12 18:59:38 +03:00
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(true, aRequest);
|
2010-08-31 20:15:52 +04:00
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
int32_t
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::WatchPosition(PositionCallback& aCallback,
|
|
|
|
PositionErrorCallback* aErrorCallback,
|
|
|
|
const PositionOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2013-01-07 09:57:00 +04:00
|
|
|
{
|
2013-04-12 22:46:36 +04:00
|
|
|
int32_t ret;
|
|
|
|
GeoPositionCallback successCallback(&aCallback);
|
|
|
|
GeoPositionErrorCallback errorCallback(aErrorCallback);
|
|
|
|
|
2013-09-07 00:44:26 +04:00
|
|
|
nsresult rv = WatchPosition(successCallback, errorCallback,
|
|
|
|
CreatePositionOptionsCopy(aOptions), &ret);
|
2013-04-12 22:46:36 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-01-07 09:57:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::WatchPosition(nsIDOMGeoPositionCallback *aCallback,
|
|
|
|
nsIDOMGeoPositionErrorCallback *aErrorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions *aOptions,
|
2013-04-12 22:47:47 +04:00
|
|
|
int32_t* aRv)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-04-12 22:46:36 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCallback);
|
|
|
|
|
|
|
|
GeoPositionCallback successCallback(aCallback);
|
|
|
|
GeoPositionErrorCallback errorCallback(aErrorCallback);
|
|
|
|
|
|
|
|
return WatchPosition(successCallback, errorCallback, aOptions, aRv);
|
|
|
|
}
|
2009-05-07 05:25:44 +04:00
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
nsresult
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::WatchPosition(GeoPositionCallback& aCallback,
|
|
|
|
GeoPositionErrorCallback& aErrorCallback,
|
2013-09-07 00:44:26 +04:00
|
|
|
PositionOptions* aOptions,
|
2013-04-12 22:47:47 +04:00
|
|
|
int32_t* aRv)
|
2013-04-12 22:46:36 +04:00
|
|
|
{
|
2013-02-03 17:39:00 +04:00
|
|
|
if (mWatchingCallbacks.Length() > MAX_GEO_REQUESTS_PER_WINDOW) {
|
2008-11-12 19:00:37 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-11-12 19:00:37 +03:00
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
// The watch ID:
|
2013-04-12 22:46:36 +04:00
|
|
|
*aRv = mLastWatchId++;
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2011-02-01 01:11:45 +03:00
|
|
|
nsRefPtr<nsGeolocationRequest> request = new nsGeolocationRequest(this,
|
2013-04-12 22:46:36 +04:00
|
|
|
aCallback,
|
|
|
|
aErrorCallback,
|
|
|
|
aOptions,
|
2012-11-01 04:19:34 +04:00
|
|
|
true,
|
2013-04-12 22:46:36 +04:00
|
|
|
*aRv);
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2012-07-27 07:19:01 +04:00
|
|
|
if (!sGeoEnabled) {
|
2014-10-30 23:39:00 +03:00
|
|
|
GPSLOG("request allow event");
|
2012-07-27 07:19:01 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(false, request);
|
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
if (!mOwner && !nsContentUtils::IsCallerChrome()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sGeoInitPending) {
|
2013-09-09 16:57:37 +04:00
|
|
|
mPendingRequests.AppendElement(request);
|
2012-11-01 04:19:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WatchPositionReady(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::WatchPositionReady(nsGeolocationRequest* aRequest)
|
2012-11-01 04:19:34 +04:00
|
|
|
{
|
2012-07-27 07:33:04 +04:00
|
|
|
if (mOwner) {
|
2012-11-01 04:19:34 +04:00
|
|
|
if (!RegisterRequestWithPrompt(aRequest))
|
2011-01-06 20:50:10 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2008-11-12 18:59:38 +03:00
|
|
|
return NS_OK;
|
2009-11-12 18:14:45 +03:00
|
|
|
}
|
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
2009-11-12 18:14:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-11-12 18:59:38 +03:00
|
|
|
|
2013-09-10 19:41:59 +04:00
|
|
|
aRequest->Allow(JS::UndefinedHandleValue);
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::ClearWatch(int32_t aWatchId)
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2012-11-01 04:19:34 +04:00
|
|
|
if (aWatchId < 0) {
|
2009-11-13 20:19:45 +03:00
|
|
|
return NS_OK;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-11-12 19:01:40 +03:00
|
|
|
|
2014-11-07 18:42:00 +03:00
|
|
|
if (!mClearedWatchIDs.Contains(aWatchId)) {
|
|
|
|
mClearedWatchIDs.AppendElement(aWatchId);
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
for (uint32_t i = 0, length = mWatchingCallbacks.Length(); i < length; ++i) {
|
|
|
|
if (mWatchingCallbacks[i]->WatchId() == aWatchId) {
|
2013-06-19 23:08:10 +04:00
|
|
|
mWatchingCallbacks[i]->Shutdown();
|
|
|
|
RemoveRequest(mWatchingCallbacks[i]);
|
2014-11-07 18:42:00 +03:00
|
|
|
mClearedWatchIDs.RemoveElement(aWatchId);
|
2012-11-01 04:19:34 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:18:00 +04:00
|
|
|
// make sure we also search through the pending requests lists for
|
|
|
|
// watches to clear...
|
|
|
|
for (uint32_t i = 0, length = mPendingRequests.Length(); i < length; ++i) {
|
2013-09-09 16:57:37 +04:00
|
|
|
if (mPendingRequests[i]->IsWatch() &&
|
|
|
|
(mPendingRequests[i]->WatchId() == aWatchId)) {
|
|
|
|
mPendingRequests[i]->Shutdown();
|
2013-08-22 02:18:00 +04:00
|
|
|
mPendingRequests.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:19:34 +04:00
|
|
|
void
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::ServiceReady()
|
2012-11-01 04:19:34 +04:00
|
|
|
{
|
|
|
|
for (uint32_t length = mPendingRequests.Length(); length > 0; --length) {
|
2013-09-09 16:57:37 +04:00
|
|
|
if (mPendingRequests[0]->IsWatch()) {
|
|
|
|
WatchPositionReady(mPendingRequests[0]);
|
|
|
|
} else {
|
|
|
|
GetCurrentPositionReady(mPendingRequests[0]);
|
2012-11-01 04:19:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mPendingRequests.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::WindowOwnerStillExists()
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
2013-04-12 22:47:47 +04:00
|
|
|
// an owner was never set when Geolocation
|
2009-11-12 18:14:45 +03:00
|
|
|
// was created, which means that this object
|
|
|
|
// is being used without a window.
|
2012-07-30 18:20:58 +04:00
|
|
|
if (mOwner == nullptr) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-07-11 04:02:32 +04:00
|
|
|
|
2009-11-12 18:14:45 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mOwner);
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2012-07-27 07:33:04 +04:00
|
|
|
if (window) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool closed = false;
|
2009-07-11 04:02:32 +04:00
|
|
|
window->GetClosed(&closed);
|
2012-07-27 07:33:04 +04:00
|
|
|
if (closed) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2009-11-12 18:14:45 +03:00
|
|
|
nsPIDOMWindow* outer = window->GetOuterWindow();
|
2012-07-27 07:33:04 +04:00
|
|
|
if (!outer || outer->GetCurrentInnerWindow() != window) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-07-27 07:33:04 +04:00
|
|
|
}
|
2009-11-12 18:14:45 +03:00
|
|
|
}
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-07-16 03:37:48 +04:00
|
|
|
}
|
2010-01-12 16:08:43 +03:00
|
|
|
|
2013-06-19 23:08:10 +04:00
|
|
|
void
|
|
|
|
Geolocation::NotifyAllowedRequest(nsGeolocationRequest* aRequest)
|
|
|
|
{
|
|
|
|
if (aRequest->IsWatch()) {
|
|
|
|
mWatchingCallbacks.AppendElement(aRequest);
|
|
|
|
} else {
|
|
|
|
mPendingCallbacks.AppendElement(aRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 20:50:10 +03:00
|
|
|
bool
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request)
|
2010-05-13 21:44:51 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
if (Preferences::GetBool("geo.prompt.testing", false)) {
|
2012-07-27 07:33:04 +04:00
|
|
|
bool allow = Preferences::GetBool("geo.prompt.testing.allow", false);
|
2014-10-30 23:39:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestAllowEvent(allow, request);
|
2011-08-30 23:57:04 +04:00
|
|
|
NS_DispatchToMainThread(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:22:20 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new RequestPromptEvent(request, mOwner);
|
2010-08-31 20:15:52 +04:00
|
|
|
NS_DispatchToMainThread(ev);
|
2011-01-06 20:50:10 +03:00
|
|
|
return true;
|
2010-05-13 21:44:51 +04:00
|
|
|
}
|
2013-04-12 22:46:36 +04:00
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
Geolocation::WrapObject(JSContext *aCtx)
|
2013-04-12 22:46:36 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return GeolocationBinding::Wrap(aCtx, this);
|
2013-04-12 22:46:36 +04:00
|
|
|
}
|