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_mac.h" | 5 #include "device/serial/serial_device_enumerator_mac.h" |
6 | 6 |
7 #include <IOKit/serial/IOSerialKeys.h> | 7 #include <IOKit/serial/IOSerialKeys.h> |
8 #include <IOKit/usb/IOUSBLib.h> | 8 #include <IOKit/usb/IOUSBLib.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <memory> | 12 #include <memory> |
| 13 #include <unordered_set> |
13 #include <utility> | 14 #include <utility> |
14 | 15 |
15 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
18 #include "base/mac/scoped_cftyperef.h" | 19 #include "base/mac/scoped_cftyperef.h" |
19 #include "base/mac/scoped_ioobject.h" | 20 #include "base/mac/scoped_ioobject.h" |
20 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
21 #include "base/strings/pattern.h" | 22 #include "base/strings/pattern.h" |
22 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 208 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
208 return std::unique_ptr<SerialDeviceEnumerator>( | 209 return std::unique_ptr<SerialDeviceEnumerator>( |
209 new SerialDeviceEnumeratorMac()); | 210 new SerialDeviceEnumeratorMac()); |
210 } | 211 } |
211 | 212 |
212 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} | 213 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} |
213 | 214 |
214 SerialDeviceEnumeratorMac::~SerialDeviceEnumeratorMac() {} | 215 SerialDeviceEnumeratorMac::~SerialDeviceEnumeratorMac() {} |
215 | 216 |
216 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorMac::GetDevices() { | 217 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorMac::GetDevices() { |
217 mojo::Array<serial::DeviceInfoPtr> newDevices = GetDevicesNew(); | 218 mojo::Array<serial::DeviceInfoPtr> devices = GetDevicesNew(); |
218 mojo::Array<serial::DeviceInfoPtr> oldDevices = GetDevicesOld(); | 219 mojo::Array<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); |
219 | 220 |
220 UMA_HISTOGRAM_SPARSE_SLOWLY( | 221 UMA_HISTOGRAM_SPARSE_SLOWLY( |
221 "Hardware.Serial.NewMinusOldDeviceListSize", | 222 "Hardware.Serial.NewMinusOldDeviceListSize", |
222 Clamp(newDevices.size() - oldDevices.size(), -10, 10)); | 223 Clamp(devices.size() - old_devices.size(), -10, 10)); |
223 | 224 |
224 // Add devices found from both the new and old methods of enumeration. If a | 225 // Add devices found from both the new and old methods of enumeration. If a |
225 // device is found using both the new and the old enumeration method, then we | 226 // device is found using both the new and the old enumeration method, then we |
226 // take the device from the new enumeration method because it's able to | 227 // take the device from the new enumeration method because it's able to |
227 // collect more information. We do this by inserting the new devices first, | 228 // collect more information. We do this by inserting the new devices first, |
228 // because insertions are ignored if the key already exists. | 229 // because insertions are ignored if the key already exists. |
229 mojo::Map<mojo::String, serial::DeviceInfoPtr> deviceMap; | 230 std::unordered_set<std::string> devices_seen; |
230 for (unsigned long i = 0; i < newDevices.size(); i++) { | 231 for (const auto& device : devices) { |
231 deviceMap.insert(newDevices[i]->path, newDevices[i].Clone()); | 232 bool inserted = devices_seen.insert(device->path).second; |
| 233 DCHECK(inserted); |
232 } | 234 } |
233 for (unsigned long i = 0; i < oldDevices.size(); i++) { | 235 for (auto& device : old_devices) { |
234 deviceMap.insert(oldDevices[i]->path, oldDevices[i].Clone()); | 236 if (devices_seen.insert(device->path).second) |
| 237 devices.push_back(std::move(device)); |
235 } | 238 } |
236 | |
237 mojo::Array<mojo::String> paths; | |
238 mojo::Array<serial::DeviceInfoPtr> devices; | |
239 deviceMap.DecomposeMapTo(&paths, &devices); | |
240 | |
241 return devices; | 239 return devices; |
242 } | 240 } |
243 | 241 |
244 } // namespace device | 242 } // namespace device |
OLD | NEW |