2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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-06-05 01:14:43 +04:00
|
|
|
#ifndef nsGeoLocation_h
|
|
|
|
#define nsGeoLocation_h
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2010-05-15 04:45:53 +04:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2008-07-16 03:37:48 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserver.h"
|
2013-04-12 22:46:32 +04:00
|
|
|
#include "nsWrapperCache.h"
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2009-07-11 04:02:32 +04:00
|
|
|
#include "nsWeakPtr.h"
|
2009-01-15 04:10:46 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
#include "nsGeoPosition.h"
|
2008-08-14 20:35:49 +04:00
|
|
|
#include "nsIDOMGeoGeolocation.h"
|
|
|
|
#include "nsIDOMGeoPosition.h"
|
|
|
|
#include "nsIDOMGeoPositionError.h"
|
|
|
|
#include "nsIDOMGeoPositionCallback.h"
|
|
|
|
#include "nsIDOMGeoPositionErrorCallback.h"
|
2016-11-15 20:46:32 +03:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-04-12 22:46:36 +04:00
|
|
|
#include "mozilla/dom/GeolocationBinding.h"
|
2013-04-12 22:46:32 +04:00
|
|
|
#include "mozilla/dom/PositionErrorBinding.h"
|
2013-04-12 22:46:36 +04:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
#include "nsIGeolocationProvider.h"
|
2010-09-10 08:59:51 +04:00
|
|
|
#include "nsIContentPermissionPrompt.h"
|
2012-06-15 06:31:55 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-09-10 09:00:08 +04:00
|
|
|
|
2008-08-14 20:35:49 +04:00
|
|
|
class nsGeolocationService;
|
2013-08-28 08:14:57 +04:00
|
|
|
class nsGeolocationRequest;
|
2013-04-12 22:46:36 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-04-12 22:47:47 +04:00
|
|
|
class Geolocation;
|
2013-04-12 22:46:36 +04:00
|
|
|
typedef CallbackObjectHolder<PositionCallback, nsIDOMGeoPositionCallback> GeoPositionCallback;
|
|
|
|
typedef CallbackObjectHolder<PositionErrorCallback, nsIDOMGeoPositionErrorCallback> GeoPositionErrorCallback;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2014-07-07 08:47:00 +04:00
|
|
|
struct CachedPositionAndAccuracy {
|
|
|
|
nsCOMPtr<nsIDOMGeoPosition> position;
|
|
|
|
bool isHighAccuracy;
|
|
|
|
};
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
/**
|
|
|
|
* Singleton that manages the geolocation provider
|
|
|
|
*/
|
2015-03-27 21:52:19 +03:00
|
|
|
class nsGeolocationService final : public nsIGeolocationUpdate,
|
|
|
|
public nsIObserver
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-11-09 03:36:50 +04:00
|
|
|
static already_AddRefed<nsGeolocationService> GetGeolocationService();
|
2013-05-31 02:34:53 +04:00
|
|
|
static mozilla::StaticRefPtr<nsGeolocationService> sService;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2008-07-16 03:37:48 +04:00
|
|
|
NS_DECL_NSIGEOLOCATIONUPDATE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2012-03-21 21:52:35 +04:00
|
|
|
nsGeolocationService() {
|
|
|
|
mHigherAccuracy = false;
|
|
|
|
}
|
2009-08-28 21:40:27 +04:00
|
|
|
|
|
|
|
nsresult Init();
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
// Management of the Geolocation objects
|
|
|
|
void AddLocator(mozilla::dom::Geolocation* locator);
|
|
|
|
void RemoveLocator(mozilla::dom::Geolocation* locator);
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2008-12-05 19:29:43 +03:00
|
|
|
void SetCachedPosition(nsIDOMGeoPosition* aPosition);
|
2014-07-07 08:47:00 +04:00
|
|
|
CachedPositionAndAccuracy GetCachedPosition();
|
2008-12-05 19:29:43 +03:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// Find and startup a geolocation device (gps, nmea, etc.)
|
2013-06-13 06:20:07 +04:00
|
|
|
nsresult StartDevice(nsIPrincipal* aPrincipal);
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
// Stop the started geolocation device (gps, nmea, etc.)
|
|
|
|
void StopDevice();
|
2012-11-01 04:19:34 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// create, or reinitalize the callback timer
|
|
|
|
void SetDisconnectTimer();
|
|
|
|
|
2013-10-20 06:14:00 +04:00
|
|
|
// Update the accuracy and notify the provider if changed
|
|
|
|
void UpdateAccuracy(bool aForceHigh = false);
|
2013-02-26 21:27:31 +04:00
|
|
|
bool HighAccuracyRequested();
|
2012-03-21 21:52:35 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
private:
|
|
|
|
|
2008-08-14 20:35:49 +04:00
|
|
|
~nsGeolocationService();
|
2008-07-16 03:37:48 +04: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-30 07:22:27 +04:00
|
|
|
nsCOMPtr<nsIGeolocationProvider> mProvider;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
// mGeolocators are not owned here. Their constructor
|
2009-07-11 04:04:39 +04:00
|
|
|
// adds them to this list, and their destructor removes
|
2008-07-16 03:37:48 +04:00
|
|
|
// them from this list.
|
2013-04-12 22:47:47 +04:00
|
|
|
nsTArray<mozilla::dom::Geolocation*> mGeolocators;
|
2008-12-05 19:29:43 +03:00
|
|
|
|
|
|
|
// This is the last geo position that we have seen.
|
2014-07-07 08:47:00 +04:00
|
|
|
CachedPositionAndAccuracy mLastPosition;
|
2012-03-21 21:52:35 +04:00
|
|
|
|
|
|
|
// Current state of requests for higher accuracy
|
|
|
|
bool mHigherAccuracy;
|
2008-07-16 03:37:48 +04:00
|
|
|
};
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Can return a geolocation info
|
2012-11-01 04:19:34 +04:00
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class Geolocation final : public nsIDOMGeoGeolocation,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIGeolocationUpdate,
|
2016-10-05 01:12:16 +03:00
|
|
|
public nsWrapperCache
|
2008-07-16 03:37:48 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-01-15 04:10:46 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Geolocation, nsIDOMGeoGeolocation)
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-06-19 23:08:11 +04:00
|
|
|
NS_DECL_NSIGEOLOCATIONUPDATE
|
2013-04-12 22:46:36 +04:00
|
|
|
NS_DECL_NSIDOMGEOGEOLOCATION
|
2009-01-15 04:10:46 +03:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation();
|
2009-11-12 18:14:45 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult Init(nsPIDOMWindowInner* aContentDom = nullptr);
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* GetParentObject() const;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext *aCtx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2016-11-15 20:46:32 +03:00
|
|
|
int32_t WatchPosition(PositionCallback& aCallback,
|
|
|
|
PositionErrorCallback* aErrorCallback,
|
|
|
|
const PositionOptions& aOptions,
|
|
|
|
CallerType aCallerType,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
void GetCurrentPosition(PositionCallback& aCallback,
|
|
|
|
PositionErrorCallback* aErrorCallback,
|
|
|
|
const PositionOptions& aOptions,
|
|
|
|
CallerType aCallerType,
|
|
|
|
ErrorResult& aRv);
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// Returns true if any of the callbacks are repeating
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasActiveCallbacks();
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-06-19 23:08:10 +04:00
|
|
|
// Register an allowed request
|
|
|
|
void NotifyAllowedRequest(nsGeolocationRequest* aRequest);
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// Remove request from all callbacks arrays
|
2008-10-20 23:37:10 +04:00
|
|
|
void RemoveRequest(nsGeolocationRequest* request);
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2014-11-07 18:42:00 +03:00
|
|
|
// Check if there is already ClearWatch called for current
|
|
|
|
// request & clear if yes
|
|
|
|
bool ClearPendingRequest(nsGeolocationRequest* aRequest);
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// Shutting down.
|
2008-10-20 23:37:10 +04:00
|
|
|
void Shutdown();
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
// Getter for the principal that this Geolocation was loaded from
|
2012-07-30 18:58:26 +04:00
|
|
|
nsIPrincipal* GetPrincipal() { return mPrincipal; }
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
// Getter for the window that this Geolocation is owned by
|
2009-07-11 04:02:32 +04:00
|
|
|
nsIWeakReference* GetOwner() { return mOwner; }
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2016-02-20 03:30:09 +03:00
|
|
|
// Check to see if the window still exists
|
2011-09-29 10:19:26 +04:00
|
|
|
bool WindowOwnerStillExists();
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2013-02-26 21:27:31 +04:00
|
|
|
// Check to see if any active request requires high accuracy
|
|
|
|
bool HighAccuracyRequested();
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
private:
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
~Geolocation();
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
nsresult GetCurrentPosition(GeoPositionCallback aCallback,
|
|
|
|
GeoPositionErrorCallback aErrorCallback,
|
2016-11-15 20:46:32 +03:00
|
|
|
UniquePtr<PositionOptions>&& aOptions,
|
|
|
|
CallerType aCallerType);
|
2016-07-28 05:00:06 +03:00
|
|
|
nsresult WatchPosition(GeoPositionCallback aCallback,
|
|
|
|
GeoPositionErrorCallback aErrorCallback,
|
2016-11-15 20:46:32 +03:00
|
|
|
UniquePtr<PositionOptions>&& aOptions,
|
|
|
|
CallerType aCallerType,
|
|
|
|
int32_t* aRv);
|
2013-04-12 22:46:36 +04:00
|
|
|
|
2011-01-06 20:50:10 +03:00
|
|
|
bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
|
2010-05-13 21:44:51 +04:00
|
|
|
|
2014-11-07 18:42:00 +03:00
|
|
|
// Check if clearWatch is already called
|
|
|
|
bool IsAlreadyCleared(nsGeolocationRequest* aRequest);
|
|
|
|
|
2017-01-27 01:38:16 +03:00
|
|
|
// Returns whether the Geolocation object should block requests
|
|
|
|
// within a context that is not secure.
|
|
|
|
bool ShouldBlockInsecureRequests() const;
|
|
|
|
|
2008-07-16 03:37:48 +04: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
|
2013-06-19 23:08:10 +04:00
|
|
|
// there is a page change. All requests held by either array are active, that
|
|
|
|
// is, they have been allowed and expect to be fulfilled.
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsGeolocationRequest> > mPendingCallbacks;
|
|
|
|
nsTArray<RefPtr<nsGeolocationRequest> > mWatchingCallbacks;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
// window that this was created for. Weak reference.
|
2009-07-11 04:02:32 +04:00
|
|
|
nsWeakPtr mOwner;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
|
|
|
// where the content was loaded from
|
2012-07-30 18:58:26 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2008-07-16 03:37:48 +04:00
|
|
|
|
2015-12-03 21:12:11 +03:00
|
|
|
// the protocols we want to measure
|
|
|
|
enum class ProtocolType: uint8_t { OTHER, HTTP, HTTPS };
|
|
|
|
|
|
|
|
// the protocol used to load the content
|
|
|
|
ProtocolType mProtocolType;
|
|
|
|
|
2008-07-16 03:37:48 +04:00
|
|
|
// owning back pointer.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsGeolocationService> mService;
|
2012-11-01 04:19:34 +04:00
|
|
|
|
|
|
|
// Watch ID
|
|
|
|
uint32_t mLastWatchId;
|
|
|
|
|
2013-09-09 16:57:37 +04:00
|
|
|
// Pending requests are used when the service is not ready
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsGeolocationRequest> > mPendingRequests;
|
2014-11-07 18:42:00 +03:00
|
|
|
|
|
|
|
// Array containing already cleared watch IDs
|
|
|
|
nsTArray<int32_t> mClearedWatchIDs;
|
2008-07-16 03:37:48 +04:00
|
|
|
};
|
2010-05-13 21:44:51 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class PositionError final : public nsIDOMGeoPositionError,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsWrapperCache
|
2013-04-12 22:46:32 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PositionError)
|
|
|
|
|
|
|
|
NS_DECL_NSIDOMGEOPOSITIONERROR
|
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
PositionError(Geolocation* aParent, int16_t aCode);
|
2013-04-12 22:46:32 +04:00
|
|
|
|
2013-04-12 22:47:47 +04:00
|
|
|
Geolocation* GetParentObject() const;
|
2013-04-12 22:46:32 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-12 22:46:32 +04:00
|
|
|
|
|
|
|
int16_t Code() const {
|
|
|
|
return mCode;
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:46:36 +04:00
|
|
|
void NotifyCallback(const GeoPositionErrorCallback& callback);
|
2013-04-12 22:46:32 +04:00
|
|
|
private:
|
|
|
|
~PositionError();
|
|
|
|
int16_t mCode;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Geolocation> mParent;
|
2013-04-12 22:46:32 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
2013-06-19 23:08:11 +04:00
|
|
|
|
|
|
|
inline nsISupports*
|
|
|
|
ToSupports(dom::Geolocation* aGeolocation)
|
|
|
|
{
|
|
|
|
return ToSupports(static_cast<nsIDOMGeoGeolocation*>(aGeolocation));
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|
2013-04-12 22:46:32 +04:00
|
|
|
|
2010-06-05 01:14:43 +04:00
|
|
|
#endif /* nsGeoLocation_h */
|