Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: device/base/device_info_query_win.cc

Issue 2700563007: Clean up DeviceInfoQueryWin. (Closed)
Patch Set: Add INITGUID to serial_io_handler_win.cc. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/base/device_info_query_win.h ('k') | device/serial/serial_io_handler_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/base/device_info_query_win.h" 5 #include "device/base/device_info_query_win.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
11 12
12 namespace device { 13 namespace device {
13 14
14 DeviceInfoQueryWin::DeviceInfoQueryWin() 15 DeviceInfoQueryWin::DeviceInfoQueryWin()
15 : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) { 16 : device_info_list_(SetupDiCreateDeviceInfoList(nullptr, nullptr)) {
16 memset(&device_info_data_, 0, sizeof(device_info_data_)); 17 memset(&device_info_data_, 0, sizeof(device_info_data_));
17 } 18 }
18 19
19 DeviceInfoQueryWin::~DeviceInfoQueryWin() { 20 DeviceInfoQueryWin::~DeviceInfoQueryWin() {
20 if (device_info_list_valid()) { 21 if (device_info_list_valid()) {
21 // Release |device_info_data_| only when it is valid. 22 // Release |device_info_data_| only when it is valid.
22 if (device_info_data_.cbSize != 0) 23 if (device_info_data_.cbSize != 0)
23 SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_); 24 SetupDiDeleteDeviceInfo(device_info_list_, &device_info_data_);
24 SetupDiDestroyDeviceInfoList(device_info_list_); 25 SetupDiDestroyDeviceInfoList(device_info_list_);
25 } 26 }
26 } 27 }
27 28
28 bool DeviceInfoQueryWin::AddDevice(const char* device_path) { 29 bool DeviceInfoQueryWin::AddDevice(const std::string& device_path) {
29 return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path, 0, 30 return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path.c_str(), 0,
30 nullptr) != FALSE; 31 nullptr) != FALSE;
31 } 32 }
32 33
33 bool DeviceInfoQueryWin::GetDeviceInfo() { 34 bool DeviceInfoQueryWin::GetDeviceInfo() {
34 DCHECK_EQ(0U, device_info_data_.cbSize); 35 DCHECK_EQ(0U, device_info_data_.cbSize);
35 device_info_data_.cbSize = sizeof(device_info_data_); 36 device_info_data_.cbSize = sizeof(device_info_data_);
36 if (!SetupDiEnumDeviceInfo(device_info_list_, 0, &device_info_data_)) { 37 if (!SetupDiEnumDeviceInfo(device_info_list_, 0, &device_info_data_)) {
37 // Clear cbSize to maintain the invariant. 38 // Clear cbSize to maintain the invariant.
38 device_info_data_.cbSize = 0; 39 device_info_data_.cbSize = 0;
39 return false; 40 return false;
40 } 41 }
41 return true; 42 return true;
42 } 43 }
43 44
44 bool DeviceInfoQueryWin::GetDeviceStringProperty(DWORD property, 45 bool DeviceInfoQueryWin::GetDeviceStringProperty(const DEVPROPKEY& property,
45 std::string* property_buffer) { 46 std::string* property_buffer) {
46 DWORD property_reg_data_type; 47 DEVPROPTYPE property_type;
47 const size_t property_buffer_length = 512; 48 DWORD required_size;
48 if (!SetupDiGetDeviceRegistryPropertyA( 49 if (SetupDiGetDeviceProperty(device_info_list_, &device_info_data_, &property,
49 device_info_list_, &device_info_data_, property, 50 &property_type, nullptr, 0, &required_size, 0) ||
50 &property_reg_data_type, 51 GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
51 reinterpret_cast<PBYTE>( 52 property_type != DEVPROP_TYPE_STRING) {
52 base::WriteInto(property_buffer, property_buffer_length)),
53 static_cast<DWORD>(property_buffer_length), nullptr))
54 return false; 53 return false;
54 }
55 55
56 if (property_reg_data_type != REG_SZ) 56 std::wstring wide_buffer;
57 if (!SetupDiGetDeviceProperty(
58 device_info_list_, &device_info_data_, &property, &property_type,
59 reinterpret_cast<PBYTE>(base::WriteInto(&wide_buffer, required_size)),
60 required_size, nullptr, 0)) {
57 return false; 61 return false;
62 }
58 63
59 // Shrink |property_buffer| down to its correct size. 64 *property_buffer = base::SysWideToUTF8(wide_buffer);
60 size_t eos = property_buffer->find('\0');
61 if (eos != std::string::npos)
62 property_buffer->resize(eos);
63
64 return true; 65 return true;
65 } 66 }
66 67
67 } // namespace device 68 } // namespace device
OLDNEW
« no previous file with comments | « device/base/device_info_query_win.h ('k') | device/serial/serial_io_handler_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698