michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SYSTEM_MONITOR_H_ michael@0: #define BASE_SYSTEM_MONITOR_H_ michael@0: michael@0: #include "base/observer_list_threadsafe.h" michael@0: #include "base/singleton.h" michael@0: michael@0: // Windows HiRes timers drain the battery faster so we need to know the battery michael@0: // status. This isn't true for other platforms. michael@0: #if defined(OS_WIN) michael@0: #define ENABLE_BATTERY_MONITORING 1 michael@0: #else michael@0: #undef ENABLE_BATTERY_MONITORING michael@0: #endif // !OS_WIN michael@0: michael@0: namespace base { michael@0: michael@0: // Class for monitoring various system-related subsystems michael@0: // such as power management, network status, etc. michael@0: // TODO(mbelshe): Add support beyond just power management. michael@0: class SystemMonitor { michael@0: public: michael@0: // Access to the Singleton michael@0: static SystemMonitor* Get() { michael@0: // Uses the LeakySingletonTrait because cleanup is optional. michael@0: return michael@0: Singleton >::get(); michael@0: } michael@0: michael@0: // Start the System Monitor within a process. This method michael@0: // is provided so that the battery check can be deferred. michael@0: // The MessageLoop must be started before calling this michael@0: // method. michael@0: // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is michael@0: // disabled. michael@0: static void Start(); michael@0: michael@0: // michael@0: // Power-related APIs michael@0: // michael@0: michael@0: // Is the computer currently on battery power. michael@0: // Can be called on any thread. michael@0: bool BatteryPower() { michael@0: // Using a lock here is not necessary for just a bool. michael@0: return battery_in_use_; michael@0: } michael@0: michael@0: // Normalized list of power events. michael@0: enum PowerEvent { michael@0: POWER_STATE_EVENT, // The Power status of the system has changed. michael@0: SUSPEND_EVENT, // The system is being suspended. michael@0: RESUME_EVENT // The system is being resumed. michael@0: }; michael@0: michael@0: // Callbacks will be called on the thread which creates the SystemMonitor. michael@0: // During the callback, Add/RemoveObserver will block until the callbacks michael@0: // are finished. Observers should implement quick callback functions; if michael@0: // lengthy operations are needed, the observer should take care to invoke michael@0: // the operation on an appropriate thread. michael@0: class PowerObserver { michael@0: public: michael@0: // Notification of a change in power status of the computer, such michael@0: // as from switching between battery and A/C power. michael@0: virtual void OnPowerStateChange(SystemMonitor*) = 0; michael@0: michael@0: // Notification that the system is suspending. michael@0: virtual void OnSuspend(SystemMonitor*) = 0; michael@0: michael@0: // Notification that the system is resuming. michael@0: virtual void OnResume(SystemMonitor*) = 0; michael@0: }; michael@0: michael@0: // Add a new observer. michael@0: // Can be called from any thread. michael@0: // Must not be called from within a notification callback. michael@0: void AddObserver(PowerObserver* obs); michael@0: michael@0: // Remove an existing observer. michael@0: // Can be called from any thread. michael@0: // Must not be called from within a notification callback. michael@0: void RemoveObserver(PowerObserver* obs); michael@0: michael@0: #if defined(OS_WIN) michael@0: // Windows-specific handling of a WM_POWERBROADCAST message. michael@0: // Embedders of this API should hook their top-level window michael@0: // message loop and forward WM_POWERBROADCAST through this call. michael@0: void ProcessWmPowerBroadcastMessage(int event_id); michael@0: #endif michael@0: michael@0: // Cross-platform handling of a power event. michael@0: void ProcessPowerMessage(PowerEvent event_id); michael@0: michael@0: // Constructor. michael@0: // Don't use this; access SystemMonitor via the Singleton. michael@0: SystemMonitor(); michael@0: michael@0: private: michael@0: // Platform-specific method to check whether the system is currently michael@0: // running on battery power. Returns true if running on batteries, michael@0: // false otherwise. michael@0: bool IsBatteryPower(); michael@0: michael@0: // Checks the battery status and notifies observers if the battery michael@0: // status has changed. michael@0: void BatteryCheck(); michael@0: michael@0: // Functions to trigger notifications. michael@0: void NotifyPowerStateChange(); michael@0: void NotifySuspend(); michael@0: void NotifyResume(); michael@0: michael@0: scoped_refptr > observer_list_; michael@0: bool battery_in_use_; michael@0: bool suspended_; michael@0: michael@0: #if defined(ENABLE_BATTERY_MONITORING) michael@0: base::OneShotTimer delayed_battery_check_; michael@0: #endif michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(SystemMonitor); michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif // BASE_SYSTEM_MONITOR_H_