OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 5 #ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 6 #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
7 | 7 |
8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
9 #include "base/timer/timer.h" | 9 #include "base/timer/timer.h" |
10 #include "content/public/renderer/platform_event_observer.h" | 10 #include "content/public/renderer/platform_event_observer.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 template <typename ListenerType> | 14 template <typename ListenerType> |
15 class CONTENT_EXPORT DeviceSensorEventPump | 15 class CONTENT_EXPORT DeviceSensorEventPump |
16 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | 16 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { |
17 public: | 17 public: |
18 // Default delay between subsequent firing of events. | 18 // Default delay between subsequent firing of events. |
19 static const int kDefaultPumpDelayMillis = 50; | 19 static const int kDefaultPumpDelayMicroseconds = 1000000 / 60; |
timvolodine
2014/09/25 21:30:51
same here
jdarpinian
2014/09/25 22:36:19
Done.
| |
20 | 20 |
21 // PlatformEventObserver | 21 // PlatformEventObserver |
22 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { | 22 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { |
23 DVLOG(2) << "requested start"; | 23 DVLOG(2) << "requested start"; |
24 | 24 |
25 if (state_ != STOPPED) | 25 if (state_ != STOPPED) |
26 return; | 26 return; |
27 | 27 |
28 DCHECK(!timer_.IsRunning()); | 28 DCHECK(!timer_.IsRunning()); |
29 | 29 |
(...skipping 12 matching lines...) Expand all Loading... | |
42 | 42 |
43 if (timer_.IsRunning()) | 43 if (timer_.IsRunning()) |
44 timer_.Stop(); | 44 timer_.Stop(); |
45 PlatformEventObserver<ListenerType>::Stop(); | 45 PlatformEventObserver<ListenerType>::Stop(); |
46 state_ = STOPPED; | 46 state_ = STOPPED; |
47 } | 47 } |
48 | 48 |
49 protected: | 49 protected: |
50 explicit DeviceSensorEventPump(RenderThread* thread) | 50 explicit DeviceSensorEventPump(RenderThread* thread) |
51 : PlatformEventObserver<ListenerType>(thread), | 51 : PlatformEventObserver<ListenerType>(thread), |
52 pump_delay_millis_(kDefaultPumpDelayMillis), | 52 pump_delay_microseconds_(kDefaultPumpDelayMicroseconds), |
53 state_(STOPPED) { | 53 state_(STOPPED) {} |
54 } | |
55 | 54 |
56 virtual ~DeviceSensorEventPump() { | 55 virtual ~DeviceSensorEventPump() { |
57 } | 56 } |
58 | 57 |
59 // The pump is a tri-state automaton with allowed transitions as follows: | 58 // The pump is a tri-state automaton with allowed transitions as follows: |
60 // STOPPED -> PENDING_START | 59 // STOPPED -> PENDING_START |
61 // PENDING_START -> RUNNING | 60 // PENDING_START -> RUNNING |
62 // PENDING_START -> STOPPED | 61 // PENDING_START -> STOPPED |
63 // RUNNING -> STOPPED | 62 // RUNNING -> STOPPED |
64 enum PumpState { | 63 enum PumpState { |
65 STOPPED, | 64 STOPPED, |
66 RUNNING, | 65 RUNNING, |
67 PENDING_START | 66 PENDING_START |
68 }; | 67 }; |
69 | 68 |
70 void OnDidStart(base::SharedMemoryHandle handle) { | 69 void OnDidStart(base::SharedMemoryHandle handle) { |
71 DVLOG(2) << "did start sensor event pump"; | 70 DVLOG(2) << "did start sensor event pump"; |
72 | 71 |
73 if (state_ != PENDING_START) | 72 if (state_ != PENDING_START) |
74 return; | 73 return; |
75 | 74 |
76 DCHECK(!timer_.IsRunning()); | 75 DCHECK(!timer_.IsRunning()); |
77 | 76 |
78 if (InitializeReader(handle)) { | 77 if (InitializeReader(handle)) { |
79 timer_.Start(FROM_HERE, | 78 timer_.Start(FROM_HERE, |
80 base::TimeDelta::FromMilliseconds(pump_delay_millis_), | 79 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
81 this, &DeviceSensorEventPump::FireEvent); | 80 this, |
81 &DeviceSensorEventPump::FireEvent); | |
82 state_ = RUNNING; | 82 state_ = RUNNING; |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 virtual void FireEvent() = 0; | 86 virtual void FireEvent() = 0; |
87 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 87 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
88 | 88 |
89 int pump_delay_millis_; | 89 int pump_delay_microseconds_; |
90 PumpState state_; | 90 PumpState state_; |
91 base::RepeatingTimer<DeviceSensorEventPump> timer_; | 91 base::RepeatingTimer<DeviceSensorEventPump> timer_; |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | 93 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |
94 }; | 94 }; |
95 | 95 |
96 } // namespace content | 96 } // namespace content |
97 | 97 |
98 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 98 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
OLD | NEW |