]> git.saurik.com Git - iphone-api.git/blob - WebCore/Geolocation.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / Geolocation.h
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 "GeolocationService.h"
30 #include "PositionCallback.h"
31 #include "PositionErrorCallback.h"
32 #include "PositionOptions.h"
33 #include "Timer.h"
34 #include <wtf/Platform.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/OwnPtr.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
41
42 namespace WebCore {
43
44 class Frame;
45 class Geoposition;
46
47 class Geolocation : public RefCounted<Geolocation>, public GeolocationServiceClient {
48 public:
49 static PassRefPtr<Geolocation> create(Frame* frame) { return adoptRef(new Geolocation(frame)); }
50
51 virtual ~Geolocation() {}
52
53 void disconnectFrame();
54
55 Geoposition* lastPosition() const { return m_service->lastPosition(); }
56
57 void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
58 int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
59 void clearWatch(int watchId);
60
61 void suspend();
62 void resume();
63
64 void setIsAllowed(bool);
65 bool isAllowed() const { return m_allowGeolocation == Yes; }
66
67 void setShouldClearCache(bool shouldClearCache) { m_shouldClearCache = shouldClearCache; }
68 bool shouldClearCache() const { return m_shouldClearCache; }
69
70 private:
71 Geolocation(Frame*);
72
73 class GeoNotifier : public RefCounted<GeoNotifier> {
74 public:
75 static PassRefPtr<GeoNotifier> create(PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(positionCallback, positionErrorCallback, options)); }
76
77 void startTimer();
78 void timerFired(Timer<GeoNotifier>*);
79
80 RefPtr<PositionCallback> m_successCallback;
81 RefPtr<PositionErrorCallback> m_errorCallback;
82 RefPtr<PositionOptions> m_options;
83 Timer<GeoNotifier> m_timer;
84
85 private:
86 GeoNotifier(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
87 };
88
89 bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
90
91 void sendError(Vector<RefPtr<GeoNotifier> >&, PositionError*);
92 void sendErrorToOneShots(PositionError*);
93 void sendErrorToWatchers(PositionError*);
94
95 void sendPosition(Vector<RefPtr<GeoNotifier> >&, Geoposition*);
96 void sendPositionToOneShots(Geoposition*);
97 void sendPositionToWatchers(Geoposition*);
98
99 static void startTimer(Vector<RefPtr<GeoNotifier> >&);
100 void startTimersForOneShots();
101 void startTimersForWatchers();
102 void startTimers();
103
104 void handleError(PositionError*);
105
106 void requestPermission();
107
108 virtual void geolocationServicePositionChanged(GeolocationService*);
109 virtual void geolocationServiceErrorOccurred(GeolocationService*);
110 virtual void geolocationServiceCachePolicyChanged(GeolocationService*);
111
112 typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet;
113 typedef HashMap<int, RefPtr<GeoNotifier> > GeoNotifierMap;
114
115 GeoNotifierSet m_oneShots;
116 GeoNotifierMap m_watchers;
117 Frame* m_frame;
118 OwnPtr<GeolocationService> m_service;
119
120 enum {
121 Unknown,
122 Yes,
123 No
124 } m_allowGeolocation;
125 bool m_shouldClearCache;
126 };
127
128 } // namespace WebCore
129
130 #endif // Geolocation_h