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

Side by Side Diff: content/browser/udev_linux.cc

Issue 10824036: Linux: Refactor udev device monitoring code into its own class so it can be reused more easily. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/udev_linux.h"
6
7 #include <libudev.h>
8
9 #include "base/message_loop.h"
10
11 namespace content {
12
13 UdevLinux::UdevLinux(const std::vector<UdevMonitorFilter>& filters,
14 const UdevNotificationCallback& callback)
15 : udev_(udev_new()),
16 monitor_(NULL),
17 monitor_fd_(-1),
18 callback_(callback) {
19 CHECK(udev_);
20
21 monitor_ = udev_monitor_new_from_netlink(udev_, "udev");
22 CHECK(monitor_);
23
24 for (size_t i = 0; i < filters.size(); ++i) {
25 int ret = udev_monitor_filter_add_match_subsystem_devtype(
26 monitor_, filters[i].subsystem, filters[i].devtype);
27 CHECK_EQ(0, ret);
28 }
29
30 int ret = udev_monitor_enable_receiving(monitor_);
31 CHECK_EQ(0, ret);
32 monitor_fd_ = udev_monitor_get_fd(monitor_);
33 CHECK_GE(monitor_fd_, 0);
34
35 bool success = MessageLoopForIO::current()->WatchFileDescriptor(monitor_fd_,
36 true, MessageLoopForIO::WATCH_READ, &monitor_watcher_, this);
37 CHECK(success);
38 }
39
40 UdevLinux::~UdevLinux() {
41 monitor_watcher_.StopWatchingFileDescriptor();
42 udev_monitor_unref(monitor_);
43 udev_unref(udev_);
44 }
45
46 udev* UdevLinux::udev_handle() {
47 return udev_;
48 }
49
50 void UdevLinux::OnFileCanReadWithoutBlocking(int fd) {
51 // Events occur when devices attached to the system are added, removed, or
52 // change state. udev_monitor_receive_device() will return a device object
53 // representing the device which changed and what type of change occured.
54 DCHECK_EQ(monitor_fd_, fd);
55 udev_device* dev = udev_monitor_receive_device(monitor_);
56 if (!dev) {
57 NOTREACHED();
58 return;
59 }
60 callback_.Run(dev);
61 udev_device_unref(dev);
62 }
63
64 void UdevLinux::OnFileCanWriteWithoutBlocking(int fd) {
65 }
66
67 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698