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