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