| 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 <utility> |
| 12 | 13 |
| 13 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 16 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 17 #include "base/mac/scoped_ioobject.h" | 18 #include "base/mac/scoped_ioobject.h" |
| 18 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/metrics/sparse_histogram.h" | 20 #include "base/metrics/sparse_histogram.h" |
| 20 #include "base/strings/pattern.h" | 21 #include "base/strings/pattern.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // Returns an array of devices as retrieved through the new method of | 100 // Returns an array of devices as retrieved through the new method of |
| 100 // enumerating serial devices (IOKit). This new method gives more information | 101 // enumerating serial devices (IOKit). This new method gives more information |
| 101 // about the devices than the old method. | 102 // about the devices than the old method. |
| 102 mojo::Array<serial::DeviceInfoPtr> GetDevicesNew() { | 103 mojo::Array<serial::DeviceInfoPtr> GetDevicesNew() { |
| 103 mojo::Array<serial::DeviceInfoPtr> devices(0); | 104 mojo::Array<serial::DeviceInfoPtr> devices(0); |
| 104 | 105 |
| 105 // Make a service query to find all serial devices. | 106 // Make a service query to find all serial devices. |
| 106 CFMutableDictionaryRef matchingDict = | 107 CFMutableDictionaryRef matchingDict = |
| 107 IOServiceMatching(kIOSerialBSDServiceValue); | 108 IOServiceMatching(kIOSerialBSDServiceValue); |
| 108 if (!matchingDict) | 109 if (!matchingDict) |
| 109 return devices.Pass(); | 110 return devices; |
| 110 | 111 |
| 111 io_iterator_t it; | 112 io_iterator_t it; |
| 112 kern_return_t kr = | 113 kern_return_t kr = |
| 113 IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &it); | 114 IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &it); |
| 114 if (kr != KERN_SUCCESS) | 115 if (kr != KERN_SUCCESS) |
| 115 return devices.Pass(); | 116 return devices; |
| 116 | 117 |
| 117 base::mac::ScopedIOObject<io_iterator_t> scoped_it(it); | 118 base::mac::ScopedIOObject<io_iterator_t> scoped_it(it); |
| 118 base::mac::ScopedIOObject<io_service_t> scoped_device; | 119 base::mac::ScopedIOObject<io_service_t> scoped_device; |
| 119 while (scoped_device.reset(IOIteratorNext(scoped_it.get())), scoped_device) { | 120 while (scoped_device.reset(IOIteratorNext(scoped_it.get())), scoped_device) { |
| 120 serial::DeviceInfoPtr callout_info(serial::DeviceInfo::New()); | 121 serial::DeviceInfoPtr callout_info(serial::DeviceInfo::New()); |
| 121 | 122 |
| 122 uint16_t vendorId; | 123 uint16_t vendorId; |
| 123 if (GetUInt16Property(scoped_device.get(), CFSTR(kUSBVendorID), | 124 if (GetUInt16Property(scoped_device.get(), CFSTR(kUSBVendorID), |
| 124 &vendorId)) { | 125 &vendorId)) { |
| 125 callout_info->has_vendor_id = true; | 126 callout_info->has_vendor_id = true; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 | 142 |
| 142 // Each serial device has two "paths" in /dev/ associated with it: a | 143 // Each serial device has two "paths" in /dev/ associated with it: a |
| 143 // "dialin" path starting with "tty" and a "callout" path starting with | 144 // "dialin" path starting with "tty" and a "callout" path starting with |
| 144 // "cu". Each of these is considered a different device from Chrome's | 145 // "cu". Each of these is considered a different device from Chrome's |
| 145 // standpoint, but both should share the device's USB properties. | 146 // standpoint, but both should share the device's USB properties. |
| 146 mojo::String dialinDevice; | 147 mojo::String dialinDevice; |
| 147 if (GetStringProperty(scoped_device.get(), CFSTR(kIODialinDeviceKey), | 148 if (GetStringProperty(scoped_device.get(), CFSTR(kIODialinDeviceKey), |
| 148 &dialinDevice)) { | 149 &dialinDevice)) { |
| 149 serial::DeviceInfoPtr dialin_info = callout_info.Clone(); | 150 serial::DeviceInfoPtr dialin_info = callout_info.Clone(); |
| 150 dialin_info->path = dialinDevice; | 151 dialin_info->path = dialinDevice; |
| 151 devices.push_back(dialin_info.Pass()); | 152 devices.push_back(std::move(dialin_info)); |
| 152 } | 153 } |
| 153 | 154 |
| 154 mojo::String calloutDevice; | 155 mojo::String calloutDevice; |
| 155 if (GetStringProperty(scoped_device.get(), CFSTR(kIOCalloutDeviceKey), | 156 if (GetStringProperty(scoped_device.get(), CFSTR(kIOCalloutDeviceKey), |
| 156 &calloutDevice)) { | 157 &calloutDevice)) { |
| 157 callout_info->path = calloutDevice; | 158 callout_info->path = calloutDevice; |
| 158 devices.push_back(callout_info.Pass()); | 159 devices.push_back(std::move(callout_info)); |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 return devices.Pass(); | 163 return devices; |
| 163 } | 164 } |
| 164 | 165 |
| 165 // Returns an array of devices as retrieved through the old method of | 166 // Returns an array of devices as retrieved through the old method of |
| 166 // enumerating serial devices (pattern matching in /dev/). This old method gives | 167 // enumerating serial devices (pattern matching in /dev/). This old method gives |
| 167 // less information about the devices than the new method. | 168 // less information about the devices than the new method. |
| 168 mojo::Array<serial::DeviceInfoPtr> GetDevicesOld() { | 169 mojo::Array<serial::DeviceInfoPtr> GetDevicesOld() { |
| 169 const base::FilePath kDevRoot("/dev"); | 170 const base::FilePath kDevRoot("/dev"); |
| 170 const int kFilesAndSymLinks = | 171 const int kFilesAndSymLinks = |
| 171 base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS; | 172 base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS; |
| 172 | 173 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 185 const base::FilePath next_device_path(enumerator.Next()); | 186 const base::FilePath next_device_path(enumerator.Next()); |
| 186 const std::string next_device = next_device_path.value(); | 187 const std::string next_device = next_device_path.value(); |
| 187 if (next_device.empty()) | 188 if (next_device.empty()) |
| 188 break; | 189 break; |
| 189 | 190 |
| 190 std::set<std::string>::const_iterator i = valid_patterns.begin(); | 191 std::set<std::string>::const_iterator i = valid_patterns.begin(); |
| 191 for (; i != valid_patterns.end(); ++i) { | 192 for (; i != valid_patterns.end(); ++i) { |
| 192 if (base::MatchPattern(next_device, *i)) { | 193 if (base::MatchPattern(next_device, *i)) { |
| 193 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); | 194 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); |
| 194 info->path = next_device; | 195 info->path = next_device; |
| 195 devices.push_back(info.Pass()); | 196 devices.push_back(std::move(info)); |
| 196 break; | 197 break; |
| 197 } | 198 } |
| 198 } | 199 } |
| 199 } while (true); | 200 } while (true); |
| 200 return devices.Pass(); | 201 return devices; |
| 201 } | 202 } |
| 202 | 203 |
| 203 } // namespace | 204 } // namespace |
| 204 | 205 |
| 205 // static | 206 // static |
| 206 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 207 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
| 207 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorMac()); | 208 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorMac()); |
| 208 } | 209 } |
| 209 | 210 |
| 210 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} | 211 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {} |
| (...skipping 18 matching lines...) Expand all Loading... |
| 229 deviceMap.insert(newDevices[i]->path, newDevices[i].Clone()); | 230 deviceMap.insert(newDevices[i]->path, newDevices[i].Clone()); |
| 230 } | 231 } |
| 231 for (unsigned long i = 0; i < oldDevices.size(); i++) { | 232 for (unsigned long i = 0; i < oldDevices.size(); i++) { |
| 232 deviceMap.insert(oldDevices[i]->path, oldDevices[i].Clone()); | 233 deviceMap.insert(oldDevices[i]->path, oldDevices[i].Clone()); |
| 233 } | 234 } |
| 234 | 235 |
| 235 mojo::Array<mojo::String> paths; | 236 mojo::Array<mojo::String> paths; |
| 236 mojo::Array<serial::DeviceInfoPtr> devices; | 237 mojo::Array<serial::DeviceInfoPtr> devices; |
| 237 deviceMap.DecomposeMapTo(&paths, &devices); | 238 deviceMap.DecomposeMapTo(&paths, &devices); |
| 238 | 239 |
| 239 return devices.Pass(); | 240 return devices; |
| 240 } | 241 } |
| 241 | 242 |
| 242 } // namespace device | 243 } // namespace device |
| OLD | NEW |