| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SYSTEM_MONITOR_H_ | 5 #ifndef BASE_SYSTEM_MONITOR_H_ |
| 6 #define BASE_SYSTEM_MONITOR_H_ | 6 #define BASE_SYSTEM_MONITOR_H_ |
| 7 | 7 |
| 8 #include "base/observer_list_threadsafe.h" | 8 #include "base/observer_list_threadsafe.h" |
| 9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
| 10 | 10 |
| 11 // Windows HiRes timers drain the battery faster so we need to know the battery |
| 12 // status. This isn't true for other platforms. |
| 13 #if defined(OS_WIN) |
| 14 #define ENABLE_BATTERY_MONITORING 1 |
| 15 #else |
| 16 #undef ENABLE_BATTERY_MONITORING |
| 17 #endif // !OS_WIN |
| 18 |
| 11 namespace base { | 19 namespace base { |
| 12 | 20 |
| 13 // Class for monitoring various system-related subsystems | 21 // Class for monitoring various system-related subsystems |
| 14 // such as power management, network status, etc. | 22 // such as power management, network status, etc. |
| 15 // TODO(mbelshe): Add support beyond just power management. | 23 // TODO(mbelshe): Add support beyond just power management. |
| 16 class SystemMonitor { | 24 class SystemMonitor { |
| 17 public: | 25 public: |
| 18 // Access to the Singleton | 26 // Access to the Singleton |
| 19 static SystemMonitor* Get() { | 27 static SystemMonitor* Get() { |
| 20 // Uses the LeakySingletonTrait because cleanup is optional. | 28 // Uses the LeakySingletonTrait because cleanup is optional. |
| 21 return | 29 return |
| 22 Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get(); | 30 Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get(); |
| 23 } | 31 } |
| 24 | 32 |
| 25 // Start the System Monitor within a process. This method | 33 // Start the System Monitor within a process. This method |
| 26 // is provided so that the battery check can be deferred. | 34 // is provided so that the battery check can be deferred. |
| 27 // The MessageLoop must be started before calling this | 35 // The MessageLoop must be started before calling this |
| 28 // method. | 36 // method. |
| 37 // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is |
| 38 // disabled. |
| 29 static void Start(); | 39 static void Start(); |
| 30 | 40 |
| 31 // | 41 // |
| 32 // Power-related APIs | 42 // Power-related APIs |
| 33 // | 43 // |
| 34 | 44 |
| 35 // Is the computer currently on battery power. | 45 // Is the computer currently on battery power. |
| 36 // Can be called on any thread. | 46 // Can be called on any thread. |
| 37 bool BatteryPower() { | 47 bool BatteryPower() { |
| 38 // Using a lock here is not necessary for just a bool. | 48 // Using a lock here is not necessary for just a bool. |
| 39 return battery_in_use_; | 49 return battery_in_use_; |
| 40 } | 50 } |
| 41 | 51 |
| 42 // Normalized list of power events. | 52 // Normalized list of power events. |
| 43 enum PowerEvent { | 53 enum PowerEvent { |
| 44 POWER_STATE_EVENT, // The Power status of the system has changed. | 54 POWER_STATE_EVENT, // The Power status of the system has changed. |
| 45 SUSPEND_EVENT, // The system is being suspended. | 55 SUSPEND_EVENT, // The system is being suspended. |
| 46 RESUME_EVENT // The system is being resumed. | 56 RESUME_EVENT // The system is being resumed. |
| 47 }; | 57 }; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 77 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
| 78 // Windows-specific handling of a WM_POWERBROADCAST message. | 88 // Windows-specific handling of a WM_POWERBROADCAST message. |
| 79 // Embedders of this API should hook their top-level window | 89 // Embedders of this API should hook their top-level window |
| 80 // message loop and forward WM_POWERBROADCAST through this call. | 90 // message loop and forward WM_POWERBROADCAST through this call. |
| 81 void ProcessWmPowerBroadcastMessage(int event_id); | 91 void ProcessWmPowerBroadcastMessage(int event_id); |
| 82 #endif | 92 #endif |
| 83 | 93 |
| 84 // Cross-platform handling of a power event. | 94 // Cross-platform handling of a power event. |
| 85 void ProcessPowerMessage(PowerEvent event_id); | 95 void ProcessPowerMessage(PowerEvent event_id); |
| 86 | 96 |
| 87 // Constructor. | 97 // Constructor. |
| 88 // Don't use this; access SystemMonitor via the Singleton. | 98 // Don't use this; access SystemMonitor via the Singleton. |
| 89 SystemMonitor(); | 99 SystemMonitor(); |
| 90 | 100 |
| 91 private: | 101 private: |
| 92 // Platform-specific method to check whether the system is currently | 102 // Platform-specific method to check whether the system is currently |
| 93 // running on battery power. Returns true if running on batteries, | 103 // running on battery power. Returns true if running on batteries, |
| 94 // false otherwise. | 104 // false otherwise. |
| 95 bool IsBatteryPower(); | 105 bool IsBatteryPower(); |
| 96 | 106 |
| 97 // Checks the battery status and notifies observers if the battery | 107 // Checks the battery status and notifies observers if the battery |
| 98 // status has changed. | 108 // status has changed. |
| 99 void BatteryCheck(); | 109 void BatteryCheck(); |
| 100 | 110 |
| 101 // Functions to trigger notifications. | 111 // Functions to trigger notifications. |
| 102 void NotifyPowerStateChange(); | 112 void NotifyPowerStateChange(); |
| 103 void NotifySuspend(); | 113 void NotifySuspend(); |
| 104 void NotifyResume(); | 114 void NotifyResume(); |
| 105 | 115 |
| 106 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; | 116 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; |
| 107 bool battery_in_use_; | 117 bool battery_in_use_; |
| 108 bool suspended_; | 118 bool suspended_; |
| 109 | 119 |
| 120 #if defined(ENABLE_BATTERY_MONITORING) |
| 110 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 121 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
| 122 #endif |
| 111 | 123 |
| 112 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 124 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 113 }; | 125 }; |
| 114 | 126 |
| 115 } | 127 } |
| 116 | 128 |
| 117 #endif // BASE_SYSTEM_MONITOR_H_ | 129 #endif // BASE_SYSTEM_MONITOR_H_ |
| 118 | 130 |
| OLD | NEW |