dom/src/geolocation/nsGeolocation.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:669a1d88cde7
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/. */
4
5 #ifndef nsGeoLocation_h
6 #define nsGeoLocation_h
7
8 // Microsoft's API Name hackery sucks
9 #undef CreateEvent
10
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"
18
19 #include "nsWeakPtr.h"
20 #include "nsCycleCollectionParticipant.h"
21
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"
31
32 #include "nsIGeolocationProvider.h"
33 #include "nsIContentPermissionPrompt.h"
34 #include "nsIDOMWindow.h"
35 #include "mozilla/Attributes.h"
36
37 class nsGeolocationService;
38 class nsGeolocationRequest;
39
40 namespace mozilla {
41 namespace dom {
42 class Geolocation;
43 typedef CallbackObjectHolder<PositionCallback, nsIDOMGeoPositionCallback> GeoPositionCallback;
44 typedef CallbackObjectHolder<PositionErrorCallback, nsIDOMGeoPositionErrorCallback> GeoPositionErrorCallback;
45 }
46 }
47
48 /**
49 * Singleton that manages the geolocation provider
50 */
51 class nsGeolocationService MOZ_FINAL : public nsIGeolocationUpdate, public nsIObserver
52 {
53 public:
54
55 static already_AddRefed<nsGeolocationService> GetGeolocationService();
56 static mozilla::StaticRefPtr<nsGeolocationService> sService;
57
58 NS_DECL_THREADSAFE_ISUPPORTS
59 NS_DECL_NSIGEOLOCATIONUPDATE
60 NS_DECL_NSIOBSERVER
61
62 nsGeolocationService() {
63 mHigherAccuracy = false;
64 }
65
66 nsresult Init();
67
68 void HandleMozsettingChanged(const char16_t* aData);
69 void HandleMozsettingValue(const bool aValue);
70
71 // Management of the Geolocation objects
72 void AddLocator(mozilla::dom::Geolocation* locator);
73 void RemoveLocator(mozilla::dom::Geolocation* locator);
74
75 void SetCachedPosition(nsIDOMGeoPosition* aPosition);
76 nsIDOMGeoPosition* GetCachedPosition();
77
78 // Find and startup a geolocation device (gps, nmea, etc.)
79 nsresult StartDevice(nsIPrincipal* aPrincipal);
80
81 // Stop the started geolocation device (gps, nmea, etc.)
82 void StopDevice();
83
84 // create, or reinitalize the callback timer
85 void SetDisconnectTimer();
86
87 // Update the accuracy and notify the provider if changed
88 void UpdateAccuracy(bool aForceHigh = false);
89 bool HighAccuracyRequested();
90
91 private:
92
93 ~nsGeolocationService();
94
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;
99
100 // The object providing geo location information to us.
101 nsCOMPtr<nsIGeolocationProvider> mProvider;
102
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;
107
108 // This is the last geo position that we have seen.
109 nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
110
111 // Current state of requests for higher accuracy
112 bool mHigherAccuracy;
113 };
114
115 namespace mozilla {
116 namespace dom {
117
118 /**
119 * Can return a geolocation info
120 */
121 class Geolocation MOZ_FINAL : public nsIDOMGeoGeolocation,
122 public nsIGeolocationUpdate,
123 public nsWrapperCache
124 {
125 public:
126
127 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
128 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Geolocation, nsIDOMGeoGeolocation)
129
130 NS_DECL_NSIGEOLOCATIONUPDATE
131 NS_DECL_NSIDOMGEOGEOLOCATION
132
133 Geolocation();
134
135 nsresult Init(nsIDOMWindow* contentDom=nullptr);
136
137 nsIDOMWindow* GetParentObject() const;
138 virtual JSObject* WrapObject(JSContext *aCtx) MOZ_OVERRIDE;
139
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);
142
143 void SetCachedPosition(Position* aPosition);
144 Position* GetCachedPosition();
145
146 // Returns true if any of the callbacks are repeating
147 bool HasActiveCallbacks();
148
149 // Register an allowed request
150 void NotifyAllowedRequest(nsGeolocationRequest* aRequest);
151
152 // Remove request from all callbacks arrays
153 void RemoveRequest(nsGeolocationRequest* request);
154
155 // Shutting down.
156 void Shutdown();
157
158 // Getter for the principal that this Geolocation was loaded from
159 nsIPrincipal* GetPrincipal() { return mPrincipal; }
160
161 // Getter for the window that this Geolocation is owned by
162 nsIWeakReference* GetOwner() { return mOwner; }
163
164 // Check to see if the widnow still exists
165 bool WindowOwnerStillExists();
166
167 // Check to see if any active request requires high accuracy
168 bool HighAccuracyRequested();
169
170 // Notification from the service:
171 void ServiceReady();
172
173 private:
174
175 ~Geolocation();
176
177 nsresult GetCurrentPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions);
178 nsresult WatchPosition(GeoPositionCallback& aCallback, GeoPositionErrorCallback& aErrorCallback, PositionOptions* aOptions, int32_t* aRv);
179
180 bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
181
182 // Methods for the service when it's ready to process requests:
183 nsresult GetCurrentPositionReady(nsGeolocationRequest* aRequest);
184 nsresult WatchPositionReady(nsGeolocationRequest* aRequest);
185
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.
191
192 nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
193 nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
194
195 // window that this was created for. Weak reference.
196 nsWeakPtr mOwner;
197
198 // where the content was loaded from
199 nsCOMPtr<nsIPrincipal> mPrincipal;
200
201 // owning back pointer.
202 nsRefPtr<nsGeolocationService> mService;
203
204 // cached Position wrapper
205 nsRefPtr<Position> mCachedPosition;
206
207 // Watch ID
208 uint32_t mLastWatchId;
209
210 // Pending requests are used when the service is not ready
211 nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingRequests;
212 };
213
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)
220
221 NS_DECL_NSIDOMGEOPOSITIONERROR
222
223 PositionError(Geolocation* aParent, int16_t aCode);
224
225 Geolocation* GetParentObject() const;
226
227 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
228
229 int16_t Code() const {
230 return mCode;
231 }
232
233 void NotifyCallback(const GeoPositionErrorCallback& callback);
234 private:
235 ~PositionError();
236 int16_t mCode;
237 nsRefPtr<Geolocation> mParent;
238 };
239
240 }
241
242 inline nsISupports*
243 ToSupports(dom::Geolocation* aGeolocation)
244 {
245 return ToSupports(static_cast<nsIDOMGeoGeolocation*>(aGeolocation));
246 }
247 }
248
249 #endif /* nsGeoLocation_h */

mercurial