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 | #pragma once |
28 | |
29 | #if ENABLE(GEOLOCATION) |
30 | |
31 | #include "ActiveDOMObject.h" |
32 | #include "Document.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 | #include <wtf/HashMap.h> |
41 | #include <wtf/HashSet.h> |
42 | |
43 | namespace WebCore { |
44 | |
45 | class Frame; |
46 | class GeoNotifier; |
47 | class GeolocationError; |
48 | class Page; |
49 | class ScriptExecutionContext; |
50 | class SecurityOrigin; |
51 | struct PositionOptions; |
52 | |
53 | class Geolocation final : public ScriptWrappable, public RefCounted<Geolocation>, public ActiveDOMObject { |
54 | WTF_MAKE_ISO_ALLOCATED(Geolocation); |
55 | friend class GeoNotifier; |
56 | public: |
57 | static Ref<Geolocation> create(ScriptExecutionContext*); |
58 | WEBCORE_EXPORT ~Geolocation(); |
59 | |
60 | WEBCORE_EXPORT void resetAllGeolocationPermission(); |
61 | Document* document() const { return downcast<Document>(scriptExecutionContext()); } |
62 | Frame* frame() const { return document() ? document()->frame() : nullptr; } |
63 | |
64 | void getCurrentPosition(Ref<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, PositionOptions&&); |
65 | int watchPosition(Ref<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, PositionOptions&&); |
66 | void clearWatch(int watchID); |
67 | |
68 | WEBCORE_EXPORT void setIsAllowed(bool); |
69 | void resetIsAllowed() { m_allowGeolocation = Unknown; } |
70 | bool isAllowed() const { return m_allowGeolocation == Yes; } |
71 | |
72 | void positionChanged(); |
73 | void setError(GeolocationError&); |
74 | bool shouldBlockGeolocationRequests(); |
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(Geoposition&); |
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(const 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 | |