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