OLD | NEW |
---|---|
(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/SensorUpdateNotifier.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 SensorUpdateNotifier::SensorUpdateNotifier( | |
14 double frequency, | |
15 std::unique_ptr<Function<void()>> sensorUpdatingFunction) | |
16 : m_period(frequency), | |
17 m_sensorUpdatingFunction(std::move(sensorUpdatingFunction)), | |
18 m_lastTimestamp(0.0) { | |
19 DCHECK_GT(frequency, 0.0); | |
20 DCHECK(m_sensorUpdatingFunction); | |
21 } | |
22 | |
23 // Implementation for sensors with 'onchange' reporting mode. | |
24 // Uses a "single-shot" timer to make sure notification is not invoked | |
25 // more frequently than expected. | |
26 class SensorUpdateNotifierOnChange : public SensorUpdateNotifier { | |
27 public: | |
28 SensorUpdateNotifierOnChange( | |
29 double period, | |
30 std::unique_ptr<Function<void()>> sensorUpdatingFunction) | |
31 : SensorUpdateNotifier(period, std::move(sensorUpdatingFunction)), | |
32 m_timer(this, &SensorUpdateNotifierOnChange::onTimer) {} | |
33 | |
34 private: | |
35 // SensorUpdateNotifier overrides. | |
36 void onSensorReadingChanged(double timestamp) override; | |
37 void cancelPendingNotifications() override; | |
38 | |
39 void onTimer(TimerBase*); | |
40 | |
41 Timer<SensorUpdateNotifierOnChange> m_timer; | |
42 }; | |
43 | |
44 void SensorUpdateNotifierOnChange::onSensorReadingChanged(double timestamp) { | |
45 if (m_timer.isActive()) | |
46 return; // Skipping changes if update notification was already sheduled. | |
47 | |
48 double elapsedTime = timestamp - m_lastTimestamp; | |
49 const double kMinInterval = | |
50 1 / device::mojom::blink::SensorConfiguration::kMaxAllowedFrequency; | |
51 double waitingTime = m_period - elapsedTime; | |
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. This is done to reduce using of timer. | |
56 if (waitingTime < kMinInterval) { | |
57 m_lastTimestamp = timestamp; | |
58 (*m_sensorUpdatingFunction)(); | |
59 } else { | |
60 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
| |
61 } | |
62 } | |
63 | |
64 void SensorUpdateNotifierOnChange::cancelPendingNotifications() { | |
65 m_timer.stop(); | |
66 } | |
67 | |
68 void SensorUpdateNotifierOnChange::onTimer(TimerBase*) { | |
69 m_lastTimestamp = WTF::monotonicallyIncreasingTime(); | |
70 (*m_sensorUpdatingFunction)(); | |
71 } | |
72 | |
73 // Implementation for sensors with 'continuous' reporting mode. | |
74 class SensorUpdateNotifierContinuous : public SensorUpdateNotifier { | |
75 public: | |
76 SensorUpdateNotifierContinuous( | |
77 double period, | |
78 std::unique_ptr<Function<void()>> sensorUpdatingFunction) | |
79 : SensorUpdateNotifier(period, std::move(sensorUpdatingFunction)) {} | |
80 | |
81 private: | |
82 // SensorUpdateNotifier overrides. | |
83 void onSensorReadingChanged(double timestamp) override { | |
84 if (timestamp - m_lastTimestamp >= m_period) { | |
85 m_lastTimestamp = timestamp; | |
86 (*m_sensorUpdatingFunction)(); | |
87 } | |
88 } | |
89 | |
90 void cancelPendingNotifications() override {} | |
91 }; | |
92 | |
93 // static | |
94 std::unique_ptr<SensorUpdateNotifier> SensorUpdateNotifier::create( | |
95 double frequency, | |
96 std::unique_ptr<Function<void()>> sensorUpdatingFunction, | |
97 device::mojom::blink::ReportingMode mode) { | |
98 double period = 1 / frequency; | |
99 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS) { | |
100 return std::unique_ptr<SensorUpdateNotifier>( | |
101 new SensorUpdateNotifierContinuous(period, | |
102 std::move(sensorUpdatingFunction))); | |
103 } | |
104 return std::unique_ptr<SensorUpdateNotifier>(new SensorUpdateNotifierOnChange( | |
105 period, std::move(sensorUpdatingFunction))); | |
106 } | |
107 | |
108 } // namespace blink | |
OLD | NEW |