Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / serial / serial_device_enumerator_mac.cc
blob8e56b91def8953309a3c8a2d8455c48b336c57cf
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
3 // found in the LICENSE file.
5 #include "device/serial/serial_device_enumerator_mac.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/pattern.h"
12 #include "base/strings/string_util.h"
14 namespace device {
16 // static
17 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() {
18 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorMac());
21 SerialDeviceEnumeratorMac::SerialDeviceEnumeratorMac() {}
23 SerialDeviceEnumeratorMac::~SerialDeviceEnumeratorMac() {}
25 // TODO(rockot): Use IOKit to enumerate serial interfaces.
26 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorMac::GetDevices() {
27 const base::FilePath kDevRoot("/dev");
28 const int kFilesAndSymLinks =
29 base::FileEnumerator::FILES | base::FileEnumerator::SHOW_SYM_LINKS;
31 std::set<std::string> valid_patterns;
32 valid_patterns.insert("/dev/*Bluetooth*");
33 valid_patterns.insert("/dev/*Modem*");
34 valid_patterns.insert("/dev/*bluetooth*");
35 valid_patterns.insert("/dev/*modem*");
36 valid_patterns.insert("/dev/*serial*");
37 valid_patterns.insert("/dev/tty.*");
38 valid_patterns.insert("/dev/cu.*");
40 mojo::Array<serial::DeviceInfoPtr> devices(0);
41 base::FileEnumerator enumerator(kDevRoot, false, kFilesAndSymLinks);
42 do {
43 const base::FilePath next_device_path(enumerator.Next());
44 const std::string next_device = next_device_path.value();
45 if (next_device.empty())
46 break;
48 std::set<std::string>::const_iterator i = valid_patterns.begin();
49 for (; i != valid_patterns.end(); ++i) {
50 if (base::MatchPattern(next_device, *i)) {
51 serial::DeviceInfoPtr info(serial::DeviceInfo::New());
52 info->path = next_device;
53 devices.push_back(info.Pass());
54 break;
57 } while (true);
58 return devices.Pass();
61 } // namespace device