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