Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp b/third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7dcbd2d024e9ef486f7d07ddb1793593f8fee925 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp |
| @@ -0,0 +1,108 @@ |
| +// 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/SensorUpdateNotifier.h" |
| + |
| +#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| +#include "platform/Timer.h" |
| +#include "wtf/CurrentTime.h" |
| + |
| +namespace blink { |
| + |
| +SensorUpdateNotifier::SensorUpdateNotifier( |
| + double frequency, |
| + std::unique_ptr<Function<void()>> sensorUpdatingFunction) |
| + : m_period(frequency), |
| + m_sensorUpdatingFunction(std::move(sensorUpdatingFunction)), |
| + m_lastTimestamp(0.0) { |
| + DCHECK_GT(frequency, 0.0); |
| + DCHECK(m_sensorUpdatingFunction); |
| +} |
| + |
| +// Implementation for sensors with 'onchange' reporting mode. |
| +// Uses a "single-shot" timer to make sure notification is not invoked |
| +// more frequently than expected. |
| +class SensorUpdateNotifierOnChange : public SensorUpdateNotifier { |
| + public: |
| + SensorUpdateNotifierOnChange( |
| + double period, |
| + std::unique_ptr<Function<void()>> sensorUpdatingFunction) |
| + : SensorUpdateNotifier(period, std::move(sensorUpdatingFunction)), |
| + m_timer(this, &SensorUpdateNotifierOnChange::onTimer) {} |
| + |
| + private: |
| + // SensorUpdateNotifier overrides. |
| + void onSensorReadingChanged(double timestamp) override; |
| + void cancelPendingNotifications() override; |
| + |
| + void onTimer(TimerBase*); |
| + |
| + Timer<SensorUpdateNotifierOnChange> m_timer; |
| +}; |
| + |
| +void SensorUpdateNotifierOnChange::onSensorReadingChanged(double timestamp) { |
| + if (m_timer.isActive()) |
| + return; // Skipping changes if update notification was already sheduled. |
| + |
| + double elapsedTime = timestamp - m_lastTimestamp; |
| + const double kMinInterval = |
| + 1 / device::mojom::blink::SensorConfiguration::kMaxAllowedFrequency; |
| + double waitingTime = m_period - elapsedTime; |
| + |
| + // Negative or zero 'waitingTime' means that polling period has elapsed. |
| + // We also avoid scheduling if the elapsed time is slightly behind the |
| + // polling period. This is done to reduce using of timer. |
| + if (waitingTime < kMinInterval) { |
| + m_lastTimestamp = timestamp; |
| + (*m_sensorUpdatingFunction)(); |
| + } else { |
| + m_timer.startOneShot(waitingTime, BLINK_FROM_HERE); |
|
Reilly Grant (use Gerrit)
2016/12/08 00:56:57
Can we also use RAF here to deliver events no more
|
| + } |
| +} |
| + |
| +void SensorUpdateNotifierOnChange::cancelPendingNotifications() { |
| + m_timer.stop(); |
| +} |
| + |
| +void SensorUpdateNotifierOnChange::onTimer(TimerBase*) { |
| + m_lastTimestamp = WTF::monotonicallyIncreasingTime(); |
| + (*m_sensorUpdatingFunction)(); |
| +} |
| + |
| +// Implementation for sensors with 'continuous' reporting mode. |
| +class SensorUpdateNotifierContinuous : public SensorUpdateNotifier { |
| + public: |
| + SensorUpdateNotifierContinuous( |
| + double period, |
| + std::unique_ptr<Function<void()>> sensorUpdatingFunction) |
| + : SensorUpdateNotifier(period, std::move(sensorUpdatingFunction)) {} |
| + |
| + private: |
| + // SensorUpdateNotifier overrides. |
| + void onSensorReadingChanged(double timestamp) override { |
| + if (timestamp - m_lastTimestamp >= m_period) { |
| + m_lastTimestamp = timestamp; |
| + (*m_sensorUpdatingFunction)(); |
| + } |
| + } |
| + |
| + void cancelPendingNotifications() override {} |
| +}; |
| + |
| +// static |
| +std::unique_ptr<SensorUpdateNotifier> SensorUpdateNotifier::create( |
| + double frequency, |
| + std::unique_ptr<Function<void()>> sensorUpdatingFunction, |
| + device::mojom::blink::ReportingMode mode) { |
| + double period = 1 / frequency; |
| + if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) { |
| + return std::unique_ptr<SensorUpdateNotifier>( |
| + new SensorUpdateNotifierContinuous(period, |
| + std::move(sensorUpdatingFunction))); |
| + } |
| + return std::unique_ptr<SensorUpdateNotifier>(new SensorUpdateNotifierOnChange( |
| + period, std::move(sensorUpdatingFunction))); |
| +} |
| + |
| +} // namespace blink |