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..2d4248cc3e319415fab2da343c3346cc72b161f0 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp |
| @@ -0,0 +1,103 @@ |
| +// 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()>> func) |
| + : m_period(frequency), m_func(std::move(func)), m_lastTimestamp(0.0) { |
| + DCHECK_GT(frequency, 0.0); |
| + DCHECK(m_func); |
| +} |
| + |
| +// 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()>> func) |
| + : SensorUpdateNotifier(period, std::move(func)), |
| + 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_func)(); |
| + } else { |
| + m_timer.startOneShot(waitingTime, BLINK_FROM_HERE); |
| + } |
| +} |
| + |
| +void SensorUpdateNotifierOnChange::cancelPendingNotifications() { |
| + m_timer.stop(); |
| +} |
| + |
| +void SensorUpdateNotifierOnChange::onTimer(TimerBase*) { |
| + m_lastTimestamp = WTF::monotonicallyIncreasingTime(); |
| + (*m_func)(); |
| +} |
| + |
| +// Implementation for sensors with 'continuous' reporting mode. |
| +class SensorUpdateNotifierContinuous : public SensorUpdateNotifier { |
| + public: |
| + SensorUpdateNotifierContinuous(double period, |
| + std::unique_ptr<Function<void()>> func) |
| + : SensorUpdateNotifier(period, std::move(func)) {} |
| + |
| + private: |
| + // SensorUpdateNotifier overrides. |
| + void onSensorReadingChanged(double timestamp) override { |
| + if (timestamp - m_lastTimestamp >= m_period) { |
| + m_lastTimestamp = timestamp; |
| + (*m_func)(); |
| + } |
| + } |
| + |
| + void cancelPendingNotifications() override {} |
| +}; |
| + |
| +// static |
| +std::unique_ptr<SensorUpdateNotifier> SensorUpdateNotifier::create( |
| + double frequency, |
| + std::unique_ptr<Function<void()>> func, |
|
haraken
2016/12/06 02:38:30
func => 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(func))); |
| + } |
| + return std::unique_ptr<SensorUpdateNotifier>( |
| + new SensorUpdateNotifierOnChange(period, std::move(func))); |
| +} |
| + |
| +} // namespace blink |