| 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_object.h" |
| 30 #endif // OS_WIN | 31 #endif // OS_WIN |
| 31 | 32 |
| 32 #if defined(USE_UDEV) | 33 #if defined(USE_UDEV) |
| 33 #include "device/udev_linux/scoped_udev.h" | 34 #include "device/udev_linux/scoped_udev.h" |
| 34 #endif // USE_UDEV | 35 #endif // USE_UDEV |
| 35 | 36 |
| 36 using net::IOBufferWithSize; | 37 using net::IOBufferWithSize; |
| 37 | 38 |
| 38 namespace device { | 39 namespace device { |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 // Standard USB requests and descriptor types: | 43 // Standard USB requests and descriptor types: |
| 43 const uint16_t kUsbVersion2_1 = 0x0210; | 44 const uint16_t kUsbVersion2_1 = 0x0210; |
| 44 const uint8_t kGetDescriptorRequest = 0x06; | 45 const uint8_t kGetDescriptorRequest = 0x06; |
| 45 const uint8_t kStringDescriptorType = 0x03; | 46 const uint8_t kStringDescriptorType = 0x03; |
| 46 const uint8_t kBosDescriptorType = 0x0F; | 47 const uint8_t kBosDescriptorType = 0x0F; |
| 47 | 48 |
| 48 // WebUSB requests: | 49 // WebUSB requests: |
| 49 const uint8_t kGetAllowedOriginsRequest = 0x01; | 50 const uint8_t kGetAllowedOriginsRequest = 0x01; |
| 50 const uint8_t kGetLandingPageRequest = 0x02; | 51 const uint8_t kGetLandingPageRequest = 0x02; |
| 51 | 52 |
| 52 const int kControlTransferTimeout = 60000; // 1 minute | 53 const int kControlTransferTimeout = 60000; // 1 minute |
| 53 | 54 |
| 54 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
| 55 | 56 |
| 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) { | 57 bool IsWinUsbInterface(const std::string& device_path) { |
| 108 ScopedDeviceInfoList dev_info_list(SetupDiCreateDeviceInfoList(NULL, NULL)); | 58 base::win::ScopedDeviceInfoList dev_info_list( |
| 109 if (!dev_info_list.valid()) { | 59 SetupDiCreateDeviceInfoList(NULL, NULL)); |
| 60 if (!dev_info_list.IsValid()) { |
| 110 USB_PLOG(ERROR) << "Failed to create a device information set"; | 61 USB_PLOG(ERROR) << "Failed to create a device information set"; |
| 111 return false; | 62 return false; |
| 112 } | 63 } |
| 113 | 64 |
| 114 // This will add the device to |dev_info_list| so we can query driver info. | 65 // 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, | 66 if (!SetupDiOpenDeviceInterfaceA(dev_info_list.Get(), device_path.c_str(), 0, |
| 116 NULL)) { | 67 NULL)) { |
| 117 USB_PLOG(ERROR) << "Failed to get device interface data for " | 68 USB_PLOG(ERROR) << "Failed to get device interface data for " |
| 118 << device_path; | 69 << device_path; |
| 119 return false; | 70 return false; |
| 120 } | 71 } |
| 121 | 72 |
| 122 ScopedDeviceInfo dev_info; | 73 base::win::ScopedDeviceInfo dev_info; |
| 123 if (!SetupDiEnumDeviceInfo(dev_info_list.get(), 0, dev_info.get())) { | 74 if (!SetupDiEnumDeviceInfo(dev_info_list.Get(), 0, dev_info.get())) { |
| 124 USB_PLOG(ERROR) << "Failed to get device info for " << device_path; | 75 USB_PLOG(ERROR) << "Failed to get device info for " << device_path; |
| 125 return false; | 76 return false; |
| 126 } | 77 } |
| 127 dev_info.set_valid(dev_info_list.get()); | 78 dev_info.set_valid(dev_info_list.Get()); |
| 128 | 79 |
| 129 DWORD reg_data_type; | 80 DWORD reg_data_type; |
| 130 BYTE buffer[256]; | 81 BYTE buffer[256]; |
| 131 if (!SetupDiGetDeviceRegistryPropertyA(dev_info_list.get(), dev_info.get(), | 82 if (!SetupDiGetDeviceRegistryPropertyA(dev_info_list.Get(), dev_info.get(), |
| 132 SPDRP_SERVICE, ®_data_type, | 83 SPDRP_SERVICE, ®_data_type, |
| 133 &buffer[0], sizeof buffer, NULL)) { | 84 &buffer[0], sizeof buffer, NULL)) { |
| 134 USB_PLOG(ERROR) << "Failed to get device service property"; | 85 USB_PLOG(ERROR) << "Failed to get device service property"; |
| 135 return false; | 86 return false; |
| 136 } | 87 } |
| 137 if (reg_data_type != REG_SZ) { | 88 if (reg_data_type != REG_SZ) { |
| 138 USB_LOG(ERROR) << "Unexpected data type for driver service: " | 89 USB_LOG(ERROR) << "Unexpected data type for driver service: " |
| 139 << reg_data_type; | 90 << reg_data_type; |
| 140 return false; | 91 return false; |
| 141 } | 92 } |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); | 804 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
| 854 if (it != platform_devices_.end()) { | 805 if (it != platform_devices_.end()) { |
| 855 RemoveDevice(it->second); | 806 RemoveDevice(it->second); |
| 856 } else { | 807 } else { |
| 857 devices_being_enumerated_.erase(platform_device); | 808 devices_being_enumerated_.erase(platform_device); |
| 858 } | 809 } |
| 859 libusb_unref_device(platform_device); | 810 libusb_unref_device(platform_device); |
| 860 } | 811 } |
| 861 | 812 |
| 862 } // namespace device | 813 } // namespace device |
| OLD | NEW |