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 #ifndef DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ | |
6 #define DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ | |
7 | |
8 #include <windows.h> | |
9 #include <setupapi.h> | |
10 | |
11 #include <string.h> | |
12 | |
13 #include <string> | |
14 | |
15 #include "base/macros.h" | |
16 #include "base/strings/string_util.h" | |
17 | |
18 namespace device { | |
19 | |
20 // Wrapper around a HDEVINFO and SP_DEVINFO_DATA that automatically destroys | |
21 // them. | |
22 class DeviceInfoQueryWin { | |
23 public: | |
24 DeviceInfoQueryWin() | |
25 : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) { | |
26 memset(&device_info_data_, 0, sizeof(device_info_data_)); | |
27 } | |
28 | |
29 ~DeviceInfoQueryWin() { | |
30 if (device_info_list_ != INVALID_HANDLE_VALUE) { | |
grt (UTC plus 2)
2015/11/16 21:41:56
nit: if (device_info_list_valid()) {
juncai
2015/11/16 23:37:51
Done.
| |
31 if (device_info_data_.cbSize != 0) | |
32 SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_); | |
33 SetupDiDestroyDeviceInfoList(device_info_list_); | |
34 } | |
35 } | |
36 | |
37 bool AddDevice(const char* device_path) { | |
38 return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path, 0, | |
39 nullptr) != FALSE; | |
40 } | |
41 | |
42 bool GetDeviceInfo() { | |
43 DCHECK_EQ(0U, device_info_data_.cbSize); | |
44 device_info_data_.cbSize = sizeof(device_info_data_); | |
45 if (!SetupDiEnumDeviceInfo(device_info_list_, 0, &device_info_data_)) { | |
46 device_info_data_.cbSize = 0; | |
47 return false; | |
48 } | |
49 return true; | |
50 } | |
51 | |
52 bool GetDeviceStringProperty(DWORD property, | |
53 PDWORD property_reg_data_type, | |
grt (UTC plus 2)
2015/11/16 21:41:56
the callers of this only care about REG_SZ. why no
juncai
2015/11/16 23:37:51
Done.
| |
54 std::string* property_buffer) { | |
55 size_t property_buffer_length = 512; | |
grt (UTC plus 2)
2015/11/16 21:41:56
nit: const
juncai
2015/11/16 23:37:51
Done.
| |
56 return SetupDiGetDeviceRegistryPropertyA( | |
57 device_info_list_, &device_info_data_, property, | |
58 property_reg_data_type, | |
59 reinterpret_cast<PBYTE>(base::WriteInto( | |
60 property_buffer, property_buffer_length + 1)), | |
grt (UTC plus 2)
2015/11/16 21:41:56
why add 1?
juncai
2015/11/16 23:37:51
Done.
| |
61 property_buffer_length, nullptr) != FALSE; | |
grt (UTC plus 2)
2015/11/16 21:41:56
the string in |property_buffer| isn't sized proper
juncai
2015/11/16 23:37:51
Done.
| |
62 } | |
63 | |
64 bool device_info_list_valid() { | |
65 return device_info_list_ != INVALID_HANDLE_VALUE; | |
66 } | |
67 | |
68 HDEVINFO device_info_list() { return device_info_list_; } | |
69 | |
70 PSP_DEVINFO_DATA device_info_data() { return &device_info_data_; } | |
71 | |
72 private: | |
73 HDEVINFO device_info_list_ = INVALID_HANDLE_VALUE; | |
74 SP_DEVINFO_DATA device_info_data_; | |
grt (UTC plus 2)
2015/11/16 21:41:56
nit: document the invariant that device_info_data_
juncai
2015/11/16 23:37:51
Done.
| |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(DeviceInfoQueryWin); | |
77 }; | |
78 | |
79 } // namespace device | |
80 | |
81 #endif // DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ | |
OLD | NEW |