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