2 * Copyright (C) 2008, 2009, 2010, 2011 Apple Inc. All Rights Reserved.
3 * Copyright 2010, The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #if ENABLE(GEOLOCATION)
31 #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"
40 #include <wtf/HashMap.h>
41 #include <wtf/HashSet.h>
47 class GeolocationError;
49 class ScriptExecutionContext;
51 struct PositionOptions;
53 class Geolocation : public ScriptWrappable, public RefCounted<Geolocation>, public ActiveDOMObject {
54 friend class GeoNotifier;
56 static Ref<Geolocation> create(ScriptExecutionContext*);
57 WEBCORE_EXPORT ~Geolocation();
59 WEBCORE_EXPORT void resetAllGeolocationPermission();
60 Document* document() const { return downcast<Document>(scriptExecutionContext()); }
61 Frame* frame() const { return document() ? document()->frame() : nullptr; }
63 void getCurrentPosition(Ref<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, PositionOptions&&);
64 int watchPosition(Ref<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, PositionOptions&&);
65 void clearWatch(int watchID);
67 WEBCORE_EXPORT void setIsAllowed(bool);
68 void resetIsAllowed() { m_allowGeolocation = Unknown; }
69 bool isAllowed() const { return m_allowGeolocation == Yes; }
71 void positionChanged();
72 void setError(GeolocationError*);
73 bool shouldBlockGeolocationRequests();
76 explicit Geolocation(ScriptExecutionContext*);
78 Geoposition* lastPosition();
82 bool canSuspendForDocumentSuspension() const override;
83 void suspend(ReasonForSuspension) override;
84 void resume() override;
85 const char* activeDOMObjectName() const override;
87 bool isDenied() const { return m_allowGeolocation == No; }
90 SecurityOrigin* securityOrigin() const;
92 typedef Vector<RefPtr<GeoNotifier>> GeoNotifierVector;
93 typedef HashSet<RefPtr<GeoNotifier>> GeoNotifierSet;
97 bool add(int id, RefPtr<GeoNotifier>&&);
98 GeoNotifier* find(int id);
100 void remove(GeoNotifier*);
101 bool contains(GeoNotifier*) const;
103 bool isEmpty() const;
104 void getNotifiersVector(GeoNotifierVector&) const;
106 typedef HashMap<int, RefPtr<GeoNotifier>> IdToNotifierMap;
107 typedef HashMap<RefPtr<GeoNotifier>, int> NotifierToIdMap;
108 IdToNotifierMap m_idToNotifierMap;
109 NotifierToIdMap m_notifierToIdMap;
112 bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
114 void sendError(GeoNotifierVector&, PositionError*);
115 void sendPosition(GeoNotifierVector&, Geoposition*);
117 static void extractNotifiersWithCachedPosition(GeoNotifierVector& notifiers, GeoNotifierVector* cached);
118 static void copyToSet(const GeoNotifierVector&, GeoNotifierSet&);
120 static void stopTimer(GeoNotifierVector&);
121 void stopTimersForOneShots();
122 void stopTimersForWatchers();
125 void cancelRequests(GeoNotifierVector&);
126 void cancelAllRequests();
128 void makeSuccessCallbacks();
129 void handleError(PositionError*);
131 void requestPermission();
133 bool startUpdating(GeoNotifier*);
136 void handlePendingPermissionNotifiers();
138 void startRequest(GeoNotifier*);
140 void fatalErrorOccurred(GeoNotifier*);
141 void requestTimedOut(GeoNotifier*);
142 void requestUsesCachedPosition(GeoNotifier*);
143 bool haveSuitableCachedPosition(const PositionOptions&);
144 void makeCachedPositionCallbacks();
146 GeoNotifierSet m_oneShots;
148 GeoNotifierSet m_pendingForPermissionNotifiers;
149 RefPtr<Geoposition> m_lastPosition;
156 } m_allowGeolocation;
158 bool m_resetOnResume;
159 bool m_hasChangedPosition;
160 RefPtr<PositionError> m_errorWaitingForResume;
162 void resumeTimerFired();
165 GeoNotifierSet m_requestsAwaitingCachedPosition;
168 } // namespace WebCore
170 #endif // ENABLE(GEOLOCATION)