| Index: device/sensors/platform_sensor_provider.cc
|
| diff --git a/device/sensors/platform_sensor_provider.cc b/device/sensors/platform_sensor_provider.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..33b007f57d12ef80be86de17f9f127145695f71c
|
| --- /dev/null
|
| +++ b/device/sensors/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/sensors/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(
|
| + 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.
|
| + 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);
|
| +
|
| + return new_sensor;
|
| +}
|
| +
|
| +scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor(
|
| + 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(SensorType type) {
|
| + DCHECK(CalledOnValidThread());
|
| + auto it = sensor_map_.find(type);
|
| + if (it != sensor_map_.end())
|
| + sensor_map_.erase(it);
|
| +
|
| + 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
|
|
|