Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: device/usb/usb_service_impl.cc

Issue 1456793002: Revert of Add code to deal with serial device disconnection detection on Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/serial/serial_io_handler_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "device/core/device_info_query_win.h"
31 #endif // OS_WIN 30 #endif // OS_WIN
32 31
33 #if defined(USE_UDEV) 32 #if defined(USE_UDEV)
34 #include "device/udev_linux/scoped_udev.h" 33 #include "device/udev_linux/scoped_udev.h"
35 #endif // USE_UDEV 34 #endif // USE_UDEV
36 35
37 using net::IOBufferWithSize; 36 using net::IOBufferWithSize;
38 37
39 namespace device { 38 namespace device {
40 39
41 namespace { 40 namespace {
42 41
43 // Standard USB requests and descriptor types: 42 // Standard USB requests and descriptor types:
44 const uint16_t kUsbVersion2_1 = 0x0210; 43 const uint16_t kUsbVersion2_1 = 0x0210;
45 const uint8_t kGetDescriptorRequest = 0x06; 44 const uint8_t kGetDescriptorRequest = 0x06;
46 const uint8_t kStringDescriptorType = 0x03; 45 const uint8_t kStringDescriptorType = 0x03;
47 const uint8_t kBosDescriptorType = 0x0F; 46 const uint8_t kBosDescriptorType = 0x0F;
48 47
49 // WebUSB requests: 48 // WebUSB requests:
50 const uint8_t kGetAllowedOriginsRequest = 0x01; 49 const uint8_t kGetAllowedOriginsRequest = 0x01;
51 const uint8_t kGetLandingPageRequest = 0x02; 50 const uint8_t kGetLandingPageRequest = 0x02;
52 51
53 const int kControlTransferTimeout = 60000; // 1 minute 52 const int kControlTransferTimeout = 60000; // 1 minute
54 53
55 #if defined(OS_WIN) 54 #if defined(OS_WIN)
56 55
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
57 bool IsWinUsbInterface(const std::string& device_path) { 107 bool IsWinUsbInterface(const std::string& device_path) {
58 DeviceInfoQueryWin device_info_query; 108 ScopedDeviceInfoList dev_info_list(SetupDiCreateDeviceInfoList(NULL, NULL));
59 if (!device_info_query.device_info_list_valid()) { 109 if (!dev_info_list.valid()) {
60 USB_PLOG(ERROR) << "Failed to create a device information set"; 110 USB_PLOG(ERROR) << "Failed to create a device information set";
61 return false; 111 return false;
62 } 112 }
63 113
64 // This will add the device so we can query driver info. 114 // This will add the device to |dev_info_list| so we can query driver info.
65 if (!device_info_query.AddDevice(device_path.c_str())) { 115 if (!SetupDiOpenDeviceInterfaceA(dev_info_list.get(), device_path.c_str(), 0,
116 NULL)) {
66 USB_PLOG(ERROR) << "Failed to get device interface data for " 117 USB_PLOG(ERROR) << "Failed to get device interface data for "
67 << device_path; 118 << device_path;
68 return false; 119 return false;
69 } 120 }
70 121
71 if (!device_info_query.GetDeviceInfo()) { 122 ScopedDeviceInfo dev_info;
123 if (!SetupDiEnumDeviceInfo(dev_info_list.get(), 0, dev_info.get())) {
72 USB_PLOG(ERROR) << "Failed to get device info for " << device_path; 124 USB_PLOG(ERROR) << "Failed to get device info for " << device_path;
73 return false; 125 return false;
74 } 126 }
127 dev_info.set_valid(dev_info_list.get());
75 128
76 std::string buffer; 129 DWORD reg_data_type;
77 if (!device_info_query.GetDeviceStringProperty(SPDRP_SERVICE, &buffer)) { 130 BYTE buffer[256];
131 if (!SetupDiGetDeviceRegistryPropertyA(dev_info_list.get(), dev_info.get(),
132 SPDRP_SERVICE, &reg_data_type,
133 &buffer[0], sizeof buffer, NULL)) {
78 USB_PLOG(ERROR) << "Failed to get device service property"; 134 USB_PLOG(ERROR) << "Failed to get device service property";
79 return false; 135 return false;
80 } 136 }
137 if (reg_data_type != REG_SZ) {
138 USB_LOG(ERROR) << "Unexpected data type for driver service: "
139 << reg_data_type;
140 return false;
141 }
81 142
82 USB_LOG(DEBUG) << "Driver for " << device_path << " is " << buffer << "."; 143 USB_LOG(DEBUG) << "Driver for " << device_path << " is " << buffer << ".";
83 if (base::StartsWith(buffer, "WinUSB", base::CompareCase::INSENSITIVE_ASCII)) 144 if (base::StartsWith(reinterpret_cast<const char*>(buffer), "WinUSB",
145 base::CompareCase::INSENSITIVE_ASCII))
84 return true; 146 return true;
85 return false; 147 return false;
86 } 148 }
87 149
88 #endif // OS_WIN 150 #endif // OS_WIN
89 151
90 void GetDeviceListOnBlockingThread( 152 void GetDeviceListOnBlockingThread(
91 const std::string& new_device_path, 153 const std::string& new_device_path,
92 scoped_refptr<UsbContext> usb_context, 154 scoped_refptr<UsbContext> usb_context,
93 scoped_refptr<base::SequencedTaskRunner> task_runner, 155 scoped_refptr<base::SequencedTaskRunner> task_runner,
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); 853 PlatformDeviceMap::iterator it = platform_devices_.find(platform_device);
792 if (it != platform_devices_.end()) { 854 if (it != platform_devices_.end()) {
793 RemoveDevice(it->second); 855 RemoveDevice(it->second);
794 } else { 856 } else {
795 devices_being_enumerated_.erase(platform_device); 857 devices_being_enumerated_.erase(platform_device);
796 } 858 }
797 libusb_unref_device(platform_device); 859 libusb_unref_device(platform_device);
798 } 860 }
799 861
800 } // namespace device 862 } // namespace device
OLDNEW
« no previous file with comments | « device/serial/serial_io_handler_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698