summaryrefslogtreecommitdiffstats
path: root/device/core
diff options
context:
space:
mode:
Diffstat (limited to 'device/core')
-rw-r--r--device/core/BUILD.gn2
-rw-r--r--device/core/core.gyp2
-rw-r--r--device/core/device_info_query_win.cc66
-rw-r--r--device/core/device_info_query_win.h49
4 files changed, 119 insertions, 0 deletions
diff --git a/device/core/BUILD.gn b/device/core/BUILD.gn
index e85acb6..6f55190 100644
--- a/device/core/BUILD.gn
+++ b/device/core/BUILD.gn
@@ -8,6 +8,8 @@ component("core") {
sources = [
"device_client.cc",
"device_client.h",
+ "device_info_query_win.cc",
+ "device_info_query_win.h",
"device_monitor_win.cc",
"device_monitor_win.h",
]
diff --git a/device/core/core.gyp b/device/core/core.gyp
index d27073d..6002a5f 100644
--- a/device/core/core.gyp
+++ b/device/core/core.gyp
@@ -19,6 +19,8 @@
'sources': [
'device_client.cc',
'device_client.h',
+ 'device_info_query_win.cc',
+ 'device_info_query_win.h',
'device_monitor_win.cc',
'device_monitor_win.h',
],
diff --git a/device/core/device_info_query_win.cc b/device/core/device_info_query_win.cc
new file mode 100644
index 0000000..4d62b32
--- /dev/null
+++ b/device/core/device_info_query_win.cc
@@ -0,0 +1,66 @@
+// 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.
+
+#include "device/core/device_info_query_win.h"
+
+#include <string.h>
+
+#include "base/strings/string_util.h"
+
+namespace device {
+
+DeviceInfoQueryWin::DeviceInfoQueryWin()
+ : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) {
+ memset(&device_info_data_, 0, sizeof(device_info_data_));
+}
+
+DeviceInfoQueryWin::~DeviceInfoQueryWin() {
+ if (device_info_list_valid()) {
+ // Release |device_info_data_| only when it is valid.
+ if (device_info_data_.cbSize != 0)
+ SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_);
+ SetupDiDestroyDeviceInfoList(device_info_list_);
+ }
+}
+
+bool DeviceInfoQueryWin::AddDevice(const char* device_path) {
+ return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path, 0,
+ nullptr) != FALSE;
+}
+
+bool DeviceInfoQueryWin::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 DeviceInfoQueryWin::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;
+
+ // Shrink |property_buffer| down to its correct size.
+ size_t eos = property_buffer->find('\0');
+ if (eos != std::string::npos)
+ property_buffer->resize(eos);
+
+ return true;
+}
+
+} // namespace device
diff --git a/device/core/device_info_query_win.h b/device/core/device_info_query_win.h
new file mode 100644
index 0000000..2e399e4
--- /dev/null
+++ b/device/core/device_info_query_win.h
@@ -0,0 +1,49 @@
+// 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>
+
+#include "base/macros.h"
+#include "device/core/device_core_export.h"
+
+namespace device {
+
+// Wraps HDEVINFO and SP_DEVINFO_DATA into a class that can automatically
+// release them. Provides interfaces that can add a device using its
+// device path, get device info and get device string property.
+class DEVICE_CORE_EXPORT DeviceInfoQueryWin {
+ public:
+ DeviceInfoQueryWin();
+ ~DeviceInfoQueryWin();
+
+ // Add a device to |device_info_list_| using its |device_path| so that
+ // its device info can be retrieved.
+ bool AddDevice(const char* device_path);
+ // Get the device info and store it into |device_info_data_|, this function
+ // should be called at most once.
+ bool GetDeviceInfo();
+ // Get device string property and store it into |property_buffer|.
+ bool GetDeviceStringProperty(DWORD property, std::string* property_buffer);
+
+ bool device_info_list_valid() {
+ return device_info_list_ != INVALID_HANDLE_VALUE;
+ }
+
+ 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_