Fix shutdown crash when AudioRendererHosts outlive the ResourceScheduler
[chromium-blink-merge.git] / device / hid / device_monitor_linux.h
blob8cec73a34a6f4c30683c7dac804fb69fce32179c
1 // Copyright 2014 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.
5 #ifndef DEVICE_HID_DEVICE_MONITOR_LINUX_H_
6 #define DEVICE_HID_DEVICE_MONITOR_LINUX_H_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_pump_libevent.h"
15 #include "base/observer_list.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/udev_linux/scoped_udev.h"
19 struct udev_device;
21 namespace device {
23 // This class listends for notifications from libudev about
24 // connected/disconnected devices. This class is *NOT* thread-safe and
25 // all methods must be accessed from the FILE thread.
26 class DeviceMonitorLinux : public base::MessageLoop::DestructionObserver,
27 public base::MessagePumpLibevent::Watcher {
28 public:
29 typedef base::Callback<void(udev_device* device)> EnumerateCallback;
31 class Observer {
32 public:
33 virtual ~Observer() {}
34 virtual void OnDeviceAdded(udev_device* device) = 0;
35 virtual void OnDeviceRemoved(udev_device* device) = 0;
38 DeviceMonitorLinux();
40 static DeviceMonitorLinux* GetInstance();
41 static bool HasInstance();
43 void AddObserver(Observer* observer);
44 void RemoveObserver(Observer* observer);
46 ScopedUdevDevicePtr GetDeviceFromPath(const std::string& path);
47 void Enumerate(const EnumerateCallback& callback);
49 // Implements base::MessageLoop::DestructionObserver
50 void WillDestroyCurrentMessageLoop() override;
52 // Implements base::MessagePumpLibevent::Watcher
53 void OnFileCanReadWithoutBlocking(int fd) override;
54 void OnFileCanWriteWithoutBlocking(int fd) override;
56 private:
57 friend struct base::DefaultDeleter<DeviceMonitorLinux>;
59 ~DeviceMonitorLinux() override;
61 ScopedUdevPtr udev_;
62 ScopedUdevMonitorPtr monitor_;
63 int monitor_fd_;
64 base::MessagePumpLibevent::FileDescriptorWatcher monitor_watcher_;
66 ObserverList<Observer> observers_;
68 base::ThreadChecker thread_checker_;
70 DISALLOW_COPY_AND_ASSIGN(DeviceMonitorLinux);
73 } // namespace device
75 #endif // DEVICE_HID_DEVICE_MONITOR_LINUX_H_