| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 = GenerateDeviceId(); |
| 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, |
| 160 FILE_FLAG_OVERLAPPED, | 152 FILE_FLAG_OVERLAPPED, |
| 161 0)); | 153 0)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 sizeof(str_property))) { | 199 sizeof(str_property))) { |
| 208 device_info.serial_number = base::SysWideToUTF8(str_property); | 200 device_info.serial_number = base::SysWideToUTF8(str_property); |
| 209 } | 201 } |
| 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 if (!platform_resource_map_.Add(device_info.device_id, device_path)) |
| 210 return; |
| 211 AddDevice(device_info); |
| 218 } | 212 } |
| 219 | 213 |
| 220 void HidServiceWin::PlatformRemoveDevice(std::string device_path) { | 214 void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) { |
| 221 HidService::RemoveDevice(device_path); | 215 HidDeviceId device_id; |
| 216 if (!platform_resource_map_.GetIdByResource(device_path, &device_id)) |
| 217 return; |
| 218 platform_resource_map_.RemoveById(device_id); |
| 219 RemoveDevice(device_id); |
| 222 } | 220 } |
| 223 | 221 |
| 224 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { | 222 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { |
| 225 Enumerate(); | 223 Enumerate(); |
| 226 HidService::GetDevices(devices); | 224 HidService::GetDevices(devices); |
| 227 } | 225 } |
| 228 | 226 |
| 229 scoped_refptr<HidConnection> HidServiceWin::Connect(std::string device_id) { | 227 scoped_refptr<HidConnection> HidServiceWin::Connect(HidDeviceId device_id) { |
| 230 if (!ContainsKey(devices_, device_id)) return NULL; | 228 HidDeviceInfo device_info; |
| 229 if (!GetInfo(device_id, &device_info)) |
| 230 return NULL; |
| 231 |
| 232 std::string device_path; |
| 233 if (!platform_resource_map_.GetResourceById(device_id, &device_path)) |
| 234 return NULL; |
| 235 |
| 231 scoped_refptr<HidConnectionWin> connection( | 236 scoped_refptr<HidConnectionWin> connection( |
| 232 new HidConnectionWin(devices_[device_id])); | 237 new HidConnectionWin(device_info, device_path)); |
| 233 if (!connection->available()) { | 238 if (!connection->available()) { |
| 234 LOG_GETLASTERROR(ERROR) << "Failed to open device."; | 239 LOG_GETLASTERROR(ERROR) << "Failed to open device."; |
| 235 return NULL; | 240 return NULL; |
| 236 } | 241 } |
| 237 return connection; | 242 return connection; |
| 238 } | 243 } |
| 239 | 244 |
| 240 } // namespace device | 245 } // namespace device |
| OLD | NEW |