| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "modules/sensor/SensorPollingStrategy.h" | |
| 6 | |
| 7 #include "wtf/CurrentTime.h" | |
| 8 | |
| 9 namespace blink { | |
| 10 | |
| 11 SensorPollingStrategy::SensorPollingStrategy( | |
| 12 double pollingPeriod, | |
| 13 std::unique_ptr<Function<void()>> func) | |
| 14 : m_pollingPeriod(pollingPeriod), | |
| 15 m_pollFunc(std::move(func)), | |
| 16 m_timer(this, &SensorPollingStrategy::pollForData) {} | |
| 17 | |
| 18 SensorPollingStrategy::~SensorPollingStrategy() = default; | |
| 19 | |
| 20 // Polls the buffer continuously using the given 'pollingPeriod'. | |
| 21 class ContiniousSensorPollingStrategy : public SensorPollingStrategy { | |
| 22 public: | |
| 23 ContiniousSensorPollingStrategy(double pollingPeriod, | |
| 24 std::unique_ptr<Function<void()>> func) | |
| 25 : SensorPollingStrategy(pollingPeriod, std::move(func)) {} | |
| 26 | |
| 27 private: | |
| 28 // SensorPollingStrategy overrides. | |
| 29 void startPolling() override; | |
| 30 void stopPolling() override; | |
| 31 | |
| 32 void pollForData(TimerBase*) override; | |
| 33 }; | |
| 34 | |
| 35 void ContiniousSensorPollingStrategy::startPolling() { | |
| 36 (*m_pollFunc)(); | |
| 37 m_timer.startRepeating(m_pollingPeriod, BLINK_FROM_HERE); | |
| 38 } | |
| 39 | |
| 40 void ContiniousSensorPollingStrategy::stopPolling() { | |
| 41 m_timer.stop(); | |
| 42 } | |
| 43 | |
| 44 void ContiniousSensorPollingStrategy::pollForData(TimerBase*) { | |
| 45 (*m_pollFunc)(); | |
| 46 } | |
| 47 | |
| 48 // Polls the buffer on signal from platform but not more frequently | |
| 49 // than the given 'pollingPeriod'. | |
| 50 class OnChangeSensorPollingStrategy : public SensorPollingStrategy { | |
| 51 public: | |
| 52 OnChangeSensorPollingStrategy(double pollingPeriod, | |
| 53 std::unique_ptr<Function<void()>> func) | |
| 54 : SensorPollingStrategy(pollingPeriod, std::move(func)), | |
| 55 m_polling(false), | |
| 56 m_lastPollingTimestamp(0.0) {} | |
| 57 | |
| 58 private: | |
| 59 // SensorPollingStrategy overrides. | |
| 60 void startPolling() override; | |
| 61 void stopPolling() override; | |
| 62 void onSensorReadingChanged() override; | |
| 63 | |
| 64 void pollForData(TimerBase*) override; | |
| 65 | |
| 66 bool m_polling; | |
| 67 double m_lastPollingTimestamp; | |
| 68 }; | |
| 69 | |
| 70 void OnChangeSensorPollingStrategy::startPolling() { | |
| 71 (*m_pollFunc)(); | |
| 72 m_polling = true; | |
| 73 } | |
| 74 | |
| 75 void OnChangeSensorPollingStrategy::stopPolling() { | |
| 76 m_polling = false; | |
| 77 } | |
| 78 | |
| 79 void OnChangeSensorPollingStrategy::onSensorReadingChanged() { | |
| 80 if (!m_polling || m_timer.isActive()) | |
| 81 return; | |
| 82 double elapsedTime = | |
| 83 WTF::monotonicallyIncreasingTime() - m_lastPollingTimestamp; | |
| 84 | |
| 85 if (elapsedTime >= m_pollingPeriod) { | |
| 86 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 87 (*m_pollFunc)(); | |
| 88 } else { | |
| 89 m_timer.startOneShot(m_pollingPeriod - elapsedTime, BLINK_FROM_HERE); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void OnChangeSensorPollingStrategy::pollForData(TimerBase*) { | |
| 94 if (!m_polling) | |
| 95 return; | |
| 96 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 97 (*m_pollFunc)(); | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 std::unique_ptr<SensorPollingStrategy> SensorPollingStrategy::create( | |
| 102 double pollingPeriod, | |
| 103 std::unique_ptr<Function<void()>> func, | |
| 104 device::mojom::blink::ReportingMode mode) { | |
| 105 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) | |
| 106 return std::unique_ptr<SensorPollingStrategy>( | |
| 107 new ContiniousSensorPollingStrategy(pollingPeriod, std::move(func))); | |
| 108 | |
| 109 return std::unique_ptr<SensorPollingStrategy>( | |
| 110 new OnChangeSensorPollingStrategy(pollingPeriod, std::move(func))); | |
| 111 } | |
| 112 | |
| 113 } // namespace blink | |
| OLD | NEW |