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/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
10 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
11 #include "modules/sensor/SensorErrorEvent.h" | 12 #include "modules/sensor/SensorErrorEvent.h" |
12 #include "modules/sensor/SensorPollingStrategy.h" | 13 #include "modules/sensor/SensorPollingStrategy.h" |
13 #include "modules/sensor/SensorProviderProxy.h" | 14 #include "modules/sensor/SensorProviderProxy.h" |
14 #include "modules/sensor/SensorReading.h" | 15 #include "modules/sensor/SensorReading.h" |
15 #include "modules/sensor/SensorReadingEvent.h" | 16 #include "modules/sensor/SensorReadingEvent.h" |
16 | 17 |
17 using namespace device::mojom::blink; | 18 using namespace device::mojom::blink; |
18 | 19 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 DCHECK(m_polling); | 262 DCHECK(m_polling); |
262 m_polling->stopPolling(); | 263 m_polling->stopPolling(); |
263 return; | 264 return; |
264 } | 265 } |
265 | 266 |
266 DCHECK(m_sensorProxy); | 267 DCHECK(m_sensorProxy); |
267 DCHECK(m_sensorProxy->isInitialized()); | 268 DCHECK(m_sensorProxy->isInitialized()); |
268 m_sensorProxy->updateInternalReading(); | 269 m_sensorProxy->updateInternalReading(); |
269 | 270 |
270 DCHECK(m_sensorReading); | 271 DCHECK(m_sensorReading); |
271 if (m_sensorReading->isReadingUpdated(m_storedData)) | 272 if (getExecutionContext() && |
272 dispatchEvent( | 273 m_sensorReading->isReadingUpdated(m_storedData)) { |
273 SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); | 274 getExecutionContext()->postTask( |
| 275 BLINK_FROM_HERE, |
| 276 createSameThreadTask(&Sensor::notifySensorReadingChanged, |
| 277 wrapWeakPersistent(this))); |
| 278 } |
274 | 279 |
275 m_storedData = m_sensorProxy->reading(); | 280 m_storedData = m_sensorProxy->reading(); |
276 } | 281 } |
277 | 282 |
278 void Sensor::updateState(Sensor::SensorState newState) { | 283 void Sensor::updateState(Sensor::SensorState newState) { |
279 if (newState == m_state) | 284 if (newState == m_state) |
280 return; | 285 return; |
281 m_state = newState; | 286 m_state = newState; |
282 dispatchEvent(Event::create(EventTypeNames::statechange)); | 287 if (getExecutionContext()) { |
| 288 getExecutionContext()->postTask( |
| 289 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, |
| 290 wrapWeakPersistent(this))); |
| 291 } |
| 292 |
283 updatePollingStatus(); | 293 updatePollingStatus(); |
284 } | 294 } |
285 | 295 |
286 void Sensor::reportError() { | 296 void Sensor::reportError() { |
287 updateState(Sensor::SensorState::ERRORED); | 297 updateState(Sensor::SensorState::ERRORED); |
288 // TODO(Mikhail) : Dispatch Sensor Error event. | 298 // TODO(Mikhail) : Dispatch Sensor Error event. |
289 } | 299 } |
290 | 300 |
291 void Sensor::updatePollingStatus() { | 301 void Sensor::updatePollingStatus() { |
292 if (!m_polling) | 302 if (!m_polling) |
293 return; | 303 return; |
294 | 304 |
295 if (m_state != Sensor::SensorState::ACTIVE || | 305 if (m_state != Sensor::SensorState::ACTIVE || |
296 page()->visibilityState() != PageVisibilityStateVisible) { | 306 page()->visibilityState() != PageVisibilityStateVisible) { |
297 m_polling->stopPolling(); | 307 m_polling->stopPolling(); |
298 } else { | 308 } else { |
299 m_polling->startPolling(); | 309 m_polling->startPolling(); |
300 } | 310 } |
301 } | 311 } |
302 | 312 |
| 313 void Sensor::notifySensorReadingChanged() { |
| 314 dispatchEvent( |
| 315 SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); |
| 316 } |
| 317 |
| 318 void Sensor::notifyStateChanged() { |
| 319 dispatchEvent(Event::create(EventTypeNames::statechange)); |
| 320 } |
| 321 |
303 } // namespace blink | 322 } // namespace blink |
OLD | NEW |