| 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 <list> | 7 #include <list> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "components/device_event_log/device_event_log.h" | 19 #include "components/device_event_log/device_event_log.h" |
| 20 #include "device/usb/usb_device_handle.h" | 20 #include "device/usb/usb_device_handle.h" |
| 21 #include "device/usb/usb_error.h" | 21 #include "device/usb/usb_error.h" |
| 22 #include "device/usb/webusb_descriptors.h" | 22 #include "device/usb/webusb_descriptors.h" |
| 23 #include "third_party/libusb/src/libusb/libusb.h" | 23 #include "third_party/libusb/src/libusb/libusb.h" |
| 24 | 24 |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 #include <setupapi.h> | 26 #include <setupapi.h> |
| 27 #include <usbiodef.h> | 27 #include <usbiodef.h> |
| 28 | 28 |
| 29 #include "base/strings/string_util.h" | 29 #include "base/strings/string_util.h" |
| 30 #include "base/win/scoped_device_info_list.h" |
| 31 #include "base/win/scoped_device_info_object.h" |
| 30 #endif // OS_WIN | 32 #endif // OS_WIN |
| 31 | 33 |
| 32 #if defined(USE_UDEV) | 34 #if defined(USE_UDEV) |
| 33 #include "device/udev_linux/scoped_udev.h" | 35 #include "device/udev_linux/scoped_udev.h" |
| 34 #endif // USE_UDEV | 36 #endif // USE_UDEV |
| 35 | 37 |
| 36 using net::IOBufferWithSize; | 38 using net::IOBufferWithSize; |
| 37 | 39 |
| 38 namespace device { | 40 namespace device { |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 // Standard USB requests and descriptor types: | 44 // Standard USB requests and descriptor types: |
| 43 const uint16_t kUsbVersion2_1 = 0x0210; | 45 const uint16_t kUsbVersion2_1 = 0x0210; |
| 44 const uint8_t kGetDescriptorRequest = 0x06; | 46 const uint8_t kGetDescriptorRequest = 0x06; |
| 45 const uint8_t kStringDescriptorType = 0x03; | 47 const uint8_t kStringDescriptorType = 0x03; |
| 46 const uint8_t kBosDescriptorType = 0x0F; | 48 const uint8_t kBosDescriptorType = 0x0F; |
| 47 | 49 |
| 48 // WebUSB requests: | 50 // WebUSB requests: |
| 49 const uint8_t kGetAllowedOriginsRequest = 0x01; | 51 const uint8_t kGetAllowedOriginsRequest = 0x01; |
| 50 const uint8_t kGetLandingPageRequest = 0x02; | 52 const uint8_t kGetLandingPageRequest = 0x02; |
| 51 | 53 |
| 52 const int kControlTransferTimeout = 60000; // 1 minute | 54 const int kControlTransferTimeout = 60000; // 1 minute |
| 53 | 55 |
| 54 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
| 55 | 57 |
| 56 // Wrapper around a HDEVINFO that automatically destroys it. | |
| 57 class ScopedDeviceInfoList { | |
| 58 public: | |
| 59 explicit ScopedDeviceInfoList(HDEVINFO handle) : handle_(handle) {} | |
| 60 | |
| 61 ~ScopedDeviceInfoList() { | |
| 62 if (valid()) { | |
| 63 SetupDiDestroyDeviceInfoList(handle_); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 bool valid() { return handle_ != INVALID_HANDLE_VALUE; } | |
| 68 | |
| 69 HDEVINFO get() { return handle_; } | |
| 70 | |
| 71 private: | |
| 72 HDEVINFO handle_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(ScopedDeviceInfoList); | |
| 75 }; | |
| 76 | |
| 77 // Wrapper around an SP_DEVINFO_DATA that initializes it properly and | |
| 78 // automatically deletes it. | |
| 79 class ScopedDeviceInfo { | |
| 80 public: | |
| 81 ScopedDeviceInfo() { | |
| 82 memset(&dev_info_data_, 0, sizeof(dev_info_data_)); | |
| 83 dev_info_data_.cbSize = sizeof(dev_info_data_); | |
| 84 } | |
| 85 | |
| 86 ~ScopedDeviceInfo() { | |
| 87 if (dev_info_set_ != INVALID_HANDLE_VALUE) { | |
| 88 SetupDiDeleteDeviceInfo(dev_info_set_, &dev_info_data_); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 // Once the SP_DEVINFO_DATA has been populated it must be freed using the | |
| 93 // HDEVINFO it was created from. | |
| 94 void set_valid(HDEVINFO dev_info_set) { | |
| 95 DCHECK(dev_info_set_ == INVALID_HANDLE_VALUE); | |
| 96 DCHECK(dev_info_set != INVALID_HANDLE_VALUE); | |
| 97 dev_info_set_ = dev_info_set; | |
| 98 } | |
| 99 | |
| 100 PSP_DEVINFO_DATA get() { return &dev_info_data_; } | |
| 101 | |
| 102 private: | |
| 103 HDEVINFO dev_info_set_ = INVALID_HANDLE_VALUE; | |
| 104 SP_DEVINFO_DATA dev_info_data_; | |
| 105 }; | |
| 106 | |
| 107 bool IsWinUsbInterface(const std::string& device_path) { | 58 bool IsWinUsbInterface(const std::string& device_path) { |
| 108 ScopedDeviceInfoList dev_info_list(SetupDiCreateDeviceInfoList(NULL, NULL)); | 59 base::win::ScopedDeviceInfoList dev_info_list( |
| 109 if (!dev_info_list.valid()) { | 60 SetupDiCreateDeviceInfoList(NULL, NULL)); |
| 61 if (!dev_info_list.IsValid()) { |
| 110 USB_PLOG(ERROR) << "Failed to create a device information set"; | 62 USB_PLOG(ERROR) << "Failed to create a device information set"; |
| 111 return false; | 63 return false; |
| 112 } | 64 } |
| 113 | 65 |
| 114 // This will add the device to |dev_info_list| so we can query driver info. | 66 // This will add the device to |dev_info_list| so we can query driver info. |
| 115 if (!SetupDiOpenDeviceInterfaceA(dev_info_list.get(), device_path.c_str(), 0, | 67 if (!SetupDiOpenDeviceInterfaceA(dev_info_list.Get(), device_path.c_str(), 0, |
| 116 NULL)) { | 68 NULL)) { |
| 117 USB_PLOG(ERROR) << "Failed to get device interface data for " | 69 USB_PLOG(ERROR) << "Failed to get device interface data for " |
| 118 << device_path; | 70 << device_path; |
| 119 return false; | 71 return false; |
| 120 } | 72 } |
| 121 | 73 |
| 122 ScopedDeviceInfo dev_info; | 74 base::win::ScopedDeviceInfo dev_info; |
| 123 if (!SetupDiEnumDeviceInfo(dev_info_list.get(), 0, dev_info.get())) { | 75 if (!SetupDiEnumDeviceInfo(dev_info_list.Get(), 0, dev_info.get())) { |
| 124 USB_PLOG(ERROR) << "Failed to get device info for " << device_path; | 76 USB_PLOG(ERROR) << "Failed to get device info for " << device_path; |
| 125 return false; | 77 return false; |
| 126 } | 78 } |
| 127 dev_info.set_valid(dev_info_list.get()); | 79 dev_info.set_valid(dev_info_list.Get()); |
| 128 | 80 |
| 129 DWORD reg_data_type; | 81 DWORD reg_data_type; |
| 130 BYTE buffer[256]; | 82 BYTE buffer[256]; |
| 131 if (!SetupDiGetDeviceRegistryPropertyA(dev_info_list.get(), dev_info.get(), | 83 if (!SetupDiGetDeviceRegistryPropertyA(dev_info_list.Get(), dev_info.get(), |
| 132 SPDRP_SERVICE, ®_data_type, | 84 SPDRP_SERVICE, ®_data_type, |
| 133 &buffer[0], sizeof buffer, NULL)) { | 85 &buffer[0], sizeof buffer, NULL)) { |
| 134 USB_PLOG(ERROR) << "Failed to get device service property"; | 86 USB_PLOG(ERROR) << "Failed to get device service property"; |
| 135 return false; | 87 return false; |
| 136 } | 88 } |
| 137 if (reg_data_type != REG_SZ) { | 89 if (reg_data_type != REG_SZ) { |
| 138 USB_LOG(ERROR) << "Unexpected data type for driver service: " | 90 USB_LOG(ERROR) << "Unexpected data type for driver service: " |
| 139 << reg_data_type; | 91 << reg_data_type; |
| 140 return false; | 92 return false; |
| 141 } | 93 } |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); | 805 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
| 854 if (it != platform_devices_.end()) { | 806 if (it != platform_devices_.end()) { |
| 855 RemoveDevice(it->second); | 807 RemoveDevice(it->second); |
| 856 } else { | 808 } else { |
| 857 devices_being_enumerated_.erase(platform_device); | 809 devices_being_enumerated_.erase(platform_device); |
| 858 } | 810 } |
| 859 libusb_unref_device(platform_device); | 811 libusb_unref_device(platform_device); |
| 860 } | 812 } |
| 861 | 813 |
| 862 } // namespace device | 814 } // namespace device |
| OLD | NEW |