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_linux.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/linux/platform_sensor_utils_linux.h" |
| 11 #include "device/generic_sensor/linux/sensor_data_linux.h" |
| 12 #include "device/generic_sensor/platform_sensor_linux.h" |
| 13 |
| 14 namespace device { |
| 15 |
| 16 // static |
| 17 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() { |
| 18 return base::Singleton< |
| 19 PlatformSensorProviderLinux, |
| 20 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get(); |
| 21 } |
| 22 |
| 23 PlatformSensorProviderLinux::PlatformSensorProviderLinux() = default; |
| 24 |
| 25 PlatformSensorProviderLinux::~PlatformSensorProviderLinux() = default; |
| 26 |
| 27 void PlatformSensorProviderLinux::CreateSensorInternal( |
| 28 mojom::SensorType type, |
| 29 mojo::ScopedSharedBufferMapping mapping, |
| 30 const CreateSensorCallback& callback) { |
| 31 SensorDataLinux 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("Sensor polling 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(&PlatformSensorProviderLinux::SensorReaderFound, |
| 53 base::Unretained(this), type, base::Passed(&mapping), callback, |
| 54 data)); |
| 55 } |
| 56 |
| 57 void PlatformSensorProviderLinux::SensorReaderFound( |
| 58 mojom::SensorType type, |
| 59 mojo::ScopedSharedBufferMapping mapping, |
| 60 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
| 61 const SensorDataLinux& 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 PlatformSensorLinux(type, std::move(mapping), this, data, |
| 74 std::move(sensor_reader), |
| 75 polling_thread_task_runner_)); |
| 76 } |
| 77 |
| 78 void PlatformSensorProviderLinux::SetFileTaskRunner( |
| 79 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
| 80 DCHECK(CalledOnValidThread()); |
| 81 if (!file_task_runner_) |
| 82 file_task_runner_ = file_task_runner; |
| 83 } |
| 84 |
| 85 void PlatformSensorProviderLinux::AllSensorsRemoved() { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 DCHECK(file_task_runner_); |
| 88 // When there are no sensors left, the polling thread must be stopped. |
| 89 // Stop() can only be called on a different thread that allows io. |
| 90 // Thus, browser's file thread is used for this purpose. |
| 91 file_task_runner_->PostTask( |
| 92 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, |
| 93 base::Unretained(this))); |
| 94 } |
| 95 |
| 96 void PlatformSensorProviderLinux::StopPollingThread() { |
| 97 DCHECK(file_task_runner_); |
| 98 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 99 polling_thread_->Stop(); |
| 100 } |
| 101 |
| 102 } // namespace device |
OLD | NEW |