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_iio.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/task_runner_util.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "device/generic_sensor/iio/platform_sensor_utils_iio.h" |
| 11 #include "device/generic_sensor/iio/sensor_data_iio.h" |
| 12 #include "device/generic_sensor/platform_sensor_iio.h" |
| 13 |
| 14 namespace device { |
| 15 |
| 16 // static |
| 17 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() { |
| 18 return base::Singleton< |
| 19 PlatformSensorProviderIio, |
| 20 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get(); |
| 21 } |
| 22 |
| 23 PlatformSensorProviderIio::PlatformSensorProviderIio() = default; |
| 24 |
| 25 PlatformSensorProviderIio::~PlatformSensorProviderIio() = default; |
| 26 |
| 27 void PlatformSensorProviderIio::CreateSensorInternal( |
| 28 mojom::SensorType type, |
| 29 mojo::ScopedSharedBufferMapping mapping, |
| 30 const CreateSensorCallback& callback) { |
| 31 SensorDataIio data; |
| 32 if (!InitSensorData(type, &data)) { |
| 33 callback.Run(nullptr); |
| 34 return; |
| 35 } |
| 36 |
| 37 if (!polling_thread_) |
| 38 polling_thread_.reset(new base::Thread("Linux/CrOS manager thread")); |
| 39 |
| 40 if (!polling_thread_->IsRunning()) { |
| 41 if (!polling_thread_->StartWithOptions( |
| 42 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { |
| 43 callback.Run(nullptr); |
| 44 return; |
| 45 } |
| 46 polling_thread_task_runner_ = polling_thread_->task_runner(); |
| 47 } |
| 48 |
| 49 base::PostTaskAndReplyWithResult( |
| 50 polling_thread_task_runner_.get(), FROM_HERE, |
| 51 base::Bind(SensorReader::Create, data), |
| 52 base::Bind(&PlatformSensorProviderIio::SensorReaderFound, |
| 53 base::Unretained(this), type, base::Passed(&mapping), callback, |
| 54 data)); |
| 55 } |
| 56 |
| 57 void PlatformSensorProviderIio::SensorReaderFound( |
| 58 mojom::SensorType type, |
| 59 mojo::ScopedSharedBufferMapping mapping, |
| 60 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
| 61 const SensorDataIio& data, |
| 62 std::unique_ptr<SensorReader> sensor_reader) { |
| 63 DCHECK(CalledOnValidThread()); |
| 64 |
| 65 if (!sensor_reader) { |
| 66 // If there are no sensors, stop polling thread. |
| 67 if (!HasSensors()) |
| 68 AllSensorsRemoved(); |
| 69 callback.Run(nullptr); |
| 70 return; |
| 71 } |
| 72 |
| 73 callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data, |
| 74 std::move(sensor_reader), |
| 75 polling_thread_task_runner_)); |
| 76 } |
| 77 |
| 78 void PlatformSensorProviderIio::SetTaskRunner( |
| 79 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 80 // This task runner mustn't belong to current IO thread. |
| 81 DCHECK(!task_runner->BelongsToCurrentThread()); |
| 82 file_task_runner_ = task_runner; |
| 83 } |
| 84 |
| 85 void PlatformSensorProviderIio::AllSensorsRemoved() { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 file_task_runner_->PostTask( |
| 88 FROM_HERE, base::Bind(&PlatformSensorProviderIio::StopPollingThread, |
| 89 base::Unretained(this))); |
| 90 } |
| 91 |
| 92 void PlatformSensorProviderIio::StopPollingThread() { |
| 93 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 94 polling_thread_->Stop(); |
| 95 } |
| 96 |
| 97 } // namespace device |
OLD | NEW |