1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/system_monitor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +// Copyright (c) 2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_SYSTEM_MONITOR_H_ 1.9 +#define BASE_SYSTEM_MONITOR_H_ 1.10 + 1.11 +#include "base/observer_list_threadsafe.h" 1.12 +#include "base/singleton.h" 1.13 + 1.14 +// Windows HiRes timers drain the battery faster so we need to know the battery 1.15 +// status. This isn't true for other platforms. 1.16 +#if defined(OS_WIN) 1.17 +#define ENABLE_BATTERY_MONITORING 1 1.18 +#else 1.19 +#undef ENABLE_BATTERY_MONITORING 1.20 +#endif // !OS_WIN 1.21 + 1.22 +namespace base { 1.23 + 1.24 +// Class for monitoring various system-related subsystems 1.25 +// such as power management, network status, etc. 1.26 +// TODO(mbelshe): Add support beyond just power management. 1.27 +class SystemMonitor { 1.28 + public: 1.29 + // Access to the Singleton 1.30 + static SystemMonitor* Get() { 1.31 + // Uses the LeakySingletonTrait because cleanup is optional. 1.32 + return 1.33 + Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get(); 1.34 + } 1.35 + 1.36 + // Start the System Monitor within a process. This method 1.37 + // is provided so that the battery check can be deferred. 1.38 + // The MessageLoop must be started before calling this 1.39 + // method. 1.40 + // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is 1.41 + // disabled. 1.42 + static void Start(); 1.43 + 1.44 + // 1.45 + // Power-related APIs 1.46 + // 1.47 + 1.48 + // Is the computer currently on battery power. 1.49 + // Can be called on any thread. 1.50 + bool BatteryPower() { 1.51 + // Using a lock here is not necessary for just a bool. 1.52 + return battery_in_use_; 1.53 + } 1.54 + 1.55 + // Normalized list of power events. 1.56 + enum PowerEvent { 1.57 + POWER_STATE_EVENT, // The Power status of the system has changed. 1.58 + SUSPEND_EVENT, // The system is being suspended. 1.59 + RESUME_EVENT // The system is being resumed. 1.60 + }; 1.61 + 1.62 + // Callbacks will be called on the thread which creates the SystemMonitor. 1.63 + // During the callback, Add/RemoveObserver will block until the callbacks 1.64 + // are finished. Observers should implement quick callback functions; if 1.65 + // lengthy operations are needed, the observer should take care to invoke 1.66 + // the operation on an appropriate thread. 1.67 + class PowerObserver { 1.68 + public: 1.69 + // Notification of a change in power status of the computer, such 1.70 + // as from switching between battery and A/C power. 1.71 + virtual void OnPowerStateChange(SystemMonitor*) = 0; 1.72 + 1.73 + // Notification that the system is suspending. 1.74 + virtual void OnSuspend(SystemMonitor*) = 0; 1.75 + 1.76 + // Notification that the system is resuming. 1.77 + virtual void OnResume(SystemMonitor*) = 0; 1.78 + }; 1.79 + 1.80 + // Add a new observer. 1.81 + // Can be called from any thread. 1.82 + // Must not be called from within a notification callback. 1.83 + void AddObserver(PowerObserver* obs); 1.84 + 1.85 + // Remove an existing observer. 1.86 + // Can be called from any thread. 1.87 + // Must not be called from within a notification callback. 1.88 + void RemoveObserver(PowerObserver* obs); 1.89 + 1.90 +#if defined(OS_WIN) 1.91 + // Windows-specific handling of a WM_POWERBROADCAST message. 1.92 + // Embedders of this API should hook their top-level window 1.93 + // message loop and forward WM_POWERBROADCAST through this call. 1.94 + void ProcessWmPowerBroadcastMessage(int event_id); 1.95 +#endif 1.96 + 1.97 + // Cross-platform handling of a power event. 1.98 + void ProcessPowerMessage(PowerEvent event_id); 1.99 + 1.100 + // Constructor. 1.101 + // Don't use this; access SystemMonitor via the Singleton. 1.102 + SystemMonitor(); 1.103 + 1.104 + private: 1.105 + // Platform-specific method to check whether the system is currently 1.106 + // running on battery power. Returns true if running on batteries, 1.107 + // false otherwise. 1.108 + bool IsBatteryPower(); 1.109 + 1.110 + // Checks the battery status and notifies observers if the battery 1.111 + // status has changed. 1.112 + void BatteryCheck(); 1.113 + 1.114 + // Functions to trigger notifications. 1.115 + void NotifyPowerStateChange(); 1.116 + void NotifySuspend(); 1.117 + void NotifyResume(); 1.118 + 1.119 + scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; 1.120 + bool battery_in_use_; 1.121 + bool suspended_; 1.122 + 1.123 +#if defined(ENABLE_BATTERY_MONITORING) 1.124 + base::OneShotTimer<SystemMonitor> delayed_battery_check_; 1.125 +#endif 1.126 + 1.127 + DISALLOW_COPY_AND_ASSIGN(SystemMonitor); 1.128 +}; 1.129 + 1.130 +} 1.131 + 1.132 +#endif // BASE_SYSTEM_MONITOR_H_