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/render_process_observer.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
| 13 class RenderThread; |
13 | 14 |
14 template <typename ListenerType> | 15 class CONTENT_EXPORT DeviceSensorEventPump : public RenderProcessObserver { |
15 class CONTENT_EXPORT DeviceSensorEventPump | |
16 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | |
17 public: | 16 public: |
18 // Default delay between subsequent firing of events. | 17 // Default delay between subsequent firing of events. |
19 static const int kDefaultPumpDelayMillis = 50; | 18 static const int kDefaultPumpDelayMillis; |
20 | 19 |
21 // PlatformEventObserver | 20 int GetDelayMillis() const; |
22 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { | |
23 DVLOG(2) << "requested start"; | |
24 | 21 |
25 if (state_ != STOPPED) | 22 void Attach(RenderThread* thread); |
26 return; | 23 virtual bool OnControlMessageReceived(const IPC::Message& message) = 0; |
27 | |
28 DCHECK(!timer_.IsRunning()); | |
29 | |
30 PlatformEventObserver<ListenerType>::Start(listener); | |
31 state_ = PENDING_START; | |
32 } | |
33 | |
34 virtual void Stop() OVERRIDE { | |
35 DVLOG(2) << "stop"; | |
36 | |
37 if (state_ == STOPPED) | |
38 return; | |
39 | |
40 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) || | |
41 (state_ == RUNNING && timer_.IsRunning())); | |
42 | |
43 if (timer_.IsRunning()) | |
44 timer_.Stop(); | |
45 PlatformEventObserver<ListenerType>::Stop(); | |
46 state_ = STOPPED; | |
47 } | |
48 | 24 |
49 protected: | 25 protected: |
50 explicit DeviceSensorEventPump(RenderThread* thread) | 26 // Constructor for a pump with default delay. |
51 : PlatformEventObserver<ListenerType>(thread), | 27 DeviceSensorEventPump(); |
52 pump_delay_millis_(kDefaultPumpDelayMillis), | |
53 state_(STOPPED) { | |
54 } | |
55 | 28 |
56 virtual ~DeviceSensorEventPump() { | 29 // Constructor for a pump with a given delay. |
57 } | 30 explicit DeviceSensorEventPump(int pump_delay_millis); |
| 31 virtual ~DeviceSensorEventPump(); |
58 | 32 |
59 // The pump is a tri-state automaton with allowed transitions as follows: | 33 // The pump is a tri-state automaton with allowed transitions as follows: |
60 // STOPPED -> PENDING_START | 34 // STOPPED -> PENDING_START |
61 // PENDING_START -> RUNNING | 35 // PENDING_START -> RUNNING |
62 // PENDING_START -> STOPPED | 36 // PENDING_START -> STOPPED |
63 // RUNNING -> STOPPED | 37 // RUNNING -> STOPPED |
64 enum PumpState { | 38 enum PumpState { |
65 STOPPED, | 39 STOPPED, |
66 RUNNING, | 40 RUNNING, |
67 PENDING_START | 41 PENDING_START |
68 }; | 42 }; |
69 | 43 |
70 void OnDidStart(base::SharedMemoryHandle handle) { | 44 bool RequestStart(); |
71 DVLOG(2) << "did start sensor event pump"; | 45 void OnDidStart(base::SharedMemoryHandle handle); |
72 | 46 bool Stop(); |
73 if (state_ != PENDING_START) | |
74 return; | |
75 | |
76 DCHECK(!timer_.IsRunning()); | |
77 | |
78 if (InitializeReader(handle)) { | |
79 timer_.Start(FROM_HERE, | |
80 base::TimeDelta::FromMilliseconds(pump_delay_millis_), | |
81 this, &DeviceSensorEventPump::FireEvent); | |
82 state_ = RUNNING; | |
83 } | |
84 } | |
85 | 47 |
86 virtual void FireEvent() = 0; | 48 virtual void FireEvent() = 0; |
87 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 49 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
| 50 virtual bool SendStartMessage() = 0; |
| 51 virtual bool SendStopMessage() = 0; |
88 | 52 |
89 int pump_delay_millis_; | 53 int pump_delay_millis_; |
90 PumpState state_; | 54 PumpState state_; |
91 base::RepeatingTimer<DeviceSensorEventPump> timer_; | 55 base::RepeatingTimer<DeviceSensorEventPump> timer_; |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | |
94 }; | 56 }; |
95 | 57 |
96 } // namespace content | 58 } // namespace content |
97 | 59 |
98 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 60 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
OLD | NEW |