dom/src/geolocation/nsGeolocation.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/src/geolocation/nsGeolocation.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,249 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsGeoLocation_h
     1.9 +#define nsGeoLocation_h
    1.10 +
    1.11 +// Microsoft's API Name hackery sucks
    1.12 +#undef CreateEvent
    1.13 +
    1.14 +#include "mozilla/StaticPtr.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsAutoPtr.h"
    1.17 +#include "nsTArray.h"
    1.18 +#include "nsITimer.h"
    1.19 +#include "nsIObserver.h"
    1.20 +#include "nsWrapperCache.h"
    1.21 +
    1.22 +#include "nsWeakPtr.h"
    1.23 +#include "nsCycleCollectionParticipant.h"
    1.24 +
    1.25 +#include "nsGeoPosition.h"
    1.26 +#include "nsIDOMGeoGeolocation.h"
    1.27 +#include "nsIDOMGeoPosition.h"
    1.28 +#include "nsIDOMGeoPositionError.h"
    1.29 +#include "nsIDOMGeoPositionCallback.h"
    1.30 +#include "nsIDOMGeoPositionErrorCallback.h"
    1.31 +#include "mozilla/dom/GeolocationBinding.h"
    1.32 +#include "mozilla/dom/PositionErrorBinding.h"
    1.33 +#include "mozilla/dom/CallbackObject.h"
    1.34 +
    1.35 +#include "nsIGeolocationProvider.h"
    1.36 +#include "nsIContentPermissionPrompt.h"
    1.37 +#include "nsIDOMWindow.h"
    1.38 +#include "mozilla/Attributes.h"
    1.39 +
    1.40 +class nsGeolocationService;
    1.41 +class nsGeolocationRequest;
    1.42 +
    1.43 +namespace mozilla {
    1.44 +namespace dom {
    1.45 +class Geolocation;
    1.46 +typedef CallbackObjectHolder<PositionCallback, nsIDOMGeoPositionCallback> GeoPositionCallback;
    1.47 +typedef CallbackObjectHolder<PositionErrorCallback, nsIDOMGeoPositionErrorCallback> GeoPositionErrorCallback;
    1.48 +}
    1.49 +}
    1.50 +
    1.51 +/**
    1.52 + * Singleton that manages the geolocation provider
    1.53 + */
    1.54 +class nsGeolocationService MOZ_FINAL : public nsIGeolocationUpdate, public nsIObserver
    1.55 +{
    1.56 +public:
    1.57 +
    1.58 +  static already_AddRefed<nsGeolocationService> GetGeolocationService();
    1.59 +  static mozilla::StaticRefPtr<nsGeolocationService> sService;
    1.60 +
    1.61 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.62 +  NS_DECL_NSIGEOLOCATIONUPDATE
    1.63 +  NS_DECL_NSIOBSERVER
    1.64 +
    1.65 +  nsGeolocationService() {
    1.66 +      mHigherAccuracy = false;
    1.67 +  }
    1.68 +
    1.69 +  nsresult Init();
    1.70 +
    1.71 +  void HandleMozsettingChanged(const char16_t* aData);
    1.72 +  void HandleMozsettingValue(const bool aValue);
    1.73 +
    1.74 +  // Management of the Geolocation objects
    1.75 +  void AddLocator(mozilla::dom::Geolocation* locator);
    1.76 +  void RemoveLocator(mozilla::dom::Geolocation* locator);
    1.77 +
    1.78 +  void SetCachedPosition(nsIDOMGeoPosition* aPosition);
    1.79 +  nsIDOMGeoPosition* GetCachedPosition();
    1.80 +
    1.81 +  // Find and startup a geolocation device (gps, nmea, etc.)
    1.82 +  nsresult StartDevice(nsIPrincipal* aPrincipal);
    1.83 +
    1.84 +  // Stop the started geolocation device (gps, nmea, etc.)
    1.85 +  void     StopDevice();
    1.86 +
    1.87 +  // create, or reinitalize the callback timer
    1.88 +  void     SetDisconnectTimer();
    1.89 +
    1.90 +  // Update the accuracy and notify the provider if changed
    1.91 +  void     UpdateAccuracy(bool aForceHigh = false);
    1.92 +  bool     HighAccuracyRequested();
    1.93 +
    1.94 +private:
    1.95 +
    1.96 +  ~nsGeolocationService();
    1.97 +
    1.98 +  // Disconnect timer.  When this timer expires, it clears all pending callbacks
    1.99 +  // and closes down the provider, unless we are watching a point, and in that
   1.100 +  // case, we disable the disconnect timer.
   1.101 +  nsCOMPtr<nsITimer> mDisconnectTimer;
   1.102 +
   1.103 +  // The object providing geo location information to us.
   1.104 +  nsCOMPtr<nsIGeolocationProvider> mProvider;
   1.105 +
   1.106 +  // mGeolocators are not owned here.  Their constructor
   1.107 +  // adds them to this list, and their destructor removes
   1.108 +  // them from this list.
   1.109 +  nsTArray<mozilla::dom::Geolocation*> mGeolocators;
   1.110 +
   1.111 +  // This is the last geo position that we have seen.
   1.112 +  nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
   1.113 +
   1.114 +  // Current state of requests for higher accuracy
   1.115 +  bool mHigherAccuracy;
   1.116 +};
   1.117 +
   1.118 +namespace mozilla {
   1.119 +namespace dom {
   1.120 +
   1.121 +/**
   1.122 + * Can return a geolocation info
   1.123 + */
   1.124 +class Geolocation MOZ_FINAL : public nsIDOMGeoGeolocation,
   1.125 +                              public nsIGeolocationUpdate,
   1.126 +                              public nsWrapperCache
   1.127 +{
   1.128 +public:
   1.129 +
   1.130 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   1.131 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Geolocation, nsIDOMGeoGeolocation)
   1.132 +
   1.133 +  NS_DECL_NSIGEOLOCATIONUPDATE
   1.134 +  NS_DECL_NSIDOMGEOGEOLOCATION
   1.135 +
   1.136 +  Geolocation();
   1.137 +
   1.138 +  nsresult Init(nsIDOMWindow* contentDom=nullptr);
   1.139 +
   1.140 +  nsIDOMWindow* GetParentObject() const;
   1.141 +  virtual JSObject* WrapObject(JSContext *aCtx) MOZ_OVERRIDE;
   1.142 +
   1.143 +  int32_t WatchPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
   1.144 +  void GetCurrentPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
   1.145 +
   1.146 +  void SetCachedPosition(Position* aPosition);
   1.147 +  Position* GetCachedPosition();
   1.148 +
   1.149 +  // Returns true if any of the callbacks are repeating
   1.150 +  bool HasActiveCallbacks();
   1.151 +
   1.152 +  // Register an allowed request
   1.153 +  void NotifyAllowedRequest(nsGeolocationRequest* aRequest);
   1.154 +
   1.155 +  // Remove request from all callbacks arrays
   1.156 +  void RemoveRequest(nsGeolocationRequest* request);
   1.157 +
   1.158 +  // Shutting down.
   1.159 +  void Shutdown();
   1.160 +
   1.161 +  // Getter for the principal that this Geolocation was loaded from
   1.162 +  nsIPrincipal* GetPrincipal() { return mPrincipal; }
   1.163 +
   1.164 +  // Getter for the window that this Geolocation is owned by
   1.165 +  nsIWeakReference* GetOwner() { return mOwner; }
   1.166 +
   1.167 +  // Check to see if the widnow still exists
   1.168 +  bool WindowOwnerStillExists();
   1.169 +
   1.170 +  // Check to see if any active request requires high accuracy
   1.171 +  bool HighAccuracyRequested();
   1.172 +
   1.173 +  // Notification from the service:
   1.174 +  void ServiceReady();
   1.175 +
   1.176 +private:
   1.177 +
   1.178 +  ~Geolocation();
   1.179 +
   1.180 +  nsresult GetCurrentPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions);
   1.181 +  nsresult WatchPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions, int32_t* aRv);
   1.182 +
   1.183 +  bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
   1.184 +
   1.185 +  // Methods for the service when it's ready to process requests:
   1.186 +  nsresult GetCurrentPositionReady(nsGeolocationRequest* aRequest);
   1.187 +  nsresult WatchPositionReady(nsGeolocationRequest* aRequest);
   1.188 +
   1.189 +  // Two callback arrays.  The first |mPendingCallbacks| holds objects for only
   1.190 +  // one callback and then they are released/removed from the array.  The second
   1.191 +  // |mWatchingCallbacks| holds objects until the object is explictly removed or
   1.192 +  // there is a page change. All requests held by either array are active, that
   1.193 +  // is, they have been allowed and expect to be fulfilled.
   1.194 +
   1.195 +  nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
   1.196 +  nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
   1.197 +
   1.198 +  // window that this was created for.  Weak reference.
   1.199 +  nsWeakPtr mOwner;
   1.200 +
   1.201 +  // where the content was loaded from
   1.202 +  nsCOMPtr<nsIPrincipal> mPrincipal;
   1.203 +
   1.204 +  // owning back pointer.
   1.205 +  nsRefPtr<nsGeolocationService> mService;
   1.206 +
   1.207 +  // cached Position wrapper
   1.208 +  nsRefPtr<Position> mCachedPosition;
   1.209 +
   1.210 +  // Watch ID
   1.211 +  uint32_t mLastWatchId;
   1.212 +
   1.213 +  // Pending requests are used when the service is not ready
   1.214 +  nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingRequests;
   1.215 +};
   1.216 +
   1.217 +class PositionError MOZ_FINAL : public nsIDOMGeoPositionError,
   1.218 +                                public nsWrapperCache
   1.219 +{
   1.220 +public:
   1.221 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   1.222 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PositionError)
   1.223 +
   1.224 +  NS_DECL_NSIDOMGEOPOSITIONERROR
   1.225 +
   1.226 +  PositionError(Geolocation* aParent, int16_t aCode);
   1.227 +
   1.228 +  Geolocation* GetParentObject() const;
   1.229 +
   1.230 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   1.231 +
   1.232 +  int16_t Code() const {
   1.233 +    return mCode;
   1.234 +  }
   1.235 +
   1.236 +  void NotifyCallback(const GeoPositionErrorCallback& callback);
   1.237 +private:
   1.238 +  ~PositionError();
   1.239 +  int16_t mCode;
   1.240 +  nsRefPtr<Geolocation> mParent;
   1.241 +};
   1.242 +
   1.243 +}
   1.244 +
   1.245 +inline nsISupports*
   1.246 +ToSupports(dom::Geolocation* aGeolocation)
   1.247 +{
   1.248 +  return ToSupports(static_cast<nsIDOMGeoGeolocation*>(aGeolocation));
   1.249 +}
   1.250 +}
   1.251 +
   1.252 +#endif /* nsGeoLocation_h */

mercurial