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..491bd7c242cd4d378066b9a565af6fabcb16964d |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_provider.cc |
| @@ -0,0 +1,95 @@ |
| +// 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> |
| + |
| +#include "base/stl_util.h" |
| +#include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
| + |
| +namespace device { |
| + |
| +namespace { |
| + |
| +const uint64_t kSharedBufferSizeInBytes = |
| + mojom::SensorReadBuffer::kReadBufferSize * |
| + static_cast<uint64_t>(mojom::SensorType::LAST); |
| + |
| +} // namespace |
| + |
| +PlatformSensorProvider* PlatformSensorProvider::s_instance_ = nullptr; |
| + |
| +// static |
| +PlatformSensorProvider* PlatformSensorProvider::GetInstance() { |
| + // TODO(shalamov): Lazily initialize s_instance_ with platform-specific |
| + // PlatformSensorProvider implementations here. |
| + return s_instance_; |
|
timvolodine
2016/08/19 16:02:30
we usually use return base::Singleton<.... instead
Mikhail
2016/08/19 19:10:02
I was considering using of Singleton, but here I b
timvolodine
2016/08/22 18:42:46
There is no need for #ifdefs when you follow the s
Mikhail
2016/08/22 19:16:15
Thanks for the links! so the solution could be to
timvolodine
2016/08/22 19:42:25
yes please, thanks ;)
|
| +} |
| + |
| +PlatformSensorProvider::PlatformSensorProvider() = default; |
| +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; |
| + |
| + scoped_refptr<PlatformSensor> new_sensor = |
| + CreateSensorInternal(type, std::move(mapping), size); |
| + if (!new_sensor) |
| + return nullptr; |
| + |
| + DCHECK(!ContainsKey(sensor_map_, type)); |
| + sensor_map_[type] = new_sensor.get(); |
| + |
| + 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(kSharedBufferSizeInBytes); |
| + return shared_buffer_handle_.is_valid(); |
| +} |
| + |
| +void PlatformSensorProvider::RemoveSensor(mojom::SensorType type) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(ContainsKey(sensor_map_, type)); |
| + sensor_map_.erase(type); |
| + |
| + if (sensor_map_.empty()) |
| + shared_buffer_handle_.reset(); |
| +} |
| + |
| +mojo::ScopedSharedBufferHandle |
| +PlatformSensorProvider::CloneSharedBufferHandle() { |
| + DCHECK(CalledOnValidThread()); |
| + CreateSharedBufferIfNeeded(); |
| + return shared_buffer_handle_->Clone(); |
| +} |
| + |
| +} // namespace device |