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..58a6b31fd8c306b6a78c88963f89b07df79fce2d |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/sensor/SensorPollingStrategy.cpp |
@@ -0,0 +1,120 @@ |
+// 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_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE); |
+} |
+ |
+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_polling = true; |
+} |
+ |
+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 |