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 "mojo/public/cpp/system/platform_handle.h" |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
| 18 template <typename Base, typename MojoInterface> |
| 19 class CONTENT_EXPORT DeviceMojoClientMixin : public Base { |
| 20 public: |
| 21 template <typename... Args> |
| 22 explicit DeviceMojoClientMixin(Args&&... args) |
| 23 : Base(std::forward<Args>(args)...) {} |
| 24 |
| 25 void SendStartMessage() override { |
| 26 GetMojoInterface().StartPolling( |
| 27 base::Bind(&DeviceMojoClientMixin<Base, MojoInterface>::DidStart, |
| 28 base::Unretained(this))); |
| 29 } |
| 30 void SendStopMessage() override { GetMojoInterface().StopPolling(); } |
| 31 |
| 32 protected: |
| 33 MojoInterface& GetMojoInterface() { |
| 34 if (!mojo_interface_) { |
| 35 RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( |
| 36 mojo::GetProxy(&mojo_interface_)); |
| 37 } |
| 38 return *mojo_interface_; |
| 39 } |
| 40 |
| 41 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { |
| 42 Base::DidStart(std::move(buffer_handle)); |
| 43 } |
| 44 |
| 45 private: |
| 46 mojo::InterfacePtr<MojoInterface> mojo_interface_; |
| 47 }; |
| 48 |
16 template <typename ListenerType> | 49 template <typename ListenerType> |
17 class CONTENT_EXPORT DeviceSensorEventPump | 50 class CONTENT_EXPORT DeviceSensorEventPump |
18 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { | 51 : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { |
19 public: | 52 public: |
20 // Default rate for firing events. | 53 // Default rate for firing events. |
21 static const int kDefaultPumpFrequencyHz = 60; | 54 static const int kDefaultPumpFrequencyHz = 60; |
22 static const int kDefaultPumpDelayMicroseconds = | 55 static const int kDefaultPumpDelayMicroseconds = |
23 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; | 56 base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz; |
24 | 57 |
25 // PlatformEventObserver | 58 // PlatformEventObserver |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // STOPPED -> PENDING_START | 97 // STOPPED -> PENDING_START |
65 // PENDING_START -> RUNNING | 98 // PENDING_START -> RUNNING |
66 // PENDING_START -> STOPPED | 99 // PENDING_START -> STOPPED |
67 // RUNNING -> STOPPED | 100 // RUNNING -> STOPPED |
68 enum PumpState { | 101 enum PumpState { |
69 STOPPED, | 102 STOPPED, |
70 RUNNING, | 103 RUNNING, |
71 PENDING_START | 104 PENDING_START |
72 }; | 105 }; |
73 | 106 |
74 void OnDidStart(base::SharedMemoryHandle handle) { | 107 void DidStart(mojo::ScopedSharedBufferHandle buffer_handle) { |
75 DVLOG(2) << "did start sensor event pump"; | 108 DVLOG(2) << "did start sensor event pump"; |
76 | 109 |
77 if (state_ != PENDING_START) | 110 if (state_ != PENDING_START) |
78 return; | 111 return; |
79 | 112 |
80 DCHECK(!timer_.IsRunning()); | 113 DCHECK(!timer_.IsRunning()); |
81 | 114 |
| 115 base::SharedMemoryHandle handle; |
| 116 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 117 std::move(buffer_handle), &handle, nullptr, nullptr); |
| 118 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 119 |
82 if (InitializeReader(handle)) { | 120 if (InitializeReader(handle)) { |
83 timer_.Start(FROM_HERE, | 121 timer_.Start(FROM_HERE, |
84 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), | 122 base::TimeDelta::FromMicroseconds(pump_delay_microseconds_), |
85 this, | 123 this, |
86 &DeviceSensorEventPump::FireEvent); | 124 &DeviceSensorEventPump::FireEvent); |
87 state_ = RUNNING; | 125 state_ = RUNNING; |
88 } | 126 } |
89 } | 127 } |
90 | 128 |
91 virtual void FireEvent() = 0; | 129 virtual void FireEvent() = 0; |
92 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; | 130 virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; |
93 | 131 |
94 int pump_delay_microseconds_; | 132 int pump_delay_microseconds_; |
95 PumpState state_; | 133 PumpState state_; |
96 base::RepeatingTimer timer_; | 134 base::RepeatingTimer timer_; |
97 | 135 |
98 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); | 136 DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |
99 }; | 137 }; |
100 | 138 |
101 } // namespace content | 139 } // namespace content |
102 | 140 |
103 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ | 141 #endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ |
OLD | NEW |