| 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/SensorReadingUpdater.h" | 5 #include "modules/sensor/SensorReadingUpdater.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 8 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 9 #include "modules/sensor/SensorProxy.h" | 9 #include "modules/sensor/SensorProxy.h" |
| 10 #include "wtf/CurrentTime.h" | 10 #include "wtf/CurrentTime.h" |
| 11 | 11 |
| 12 using device::mojom::blink::ReportingMode; | 12 using device::mojom::blink::ReportingMode; |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensorProxy) | 16 SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensorProxy) |
| 17 : m_sensorProxy(sensorProxy), m_hasPendingAnimationFrameTask(false) {} | 17 : m_sensorProxy(sensorProxy), |
| 18 m_document(m_sensorProxy->document()), |
| 19 m_hasPendingAnimationFrameTask(false) {} |
| 18 | 20 |
| 19 void SensorReadingUpdater::enqueueAnimationFrameTask() { | 21 void SensorReadingUpdater::enqueueAnimationFrameTask() { |
| 22 if (!m_document || m_document->isDetached()) { |
| 23 // If the document has detached the scheduled callbacks |
| 24 // will never be called. |
| 25 m_hasPendingAnimationFrameTask = false; |
| 26 m_document = m_sensorProxy->document(); |
| 27 if (!m_document || m_document->isDetached()) |
| 28 return; |
| 29 } |
| 30 |
| 20 if (m_hasPendingAnimationFrameTask) | 31 if (m_hasPendingAnimationFrameTask) |
| 21 return; | 32 return; |
| 22 | 33 |
| 23 auto callback = WTF::bind(&SensorReadingUpdater::onAnimationFrame, | 34 auto callback = WTF::bind(&SensorReadingUpdater::onAnimationFrame, |
| 24 wrapWeakPersistent(this)); | 35 wrapWeakPersistent(this)); |
| 25 m_sensorProxy->document()->enqueueAnimationFrameTask(std::move(callback)); | 36 m_document->enqueueAnimationFrameTask(std::move(callback)); |
| 26 m_hasPendingAnimationFrameTask = true; | 37 m_hasPendingAnimationFrameTask = true; |
| 27 } | 38 } |
| 28 | 39 |
| 29 void SensorReadingUpdater::start() { | 40 void SensorReadingUpdater::start() { |
| 30 enqueueAnimationFrameTask(); | 41 enqueueAnimationFrameTask(); |
| 31 } | 42 } |
| 32 | 43 |
| 33 void SensorReadingUpdater::onAnimationFrame() { | 44 void SensorReadingUpdater::onAnimationFrame() { |
| 34 m_hasPendingAnimationFrameTask = false; | 45 m_hasPendingAnimationFrameTask = false; |
| 35 onAnimationFrameInternal(); | 46 onAnimationFrameInternal(); |
| 36 } | 47 } |
| 37 | 48 |
| 38 DEFINE_TRACE(SensorReadingUpdater) { | 49 DEFINE_TRACE(SensorReadingUpdater) { |
| 50 visitor->trace(m_document); |
| 39 visitor->trace(m_sensorProxy); | 51 visitor->trace(m_sensorProxy); |
| 40 } | 52 } |
| 41 | 53 |
| 42 class SensorReadingUpdaterContinuous : public SensorReadingUpdater { | 54 class SensorReadingUpdaterContinuous : public SensorReadingUpdater { |
| 43 public: | 55 public: |
| 44 explicit SensorReadingUpdaterContinuous(SensorProxy* sensorProxy) | 56 explicit SensorReadingUpdaterContinuous(SensorProxy* sensorProxy) |
| 45 : SensorReadingUpdater(sensorProxy) {} | 57 : SensorReadingUpdater(sensorProxy) {} |
| 46 | 58 |
| 47 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); } | 59 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); } |
| 48 | 60 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 | 115 |
| 104 // static | 116 // static |
| 105 SensorReadingUpdater* SensorReadingUpdater::create(SensorProxy* proxy, | 117 SensorReadingUpdater* SensorReadingUpdater::create(SensorProxy* proxy, |
| 106 ReportingMode mode) { | 118 ReportingMode mode) { |
| 107 if (mode == ReportingMode::CONTINUOUS) | 119 if (mode == ReportingMode::CONTINUOUS) |
| 108 return new SensorReadingUpdaterContinuous(proxy); | 120 return new SensorReadingUpdaterContinuous(proxy); |
| 109 return new SensorReadingUpdaterOnChange(proxy); | 121 return new SensorReadingUpdaterOnChange(proxy); |
| 110 } | 122 } |
| 111 | 123 |
| 112 } // namespace blink | 124 } // namespace blink |
| OLD | NEW |