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