Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.cpp b/third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..143846598aa2856d9a1c8eb8ad3898baeb124832 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.cpp |
| @@ -0,0 +1,85 @@ |
| +// 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/SensorUpdateNotificationStrategy.h" |
| + |
| +#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| +#include "platform/Timer.h" |
| +#include "wtf/CurrentTime.h" |
| + |
| +namespace blink { |
| + |
| +// Polls the buffer on signal from platform but not more frequently |
| +// than the given 'pollingPeriod'. |
| +class SensorUpdateNotificationStrategyImpl |
| + : public SensorUpdateNotificationStrategy { |
| + public: |
| + SensorUpdateNotificationStrategyImpl(double frequency, |
| + std::unique_ptr<Function<void()>> func) |
| + : m_pollingPeriod(1 / frequency), |
| + m_func(std::move(func)), |
| + m_timer(this, &SensorUpdateNotificationStrategyImpl::onTimer), |
| + m_lastPollingTimestamp(0.0) { |
| + DCHECK_GT(frequency, 0.0); |
| + DCHECK(m_func); |
| + } |
| + |
| + private: |
| + // SensorUpdateNotificationStrategy overrides. |
| + void onSensorReadingChanged() override; |
| + void cancelPendingNotifications() override; |
| + |
| + void onTimer(TimerBase*); |
| + void notifyUpdate(); |
| + |
| + double m_pollingPeriod; |
| + std::unique_ptr<Function<void()>> m_func; |
| + Timer<SensorUpdateNotificationStrategyImpl> m_timer; |
| + double m_lastPollingTimestamp; |
| +}; |
| + |
| +void SensorUpdateNotificationStrategyImpl::onSensorReadingChanged() { |
| + if (m_timer.isActive()) |
| + return; // Skipping changes if update notifiation was already sheduled. |
|
timvolodine
2016/11/21 16:30:35
->notification
Mikhail
2016/11/22 12:54:23
Done.
|
| + |
| + double elapsedTime = |
| + WTF::monotonicallyIncreasingTime() - m_lastPollingTimestamp; |
| + |
| + double waitingTime = m_pollingPeriod - elapsedTime; |
| + const double minInterval = |
| + 1 / device::mojom::blink::SensorConfiguration::kMaxAllowedFrequency; |
| + |
| + // Negative or zero 'waitingTime' means that polling period has elapsed. |
| + // We also avoid scheduling if the elapsed time is slightly behind the |
| + // polling period. |
| + if (waitingTime < minInterval) { |
| + notifyUpdate(); |
| + } else { |
| + m_timer.startOneShot(waitingTime, BLINK_FROM_HERE); |
|
timvolodine
2016/11/21 16:30:35
so there is still a timer for each sensor?
I unde
Mikhail
2016/11/22 12:54:23
yes, but it is not repeating, single shot is sched
|
| + } |
| +} |
| + |
| +void SensorUpdateNotificationStrategyImpl::cancelPendingNotifications() { |
| + m_timer.stop(); |
| +} |
| + |
| +void SensorUpdateNotificationStrategyImpl::notifyUpdate() { |
| + m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime(); |
| + (*m_func)(); |
| +} |
| + |
| +void SensorUpdateNotificationStrategyImpl::onTimer(TimerBase*) { |
| + notifyUpdate(); |
| +} |
| + |
| +// static |
| +std::unique_ptr<SensorUpdateNotificationStrategy> |
| +SensorUpdateNotificationStrategy::create( |
| + double pollingPeriod, |
| + std::unique_ptr<Function<void()>> func) { |
| + return std::unique_ptr<SensorUpdateNotificationStrategy>( |
| + new SensorUpdateNotificationStrategyImpl(pollingPeriod, std::move(func))); |
| +} |
| + |
| +} // namespace blink |