Chromium Code Reviews| 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 "platform/Timer.h" | |
| 8 #include "wtf/CurrentTime.h" | |
| 9 | |
| 7 namespace blink { | 10 namespace blink { |
| 8 | 11 |
| 12 SensorPollingStrategy::SensorPollingStrategy(double frequency, std::unique_ptr<F unction<void()>> func) | |
| 13 : m_frequency(frequency) | |
| 14 , m_pollFunc(std::move(func)) | |
| 15 { | |
| 16 DCHECK(m_frequency); | |
|
timvolodine
2016/09/08 17:02:06
DCHECK(m_frequency > 0)?
Mikhail
2016/09/08 19:03:19
Style checker proposes DCHECK_GT
| |
| 17 } | |
| 18 | |
| 19 // Polls the buffer continuously using the given 'frequency'. | |
| 20 class ContiniousSensorPollingStrategy : public SensorPollingStrategy { | |
| 21 public: | |
| 22 ContiniousSensorPollingStrategy(double frequency, std::unique_ptr<Function<v oid()>> func) | |
| 23 : SensorPollingStrategy(frequency, std::move(func)) | |
| 24 , m_timer(this, &ContiniousSensorPollingStrategy::pollForData) {} | |
| 25 private: | |
| 26 // SensorPollingStrategy overrides. | |
| 27 void startPolling() override; | |
| 28 void stopPolling() override; | |
| 29 | |
| 30 void pollForData(TimerBase*); | |
| 31 | |
| 32 Timer<ContiniousSensorPollingStrategy> m_timer; | |
| 33 }; | |
| 34 | |
| 35 void ContiniousSensorPollingStrategy::startPolling() | |
| 36 { | |
| 37 (*m_pollFunc)(); | |
| 38 m_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE); | |
| 39 } | |
| 40 | |
| 41 void ContiniousSensorPollingStrategy::stopPolling() | |
| 42 { | |
| 43 m_timer.stop(); | |
| 44 } | |
| 45 | |
| 46 void ContiniousSensorPollingStrategy::pollForData(TimerBase*) | |
| 47 { | |
| 48 (*m_pollFunc)(); | |
| 49 } | |
| 50 | |
| 51 // Polls the buffer on signal from platform but not more frequently | |
| 52 // than the given 'frequency'. | |
| 53 class OnChangeSensorPollingStrategy : public SensorPollingStrategy { | |
| 54 public: | |
| 55 OnChangeSensorPollingStrategy(double frequency, std::unique_ptr<Function<voi d()>> func) | |
| 56 : SensorPollingStrategy(frequency, std::move(func)) | |
| 57 , m_timer(this, &OnChangeSensorPollingStrategy::pollForData) | |
| 58 , m_polling(false) | |
| 59 , m_lastPollingTimestamp(0.0) {} | |
| 60 private: | |
| 61 // SensorPollingStrategy overrides. | |
| 62 void startPolling() override; | |
| 63 void stopPolling() override; | |
| 64 void onSensorReadingChanged() override; | |
| 65 | |
| 66 void pollForData(TimerBase*); | |
| 67 | |
| 68 Timer<OnChangeSensorPollingStrategy> m_timer; | |
|
timvolodine
2016/09/08 17:02:06
If the polling strategies generally use timers you
Mikhail
2016/09/08 19:03:19
Done.
| |
| 69 bool m_polling; | |
| 70 double m_lastPollingTimestamp; | |
| 71 }; | |
| 72 | |
| 73 void OnChangeSensorPollingStrategy::startPolling() | |
| 74 { | |
| 75 (*m_pollFunc)(); | |
| 76 m_polling = true; | |
| 77 } | |
| 78 | |
| 79 void OnChangeSensorPollingStrategy::stopPolling() | |
| 80 { | |
| 81 m_polling = false; | |
| 82 } | |
| 83 | |
| 84 void OnChangeSensorPollingStrategy::onSensorReadingChanged() | |
| 85 { | |
| 86 if (!m_polling || m_timer.isActive()) | |
| 87 return; | |
| 88 double pollingPeriod = 1 / m_frequency; | |
|
timvolodine
2016/09/08 17:02:06
maybe store it somewhere instead of recomputing on
Mikhail
2016/09/08 19:03:19
Done.
| |
| 89 double elapsedTime = WTF::monotonicallyIncreasingTime() - m_lastPollingTimes tamp; | |
| 90 | |
| 91 if (elapsedTime >= pollingPeriod) { | |
| 92 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 93 (*m_pollFunc)(); | |
| 94 } else { | |
| 95 m_timer.startOneShot(pollingPeriod - elapsedTime, BLINK_FROM_HERE); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void OnChangeSensorPollingStrategy::pollForData(TimerBase*) | |
| 100 { | |
| 101 if (!m_polling) | |
| 102 return; | |
| 103 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 104 (*m_pollFunc)(); | |
| 105 } | |
| 106 | |
| 9 // static | 107 // static |
| 10 std::unique_ptr<SensorPollingStrategy> SensorPollingStrategy::create(double freq uency, std::unique_ptr<Function<void()>> func, device::mojom::blink::ReportingMo de mode) | 108 std::unique_ptr<SensorPollingStrategy> SensorPollingStrategy::create(double freq uency, std::unique_ptr<Function<void()>> func, device::mojom::blink::ReportingMo de mode) |
| 11 { | 109 { |
| 12 // TODO(Mikhail): Implement. | 110 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) |
| 13 return nullptr; | 111 return std::unique_ptr<SensorPollingStrategy>(new ContiniousSensorPollin gStrategy(frequency, std::move(func))); |
| 112 | |
| 113 return std::unique_ptr<SensorPollingStrategy>(new OnChangeSensorPollingStrat egy(frequency, std::move(func))); | |
| 14 } | 114 } |
| 15 | 115 |
| 16 } // namespace blink | 116 } // namespace blink |
| OLD | NEW |