summaryrefslogtreecommitdiffstats
path: root/base/system_monitor.h
diff options
context:
space:
mode:
authormbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-25 18:46:11 +0000
committermbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-25 18:46:11 +0000
commita20e82fa0c45fee5515a2ebfc46f38dc65446ef7 (patch)
tree3d14c292858f52cee26ee431410849499f7fcc52 /base/system_monitor.h
parentf9ef79b3c3f5e6a1b00909b30692d47ba92925e6 (diff)
downloadchromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.zip
chromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.tar.gz
chromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.tar.bz2
Implement a SystemMonitor class for monitoring system status
changes. For now, this is just implementing power changes (e.g. battery/AC power changes). This CL just contains this class; the hooking into Chrome will come in a separate CL, but I've tested the basics already. Review URL: http://codereview.chromium.org/4051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2593 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor.h')
-rw-r--r--base/system_monitor.h110
1 files changed, 110 insertions, 0 deletions
diff --git a/base/system_monitor.h b/base/system_monitor.h
new file mode 100644
index 0000000..ec4c593
--- /dev/null
+++ b/base/system_monitor.h
@@ -0,0 +1,110 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_SYSTEM_MONITOR_H_
+#define BASE_SYSTEM_MONITOR_H_
+
+#include "base/logging.h"
+#include "base/observer_list.h"
+#include "base/singleton.h"
+
+namespace base {
+
+// Singleton class for monitoring various system-related subsystems
+// such as power management, network status, etc.
+// TODO(mbelshe): Add support beyond just power management.
+class SystemMonitor {
+ public:
+ // Access to the Singleton
+ static SystemMonitor* Get() {
+ return Singleton<SystemMonitor>::get();
+ }
+
+ //
+ // Power-related APIs
+ //
+
+ // Is the computer currently on battery power.
+ bool BatteryPower() { return battery_in_use_; }
+
+ // Normalized list of power events.
+ enum PowerEvent {
+ // The Power status of the system has changed.
+ PowerStateEvent,
+
+ // The system is being suspended.
+ SuspendEvent,
+
+ // The system is being resumed.
+ ResumeEvent
+ };
+
+ class PowerObserver {
+ public:
+ // Notification of a change in power status of the computer, such
+ // as from switching between battery and A/C power.
+ virtual void OnPowerStateChange(SystemMonitor*) = 0;
+
+ // Notification that the system is suspending.
+ virtual void OnSuspend(SystemMonitor*) = 0;
+
+ // Notification that the system is resuming.
+ virtual void OnResume(SystemMonitor*) = 0;
+ };
+
+ void AddObserver(PowerObserver* obs) {
+ observer_list_.AddObserver(obs);
+ }
+
+ void RemoveObserver(PowerObserver* obs) {
+ observer_list_.RemoveObserver(obs);
+ }
+
+ void NotifyPowerStateChange() {
+ LOG(INFO) << L"PowerStateChange: "
+ << (BatteryPower() ? L"On" : L"Off") << L" battery";
+ FOR_EACH_OBSERVER(PowerObserver, observer_list_,
+ OnPowerStateChange(this));
+ }
+
+ void NotifySuspend() {
+ FOR_EACH_OBSERVER(PowerObserver, observer_list_, OnSuspend(this));
+ }
+
+ void NotifyResume() {
+ FOR_EACH_OBSERVER(PowerObserver, observer_list_, OnResume(this));
+ }
+
+ // Constructor.
+ // Don't use this; access SystemMonitor via the Singleton.
+ SystemMonitor();
+
+#if defined(OS_WIN)
+ // Windows-specific handling of a WM_POWERBROADCAST message.
+ // Embedders of this API should hook their top-level window
+ // message loop and forward WM_POWERBROADCAST through this call.
+ void ProcessWmPowerBroadcastMessage(int event_id);
+#endif
+
+ // Cross-platform handling of a power event.
+ // This is only exposed for testing.
+ void ProcessPowerMessage(PowerEvent event_id);
+
+ private:
+ // Platform-specific method to check whether the system is currently
+ // running on battery power. Returns true if running on batteries,
+ // false otherwise.
+ bool IsBatteryPower();
+
+ ObserverList<PowerObserver> observer_list_;
+ bool battery_in_use_;
+ bool suspended_;
+
+ DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
+};
+
+}
+
+#endif // BASE_SYSTEM_MONITOR_H_
+