Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_provider.cc |
| diff --git a/device/generic_sensor/platform_sensor_provider.cc b/device/generic_sensor/platform_sensor_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ae5f67d764651e5132ad4a648d7b34d08876b6b8 |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_provider.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/generic_sensor/platform_sensor_provider.h" |
| + |
| +#include <utility> |
| + |
| +namespace device { |
| + |
| +// static |
| +PlatformSensorProvider* PlatformSensorProvider::Create( |
| + uint64_t shared_buffer_size) { |
| + // TODO(shalamov): Add platform-specific PlatformSensorProvider |
| + // implementations here. |
| + return nullptr; |
| +} |
| + |
| +PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size) |
| + : shared_buffer_size_(shared_buffer_size) {} |
| + |
| +PlatformSensorProvider::~PlatformSensorProvider() = default; |
| + |
| +scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( |
| + mojom::SensorType type, |
| + uint64_t size, |
| + uint64_t offset) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + if (!CreateSharedBufferIfNeeded()) |
| + return nullptr; |
| + |
| + mojo::ScopedSharedBufferMapping mapping = |
| + shared_buffer_handle_->MapAtOffset(size, offset); |
| + if (!mapping) |
| + return nullptr; |
| + |
| + // Create new platform sensor instance. |
|
Reilly Grant (use Gerrit)
2016/08/11 20:59:37
This comment is unnecessary.
Mikhail
2016/08/12 10:21:48
Done.
|
| + scoped_refptr<PlatformSensor> new_sensor = |
| + CreateSensor(type, std::move(mapping), size); |
| + if (!new_sensor) |
| + return nullptr; |
| + |
| + const auto& add_result = |
| + sensor_map_.insert(std::make_pair(type, new_sensor.get())); |
| + DCHECK(add_result.second); |
|
Reilly Grant (use Gerrit)
2016/08/11 20:59:37
It's a little shorter/clearer to write:
DCHECK(!C
Mikhail
2016/08/12 10:21:48
Done.
|
| + |
| + return new_sensor; |
| +} |
| + |
| +scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( |
| + mojom::SensorType type) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + auto it = sensor_map_.find(type); |
| + if (it != sensor_map_.end()) |
| + return it->second; |
| + return nullptr; |
| +} |
| + |
| +bool PlatformSensorProvider::CreateSharedBufferIfNeeded() { |
| + DCHECK(CalledOnValidThread()); |
| + if (shared_buffer_handle_.is_valid()) |
| + return true; |
| + |
| + shared_buffer_handle_ = mojo::SharedBufferHandle::Create(shared_buffer_size_); |
| + return shared_buffer_handle_.is_valid(); |
| +} |
| + |
| +void PlatformSensorProvider::RemoveSensor(mojom::SensorType type) { |
| + DCHECK(CalledOnValidThread()); |
| + auto it = sensor_map_.find(type); |
| + if (it != sensor_map_.end()) |
| + sensor_map_.erase(it); |
|
Reilly Grant (use Gerrit)
2016/08/11 20:59:37
sensor_map_.erase(type) is simpler.
Mikhail
2016/08/12 10:21:48
Done.
|
| + |
| + if (sensor_map_.empty() && shared_buffer_handle_->is_valid()) |
| + shared_buffer_handle_.reset(); |
| +} |
| + |
| +mojo::ScopedSharedBufferHandle |
| +PlatformSensorProvider::GetClonedSharedBufferHandle() { |
| + DCHECK(CalledOnValidThread()); |
| + CreateSharedBufferIfNeeded(); |
| + return shared_buffer_handle_->Clone(); |
| +} |
| + |
| +} // namespace device |