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

Side by Side Diff: third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.cpp

Issue 2590513002: Revert of [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: Created 3 years, 12 months 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "modules/sensor/SensorUpdateNotificationStrategy.h"
6
7 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
8 #include "platform/Timer.h"
9 #include "wtf/CurrentTime.h"
10
11 namespace blink {
12
13 // Polls the buffer on signal from platform but not more frequently
14 // than the given 'pollingPeriod'.
15 class SensorUpdateNotificationStrategyImpl
16 : public SensorUpdateNotificationStrategy {
17 public:
18 SensorUpdateNotificationStrategyImpl(double frequency,
19 std::unique_ptr<Function<void()>> func)
20 : m_pollingPeriod(1 / frequency),
21 m_func(std::move(func)),
22 m_timer(this, &SensorUpdateNotificationStrategyImpl::onTimer),
23 m_lastPollingTimestamp(0.0) {
24 DCHECK_GT(frequency, 0.0);
25 DCHECK(m_func);
26 }
27
28 private:
29 // SensorUpdateNotificationStrategy overrides.
30 void onSensorReadingChanged() override;
31 void cancelPendingNotifications() override;
32
33 void onTimer(TimerBase*);
34 void notifyUpdate();
35
36 double m_pollingPeriod;
37 std::unique_ptr<Function<void()>> m_func;
38 Timer<SensorUpdateNotificationStrategyImpl> m_timer;
39 double m_lastPollingTimestamp;
40 };
41
42 void SensorUpdateNotificationStrategyImpl::onSensorReadingChanged() {
43 if (m_timer.isActive())
44 return; // Skipping changes if update notification was already sheduled.
45
46 double elapsedTime =
47 WTF::monotonicallyIncreasingTime() - m_lastPollingTimestamp;
48
49 double waitingTime = m_pollingPeriod - elapsedTime;
50 const double minInterval =
51 1 / device::mojom::blink::SensorConfiguration::kMaxAllowedFrequency;
52
53 // Negative or zero 'waitingTime' means that polling period has elapsed.
54 // We also avoid scheduling if the elapsed time is slightly behind the
55 // polling period.
56 if (waitingTime < minInterval) {
57 notifyUpdate();
58 } else {
59 m_timer.startOneShot(waitingTime, BLINK_FROM_HERE);
60 }
61 }
62
63 void SensorUpdateNotificationStrategyImpl::cancelPendingNotifications() {
64 m_timer.stop();
65 }
66
67 void SensorUpdateNotificationStrategyImpl::notifyUpdate() {
68 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime();
69 (*m_func)();
70 }
71
72 void SensorUpdateNotificationStrategyImpl::onTimer(TimerBase*) {
73 notifyUpdate();
74 }
75
76 // static
77 std::unique_ptr<SensorUpdateNotificationStrategy>
78 SensorUpdateNotificationStrategy::create(
79 double pollingPeriod,
80 std::unique_ptr<Function<void()>> func) {
81 return std::unique_ptr<SensorUpdateNotificationStrategy>(
82 new SensorUpdateNotificationStrategyImpl(pollingPeriod, std::move(func)));
83 }
84
85 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/SensorUpdateNotificationStrategy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698