Chromium Code Reviews| 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/basictypes.h" | |
| 8 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/time/time.h" | |
| 9 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
| 10 #include "content/public/renderer/platform_event_observer.h" | 12 #include "content/public/renderer/platform_event_observer.h" |
| 11 | 13 |
| 12 namespace content { | 14 namespace content { |
| 13 | 15 |
| 14 template <typename ListenerType> | 16 template <typename ListenerType> |
| 15 class CONTENT_EXPORT DeviceSensorEventPump | 17 class CONTENT_EXPORT DeviceSensorEventPump |
| 16 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | 18 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { |
| 17 public: | 19 public: |
| 18 // Default delay between subsequent firing of events. | 20 // Default rate for firing events. |
| 19 static const int kDefaultPumpDelayMillis = 50; | 21 static const int64 kDefaultPumpFrequencyHz = 60; |
| 22 static const int64 kDefaultPumpDelayMicroseconds = | |
| 23 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | |
| 20 | 24 |
| 21 // PlatformEventObserver | 25 // PlatformEventObserver |
| 22 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { | 26 virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { |
| 23 DVLOG(2) << "requested start"; | 27 DVLOG(2) << "requested start"; |
| 24 | 28 |
| 25 if (state_ != STOPPED) | 29 if (state_ != STOPPED) |
| 26 return; | 30 return; |
| 27 | 31 |
| 28 DCHECK(!timer_.IsRunning()); | 32 DCHECK(!timer_.IsRunning()); |
| 29 | 33 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 42 | 46 |
| 43 if (timer_.IsRunning()) | 47 if (timer_.IsRunning()) |
| 44 timer_.Stop(); | 48 timer_.Stop(); |
| 45 PlatformEventObserver<ListenerType>::Stop(); | 49 PlatformEventObserver<ListenerType>::Stop(); |
| 46 state_ = STOPPED; | 50 state_ = STOPPED; |
| 47 } | 51 } |
| 48 | 52 |
| 49 protected: | 53 protected: |
| 50 explicit DeviceSensorEventPump(RenderThread* thread) | 54 explicit DeviceSensorEventPump(RenderThread* thread) |
| 51 : PlatformEventObserver<ListenerType>(thread), | 55 : PlatformEventObserver<ListenerType>(thread), |
| 52 pump_delay_millis_(kDefaultPumpDelayMillis), | 56 pump_delay_microseconds_(kDefaultPumpDelayMicroseconds), |
| 53 state_(STOPPED) { | 57 state_(STOPPED) {} |
| 54 } | |
| 55 | 58 |
| 56 virtual ~DeviceSensorEventPump() { | 59 virtual ~DeviceSensorEventPump() { |
| 57 } | 60 } |
| 58 | 61 |
| 59 // The pump is a tri-state automaton with allowed transitions as follows: | 62 // The pump is a tri-state automaton with allowed transitions as follows: |
| 60 // STOPPED -> PENDING_START | 63 // STOPPED -> PENDING_START |
| 61 // PENDING_START -> RUNNING | 64 // PENDING_START -> RUNNING |
| 62 // PENDING_START -> STOPPED | 65 // PENDING_START -> STOPPED |
| 63 // RUNNING -> STOPPED | 66 // RUNNING -> STOPPED |
| 64 enum PumpState { | 67 enum PumpState { |
| 65 STOPPED, | 68 STOPPED, |
| 66 RUNNING, | 69 RUNNING, |
| 67 PENDING_START | 70 PENDING_START |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 void OnDidStart(base::SharedMemoryHandle handle) { | 73 void OnDidStart(base::SharedMemoryHandle handle) { |
| 71 DVLOG(2) << "did start sensor event pump"; | 74 DVLOG(2) << "did start sensor event pump"; |
| 72 | 75 |
| 73 if (state_ != PENDING_START) | 76 if (state_ != PENDING_START) |
| 74 return; | 77 return; |
| 75 | 78 |
| 76 DCHECK(!timer_.IsRunning()); | 79 DCHECK(!timer_.IsRunning()); |
| 77 | 80 |
| 78 if (InitializeReader(handle)) { | 81 if (InitializeReader(handle)) { |
| 79 timer_.Start(FROM_HERE, | 82 timer_.Start(FROM_HERE, |
| 80 base::TimeDelta::FromMilliseconds(pump_delay_millis_), | 83 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
| 81 this, &DeviceSensorEventPump::FireEvent); | 84 this, |
| 85 &DeviceSensorEventPump::FireEvent); | |
| 82 state_ = RUNNING; | 86 state_ = RUNNING; |
| 83 } | 87 } |
| 84 } | 88 } |
| 85 | 89 |
| 86 virtual void FireEvent() = 0; | 90 virtual void FireEvent() = 0; |
| 87 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 91 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
| 88 | 92 |
| 89 int pump_delay_millis_; | 93 int pump_delay_microseconds_; |
|
timvolodine
2014/09/26 13:09:16
int64?
| |
| 90 PumpState state_; | 94 PumpState state_; |
| 91 base::RepeatingTimer<DeviceSensorEventPump> timer_; | 95 base::RepeatingTimer<DeviceSensorEventPump> timer_; |
| 92 | 96 |
| 93 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | 97 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |
| 94 }; | 98 }; |
| 95 | 99 |
| 96 } // namespace content | 100 } // namespace content |
| 97 | 101 |
| 98 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 102 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
| OLD | NEW |