| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_impl.h" | 5 #include "device/usb/usb_service_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 } | 229 } |
| 230 | 230 |
| 231 UsbServiceImpl::~UsbServiceImpl() { | 231 UsbServiceImpl::~UsbServiceImpl() { |
| 232 if (hotplug_enabled_) | 232 if (hotplug_enabled_) |
| 233 libusb_hotplug_deregister_callback(context_->context(), hotplug_handle_); | 233 libusb_hotplug_deregister_callback(context_->context(), hotplug_handle_); |
| 234 for (auto* platform_device : ignored_devices_) | 234 for (auto* platform_device : ignored_devices_) |
| 235 libusb_unref_device(platform_device); | 235 libusb_unref_device(platform_device); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void UsbServiceImpl::GetDevices(const GetDevicesCallback& callback) { | 238 void UsbServiceImpl::GetDevices(const GetDevicesCallback& callback) { |
| 239 DCHECK(CalledOnValidThread()); | 239 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 240 | 240 |
| 241 if (usb_unavailable_) { | 241 if (usb_unavailable_) { |
| 242 task_runner()->PostTask( | 242 task_runner()->PostTask( |
| 243 FROM_HERE, | 243 FROM_HERE, |
| 244 base::Bind(callback, std::vector<scoped_refptr<UsbDevice>>())); | 244 base::Bind(callback, std::vector<scoped_refptr<UsbDevice>>())); |
| 245 return; | 245 return; |
| 246 } | 246 } |
| 247 | 247 |
| 248 if (hotplug_enabled_ && !enumeration_in_progress_) { | 248 if (hotplug_enabled_ && !enumeration_in_progress_) { |
| 249 // The device list is updated live when hotplug events are supported. | 249 // The device list is updated live when hotplug events are supported. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 RefreshDevices(); | 301 RefreshDevices(); |
| 302 | 302 |
| 303 #if defined(OS_WIN) | 303 #if defined(OS_WIN) |
| 304 DeviceMonitorWin* device_monitor = DeviceMonitorWin::GetForAllInterfaces(); | 304 DeviceMonitorWin* device_monitor = DeviceMonitorWin::GetForAllInterfaces(); |
| 305 if (device_monitor) | 305 if (device_monitor) |
| 306 device_observer_.Add(device_monitor); | 306 device_observer_.Add(device_monitor); |
| 307 #endif // OS_WIN | 307 #endif // OS_WIN |
| 308 } | 308 } |
| 309 | 309 |
| 310 void UsbServiceImpl::RefreshDevices() { | 310 void UsbServiceImpl::RefreshDevices() { |
| 311 DCHECK(CalledOnValidThread()); | 311 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 312 | 312 |
| 313 if (!context_ || enumeration_in_progress_) | 313 if (!context_ || enumeration_in_progress_) |
| 314 return; | 314 return; |
| 315 | 315 |
| 316 enumeration_in_progress_ = true; | 316 enumeration_in_progress_ = true; |
| 317 DCHECK(devices_being_enumerated_.empty()); | 317 DCHECK(devices_being_enumerated_.empty()); |
| 318 | 318 |
| 319 std::string device_path; | 319 std::string device_path; |
| 320 if (!pending_path_enumerations_.empty()) { | 320 if (!pending_path_enumerations_.empty()) { |
| 321 device_path = pending_path_enumerations_.front(); | 321 device_path = pending_path_enumerations_.front(); |
| 322 pending_path_enumerations_.pop(); | 322 pending_path_enumerations_.pop(); |
| 323 } | 323 } |
| 324 | 324 |
| 325 base::PostTaskWithTraits(FROM_HERE, kBlockingTaskTraits, | 325 base::PostTaskWithTraits(FROM_HERE, kBlockingTaskTraits, |
| 326 base::Bind(&GetDeviceListOnBlockingThread, | 326 base::Bind(&GetDeviceListOnBlockingThread, |
| 327 device_path, context_, task_runner(), | 327 device_path, context_, task_runner(), |
| 328 base::Bind(&UsbServiceImpl::OnDeviceList, | 328 base::Bind(&UsbServiceImpl::OnDeviceList, |
| 329 weak_factory_.GetWeakPtr()))); | 329 weak_factory_.GetWeakPtr()))); |
| 330 } | 330 } |
| 331 | 331 |
| 332 void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices, | 332 void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices, |
| 333 size_t device_count) { | 333 size_t device_count) { |
| 334 DCHECK(CalledOnValidThread()); | 334 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 335 if (!platform_devices) { | 335 if (!platform_devices) { |
| 336 RefreshDevicesComplete(); | 336 RefreshDevicesComplete(); |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 | 339 |
| 340 base::Closure refresh_complete = | 340 base::Closure refresh_complete = |
| 341 base::BarrierClosure(static_cast<int>(device_count), | 341 base::BarrierClosure(static_cast<int>(device_count), |
| 342 base::Bind(&UsbServiceImpl::RefreshDevicesComplete, | 342 base::Bind(&UsbServiceImpl::RefreshDevicesComplete, |
| 343 weak_factory_.GetWeakPtr())); | 343 weak_factory_.GetWeakPtr())); |
| 344 std::list<PlatformUsbDevice> new_devices; | 344 std::list<PlatformUsbDevice> new_devices; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 } | 388 } |
| 389 | 389 |
| 390 for (PlatformUsbDevice platform_device : new_devices) { | 390 for (PlatformUsbDevice platform_device : new_devices) { |
| 391 EnumerateDevice(platform_device, refresh_complete); | 391 EnumerateDevice(platform_device, refresh_complete); |
| 392 } | 392 } |
| 393 | 393 |
| 394 libusb_free_device_list(platform_devices, true); | 394 libusb_free_device_list(platform_devices, true); |
| 395 } | 395 } |
| 396 | 396 |
| 397 void UsbServiceImpl::RefreshDevicesComplete() { | 397 void UsbServiceImpl::RefreshDevicesComplete() { |
| 398 DCHECK(CalledOnValidThread()); | 398 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 399 DCHECK(enumeration_in_progress_); | 399 DCHECK(enumeration_in_progress_); |
| 400 | 400 |
| 401 enumeration_ready_ = true; | 401 enumeration_ready_ = true; |
| 402 enumeration_in_progress_ = false; | 402 enumeration_in_progress_ = false; |
| 403 devices_being_enumerated_.clear(); | 403 devices_being_enumerated_.clear(); |
| 404 | 404 |
| 405 if (!pending_enumeration_callbacks_.empty()) { | 405 if (!pending_enumeration_callbacks_.empty()) { |
| 406 std::vector<scoped_refptr<UsbDevice>> result; | 406 std::vector<scoped_refptr<UsbDevice>> result; |
| 407 result.reserve(devices().size()); | 407 result.reserve(devices().size()); |
| 408 for (const auto& map_entry : devices()) | 408 for (const auto& map_entry : devices()) |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 base::Unretained(self), device)); | 523 base::Unretained(self), device)); |
| 524 break; | 524 break; |
| 525 default: | 525 default: |
| 526 NOTREACHED(); | 526 NOTREACHED(); |
| 527 } | 527 } |
| 528 | 528 |
| 529 return 0; | 529 return 0; |
| 530 } | 530 } |
| 531 | 531 |
| 532 void UsbServiceImpl::OnPlatformDeviceAdded(PlatformUsbDevice platform_device) { | 532 void UsbServiceImpl::OnPlatformDeviceAdded(PlatformUsbDevice platform_device) { |
| 533 DCHECK(CalledOnValidThread()); | 533 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 534 DCHECK(!base::ContainsKey(platform_devices_, platform_device)); | 534 DCHECK(!base::ContainsKey(platform_devices_, platform_device)); |
| 535 EnumerateDevice(platform_device, base::Bind(&base::DoNothing)); | 535 EnumerateDevice(platform_device, base::Bind(&base::DoNothing)); |
| 536 libusb_unref_device(platform_device); | 536 libusb_unref_device(platform_device); |
| 537 } | 537 } |
| 538 | 538 |
| 539 void UsbServiceImpl::OnPlatformDeviceRemoved( | 539 void UsbServiceImpl::OnPlatformDeviceRemoved( |
| 540 PlatformUsbDevice platform_device) { | 540 PlatformUsbDevice platform_device) { |
| 541 DCHECK(CalledOnValidThread()); | 541 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 542 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); | 542 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
| 543 if (it != platform_devices_.end()) { | 543 if (it != platform_devices_.end()) { |
| 544 RemoveDevice(it->second); | 544 RemoveDevice(it->second); |
| 545 } else { | 545 } else { |
| 546 devices_being_enumerated_.erase(platform_device); | 546 devices_being_enumerated_.erase(platform_device); |
| 547 } | 547 } |
| 548 libusb_unref_device(platform_device); | 548 libusb_unref_device(platform_device); |
| 549 } | 549 } |
| 550 | 550 |
| 551 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, | 551 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, |
| 552 const base::Closure& refresh_complete) { | 552 const base::Closure& refresh_complete) { |
| 553 libusb_ref_device(platform_device); | 553 libusb_ref_device(platform_device); |
| 554 ignored_devices_.insert(platform_device); | 554 ignored_devices_.insert(platform_device); |
| 555 refresh_complete.Run(); | 555 refresh_complete.Run(); |
| 556 } | 556 } |
| 557 | 557 |
| 558 } // namespace device | 558 } // namespace device |
| OLD | NEW |