Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: device/udev_linux/udev_linux.cc

Issue 2392583002: Use FileDescriptorWatcher in UdevLinux (Closed)
Patch Set: fix build error Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/udev_linux/udev_linux.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "device/udev_linux/udev_linux.h" 5 #include "device/udev_linux/udev_linux.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/bind.h"
10 10
11 namespace device { 11 namespace device {
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_(udev_monitor_new_from_netlink(udev_.get(), "udev")), 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 CHECK(monitor_); 20 CHECK(monitor_);
21 CHECK_EQ(base::MessageLoop::TYPE_IO, base::MessageLoop::current()->type());
22 21
23 for (const UdevMonitorFilter& filter : filters) { 22 for (const UdevMonitorFilter& filter : filters) {
24 const int ret = udev_monitor_filter_add_match_subsystem_devtype( 23 const int ret = udev_monitor_filter_add_match_subsystem_devtype(
25 monitor_.get(), filter.subsystem, filter.devtype); 24 monitor_.get(), filter.subsystem, filter.devtype);
26 CHECK_EQ(0, ret); 25 CHECK_EQ(0, ret);
27 } 26 }
28 27
29 const int ret = udev_monitor_enable_receiving(monitor_.get()); 28 const int ret = udev_monitor_enable_receiving(monitor_.get());
30 CHECK_EQ(0, ret); 29 CHECK_EQ(0, ret);
31 monitor_fd_ = udev_monitor_get_fd(monitor_.get()); 30 monitor_fd_ = udev_monitor_get_fd(monitor_.get());
32 CHECK_GE(monitor_fd_, 0); 31 CHECK_GE(monitor_fd_, 0);
33 32
34 bool success = base::MessageLoopForIO::current()->WatchFileDescriptor( 33 monitor_watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
35 monitor_fd_, true, base::MessageLoopForIO::WATCH_READ, &monitor_watcher_, 34 monitor_fd_, base::Bind(&UdevLinux::OnMonitorCanReadWithoutBlocking,
36 this); 35 base::Unretained(this)));
37 CHECK(success);
38 } 36 }
39 37
40 UdevLinux::~UdevLinux() { 38 UdevLinux::~UdevLinux() = default;
41 monitor_watcher_.StopWatchingFileDescriptor();
42 }
43 39
44 udev* UdevLinux::udev_handle() { 40 udev* UdevLinux::udev_handle() {
45 return udev_.get(); 41 return udev_.get();
46 } 42 }
47 43
48 void UdevLinux::OnFileCanReadWithoutBlocking(int fd) { 44 void UdevLinux::OnMonitorCanReadWithoutBlocking() {
49 // Events occur when devices attached to the system are added, removed, or 45 // Events occur when devices attached to the system are added, removed, or
50 // change state. udev_monitor_receive_device() will return a device object 46 // change state. udev_monitor_receive_device() will return a device object
51 // representing the device which changed and what type of change occured. 47 // representing the device which changed and what type of change occured.
52 DCHECK_EQ(monitor_fd_, fd);
53 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); 48 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get()));
54 if (!dev) 49 if (!dev)
55 return; 50 return;
56 51
57 callback_.Run(dev.get()); 52 callback_.Run(dev.get());
58 } 53 }
59 54
60 void UdevLinux::OnFileCanWriteWithoutBlocking(int fd) {}
61
62 } // namespace device 55 } // namespace device
OLDNEW
« no previous file with comments | « device/udev_linux/udev_linux.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698