Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / hid / hid_service_win.cc
blob0af821c83d53472e0168eb3333873098b9bffbd0
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
3 // found in the LICENSE file.
5 #include "device/hid/hid_service_win.h"
7 #define INITGUID
9 #include <dbt.h>
10 #include <setupapi.h>
11 #include <winioctl.h>
13 #include "base/bind.h"
14 #include "base/files/file.h"
15 #include "base/location.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "components/device_event_log/device_event_log.h"
22 #include "device/hid/hid_connection_win.h"
23 #include "device/hid/hid_device_info.h"
24 #include "net/base/io_buffer.h"
26 // Setup API is required to enumerate HID devices.
27 #pragma comment(lib, "setupapi.lib")
28 #pragma comment(lib, "hid.lib")
30 namespace device {
32 HidServiceWin::HidServiceWin(
33 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
34 : file_task_runner_(file_task_runner),
35 device_observer_(this),
36 weak_factory_(this) {
37 task_runner_ = base::ThreadTaskRunnerHandle::Get();
38 DCHECK(task_runner_.get());
39 DeviceMonitorWin* device_monitor =
40 DeviceMonitorWin::GetForDeviceInterface(GUID_DEVINTERFACE_HID);
41 if (device_monitor) {
42 device_observer_.Add(device_monitor);
44 file_task_runner_->PostTask(
45 FROM_HERE, base::Bind(&HidServiceWin::EnumerateOnFileThread,
46 weak_factory_.GetWeakPtr(), task_runner_));
49 HidServiceWin::~HidServiceWin() {}
51 void HidServiceWin::Connect(const HidDeviceId& device_id,
52 const ConnectCallback& callback) {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 const auto& map_entry = devices().find(device_id);
55 if (map_entry == devices().end()) {
56 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
57 return;
59 scoped_refptr<HidDeviceInfo> device_info = map_entry->second;
61 base::win::ScopedHandle file(OpenDevice(device_info->device_id()));
62 if (!file.IsValid()) {
63 HID_PLOG(EVENT) << "Failed to open device";
64 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
65 return;
68 task_runner_->PostTask(
69 FROM_HERE,
70 base::Bind(callback, new HidConnectionWin(device_info, file.Pass())));
73 // static
74 void HidServiceWin::EnumerateOnFileThread(
75 base::WeakPtr<HidServiceWin> service,
76 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
77 HDEVINFO device_info_set =
78 SetupDiGetClassDevs(&GUID_DEVINTERFACE_HID, NULL, NULL,
79 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
81 if (device_info_set != INVALID_HANDLE_VALUE) {
82 SP_DEVICE_INTERFACE_DATA device_interface_data;
83 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
85 for (int device_index = 0;
86 SetupDiEnumDeviceInterfaces(device_info_set,
87 NULL,
88 &GUID_DEVINTERFACE_HID,
89 device_index,
90 &device_interface_data);
91 ++device_index) {
92 DWORD required_size = 0;
94 // Determime the required size of detail struct.
95 SetupDiGetDeviceInterfaceDetail(device_info_set, &device_interface_data,
96 NULL, 0, &required_size, NULL);
98 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>
99 device_interface_detail_data(
100 static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(required_size)));
101 device_interface_detail_data->cbSize =
102 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
104 // Get the detailed data for this device.
105 BOOL res = SetupDiGetDeviceInterfaceDetail(
106 device_info_set, &device_interface_data,
107 device_interface_detail_data.get(), required_size, NULL, NULL);
108 if (!res) {
109 continue;
112 std::string device_path(
113 base::SysWideToUTF8(device_interface_detail_data->DevicePath));
114 DCHECK(base::IsStringASCII(device_path));
115 AddDeviceOnFileThread(service, task_runner,
116 base::ToLowerASCII(device_path));
120 task_runner->PostTask(
121 FROM_HERE, base::Bind(&HidServiceWin::FirstEnumerationComplete, service));
124 // static
125 void HidServiceWin::CollectInfoFromButtonCaps(
126 PHIDP_PREPARSED_DATA preparsed_data,
127 HIDP_REPORT_TYPE report_type,
128 USHORT button_caps_length,
129 HidCollectionInfo* collection_info) {
130 if (button_caps_length > 0) {
131 scoped_ptr<HIDP_BUTTON_CAPS[]> button_caps(
132 new HIDP_BUTTON_CAPS[button_caps_length]);
133 if (HidP_GetButtonCaps(report_type,
134 &button_caps[0],
135 &button_caps_length,
136 preparsed_data) == HIDP_STATUS_SUCCESS) {
137 for (size_t i = 0; i < button_caps_length; i++) {
138 int report_id = button_caps[i].ReportID;
139 if (report_id != 0) {
140 collection_info->report_ids.insert(report_id);
147 // static
148 void HidServiceWin::CollectInfoFromValueCaps(
149 PHIDP_PREPARSED_DATA preparsed_data,
150 HIDP_REPORT_TYPE report_type,
151 USHORT value_caps_length,
152 HidCollectionInfo* collection_info) {
153 if (value_caps_length > 0) {
154 scoped_ptr<HIDP_VALUE_CAPS[]> value_caps(
155 new HIDP_VALUE_CAPS[value_caps_length]);
156 if (HidP_GetValueCaps(
157 report_type, &value_caps[0], &value_caps_length, preparsed_data) ==
158 HIDP_STATUS_SUCCESS) {
159 for (size_t i = 0; i < value_caps_length; i++) {
160 int report_id = value_caps[i].ReportID;
161 if (report_id != 0) {
162 collection_info->report_ids.insert(report_id);
169 // static
170 void HidServiceWin::AddDeviceOnFileThread(
171 base::WeakPtr<HidServiceWin> service,
172 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
173 const std::string& device_path) {
174 base::win::ScopedHandle device_handle(OpenDevice(device_path));
175 if (!device_handle.IsValid()) {
176 return;
179 HIDD_ATTRIBUTES attrib = {0};
180 attrib.Size = sizeof(HIDD_ATTRIBUTES);
181 if (!HidD_GetAttributes(device_handle.Get(), &attrib)) {
182 HID_LOG(EVENT) << "Failed to get device attributes.";
183 return;
186 PHIDP_PREPARSED_DATA preparsed_data = nullptr;
187 if (!HidD_GetPreparsedData(device_handle.Get(), &preparsed_data) ||
188 !preparsed_data) {
189 HID_LOG(EVENT) << "Failed to get device data.";
190 return;
193 HIDP_CAPS capabilities = {0};
194 if (HidP_GetCaps(preparsed_data, &capabilities) != HIDP_STATUS_SUCCESS) {
195 HID_LOG(EVENT) << "Failed to get device capabilities.";
196 HidD_FreePreparsedData(preparsed_data);
197 return;
200 // Whether or not the device includes report IDs in its reports the size
201 // of the report ID is included in the value provided by Windows. This
202 // appears contrary to the MSDN documentation.
203 size_t max_input_report_size = 0;
204 size_t max_output_report_size = 0;
205 size_t max_feature_report_size = 0;
206 if (capabilities.InputReportByteLength > 0) {
207 max_input_report_size = capabilities.InputReportByteLength - 1;
209 if (capabilities.OutputReportByteLength > 0) {
210 max_output_report_size = capabilities.OutputReportByteLength - 1;
212 if (capabilities.FeatureReportByteLength > 0) {
213 max_feature_report_size = capabilities.FeatureReportByteLength - 1;
216 HidCollectionInfo collection_info;
217 collection_info.usage = HidUsageAndPage(
218 capabilities.Usage,
219 static_cast<HidUsageAndPage::Page>(capabilities.UsagePage));
220 CollectInfoFromButtonCaps(preparsed_data, HidP_Input,
221 capabilities.NumberInputButtonCaps,
222 &collection_info);
223 CollectInfoFromButtonCaps(preparsed_data, HidP_Output,
224 capabilities.NumberOutputButtonCaps,
225 &collection_info);
226 CollectInfoFromButtonCaps(preparsed_data, HidP_Feature,
227 capabilities.NumberFeatureButtonCaps,
228 &collection_info);
229 CollectInfoFromValueCaps(preparsed_data, HidP_Input,
230 capabilities.NumberInputValueCaps, &collection_info);
231 CollectInfoFromValueCaps(preparsed_data, HidP_Output,
232 capabilities.NumberOutputValueCaps,
233 &collection_info);
234 CollectInfoFromValueCaps(preparsed_data, HidP_Feature,
235 capabilities.NumberFeatureValueCaps,
236 &collection_info);
238 // 1023 characters plus NULL terminator is more than enough for a USB string
239 // descriptor which is limited to 126 characters.
240 wchar_t buffer[1024];
241 std::string product_name;
242 if (HidD_GetProductString(device_handle.Get(), &buffer[0], sizeof(buffer))) {
243 // NULL termination guaranteed by the API.
244 product_name = base::SysWideToUTF8(buffer);
246 std::string serial_number;
247 if (HidD_GetSerialNumberString(device_handle.Get(), &buffer[0],
248 sizeof(buffer))) {
249 // NULL termination guaranteed by the API.
250 serial_number = base::SysWideToUTF8(buffer);
253 // This populates the HidDeviceInfo instance without a raw report descriptor.
254 // The descriptor is unavailable on Windows because HID devices are exposed to
255 // user-space as individual top-level collections.
256 scoped_refptr<HidDeviceInfo> device_info(new HidDeviceInfo(
257 device_path, attrib.VendorID, attrib.ProductID, product_name,
258 serial_number,
259 kHIDBusTypeUSB, // TODO(reillyg): Detect Bluetooth. crbug.com/443335
260 collection_info, max_input_report_size, max_output_report_size,
261 max_feature_report_size));
263 HidD_FreePreparsedData(preparsed_data);
264 task_runner->PostTask(
265 FROM_HERE, base::Bind(&HidServiceWin::AddDevice, service, device_info));
268 void HidServiceWin::OnDeviceAdded(const GUID& class_guid,
269 const std::string& device_path) {
270 file_task_runner_->PostTask(
271 FROM_HERE,
272 base::Bind(&HidServiceWin::AddDeviceOnFileThread,
273 weak_factory_.GetWeakPtr(), task_runner_, device_path));
276 void HidServiceWin::OnDeviceRemoved(const GUID& class_guid,
277 const std::string& device_path) {
278 // Execute a no-op closure on the file task runner to synchronize with any
279 // devices that are still being enumerated.
280 file_task_runner_->PostTaskAndReply(
281 FROM_HERE, base::Bind(&base::DoNothing),
282 base::Bind(&HidServiceWin::RemoveDevice, weak_factory_.GetWeakPtr(),
283 device_path));
286 // static
287 base::win::ScopedHandle HidServiceWin::OpenDevice(
288 const std::string& device_path) {
289 base::win::ScopedHandle file(
290 CreateFileA(device_path.c_str(), GENERIC_WRITE | GENERIC_READ,
291 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
292 FILE_FLAG_OVERLAPPED, NULL));
293 if (!file.IsValid() &&
294 GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) {
295 file.Set(CreateFileA(device_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
296 NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL));
298 return file.Pass();
301 } // namespace device