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 "content/browser/device_sensors/data_fetcher_shared_memory_base.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() | 112 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() |
113 : started_consumers_(0) { | 113 : started_consumers_(0) { |
114 } | 114 } |
115 | 115 |
116 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { | 116 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { |
117 DCHECK_EQ(0u, started_consumers_); | 117 DCHECK_EQ(0u, started_consumers_); |
118 | 118 |
119 // make sure polling thread stops asap. | 119 // make sure polling thread stops asap. |
120 if (polling_thread_) | 120 if (polling_thread_) |
121 polling_thread_->Stop(); | 121 polling_thread_->Stop(); |
122 | |
123 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), | |
124 shared_memory_map_.end()); | |
125 } | 122 } |
126 | 123 |
127 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 124 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
128 ConsumerType consumer_type) { | 125 ConsumerType consumer_type) { |
129 if (started_consumers_ & consumer_type) | 126 if (started_consumers_ & consumer_type) |
130 return true; | 127 return true; |
131 | 128 |
132 void* buffer = GetSharedMemoryBuffer(consumer_type); | 129 void* buffer = GetSharedMemoryBuffer(consumer_type); |
133 if (!buffer) | 130 if (!buffer) |
134 return false; | 131 return false; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 return true; | 173 return true; |
177 } | 174 } |
178 | 175 |
179 void DataFetcherSharedMemoryBase::Shutdown() { | 176 void DataFetcherSharedMemoryBase::Shutdown() { |
180 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); | 177 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); |
181 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); | 178 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); |
182 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 179 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
183 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); | 180 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); |
184 } | 181 } |
185 | 182 |
186 base::SharedMemoryHandle | 183 mojo::ScopedSharedBufferHandle |
187 DataFetcherSharedMemoryBase::GetSharedMemoryHandleForProcess( | 184 DataFetcherSharedMemoryBase::GetSharedMemoryHandle(ConsumerType consumer_type) { |
188 ConsumerType consumer_type, base::ProcessHandle process) { | 185 auto it = shared_memory_map_.find(consumer_type); |
189 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); | |
190 if (it == shared_memory_map_.end()) | 186 if (it == shared_memory_map_.end()) |
191 return base::SharedMemory::NULLHandle(); | 187 return mojo::ScopedSharedBufferHandle(); |
192 | 188 |
193 base::SharedMemoryHandle renderer_handle; | 189 return it->second->Clone(); |
194 it->second->ShareToProcess(process, &renderer_handle); | |
195 return renderer_handle; | |
196 } | 190 } |
197 | 191 |
198 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { | 192 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { |
199 if (polling_thread_) | 193 if (polling_thread_) |
200 return true; | 194 return true; |
201 | 195 |
202 polling_thread_.reset( | 196 polling_thread_.reset( |
203 new PollingThread("Inertial Device Sensor poller", this)); | 197 new PollingThread("Inertial Device Sensor poller", this)); |
204 | 198 |
205 if (!polling_thread_->Start()) { | 199 if (!polling_thread_->Start()) { |
206 LOG(ERROR) << "Failed to start inertial sensor data polling thread"; | 200 LOG(ERROR) << "Failed to start inertial sensor data polling thread"; |
207 return false; | 201 return false; |
208 } | 202 } |
209 return true; | 203 return true; |
210 } | 204 } |
211 | 205 |
212 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { | 206 void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { |
213 NOTIMPLEMENTED(); | 207 NOTIMPLEMENTED(); |
214 } | 208 } |
215 | 209 |
216 DataFetcherSharedMemoryBase::FetcherType | 210 DataFetcherSharedMemoryBase::FetcherType |
217 DataFetcherSharedMemoryBase::GetType() const { | 211 DataFetcherSharedMemoryBase::GetType() const { |
218 return FETCHER_TYPE_DEFAULT; | 212 return FETCHER_TYPE_DEFAULT; |
219 } | 213 } |
220 | 214 |
221 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { | 215 base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { |
222 return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds); | 216 return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds); |
223 } | 217 } |
224 | 218 |
225 base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory( | 219 void* DataFetcherSharedMemoryBase::GetSharedMemoryBuffer( |
226 ConsumerType consumer_type) { | 220 ConsumerType consumer_type) { |
227 SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); | 221 auto it = shared_memory_mapping_map_.find(consumer_type); |
228 if (it != shared_memory_map_.end()) | 222 if (it != shared_memory_mapping_map_.end()) |
229 return it->second; | 223 return it->second.get(); |
230 | 224 |
231 size_t buffer_size = GetConsumerSharedMemoryBufferSize(consumer_type); | 225 size_t buffer_size = GetConsumerSharedMemoryBufferSize(consumer_type); |
232 if (buffer_size == 0) | 226 if (buffer_size == 0) |
233 return nullptr; | 227 return nullptr; |
234 | 228 |
235 std::unique_ptr<base::SharedMemory> new_shared_mem(new base::SharedMemory); | 229 mojo::SharedBuffer buffer(buffer_size); |
236 if (new_shared_mem->CreateAndMapAnonymous(buffer_size)) { | 230 mojo::ScopedSharedBufferMapping mapping = buffer.handle->Map(buffer_size); |
237 if (void* mem = new_shared_mem->memory()) { | 231 if (!mapping) |
238 memset(mem, 0, buffer_size); | 232 return nullptr; |
239 base::SharedMemory* shared_mem = new_shared_mem.release(); | 233 void* mem = mapping.get(); |
240 shared_memory_map_[consumer_type] = shared_mem; | 234 memset(mem, 0, buffer_size); |
241 return shared_mem; | 235 shared_memory_map_[consumer_type] = std::move(buffer.handle); |
242 } | 236 shared_memory_mapping_map_[consumer_type] = std::move(mapping); |
243 } | 237 return mem; |
244 LOG(ERROR) << "Failed to initialize shared memory"; | |
245 return nullptr; | |
246 } | |
247 | |
248 void* DataFetcherSharedMemoryBase::GetSharedMemoryBuffer( | |
249 ConsumerType consumer_type) { | |
250 if (base::SharedMemory* shared_memory = GetSharedMemory(consumer_type)) | |
251 return shared_memory->memory(); | |
252 return nullptr; | |
253 } | 238 } |
254 | 239 |
255 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 240 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
256 return polling_thread_ ? polling_thread_->message_loop() : nullptr; | 241 return polling_thread_ ? polling_thread_->message_loop() : nullptr; |
257 } | 242 } |
258 | 243 |
259 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { | 244 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { |
260 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; | 245 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; |
261 } | 246 } |
262 | 247 |
263 } // namespace content | 248 } // namespace content |
OLD | NEW |