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

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

Issue 2121313002: [sensors] Generic Sensors Framework blink side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sensors_mojo_interfaces
Patch Set: Comments from Tim Created 4 years, 3 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
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/SensorPollingStrategy.h"
6
7 #include "platform/Timer.h"
8 #include "wtf/CurrentTime.h"
9
10 namespace blink {
11
12 SensorPollingStrategy::SensorPollingStrategy(double frequency, std::unique_ptr<F unction<void()>> func)
13 : m_frequency(frequency)
14 , m_pollFunc(std::move(func))
15 {
16 DCHECK(m_frequency);
17 }
18
19 class ContiniousSensorPollingStrategy : public SensorPollingStrategy {
20 public:
21 DEFINE_INLINE_VIRTUAL_TRACE()
22 {
23 SensorPollingStrategy::trace(visitor);
24 }
25
26 ContiniousSensorPollingStrategy(double frequency, std::unique_ptr<Function<v oid()>> func)
27 : SensorPollingStrategy(frequency, std::move(func))
28 , m_timer(this, &ContiniousSensorPollingStrategy::pollForData) {}
29 private:
30 // SensorPollingStrategy overrides.
31 void startPolling() override;
32 void stopPolling() override;
33
34 void pollForData(TimerBase*);
35
36 Timer<ContiniousSensorPollingStrategy> m_timer;
37 };
38
39 void ContiniousSensorPollingStrategy::startPolling()
40 {
41 (*m_pollFunc)();
42 m_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE);
43 }
44
45 void ContiniousSensorPollingStrategy::stopPolling()
46 {
47 m_timer.stop();
48 }
49
50 void ContiniousSensorPollingStrategy::pollForData(TimerBase*)
51 {
52 (*m_pollFunc)();
53 }
54
55 class OnChangeSensorPollingStrategy : public SensorPollingStrategy {
56 public:
57 DEFINE_INLINE_VIRTUAL_TRACE()
58 {
59 SensorPollingStrategy::trace(visitor);
60 }
61
62 OnChangeSensorPollingStrategy(double frequency, std::unique_ptr<Function<voi d()>> func)
63 : SensorPollingStrategy(frequency, std::move(func))
64 , m_timer(this, &OnChangeSensorPollingStrategy::pollForData)
65 , m_polling(false)
66 , m_lastPollingTimestamp(0.0) {}
67 private:
68 // SensorPollingStrategy overrides.
69 void startPolling() override;
70 void stopPolling() override;
71 void onSensorReadingChanged() override;
72
73 void pollForData(TimerBase*);
74
75 Timer<OnChangeSensorPollingStrategy> m_timer;
76 bool m_polling;
77 double m_lastPollingTimestamp;
78 };
79
80 void OnChangeSensorPollingStrategy::startPolling()
81 {
82 (*m_pollFunc)();
83 m_polling = true;
84 }
85
86 void OnChangeSensorPollingStrategy::stopPolling()
87 {
88 m_polling = false;
89 }
90
91 void OnChangeSensorPollingStrategy::onSensorReadingChanged()
92 {
93 if (!m_polling || m_timer.isActive())
94 return;
95 double pollingPeriod = 1 / m_frequency;
96 double elapsedTime = WTF::monotonicallyIncreasingTime() - m_lastPollingTimes tamp;
97
98 if (elapsedTime >= pollingPeriod) {
99 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime();
100 (*m_pollFunc)();
101 } else {
102 m_timer.startOneShot(pollingPeriod - elapsedTime, BLINK_FROM_HERE);
103 }
104 }
105
106 void OnChangeSensorPollingStrategy::pollForData(TimerBase*)
107 {
108 if (!m_polling)
109 return;
110 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime();
111 (*m_pollFunc)();
112 }
113
114 // static
115 SensorPollingStrategy* SensorPollingStrategy::create(double frequency, std::uniq ue_ptr<Function<void()>> func, device::mojom::blink::ReportingMode mode)
116 {
117 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS)
118 return new ContiniousSensorPollingStrategy(frequency, std::move(func));
119
120 return new OnChangeSensorPollingStrategy(frequency, std::move(func));
121 }
122
123 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698