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. |
| 33 // |
| 34 // TODO(sammc): Remove this when JS layout test support for shared buffers |
| 35 // is ready and the layout tests are converted to use that for mocking. |
| 36 if (RenderThreadImpl::current() && |
| 37 !RenderThreadImpl::current()->layout_test_mode()) { |
| 38 RenderThread::Get()->GetRemoteInterfaces()->GetInterface( |
| 39 std::move(request)); |
| 40 } |
| 41 } |
| 42 |
| 43 void SendStartMessage() override { |
| 44 mojo_interface_->StartPolling( |
| 45 base::Bind(&DeviceSensorMojoClientMixin<Base, MojoInterface>::DidStart, |
| 46 base::Unretained(this))); |
| 47 } |
| 48 void SendStopMessage() override { mojo_interface_->StopPolling(); } |
| 49 |
| 50 protected: |
| 51 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { |
| 52 Base::DidStart(std::move(buffer_handle)); |
| 53 } |
| 54 |
| 55 private: |
| 56 mojo::InterfacePtr<MojoInterface> mojo_interface_; |
| 57 }; |
| 58 |
16 template <typename ListenerType> | 59 template <typename ListenerType> |
17 class CONTENT_EXPORT DeviceSensorEventPump | 60 class CONTENT_EXPORT DeviceSensorEventPump |
18 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | 61 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { |
19 public: | 62 public: |
20 // Default rate for firing events. | 63 // Default rate for firing events. |
21 static const int kDefaultPumpFrequencyHz = 60; | 64 static const int kDefaultPumpFrequencyHz = 60; |
22 static const int kDefaultPumpDelayMicroseconds = | 65 static const int kDefaultPumpDelayMicroseconds = |
23 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | 66 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; |
24 | 67 |
25 // PlatformEventObserver | 68 // PlatformEventObserver |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // STOPPED -> PENDING_START | 107 // STOPPED -> PENDING_START |
65 // PENDING_START -> RUNNING | 108 // PENDING_START -> RUNNING |
66 // PENDING_START -> STOPPED | 109 // PENDING_START -> STOPPED |
67 // RUNNING -> STOPPED | 110 // RUNNING -> STOPPED |
68 enum PumpState { | 111 enum PumpState { |
69 STOPPED, | 112 STOPPED, |
70 RUNNING, | 113 RUNNING, |
71 PENDING_START | 114 PENDING_START |
72 }; | 115 }; |
73 | 116 |
74 void OnDidStart(base::SharedMemoryHandle handle) { | 117 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { |
75 DVLOG(2) << "did start sensor event pump"; | 118 DVLOG(2) << "did start sensor event pump"; |
76 | 119 |
77 if (state_ != PENDING_START) | 120 if (state_ != PENDING_START) |
78 return; | 121 return; |
79 | 122 |
80 DCHECK(!timer_.IsRunning()); | 123 DCHECK(!timer_.IsRunning()); |
81 | 124 |
| 125 base::SharedMemoryHandle handle; |
| 126 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 127 std::move(buffer_handle), &handle, nullptr, nullptr); |
| 128 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 129 |
82 if (InitializeReader(handle)) { | 130 if (InitializeReader(handle)) { |
83 timer_.Start(FROM_HERE, | 131 timer_.Start(FROM_HERE, |
84 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), | 132 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
85 this, | 133 this, |
86 &DeviceSensorEventPump::FireEvent); | 134 &DeviceSensorEventPump::FireEvent); |
87 state_ = RUNNING; | 135 state_ = RUNNING; |
88 } | 136 } |
89 } | 137 } |
90 | 138 |
91 virtual void FireEvent() = 0; | 139 virtual void FireEvent() = 0; |
92 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 140 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
93 | 141 |
94 int pump_delay_microseconds_; | 142 int pump_delay_microseconds_; |
95 PumpState state_; | 143 PumpState state_; |
96 base::RepeatingTimer timer_; | 144 base::RepeatingTimer timer_; |
97 | 145 |
98 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | 146 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |
99 }; | 147 }; |
100 | 148 |
101 } // namespace content | 149 } // namespace content |
102 | 150 |
103 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 151 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
OLD | NEW |