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

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

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 | « no previous file | device/udev_linux/udev_linux.cc » ('j') | 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 // UdevLinux listens for device change notifications from udev and runs 5 // UdevLinux listens for device change notifications from udev and runs
6 // callbacks when notifications occur. 6 // callbacks when notifications occur.
7 // 7 //
8 // UdevLinux must be created on a MessageLoop of TYPE_IO. 8 // UdevLinux must be created on a thread that has instantiated a
9 // UdevLinux is not thread-safe. 9 // FileDescriptorWatcher. UdevLinux is not thread-safe.
10 // 10 //
11 // Example usage: 11 // Example usage:
12 // 12 //
13 // class UdevLinux; 13 // class UdevLinux;
14 // 14 //
15 // class Foo { 15 // class Foo {
16 // public: 16 // public:
17 // Foo() { 17 // Foo() {
18 // std::vector<UdevLinux::UdevMonitorFilter> filters; 18 // std::vector<UdevLinux::UdevMonitorFilter> filters;
19 // filters.push_back(UdevLinux::UdevMonitorFilter("block", NULL)); 19 // filters.push_back(UdevLinux::UdevMonitorFilter("block", NULL));
20 // udev_.reset(new UdevLinux(filters, 20 // udev_.reset(new UdevLinux(filters,
21 // base::Bind(&Foo::Notify, this))); 21 // base::Bind(&Foo::Notify, this)));
22 // } 22 // }
23 // 23 //
24 // // Called when a "block" device attaches/detaches. 24 // // Called when a "block" device attaches/detaches.
25 // // To hold on to |device|, call udev_device_ref(device). 25 // // To hold on to |device|, call udev_device_ref(device).
26 // void Notify(udev_device* device) { 26 // void Notify(udev_device* device) {
27 // // Do something with |device|. 27 // // Do something with |device|.
28 // } 28 // }
29 // 29 //
30 // private: 30 // private:
31 // std::unique_ptr<UdevLinux> udev_; 31 // std::unique_ptr<UdevLinux> udev_;
32 // 32 //
33 // DISALLOW_COPY_AND_ASSIGN(Foo); 33 // DISALLOW_COPY_AND_ASSIGN(Foo);
34 // }; 34 // };
35 35
36 #ifndef DEVICE_UDEV_LINUX_UDEV_LINUX_H_ 36 #ifndef DEVICE_UDEV_LINUX_UDEV_LINUX_H_
37 #define DEVICE_UDEV_LINUX_UDEV_LINUX_H_ 37 #define DEVICE_UDEV_LINUX_UDEV_LINUX_H_
38 38
39 #include <memory>
39 #include <vector> 40 #include <vector>
40 41
41 #include "base/callback.h" 42 #include "base/callback.h"
42 #include "base/compiler_specific.h" 43 #include "base/compiler_specific.h"
44 #include "base/files/file_descriptor_watcher_posix.h"
43 #include "base/macros.h" 45 #include "base/macros.h"
44 #include "base/message_loop/message_pump_libevent.h"
45 #include "device/udev_linux/scoped_udev.h" 46 #include "device/udev_linux/scoped_udev.h"
46 47
47 extern "C" { 48 extern "C" {
48 struct udev; 49 struct udev;
49 struct udev_device; 50 struct udev_device;
50 struct udev_monitor; 51 struct udev_monitor;
51 } 52 }
52 53
53 namespace device { 54 namespace device {
54 55
55 class UdevLinux : public base::MessagePumpLibevent::Watcher { 56 class UdevLinux {
56 public: 57 public:
57 typedef base::Callback<void(udev_device*)> UdevNotificationCallback; 58 typedef base::Callback<void(udev_device*)> UdevNotificationCallback;
58 59
59 // subsystem and devtype parameter for 60 // subsystem and devtype parameter for
60 // udev_monitor_filter_add_match_subsystem_devtype(). 61 // udev_monitor_filter_add_match_subsystem_devtype().
61 struct UdevMonitorFilter { 62 struct UdevMonitorFilter {
62 UdevMonitorFilter(const char* subsystem_in, const char* devtype_in) 63 UdevMonitorFilter(const char* subsystem_in, const char* devtype_in)
63 : subsystem(subsystem_in), devtype(devtype_in) {} 64 : subsystem(subsystem_in), devtype(devtype_in) {}
64 const char* subsystem; 65 const char* subsystem;
65 const char* devtype; 66 const char* devtype;
66 }; 67 };
67 68
68 // Filter incoming devices based on |filters|. 69 // Filter incoming devices based on |filters|.
69 // Calls |callback| upon device change events. 70 // Calls |callback| upon device change events.
70 UdevLinux(const std::vector<UdevMonitorFilter>& filters, 71 UdevLinux(const std::vector<UdevMonitorFilter>& filters,
71 const UdevNotificationCallback& callback); 72 const UdevNotificationCallback& callback);
72 ~UdevLinux() override; 73 ~UdevLinux();
73 74
74 // Returns the udev handle to be passed into other udev_*() functions. 75 // Returns the udev handle to be passed into other udev_*() functions.
75 udev* udev_handle(); 76 udev* udev_handle();
76 77
77 private: 78 private:
78 // base::MessagePump:Libevent::Watcher implementation. 79 // Called when |monitor_fd_| can be read without blocking.
79 void OnFileCanReadWithoutBlocking(int fd) override; 80 void OnMonitorCanReadWithoutBlocking();
80 void OnFileCanWriteWithoutBlocking(int fd) override;
81 81
82 // libudev-related items, the main context, and the monitoring context to be 82 // libudev-related items, the main context, and the monitoring context to be
83 // notified about changes to device states. 83 // notified about changes to device states.
84 const ScopedUdevPtr udev_; 84 const ScopedUdevPtr udev_;
85 const ScopedUdevMonitorPtr monitor_; 85 const ScopedUdevMonitorPtr monitor_;
86 int monitor_fd_; 86 int monitor_fd_;
87 base::MessagePumpLibevent::FileDescriptorWatcher monitor_watcher_; 87 std::unique_ptr<base::FileDescriptorWatcher::Controller>
88 monitor_watch_controller_;
88 const UdevNotificationCallback callback_; 89 const UdevNotificationCallback callback_;
89 90
90 DISALLOW_COPY_AND_ASSIGN(UdevLinux); 91 DISALLOW_COPY_AND_ASSIGN(UdevLinux);
91 }; 92 };
92 93
93 } // namespace device 94 } // namespace device
94 95
95 #endif // DEVICE_UDEV_LINUX_UDEV_LINUX_H_ 96 #endif // DEVICE_UDEV_LINUX_UDEV_LINUX_H_
OLDNEW
« no previous file with comments | « no previous file | device/udev_linux/udev_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698