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

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

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: linux headers Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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 #include <string>
9 8
10 #include "base/callback_helpers.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/stl_util.h" 9 #include "base/stl_util.h"
14 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
15 #include "device/hid/hid_connection.h"
16 #include "device/hid/hid_connection_win.h" 11 #include "device/hid/hid_connection_win.h"
17 #include "device/hid/hid_service.h" 12 #include "device/hid/hid_device_info.h"
18 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
19 14
20 #if defined(OS_WIN) 15 #if defined(OS_WIN)
21 16
22 #define INITGUID 17 #define INITGUID
23 18
24 #include <windows.h> 19 #include <windows.h>
25 #include <hidclass.h> 20 #include <hidclass.h>
26 21
27 extern "C" { 22 extern "C" {
(...skipping 13 matching lines...) Expand all
41 #pragma comment(lib, "setupapi.lib") 36 #pragma comment(lib, "setupapi.lib")
42 #pragma comment(lib, "hid.lib") 37 #pragma comment(lib, "hid.lib")
43 38
44 namespace device { 39 namespace device {
45 namespace { 40 namespace {
46 41
47 const char kHIDClass[] = "HIDClass"; 42 const char kHIDClass[] = "HIDClass";
48 43
49 } // namespace 44 } // namespace
50 45
51 HidServiceWin::HidServiceWin() { 46 HidServiceWin::HidServiceWin() { Enumerate(); }
52 initialized_ = Enumerate(); 47
53 }
54 HidServiceWin::~HidServiceWin() {} 48 HidServiceWin::~HidServiceWin() {}
55 49
56 bool HidServiceWin::Enumerate() { 50 void HidServiceWin::Enumerate() {
57 BOOL res; 51 BOOL res;
58 HDEVINFO device_info_set; 52 HDEVINFO device_info_set;
59 SP_DEVINFO_DATA devinfo_data; 53 SP_DEVINFO_DATA devinfo_data;
60 SP_DEVICE_INTERFACE_DATA device_interface_data; 54 SP_DEVICE_INTERFACE_DATA device_interface_data;
61 55
62 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA)); 56 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA));
63 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); 57 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
64 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 58 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
65 59
66 device_info_set = SetupDiGetClassDevs( 60 device_info_set = SetupDiGetClassDevs(
67 &GUID_DEVINTERFACE_HID, 61 &GUID_DEVINTERFACE_HID,
68 NULL, 62 NULL,
69 NULL, 63 NULL,
70 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 64 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
71 65
72 if (device_info_set == INVALID_HANDLE_VALUE) 66 if (device_info_set == INVALID_HANDLE_VALUE)
73 return false; 67 return;
74 68
75 for (int device_index = 0; 69 for (int device_index = 0;
76 SetupDiEnumDeviceInterfaces(device_info_set, 70 SetupDiEnumDeviceInterfaces(device_info_set,
77 NULL, 71 NULL,
78 &GUID_DEVINTERFACE_HID, 72 &GUID_DEVINTERFACE_HID,
79 device_index, 73 device_index,
80 &device_interface_data); 74 &device_interface_data);
81 device_index++) { 75 device_index++) {
82 DWORD required_size = 0; 76 DWORD required_size = 0;
83 77
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 char driver_name[256] = {0}; 118 char driver_name[256] = {0};
125 // Get bounded driver. 119 // Get bounded driver.
126 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, 120 res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
127 &devinfo_data, 121 &devinfo_data,
128 SPDRP_DRIVER, 122 SPDRP_DRIVER,
129 NULL, 123 NULL,
130 (PBYTE) driver_name, 124 (PBYTE) driver_name,
131 sizeof(driver_name) - 1, 125 sizeof(driver_name) - 1,
132 NULL); 126 NULL);
133 if (res) { 127 if (res) {
134 // Found the drive. 128 // Found the driver.
135 break; 129 break;
136 } 130 }
137 } 131 }
138 } 132 }
139 133
140 if (!res) 134 if (!res)
141 continue; 135 continue;
142 136
143 PlatformAddDevice(device_interface_detail_data->DevicePath); 137 PlatformAddDevice(device_interface_detail_data->DevicePath);
144 } 138 }
145
146 return true;
147 } 139 }
148 140
149 void HidServiceWin::PlatformAddDevice(std::string device_path) { 141 void HidServiceWin::PlatformAddDevice(const std::string& device_path) {
150 HidDeviceInfo device_info; 142 HidDeviceInfo device_info;
151 device_info.device_id = device_path; 143 device_info.device_id = device_path;
152 144
153 // Try to open the device. 145 // Try to open the device.
154 base::win::ScopedHandle device_handle( 146 base::win::ScopedHandle device_handle(
155 CreateFileA(device_path.c_str(), 147 CreateFileA(device_path.c_str(),
156 0, 148 0,
157 FILE_SHARE_READ | FILE_SHARE_WRITE, 149 FILE_SHARE_READ | FILE_SHARE_WRITE,
158 NULL, 150 NULL,
159 OPEN_EXISTING, 151 OPEN_EXISTING,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 202
211 if (HidD_GetProductString(device_handle.Get(), 203 if (HidD_GetProductString(device_handle.Get(),
212 str_property, 204 str_property,
213 sizeof(str_property))) { 205 sizeof(str_property))) {
214 device_info.product_name = base::SysWideToUTF8(str_property); 206 device_info.product_name = base::SysWideToUTF8(str_property);
215 } 207 }
216 208
217 HidService::AddDevice(device_info); 209 HidService::AddDevice(device_info);
218 } 210 }
219 211
220 void HidServiceWin::PlatformRemoveDevice(std::string device_path) { 212 void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) {
221 HidService::RemoveDevice(device_path); 213 HidService::RemoveDevice(device_path);
222 } 214 }
223 215
224 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { 216 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) {
225 Enumerate(); 217 Enumerate();
226 HidService::GetDevices(devices); 218 HidService::GetDevices(devices);
227 } 219 }
228 220
229 scoped_refptr<HidConnection> HidServiceWin::Connect(std::string device_id) { 221 scoped_refptr<HidConnection> HidServiceWin::Connect(
222 const std::string& device_id) {
230 if (!ContainsKey(devices_, device_id)) return NULL; 223 if (!ContainsKey(devices_, device_id)) return NULL;
231 scoped_refptr<HidConnectionWin> connection( 224 scoped_refptr<HidConnectionWin> connection(
232 new HidConnectionWin(devices_[device_id])); 225 new HidConnectionWin(devices_[device_id]));
233 if (!connection->available()) { 226 if (!connection->available()) {
234 LOG_GETLASTERROR(ERROR) << "Failed to open device."; 227 LOG_GETLASTERROR(ERROR) << "Failed to open device.";
235 return NULL; 228 return NULL;
236 } 229 }
237 return connection; 230 return connection;
238 } 231 }
239 232
240 } // namespace device 233 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698