| Index: device/generic_sensor/platform_sensor_provider_iio.cc
|
| diff --git a/device/generic_sensor/platform_sensor_provider_iio.cc b/device/generic_sensor/platform_sensor_provider_iio.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a7ca80bdedb7df0f26fe200a6240cbdead3059ed
|
| --- /dev/null
|
| +++ b/device/generic_sensor/platform_sensor_provider_iio.cc
|
| @@ -0,0 +1,105 @@
|
| +// 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 "base/memory/singleton.h"
|
| +#include "base/task_runner_util.h"
|
| +#include "base/threading/thread.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| +#include "device/generic_sensor/platform_sensor_ambient_light_iio.h"
|
| +
|
| +namespace base {
|
| +class SingleThreadTaskRunner;
|
| +}
|
| +
|
| +namespace device {
|
| +
|
| +namespace {
|
| +
|
| +using mojom::SensorType;
|
| +
|
| +} // namespace
|
| +
|
| +class PlatformSensorProviderIio : public PlatformSensorProvider {
|
| + public:
|
| + PlatformSensorProviderIio();
|
| + ~PlatformSensorProviderIio() override;
|
| +
|
| + static PlatformSensorProviderIio* GetInstance();
|
| +
|
| + protected:
|
| + void CreateSensorInternal(mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const CreateSensorCallback& callback) override;
|
| +
|
| + private:
|
| + scoped_refptr<PlatformSensor> DoCreateSensor(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner);
|
| +
|
| + std::unique_ptr<base::Thread> polling_thread_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio);
|
| +};
|
| +
|
| +PlatformSensorProvider* PlatformSensorProvider::GetInstance() {
|
| + return PlatformSensorProviderIio::GetInstance();
|
| +}
|
| +
|
| +// static
|
| +PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() {
|
| + return base::Singleton<
|
| + PlatformSensorProviderIio,
|
| + base::LeakySingletonTraits<PlatformSensorProviderIio>>::get();
|
| +}
|
| +
|
| +PlatformSensorProviderIio::PlatformSensorProviderIio() = default;
|
| +
|
| +PlatformSensorProviderIio::~PlatformSensorProviderIio() {
|
| + if (polling_thread_)
|
| + polling_thread_->Stop();
|
| +}
|
| +
|
| +void PlatformSensorProviderIio::CreateSensorInternal(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + const CreateSensorCallback& callback) {
|
| + if (!polling_thread_) {
|
| + polling_thread_.reset(
|
| + new base::Thread("Sensors polling thread CrOS/Linux"));
|
| + if (!polling_thread_->StartWithOptions(
|
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
| + }
|
| +
|
| + base::PostTaskAndReplyWithResult(
|
| + polling_thread_->message_loop()->task_runner().get(), FROM_HERE,
|
| + base::Bind(&PlatformSensorProviderIio::DoCreateSensor,
|
| + base::Unretained(this), type, base::Passed(&mapping),
|
| + base::ThreadTaskRunnerHandle::Get()),
|
| + callback);
|
| +}
|
| +
|
| +scoped_refptr<PlatformSensor> PlatformSensorProviderIio::DoCreateSensor(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) {
|
| + scoped_refptr<PlatformSensor> sensor = nullptr;
|
| + switch (type) {
|
| + case SensorType::AMBIENT_LIGHT:
|
| + sensor = PlatformSensorAmbientLightIio::CreateSensor(
|
| + type, std::move(mapping), this, ipc_task_runner);
|
| + break;
|
| + default:
|
| + NOTIMPLEMENTED();
|
| + break;
|
| + }
|
| + return sensor;
|
| +}
|
| +
|
| +} // namespace device
|
|
|