OLD | NEW |
| (Empty) |
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 "modules/geolocation/GeolocationController.h" | |
27 | |
28 #include "core/page/Page.h" | |
29 #include "modules/geolocation/GeolocationClient.h" | |
30 #include "modules/geolocation/GeolocationError.h" | |
31 #include "modules/geolocation/GeolocationPosition.h" | |
32 | |
33 namespace blink { | |
34 | |
35 GeolocationController::GeolocationController(LocalFrame& frame, GeolocationClien
t* client) | |
36 : PageLifecycleObserver(frame.page()) | |
37 , m_client(client) | |
38 , m_hasClientForTest(false) | |
39 , m_isClientUpdating(false) | |
40 { | |
41 if (!frame.isMainFrame() && frame.page()->mainFrame()->isLocalFrame()) { | |
42 // internals.setGeolocationClientMock is per page. | |
43 GeolocationController* mainController = GeolocationController::from(fram
e.page()->deprecatedLocalMainFrame()); | |
44 if (mainController->hasClientForTest()) | |
45 setClientForTest(mainController->client()); | |
46 } | |
47 } | |
48 | |
49 void GeolocationController::startUpdatingIfNeeded() | |
50 { | |
51 if (m_isClientUpdating) | |
52 return; | |
53 m_isClientUpdating = true; | |
54 m_client->startUpdating(); | |
55 } | |
56 | |
57 void GeolocationController::stopUpdatingIfNeeded() | |
58 { | |
59 if (!m_isClientUpdating) | |
60 return; | |
61 m_isClientUpdating = false; | |
62 m_client->stopUpdating(); | |
63 } | |
64 | |
65 GeolocationController::~GeolocationController() | |
66 { | |
67 ASSERT(m_observers.isEmpty()); | |
68 } | |
69 | |
70 GeolocationController* GeolocationController::create(LocalFrame& frame, Geolocat
ionClient* client) | |
71 { | |
72 return new GeolocationController(frame, client); | |
73 } | |
74 | |
75 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAc
curacy) | |
76 { | |
77 // This may be called multiple times with the same observer, though removeOb
server() | |
78 // is called only once with each. | |
79 bool wasEmpty = m_observers.isEmpty(); | |
80 m_observers.add(observer); | |
81 if (enableHighAccuracy) | |
82 m_highAccuracyObservers.add(observer); | |
83 | |
84 if (m_client) { | |
85 if (enableHighAccuracy) | |
86 m_client->setEnableHighAccuracy(true); | |
87 if (wasEmpty && page() && page()->isPageVisible()) | |
88 startUpdatingIfNeeded(); | |
89 } | |
90 } | |
91 | |
92 void GeolocationController::removeObserver(Geolocation* observer) | |
93 { | |
94 if (!m_observers.contains(observer)) | |
95 return; | |
96 | |
97 m_observers.remove(observer); | |
98 m_highAccuracyObservers.remove(observer); | |
99 | |
100 if (m_client) { | |
101 if (m_observers.isEmpty()) | |
102 stopUpdatingIfNeeded(); | |
103 else if (m_highAccuracyObservers.isEmpty()) | |
104 m_client->setEnableHighAccuracy(false); | |
105 } | |
106 } | |
107 | |
108 void GeolocationController::requestPermission(Geolocation* geolocation) | |
109 { | |
110 if (m_client) | |
111 m_client->requestPermission(geolocation); | |
112 } | |
113 | |
114 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation) | |
115 { | |
116 if (m_client) | |
117 m_client->cancelPermissionRequest(geolocation); | |
118 } | |
119 | |
120 void GeolocationController::positionChanged(GeolocationPosition* position) | |
121 { | |
122 if (!position) { | |
123 errorOccurred(GeolocationError::create(GeolocationError::PositionUnavail
able, "PositionUnavailable")); | |
124 return; | |
125 } | |
126 m_lastPosition = position; | |
127 HeapVector<Member<Geolocation>> observersVector; | |
128 copyToVector(m_observers, observersVector); | |
129 for (size_t i = 0; i < observersVector.size(); ++i) | |
130 observersVector[i]->positionChanged(); | |
131 } | |
132 | |
133 void GeolocationController::errorOccurred(GeolocationError* error) | |
134 { | |
135 HeapVector<Member<Geolocation>> observersVector; | |
136 copyToVector(m_observers, observersVector); | |
137 for (size_t i = 0; i < observersVector.size(); ++i) | |
138 observersVector[i]->setError(error); | |
139 } | |
140 | |
141 GeolocationPosition* GeolocationController::lastPosition() | |
142 { | |
143 if (m_lastPosition) | |
144 return m_lastPosition; | |
145 | |
146 if (!m_client) | |
147 return 0; | |
148 | |
149 return m_client->lastPosition(); | |
150 } | |
151 | |
152 void GeolocationController::setClientForTest(GeolocationClient* client) | |
153 { | |
154 if (m_hasClientForTest) | |
155 m_client->controllerForTestRemoved(this); | |
156 m_client = client; | |
157 m_hasClientForTest = true; | |
158 | |
159 client->controllerForTestAdded(this); | |
160 } | |
161 | |
162 void GeolocationController::pageVisibilityChanged() | |
163 { | |
164 if (m_observers.isEmpty() || !m_client) | |
165 return; | |
166 | |
167 if (page() && page()->isPageVisible()) | |
168 startUpdatingIfNeeded(); | |
169 else | |
170 stopUpdatingIfNeeded(); | |
171 } | |
172 | |
173 const char* GeolocationController::supplementName() | |
174 { | |
175 return "GeolocationController"; | |
176 } | |
177 | |
178 DEFINE_TRACE(GeolocationController) | |
179 { | |
180 visitor->trace(m_client); | |
181 visitor->trace(m_lastPosition); | |
182 visitor->trace(m_observers); | |
183 visitor->trace(m_highAccuracyObservers); | |
184 Supplement<LocalFrame>::trace(visitor); | |
185 PageLifecycleObserver::trace(visitor); | |
186 } | |
187 | |
188 void provideGeolocationTo(LocalFrame& frame, GeolocationClient* client) | |
189 { | |
190 Supplement<LocalFrame>::provideTo(frame, GeolocationController::supplementNa
me(), GeolocationController::create(frame, client)); | |
191 } | |
192 | |
193 } // namespace blink | |
OLD | NEW |