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

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. Removed the default sensor configuration management (to be added with a separate… 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 // Polls the buffer continuously using the given 'frequency'.
20 class ContiniousSensorPollingStrategy : public SensorPollingStrategy {
21 public:
22 DEFINE_INLINE_VIRTUAL_TRACE()
23 {
24 SensorPollingStrategy::trace(visitor);
25 }
26
27 ContiniousSensorPollingStrategy(double frequency, std::unique_ptr<Function<v oid()>> func)
28 : SensorPollingStrategy(frequency, std::move(func))
29 , m_timer(this, &ContiniousSensorPollingStrategy::pollForData) {}
30 private:
31 // SensorPollingStrategy overrides.
32 void startPolling() override;
33 void stopPolling() override;
34
35 void pollForData(TimerBase*);
36
37 Timer<ContiniousSensorPollingStrategy> m_timer;
38 };
39
40 void ContiniousSensorPollingStrategy::startPolling()
41 {
42 (*m_pollFunc)();
43 m_timer.startRepeating(1 / m_frequency, BLINK_FROM_HERE);
44 }
45
46 void ContiniousSensorPollingStrategy::stopPolling()
47 {
48 m_timer.stop();
49 }
50
51 void ContiniousSensorPollingStrategy::pollForData(TimerBase*)
52 {
53 (*m_pollFunc)();
54 }
55
56 // Polls the buffer on signal from platform but not more frequently
57 // than the given 'frequency'.
58 class OnChangeSensorPollingStrategy : public SensorPollingStrategy {
59 public:
60 DEFINE_INLINE_VIRTUAL_TRACE()
61 {
62 SensorPollingStrategy::trace(visitor);
63 }
64
65 OnChangeSensorPollingStrategy(double frequency, std::unique_ptr<Function<voi d()>> func)
66 : SensorPollingStrategy(frequency, std::move(func))
67 , m_timer(this, &OnChangeSensorPollingStrategy::pollForData)
68 , m_polling(false)
69 , m_lastPollingTimestamp(0.0) {}
70 private:
71 // SensorPollingStrategy overrides.
72 void startPolling() override;
73 void stopPolling() override;
74 void onSensorReadingChanged() override;
75
76 void pollForData(TimerBase*);
77
78 Timer<OnChangeSensorPollingStrategy> m_timer;
79 bool m_polling;
80 double m_lastPollingTimestamp;
81 };
82
83 void OnChangeSensorPollingStrategy::startPolling()
84 {
85 (*m_pollFunc)();
86 m_polling = true;
87 }
88
89 void OnChangeSensorPollingStrategy::stopPolling()
90 {
91 m_polling = false;
92 }
93
94 void OnChangeSensorPollingStrategy::onSensorReadingChanged()
95 {
96 if (!m_polling || m_timer.isActive())
97 return;
98 double pollingPeriod = 1 / m_frequency;
99 double elapsedTime = WTF::monotonicallyIncreasingTime() - m_lastPollingTimes tamp;
100
101 if (elapsedTime >= pollingPeriod) {
102 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime();
103 (*m_pollFunc)();
104 } else {
105 m_timer.startOneShot(pollingPeriod - elapsedTime, BLINK_FROM_HERE);
106 }
107 }
108
109 void OnChangeSensorPollingStrategy::pollForData(TimerBase*)
110 {
111 if (!m_polling)
112 return;
113 m_lastPollingTimestamp = WTF::monotonicallyIncreasingTime();
114 (*m_pollFunc)();
115 }
116
117 // static
118 SensorPollingStrategy* SensorPollingStrategy::create(double frequency, std::uniq ue_ptr<Function<void()>> func, device::mojom::blink::ReportingMode mode)
119 {
120 if (mode == device::mojom::blink::ReportingMode::CONTINUOUS)
121 return new ContiniousSensorPollingStrategy(frequency, std::move(func));
122
123 return new OnChangeSensorPollingStrategy(frequency, std::move(func));
124 }
125
126 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698