| 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/message_loop/message_loop.h" | |
| 9 #include "base/task_runner_util.h" | |
| 10 #include "base/threading/thread.h" | |
| 11 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
| 12 #include "device/generic_sensor/platform_sensor_linux.h" | |
| 13 #include "device/generic_sensor/platform_sensor_reader_linux.h" | |
| 14 | |
| 15 namespace device { | |
| 16 | |
| 17 // static | |
| 18 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() { | |
| 19 return base::Singleton< | |
| 20 PlatformSensorProviderLinux, | |
| 21 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get(); | |
| 22 } | |
| 23 | |
| 24 PlatformSensorProviderLinux::PlatformSensorProviderLinux() | |
| 25 : sensor_nodes_enumerated_(false), | |
| 26 sensor_nodes_enumeration_started_(false), | |
| 27 sensor_device_manager_(nullptr) {} | |
| 28 | |
| 29 PlatformSensorProviderLinux::~PlatformSensorProviderLinux() { | |
| 30 DCHECK(!sensor_device_manager_); | |
| 31 } | |
| 32 | |
| 33 void PlatformSensorProviderLinux::CreateSensorInternal( | |
| 34 mojom::SensorType type, | |
| 35 mojo::ScopedSharedBufferMapping mapping, | |
| 36 const CreateSensorCallback& callback) { | |
| 37 if (!sensor_device_manager_) | |
| 38 sensor_device_manager_.reset(new SensorDeviceManager()); | |
| 39 | |
| 40 if (!sensor_nodes_enumerated_) { | |
| 41 if (!sensor_nodes_enumeration_started_) { | |
| 42 sensor_nodes_enumeration_started_ = file_task_runner_->PostTask( | |
| 43 FROM_HERE, | |
| 44 base::Bind(&SensorDeviceManager::Start, | |
| 45 base::Unretained(sensor_device_manager_.get()), this)); | |
| 46 } | |
| 47 return; | |
| 48 } | |
| 49 | |
| 50 SensorInfoLinux* sensor_device = GetSensorDevice(type); | |
| 51 if (!sensor_device) { | |
| 52 // If there are no sensors, stop polling thread. | |
| 53 if (!HasSensors()) | |
| 54 AllSensorsRemoved(); | |
| 55 callback.Run(nullptr); | |
| 56 return; | |
| 57 } | |
| 58 SensorDeviceFound(type, std::move(mapping), callback, sensor_device); | |
| 59 } | |
| 60 | |
| 61 void PlatformSensorProviderLinux::SensorDeviceFound( | |
| 62 mojom::SensorType type, | |
| 63 mojo::ScopedSharedBufferMapping mapping, | |
| 64 const PlatformSensorProviderBase::CreateSensorCallback& callback, | |
| 65 const SensorInfoLinux* sensor_device) { | |
| 66 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 67 DCHECK(sensor_device); | |
| 68 | |
| 69 if (!StartPollingThread()) { | |
| 70 callback.Run(nullptr); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 scoped_refptr<PlatformSensorLinux> sensor = | |
| 75 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, | |
| 76 polling_thread_->task_runner()); | |
| 77 callback.Run(sensor); | |
| 78 } | |
| 79 | |
| 80 void PlatformSensorProviderLinux::SetFileTaskRunner( | |
| 81 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { | |
| 82 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 83 if (!file_task_runner_) | |
| 84 file_task_runner_ = file_task_runner; | |
| 85 } | |
| 86 | |
| 87 void PlatformSensorProviderLinux::AllSensorsRemoved() { | |
| 88 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 89 DCHECK(file_task_runner_); | |
| 90 Shutdown(); | |
| 91 // When there are no sensors left, the polling thread must be stopped. | |
| 92 // Stop() can only be called on a different thread that allows I/O. | |
| 93 // Thus, browser's file thread is used for this purpose. | |
| 94 file_task_runner_->PostTask( | |
| 95 FROM_HERE, base::Bind(&PlatformSensorProviderLinux::StopPollingThread, | |
| 96 base::Unretained(this))); | |
| 97 } | |
| 98 | |
| 99 bool PlatformSensorProviderLinux::StartPollingThread() { | |
| 100 if (!polling_thread_) | |
| 101 polling_thread_.reset(new base::Thread("Sensor polling thread")); | |
| 102 | |
| 103 if (!polling_thread_->IsRunning()) { | |
| 104 return polling_thread_->StartWithOptions( | |
| 105 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 106 } | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 void PlatformSensorProviderLinux::StopPollingThread() { | |
| 111 DCHECK(file_task_runner_); | |
| 112 DCHECK(file_task_runner_->BelongsToCurrentThread()); | |
| 113 if (polling_thread_ && polling_thread_->IsRunning()) | |
| 114 polling_thread_->Stop(); | |
| 115 } | |
| 116 | |
| 117 void PlatformSensorProviderLinux::Shutdown() { | |
| 118 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 119 const bool did_post_task = file_task_runner_->DeleteSoon( | |
| 120 FROM_HERE, sensor_device_manager_.release()); | |
| 121 DCHECK(did_post_task); | |
| 122 sensor_nodes_enumerated_ = false; | |
| 123 sensor_nodes_enumeration_started_ = false; | |
| 124 sensor_devices_by_type_.clear(); | |
| 125 } | |
| 126 | |
| 127 SensorInfoLinux* PlatformSensorProviderLinux::GetSensorDevice( | |
| 128 mojom::SensorType type) { | |
| 129 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 130 auto sensor = sensor_devices_by_type_.find(type); | |
| 131 if (sensor == sensor_devices_by_type_.end()) | |
| 132 return nullptr; | |
| 133 return sensor->second.get(); | |
| 134 } | |
| 135 | |
| 136 void PlatformSensorProviderLinux::GetAllSensorDevices() { | |
| 137 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 138 // TODO(maksims): implement this method once we have discovery API. | |
| 139 NOTIMPLEMENTED(); | |
| 140 } | |
| 141 | |
| 142 void PlatformSensorProviderLinux::SetSensorDeviceManagerForTesting( | |
| 143 std::unique_ptr<SensorDeviceManager> sensor_device_manager) { | |
| 144 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 145 Shutdown(); | |
| 146 sensor_device_manager_ = std::move(sensor_device_manager); | |
| 147 } | |
| 148 | |
| 149 void PlatformSensorProviderLinux::SetFileTaskRunnerForTesting( | |
| 150 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 151 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 152 file_task_runner_ = std::move(task_runner); | |
| 153 } | |
| 154 | |
| 155 void PlatformSensorProviderLinux::ProcessStoredRequests() { | |
| 156 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 157 std::vector<mojom::SensorType> request_types = GetPendingRequestTypes(); | |
| 158 if (request_types.empty()) | |
| 159 return; | |
| 160 | |
| 161 for (auto const& type : request_types) { | |
| 162 SensorInfoLinux* device = nullptr; | |
| 163 auto device_entry = sensor_devices_by_type_.find(type); | |
| 164 if (device_entry != sensor_devices_by_type_.end()) | |
| 165 device = device_entry->second.get(); | |
| 166 CreateSensorAndNotify(type, device); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void PlatformSensorProviderLinux::CreateSensorAndNotify( | |
| 171 mojom::SensorType type, | |
| 172 SensorInfoLinux* sensor_device) { | |
| 173 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 174 scoped_refptr<PlatformSensorLinux> sensor; | |
| 175 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type); | |
| 176 if (sensor_device && mapping && StartPollingThread()) { | |
| 177 sensor = | |
| 178 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device, | |
| 179 polling_thread_->task_runner()); | |
| 180 } | |
| 181 NotifySensorCreated(type, sensor); | |
| 182 } | |
| 183 | |
| 184 void PlatformSensorProviderLinux::OnSensorNodesEnumerated() { | |
| 185 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 186 DCHECK(!sensor_nodes_enumerated_); | |
| 187 sensor_nodes_enumerated_ = true; | |
| 188 ProcessStoredRequests(); | |
| 189 } | |
| 190 | |
| 191 void PlatformSensorProviderLinux::OnDeviceAdded( | |
| 192 mojom::SensorType type, | |
| 193 std::unique_ptr<SensorInfoLinux> sensor_device) { | |
| 194 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 195 // At the moment, we support only one device per type. | |
| 196 if (base::ContainsKey(sensor_devices_by_type_, type)) { | |
| 197 DVLOG(1) << "Sensor ignored. Type " << type | |
| 198 << ". Node: " << sensor_device->device_node; | |
| 199 return; | |
| 200 } | |
| 201 sensor_devices_by_type_[type] = std::move(sensor_device); | |
| 202 } | |
| 203 | |
| 204 void PlatformSensorProviderLinux::OnDeviceRemoved( | |
| 205 mojom::SensorType type, | |
| 206 const std::string& device_node) { | |
| 207 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 208 auto it = sensor_devices_by_type_.find(type); | |
| 209 if (it != sensor_devices_by_type_.end() && | |
| 210 it->second->device_node == device_node) | |
| 211 sensor_devices_by_type_.erase(it); | |
| 212 } | |
| 213 | |
| 214 } // namespace device | |
| OLD | NEW |