Chromium Code Reviews| 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.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/stl_util.h" | |
| 10 | |
| 11 namespace device { | |
| 12 | |
| 13 // static | |
| 14 PlatformSensorProvider* PlatformSensorProvider::Create( | |
| 15 uint64_t shared_buffer_size) { | |
| 16 // TODO(shalamov): Add platform-specific PlatformSensorProvider | |
| 17 // implementations here. | |
|
timvolodine
2016/08/18 22:52:14
NOTREACHED()?
Mikhail
2016/08/19 09:29:55
'nullptr' just means that there is no available im
| |
| 18 return nullptr; | |
| 19 } | |
| 20 | |
| 21 PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size) | |
| 22 : shared_buffer_size_(shared_buffer_size) {} | |
| 23 | |
| 24 PlatformSensorProvider::~PlatformSensorProvider() = default; | |
| 25 | |
| 26 scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( | |
| 27 mojom::SensorType type, | |
| 28 uint64_t size, | |
| 29 uint64_t offset) { | |
| 30 DCHECK(CalledOnValidThread()); | |
| 31 | |
| 32 if (!CreateSharedBufferIfNeeded()) | |
| 33 return nullptr; | |
| 34 | |
| 35 mojo::ScopedSharedBufferMapping mapping = | |
| 36 shared_buffer_handle_->MapAtOffset(size, offset); | |
| 37 if (!mapping) | |
| 38 return nullptr; | |
| 39 | |
| 40 scoped_refptr<PlatformSensor> new_sensor = | |
| 41 CreateSensorInternal(type, std::move(mapping), size); | |
| 42 if (!new_sensor) | |
| 43 return nullptr; | |
| 44 | |
| 45 DCHECK(!ContainsKey(sensor_map_, type)); | |
| 46 sensor_map_[type] = new_sensor.get(); | |
| 47 | |
| 48 return new_sensor; | |
| 49 } | |
| 50 | |
| 51 scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( | |
| 52 mojom::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(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 PlatformSensorProvider::CloneSharedBufferHandle() { | |
| 81 DCHECK(CalledOnValidThread()); | |
| 82 CreateSharedBufferIfNeeded(); | |
| 83 return shared_buffer_handle_->Clone(); | |
| 84 } | |
| 85 | |
| 86 } // namespace device | |
| OLD | NEW |