2 * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "GeolocationService.h"
30 #include "PositionCallback.h"
31 #include "PositionErrorCallback.h"
32 #include "PositionOptions.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>
47 class Geolocation
: public RefCounted
<Geolocation
>, public GeolocationServiceClient
{
49 static PassRefPtr
<Geolocation
> create(Frame
* frame
) { return adoptRef(new Geolocation(frame
)); }
51 virtual ~Geolocation() {}
53 void disconnectFrame();
55 Geoposition
* lastPosition() const { return m_service
->lastPosition(); }
57 void getCurrentPosition(PassRefPtr
<PositionCallback
>, PassRefPtr
<PositionErrorCallback
>, PassRefPtr
<PositionOptions
>);
58 int watchPosition(PassRefPtr
<PositionCallback
>, PassRefPtr
<PositionErrorCallback
>, PassRefPtr
<PositionOptions
>);
59 void clearWatch(int watchId
);
64 void setIsAllowed(bool);
65 bool isAllowed() const { return m_allowGeolocation
== Yes
; }
67 void setShouldClearCache(bool shouldClearCache
) { m_shouldClearCache
= shouldClearCache
; }
68 bool shouldClearCache() const { return m_shouldClearCache
; }
73 class GeoNotifier
: public RefCounted
<GeoNotifier
> {
75 static PassRefPtr
<GeoNotifier
> create(PassRefPtr
<PositionCallback
> positionCallback
, PassRefPtr
<PositionErrorCallback
> positionErrorCallback
, PassRefPtr
<PositionOptions
> options
) { return adoptRef(new GeoNotifier(positionCallback
, positionErrorCallback
, options
)); }
78 void timerFired(Timer
<GeoNotifier
>*);
80 RefPtr
<PositionCallback
> m_successCallback
;
81 RefPtr
<PositionErrorCallback
> m_errorCallback
;
82 RefPtr
<PositionOptions
> m_options
;
83 Timer
<GeoNotifier
> m_timer
;
86 GeoNotifier(PassRefPtr
<PositionCallback
>, PassRefPtr
<PositionErrorCallback
>, PassRefPtr
<PositionOptions
>);
89 bool hasListeners() const { return !m_oneShots
.isEmpty() || !m_watchers
.isEmpty(); }
91 void sendError(Vector
<RefPtr
<GeoNotifier
> >&, PositionError
*);
92 void sendErrorToOneShots(PositionError
*);
93 void sendErrorToWatchers(PositionError
*);
95 void sendPosition(Vector
<RefPtr
<GeoNotifier
> >&, Geoposition
*);
96 void sendPositionToOneShots(Geoposition
*);
97 void sendPositionToWatchers(Geoposition
*);
99 static void startTimer(Vector
<RefPtr
<GeoNotifier
> >&);
100 void startTimersForOneShots();
101 void startTimersForWatchers();
104 void handleError(PositionError
*);
106 void requestPermission();
108 virtual void geolocationServicePositionChanged(GeolocationService
*);
109 virtual void geolocationServiceErrorOccurred(GeolocationService
*);
110 virtual void geolocationServiceCachePolicyChanged(GeolocationService
*);
112 typedef HashSet
<RefPtr
<GeoNotifier
> > GeoNotifierSet
;
113 typedef HashMap
<int, RefPtr
<GeoNotifier
> > GeoNotifierMap
;
115 GeoNotifierSet m_oneShots
;
116 GeoNotifierMap m_watchers
;
118 OwnPtr
<GeolocationService
> m_service
;
124 } m_allowGeolocation
;
125 bool m_shouldClearCache
;
128 } // namespace WebCore
130 #endif // Geolocation_h