Chromium Code Reviews| Index: device/core/device_info_query_win.h |
| diff --git a/device/core/device_info_query_win.h b/device/core/device_info_query_win.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bd6c7829b2f3ab972102dc675aeb3dc815d1011d |
| --- /dev/null |
| +++ b/device/core/device_info_query_win.h |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ |
| +#define DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ |
| + |
| +#include <windows.h> |
| +#include <setupapi.h> |
| + |
| +#include <string.h> |
| + |
| +#include <string> |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/string_util.h" |
| + |
| +namespace device { |
| + |
| +// Wrapper around a HDEVINFO and SP_DEVINFO_DATA that automatically destroys |
| +// them. |
| +class DeviceInfoQueryWin { |
| + public: |
| + DeviceInfoQueryWin() |
| + : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) { |
| + memset(&device_info_data_, 0, sizeof(device_info_data_)); |
| + } |
| + |
| + ~DeviceInfoQueryWin() { |
| + if (device_info_list_valid()) { |
| + if (device_info_data_.cbSize != 0) |
| + SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_); |
| + SetupDiDestroyDeviceInfoList(device_info_list_); |
| + } |
| + } |
| + |
| + bool AddDevice(const char* device_path) { |
| + return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path, 0, |
| + nullptr) != FALSE; |
| + } |
| + |
| + bool GetDeviceInfo() { |
| + DCHECK_EQ(0U, device_info_data_.cbSize); |
| + device_info_data_.cbSize = sizeof(device_info_data_); |
| + if (!SetupDiEnumDeviceInfo(device_info_list_, 0, &device_info_data_)) { |
| + // Clear cbSize to maintain the invariant. |
| + device_info_data_.cbSize = 0; |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + bool GetDeviceStringProperty(DWORD property, std::string* property_buffer) { |
| + DWORD property_reg_data_type; |
| + const size_t property_buffer_length = 512; |
| + if (!SetupDiGetDeviceRegistryPropertyA( |
| + device_info_list_, &device_info_data_, property, |
| + &property_reg_data_type, |
| + reinterpret_cast<PBYTE>( |
| + base::WriteInto(property_buffer, property_buffer_length)), |
| + static_cast<DWORD>(property_buffer_length), nullptr)) |
| + return false; |
| + |
| + if (property_reg_data_type != REG_SZ) |
| + return false; |
| + |
| + size_t eos = property_buffer->find('\0'); |
| + if (eos != std::string::npos) |
| + property_buffer->resize(eos); |
| + |
| + return true; |
| + } |
| + |
| + bool device_info_list_valid() { |
| + return device_info_list_ != INVALID_HANDLE_VALUE; |
| + } |
| + |
| + 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.
|
| + |
| + 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.
|
| + |
| + private: |
| + HDEVINFO device_info_list_ = INVALID_HANDLE_VALUE; |
| + // When device_info_data_.cbSize != 0, |device_info_data_| is valid. |
| + SP_DEVINFO_DATA device_info_data_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DeviceInfoQueryWin); |
| +}; |
| + |
| +} // namespace device |
| + |
| +#endif // DEVICE_CORE_DEVICE_INFO_QUERY_WIN_H_ |