Chromium Code Reviews| 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 "platform/Timer.h" | |
| 8 #include "wtf/CurrentTime.h" | |
| 9 | |
| 10 namespace blink { | |
| 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 } | |
| 17 | |
| 18 class ContiniousSensorPollingStrategy : public SensorPollingStrategy { | |
| 19 public: | |
| 20 DEFINE_INLINE_VIRTUAL_TRACE() | |
| 21 { | |
| 22 SensorPollingStrategy::trace(visitor); | |
| 23 } | |
| 24 | |
| 25 ContiniousSensorPollingStrategy(double frequency, std::unique_ptr<Function<v oid()>> func) | |
| 26 : SensorPollingStrategy(frequency, std::move(func)) | |
| 27 , m_timer(this, &ContiniousSensorPollingStrategy::pollForData) {} | |
| 28 private: | |
| 29 // SensorPollingStrategy overrides. | |
| 30 void startPolling() override; | |
| 31 void stopPolling() override; | |
| 32 | |
| 33 void pollForData(TimerBase*); | |
| 34 | |
| 35 Timer<ContiniousSensorPollingStrategy> m_timer; | |
| 36 }; | |
| 37 | |
| 38 void ContiniousSensorPollingStrategy::startPolling() | |
| 39 { | |
| 40 (*m_pollFunc)(); | |
| 41 m_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE); | |
|
timvolodine
2016/09/01 19:02:06
what happens if the sensor is notification only, i
timvolodine
2016/09/01 19:02:06
also maybe DCHECK(frequency>0) ?
Mikhail
2016/09/02 08:23:43
Different polling strategy class OnChangeSensorPol
Mikhail
2016/09/02 08:23:43
Done.
| |
| 42 } | |
| 43 | |
| 44 void ContiniousSensorPollingStrategy::stopPolling() | |
| 45 { | |
| 46 m_timer.stop(); | |
| 47 } | |
| 48 | |
| 49 void ContiniousSensorPollingStrategy::pollForData(TimerBase*) | |
| 50 { | |
| 51 (*m_pollFunc)(); | |
| 52 } | |
| 53 | |
| 54 class OnChangeSensorPollingStrategy : public SensorPollingStrategy { | |
| 55 public: | |
| 56 DEFINE_INLINE_VIRTUAL_TRACE() | |
| 57 { | |
| 58 SensorPollingStrategy::trace(visitor); | |
| 59 } | |
| 60 | |
| 61 OnChangeSensorPollingStrategy(double frequency, std::unique_ptr<Function<voi d()>> func) | |
| 62 : SensorPollingStrategy(frequency, std::move(func)) | |
| 63 , m_timer(this, &OnChangeSensorPollingStrategy::pollForData) | |
| 64 , m_polling(false) | |
| 65 , m_lastPollingTimestamp(0.0) {} | |
| 66 private: | |
| 67 // SensorPollingStrategy overrides. | |
| 68 void startPolling() override; | |
| 69 void stopPolling() override; | |
| 70 void onSensorReadingChanged() override; | |
| 71 | |
| 72 void pollForData(TimerBase*); | |
| 73 | |
| 74 Timer<OnChangeSensorPollingStrategy> m_timer; | |
| 75 bool m_polling; | |
| 76 double m_lastPollingTimestamp; | |
| 77 }; | |
| 78 | |
| 79 void OnChangeSensorPollingStrategy::startPolling() | |
| 80 { | |
| 81 (*m_pollFunc)(); | |
| 82 m_polling = true; | |
|
timvolodine
2016/09/01 19:02:06
my understanding was that some sensors do not requ
Mikhail
2016/09/02 08:23:43
Polling here means just refreshing a sensor readin
timvolodine
2016/09/02 19:41:28
In that case, could you pls add some comments with
Mikhail
2016/09/05 10:26:27
Done.
| |
| 83 } | |
| 84 | |
| 85 void OnChangeSensorPollingStrategy::stopPolling() | |
| 86 { | |
| 87 m_polling = false; | |
| 88 } | |
| 89 | |
| 90 void OnChangeSensorPollingStrategy::onSensorReadingChanged() | |
| 91 { | |
| 92 if (!m_polling || m_timer.isActive()) | |
| 93 return; | |
| 94 double pollingPeriod = 1 / m_frequency; | |
| 95 double elapsedTime = WTF::monotonicallyIncreasingTime() - m_lastPollingTimes tamp; | |
| 96 | |
| 97 if (elapsedTime >= pollingPeriod) { | |
| 98 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 99 (*m_pollFunc)(); | |
| 100 } else { | |
| 101 m_timer.startOneShot(pollingPeriod - elapsedTime, BLINK_FROM_HERE); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void OnChangeSensorPollingStrategy::pollForData(TimerBase*) | |
| 106 { | |
| 107 if (!m_polling) | |
| 108 return; | |
| 109 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); | |
| 110 (*m_pollFunc)(); | |
| 111 } | |
| 112 | |
| 113 // static | |
| 114 SensorPollingStrategy* SensorPollingStrategy::create(double frequency, std::uniq ue_ptr<Function<void()>> func, device::mojom::blink::ReportingMode mode) | |
| 115 { | |
| 116 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) | |
| 117 return new ContiniousSensorPollingStrategy(frequency, std::move(func)); | |
| 118 | |
| 119 return new OnChangeSensorPollingStrategy(frequency, std::move(func)); | |
| 120 } | |
| 121 | |
| 122 } // namespace blink | |
| OLD | NEW |