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/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
| 12 #include "content/public/common/service_registry.h" | |
| 12 #include "content/public/renderer/platform_event_observer.h" | 13 #include "content/public/renderer/platform_event_observer.h" |
| 14 #include "content/renderer/render_thread_impl.h" | |
| 15 #include "mojo/public/cpp/system/platform_handle.h" | |
| 13 | 16 |
| 14 namespace content { | 17 namespace content { |
| 15 | 18 |
| 19 template <typename Base, typename MojoInterface> | |
| 20 class CONTENT_EXPORT DeviceSensorMojoClientMixin : public Base { | |
| 21 public: | |
| 22 template <typename... Args> | |
| 23 explicit DeviceSensorMojoClientMixin(Args&&... args) | |
| 24 : Base(std::forward<Args>(args)...) {} | |
| 25 | |
| 26 void SendStartMessage() override { | |
| 27 GetMojoInterface().StartPolling( | |
| 28 base::Bind(&DeviceSensorMojoClientMixin<Base, MojoInterface>::DidStart, | |
| 29 base::Unretained(this))); | |
| 30 } | |
| 31 void SendStopMessage() override { GetMojoInterface().StopPolling(); } | |
| 32 | |
| 33 protected: | |
| 34 MojoInterface& GetMojoInterface() { | |
| 35 if (!mojo_interface_) { | |
| 36 RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( | |
| 37 mojo::GetProxy(&mojo_interface_)); | |
| 38 } | |
| 39 return *mojo_interface_; | |
| 40 } | |
| 41 | |
| 42 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { | |
| 43 // When running layout tests, those observers should not listen to the | |
| 44 // actual hardware changes. In order to make that happen, discard start | |
| 45 // responses. | |
| 46 if (RenderThreadImpl::current() && | |
| 47 RenderThreadImpl::current()->layout_test_mode()) { | |
|
timvolodine
2016/06/15 15:54:47
Probably even better to put this in SendStartMessa
Sam McNally
2016/06/16 00:51:03
Done.
| |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 Base::DidStart(std::move(buffer_handle)); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 mojo::InterfacePtr<MojoInterface> mojo_interface_; | |
| 56 }; | |
| 57 | |
| 16 template <typename ListenerType> | 58 template <typename ListenerType> |
| 17 class CONTENT_EXPORT DeviceSensorEventPump | 59 class CONTENT_EXPORT DeviceSensorEventPump |
| 18 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | 60 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { |
| 19 public: | 61 public: |
| 20 // Default rate for firing events. | 62 // Default rate for firing events. |
| 21 static const int kDefaultPumpFrequencyHz = 60; | 63 static const int kDefaultPumpFrequencyHz = 60; |
| 22 static const int kDefaultPumpDelayMicroseconds = | 64 static const int kDefaultPumpDelayMicroseconds = |
| 23 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | 65 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; |
| 24 | 66 |
| 25 // PlatformEventObserver | 67 // PlatformEventObserver |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 // STOPPED -> PENDING_START | 106 // STOPPED -> PENDING_START |
| 65 // PENDING_START -> RUNNING | 107 // PENDING_START -> RUNNING |
| 66 // PENDING_START -> STOPPED | 108 // PENDING_START -> STOPPED |
| 67 // RUNNING -> STOPPED | 109 // RUNNING -> STOPPED |
| 68 enum PumpState { | 110 enum PumpState { |
| 69 STOPPED, | 111 STOPPED, |
| 70 RUNNING, | 112 RUNNING, |
| 71 PENDING_START | 113 PENDING_START |
| 72 }; | 114 }; |
| 73 | 115 |
| 74 void OnDidStart(base::SharedMemoryHandle handle) { | 116 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { |
| 75 DVLOG(2) << "did start sensor event pump"; | 117 DVLOG(2) << "did start sensor event pump"; |
| 76 | 118 |
| 77 if (state_ != PENDING_START) | 119 if (state_ != PENDING_START) |
| 78 return; | 120 return; |
| 79 | 121 |
| 80 DCHECK(!timer_.IsRunning()); | 122 DCHECK(!timer_.IsRunning()); |
| 81 | 123 |
| 124 base::SharedMemoryHandle handle; | |
| 125 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 126 std::move(buffer_handle), &handle, nullptr, nullptr); | |
| 127 DCHECK_EQ(MOJO_RESULT_OK, result); | |
| 128 | |
| 82 if (InitializeReader(handle)) { | 129 if (InitializeReader(handle)) { |
| 83 timer_.Start(FROM_HERE, | 130 timer_.Start(FROM_HERE, |
| 84 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), | 131 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
| 85 this, | 132 this, |
| 86 &DeviceSensorEventPump::FireEvent); | 133 &DeviceSensorEventPump::FireEvent); |
| 87 state_ = RUNNING; | 134 state_ = RUNNING; |
| 88 } | 135 } |
| 89 } | 136 } |
| 90 | 137 |
| 91 virtual void FireEvent() = 0; | 138 virtual void FireEvent() = 0; |
| 92 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 139 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
| 93 | 140 |
| 94 int pump_delay_microseconds_; | 141 int pump_delay_microseconds_; |
| 95 PumpState state_; | 142 PumpState state_; |
| 96 base::RepeatingTimer timer_; | 143 base::RepeatingTimer timer_; |
| 97 | 144 |
| 98 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | 145 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |
| 99 }; | 146 }; |
| 100 | 147 |
| 101 } // namespace content | 148 } // namespace content |
| 102 | 149 |
| 103 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 150 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
| OLD | NEW |