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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9a2aea1694334d6d32f87f1d0ebeeae7bac8dc59 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
| @@ -0,0 +1,122 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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)) |
| +{ |
| +} |
| + |
| +class ContiniousSensorPollingStrategy : public SensorPollingStrategy { |
| +public: |
| + DEFINE_INLINE_VIRTUAL_TRACE() |
| + { |
| + SensorPollingStrategy::trace(visitor); |
| + } |
| + |
| + 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); |
|
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.
|
| +} |
| + |
| +void ContiniousSensorPollingStrategy::stopPolling() |
| +{ |
| + m_timer.stop(); |
| +} |
| + |
| +void ContiniousSensorPollingStrategy::pollForData(TimerBase*) |
| +{ |
| + (*m_pollFunc)(); |
| +} |
| + |
| +class OnChangeSensorPollingStrategy : public SensorPollingStrategy { |
| +public: |
| + DEFINE_INLINE_VIRTUAL_TRACE() |
| + { |
| + SensorPollingStrategy::trace(visitor); |
| + } |
| + |
| + 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; |
| + bool m_polling; |
| + double m_lastPollingTimestamp; |
| +}; |
| + |
| +void OnChangeSensorPollingStrategy::startPolling() |
| +{ |
| + (*m_pollFunc)(); |
| + 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.
|
| +} |
| + |
| +void OnChangeSensorPollingStrategy::stopPolling() |
| +{ |
| + m_polling = false; |
| +} |
| + |
| +void OnChangeSensorPollingStrategy::onSensorReadingChanged() |
| +{ |
| + if (!m_polling || m_timer.isActive()) |
| + return; |
| + double pollingPeriod = 1 / m_frequency; |
| + 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 |
| +SensorPollingStrategy* SensorPollingStrategy::create(double frequency, std::unique_ptr<Function<void()>> func, device::mojom::blink::ReportingMode mode) |
| +{ |
| + if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) |
| + return new ContiniousSensorPollingStrategy(frequency, std::move(func)); |
| + |
| + return new OnChangeSensorPollingStrategy(frequency, std::move(func)); |
| +} |
| + |
| +} // namespace blink |