device_monitor_linux.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.
  4. #ifndef DEVICE_BASE_DEVICE_MONITOR_LINUX_H_
  5. #define DEVICE_BASE_DEVICE_MONITOR_LINUX_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "base/files/file_descriptor_watcher_posix.h"
  9. #include "base/observer_list.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "device/base/device_base_export.h"
  12. #include "device/udev_linux/scoped_udev.h"
  13. struct udev_device;
  14. namespace device {
  15. // This class listends for notifications from libudev about
  16. // connected/disconnected devices. This class is *NOT* thread-safe.
  17. class DEVICE_BASE_EXPORT DeviceMonitorLinux {
  18. public:
  19. typedef base::RepeatingCallback<void(udev_device* device)> EnumerateCallback;
  20. class Observer {
  21. public:
  22. virtual ~Observer() {}
  23. virtual void OnDeviceAdded(udev_device* device) = 0;
  24. virtual void OnDeviceRemoved(udev_device* device) = 0;
  25. };
  26. DeviceMonitorLinux();
  27. DeviceMonitorLinux(const DeviceMonitorLinux&) = delete;
  28. DeviceMonitorLinux& operator=(const DeviceMonitorLinux&) = delete;
  29. static DeviceMonitorLinux* GetInstance();
  30. void AddObserver(Observer* observer);
  31. void RemoveObserver(Observer* observer);
  32. void Enumerate(const EnumerateCallback& callback);
  33. private:
  34. friend std::default_delete<DeviceMonitorLinux>;
  35. ~DeviceMonitorLinux();
  36. void OnMonitorCanReadWithoutBlocking();
  37. ScopedUdevPtr udev_;
  38. ScopedUdevMonitorPtr monitor_;
  39. int monitor_fd_;
  40. std::unique_ptr<base::FileDescriptorWatcher::Controller>
  41. monitor_watch_controller_;
  42. base::ObserverList<Observer, true>::Unchecked observers_;
  43. base::ThreadChecker thread_checker_;
  44. };
  45. } // namespace device
  46. #endif // DEVICE_BASE_DEVICE_MONITOR_LINUX_H_