Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(655)

Unified Diff: third_party/WebKit/Source/modules/sensor/SensorUpdateNotifier.cpp

Issue 2551223003: [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698