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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 base::BarrierClosure(static_cast<int>(device_count), | 320 base::BarrierClosure(static_cast<int>(device_count), |
321 base::Bind(&UsbServiceImpl::RefreshDevicesComplete, | 321 base::Bind(&UsbServiceImpl::RefreshDevicesComplete, |
322 weak_factory_.GetWeakPtr())); | 322 weak_factory_.GetWeakPtr())); |
323 std::list<PlatformUsbDevice> new_devices; | 323 std::list<PlatformUsbDevice> new_devices; |
324 std::set<PlatformUsbDevice> existing_ignored_devices; | 324 std::set<PlatformUsbDevice> existing_ignored_devices; |
325 | 325 |
326 // Look for new and existing devices. | 326 // Look for new and existing devices. |
327 for (size_t i = 0; i < device_count; ++i) { | 327 for (size_t i = 0; i < device_count; ++i) { |
328 PlatformUsbDevice platform_device = platform_devices[i]; | 328 PlatformUsbDevice platform_device = platform_devices[i]; |
329 // Ignore some devices. | 329 // Ignore some devices. |
330 if (ContainsValue(ignored_devices_, platform_device)) { | 330 if (base::ContainsValue(ignored_devices_, platform_device)) { |
331 existing_ignored_devices.insert(platform_device); | 331 existing_ignored_devices.insert(platform_device); |
332 refresh_complete.Run(); | 332 refresh_complete.Run(); |
333 continue; | 333 continue; |
334 } | 334 } |
335 | 335 |
336 auto it = platform_devices_.find(platform_device); | 336 auto it = platform_devices_.find(platform_device); |
337 | 337 |
338 if (it == platform_devices_.end()) { | 338 if (it == platform_devices_.end()) { |
339 new_devices.push_back(platform_device); | 339 new_devices.push_back(platform_device); |
340 } else { | 340 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
353 device->set_visited(false); | 353 device->set_visited(false); |
354 } else { | 354 } else { |
355 RemoveDevice(device); | 355 RemoveDevice(device); |
356 } | 356 } |
357 } | 357 } |
358 | 358 |
359 // Remove devices not seen in this enumeration from |ignored_devices_|. | 359 // Remove devices not seen in this enumeration from |ignored_devices_|. |
360 for (auto it = ignored_devices_.begin(); it != ignored_devices_.end(); | 360 for (auto it = ignored_devices_.begin(); it != ignored_devices_.end(); |
361 /* incremented internally */) { | 361 /* incremented internally */) { |
362 auto current = it++; | 362 auto current = it++; |
363 if (!ContainsValue(existing_ignored_devices, *current)) { | 363 if (!base::ContainsValue(existing_ignored_devices, *current)) { |
364 libusb_unref_device(*current); | 364 libusb_unref_device(*current); |
365 ignored_devices_.erase(current); | 365 ignored_devices_.erase(current); |
366 } | 366 } |
367 } | 367 } |
368 | 368 |
369 for (PlatformUsbDevice platform_device : new_devices) { | 369 for (PlatformUsbDevice platform_device : new_devices) { |
370 EnumerateDevice(platform_device, refresh_complete); | 370 EnumerateDevice(platform_device, refresh_complete); |
371 } | 371 } |
372 | 372 |
373 libusb_free_device_list(platform_devices, true); | 373 libusb_free_device_list(platform_devices, true); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 void UsbServiceImpl::AddDevice(const base::Closure& refresh_complete, | 444 void UsbServiceImpl::AddDevice(const base::Closure& refresh_complete, |
445 scoped_refptr<UsbDeviceImpl> device) { | 445 scoped_refptr<UsbDeviceImpl> device) { |
446 auto it = devices_being_enumerated_.find(device->platform_device()); | 446 auto it = devices_being_enumerated_.find(device->platform_device()); |
447 if (it == devices_being_enumerated_.end()) { | 447 if (it == devices_being_enumerated_.end()) { |
448 // Device was removed while being enumerated. | 448 // Device was removed while being enumerated. |
449 refresh_complete.Run(); | 449 refresh_complete.Run(); |
450 return; | 450 return; |
451 } | 451 } |
452 | 452 |
453 platform_devices_[device->platform_device()] = device; | 453 platform_devices_[device->platform_device()] = device; |
454 DCHECK(!ContainsKey(devices(), device->guid())); | 454 DCHECK(!base::ContainsKey(devices(), device->guid())); |
455 devices()[device->guid()] = device; | 455 devices()[device->guid()] = device; |
456 | 456 |
457 USB_LOG(USER) << "USB device added: vendor=" << device->vendor_id() << " \"" | 457 USB_LOG(USER) << "USB device added: vendor=" << device->vendor_id() << " \"" |
458 << device->manufacturer_string() | 458 << device->manufacturer_string() |
459 << "\", product=" << device->product_id() << " \"" | 459 << "\", product=" << device->product_id() << " \"" |
460 << device->product_string() << "\", serial=\"" | 460 << device->product_string() << "\", serial=\"" |
461 << device->serial_number() << "\", guid=" << device->guid(); | 461 << device->serial_number() << "\", guid=" << device->guid(); |
462 | 462 |
463 if (enumeration_ready_) { | 463 if (enumeration_ready_) { |
464 NotifyDeviceAdded(device); | 464 NotifyDeviceAdded(device); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 break; | 510 break; |
511 default: | 511 default: |
512 NOTREACHED(); | 512 NOTREACHED(); |
513 } | 513 } |
514 | 514 |
515 return 0; | 515 return 0; |
516 } | 516 } |
517 | 517 |
518 void UsbServiceImpl::OnPlatformDeviceAdded(PlatformUsbDevice platform_device) { | 518 void UsbServiceImpl::OnPlatformDeviceAdded(PlatformUsbDevice platform_device) { |
519 DCHECK(CalledOnValidThread()); | 519 DCHECK(CalledOnValidThread()); |
520 DCHECK(!ContainsKey(platform_devices_, platform_device)); | 520 DCHECK(!base::ContainsKey(platform_devices_, platform_device)); |
521 EnumerateDevice(platform_device, base::Bind(&base::DoNothing)); | 521 EnumerateDevice(platform_device, base::Bind(&base::DoNothing)); |
522 libusb_unref_device(platform_device); | 522 libusb_unref_device(platform_device); |
523 } | 523 } |
524 | 524 |
525 void UsbServiceImpl::OnPlatformDeviceRemoved( | 525 void UsbServiceImpl::OnPlatformDeviceRemoved( |
526 PlatformUsbDevice platform_device) { | 526 PlatformUsbDevice platform_device) { |
527 DCHECK(CalledOnValidThread()); | 527 DCHECK(CalledOnValidThread()); |
528 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); | 528 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
529 if (it != platform_devices_.end()) { | 529 if (it != platform_devices_.end()) { |
530 RemoveDevice(it->second); | 530 RemoveDevice(it->second); |
531 } else { | 531 } else { |
532 devices_being_enumerated_.erase(platform_device); | 532 devices_being_enumerated_.erase(platform_device); |
533 } | 533 } |
534 libusb_unref_device(platform_device); | 534 libusb_unref_device(platform_device); |
535 } | 535 } |
536 | 536 |
537 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, | 537 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, |
538 const base::Closure& refresh_complete) { | 538 const base::Closure& refresh_complete) { |
539 libusb_ref_device(platform_device); | 539 libusb_ref_device(platform_device); |
540 ignored_devices_.insert(platform_device); | 540 ignored_devices_.insert(platform_device); |
541 refresh_complete.Run(); | 541 refresh_complete.Run(); |
542 } | 542 } |
543 | 543 |
544 } // namespace device | 544 } // namespace device |
OLD | NEW |