OLD | NEW |
| (Empty) |
1 // Copyright 2015 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. | |
4 | |
5 #include "device/core/device_info_query_win.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 | |
11 namespace device { | |
12 | |
13 DeviceInfoQueryWin::DeviceInfoQueryWin() | |
14 : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) { | |
15 memset(&device_info_data_, 0, sizeof(device_info_data_)); | |
16 } | |
17 | |
18 DeviceInfoQueryWin::~DeviceInfoQueryWin() { | |
19 if (device_info_list_valid()) { | |
20 // Release |device_info_data_| only when it is valid. | |
21 if (device_info_data_.cbSize != 0) | |
22 SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_); | |
23 SetupDiDestroyDeviceInfoList(device_info_list_); | |
24 } | |
25 } | |
26 | |
27 bool DeviceInfoQueryWin::AddDevice(const char* device_path) { | |
28 return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path, 0, | |
29 nullptr) != FALSE; | |
30 } | |
31 | |
32 bool DeviceInfoQueryWin::GetDeviceInfo() { | |
33 DCHECK_EQ(0U, device_info_data_.cbSize); | |
34 device_info_data_.cbSize = sizeof(device_info_data_); | |
35 if (!SetupDiEnumDeviceInfo(device_info_list_, 0, &device_info_data_)) { | |
36 // Clear cbSize to maintain the invariant. | |
37 device_info_data_.cbSize = 0; | |
38 return false; | |
39 } | |
40 return true; | |
41 } | |
42 | |
43 bool DeviceInfoQueryWin::GetDeviceStringProperty(DWORD property, | |
44 std::string* property_buffer) { | |
45 DWORD property_reg_data_type; | |
46 const size_t property_buffer_length = 512; | |
47 if (!SetupDiGetDeviceRegistryPropertyA( | |
48 device_info_list_, &device_info_data_, property, | |
49 &property_reg_data_type, | |
50 reinterpret_cast<PBYTE>( | |
51 base::WriteInto(property_buffer, property_buffer_length)), | |
52 static_cast<DWORD>(property_buffer_length), nullptr)) | |
53 return false; | |
54 | |
55 if (property_reg_data_type != REG_SZ) | |
56 return false; | |
57 | |
58 // Shrink |property_buffer| down to its correct size. | |
59 size_t eos = property_buffer->find('\0'); | |
60 if (eos != std::string::npos) | |
61 property_buffer->resize(eos); | |
62 | |
63 return true; | |
64 } | |
65 | |
66 } // namespace device | |
OLD | NEW |