| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/serial/serial_device_enumerator_win.h" | 5 #include "device/serial/serial_device_enumerator_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include <devguid.h> | 7 #include <devguid.h> |
| 10 #include <setupapi.h> | 8 #include <setupapi.h> |
| 11 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <windows.h> |
| 12 | 11 |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include <memory> |
| 13 |
| 14 #include "base/metrics/sparse_histogram.h" | 14 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/win/registry.h" | 19 #include "base/win/registry.h" |
| 20 #include "third_party/re2/src/re2/re2.h" | 20 #include "third_party/re2/src/re2/re2.h" |
| 21 | 21 |
| 22 namespace device { | 22 namespace device { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Searches the specified device info for a property with the specified key, | 26 // Searches the specified device info for a property with the specified key, |
| 27 // assigns the result to value, and returns whether the operation was | 27 // assigns the result to value, and returns whether the operation was |
| 28 // successful. | 28 // successful. |
| 29 bool GetProperty(HDEVINFO dev_info, | 29 bool GetProperty(HDEVINFO dev_info, |
| 30 SP_DEVINFO_DATA dev_info_data, | 30 SP_DEVINFO_DATA dev_info_data, |
| 31 const int key, | 31 const int key, |
| 32 std::string* value) { | 32 std::string* value) { |
| 33 // We don't know how much space the property's value will take up, so we call | 33 // We don't know how much space the property's value will take up, so we call |
| 34 // the property retrieval function once to fetch the size of the required | 34 // the property retrieval function once to fetch the size of the required |
| 35 // value buffer, then again once we've allocated a sufficiently large buffer. | 35 // value buffer, then again once we've allocated a sufficiently large buffer. |
| 36 DWORD buffer_size = 0; | 36 DWORD buffer_size = 0; |
| 37 SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, key, nullptr, | 37 SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, key, nullptr, |
| 38 nullptr, buffer_size, &buffer_size); | 38 nullptr, buffer_size, &buffer_size); |
| 39 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) | 39 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 scoped_ptr<wchar_t[]> buffer(new wchar_t[buffer_size]); | 42 std::unique_ptr<wchar_t[]> buffer(new wchar_t[buffer_size]); |
| 43 if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, key, nullptr, | 43 if (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, key, nullptr, |
| 44 reinterpret_cast<PBYTE>(buffer.get()), | 44 reinterpret_cast<PBYTE>(buffer.get()), |
| 45 buffer_size, nullptr)) | 45 buffer_size, nullptr)) |
| 46 return false; | 46 return false; |
| 47 | 47 |
| 48 *value = base::WideToUTF8(buffer.get()); | 48 *value = base::WideToUTF8(buffer.get()); |
| 49 return true; | 49 return true; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Searches for the COM port in the device's friendly name, assigns its value to | 52 // Searches for the COM port in the device's friendly name, assigns its value to |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); | 147 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); |
| 148 info->path = base::UTF16ToASCII(iter_key.Value()); | 148 info->path = base::UTF16ToASCII(iter_key.Value()); |
| 149 devices.push_back(std::move(info)); | 149 devices.push_back(std::move(info)); |
| 150 } | 150 } |
| 151 return devices; | 151 return devices; |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace | 154 } // namespace |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 157 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
| 158 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorWin()); | 158 return std::unique_ptr<SerialDeviceEnumerator>( |
| 159 new SerialDeviceEnumeratorWin()); |
| 159 } | 160 } |
| 160 | 161 |
| 161 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} | 162 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} |
| 162 | 163 |
| 163 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} | 164 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} |
| 164 | 165 |
| 165 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { | 166 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { |
| 166 mojo::Array<serial::DeviceInfoPtr> newDevices = GetDevicesNew(); | 167 mojo::Array<serial::DeviceInfoPtr> newDevices = GetDevicesNew(); |
| 167 mojo::Array<serial::DeviceInfoPtr> oldDevices = GetDevicesOld(); | 168 mojo::Array<serial::DeviceInfoPtr> oldDevices = GetDevicesOld(); |
| 168 | 169 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 184 } | 185 } |
| 185 | 186 |
| 186 mojo::Array<mojo::String> paths; | 187 mojo::Array<mojo::String> paths; |
| 187 mojo::Array<serial::DeviceInfoPtr> devices; | 188 mojo::Array<serial::DeviceInfoPtr> devices; |
| 188 deviceMap.DecomposeMapTo(&paths, &devices); | 189 deviceMap.DecomposeMapTo(&paths, &devices); |
| 189 | 190 |
| 190 return devices; | 191 return devices; |
| 191 } | 192 } |
| 192 | 193 |
| 193 } // namespace device | 194 } // namespace device |
| OLD | NEW |