1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Geolocation.
15 : *
16 : * The Initial Developer of the Original Code is Mozilla Foundation
17 : * Portions created by the Initial Developer are Copyright (C) 2008
18 : * the Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * Doug Turner <dougt@meer.net> (Original Author)
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef nsGeoLocation_h
38 : #define nsGeoLocation_h
39 :
40 : #include "mozilla/dom/PContentPermissionRequestChild.h"
41 : // Microsoft's API Name hackery sucks
42 : #undef CreateEvent
43 :
44 : #include "nsCOMPtr.h"
45 : #include "nsAutoPtr.h"
46 : #include "nsCOMArray.h"
47 : #include "nsTArray.h"
48 : #include "nsITimer.h"
49 : #include "nsIObserver.h"
50 : #include "nsIURI.h"
51 :
52 : #include "nsWeakPtr.h"
53 : #include "nsCycleCollectionParticipant.h"
54 :
55 : #include "nsIDOMGeoGeolocation.h"
56 : #include "nsIDOMGeoPosition.h"
57 : #include "nsIDOMGeoPositionError.h"
58 : #include "nsIDOMGeoPositionCallback.h"
59 : #include "nsIDOMGeoPositionErrorCallback.h"
60 : #include "nsIDOMGeoPositionOptions.h"
61 : #include "nsIDOMNavigatorGeolocation.h"
62 :
63 : #include "nsPIDOMWindow.h"
64 :
65 : #include "nsIGeolocationProvider.h"
66 : #include "nsIContentPermissionPrompt.h"
67 :
68 : #include "PCOMContentPermissionRequestChild.h"
69 :
70 : class nsGeolocationService;
71 : class nsGeolocation;
72 :
73 : class nsGeolocationRequest
74 : : public nsIContentPermissionRequest
75 : , public nsITimerCallback
76 : , public PCOMContentPermissionRequestChild
77 : {
78 : public:
79 1 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
80 : NS_DECL_NSICONTENTPERMISSIONREQUEST
81 : NS_DECL_NSITIMERCALLBACK
82 :
83 1488 : NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsGeolocationRequest, nsIContentPermissionRequest)
84 :
85 : nsGeolocationRequest(nsGeolocation* locator,
86 : nsIDOMGeoPositionCallback* callback,
87 : nsIDOMGeoPositionErrorCallback* errorCallback,
88 : nsIDOMGeoPositionOptions* options,
89 : bool watchPositionRequest = false);
90 : nsresult Init();
91 : void Shutdown();
92 :
93 : // Called by the geolocation device to notify that a location has changed.
94 : bool Update(nsIDOMGeoPosition* aPosition);
95 :
96 : void SendLocation(nsIDOMGeoPosition* location);
97 : void MarkCleared();
98 1 : bool IsActive() {return !mCleared;}
99 : bool Allowed() {return mAllowed;}
100 : void SetTimeoutTimer();
101 :
102 : ~nsGeolocationRequest();
103 :
104 : bool Recv__delete__(const bool& allow);
105 0 : void IPDLRelease() { Release(); }
106 :
107 : private:
108 :
109 : void NotifyError(PRInt16 errorCode);
110 : bool mAllowed;
111 : bool mCleared;
112 : bool mIsWatchPositionRequest;
113 :
114 : nsCOMPtr<nsITimer> mTimeoutTimer;
115 : nsCOMPtr<nsIDOMGeoPositionCallback> mCallback;
116 : nsCOMPtr<nsIDOMGeoPositionErrorCallback> mErrorCallback;
117 : nsCOMPtr<nsIDOMGeoPositionOptions> mOptions;
118 :
119 : nsRefPtr<nsGeolocation> mLocator;
120 : };
121 :
122 : /**
123 : * Singleton that manages the geolocation provider
124 : */
125 : class nsGeolocationService : public nsIGeolocationUpdate, public nsIObserver
126 : {
127 : public:
128 :
129 : static nsGeolocationService* GetGeolocationService();
130 : static nsGeolocationService* GetInstance(); // Non-Addref'ing
131 : static nsGeolocationService* gService;
132 :
133 : NS_DECL_ISUPPORTS
134 : NS_DECL_NSIGEOLOCATIONUPDATE
135 : NS_DECL_NSIOBSERVER
136 :
137 1 : nsGeolocationService() {}
138 :
139 : nsresult Init();
140 :
141 : // Management of the nsGeolocation objects
142 : void AddLocator(nsGeolocation* locator);
143 : void RemoveLocator(nsGeolocation* locator);
144 :
145 : void SetCachedPosition(nsIDOMGeoPosition* aPosition);
146 : nsIDOMGeoPosition* GetCachedPosition();
147 :
148 : // Find and startup a geolocation device (gps, nmea, etc.)
149 : nsresult StartDevice();
150 :
151 : // Stop the started geolocation device (gps, nmea, etc.)
152 : void StopDevice();
153 :
154 : // create, or reinitalize the callback timer
155 : void SetDisconnectTimer();
156 :
157 : private:
158 :
159 : ~nsGeolocationService();
160 :
161 : // Disconnect timer. When this timer expires, it clears all pending callbacks
162 : // and closes down the provider, unless we are watching a point, and in that
163 : // case, we disable the disconnect timer.
164 : nsCOMPtr<nsITimer> mDisconnectTimer;
165 :
166 : // The object providing geo location information to us.
167 : nsCOMArray<nsIGeolocationProvider> mProviders;
168 :
169 : // mGeolocators are not owned here. Their constructor
170 : // adds them to this list, and their destructor removes
171 : // them from this list.
172 : nsTArray<nsGeolocation*> mGeolocators;
173 :
174 : // This is the last geo position that we have seen.
175 : nsCOMPtr<nsIDOMGeoPosition> mLastPosition;
176 : };
177 :
178 :
179 : /**
180 : * Can return a geolocation info
181 : */
182 : class nsGeolocation : public nsIDOMGeoGeolocation
183 : {
184 : public:
185 :
186 2 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
187 : NS_DECL_NSIDOMGEOGEOLOCATION
188 :
189 1497 : NS_DECL_CYCLE_COLLECTION_CLASS(nsGeolocation)
190 :
191 : nsGeolocation();
192 :
193 : nsresult Init(nsIDOMWindow* contentDom=nsnull);
194 :
195 : // Called by the geolocation device to notify that a location has changed.
196 : void Update(nsIDOMGeoPosition* aPosition);
197 :
198 : // Returns true if any of the callbacks are repeating
199 : bool HasActiveCallbacks();
200 :
201 : // Remove request from all callbacks arrays
202 : void RemoveRequest(nsGeolocationRequest* request);
203 :
204 : // Shutting down.
205 : void Shutdown();
206 :
207 : // Getter for the URI that this nsGeolocation was loaded from
208 0 : nsIURI* GetURI() { return mURI; }
209 :
210 : // Getter for the window that this nsGeolocation is owned by
211 0 : nsIWeakReference* GetOwner() { return mOwner; }
212 :
213 : // Check to see if the widnow still exists
214 : bool WindowOwnerStillExists();
215 :
216 : private:
217 :
218 : ~nsGeolocation();
219 :
220 : bool RegisterRequestWithPrompt(nsGeolocationRequest* request);
221 :
222 : // Two callback arrays. The first |mPendingCallbacks| holds objects for only
223 : // one callback and then they are released/removed from the array. The second
224 : // |mWatchingCallbacks| holds objects until the object is explictly removed or
225 : // there is a page change.
226 :
227 : nsTArray<nsRefPtr<nsGeolocationRequest> > mPendingCallbacks;
228 : nsTArray<nsRefPtr<nsGeolocationRequest> > mWatchingCallbacks;
229 :
230 : // window that this was created for. Weak reference.
231 : nsWeakPtr mOwner;
232 :
233 : // where the content was loaded from
234 : nsCOMPtr<nsIURI> mURI;
235 :
236 : // owning back pointer.
237 : nsRefPtr<nsGeolocationService> mService;
238 : };
239 :
240 : #endif /* nsGeoLocation_h */
|