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.
28 #include "modules/geolocation/GeolocationController.h"
30 #include "core/inspector/InspectorController.h"
31 #include "modules/geolocation/GeolocationClient.h"
32 #include "modules/geolocation/GeolocationError.h"
33 #include "modules/geolocation/GeolocationInspectorAgent.h"
34 #include "modules/geolocation/GeolocationPosition.h"
38 GeolocationController::GeolocationController(Page& page, GeolocationClient* client)
39 : PageLifecycleObserver(&page)
41 , m_hasClientForTest(false)
42 , m_isClientUpdating(false)
45 OwnPtr<GeolocationInspectorAgent> geolocationAgent(GeolocationInspectorAgent::create(this));
46 m_inspectorAgent = geolocationAgent.get();
47 page.inspectorController().registerModuleAgent(geolocationAgent.release());
50 void GeolocationController::startUpdatingIfNeeded()
52 if (m_isClientUpdating)
54 m_isClientUpdating = true;
55 m_client->startUpdating();
58 void GeolocationController::stopUpdatingIfNeeded()
60 if (!m_isClientUpdating)
62 m_isClientUpdating = false;
63 m_client->stopUpdating();
66 GeolocationController::~GeolocationController()
68 ASSERT(m_observers.isEmpty());
71 void GeolocationController::willBeDestroyed()
74 m_client->geolocationDestroyed();
77 PassOwnPtrWillBeRawPtr<GeolocationController> GeolocationController::create(Page& page, GeolocationClient* client)
79 return adoptPtrWillBeNoop(new GeolocationController(page, client));
82 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
84 // This may be called multiple times with the same observer, though removeObserver()
85 // is called only once with each.
86 bool wasEmpty = m_observers.isEmpty();
87 m_observers.add(observer);
88 if (enableHighAccuracy)
89 m_highAccuracyObservers.add(observer);
92 if (enableHighAccuracy)
93 m_client->setEnableHighAccuracy(true);
94 if (wasEmpty && page() && page()->visibilityState() == PageVisibilityStateVisible)
95 startUpdatingIfNeeded();
99 void GeolocationController::removeObserver(Geolocation* observer)
101 if (!m_observers.contains(observer))
104 m_observers.remove(observer);
105 m_highAccuracyObservers.remove(observer);
108 if (m_observers.isEmpty())
109 stopUpdatingIfNeeded();
110 else if (m_highAccuracyObservers.isEmpty())
111 m_client->setEnableHighAccuracy(false);
115 void GeolocationController::requestPermission(Geolocation* geolocation)
118 m_client->requestPermission(geolocation);
121 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
124 m_client->cancelPermissionRequest(geolocation);
127 void GeolocationController::positionChanged(GeolocationPosition* position)
129 position = m_inspectorAgent->overrideGeolocationPosition(position);
131 errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, "PositionUnavailable").get());
134 m_lastPosition = position;
135 WillBeHeapVector<RefPtrWillBeMember<Geolocation> > observersVector;
136 copyToVector(m_observers, observersVector);
137 for (size_t i = 0; i < observersVector.size(); ++i)
138 observersVector[i]->positionChanged();
141 void GeolocationController::errorOccurred(GeolocationError* error)
143 WillBeHeapVector<RefPtrWillBeMember<Geolocation> > observersVector;
144 copyToVector(m_observers, observersVector);
145 for (size_t i = 0; i < observersVector.size(); ++i)
146 observersVector[i]->setError(error);
149 GeolocationPosition* GeolocationController::lastPosition()
151 if (m_lastPosition.get())
152 return m_lastPosition.get();
157 return m_client->lastPosition();
160 void GeolocationController::setClientForTest(GeolocationClient* client)
163 m_hasClientForTest = true;
166 void GeolocationController::pageVisibilityChanged()
168 if (m_observers.isEmpty() || !m_client)
171 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
172 startUpdatingIfNeeded();
174 stopUpdatingIfNeeded();
177 const char* GeolocationController::supplementName()
179 return "GeolocationController";
182 void GeolocationController::trace(Visitor* visitor)
184 visitor->trace(m_lastPosition);
185 visitor->trace(m_observers);
186 visitor->trace(m_highAccuracyObservers);
187 WillBeHeapSupplement<Page>::trace(visitor);
190 void provideGeolocationTo(Page& page, GeolocationClient* client)
192 WillBeHeapSupplement<Page>::provideTo(page, GeolocationController::supplementName(), GeolocationController::create(page, client));
195 } // namespace WebCore