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