xref: /aosp_15_r20/external/cronet/base/system/system_monitor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_SYSTEM_SYSTEM_MONITOR_H_
6 #define BASE_SYSTEM_SYSTEM_MONITOR_H_
7 
8 #include "base/base_export.h"
9 #include "base/observer_list_threadsafe.h"
10 #include "build/build_config.h"
11 
12 namespace base {
13 
14 // Class for monitoring various system-related subsystems
15 // such as power management, network status, etc.
16 // TODO(mbelshe):  Add support beyond just power management.
17 class BASE_EXPORT SystemMonitor {
18  public:
19   // Type of devices whose change need to be monitored, such as add/remove.
20   enum DeviceType {
21     DEVTYPE_AUDIO,          // Audio device, e.g., microphone.
22     DEVTYPE_VIDEO_CAPTURE,  // Video capture device, e.g., webcam.
23     DEVTYPE_UNKNOWN,        // Other devices.
24   };
25 
26   // Create SystemMonitor. Only one SystemMonitor instance per application
27   // is allowed.
28   SystemMonitor();
29 
30   SystemMonitor(const SystemMonitor&) = delete;
31   SystemMonitor& operator=(const SystemMonitor&) = delete;
32 
33   ~SystemMonitor();
34 
35   // Get the application-wide SystemMonitor (if not present, returns NULL).
36   static SystemMonitor* Get();
37 
38   class BASE_EXPORT DevicesChangedObserver {
39    public:
40     // Notification that the devices connected to the system have changed.
41     // This is only implemented on Windows currently.
OnDevicesChanged(DeviceType device_type)42     virtual void OnDevicesChanged(DeviceType device_type) {}
43 
44    protected:
45     virtual ~DevicesChangedObserver() = default;
46   };
47 
48   // Add a new observer.
49   // Can be called from any thread.
50   // Must not be called from within a notification callback.
51   void AddDevicesChangedObserver(DevicesChangedObserver* obs);
52 
53   // Remove an existing observer.
54   // Can be called from any thread.
55   // Must not be called from within a notification callback.
56   void RemoveDevicesChangedObserver(DevicesChangedObserver* obs);
57 
58   // The ProcessFoo() style methods are a broken pattern and should not
59   // be copied. Any significant addition to this class is blocked on
60   // refactoring to improve the state of affairs. See http://crbug.com/149059
61 
62   // Cross-platform handling of a device change event.
63   void ProcessDevicesChanged(DeviceType device_type);
64 
65  private:
66   // Functions to trigger notifications.
67   void NotifyDevicesChanged(DeviceType device_type);
68 
69   scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver>>
70       devices_changed_observer_list_;
71 };
72 
73 }  // namespace base
74 
75 #endif  // BASE_SYSTEM_SYSTEM_MONITOR_H_
76