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

Side by Side Diff: device/hid/hid_service_win.cc

Issue 399313008: Remove HID devices not found during enumeration on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a protected const accessor instead of making devices_ protected. Created 6 years, 5 months 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/hid/hid_service.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/hid/hid_service_win.h" 5 #include "device/hid/hid_service_win.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 8
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA)); 59 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA));
60 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); 60 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
61 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 61 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
62 62
63 device_info_set = SetupDiGetClassDevs( 63 device_info_set = SetupDiGetClassDevs(
64 &GUID_DEVINTERFACE_HID, 64 &GUID_DEVINTERFACE_HID,
65 NULL, 65 NULL,
66 NULL, 66 NULL,
67 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 67 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
68 68
69 if (device_info_set == INVALID_HANDLE_VALUE) 69 std::set<std::string> connected_devices;
70 return;
71 70
72 for (int device_index = 0; 71 if (device_info_set != INVALID_HANDLE_VALUE) {
73 SetupDiEnumDeviceInterfaces(device_info_set, 72 for (int device_index = 0;
74 NULL, 73 SetupDiEnumDeviceInterfaces(device_info_set,
75 &GUID_DEVINTERFACE_HID, 74 NULL,
76 device_index, 75 &GUID_DEVINTERFACE_HID,
77 &device_interface_data); 76 device_index,
78 ++device_index) { 77 &device_interface_data);
79 DWORD required_size = 0; 78 ++device_index) {
79 DWORD required_size = 0;
80 80
81 // Determime the required size of detail struct. 81 // Determime the required size of detail struct.
82 SetupDiGetDeviceInterfaceDetailA(device_info_set, 82 SetupDiGetDeviceInterfaceDetailA(device_info_set,
83 &device_interface_data, 83 &device_interface_data,
84 NULL, 84 NULL,
85 0, 85 0,
86 &required_size, 86 &required_size,
87 NULL); 87 NULL);
88 88
89 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA_A, base::FreeDeleter> 89 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA_A, base::FreeDeleter>
90 device_interface_detail_data( 90 device_interface_detail_data(
91 static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>( 91 static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>(
92 malloc(required_size))); 92 malloc(required_size)));
93 device_interface_detail_data->cbSize = 93 device_interface_detail_data->cbSize =
94 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); 94 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
95 95
96 // Get the detailed data for this device. 96 // Get the detailed data for this device.
97 res = SetupDiGetDeviceInterfaceDetailA(device_info_set, 97 res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
98 &device_interface_data, 98 &device_interface_data,
99 device_interface_detail_data.get(), 99 device_interface_detail_data.get(),
100 required_size, 100 required_size,
101 NULL, 101 NULL,
102 NULL); 102 NULL);
103 if (!res) 103 if (!res)
104 continue; 104 continue;
105 105
106 // Enumerate device info. Looking for Setup Class "HIDClass". 106 // Enumerate device info. Looking for Setup Class "HIDClass".
107 for (DWORD i = 0; 107 for (DWORD i = 0;
108 SetupDiEnumDeviceInfo(device_info_set, i, &devinfo_data); 108 SetupDiEnumDeviceInfo(device_info_set, i, &devinfo_data);
109 i++) { 109 i++) {
110 char class_name[256] = {0}; 110 char class_name[256] = {0};
111 res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
112 &devinfo_data,
113 SPDRP_CLASS,
114 NULL,
115 (PBYTE) class_name,
116 sizeof(class_name) - 1,
117 NULL);
118 if (!res)
119 break;
120 if (memcmp(class_name, kHIDClass, sizeof(kHIDClass)) == 0) {
121 char driver_name[256] = {0};
122 // Get bounded driver.
123 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, 111 res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
124 &devinfo_data, 112 &devinfo_data,
125 SPDRP_DRIVER, 113 SPDRP_CLASS,
126 NULL, 114 NULL,
127 (PBYTE) driver_name, 115 (PBYTE) class_name,
128 sizeof(driver_name) - 1, 116 sizeof(class_name) - 1,
129 NULL); 117 NULL);
130 if (res) { 118 if (!res)
131 // Found the driver.
132 break; 119 break;
120 if (memcmp(class_name, kHIDClass, sizeof(kHIDClass)) == 0) {
121 char driver_name[256] = {0};
122 // Get bounded driver.
123 res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
124 &devinfo_data,
125 SPDRP_DRIVER,
126 NULL,
127 (PBYTE) driver_name,
128 sizeof(driver_name) - 1,
129 NULL);
130 if (res) {
131 // Found the driver.
132 break;
133 }
133 } 134 }
134 } 135 }
136
137 if (!res)
138 continue;
139
140 PlatformAddDevice(device_interface_detail_data->DevicePath);
141 connected_devices.insert(device_interface_detail_data->DevicePath);
135 } 142 }
143 }
136 144
137 if (!res) 145 // Find disconnected devices.
138 continue; 146 const DeviceMap& devices = GetDevicesNoEnumerate();
147 std::vector<std::string> disconnected_devices;
148 for (DeviceMap::const_iterator it = devices.begin();
149 it != devices.end();
150 ++it) {
151 if (!ContainsKey(connected_devices, it->first)) {
152 disconnected_devices.push_back(it->first);
153 }
154 }
139 155
140 PlatformAddDevice(device_interface_detail_data->DevicePath); 156 // Remove disconnected devices.
157 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
158 PlatformRemoveDevice(disconnected_devices[i]);
141 } 159 }
142 } 160 }
143 161
144 void HidServiceWin::PlatformAddDevice(const std::string& device_path) { 162 void HidServiceWin::PlatformAddDevice(const std::string& device_path) {
145 HidDeviceInfo device_info; 163 HidDeviceInfo device_info;
146 device_info.device_id = device_path; 164 device_info.device_id = device_path;
147 165
148 // Try to open the device. 166 // Try to open the device.
149 base::win::ScopedHandle device_handle( 167 base::win::ScopedHandle device_handle(
150 CreateFileA(device_path.c_str(), 168 CreateFileA(device_path.c_str(),
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 return NULL; 271 return NULL;
254 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info)); 272 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info));
255 if (!connection->available()) { 273 if (!connection->available()) {
256 PLOG(ERROR) << "Failed to open device."; 274 PLOG(ERROR) << "Failed to open device.";
257 return NULL; 275 return NULL;
258 } 276 }
259 return connection; 277 return connection;
260 } 278 }
261 279
262 } // namespace device 280 } // namespace device
OLDNEW
« no previous file with comments | « device/hid/hid_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698