OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/udev_linux.h" | 5 #include "content/browser/udev_linux.h" |
6 | 6 |
7 #include <libudev.h> | |
8 | |
9 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
10 | 8 |
11 namespace content { | 9 namespace content { |
12 | 10 |
13 UdevLinux::UdevLinux(const std::vector<UdevMonitorFilter>& filters, | 11 UdevLinux::UdevLinux(const std::vector<UdevMonitorFilter>& filters, |
14 const UdevNotificationCallback& callback) | 12 const UdevNotificationCallback& callback) |
15 : udev_(udev_new()), | 13 : udev_(device::udev_new()), |
16 monitor_(udev_monitor_new_from_netlink(udev_.get(), "udev")), | 14 monitor_(device::udev_monitor_new_from_netlink(udev_.get(), "udev")), |
17 monitor_fd_(-1), | 15 monitor_fd_(-1), |
18 callback_(callback) { | 16 callback_(callback) { |
19 CHECK(udev_); | 17 CHECK(udev_); |
20 CHECK(monitor_); | 18 CHECK(monitor_); |
21 | 19 |
22 for (size_t i = 0; i < filters.size(); ++i) { | 20 for (size_t i = 0; i < filters.size(); ++i) { |
23 int ret = udev_monitor_filter_add_match_subsystem_devtype( | 21 int ret = device::udev_monitor_filter_add_match_subsystem_devtype( |
24 monitor_.get(), filters[i].subsystem, filters[i].devtype); | 22 monitor_.get(), filters[i].subsystem, filters[i].devtype); |
25 CHECK_EQ(0, ret); | 23 CHECK_EQ(0, ret); |
26 } | 24 } |
27 | 25 |
28 int ret = udev_monitor_enable_receiving(monitor_.get()); | 26 int ret = device::udev_monitor_enable_receiving(monitor_.get()); |
29 CHECK_EQ(0, ret); | 27 CHECK_EQ(0, ret); |
30 monitor_fd_ = udev_monitor_get_fd(monitor_.get()); | 28 monitor_fd_ = device::udev_monitor_get_fd(monitor_.get()); |
31 CHECK_GE(monitor_fd_, 0); | 29 CHECK_GE(monitor_fd_, 0); |
32 | 30 |
33 bool success = base::MessageLoopForIO::current()->WatchFileDescriptor( | 31 bool success = base::MessageLoopForIO::current()->WatchFileDescriptor( |
34 monitor_fd_, | 32 monitor_fd_, |
35 true, | 33 true, |
36 base::MessageLoopForIO::WATCH_READ, | 34 base::MessageLoopForIO::WATCH_READ, |
37 &monitor_watcher_, | 35 &monitor_watcher_, |
38 this); | 36 this); |
39 CHECK(success); | 37 CHECK(success); |
40 } | 38 } |
41 | 39 |
42 UdevLinux::~UdevLinux() { | 40 UdevLinux::~UdevLinux() { |
43 monitor_watcher_.StopWatchingFileDescriptor(); | 41 monitor_watcher_.StopWatchingFileDescriptor(); |
44 } | 42 } |
45 | 43 |
46 udev* UdevLinux::udev_handle() { | 44 udev* UdevLinux::udev_handle() { |
47 return udev_.get(); | 45 return udev_.get(); |
48 } | 46 } |
49 | 47 |
50 void UdevLinux::OnFileCanReadWithoutBlocking(int fd) { | 48 void UdevLinux::OnFileCanReadWithoutBlocking(int fd) { |
51 // Events occur when devices attached to the system are added, removed, or | 49 // Events occur when devices attached to the system are added, removed, or |
52 // change state. udev_monitor_receive_device() will return a device object | 50 // change state. udev_monitor_receive_device() will return a device object |
53 // representing the device which changed and what type of change occured. | 51 // representing the device which changed and what type of change occured. |
54 DCHECK_EQ(monitor_fd_, fd); | 52 DCHECK_EQ(monitor_fd_, fd); |
55 device::ScopedUdevDevicePtr dev( | 53 device::ScopedUdevDevicePtr dev( |
56 udev_monitor_receive_device(monitor_.get())); | 54 device::udev_monitor_receive_device(monitor_.get())); |
57 if (!dev) | 55 if (!dev) |
58 return; | 56 return; |
59 | 57 |
60 callback_.Run(dev.get()); | 58 callback_.Run(dev.get()); |
61 } | 59 } |
62 | 60 |
63 void UdevLinux::OnFileCanWriteWithoutBlocking(int fd) { | 61 void UdevLinux::OnFileCanWriteWithoutBlocking(int fd) { |
64 } | 62 } |
65 | 63 |
66 } // namespace content | 64 } // namespace content |
OLD | NEW |