Chromium Code Reviews| 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_valid()) { | |
| 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 // Clear cbSize to maintain the invariant. | |
| 47 device_info_data_.cbSize = 0; | |
| 48 return false; | |
| 49 } | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 bool GetDeviceStringProperty(DWORD property, std::string* property_buffer) { | |
| 54 DWORD property_reg_data_type; | |
| 55 const size_t property_buffer_length = 512; | |
| 56 if (!SetupDiGetDeviceRegistryPropertyA( | |
| 57 device_info_list_, &device_info_data_, property, | |
| 58 &property_reg_data_type, | |
| 59 reinterpret_cast<PBYTE>( | |
| 60 base::WriteInto(property_buffer, property_buffer_length)), | |
| 61 static_cast<DWORD>(property_buffer_length), nullptr)) | |
| 62 return false; | |
| 63 | |
| 64 if (property_reg_data_type != REG_SZ) | |
| 65 return false; | |
| 66 | |
| 67 size_t eos = property_buffer->find('\0'); | |
| 68 if (eos != std::string::npos) | |
| 69 property_buffer->resize(eos); | |
| 70 | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 bool device_info_list_valid() { | |
| 75 return device_info_list_ != INVALID_HANDLE_VALUE; | |
| 76 } | |
| 77 | |
| 78 HDEVINFO device_info_list() { return device_info_list_; } | |
|
grt (UTC plus 2)
2015/11/17 16:06:56
unused; remove
juncai
2015/11/17 19:14:07
Done.
| |
| 79 | |
| 80 PSP_DEVINFO_DATA device_info_data() { return &device_info_data_; } | |
|
grt (UTC plus 2)
2015/11/17 16:06:56
unused; remove
juncai
2015/11/17 19:14:07
Done.
| |
| 81 | |
| 82 private: | |
| 83 HDEVINFO device_info_list_ = INVALID_HANDLE_VALUE; | |
| 84 // When device_info_data_.cbSize != 0, |device_info_data_| is valid. | |
| 85 SP_DEVINFO_DATA device_info_data_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(DeviceInfoQueryWin); | |
| 88 }; | |
| 89 | |
| 90 } // namespace device | |
| 91 | |
| 92 #endif // DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ | |
| OLD | NEW |