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 <devguid.h> | 7 #include <devguid.h> |
8 #include <setupapi.h> | 8 #include <setupapi.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <windows.h> | 10 #include <windows.h> |
11 | 11 |
12 #include <memory> | 12 #include <memory> |
| 13 #include <unordered_set> |
13 | 14 |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "base/win/registry.h" | 20 #include "base/win/registry.h" |
20 #include "third_party/re2/src/re2/re2.h" | 21 #include "third_party/re2/src/re2/re2.h" |
21 | 22 |
22 namespace device { | 23 namespace device { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 158 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
158 return std::unique_ptr<SerialDeviceEnumerator>( | 159 return std::unique_ptr<SerialDeviceEnumerator>( |
159 new SerialDeviceEnumeratorWin()); | 160 new SerialDeviceEnumeratorWin()); |
160 } | 161 } |
161 | 162 |
162 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} | 163 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} |
163 | 164 |
164 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} | 165 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} |
165 | 166 |
166 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { | 167 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { |
167 mojo::Array<serial::DeviceInfoPtr> newDevices = GetDevicesNew(); | 168 mojo::Array<serial::DeviceInfoPtr> devices = GetDevicesNew(); |
168 mojo::Array<serial::DeviceInfoPtr> oldDevices = GetDevicesOld(); | 169 mojo::Array<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); |
169 | 170 |
170 UMA_HISTOGRAM_SPARSE_SLOWLY( | 171 UMA_HISTOGRAM_SPARSE_SLOWLY( |
171 "Hardware.Serial.NewMinusOldDeviceListSize", | 172 "Hardware.Serial.NewMinusOldDeviceListSize", |
172 Clamp((int)newDevices.size() - (int)oldDevices.size(), -10, 10)); | 173 Clamp(devices.size() - old_devices.size(), -10, 10)); |
173 | 174 |
174 // Add devices found from both the new and old methods of enumeration. If a | 175 // Add devices found from both the new and old methods of enumeration. If a |
175 // device is found using both the new and the old enumeration method, then we | 176 // device is found using both the new and the old enumeration method, then we |
176 // take the device from the new enumeration method because it's able to | 177 // take the device from the new enumeration method because it's able to |
177 // collect more information. We do this by inserting the new devices first, | 178 // collect more information. We do this by inserting the new devices first, |
178 // because insertions are ignored if the key already exists. | 179 // because insertions are ignored if the key already exists. |
179 mojo::Map<mojo::String, serial::DeviceInfoPtr> deviceMap; | 180 std::unordered_set<std::string> devices_seen; |
180 for (unsigned long i = 0; i < newDevices.size(); i++) { | 181 for (const auto& device : devices) { |
181 deviceMap.insert(newDevices[i]->path, newDevices[i].Clone()); | 182 bool inserted = devices_seen.insert(device->path).second; |
| 183 DCHECK(inserted); |
182 } | 184 } |
183 for (unsigned long i = 0; i < oldDevices.size(); i++) { | 185 for (auto& device : old_devices) { |
184 deviceMap.insert(oldDevices[i]->path, oldDevices[i].Clone()); | 186 if (devices_seen.insert(device->path).second) |
| 187 devices.push_back(std::move(device)); |
185 } | 188 } |
186 | |
187 mojo::Array<mojo::String> paths; | |
188 mojo::Array<serial::DeviceInfoPtr> devices; | |
189 deviceMap.DecomposeMapTo(&paths, &devices); | |
190 | |
191 return devices; | 189 return devices; |
192 } | 190 } |
193 | 191 |
194 } // namespace device | 192 } // namespace device |
OLD | NEW |