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 "core/page/Page.h"
32 #include "modules/geolocation/GeolocationClient.h"
33 #include "modules/geolocation/GeolocationError.h"
34 #include "modules/geolocation/GeolocationInspectorAgent.h"
35 #include "modules/geolocation/GeolocationPosition.h"
39 GeolocationController::GeolocationController(LocalFrame& frame, GeolocationClient* client)
40 : PageLifecycleObserver(frame.page())
42 , m_hasClientForTest(false)
43 , m_isClientUpdating(false)
44 , m_inspectorAgent(nullptr)
46 // FIXME: Once GeolocationInspectorAgent is per frame, there will be a 1:1 relationship between
47 // it and this class. Until then, there's one GeolocationInspectorAgent per page that the main
48 // frame is responsible for creating.
49 if (frame.isMainFrame()) {
50 OwnPtrWillBeRawPtr<GeolocationInspectorAgent> geolocationAgent(GeolocationInspectorAgent::create());
51 m_inspectorAgent = geolocationAgent.get();
52 frame.page()->inspectorController().registerModuleAgent(geolocationAgent.release());
53 } else if (frame.page()->mainFrame()->isLocalFrame()) {
54 m_inspectorAgent = GeolocationController::from(frame.page()->deprecatedLocalMainFrame())->m_inspectorAgent;
57 // m_inspectorAgent is 0 for out of process iframe instantiations, since inspector is currently unable
58 // to handle that scenario.
60 m_inspectorAgent->addController(this);
62 if (!frame.isMainFrame() && frame.page()->mainFrame()->isLocalFrame()) {
63 // internals.setGeolocationClientMock is per page.
64 GeolocationController* mainController = GeolocationController::from(frame.page()->deprecatedLocalMainFrame());
65 if (mainController->hasClientForTest())
66 setClientForTest(mainController->client());
70 void GeolocationController::startUpdatingIfNeeded()
72 if (m_isClientUpdating)
74 m_isClientUpdating = true;
75 m_client->startUpdating();
78 void GeolocationController::stopUpdatingIfNeeded()
80 if (!m_isClientUpdating)
82 m_isClientUpdating = false;
83 m_client->stopUpdating();
86 GeolocationController::~GeolocationController()
88 ASSERT(m_observers.isEmpty());
92 void GeolocationController::detach()
95 if (page() && m_inspectorAgent) {
96 m_inspectorAgent->removeController(this);
97 m_inspectorAgent = nullptr;
101 if (m_hasClientForTest) {
102 m_client->controllerForTestRemoved(this);
103 m_hasClientForTest = false;
107 // FIXME: Oilpan: Once GeolocationClient is on-heap m_client should be a strong
108 // pointer and |willBeDestroyed| can potentially be removed from Supplement.
109 void GeolocationController::willBeDestroyed()
113 m_client->geolocationDestroyed();
118 void GeolocationController::persistentHostHasBeenDestroyed()
123 PassOwnPtrWillBeRawPtr<GeolocationController> GeolocationController::create(LocalFrame& frame, GeolocationClient* client)
125 return adoptPtrWillBeNoop(new GeolocationController(frame, client));
128 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
130 // This may be called multiple times with the same observer, though removeObserver()
131 // is called only once with each.
132 bool wasEmpty = m_observers.isEmpty();
133 m_observers.add(observer);
134 if (enableHighAccuracy)
135 m_highAccuracyObservers.add(observer);
138 if (enableHighAccuracy)
139 m_client->setEnableHighAccuracy(true);
140 if (wasEmpty && page() && page()->visibilityState() == PageVisibilityStateVisible)
141 startUpdatingIfNeeded();
145 void GeolocationController::removeObserver(Geolocation* observer)
147 if (!m_observers.contains(observer))
150 m_observers.remove(observer);
151 m_highAccuracyObservers.remove(observer);
154 if (m_observers.isEmpty())
155 stopUpdatingIfNeeded();
156 else if (m_highAccuracyObservers.isEmpty())
157 m_client->setEnableHighAccuracy(false);
161 void GeolocationController::requestPermission(Geolocation* geolocation)
164 m_client->requestPermission(geolocation);
167 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
170 m_client->cancelPermissionRequest(geolocation);
173 void GeolocationController::positionChanged(GeolocationPosition* position)
175 position = m_inspectorAgent->overrideGeolocationPosition(position);
177 errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, "PositionUnavailable"));
180 m_lastPosition = position;
181 HeapVector<Member<Geolocation> > observersVector;
182 copyToVector(m_observers, observersVector);
183 for (size_t i = 0; i < observersVector.size(); ++i)
184 observersVector[i]->positionChanged();
187 void GeolocationController::errorOccurred(GeolocationError* error)
189 HeapVector<Member<Geolocation> > observersVector;
190 copyToVector(m_observers, observersVector);
191 for (size_t i = 0; i < observersVector.size(); ++i)
192 observersVector[i]->setError(error);
195 GeolocationPosition* GeolocationController::lastPosition()
197 if (m_lastPosition.get())
198 return m_lastPosition.get();
203 return m_client->lastPosition();
206 void GeolocationController::setClientForTest(GeolocationClient* client)
208 if (m_hasClientForTest)
209 m_client->controllerForTestRemoved(this);
211 m_hasClientForTest = true;
213 client->controllerForTestAdded(this);
216 void GeolocationController::pageVisibilityChanged()
218 if (m_observers.isEmpty() || !m_client)
221 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
222 startUpdatingIfNeeded();
224 stopUpdatingIfNeeded();
227 const char* GeolocationController::supplementName()
229 return "GeolocationController";
232 void GeolocationController::trace(Visitor* visitor)
234 visitor->trace(m_lastPosition);
235 visitor->trace(m_observers);
236 visitor->trace(m_highAccuracyObservers);
237 visitor->trace(m_inspectorAgent);
238 WillBeHeapSupplement<LocalFrame>::trace(visitor);
241 void provideGeolocationTo(LocalFrame& frame, GeolocationClient* client)
243 WillBeHeapSupplement<LocalFrame>::provideTo(frame, GeolocationController::supplementName(), GeolocationController::create(frame, client));