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/observer_list_threadsafe.h"
  8. #include "build/build_config.h"
  9. namespace base {
  10. // Class for monitoring various system-related subsystems
  11. // such as power management, network status, etc.
  12. // TODO(mbelshe): Add support beyond just power management.
  13. class BASE_EXPORT SystemMonitor {
  14. public:
  15. // Type of devices whose change need to be monitored, such as add/remove.
  16. enum DeviceType {
  17. DEVTYPE_AUDIO, // Audio device, e.g., microphone.
  18. DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam.
  19. DEVTYPE_UNKNOWN, // Other devices.
  20. };
  21. // Create SystemMonitor. Only one SystemMonitor instance per application
  22. // is allowed.
  23. SystemMonitor();
  24. SystemMonitor(const SystemMonitor&) = delete;
  25. SystemMonitor& operator=(const SystemMonitor&) = delete;
  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. };
  56. } // namespace base
  57. #endif // BASE_SYSTEM_SYSTEM_MONITOR_H_