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

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

Powered by Google App Engine
This is Rietveld 408576698