• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef Geolocation_h
27 #define Geolocation_h
28 
29 #include "EventListener.h"
30 #include "GeolocationService.h"
31 #include "Geoposition.h"
32 #include "PositionCallback.h"
33 #include "PositionError.h"
34 #include "PositionErrorCallback.h"
35 #include "PositionOptions.h"
36 #include "Timer.h"
37 #include <wtf/Platform.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/RefCounted.h>
43 #include <wtf/RefPtr.h>
44 #include <wtf/Vector.h>
45 
46 namespace WebCore {
47 
48 class Frame;
49 class CachedPositionManager;
50 
51 
52 class Geolocation : public GeolocationServiceClient, public EventListener {
53 public:
create(Frame * frame)54     static PassRefPtr<Geolocation> create(Frame* frame) { return adoptRef(new Geolocation(frame)); }
55 
56     virtual ~Geolocation();
57 
58     void disconnectFrame();
59 
lastPosition()60     Geoposition* lastPosition() const { return m_service->lastPosition(); }
61 
62     void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
63     int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
64     void clearWatch(int watchId);
65 
66     void suspend();
67     void resume();
68 
69     void setIsAllowed(bool);
isAllowed()70     bool isAllowed() const { return m_allowGeolocation == Yes; }
isDenied()71     bool isDenied() const { return m_allowGeolocation == No; }
72 
setShouldClearCache(bool shouldClearCache)73     void setShouldClearCache(bool shouldClearCache) { m_shouldClearCache = shouldClearCache; }
shouldClearCache()74     bool shouldClearCache() const { return m_shouldClearCache; }
75 
76     static void setDatabasePath(String);
77 
78 private:
79     Geolocation(Frame*);
80 
81     class GeoNotifier : public RefCounted<GeoNotifier> {
82     public:
create(Geolocation * geolocation,PassRefPtr<PositionCallback> positionCallback,PassRefPtr<PositionErrorCallback> positionErrorCallback,PassRefPtr<PositionOptions> options)83         static PassRefPtr<GeoNotifier> create(Geolocation* geolocation, PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options)); }
84 
85         void setFatalError(PassRefPtr<PositionError> error);
86         void setCachedPosition(Geoposition* cachedPosition);
87         void startTimerIfNeeded();
88         void timerFired(Timer<GeoNotifier>*);
89 
90         Geolocation* m_geolocation;
91         RefPtr<PositionCallback> m_successCallback;
92         RefPtr<PositionErrorCallback> m_errorCallback;
93         RefPtr<PositionOptions> m_options;
94         Timer<GeoNotifier> m_timer;
95         RefPtr<PositionError> m_fatalError;
96         RefPtr<Geoposition> m_cachedPosition;
97 
98     private:
99         GeoNotifier(Geolocation* geolocation, PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
100     };
101 
hasListeners()102     bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
103 
104     void sendError(Vector<RefPtr<GeoNotifier> >&, PositionError*);
105     void sendPosition(Vector<RefPtr<GeoNotifier> >&, Geoposition*);
106 
107     static void stopTimer(Vector<RefPtr<GeoNotifier> >&);
108     void stopTimersForOneShots();
109     void stopTimersForWatchers();
110     void stopTimers();
111 
112     void makeSuccessCallbacks();
113     void handleError(PositionError*);
114 
115     void requestPermission();
116     PassRefPtr<GeoNotifier> makeRequest(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
117 
118     // GeolocationServiceClient
119     virtual void geolocationServicePositionChanged(GeolocationService*);
120     virtual void geolocationServiceErrorOccurred(GeolocationService*);
121 
122     // EventListener
123     virtual void handleEvent(Event*, bool isWindowEvent);
124 
125     void fatalErrorOccurred(GeoNotifier* notifier);
126     void requestTimedOut(GeoNotifier* notifier);
127     void requestReturnedCachedPosition(GeoNotifier* notifier);
128     bool haveSuitableCachedPosition(PositionOptions*);
129 
130     typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet;
131     typedef HashMap<int, RefPtr<GeoNotifier> > GeoNotifierMap;
132 
133     GeoNotifierSet m_oneShots;
134     GeoNotifierMap m_watchers;
135     Frame* m_frame;
136     OwnPtr<GeolocationService> m_service;
137 
138     enum {
139         Unknown,
140         InProgress,
141         Yes,
142         No
143     } m_allowGeolocation;
144     bool m_shouldClearCache;
145 
146     CachedPositionManager* m_cachedPositionManager;
147     GeoNotifierSet m_requestsAwaitingCachedPosition;
148 };
149 
150 } // namespace WebCore
151 
152 #endif // Geolocation_h
153