dom/src/geolocation/nsGeolocation.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef nsGeoLocation_h
michael@0 6 #define nsGeoLocation_h
michael@0 7
michael@0 8 // Microsoft's API Name hackery sucks
michael@0 9 #undef CreateEvent
michael@0 10
michael@0 11 #include "mozilla/StaticPtr.h"
michael@0 12 #include "nsCOMPtr.h"
michael@0 13 #include "nsAutoPtr.h"
michael@0 14 #include "nsTArray.h"
michael@0 15 #include "nsITimer.h"
michael@0 16 #include "nsIObserver.h"
michael@0 17 #include "nsWrapperCache.h"
michael@0 18
michael@0 19 #include "nsWeakPtr.h"
michael@0 20 #include "nsCycleCollectionParticipant.h"
michael@0 21
michael@0 22 #include "nsGeoPosition.h"
michael@0 23 #include "nsIDOMGeoGeolocation.h"
michael@0 24 #include "nsIDOMGeoPosition.h"
michael@0 25 #include "nsIDOMGeoPositionError.h"
michael@0 26 #include "nsIDOMGeoPositionCallback.h"
michael@0 27 #include "nsIDOMGeoPositionErrorCallback.h"
michael@0 28 #include "mozilla/dom/GeolocationBinding.h"
michael@0 29 #include "mozilla/dom/PositionErrorBinding.h"
michael@0 30 #include "mozilla/dom/CallbackObject.h"
michael@0 31
michael@0 32 #include "nsIGeolocationProvider.h"
michael@0 33 #include "nsIContentPermissionPrompt.h"
michael@0 34 #include "nsIDOMWindow.h"
michael@0 35 #include "mozilla/Attributes.h"
michael@0 36
michael@0 37 class nsGeolocationService;
michael@0 38 class nsGeolocationRequest;
michael@0 39
michael@0 40 namespace mozilla {
michael@0 41 namespace dom {
michael@0 42 class Geolocation;
michael@0 43 typedef CallbackObjectHolder<PositionCallback, nsIDOMGeoPositionCallback> GeoPositionCallback;
michael@0 44 typedef CallbackObjectHolder<PositionErrorCallback, nsIDOMGeoPositionErrorCallback> GeoPositionErrorCallback;
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 /**
michael@0 49 * Singleton that manages the geolocation provider
michael@0 50 */
michael@0 51 class nsGeolocationService MOZ_FINAL : public nsIGeolocationUpdate, public nsIObserver
michael@0 52 {
michael@0 53 public:
michael@0 54
michael@0 55 static already_AddRefed<nsGeolocationService> GetGeolocationService();
michael@0 56 static mozilla::StaticRefPtr<nsGeolocationService> sService;
michael@0 57
michael@0 58 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 59 NS_DECL_NSIGEOLOCATIONUPDATE
michael@0 60 NS_DECL_NSIOBSERVER
michael@0 61
michael@0 62 nsGeolocationService() {
michael@0 63 mHigherAccuracy = false;
michael@0 64 }
michael@0 65
michael@0 66 nsresult Init();
michael@0 67
michael@0 68 void HandleMozsettingChanged(const char16_t* aData);
michael@0 69 void HandleMozsettingValue(const bool aValue);
michael@0 70
michael@0 71 // Management of the Geolocation objects
michael@0 72 void AddLocator(mozilla::dom::Geolocation* locator);
michael@0 73 void RemoveLocator(mozilla::dom::Geolocation* locator);
michael@0 74
michael@0 75 void SetCachedPosition(nsIDOMGeoPosition* aPosition);
michael@0 76 nsIDOMGeoPosition* GetCachedPosition();
michael@0 77
michael@0 78 // Find and startup a geolocation device (gps, nmea, etc.)
michael@0 79 nsresult StartDevice(nsIPrincipal* aPrincipal);
michael@0 80
michael@0 81 // Stop the started geolocation device (gps, nmea, etc.)
michael@0 82 void StopDevice();
michael@0 83
michael@0 84 // create, or reinitalize the callback timer
michael@0 85 void SetDisconnectTimer();
michael@0 86
michael@0 87 // Update the accuracy and notify the provider if changed
michael@0 88 void UpdateAccuracy(bool aForceHigh = false);
michael@0 89 bool HighAccuracyRequested();
michael@0 90
michael@0 91 private:
michael@0 92
michael@0 93 ~nsGeolocationService();
michael@0 94
michael@0 95 // Disconnect timer. When this timer expires, it clears all pending callbacks
michael@0 96 // and closes down the provider, unless we are watching a point, and in that
michael@0 97 // case, we disable the disconnect timer.
michael@0 98 nsCOMPtr<nsITimer> mDisconnectTimer;
michael@0 99
michael@0 100 // The object providing geo location information to us.
michael@0 101 nsCOMPtr<nsIGeolocationProvider> mProvider;
michael@0 102
michael@0 103 // mGeolocators are not owned here. Their constructor
michael@0 104 // adds them to this list, and their destructor removes
michael@0 105 // them from this list.
michael@0 106 nsTArray<mozilla::dom::Geolocation*> mGeolocators;
michael@0 107
michael@0 108 // This is the last geo position that we have seen.
michael@0 109 nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
michael@0 110
michael@0 111 // Current state of requests for higher accuracy
michael@0 112 bool mHigherAccuracy;
michael@0 113 };
michael@0 114
michael@0 115 namespace mozilla {
michael@0 116 namespace dom {
michael@0 117
michael@0 118 /**
michael@0 119 * Can return a geolocation info
michael@0 120 */
michael@0 121 class Geolocation MOZ_FINAL : public nsIDOMGeoGeolocation,
michael@0 122 public nsIGeolocationUpdate,
michael@0 123 public nsWrapperCache
michael@0 124 {
michael@0 125 public:
michael@0 126
michael@0 127 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 128 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Geolocation, nsIDOMGeoGeolocation)
michael@0 129
michael@0 130 NS_DECL_NSIGEOLOCATIONUPDATE
michael@0 131 NS_DECL_NSIDOMGEOGEOLOCATION
michael@0 132
michael@0 133 Geolocation();
michael@0 134
michael@0 135 nsresult Init(nsIDOMWindow* contentDom=nullptr);
michael@0 136
michael@0 137 nsIDOMWindow* GetParentObject() const;
michael@0 138 virtual JSObject* WrapObject(JSContext *aCtx) MOZ_OVERRIDE;
michael@0 139
michael@0 140 int32_t WatchPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
michael@0 141 void GetCurrentPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
michael@0 142
michael@0 143 void SetCachedPosition(Position* aPosition);
michael@0 144 Position* GetCachedPosition();
michael@0 145
michael@0 146 // Returns true if any of the callbacks are repeating
michael@0 147 bool HasActiveCallbacks();
michael@0 148
michael@0 149 // Register an allowed request
michael@0 150 void NotifyAllowedRequest(nsGeolocationRequest* aRequest);
michael@0 151
michael@0 152 // Remove request from all callbacks arrays
michael@0 153 void RemoveRequest(nsGeolocationRequest* request);
michael@0 154
michael@0 155 // Shutting down.
michael@0 156 void Shutdown();
michael@0 157
michael@0 158 // Getter for the principal that this Geolocation was loaded from
michael@0 159 nsIPrincipal* GetPrincipal() { return mPrincipal; }
michael@0 160
michael@0 161 // Getter for the window that this Geolocation is owned by
michael@0 162 nsIWeakReference* GetOwner() { return mOwner; }
michael@0 163
michael@0 164 // Check to see if the widnow still exists
michael@0 165 bool WindowOwnerStillExists();
michael@0 166
michael@0 167 // Check to see if any active request requires high accuracy
michael@0 168 bool HighAccuracyRequested();
michael@0 169
michael@0 170 // Notification from the service:
michael@0 171 void ServiceReady();
michael@0 172
michael@0 173 private:
michael@0 174
michael@0 175 ~Geolocation();
michael@0 176
michael@0 177 nsresult GetCurrentPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions);
michael@0 178 nsresult WatchPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions, int32_t* aRv);
michael@0 179
michael@0 180 bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
michael@0 181
michael@0 182 // Methods for the service when it's ready to process requests:
michael@0 183 nsresult GetCurrentPositionReady(nsGeolocationRequest* aRequest);
michael@0 184 nsresult WatchPositionReady(nsGeolocationRequest* aRequest);
michael@0 185
michael@0 186 // Two callback arrays. The first |mPendingCallbacks| holds objects for only
michael@0 187 // one callback and then they are released/removed from the array. The second
michael@0 188 // |mWatchingCallbacks| holds objects until the object is explictly removed or
michael@0 189 // there is a page change. All requests held by either array are active, that
michael@0 190 // is, they have been allowed and expect to be fulfilled.
michael@0 191
michael@0 192 nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
michael@0 193 nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
michael@0 194
michael@0 195 // window that this was created for. Weak reference.
michael@0 196 nsWeakPtr mOwner;
michael@0 197
michael@0 198 // where the content was loaded from
michael@0 199 nsCOMPtr<nsIPrincipal> mPrincipal;
michael@0 200
michael@0 201 // owning back pointer.
michael@0 202 nsRefPtr<nsGeolocationService> mService;
michael@0 203
michael@0 204 // cached Position wrapper
michael@0 205 nsRefPtr<Position> mCachedPosition;
michael@0 206
michael@0 207 // Watch ID
michael@0 208 uint32_t mLastWatchId;
michael@0 209
michael@0 210 // Pending requests are used when the service is not ready
michael@0 211 nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingRequests;
michael@0 212 };
michael@0 213
michael@0 214 class PositionError MOZ_FINAL : public nsIDOMGeoPositionError,
michael@0 215 public nsWrapperCache
michael@0 216 {
michael@0 217 public:
michael@0 218 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 219 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PositionError)
michael@0 220
michael@0 221 NS_DECL_NSIDOMGEOPOSITIONERROR
michael@0 222
michael@0 223 PositionError(Geolocation* aParent, int16_t aCode);
michael@0 224
michael@0 225 Geolocation* GetParentObject() const;
michael@0 226
michael@0 227 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 228
michael@0 229 int16_t Code() const {
michael@0 230 return mCode;
michael@0 231 }
michael@0 232
michael@0 233 void NotifyCallback(const GeoPositionErrorCallback& callback);
michael@0 234 private:
michael@0 235 ~PositionError();
michael@0 236 int16_t mCode;
michael@0 237 nsRefPtr<Geolocation> mParent;
michael@0 238 };
michael@0 239
michael@0 240 }
michael@0 241
michael@0 242 inline nsISupports*
michael@0 243 ToSupports(dom::Geolocation* aGeolocation)
michael@0 244 {
michael@0 245 return ToSupports(static_cast<nsIDOMGeoGeolocation*>(aGeolocation));
michael@0 246 }
michael@0 247 }
michael@0 248
michael@0 249 #endif /* nsGeoLocation_h */

mercurial