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"
32 #include "Geoposition.h"
33 #include "PositionCallback.h"
34 #include "PositionError.h"
35 #include "PositionErrorCallback.h"
36 #include "PositionOptions.h"
37 #include "ScriptWrappable.h"
39 #include <wtf/HashMap.h>
40 #include <wtf/HashSet.h>
47 class GeolocationError;
49 class ScriptExecutionContext;
52 class Geolocation : public ScriptWrappable, public RefCounted<Geolocation>, public ActiveDOMObject
54 friend class GeoNotifier;
57 static Ref<Geolocation> create(ScriptExecutionContext*);
58 WEBCORE_EXPORT ~Geolocation();
60 WEBCORE_EXPORT void resetAllGeolocationPermission();
61 Document* document() const;
62 WEBCORE_EXPORT Frame* frame() const;
64 void getCurrentPosition(RefPtr<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, RefPtr<PositionOptions>&&);
65 int watchPosition(RefPtr<PositionCallback>&&, RefPtr<PositionErrorCallback>&&, RefPtr<PositionOptions>&&);
66 void clearWatch(int watchID);
68 WEBCORE_EXPORT void setIsAllowed(bool);
69 void resetIsAllowed() { m_allowGeolocation = Unknown; }
70 bool isAllowed() const { return m_allowGeolocation == Yes; }
72 void positionChanged();
73 void setError(GeolocationError*);
74 bool shouldBlockGeolocationRequests();
77 explicit Geolocation(ScriptExecutionContext*);
79 Geoposition* lastPosition();
83 bool canSuspendForDocumentSuspension() const override;
84 void suspend(ReasonForSuspension) override;
85 void resume() override;
86 const char* activeDOMObjectName() const override;
88 bool isDenied() const { return m_allowGeolocation == No; }
91 SecurityOrigin* securityOrigin() const;
93 typedef Vector<RefPtr<GeoNotifier>> GeoNotifierVector;
94 typedef HashSet<RefPtr<GeoNotifier>> GeoNotifierSet;
98 bool add(int id, RefPtr<GeoNotifier>&&);
99 GeoNotifier* find(int id);
101 void remove(GeoNotifier*);
102 bool contains(GeoNotifier*) const;
104 bool isEmpty() const;
105 void getNotifiersVector(GeoNotifierVector&) const;
107 typedef HashMap<int, RefPtr<GeoNotifier>> IdToNotifierMap;
108 typedef HashMap<RefPtr<GeoNotifier>, int> NotifierToIdMap;
109 IdToNotifierMap m_idToNotifierMap;
110 NotifierToIdMap m_notifierToIdMap;
113 bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
115 void sendError(GeoNotifierVector&, PositionError*);
116 void sendPosition(GeoNotifierVector&, Geoposition*);
118 static void extractNotifiersWithCachedPosition(GeoNotifierVector& notifiers, GeoNotifierVector* cached);
119 static void copyToSet(const GeoNotifierVector&, GeoNotifierSet&);
121 static void stopTimer(GeoNotifierVector&);
122 void stopTimersForOneShots();
123 void stopTimersForWatchers();
126 void cancelRequests(GeoNotifierVector&);
127 void cancelAllRequests();
129 void makeSuccessCallbacks();
130 void handleError(PositionError*);
132 void requestPermission();
134 bool startUpdating(GeoNotifier*);
137 void handlePendingPermissionNotifiers();
139 void startRequest(GeoNotifier*);
141 void fatalErrorOccurred(GeoNotifier*);
142 void requestTimedOut(GeoNotifier*);
143 void requestUsesCachedPosition(GeoNotifier*);
144 bool haveSuitableCachedPosition(PositionOptions*);
145 void makeCachedPositionCallbacks();
147 GeoNotifierSet m_oneShots;
149 GeoNotifierSet m_pendingForPermissionNotifiers;
150 RefPtr<Geoposition> m_lastPosition;
157 } m_allowGeolocation;
159 bool m_resetOnResume;
160 bool m_hasChangedPosition;
161 RefPtr<PositionError> m_errorWaitingForResume;
163 void resumeTimerFired();
166 GeoNotifierSet m_requestsAwaitingCachedPosition;
169 } // namespace WebCore
171 #endif // ENABLE(GEOLOCATION)