OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/sensors/platform_sensor_provider.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 namespace device { |
| 10 |
| 11 // static |
| 12 PlatformSensorProvider* PlatformSensorProvider::Create( |
| 13 uint64_t shared_buffer_size) { |
| 14 // TODO(shalamov): Add platform-specific PlatformSensorProvider |
| 15 // implementations here. |
| 16 return nullptr; |
| 17 } |
| 18 |
| 19 PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size) |
| 20 : shared_buffer_size_(shared_buffer_size) {} |
| 21 |
| 22 PlatformSensorProvider::~PlatformSensorProvider() = default; |
| 23 |
| 24 scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( |
| 25 SensorType type, |
| 26 uint64_t size, |
| 27 uint64_t offset) { |
| 28 DCHECK(CalledOnValidThread()); |
| 29 |
| 30 if (!CreateSharedBufferIfNeeded()) |
| 31 return nullptr; |
| 32 |
| 33 mojo::ScopedSharedBufferMapping mapping = |
| 34 shared_buffer_handle_->MapAtOffset(size, offset); |
| 35 if (!mapping) |
| 36 return nullptr; |
| 37 |
| 38 // Create new platform sensor instance. |
| 39 scoped_refptr<PlatformSensor> new_sensor = |
| 40 CreateSensor(type, std::move(mapping), size); |
| 41 if (!new_sensor) |
| 42 return nullptr; |
| 43 |
| 44 const auto& add_result = |
| 45 sensor_map_.insert(std::make_pair(type, new_sensor.get())); |
| 46 DCHECK(add_result.second); |
| 47 |
| 48 return new_sensor; |
| 49 } |
| 50 |
| 51 scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( |
| 52 SensorType type) { |
| 53 DCHECK(CalledOnValidThread()); |
| 54 |
| 55 auto it = sensor_map_.find(type); |
| 56 if (it != sensor_map_.end()) |
| 57 return it->second; |
| 58 return nullptr; |
| 59 } |
| 60 |
| 61 bool PlatformSensorProvider::CreateSharedBufferIfNeeded() { |
| 62 DCHECK(CalledOnValidThread()); |
| 63 if (shared_buffer_handle_.is_valid()) |
| 64 return true; |
| 65 |
| 66 shared_buffer_handle_ = mojo::SharedBufferHandle::Create(shared_buffer_size_); |
| 67 return shared_buffer_handle_.is_valid(); |
| 68 } |
| 69 |
| 70 void PlatformSensorProvider::RemoveSensor(SensorType type) { |
| 71 DCHECK(CalledOnValidThread()); |
| 72 auto it = sensor_map_.find(type); |
| 73 if (it != sensor_map_.end()) |
| 74 sensor_map_.erase(it); |
| 75 |
| 76 if (sensor_map_.empty() && shared_buffer_handle_->is_valid()) |
| 77 shared_buffer_handle_.reset(); |
| 78 } |
| 79 |
| 80 mojo::ScopedSharedBufferHandle |
| 81 PlatformSensorProvider::GetClonedSharedBufferHandle() { |
| 82 DCHECK(CalledOnValidThread()); |
| 83 CreateSharedBufferIfNeeded(); |
| 84 return shared_buffer_handle_->Clone(); |
| 85 } |
| 86 |
| 87 } // namespace device |
OLD | NEW |