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.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/task_runner_util.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "device/generic_sensor/platform_sensor_ambient_light_iio.h" |
| 12 |
| 13 namespace base { |
| 14 class SingleThreadTaskRunner; |
| 15 } |
| 16 |
| 17 namespace device { |
| 18 |
| 19 namespace { |
| 20 |
| 21 using mojom::SensorType; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class PlatformSensorProviderIio : public PlatformSensorProvider { |
| 26 public: |
| 27 PlatformSensorProviderIio(); |
| 28 ~PlatformSensorProviderIio() override; |
| 29 |
| 30 static PlatformSensorProviderIio* GetInstance(); |
| 31 |
| 32 protected: |
| 33 void CreateSensorInternal(mojom::SensorType type, |
| 34 mojo::ScopedSharedBufferMapping mapping, |
| 35 const CreateSensorCallback& callback) override; |
| 36 |
| 37 private: |
| 38 scoped_refptr<PlatformSensor> DoCreateSensor( |
| 39 mojom::SensorType type, |
| 40 mojo::ScopedSharedBufferMapping mapping, |
| 41 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); |
| 42 |
| 43 std::unique_ptr<base::Thread> polling_thread_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio); |
| 46 }; |
| 47 |
| 48 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { |
| 49 return PlatformSensorProviderIio::GetInstance(); |
| 50 } |
| 51 |
| 52 // static |
| 53 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() { |
| 54 return base::Singleton< |
| 55 PlatformSensorProviderIio, |
| 56 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get(); |
| 57 } |
| 58 |
| 59 PlatformSensorProviderIio::PlatformSensorProviderIio() = default; |
| 60 |
| 61 PlatformSensorProviderIio::~PlatformSensorProviderIio() { |
| 62 if (polling_thread_) |
| 63 polling_thread_->Stop(); |
| 64 } |
| 65 |
| 66 void PlatformSensorProviderIio::CreateSensorInternal( |
| 67 mojom::SensorType type, |
| 68 mojo::ScopedSharedBufferMapping mapping, |
| 69 const CreateSensorCallback& callback) { |
| 70 if (!polling_thread_) { |
| 71 polling_thread_.reset( |
| 72 new base::Thread("Sensors polling thread CrOS/Linux")); |
| 73 if (!polling_thread_->StartWithOptions( |
| 74 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { |
| 75 callback.Run(nullptr); |
| 76 return; |
| 77 } |
| 78 } |
| 79 |
| 80 base::PostTaskAndReplyWithResult( |
| 81 polling_thread_->message_loop()->task_runner().get(), FROM_HERE, |
| 82 base::Bind(&PlatformSensorProviderIio::DoCreateSensor, |
| 83 base::Unretained(this), type, base::Passed(&mapping), |
| 84 base::ThreadTaskRunnerHandle::Get()), |
| 85 callback); |
| 86 } |
| 87 |
| 88 scoped_refptr<PlatformSensor> PlatformSensorProviderIio::DoCreateSensor( |
| 89 mojom::SensorType type, |
| 90 mojo::ScopedSharedBufferMapping mapping, |
| 91 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) { |
| 92 scoped_refptr<PlatformSensor> sensor = nullptr; |
| 93 switch (type) { |
| 94 case SensorType::AMBIENT_LIGHT: |
| 95 sensor = PlatformSensorAmbientLightIio::CreateSensor( |
| 96 type, std::move(mapping), this, ipc_task_runner); |
| 97 break; |
| 98 default: |
| 99 NOTIMPLEMENTED(); |
| 100 break; |
| 101 } |
| 102 return sensor; |
| 103 } |
| 104 |
| 105 } // namespace device |
OLD | NEW |