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/generic_sensor/platform_sensor_provider_base.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/stl_util.h" |
| 10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const uint64_t kSharedBufferSizeInBytes = |
| 17 mojom::SensorReadBuffer::kReadBufferSize * |
| 18 static_cast<uint64_t>(mojom::SensorType::LAST); |
| 19 |
| 20 } // namespace |
| 21 |
| 22 PlatformSensorProviderBase::PlatformSensorProviderBase() = default; |
| 23 PlatformSensorProviderBase::~PlatformSensorProviderBase() = default; |
| 24 |
| 25 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::CreateSensor( |
| 26 mojom::SensorType type, |
| 27 uint64_t size, |
| 28 uint64_t offset) { |
| 29 DCHECK(CalledOnValidThread()); |
| 30 |
| 31 if (!CreateSharedBufferIfNeeded()) |
| 32 return nullptr; |
| 33 |
| 34 mojo::ScopedSharedBufferMapping mapping = |
| 35 shared_buffer_handle_->MapAtOffset(size, offset); |
| 36 if (!mapping) |
| 37 return nullptr; |
| 38 |
| 39 scoped_refptr<PlatformSensor> new_sensor = |
| 40 CreateSensorInternal(type, std::move(mapping), size); |
| 41 if (!new_sensor) |
| 42 return nullptr; |
| 43 |
| 44 DCHECK(!ContainsKey(sensor_map_, type)); |
| 45 sensor_map_[type] = new_sensor.get(); |
| 46 |
| 47 return new_sensor; |
| 48 } |
| 49 |
| 50 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( |
| 51 mojom::SensorType type) { |
| 52 DCHECK(CalledOnValidThread()); |
| 53 |
| 54 auto it = sensor_map_.find(type); |
| 55 if (it != sensor_map_.end()) |
| 56 return it->second; |
| 57 return nullptr; |
| 58 } |
| 59 |
| 60 bool PlatformSensorProviderBase::CreateSharedBufferIfNeeded() { |
| 61 DCHECK(CalledOnValidThread()); |
| 62 if (shared_buffer_handle_.is_valid()) |
| 63 return true; |
| 64 |
| 65 shared_buffer_handle_ = |
| 66 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); |
| 67 return shared_buffer_handle_.is_valid(); |
| 68 } |
| 69 |
| 70 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) { |
| 71 DCHECK(CalledOnValidThread()); |
| 72 DCHECK(ContainsKey(sensor_map_, type)); |
| 73 sensor_map_.erase(type); |
| 74 |
| 75 if (sensor_map_.empty()) |
| 76 shared_buffer_handle_.reset(); |
| 77 } |
| 78 |
| 79 mojo::ScopedSharedBufferHandle |
| 80 PlatformSensorProviderBase::CloneSharedBufferHandle() { |
| 81 DCHECK(CalledOnValidThread()); |
| 82 CreateSharedBufferIfNeeded(); |
| 83 return shared_buffer_handle_->Clone(); |
| 84 } |
| 85 |
| 86 } // namespace device |
OLD | NEW |