Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / geolocation / GeolocationController.cpp
1 /*
2  * Copyright (C) 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. 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.
24  */
25
26 #include "config.h"
27
28 #include "modules/geolocation/GeolocationController.h"
29
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"
36
37 namespace blink {
38
39 GeolocationController::GeolocationController(LocalFrame& frame, GeolocationClient* client)
40     : PageLifecycleObserver(frame.page())
41     , m_client(client)
42     , m_hasClientForTest(false)
43     , m_isClientUpdating(false)
44     , m_inspectorAgent(nullptr)
45 {
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;
55     }
56
57     // m_inspectorAgent is 0 for out of process iframe instantiations, since inspector is currently unable
58     // to handle that scenario.
59     if (m_inspectorAgent)
60         m_inspectorAgent->addController(this);
61
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());
67     }
68 }
69
70 void GeolocationController::startUpdatingIfNeeded()
71 {
72     if (m_isClientUpdating)
73         return;
74     m_isClientUpdating = true;
75     m_client->startUpdating();
76 }
77
78 void GeolocationController::stopUpdatingIfNeeded()
79 {
80     if (!m_isClientUpdating)
81         return;
82     m_isClientUpdating = false;
83     m_client->stopUpdating();
84 }
85
86 GeolocationController::~GeolocationController()
87 {
88     ASSERT(m_observers.isEmpty());
89     detach();
90 }
91
92 void GeolocationController::detach()
93 {
94 #if !ENABLE(OILPAN)
95     if (page() && m_inspectorAgent) {
96         m_inspectorAgent->removeController(this);
97         m_inspectorAgent = nullptr;
98     }
99 #endif
100
101     if (m_hasClientForTest) {
102         m_client->controllerForTestRemoved(this);
103         m_hasClientForTest = false;
104     }
105 }
106
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()
110 {
111     detach();
112     if (m_client) {
113         m_client->geolocationDestroyed();
114         m_client = 0;
115     }
116 }
117
118 void GeolocationController::persistentHostHasBeenDestroyed()
119 {
120     observeContext(0);
121 }
122
123 PassOwnPtrWillBeRawPtr<GeolocationController> GeolocationController::create(LocalFrame& frame, GeolocationClient* client)
124 {
125     return adoptPtrWillBeNoop(new GeolocationController(frame, client));
126 }
127
128 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
129 {
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);
136
137     if (m_client) {
138         if (enableHighAccuracy)
139             m_client->setEnableHighAccuracy(true);
140         if (wasEmpty && page() && page()->visibilityState() == PageVisibilityStateVisible)
141             startUpdatingIfNeeded();
142     }
143 }
144
145 void GeolocationController::removeObserver(Geolocation* observer)
146 {
147     if (!m_observers.contains(observer))
148         return;
149
150     m_observers.remove(observer);
151     m_highAccuracyObservers.remove(observer);
152
153     if (m_client) {
154         if (m_observers.isEmpty())
155             stopUpdatingIfNeeded();
156         else if (m_highAccuracyObservers.isEmpty())
157             m_client->setEnableHighAccuracy(false);
158     }
159 }
160
161 void GeolocationController::requestPermission(Geolocation* geolocation)
162 {
163     if (m_client)
164         m_client->requestPermission(geolocation);
165 }
166
167 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
168 {
169     if (m_client)
170         m_client->cancelPermissionRequest(geolocation);
171 }
172
173 void GeolocationController::positionChanged(GeolocationPosition* position)
174 {
175     position = m_inspectorAgent->overrideGeolocationPosition(position);
176     if (!position) {
177         errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, "PositionUnavailable"));
178         return;
179     }
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();
185 }
186
187 void GeolocationController::errorOccurred(GeolocationError* error)
188 {
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);
193 }
194
195 GeolocationPosition* GeolocationController::lastPosition()
196 {
197     if (m_lastPosition.get())
198         return m_lastPosition.get();
199
200     if (!m_client)
201         return 0;
202
203     return m_client->lastPosition();
204 }
205
206 void GeolocationController::setClientForTest(GeolocationClient* client)
207 {
208     if (m_hasClientForTest)
209         m_client->controllerForTestRemoved(this);
210     m_client = client;
211     m_hasClientForTest = true;
212
213     client->controllerForTestAdded(this);
214 }
215
216 void GeolocationController::pageVisibilityChanged()
217 {
218     if (m_observers.isEmpty() || !m_client)
219         return;
220
221     if (page() && page()->visibilityState() == PageVisibilityStateVisible)
222         startUpdatingIfNeeded();
223     else
224         stopUpdatingIfNeeded();
225 }
226
227 const char* GeolocationController::supplementName()
228 {
229     return "GeolocationController";
230 }
231
232 void GeolocationController::trace(Visitor* visitor)
233 {
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);
239 }
240
241 void provideGeolocationTo(LocalFrame& frame, GeolocationClient* client)
242 {
243     WillBeHeapSupplement<LocalFrame>::provideTo(frame, GeolocationController::supplementName(), GeolocationController::create(frame, client));
244 }
245
246 } // namespace blink