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