| 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 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } | 94 } |
| 95 | 95 |
| 96 // Returns value clamped to the range of [min, max]. | 96 // Returns value clamped to the range of [min, max]. |
| 97 int Clamp(int value, int min, int max) { | 97 int Clamp(int value, int min, int max) { |
| 98 return std::min(std::max(value, min), max); | 98 return std::min(std::max(value, min), max); |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Returns an array of devices as retrieved through the new method of | 101 // Returns an array of devices as retrieved through the new method of |
| 102 // enumerating serial devices (IOKit). This new method gives more information | 102 // enumerating serial devices (IOKit). This new method gives more information |
| 103 // about the devices than the old method. | 103 // about the devices than the old method. |
| 104 mojo::Array<serial::DeviceInfoPtr> GetDevicesNew() { | 104 std::vector<serial::DeviceInfoPtr> GetDevicesNew() { |
| 105 mojo::Array<serial::DeviceInfoPtr> devices; | 105 std::vector<serial::DeviceInfoPtr> devices; |
| 106 | 106 |
| 107 // Make a service query to find all serial devices. | 107 // Make a service query to find all serial devices. |
| 108 CFMutableDictionaryRef matchingDict = | 108 CFMutableDictionaryRef matchingDict = |
| 109 IOServiceMatching(kIOSerialBSDServiceValue); | 109 IOServiceMatching(kIOSerialBSDServiceValue); |
| 110 if (!matchingDict) | 110 if (!matchingDict) |
| 111 return devices; | 111 return devices; |
| 112 | 112 |
| 113 io_iterator_t it; | 113 io_iterator_t it; |
| 114 kern_return_t kr = | 114 kern_return_t kr = |
| 115 IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &it); | 115 IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &it); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 devices.push_back(std::move(callout_info)); | 160 devices.push_back(std::move(callout_info)); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 return devices; | 164 return devices; |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Returns an array of devices as retrieved through the old method of | 167 // Returns an array of devices as retrieved through the old method of |
| 168 // enumerating serial devices (pattern matching in /dev/). This old method gives | 168 // enumerating serial devices (pattern matching in /dev/). This old method gives |
| 169 // less information about the devices than the new method. | 169 // less information about the devices than the new method. |
| 170 mojo::Array<serial::DeviceInfoPtr> GetDevicesOld() { | 170 std::vector<serial::DeviceInfoPtr> GetDevicesOld() { |
| 171 const base::FilePath kDevRoot("/dev"); | 171 const base::FilePath kDevRoot("/dev"); |
| 172 const int kFilesAndSymLinks = | 172 const int kFilesAndSymLinks = |
| 173 base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS; | 173 base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS; |
| 174 | 174 |
| 175 std::set<std::string> valid_patterns; | 175 std::set<std::string> valid_patterns; |
| 176 valid_patterns.insert("/dev/*Bluetooth*"); | 176 valid_patterns.insert("/dev/*Bluetooth*"); |
| 177 valid_patterns.insert("/dev/*Modem*"); | 177 valid_patterns.insert("/dev/*Modem*"); |
| 178 valid_patterns.insert("/dev/*bluetooth*"); | 178 valid_patterns.insert("/dev/*bluetooth*"); |
| 179 valid_patterns.insert("/dev/*modem*"); | 179 valid_patterns.insert("/dev/*modem*"); |
| 180 valid_patterns.insert("/dev/*serial*"); | 180 valid_patterns.insert("/dev/*serial*"); |
| 181 valid_patterns.insert("/dev/tty.*"); | 181 valid_patterns.insert("/dev/tty.*"); |
| 182 valid_patterns.insert("/dev/cu.*"); | 182 valid_patterns.insert("/dev/cu.*"); |
| 183 | 183 |
| 184 mojo::Array<serial::DeviceInfoPtr> devices; | 184 std::vector<serial::DeviceInfoPtr> devices; |
| 185 base::FileEnumerator enumerator(kDevRoot, false, kFilesAndSymLinks); | 185 base::FileEnumerator enumerator(kDevRoot, false, kFilesAndSymLinks); |
| 186 do { | 186 do { |
| 187 const base::FilePath next_device_path(enumerator.Next()); | 187 const base::FilePath next_device_path(enumerator.Next()); |
| 188 const std::string next_device = next_device_path.value(); | 188 const std::string next_device = next_device_path.value(); |
| 189 if (next_device.empty()) | 189 if (next_device.empty()) |
| 190 break; | 190 break; |
| 191 | 191 |
| 192 std::set<std::string>::const_iterator i = valid_patterns.begin(); | 192 std::set<std::string>::const_iterator i = valid_patterns.begin(); |
| 193 for (; i != valid_patterns.end(); ++i) { | 193 for (; i != valid_patterns.end(); ++i) { |
| 194 if (base::MatchPattern(next_device, *i)) { | 194 if (base::MatchPattern(next_device, *i)) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 207 // static | 207 // static |
| 208 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 208 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
| 209 return std::unique_ptr<SerialDeviceEnumerator>( | 209 return std::unique_ptr<SerialDeviceEnumerator>( |
| 210 new SerialDeviceEnumeratorMac()); | 210 new SerialDeviceEnumeratorMac()); |
| 211 } | 211 } |
| 212 | 212 |
| 213 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} | 213 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} |
| 214 | 214 |
| 215 SerialDeviceEnumeratorMac::~SerialDeviceEnumeratorMac() {} | 215 SerialDeviceEnumeratorMac::~SerialDeviceEnumeratorMac() {} |
| 216 | 216 |
| 217 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorMac::GetDevices() { | 217 std::vector<serial::DeviceInfoPtr> SerialDeviceEnumeratorMac::GetDevices() { |
| 218 mojo::Array<serial::DeviceInfoPtr> devices = GetDevicesNew(); | 218 std::vector<serial::DeviceInfoPtr> devices = GetDevicesNew(); |
| 219 mojo::Array<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); | 219 std::vector<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); |
| 220 | 220 |
| 221 UMA_HISTOGRAM_SPARSE_SLOWLY( | 221 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 222 "Hardware.Serial.NewMinusOldDeviceListSize", | 222 "Hardware.Serial.NewMinusOldDeviceListSize", |
| 223 Clamp(devices.size() - old_devices.size(), -10, 10)); | 223 Clamp(devices.size() - old_devices.size(), -10, 10)); |
| 224 | 224 |
| 225 // 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 |
| 226 // 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 |
| 227 // 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 |
| 228 // 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, |
| 229 // because insertions are ignored if the key already exists. | 229 // because insertions are ignored if the key already exists. |
| 230 std::unordered_set<std::string> devices_seen; | 230 std::unordered_set<std::string> devices_seen; |
| 231 for (const auto& device : devices) { | 231 for (const auto& device : devices) { |
| 232 bool inserted = devices_seen.insert(device->path).second; | 232 bool inserted = devices_seen.insert(device->path).second; |
| 233 DCHECK(inserted); | 233 DCHECK(inserted); |
| 234 } | 234 } |
| 235 for (auto& device : old_devices) { | 235 for (auto& device : old_devices) { |
| 236 if (devices_seen.insert(device->path).second) | 236 if (devices_seen.insert(device->path).second) |
| 237 devices.push_back(std::move(device)); | 237 devices.push_back(std::move(device)); |
| 238 } | 238 } |
| 239 return devices; | 239 return devices; |
| 240 } | 240 } |
| 241 | 241 |
| 242 } // namespace device | 242 } // namespace device |
| OLD | NEW |