| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/usb/usb_service_win.h" | 5 #include "device/usb/usb_service_win.h" |
| 6 | 6 |
| 7 #include <setupapi.h> | 7 #include <setupapi.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <usbiodef.h> | 9 #include <usbiodef.h> |
| 10 | 10 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 USB_PLOG(ERROR) << "SetupDiEnumDeviceInterfaces"; | 163 USB_PLOG(ERROR) << "SetupDiEnumDeviceInterfaces"; |
| 164 return false; | 164 return false; |
| 165 } | 165 } |
| 166 | 166 |
| 167 return GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data, | 167 return GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data, |
| 168 device_path, nullptr, nullptr, nullptr); | 168 device_path, nullptr, nullptr, nullptr); |
| 169 } | 169 } |
| 170 | 170 |
| 171 } // namespace | 171 } // namespace |
| 172 | 172 |
| 173 class UsbServiceWin::BlockingThreadHelper { | 173 class UsbServiceWin::BlockingTaskHelper { |
| 174 public: | 174 public: |
| 175 explicit BlockingThreadHelper(base::WeakPtr<UsbServiceWin> service) | 175 explicit BlockingTaskHelper(base::WeakPtr<UsbServiceWin> service) |
| 176 : service_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 176 : service_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 177 service_(service) {} | 177 service_(service) {} |
| 178 ~BlockingThreadHelper() {} | 178 ~BlockingTaskHelper() {} |
| 179 | 179 |
| 180 void EnumerateDevices() { | 180 void EnumerateDevices() { |
| 181 ScopedDevInfo dev_info( | 181 ScopedDevInfo dev_info( |
| 182 SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, nullptr, 0, | 182 SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, nullptr, 0, |
| 183 DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); | 183 DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); |
| 184 if (!dev_info.is_valid()) { | 184 if (!dev_info.is_valid()) { |
| 185 USB_PLOG(ERROR) << "Failed to set up device enumeration"; | 185 USB_PLOG(ERROR) << "Failed to set up device enumeration"; |
| 186 service_task_runner_->PostTask( | 186 service_task_runner_->PostTask( |
| 187 FROM_HERE, base::Bind(&UsbServiceWin::HelperStarted, service_)); | 187 FROM_HERE, base::Bind(&UsbServiceWin::HelperStarted, service_)); |
| 188 return; | 188 return; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 269 |
| 270 private: | 270 private: |
| 271 std::unordered_map<std::string, std::string> hub_paths_; | 271 std::unordered_map<std::string, std::string> hub_paths_; |
| 272 | 272 |
| 273 // Calls back to |service_| must be posted to |service_task_runner_|, which | 273 // Calls back to |service_| must be posted to |service_task_runner_|, which |
| 274 // runs tasks on the thread where that object lives. | 274 // runs tasks on the thread where that object lives. |
| 275 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner_; | 275 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner_; |
| 276 base::WeakPtr<UsbServiceWin> service_; | 276 base::WeakPtr<UsbServiceWin> service_; |
| 277 }; | 277 }; |
| 278 | 278 |
| 279 UsbServiceWin::UsbServiceWin( | 279 UsbServiceWin::UsbServiceWin() |
| 280 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 280 : UsbService(CreateBlockingTaskRunner()), |
| 281 : UsbService(blocking_task_runner), | |
| 282 device_observer_(this), | 281 device_observer_(this), |
| 283 weak_factory_(this) { | 282 weak_factory_(this) { |
| 284 DeviceMonitorWin* device_monitor = | 283 DeviceMonitorWin* device_monitor = |
| 285 DeviceMonitorWin::GetForDeviceInterface(GUID_DEVINTERFACE_USB_DEVICE); | 284 DeviceMonitorWin::GetForDeviceInterface(GUID_DEVINTERFACE_USB_DEVICE); |
| 286 if (device_monitor) | 285 if (device_monitor) |
| 287 device_observer_.Add(device_monitor); | 286 device_observer_.Add(device_monitor); |
| 288 | 287 |
| 289 helper_ = new BlockingThreadHelper(weak_factory_.GetWeakPtr()); | 288 helper_ = base::MakeUnique<BlockingTaskHelper>(weak_factory_.GetWeakPtr()); |
| 290 blocking_task_runner->PostTask( | 289 blocking_task_runner()->PostTask( |
| 291 FROM_HERE, base::Bind(&BlockingThreadHelper::EnumerateDevices, | 290 FROM_HERE, base::Bind(&BlockingTaskHelper::EnumerateDevices, |
| 292 base::Unretained(helper_))); | 291 base::Unretained(helper_.get()))); |
| 293 } | 292 } |
| 294 | 293 |
| 295 UsbServiceWin::~UsbServiceWin() {} | 294 UsbServiceWin::~UsbServiceWin() { |
| 295 DCHECK(!helper_); |
| 296 } |
| 297 |
| 298 void UsbServiceWin::Shutdown() { |
| 299 blocking_task_runner()->DeleteSoon(FROM_HERE, helper_.release()); |
| 300 UsbService::Shutdown(); |
| 301 } |
| 296 | 302 |
| 297 void UsbServiceWin::GetDevices(const GetDevicesCallback& callback) { | 303 void UsbServiceWin::GetDevices(const GetDevicesCallback& callback) { |
| 298 DCHECK(CalledOnValidThread()); | 304 DCHECK(CalledOnValidThread()); |
| 299 if (enumeration_ready()) | 305 if (enumeration_ready()) |
| 300 UsbService::GetDevices(callback); | 306 UsbService::GetDevices(callback); |
| 301 else | 307 else |
| 302 enumeration_callbacks_.push_back(callback); | 308 enumeration_callbacks_.push_back(callback); |
| 303 } | 309 } |
| 304 | 310 |
| 305 void UsbServiceWin::OnDeviceAdded(const GUID& class_guid, | 311 void UsbServiceWin::OnDeviceAdded(const GUID& class_guid, |
| 306 const std::string& device_path) { | 312 const std::string& device_path) { |
| 307 blocking_task_runner()->PostTask( | 313 blocking_task_runner()->PostTask( |
| 308 FROM_HERE, base::Bind(&BlockingThreadHelper::EnumerateDevicePath, | 314 FROM_HERE, base::Bind(&BlockingTaskHelper::EnumerateDevicePath, |
| 309 base::Unretained(helper_), device_path)); | 315 base::Unretained(helper_.get()), device_path)); |
| 310 } | 316 } |
| 311 | 317 |
| 312 void UsbServiceWin::OnDeviceRemoved(const GUID& class_guid, | 318 void UsbServiceWin::OnDeviceRemoved(const GUID& class_guid, |
| 313 const std::string& device_path) { | 319 const std::string& device_path) { |
| 314 DCHECK(CalledOnValidThread()); | 320 DCHECK(CalledOnValidThread()); |
| 315 auto by_path_it = devices_by_path_.find(device_path); | 321 auto by_path_it = devices_by_path_.find(device_path); |
| 316 if (by_path_it == devices_by_path_.end()) | 322 if (by_path_it == devices_by_path_.end()) |
| 317 return; | 323 return; |
| 318 | 324 |
| 319 scoped_refptr<UsbDeviceWin> device = by_path_it->second; | 325 scoped_refptr<UsbDeviceWin> device = by_path_it->second; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 result.push_back(map_entry.second); | 406 result.push_back(map_entry.second); |
| 401 for (const auto& callback : enumeration_callbacks_) | 407 for (const auto& callback : enumeration_callbacks_) |
| 402 callback.Run(result); | 408 callback.Run(result); |
| 403 enumeration_callbacks_.clear(); | 409 enumeration_callbacks_.clear(); |
| 404 } else if (success && enumeration_ready()) { | 410 } else if (success && enumeration_ready()) { |
| 405 NotifyDeviceAdded(device); | 411 NotifyDeviceAdded(device); |
| 406 } | 412 } |
| 407 } | 413 } |
| 408 | 414 |
| 409 } // namespace device | 415 } // namespace device |
| OLD | NEW |