Chromium Code Reviews| 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 "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 base { | |
| 15 class SingleThreadTaskRunner; | |
| 16 } | |
| 17 | |
| 18 namespace device { | |
| 19 | |
| 20 class PlatformSensorProviderIio : public PlatformSensorProvider { | |
| 21 public: | |
| 22 static PlatformSensorProviderIio* GetInstance(); | |
| 23 | |
| 24 protected: | |
| 25 void CreateSensorInternal(mojom::SensorType type, | |
| 26 mojo::ScopedSharedBufferMapping mapping, | |
| 27 const CreateSensorCallback& callback) override; | |
| 28 | |
| 29 private: | |
| 30 friend struct base::DefaultSingletonTraits<PlatformSensorProviderIio>; | |
| 31 PlatformSensorProviderIio(); | |
| 32 ~PlatformSensorProviderIio() override; | |
| 33 | |
| 34 void SensorReaderFound( | |
| 35 mojom::SensorType type, | |
| 36 mojo::ScopedSharedBufferMapping mapping, | |
| 37 const PlatformSensorProviderBase::CreateSensorCallback& callback, | |
| 38 const SensorDataIio& data, | |
| 39 std::unique_ptr<base::Thread> polling_thread, | |
| 40 std::unique_ptr<SensorReader> sensor_reader); | |
| 41 | |
| 42 // Helper function that gets a new sensor reader with a sensor type specific | |
| 43 // options taken from SensorDataIio. | |
| 44 std::unique_ptr<SensorReader> GetSensorReader( | |
| 45 const SensorDataIio& data, | |
| 46 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 47 | |
| 48 // TODO(maksims): make this as a separate class Manager that will | |
| 49 // create threads for new sensors, check sensors existence and notify provider | |
| 50 // if a new sensor has appeared and it can be created if a request comes | |
| 51 // again for the same sensor. | |
| 52 // A use case example: a request for a sensor X comes, manager checks if the | |
| 53 // sensor exists on a platform and notifies a provider it is not found. | |
| 54 // The provider stores this information into its cache and doesn't try to | |
| 55 // create this specific sensor if a request comes. But when, for example, | |
| 56 // the sensor X is plugged into a usb port, the manager notices that and | |
| 57 // notifies the provider, which updates its cache and starts handling requests | |
| 58 // for the sensor X. | |
| 59 // | |
| 60 // Right now, it is only used to delete polling threads if a new sensor | |
| 61 // is not created and the thread cannot be passed to it. | |
| 62 std::unique_ptr<base::Thread> manager_thread_; | |
|
Reilly Grant (use Gerrit)
2016/10/26 21:53:42
Can you explain why a separate thread for this is
maksims (do not use this acc)
2016/10/27 05:29:19
Yes, sure.
First of all, BrowserThread is located
| |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderIio); | |
| 65 }; | |
| 66 | |
| 67 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | |
| 68 return PlatformSensorProviderIio::GetInstance(); | |
| 69 } | |
| 70 | |
| 71 // static | |
| 72 PlatformSensorProviderIio* PlatformSensorProviderIio::GetInstance() { | |
| 73 return base::Singleton< | |
| 74 PlatformSensorProviderIio, | |
| 75 base::LeakySingletonTraits<PlatformSensorProviderIio>>::get(); | |
| 76 } | |
| 77 | |
| 78 PlatformSensorProviderIio::PlatformSensorProviderIio() = default; | |
| 79 | |
| 80 PlatformSensorProviderIio::~PlatformSensorProviderIio() = default; | |
| 81 | |
| 82 void PlatformSensorProviderIio::CreateSensorInternal( | |
| 83 mojom::SensorType type, | |
| 84 mojo::ScopedSharedBufferMapping mapping, | |
| 85 const CreateSensorCallback& callback) { | |
| 86 SensorDataIio data; | |
| 87 if (!InitSensorData(type, &data)) { | |
| 88 callback.Run(nullptr); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 if (!manager_thread_) { | |
| 93 manager_thread_.reset(new base::Thread("Linux/CrOS manager thread")); | |
| 94 if (!manager_thread_->StartWithOptions( | |
| 95 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { | |
| 96 callback.Run(nullptr); | |
| 97 return; | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 std::unique_ptr<base::Thread> polling_thread( | |
| 102 new base::Thread("Linux/CrOS polling sensor thread.")); | |
| 103 | |
| 104 if (!polling_thread->StartWithOptions( | |
| 105 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) { | |
| 106 callback.Run(nullptr); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 base::PostTaskAndReplyWithResult( | |
| 111 polling_thread->task_runner().get(), FROM_HERE, | |
| 112 base::Bind(SensorReader::Create, data), | |
| 113 base::Bind(&PlatformSensorProviderIio::SensorReaderFound, | |
| 114 base::Unretained(this), type, base::Passed(&mapping), callback, | |
| 115 data, base::Passed(&polling_thread))); | |
| 116 } | |
| 117 | |
| 118 void PlatformSensorProviderIio::SensorReaderFound( | |
| 119 mojom::SensorType type, | |
| 120 mojo::ScopedSharedBufferMapping mapping, | |
| 121 const PlatformSensorProviderBase::CreateSensorCallback& callback, | |
| 122 const SensorDataIio& data, | |
| 123 std::unique_ptr<base::Thread> polling_thread, | |
| 124 std::unique_ptr<SensorReader> sensor_reader) { | |
| 125 DCHECK(CalledOnValidThread()); | |
| 126 | |
| 127 if (!sensor_reader) { | |
| 128 manager_thread_->task_runner()->DeleteSoon(FROM_HERE, | |
| 129 polling_thread.release()); | |
| 130 callback.Run(nullptr); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 callback.Run(new PlatformSensorIio(type, std::move(mapping), this, data, | |
| 135 std::move(polling_thread), | |
| 136 std::move(sensor_reader))); | |
| 137 } | |
| 138 | |
| 139 } // namespace device | |
| OLD | NEW |