| 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/message_loop/message_loop.h" |
| 9 #include "base/task_runner_util.h" |
| 10 #include "base/threading/thread.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 uint64_t buffer_size, |
| 36 const CreateSensorCallback& callback) override; |
| 37 |
| 38 private: |
| 39 scoped_refptr<PlatformSensor> DoCreateSensor( |
| 40 mojom::SensorType type, |
| 41 mojo::ScopedSharedBufferMapping mapping, |
| 42 uint64_t buffer_size, |
| 43 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); |
| 44 |
| 45 std::unique_ptr<base::Thread> polling_thread_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio); |
| 48 }; |
| 49 |
| 50 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { |
| 51 return PlatformSensorProviderIio::GetInstance(); |
| 52 } |
| 53 |
| 54 // static |
| 55 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() { |
| 56 return base::Singleton< |
| 57 PlatformSensorProviderIio, |
| 58 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get(); |
| 59 } |
| 60 |
| 61 PlatformSensorProviderIio::PlatformSensorProviderIio() = default; |
| 62 |
| 63 PlatformSensorProviderIio::~PlatformSensorProviderIio() { |
| 64 if (polling_thread_) |
| 65 polling_thread_->Stop(); |
| 66 } |
| 67 |
| 68 void PlatformSensorProviderIio::CreateSensorInternal( |
| 69 mojom::SensorType type, |
| 70 mojo::ScopedSharedBufferMapping mapping, |
| 71 uint64_t buffer_size, |
| 72 const CreateSensorCallback& callback) { |
| 73 if (!polling_thread_) { |
| 74 polling_thread_.reset( |
| 75 new base::Thread("Sensors polling thread CrOS/Linux")); |
| 76 if (!polling_thread_->StartWithOptions( |
| 77 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { |
| 78 callback.Run(nullptr); |
| 79 return; |
| 80 } |
| 81 } |
| 82 |
| 83 base::PostTaskAndReplyWithResult( |
| 84 polling_thread_->message_loop()->task_runner().get(), FROM_HERE, |
| 85 base::Bind(&PlatformSensorProviderIio::DoCreateSensor, |
| 86 base::Unretained(this), type, base::Passed(&mapping), |
| 87 buffer_size, base::MessageLoop::current()->task_runner()), |
| 88 callback); |
| 89 } |
| 90 |
| 91 scoped_refptr<PlatformSensor> PlatformSensorProviderIio::DoCreateSensor( |
| 92 mojom::SensorType type, |
| 93 mojo::ScopedSharedBufferMapping mapping, |
| 94 uint64_t buffer_size, |
| 95 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) { |
| 96 scoped_refptr<PlatformSensor> sensor = nullptr; |
| 97 switch (type) { |
| 98 case SensorType::AMBIENT_LIGHT: |
| 99 sensor = PlatformSensorAmbientLightIio::CreateSensor( |
| 100 type, std::move(mapping), buffer_size, this, ipc_task_runner); |
| 101 break; |
| 102 default: |
| 103 NOTIMPLEMENTED(); |
| 104 break; |
| 105 } |
| 106 return sensor; |
| 107 } |
| 108 |
| 109 } // namespace device |
| OLD | NEW |