2012-05-21 04:12:37 -07: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-15 16:37:48 -07:00
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#ifndef nsGeoLocation_h
|
|
|
|
#define nsGeoLocation_h
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-09-09 22:00:08 -07:00
|
|
|
#include "mozilla/dom/PContentPermissionRequestChild.h"
|
2010-05-14 17:45:53 -07:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-05-30 15:34:53 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIURI.h"
|
2013-04-12 11:46:32 -07:00
|
|
|
#include "nsWrapperCache.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2009-07-10 17:02:32 -07:00
|
|
|
#include "nsWeakPtr.h"
|
2009-01-14 17:10:46 -08:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
#include "nsGeoPosition.h"
|
2008-08-14 09:35:49 -07:00
|
|
|
#include "nsIDOMGeoGeolocation.h"
|
|
|
|
#include "nsIDOMGeoPosition.h"
|
|
|
|
#include "nsIDOMGeoPositionError.h"
|
|
|
|
#include "nsIDOMGeoPositionCallback.h"
|
|
|
|
#include "nsIDOMGeoPositionErrorCallback.h"
|
|
|
|
#include "nsIDOMNavigatorGeolocation.h"
|
2013-04-12 11:46:36 -07:00
|
|
|
#include "mozilla/dom/GeolocationBinding.h"
|
2013-04-12 11:46:32 -07:00
|
|
|
#include "mozilla/dom/PositionErrorBinding.h"
|
2013-04-12 11:46:36 -07:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
#include "nsIGeolocationProvider.h"
|
2010-09-09 21:59:51 -07:00
|
|
|
#include "nsIContentPermissionPrompt.h"
|
2012-04-16 12:08:48 -07:00
|
|
|
#include "DictionaryHelpers.h"
|
2010-09-09 22:00:08 -07:00
|
|
|
#include "PCOMContentPermissionRequestChild.h"
|
2012-06-14 19:31:55 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-09-09 22:00:08 -07:00
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
class nsGeolocationService;
|
2013-04-12 11:46:36 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-04-12 11:47:47 -07:00
|
|
|
class Geolocation;
|
2013-04-12 11:46:36 -07:00
|
|
|
typedef CallbackObjectHolder<PositionCallback, nsIDOMGeoPositionCallback> GeoPositionCallback;
|
|
|
|
typedef CallbackObjectHolder<PositionErrorCallback, nsIDOMGeoPositionErrorCallback> GeoPositionErrorCallback;
|
|
|
|
}
|
|
|
|
}
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2010-05-13 10:44:51 -07:00
|
|
|
class nsGeolocationRequest
|
2010-09-09 21:59:51 -07:00
|
|
|
: public nsIContentPermissionRequest
|
2010-05-13 10:44:51 -07:00
|
|
|
, public nsITimerCallback
|
2010-09-09 22:00:08 -07:00
|
|
|
, public PCOMContentPermissionRequestChild
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_DECL_NSICONTENTPERMISSIONREQUEST
|
2008-10-20 12:37:10 -07:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2009-01-14 17:10:46 -08:00
|
|
|
|
2010-09-09 21:59:51 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsGeolocationRequest, nsIContentPermissionRequest)
|
2009-01-14 17:10:46 -08:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
nsGeolocationRequest(mozilla::dom::Geolocation* locator,
|
2013-04-12 11:46:36 -07:00
|
|
|
const mozilla::dom::GeoPositionCallback& callback,
|
|
|
|
const mozilla::dom::GeoPositionErrorCallback& errorCallback,
|
2013-02-11 11:37:50 -08:00
|
|
|
mozilla::idl::GeoPositionOptions* aOptions,
|
2012-10-31 17:19:34 -07:00
|
|
|
bool watchPositionRequest = false,
|
|
|
|
int32_t watchId = 0);
|
2008-07-15 16:37:48 -07:00
|
|
|
void Shutdown();
|
|
|
|
|
2011-01-04 13:25:04 -08:00
|
|
|
// Called by the geolocation device to notify that a location has changed.
|
2013-04-29 20:22:27 -07:00
|
|
|
bool Update(nsIDOMGeoPosition* aPosition);
|
2011-01-04 13:25:04 -08:00
|
|
|
|
2013-05-02 23:04:58 -07:00
|
|
|
void SendLocation(nsIDOMGeoPosition* location);
|
2008-07-15 16:37:48 -07:00
|
|
|
void MarkCleared();
|
2013-02-26 09:27:31 -08:00
|
|
|
bool WantsHighAccuracy() {return mOptions && mOptions->enableHighAccuracy;}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsActive() {return !mCleared;}
|
|
|
|
bool Allowed() {return mAllowed;}
|
2010-08-31 09:15:52 -07:00
|
|
|
void SetTimeoutTimer();
|
2013-01-02 13:21:37 -08:00
|
|
|
nsIPrincipal* GetPrincipal();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
~nsGeolocationRequest();
|
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual bool Recv__delete__(const bool& allow) MOZ_OVERRIDE;
|
|
|
|
virtual void IPDLRelease() MOZ_OVERRIDE { Release(); }
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2012-10-31 17:19:34 -07:00
|
|
|
int32_t WatchId() { return mWatchId; }
|
2008-07-15 16:37:48 -07:00
|
|
|
private:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void NotifyError(int16_t errorCode);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAllowed;
|
|
|
|
bool mCleared;
|
|
|
|
bool mIsWatchPositionRequest;
|
2008-10-20 12:37:10 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimeoutTimer;
|
2013-04-12 11:46:36 -07:00
|
|
|
mozilla::dom::GeoPositionCallback mCallback;
|
|
|
|
mozilla::dom::GeoPositionErrorCallback mErrorCallback;
|
2013-02-11 11:37:50 -08:00
|
|
|
nsAutoPtr<mozilla::idl::GeoPositionOptions> mOptions;
|
2008-10-20 12:37:10 -07:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
nsRefPtr<mozilla::dom::Geolocation> mLocator;
|
2012-10-31 17:19:34 -07:00
|
|
|
|
|
|
|
int32_t mWatchId;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton that manages the geolocation provider
|
|
|
|
*/
|
2012-06-14 19:31:55 -07:00
|
|
|
class nsGeolocationService MOZ_FINAL : public nsIGeolocationUpdate, public nsIObserver
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-11-08 15:36:50 -08:00
|
|
|
static already_AddRefed<nsGeolocationService> GetGeolocationService();
|
2013-05-30 15:34:53 -07:00
|
|
|
static mozilla::StaticRefPtr<nsGeolocationService> sService;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIGEOLOCATIONUPDATE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2012-03-21 10:52:35 -07:00
|
|
|
nsGeolocationService() {
|
|
|
|
mHigherAccuracy = false;
|
|
|
|
}
|
2009-08-28 10:40:27 -07:00
|
|
|
|
|
|
|
nsresult Init();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2012-07-26 20:19:01 -07:00
|
|
|
void HandleMozsettingChanged(const PRUnichar* aData);
|
2012-10-31 17:19:34 -07:00
|
|
|
void HandleMozsettingValue(const bool aValue);
|
2012-07-26 20:19:01 -07:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
// Management of the Geolocation objects
|
|
|
|
void AddLocator(mozilla::dom::Geolocation* locator);
|
|
|
|
void RemoveLocator(mozilla::dom::Geolocation* locator);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2008-12-05 08:29:43 -08:00
|
|
|
void SetCachedPosition(nsIDOMGeoPosition* aPosition);
|
|
|
|
nsIDOMGeoPosition* GetCachedPosition();
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Find and startup a geolocation device (gps, nmea, etc.)
|
2013-01-02 13:21:37 -08:00
|
|
|
nsresult StartDevice(nsIPrincipal* aPrincipal, bool aRequestPrivate);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Stop the started geolocation device (gps, nmea, etc.)
|
|
|
|
void StopDevice();
|
2012-10-31 17:19:34 -07:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// create, or reinitalize the callback timer
|
|
|
|
void SetDisconnectTimer();
|
|
|
|
|
2012-03-21 10:52:35 -07:00
|
|
|
// request higher accuracy, if possible
|
|
|
|
void SetHigherAccuracy(bool aEnable);
|
2013-02-26 09:27:31 -08:00
|
|
|
bool HighAccuracyRequested();
|
2012-03-21 10:52:35 -07:00
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
private:
|
|
|
|
|
2008-08-14 09:35:49 -07:00
|
|
|
~nsGeolocationService();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Disconnect timer. When this timer expires, it clears all pending callbacks
|
|
|
|
// and closes down the provider, unless we are watching a point, and in that
|
|
|
|
// case, we disable the disconnect timer.
|
|
|
|
nsCOMPtr<nsITimer> mDisconnectTimer;
|
|
|
|
|
|
|
|
// The object providing geo location information to us.
|
2013-04-29 20:22:27 -07:00
|
|
|
nsCOMPtr<nsIGeolocationProvider> mProvider;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// mGeolocators are not owned here. Their constructor
|
2009-07-10 17:04:39 -07:00
|
|
|
// adds them to this list, and their destructor removes
|
2008-07-15 16:37:48 -07:00
|
|
|
// them from this list.
|
2013-04-12 11:47:47 -07:00
|
|
|
nsTArray<mozilla::dom::Geolocation*> mGeolocators;
|
2008-12-05 08:29:43 -08:00
|
|
|
|
|
|
|
// This is the last geo position that we have seen.
|
|
|
|
nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
|
2012-03-21 10:52:35 -07:00
|
|
|
|
|
|
|
// Current state of requests for higher accuracy
|
|
|
|
bool mHigherAccuracy;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Can return a geolocation info
|
2012-10-31 17:19:34 -07:00
|
|
|
*/
|
2013-04-12 11:47:47 -07:00
|
|
|
class Geolocation MOZ_FINAL : public nsIDOMGeoGeolocation,
|
2013-04-12 11:46:36 -07:00
|
|
|
public nsWrapperCache
|
2008-07-15 16:37:48 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-01-14 17:10:46 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-04-12 11:47:47 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Geolocation)
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
NS_DECL_NSIDOMGEOGEOLOCATION
|
2009-01-14 17:10:46 -08:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
Geolocation();
|
2009-11-12 07:14:45 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult Init(nsIDOMWindow* contentDom=nullptr);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
nsIDOMWindow* GetParentObject() const;
|
2013-04-25 09:29:54 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext *aCtx,
|
|
|
|
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
2013-04-12 11:46:36 -07:00
|
|
|
|
|
|
|
int32_t WatchPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
|
|
|
|
void GetCurrentPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Called by the geolocation device to notify that a location has changed.
|
2013-04-29 20:22:27 -07:00
|
|
|
void Update(nsIDOMGeoPosition* aPosition);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
void SetCachedPosition(Position* aPosition);
|
|
|
|
Position* GetCachedPosition();
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Returns true if any of the callbacks are repeating
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasActiveCallbacks();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Remove request from all callbacks arrays
|
2008-10-20 12:37:10 -07:00
|
|
|
void RemoveRequest(nsGeolocationRequest* request);
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Shutting down.
|
2008-10-20 12:37:10 -07:00
|
|
|
void Shutdown();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
// Getter for the principal that this Geolocation was loaded from
|
2012-07-30 07:58:26 -07:00
|
|
|
nsIPrincipal* GetPrincipal() { return mPrincipal; }
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
// Getter for the window that this Geolocation is owned by
|
2009-07-10 17:02:32 -07:00
|
|
|
nsIWeakReference* GetOwner() { return mOwner; }
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// Check to see if the widnow still exists
|
2011-09-28 23:19:26 -07:00
|
|
|
bool WindowOwnerStillExists();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-02-26 09:27:31 -08:00
|
|
|
// Check to see if any active request requires high accuracy
|
|
|
|
bool HighAccuracyRequested();
|
|
|
|
|
2012-10-31 17:19:34 -07:00
|
|
|
// Notification from the service:
|
|
|
|
void ServiceReady();
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
private:
|
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
~Geolocation();
|
2008-07-15 16:37:48 -07:00
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
nsresult GetCurrentPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, mozilla::idl::GeoPositionOptions* aOptions);
|
|
|
|
nsresult WatchPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, mozilla::idl::GeoPositionOptions* aOptions, int32_t* aRv);
|
|
|
|
|
2011-01-06 09:50:10 -08:00
|
|
|
bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2012-10-31 17:19:34 -07:00
|
|
|
// Methods for the service when it's ready to process requests:
|
|
|
|
nsresult GetCurrentPositionReady(nsGeolocationRequest* aRequest);
|
|
|
|
nsresult WatchPositionReady(nsGeolocationRequest* aRequest);
|
|
|
|
|
2008-07-15 16:37:48 -07:00
|
|
|
// Two callback arrays. The first |mPendingCallbacks| holds objects for only
|
|
|
|
// one callback and then they are released/removed from the array. The second
|
|
|
|
// |mWatchingCallbacks| holds objects until the object is explictly removed or
|
|
|
|
// there is a page change.
|
|
|
|
|
2008-10-20 12:37:10 -07:00
|
|
|
nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
|
|
|
|
nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// window that this was created for. Weak reference.
|
2009-07-10 17:02:32 -07:00
|
|
|
nsWeakPtr mOwner;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// where the content was loaded from
|
2012-07-30 07:58:26 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2008-07-15 16:37:48 -07:00
|
|
|
|
|
|
|
// owning back pointer.
|
2008-08-14 09:35:49 -07:00
|
|
|
nsRefPtr<nsGeolocationService> mService;
|
2012-10-31 17:19:34 -07:00
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
// cached Position wrapper
|
|
|
|
nsRefPtr<Position> mCachedPosition;
|
|
|
|
|
2012-10-31 17:19:34 -07:00
|
|
|
// Watch ID
|
|
|
|
uint32_t mLastWatchId;
|
|
|
|
|
|
|
|
// Pending requests are used when the service is not ready:
|
|
|
|
class PendingRequest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsRefPtr<nsGeolocationRequest> request;
|
|
|
|
enum {
|
|
|
|
GetCurrentPosition,
|
|
|
|
WatchPosition
|
|
|
|
} type;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTArray<PendingRequest> mPendingRequests;
|
2008-07-15 16:37:48 -07:00
|
|
|
};
|
2010-05-13 10:44:51 -07:00
|
|
|
|
2013-04-12 11:46:32 -07:00
|
|
|
class PositionError MOZ_FINAL : public nsIDOMGeoPositionError,
|
|
|
|
public nsWrapperCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PositionError)
|
|
|
|
|
|
|
|
NS_DECL_NSIDOMGEOPOSITIONERROR
|
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
PositionError(Geolocation* aParent, int16_t aCode);
|
2013-04-12 11:46:32 -07:00
|
|
|
|
2013-04-12 11:47:47 -07:00
|
|
|
Geolocation* GetParentObject() const;
|
2013-04-12 11:46:32 -07:00
|
|
|
|
2013-04-25 09:29:54 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
2013-04-12 11:46:32 -07:00
|
|
|
|
|
|
|
int16_t Code() const {
|
|
|
|
return mCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetMessage(nsString& aRetVal) const {
|
|
|
|
aRetVal.Truncate();
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:46:36 -07:00
|
|
|
void NotifyCallback(const GeoPositionErrorCallback& callback);
|
2013-04-12 11:46:32 -07:00
|
|
|
private:
|
|
|
|
~PositionError();
|
|
|
|
int16_t mCode;
|
2013-04-12 11:47:47 -07:00
|
|
|
nsRefPtr<Geolocation> mParent;
|
2013-04-12 11:46:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-04 14:14:43 -07:00
|
|
|
#endif /* nsGeoLocation_h */
|