Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp b/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
| index 1212cc7587ade107ef2208eb9c359adbb3303f42..325dd059fa6554fa766bc25845c6ace583cdec65 100644 |
| --- a/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
| @@ -4,13 +4,113 @@ |
| #include "modules/sensor/SensorPollingStrategy.h" |
| +#include "platform/Timer.h" |
| +#include "wtf/CurrentTime.h" |
| + |
| namespace blink { |
| +SensorPollingStrategy::SensorPollingStrategy(double frequency, std::unique_ptr<Function<void()>> func) |
| + : m_frequency(frequency) |
| + , m_pollFunc(std::move(func)) |
| +{ |
| + 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
|
| +} |
| + |
| +// Polls the buffer continuously using the given 'frequency'. |
| +class ContiniousSensorPollingStrategy : public SensorPollingStrategy { |
| +public: |
| + ContiniousSensorPollingStrategy(double frequency, std::unique_ptr<Function<void()>> func) |
| + : SensorPollingStrategy(frequency, std::move(func)) |
| + , m_timer(this, &ContiniousSensorPollingStrategy::pollForData) {} |
| +private: |
| + // SensorPollingStrategy overrides. |
| + void startPolling() override; |
| + void stopPolling() override; |
| + |
| + void pollForData(TimerBase*); |
| + |
| + Timer<ContiniousSensorPollingStrategy> m_timer; |
| +}; |
| + |
| +void ContiniousSensorPollingStrategy::startPolling() |
| +{ |
| + (*m_pollFunc)(); |
| + m_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE); |
| +} |
| + |
| +void ContiniousSensorPollingStrategy::stopPolling() |
| +{ |
| + m_timer.stop(); |
| +} |
| + |
| +void ContiniousSensorPollingStrategy::pollForData(TimerBase*) |
| +{ |
| + (*m_pollFunc)(); |
| +} |
| + |
| +// Polls the buffer on signal from platform but not more frequently |
| +// than the given 'frequency'. |
| +class OnChangeSensorPollingStrategy : public SensorPollingStrategy { |
| +public: |
| + OnChangeSensorPollingStrategy(double frequency, std::unique_ptr<Function<void()>> func) |
| + : SensorPollingStrategy(frequency, std::move(func)) |
| + , m_timer(this, &OnChangeSensorPollingStrategy::pollForData) |
| + , m_polling(false) |
| + , m_lastPollingTimestamp(0.0) {} |
| +private: |
| + // SensorPollingStrategy overrides. |
| + void startPolling() override; |
| + void stopPolling() override; |
| + void onSensorReadingChanged() override; |
| + |
| + void pollForData(TimerBase*); |
| + |
| + 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.
|
| + bool m_polling; |
| + double m_lastPollingTimestamp; |
| +}; |
| + |
| +void OnChangeSensorPollingStrategy::startPolling() |
| +{ |
| + (*m_pollFunc)(); |
| + m_polling = true; |
| +} |
| + |
| +void OnChangeSensorPollingStrategy::stopPolling() |
| +{ |
| + m_polling = false; |
| +} |
| + |
| +void OnChangeSensorPollingStrategy::onSensorReadingChanged() |
| +{ |
| + if (!m_polling || m_timer.isActive()) |
| + return; |
| + 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.
|
| + double elapsedTime = WTF::monotonicallyIncreasingTime() - m_lastPollingTimestamp; |
| + |
| + if (elapsedTime >= pollingPeriod) { |
| + m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); |
| + (*m_pollFunc)(); |
| + } else { |
| + m_timer.startOneShot(pollingPeriod - elapsedTime, BLINK_FROM_HERE); |
| + } |
| +} |
| + |
| +void OnChangeSensorPollingStrategy::pollForData(TimerBase*) |
| +{ |
| + if (!m_polling) |
| + return; |
| + m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); |
| + (*m_pollFunc)(); |
| +} |
| + |
| // static |
| std::unique_ptr<SensorPollingStrategy> SensorPollingStrategy::create(double frequency, std::unique_ptr<Function<void()>> func, device::mojom::blink::ReportingMode mode) |
| { |
| - // TODO(Mikhail): Implement. |
| - return nullptr; |
| + if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) |
| + return std::unique_ptr<SensorPollingStrategy>(new ContiniousSensorPollingStrategy(frequency, std::move(func))); |
| + |
| + return std::unique_ptr<SensorPollingStrategy>(new OnChangeSensorPollingStrategy(frequency, std::move(func))); |
| } |
| } // namespace blink |