1 | /* |
2 | * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All Rights Reserved. |
3 | * Copyright 2010, The Android Open Source Project |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #ifndef Geolocation_h |
28 | #define Geolocation_h |
29 | |
30 | #if ENABLE(GEOLOCATION) |
31 | |
32 | #include "ActiveDOMObject.h" |
33 | #include "Geoposition.h" |
34 | #include "PositionCallback.h" |
35 | #include "PositionError.h" |
36 | #include "PositionErrorCallback.h" |
37 | #include "PositionOptions.h" |
38 | #include "ScriptWrappable.h" |
39 | #include "Timer.h" |
40 | |
41 | namespace WebCore { |
42 | |
43 | class Document; |
44 | class Frame; |
45 | class GeoNotifier; |
46 | class GeolocationController; |
47 | class GeolocationError; |
48 | class GeolocationPosition; |
49 | class Page; |
50 | class ScriptExecutionContext; |
51 | class SecurityOrigin; |
52 | |
53 | class Geolocation : public ScriptWrappable, public RefCounted<Geolocation>, public ActiveDOMObject |
54 | { |
55 | friend class GeoNotifier; |
56 | |
57 | public: |
58 | static Ref<Geolocation> create(ScriptExecutionContext*); |
59 | WEBCORE_EXPORT ~Geolocation(); |
60 | |
61 | WEBCORE_EXPORT void resetAllGeolocationPermission(); |
62 | Document* document() const; |
63 | WEBCORE_EXPORT Frame* frame() const; |
64 | |
65 | void getCurrentPosition(RefPtr<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, RefPtr<PositionOptions>&&); |
66 | int watchPosition(RefPtr<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, RefPtr<PositionOptions>&&); |
67 | void clearWatch(int watchID); |
68 | |
69 | WEBCORE_EXPORT void setIsAllowed(bool); |
70 | void resetIsAllowed() { m_allowGeolocation = Unknown; } |
71 | bool isAllowed() const { return m_allowGeolocation == Yes; } |
72 | |
73 | void positionChanged(); |
74 | void setError(GeolocationError*); |
75 | |
76 | private: |
77 | explicit Geolocation(ScriptExecutionContext*); |
78 | |
79 | Geoposition* lastPosition(); |
80 | |
81 | // ActiveDOMObject |
82 | void stop() override; |
83 | bool canSuspendForDocumentSuspension() const override; |
84 | void suspend(ReasonForSuspension) override; |
85 | void resume() override; |
86 | const char* activeDOMObjectName() const override; |
87 | |
88 | bool isDenied() const { return m_allowGeolocation == No; } |
89 | |
90 | Page* page() const; |
91 | SecurityOrigin* securityOrigin() const; |
92 | |
93 | typedef Vector<RefPtr<GeoNotifier>> GeoNotifierVector; |
94 | typedef HashSet<RefPtr<GeoNotifier>> GeoNotifierSet; |
95 | |
96 | class Watchers { |
97 | public: |
98 | bool add(int id, RefPtr<GeoNotifier>&&); |
99 | GeoNotifier* find(int id); |
100 | void remove(int id); |
101 | void remove(GeoNotifier*); |
102 | bool contains(GeoNotifier*) const; |
103 | void clear(); |
104 | bool isEmpty() const; |
105 | void getNotifiersVector(GeoNotifierVector&) const; |
106 | private: |
107 | typedef HashMap<int, RefPtr<GeoNotifier>> IdToNotifierMap; |
108 | typedef HashMap<RefPtr<GeoNotifier>, int> NotifierToIdMap; |
109 | IdToNotifierMap m_idToNotifierMap; |
110 | NotifierToIdMap m_notifierToIdMap; |
111 | }; |
112 | |
113 | bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); } |
114 | |
115 | void sendError(GeoNotifierVector&, PositionError*); |
116 | void sendPosition(GeoNotifierVector&, Geoposition*); |
117 | |
118 | static void (GeoNotifierVector& notifiers, GeoNotifierVector* cached); |
119 | static void copyToSet(const GeoNotifierVector&, GeoNotifierSet&); |
120 | |
121 | static void stopTimer(GeoNotifierVector&); |
122 | void stopTimersForOneShots(); |
123 | void stopTimersForWatchers(); |
124 | void stopTimers(); |
125 | |
126 | void cancelRequests(GeoNotifierVector&); |
127 | void cancelAllRequests(); |
128 | |
129 | void makeSuccessCallbacks(); |
130 | void handleError(PositionError*); |
131 | |
132 | void requestPermission(); |
133 | |
134 | bool startUpdating(GeoNotifier*); |
135 | void stopUpdating(); |
136 | |
137 | void handlePendingPermissionNotifiers(); |
138 | |
139 | void startRequest(GeoNotifier*); |
140 | |
141 | void fatalErrorOccurred(GeoNotifier*); |
142 | void requestTimedOut(GeoNotifier*); |
143 | void requestUsesCachedPosition(GeoNotifier*); |
144 | bool haveSuitableCachedPosition(PositionOptions*); |
145 | void makeCachedPositionCallbacks(); |
146 | |
147 | GeoNotifierSet m_oneShots; |
148 | Watchers m_watchers; |
149 | GeoNotifierSet m_pendingForPermissionNotifiers; |
150 | RefPtr<Geoposition> m_lastPosition; |
151 | |
152 | enum { |
153 | Unknown, |
154 | InProgress, |
155 | Yes, |
156 | No |
157 | } m_allowGeolocation; |
158 | bool m_isSuspended; |
159 | bool m_resetOnResume; |
160 | bool m_hasChangedPosition; |
161 | RefPtr<PositionError> m_errorWaitingForResume; |
162 | |
163 | void resumeTimerFired(); |
164 | Timer m_resumeTimer; |
165 | |
166 | GeoNotifierSet m_requestsAwaitingCachedPosition; |
167 | }; |
168 | |
169 | } // namespace WebCore |
170 | |
171 | #endif // ENABLE(GEOLOCATION) |
172 | |
173 | #endif // Geolocation_h |
174 | |
175 | |