system_monitor.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef BASE_SYSTEM_SYSTEM_MONITOR_H_
  5. #define BASE_SYSTEM_SYSTEM_MONITOR_H_
  6. #include "base/base_export.h"
  7. #include "base/macros.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/observer_list_threadsafe.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. // Class for monitoring various system-related subsystems
  13. // such as power management, network status, etc.
  14. // TODO(mbelshe): Add support beyond just power management.
  15. class BASE_EXPORT SystemMonitor {
  16. public:
  17. // Type of devices whose change need to be monitored, such as add/remove.
  18. enum DeviceType {
  19. DEVTYPE_AUDIO, // Audio device, e.g., microphone.
  20. DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam.
  21. DEVTYPE_UNKNOWN, // Other devices.
  22. };
  23. // Create SystemMonitor. Only one SystemMonitor instance per application
  24. // is allowed.
  25. SystemMonitor();
  26. ~SystemMonitor();
  27. // Get the application-wide SystemMonitor (if not present, returns NULL).
  28. static SystemMonitor* Get();
  29. class BASE_EXPORT DevicesChangedObserver {
  30. public:
  31. // Notification that the devices connected to the system have changed.
  32. // This is only implemented on Windows currently.
  33. virtual void OnDevicesChanged(DeviceType device_type) {}
  34. protected:
  35. virtual ~DevicesChangedObserver() = default;
  36. };
  37. // Add a new observer.
  38. // Can be called from any thread.
  39. // Must not be called from within a notification callback.
  40. void AddDevicesChangedObserver(DevicesChangedObserver* obs);
  41. // Remove an existing observer.
  42. // Can be called from any thread.
  43. // Must not be called from within a notification callback.
  44. void RemoveDevicesChangedObserver(DevicesChangedObserver* obs);
  45. // The ProcessFoo() style methods are a broken pattern and should not
  46. // be copied. Any significant addition to this class is blocked on
  47. // refactoring to improve the state of affairs. See http://crbug.com/149059
  48. // Cross-platform handling of a device change event.
  49. void ProcessDevicesChanged(DeviceType device_type);
  50. private:
  51. // Functions to trigger notifications.
  52. void NotifyDevicesChanged(DeviceType device_type);
  53. scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver>>
  54. devices_changed_observer_list_;
  55. DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
  56. };
  57. } // namespace base
  58. #endif // BASE_SYSTEM_SYSTEM_MONITOR_H_