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