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