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