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 #include "bindings/v8/ScriptWrappable.h" 31 #include "core/dom/ActiveDOMObject.h" 32 #include "core/platform/Timer.h" 33 #include "modules/geolocation/Geoposition.h" 34 #include "modules/geolocation/PositionCallback.h" 35 #include "modules/geolocation/PositionError.h" 36 #include "modules/geolocation/PositionErrorCallback.h" 37 #include "modules/geolocation/PositionOptions.h" 38 39 namespace WebCore { 40 41 class Document; 42 class Frame; 43 class GeolocationController; 44 class GeolocationError; 45 class GeolocationPosition; 46 class Page; 47 class ScriptExecutionContext; 48 49 class Geolocation : public ScriptWrappable, public RefCounted<Geolocation>, public ActiveDOMObject 50 { 51 public: 52 static PassRefPtr<Geolocation> create(ScriptExecutionContext*); 53 ~Geolocation(); 54 55 virtual void stop() OVERRIDE; 56 Document* document() const; 57 Frame* frame() const; 58 59 void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>); 60 int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>); 61 void clearWatch(int watchID); 62 63 void setIsAllowed(bool); 64 bool isAllowed() const { return m_allowGeolocation == Yes; } 65 66 void positionChanged(); 67 void setError(GeolocationError*); 68 69 private: 70 Geoposition* lastPosition(); 71 72 bool isDenied() const { return m_allowGeolocation == No; } 73 74 explicit Geolocation(ScriptExecutionContext*); 75 76 Page* page() const; 77 78 class GeoNotifier : public RefCounted<GeoNotifier> { 79 public: 80 static PassRefPtr<GeoNotifier> create(Geolocation* geolocation, PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options)); } 81 82 PositionOptions* options() const { return m_options.get(); }; 83 void setFatalError(PassRefPtr<PositionError>); 84 85 bool useCachedPosition() const { return m_useCachedPosition; } 86 void setUseCachedPosition(); 87 88 void runSuccessCallback(Geoposition*); 89 void runErrorCallback(PositionError*); 90 91 void startTimerIfNeeded(); 92 void stopTimer(); 93 void timerFired(Timer<GeoNotifier>*); 94 bool hasZeroTimeout() const; 95 96 private: 97 GeoNotifier(Geolocation*, PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>); 98 99 RefPtr<Geolocation> m_geolocation; 100 RefPtr<PositionCallback> m_successCallback; 101 RefPtr<PositionErrorCallback> m_errorCallback; 102 RefPtr<PositionOptions> m_options; 103 Timer<GeoNotifier> m_timer; 104 RefPtr<PositionError> m_fatalError; 105 bool m_useCachedPosition; 106 }; 107 108 typedef Vector<RefPtr<GeoNotifier> > GeoNotifierVector; 109 typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet; 110 111 class Watchers { 112 public: 113 bool add(int id, PassRefPtr<GeoNotifier>); 114 GeoNotifier* find(int id); 115 void remove(int id); 116 void remove(GeoNotifier*); 117 bool contains(GeoNotifier*) const; 118 void clear(); 119 bool isEmpty() const; 120 void getNotifiersVector(GeoNotifierVector&) const; 121 private: 122 typedef HashMap<int, RefPtr<GeoNotifier> > IdToNotifierMap; 123 typedef HashMap<RefPtr<GeoNotifier>, int> NotifierToIdMap; 124 IdToNotifierMap m_idToNotifierMap; 125 NotifierToIdMap m_notifierToIdMap; 126 }; 127 128 bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); } 129 130 void sendError(GeoNotifierVector&, PositionError*); 131 void sendPosition(GeoNotifierVector&, Geoposition*); 132 133 static void extractNotifiersWithCachedPosition(GeoNotifierVector& notifiers, GeoNotifierVector* cached); 134 static void copyToSet(const GeoNotifierVector&, GeoNotifierSet&); 135 136 static void stopTimer(GeoNotifierVector&); 137 void stopTimersForOneShots(); 138 void stopTimersForWatchers(); 139 void stopTimers(); 140 141 void cancelRequests(GeoNotifierVector&); 142 void cancelAllRequests(); 143 144 void makeSuccessCallbacks(); 145 void handleError(PositionError*); 146 147 void requestPermission(); 148 149 bool startUpdating(GeoNotifier*); 150 void stopUpdating(); 151 152 void handlePendingPermissionNotifiers(); 153 154 void startRequest(GeoNotifier*); 155 156 void fatalErrorOccurred(GeoNotifier*); 157 void requestTimedOut(GeoNotifier*); 158 void requestUsesCachedPosition(GeoNotifier*); 159 bool haveSuitableCachedPosition(PositionOptions*); 160 void makeCachedPositionCallbacks(); 161 162 GeoNotifierSet m_oneShots; 163 Watchers m_watchers; 164 GeoNotifierSet m_pendingForPermissionNotifiers; 165 RefPtr<Geoposition> m_lastPosition; 166 167 enum { 168 Unknown, 169 InProgress, 170 Yes, 171 No 172 } m_allowGeolocation; 173 174 GeoNotifierSet m_requestsAwaitingCachedPosition; 175 }; 176 177 } // namespace WebCore 178 179 #endif // Geolocation_h 180 181