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 #include "content/browser/device_sensors/data_fetcher_shared_memory_base.h" | 5 #include "device/device_sensors/data_fetcher_shared_memory_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 #include "content/common/device_sensors/device_light_hardware_buffer.h" | 12 #include "device/device_sensors/device_light_hardware_buffer.h" |
13 #include "content/common/device_sensors/device_motion_hardware_buffer.h" | 13 #include "device/device_sensors/device_motion_hardware_buffer.h" |
14 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | 14 #include "device/device_sensors/device_orientation_hardware_buffer.h" |
15 | 15 |
16 namespace content { | 16 namespace device { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { | 20 size_t GetConsumerSharedMemoryBufferSize(ConsumerType consumer_type) { |
21 switch (consumer_type) { | 21 switch (consumer_type) { |
22 case CONSUMER_TYPE_MOTION: | 22 case CONSUMER_TYPE_MOTION: |
23 return sizeof(DeviceMotionHardwareBuffer); | 23 return sizeof(DeviceMotionHardwareBuffer); |
24 case CONSUMER_TYPE_ORIENTATION: | 24 case CONSUMER_TYPE_ORIENTATION: |
25 return sizeof(DeviceOrientationHardwareBuffer); | 25 return sizeof(DeviceOrientationHardwareBuffer); |
26 case CONSUMER_TYPE_LIGHT: | 26 case CONSUMER_TYPE_LIGHT: |
(...skipping 15 matching lines...) Expand all Loading... |
42 void RemoveConsumer(ConsumerType consumer_type); | 42 void RemoveConsumer(ConsumerType consumer_type); |
43 | 43 |
44 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } | 44 unsigned GetConsumersBitmask() const { return consumers_bitmask_; } |
45 bool IsTimerRunning() const { return timer_ ? timer_->IsRunning() : false; } | 45 bool IsTimerRunning() const { return timer_ ? timer_->IsRunning() : false; } |
46 | 46 |
47 private: | 47 private: |
48 void DoPoll(); | 48 void DoPoll(); |
49 | 49 |
50 unsigned consumers_bitmask_; | 50 unsigned consumers_bitmask_; |
51 DataFetcherSharedMemoryBase* fetcher_; | 51 DataFetcherSharedMemoryBase* fetcher_; |
52 scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; | 52 scoped_ptr<base::RepeatingTimer<PollingThread>> timer_; |
53 | 53 |
54 DISALLOW_COPY_AND_ASSIGN(PollingThread); | 54 DISALLOW_COPY_AND_ASSIGN(PollingThread); |
55 }; | 55 }; |
56 | 56 |
57 // --- PollingThread methods | 57 // --- PollingThread methods |
58 | 58 |
59 DataFetcherSharedMemoryBase::PollingThread::PollingThread( | 59 DataFetcherSharedMemoryBase::PollingThread::PollingThread( |
60 const char* name, DataFetcherSharedMemoryBase* fetcher) | 60 const char* name, |
61 : base::Thread(name), | 61 DataFetcherSharedMemoryBase* fetcher) |
62 consumers_bitmask_(0), | 62 : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) { |
63 fetcher_(fetcher) { | |
64 } | 63 } |
65 | 64 |
66 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { | 65 DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { |
67 } | 66 } |
68 | 67 |
69 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( | 68 void DataFetcherSharedMemoryBase::PollingThread::AddConsumer( |
70 ConsumerType consumer_type, void* buffer) { | 69 ConsumerType consumer_type, |
| 70 void* buffer) { |
71 DCHECK(fetcher_); | 71 DCHECK(fetcher_); |
72 if (!fetcher_->Start(consumer_type, buffer)) | 72 if (!fetcher_->Start(consumer_type, buffer)) |
73 return; | 73 return; |
74 | 74 |
75 consumers_bitmask_ |= consumer_type; | 75 consumers_bitmask_ |= consumer_type; |
76 | 76 |
77 if (!timer_ && fetcher_->GetType() == FETCHER_TYPE_POLLING_CALLBACK) { | 77 if (!timer_ && fetcher_->GetType() == FETCHER_TYPE_POLLING_CALLBACK) { |
78 timer_.reset(new base::RepeatingTimer<PollingThread>()); | 78 timer_.reset(new base::RepeatingTimer<PollingThread>()); |
79 timer_->Start(FROM_HERE, | 79 timer_->Start(FROM_HERE, fetcher_->GetInterval(), this, |
80 fetcher_->GetInterval(), | 80 &PollingThread::DoPoll); |
81 this, &PollingThread::DoPoll); | |
82 } | 81 } |
83 } | 82 } |
84 | 83 |
85 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( | 84 void DataFetcherSharedMemoryBase::PollingThread::RemoveConsumer( |
86 ConsumerType consumer_type) { | 85 ConsumerType consumer_type) { |
87 DCHECK(fetcher_); | 86 DCHECK(fetcher_); |
88 if (!fetcher_->Stop(consumer_type)) | 87 if (!fetcher_->Stop(consumer_type)) |
89 return; | 88 return; |
90 | 89 |
91 consumers_bitmask_ ^= consumer_type; | 90 consumers_bitmask_ ^= consumer_type; |
(...skipping 15 matching lines...) Expand all Loading... |
107 } | 106 } |
108 | 107 |
109 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { | 108 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { |
110 DCHECK_EQ(0u, started_consumers_); | 109 DCHECK_EQ(0u, started_consumers_); |
111 | 110 |
112 // make sure polling thread stops asap. | 111 // make sure polling thread stops asap. |
113 if (polling_thread_) | 112 if (polling_thread_) |
114 polling_thread_->Stop(); | 113 polling_thread_->Stop(); |
115 | 114 |
116 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), | 115 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), |
117 shared_memory_map_.end()); | 116 shared_memory_map_.end()); |
118 } | 117 } |
119 | 118 |
120 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 119 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
121 ConsumerType consumer_type) { | 120 ConsumerType consumer_type) { |
122 if (started_consumers_ & consumer_type) | 121 if (started_consumers_ & consumer_type) |
123 return true; | 122 return true; |
124 | 123 |
125 void* buffer = GetSharedMemoryBuffer(consumer_type); | 124 void* buffer = GetSharedMemoryBuffer(consumer_type); |
126 if (!buffer) | 125 if (!buffer) |
127 return false; | 126 return false; |
128 | 127 |
129 if (GetType() != FETCHER_TYPE_DEFAULT) { | 128 if (GetType() != FETCHER_TYPE_DEFAULT) { |
130 if (!InitAndStartPollingThreadIfNecessary()) | 129 if (!InitAndStartPollingThreadIfNecessary()) |
131 return false; | 130 return false; |
132 polling_thread_->message_loop()->PostTask( | 131 polling_thread_->message_loop()->PostTask( |
133 FROM_HERE, | 132 FROM_HERE, base::Bind(&PollingThread::AddConsumer, |
134 base::Bind(&PollingThread::AddConsumer, | 133 base::Unretained(polling_thread_.get()), |
135 base::Unretained(polling_thread_.get()), | 134 consumer_type, buffer)); |
136 consumer_type, buffer)); | |
137 } else { | 135 } else { |
138 if (!Start(consumer_type, buffer)) | 136 if (!Start(consumer_type, buffer)) |
139 return false; | 137 return false; |
140 } | 138 } |
141 | 139 |
142 started_consumers_ |= consumer_type; | 140 started_consumers_ |= consumer_type; |
143 | 141 |
144 return true; | 142 return true; |
145 } | 143 } |
146 | 144 |
147 bool DataFetcherSharedMemoryBase::StopFetchingDeviceData( | 145 bool DataFetcherSharedMemoryBase::StopFetchingDeviceData( |
148 ConsumerType consumer_type) { | 146 ConsumerType consumer_type) { |
149 if (!(started_consumers_ & consumer_type)) | 147 if (!(started_consumers_ & consumer_type)) |
150 return true; | 148 return true; |
151 | 149 |
152 if (GetType() != FETCHER_TYPE_DEFAULT) { | 150 if (GetType() != FETCHER_TYPE_DEFAULT) { |
153 polling_thread_->message_loop()->PostTask( | 151 polling_thread_->message_loop()->PostTask( |
154 FROM_HERE, | 152 FROM_HERE, |
155 base::Bind(&PollingThread::RemoveConsumer, | 153 base::Bind(&PollingThread::RemoveConsumer, |
156 base::Unretained(polling_thread_.get()), | 154 base::Unretained(polling_thread_.get()), consumer_type)); |
157 consumer_type)); | |
158 } else { | 155 } else { |
159 if (!Stop(consumer_type)) | 156 if (!Stop(consumer_type)) |
160 return false; | 157 return false; |
161 } | 158 } |
162 | 159 |
163 started_consumers_ ^= consumer_type; | 160 started_consumers_ ^= consumer_type; |
164 | 161 |
165 return true; | 162 return true; |
166 } | 163 } |
167 | 164 |
168 void DataFetcherSharedMemoryBase::Shutdown() { | 165 void DataFetcherSharedMemoryBase::Shutdown() { |
169 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); | 166 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); |
170 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); | 167 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); |
171 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); | 168 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); |
172 } | 169 } |
173 | 170 |
174 base::SharedMemoryHandle | 171 base::SharedMemoryHandle |
175 DataFetcherSharedMemoryBase::GetSharedMemoryHandleForProcess( | 172 DataFetcherSharedMemoryBase::GetSharedMemoryHandleForProcess( |
176 ConsumerType consumer_type, base::ProcessHandle process) { | 173 ConsumerType consumer_type, |
| 174 base::ProcessHandle process) { |
177 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); | 175 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); |
178 if (it == shared_memory_map_.end()) | 176 if (it == shared_memory_map_.end()) |
179 return base::SharedMemory::NULLHandle(); | 177 return base::SharedMemory::NULLHandle(); |
180 | 178 |
181 base::SharedMemoryHandle renderer_handle; | 179 base::SharedMemoryHandle renderer_handle; |
182 it->second->ShareToProcess(process, &renderer_handle); | 180 it->second->ShareToProcess(process, &renderer_handle); |
183 return renderer_handle; | 181 return renderer_handle; |
184 } | 182 } |
185 | 183 |
186 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { | 184 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { |
187 if (polling_thread_) | 185 if (polling_thread_) |
188 return true; | 186 return true; |
189 | 187 |
190 polling_thread_.reset( | 188 polling_thread_.reset( |
191 new PollingThread("Inertial Device Sensor poller", this)); | 189 new PollingThread("Inertial Device Sensor poller", this)); |
192 | 190 |
193 if (!polling_thread_->Start()) { | 191 if (!polling_thread_->Start()) { |
194 LOG(ERROR) << "Failed to start inertial sensor data polling thread"; | 192 LOG(ERROR) << "Failed to start inertial sensor data polling thread"; |
195 return false; | 193 return false; |
196 } | 194 } |
197 return true; | 195 return true; |
198 } | 196 } |
199 | 197 |
200 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { | 198 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { |
201 NOTIMPLEMENTED(); | 199 NOTIMPLEMENTED(); |
202 } | 200 } |
203 | 201 |
204 DataFetcherSharedMemoryBase::FetcherType | 202 DataFetcherSharedMemoryBase::FetcherType DataFetcherSharedMemoryBase::GetType() |
205 DataFetcherSharedMemoryBase::GetType() const { | 203 const { |
206 return FETCHER_TYPE_DEFAULT; | 204 return FETCHER_TYPE_DEFAULT; |
207 } | 205 } |
208 | 206 |
209 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { | 207 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { |
210 return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds); | 208 return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds); |
211 } | 209 } |
212 | 210 |
213 base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory( | 211 base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory( |
214 ConsumerType consumer_type) { | 212 ConsumerType consumer_type) { |
215 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); | 213 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); |
(...skipping 25 matching lines...) Expand all Loading... |
241 } | 239 } |
242 | 240 |
243 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 241 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
244 return polling_thread_ ? polling_thread_->message_loop() : nullptr; | 242 return polling_thread_ ? polling_thread_->message_loop() : nullptr; |
245 } | 243 } |
246 | 244 |
247 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { | 245 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { |
248 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; | 246 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; |
249 } | 247 } |
250 | 248 |
251 } // namespace content | 249 } // namespace device |
OLD | NEW |