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