2 * Copyright (C) 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "GeolocationController.h"
28 #include "GeolocationPosition.h"
30 #if ENABLE(CLIENT_BASED_GEOLOCATION)
32 #include "GeolocationClient.h"
36 GeolocationController::GeolocationController(Page* page, GeolocationClient* client)
42 GeolocationController::~GeolocationController()
44 ASSERT(m_observers.isEmpty());
47 m_client->geolocationDestroyed();
50 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
52 // This may be called multiple times with the same observer, though removeObserver()
53 // is called only once with each.
54 bool wasEmpty = m_observers.isEmpty();
55 m_observers.add(observer);
56 if (enableHighAccuracy)
57 m_highAccuracyObservers.add(observer);
60 if (enableHighAccuracy)
61 m_client->setEnableHighAccuracy(true);
63 m_client->startUpdating();
67 void GeolocationController::removeObserver(Geolocation* observer)
69 if (!m_observers.contains(observer))
72 m_observers.remove(observer);
73 m_highAccuracyObservers.remove(observer);
76 if (m_observers.isEmpty())
77 m_client->stopUpdating();
78 else if (m_highAccuracyObservers.isEmpty())
79 m_client->setEnableHighAccuracy(false);
83 void GeolocationController::requestPermission(Geolocation* geolocation)
86 m_client->requestPermission(geolocation);
89 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
92 m_client->cancelPermissionRequest(geolocation);
95 void GeolocationController::positionChanged(GeolocationPosition* position)
97 m_lastPosition = position;
98 Vector<RefPtr<Geolocation> > observersVector;
99 copyToVector(m_observers, observersVector);
100 for (size_t i = 0; i < observersVector.size(); ++i)
101 observersVector[i]->positionChanged();
104 void GeolocationController::errorOccurred(GeolocationError* error)
106 Vector<RefPtr<Geolocation> > observersVector;
107 copyToVector(m_observers, observersVector);
108 for (size_t i = 0; i < observersVector.size(); ++i)
109 observersVector[i]->setError(error);
112 GeolocationPosition* GeolocationController::lastPosition()
114 if (m_lastPosition.get())
115 return m_lastPosition.get();
120 return m_client->lastPosition();
123 } // namespace WebCore
125 #endif // ENABLE(CLIENT_BASED_GEOLOCATION)