OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/sensor/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/ExecutionContextTask.h" | 9 #include "core/dom/ExecutionContextTask.h" |
10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
12 #include "modules/sensor/SensorErrorEvent.h" | 12 #include "modules/sensor/SensorErrorEvent.h" |
13 #include "modules/sensor/SensorPollingStrategy.h" | 13 #include "modules/sensor/SensorPollingStrategy.h" |
14 #include "modules/sensor/SensorProviderProxy.h" | 14 #include "modules/sensor/SensorProviderProxy.h" |
15 #include "modules/sensor/SensorReading.h" | 15 #include "modules/sensor/SensorReading.h" |
16 #include "modules/sensor/SensorReadingEvent.h" | |
17 | 16 |
18 using namespace device::mojom::blink; | 17 using namespace device::mojom::blink; |
19 | 18 |
20 namespace blink { | 19 namespace blink { |
21 | 20 |
22 Sensor::Sensor(ScriptState* scriptState, | 21 Sensor::Sensor(ScriptState* scriptState, |
23 const SensorOptions& sensorOptions, | 22 const SensorOptions& sensorOptions, |
24 ExceptionState& exceptionState, | 23 ExceptionState& exceptionState, |
25 SensorType type) | 24 SensorType type) |
26 : ActiveScriptWrappable(this), | 25 : ActiveScriptWrappable(this), |
27 ContextLifecycleObserver(scriptState->getExecutionContext()), | 26 ContextLifecycleObserver(scriptState->getExecutionContext()), |
28 PageVisibilityObserver( | 27 PageVisibilityObserver( |
29 toDocument(scriptState->getExecutionContext())->page()), | 28 toDocument(scriptState->getExecutionContext())->page()), |
30 m_sensorOptions(sensorOptions), | 29 m_sensorOptions(sensorOptions), |
31 m_type(type), | 30 m_type(type), |
32 m_state(Sensor::SensorState::IDLE), | 31 m_state(Sensor::SensorState::IDLE) { |
33 m_storedData() { | |
34 // Check secure context. | 32 // Check secure context. |
35 String errorMessage; | 33 String errorMessage; |
36 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { | 34 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { |
37 exceptionState.throwDOMException(SecurityError, errorMessage); | 35 exceptionState.throwDOMException(SecurityError, errorMessage); |
38 return; | 36 return; |
39 } | 37 } |
40 | 38 |
41 // Check top-level browsing context. | 39 // Check top-level browsing context. |
42 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || | 40 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || |
43 !scriptState->domWindow()->frame()->isMainFrame()) { | 41 !scriptState->domWindow()->frame()->isMainFrame()) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 110 } |
113 return "idle"; | 111 return "idle"; |
114 } | 112 } |
115 | 113 |
116 // Getters | 114 // Getters |
117 String Sensor::state() const { | 115 String Sensor::state() const { |
118 return ToString(m_state); | 116 return ToString(m_state); |
119 } | 117 } |
120 | 118 |
121 SensorReading* Sensor::reading() const { | 119 SensorReading* Sensor::reading() const { |
122 return m_sensorReading.get(); | 120 if (m_state != Sensor::SensorState::ACTIVE) |
| 121 return nullptr; |
| 122 DCHECK(m_sensorProxy); |
| 123 return m_sensorProxy->sensorReading(); |
123 } | 124 } |
124 | 125 |
125 DEFINE_TRACE(Sensor) { | 126 DEFINE_TRACE(Sensor) { |
126 visitor->trace(m_sensorProxy); | 127 visitor->trace(m_sensorProxy); |
127 visitor->trace(m_sensorReading); | |
128 ActiveScriptWrappable::trace(visitor); | 128 ActiveScriptWrappable::trace(visitor); |
129 ContextLifecycleObserver::trace(visitor); | 129 ContextLifecycleObserver::trace(visitor); |
130 PageVisibilityObserver::trace(visitor); | 130 PageVisibilityObserver::trace(visitor); |
131 EventTargetWithInlineData::trace(visitor); | 131 EventTargetWithInlineData::trace(visitor); |
132 } | 132 } |
133 | 133 |
134 bool Sensor::hasPendingActivity() const { | 134 bool Sensor::hasPendingActivity() const { |
135 if (m_state == Sensor::SensorState::IDLE || | 135 if (m_state == Sensor::SensorState::IDLE || |
136 m_state == Sensor::SensorState::ERRORED) | 136 m_state == Sensor::SensorState::ERRORED) |
137 return false; | 137 return false; |
138 return hasEventListeners(); | 138 return hasEventListeners(); |
139 } | 139 } |
140 | 140 |
141 void Sensor::initSensorProxyIfNeeded() { | 141 void Sensor::initSensorProxyIfNeeded() { |
142 if (m_sensorProxy) | 142 if (m_sensorProxy) |
143 return; | 143 return; |
144 | 144 |
145 Document* document = toDocument(getExecutionContext()); | 145 Document* document = toDocument(getExecutionContext()); |
146 if (!document || !document->frame()) | 146 if (!document || !document->frame()) |
147 return; | 147 return; |
148 | 148 |
149 m_sensorProxy = | 149 auto provider = SensorProviderProxy::from(document->frame()); |
150 SensorProviderProxy::from(document->frame())->getOrCreateSensor(m_type); | 150 m_sensorProxy = provider->getSensor(m_type); |
| 151 |
| 152 if (!m_sensorProxy) { |
| 153 m_sensorProxy = |
| 154 provider->createSensor(m_type, createSensorReadingFactory()); |
| 155 } |
151 } | 156 } |
152 | 157 |
153 void Sensor::contextDestroyed() { | 158 void Sensor::contextDestroyed() { |
154 if (m_state == Sensor::SensorState::ACTIVE || | 159 if (m_state == Sensor::SensorState::ACTIVE || |
155 m_state == Sensor::SensorState::ACTIVATING) | 160 m_state == Sensor::SensorState::ACTIVATING) |
156 stopListening(); | 161 stopListening(); |
157 } | 162 } |
158 | 163 |
159 void Sensor::onSensorInitialized() { | 164 void Sensor::onSensorInitialized() { |
160 if (m_state != Sensor::SensorState::ACTIVATING) | 165 if (m_state != Sensor::SensorState::ACTIVATING) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 if (page()->visibilityState() != PageVisibilityStateVisible) { | 220 if (page()->visibilityState() != PageVisibilityStateVisible) { |
216 m_sensorProxy->suspend(); | 221 m_sensorProxy->suspend(); |
217 } else { | 222 } else { |
218 m_sensorProxy->resume(); | 223 m_sensorProxy->resume(); |
219 } | 224 } |
220 } | 225 } |
221 | 226 |
222 void Sensor::startListening() { | 227 void Sensor::startListening() { |
223 DCHECK(m_sensorProxy); | 228 DCHECK(m_sensorProxy); |
224 updateState(Sensor::SensorState::ACTIVATING); | 229 updateState(Sensor::SensorState::ACTIVATING); |
225 if (!m_sensorReading) { | |
226 m_sensorReading = createSensorReading(m_sensorProxy); | |
227 DCHECK(m_sensorReading); | |
228 } | |
229 | 230 |
230 m_sensorProxy->addObserver(this); | 231 m_sensorProxy->addObserver(this); |
231 if (!m_sensorProxy->isInitialized()) { | 232 if (!m_sensorProxy->isInitialized()) { |
232 m_sensorProxy->initialize(); | 233 m_sensorProxy->initialize(); |
233 return; | 234 return; |
234 } | 235 } |
235 | 236 |
236 if (!m_configuration) { | 237 if (!m_configuration) { |
237 m_configuration = | 238 m_configuration = |
238 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig()); | 239 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig()); |
239 DCHECK(m_configuration); | 240 DCHECK(m_configuration); |
240 } | 241 } |
241 | 242 |
242 auto startCallback = | 243 auto startCallback = |
243 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 244 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
244 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 245 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
245 std::move(startCallback)); | 246 std::move(startCallback)); |
246 } | 247 } |
247 | 248 |
248 void Sensor::stopListening() { | 249 void Sensor::stopListening() { |
249 DCHECK(m_sensorProxy); | 250 DCHECK(m_sensorProxy); |
250 m_sensorReading = nullptr; | |
251 updateState(Sensor::SensorState::IDLE); | 251 updateState(Sensor::SensorState::IDLE); |
252 | 252 |
253 if (m_sensorProxy->isInitialized()) { | 253 if (m_sensorProxy->isInitialized()) { |
254 auto callback = | 254 auto callback = |
255 WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this)); | 255 WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this)); |
256 DCHECK(m_configuration); | 256 DCHECK(m_configuration); |
257 m_sensorProxy->removeConfiguration(m_configuration->Clone(), | 257 m_sensorProxy->removeConfiguration(m_configuration->Clone(), |
258 std::move(callback)); | 258 std::move(callback)); |
259 } else { | 259 } else { |
260 m_sensorProxy->removeObserver(this); | 260 m_sensorProxy->removeObserver(this); |
261 } | 261 } |
262 } | 262 } |
263 | 263 |
264 void Sensor::pollForData() { | 264 void Sensor::pollForData() { |
265 if (m_state != Sensor::SensorState::ACTIVE) { | 265 if (m_state != Sensor::SensorState::ACTIVE) { |
266 DCHECK(m_polling); | 266 DCHECK(m_polling); |
267 m_polling->stopPolling(); | 267 m_polling->stopPolling(); |
268 return; | 268 return; |
269 } | 269 } |
270 | 270 |
271 DCHECK(m_sensorProxy); | 271 DCHECK(m_sensorProxy); |
272 DCHECK(m_sensorProxy->isInitialized()); | 272 DCHECK(m_sensorProxy->isInitialized()); |
273 m_sensorProxy->updateInternalReading(); | 273 m_sensorProxy->updateSensorReading(); |
274 | 274 |
275 DCHECK(m_sensorReading); | 275 DCHECK(m_sensorProxy->sensorReading()); |
276 if (getExecutionContext() && | 276 if (getExecutionContext() && |
277 m_sensorReading->isReadingUpdated(m_storedData)) { | 277 m_sensorProxy->sensorReading()->isReadingUpdated(m_storedData)) { |
278 getExecutionContext()->postTask( | 278 getExecutionContext()->postTask( |
279 BLINK_FROM_HERE, | 279 BLINK_FROM_HERE, |
280 createSameThreadTask(&Sensor::notifySensorReadingChanged, | 280 createSameThreadTask(&Sensor::notifySensorReadingChanged, |
281 wrapWeakPersistent(this))); | 281 wrapWeakPersistent(this))); |
282 } | 282 } |
283 | 283 |
284 m_storedData = m_sensorProxy->reading(); | 284 m_storedData = m_sensorProxy->sensorReading()->data(); |
285 } | 285 } |
286 | 286 |
287 void Sensor::updateState(Sensor::SensorState newState) { | 287 void Sensor::updateState(Sensor::SensorState newState) { |
288 if (newState == m_state) | 288 if (newState == m_state) |
289 return; | 289 return; |
290 m_state = newState; | 290 m_state = newState; |
291 if (getExecutionContext()) { | 291 if (getExecutionContext()) { |
292 getExecutionContext()->postTask( | 292 getExecutionContext()->postTask( |
293 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, | 293 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, |
294 wrapWeakPersistent(this))); | 294 wrapWeakPersistent(this))); |
(...skipping 22 matching lines...) Expand all Loading... |
317 | 317 |
318 if (m_state != Sensor::SensorState::ACTIVE || | 318 if (m_state != Sensor::SensorState::ACTIVE || |
319 page()->visibilityState() != PageVisibilityStateVisible) { | 319 page()->visibilityState() != PageVisibilityStateVisible) { |
320 m_polling->stopPolling(); | 320 m_polling->stopPolling(); |
321 } else { | 321 } else { |
322 m_polling->startPolling(); | 322 m_polling->startPolling(); |
323 } | 323 } |
324 } | 324 } |
325 | 325 |
326 void Sensor::notifySensorReadingChanged() { | 326 void Sensor::notifySensorReadingChanged() { |
327 dispatchEvent( | 327 dispatchEvent(Event::create(EventTypeNames::change)); |
328 SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); | |
329 } | 328 } |
330 | 329 |
331 void Sensor::notifyStateChanged() { | 330 void Sensor::notifyStateChanged() { |
332 dispatchEvent(Event::create(EventTypeNames::statechange)); | 331 dispatchEvent(Event::create(EventTypeNames::statechange)); |
333 } | 332 } |
334 | 333 |
335 void Sensor::notifyError(DOMException* error) { | 334 void Sensor::notifyError(DOMException* error) { |
336 dispatchEvent( | 335 dispatchEvent( |
337 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 336 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
338 } | 337 } |
339 | 338 |
340 } // namespace blink | 339 } // namespace blink |
OLD | NEW |