summaryrefslogtreecommitdiffstats
path: root/base/system_monitor.cc
diff options
context:
space:
mode:
authormbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-11-03 17:15:07 +0000
committermbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-11-03 17:15:07 +0000
commit67bdbe0e44d9af419bdfa7d658d270c8f1005178 (patch)
tree9cfbf67f81d6f6f01cbea978b6cc890fc2c6fa60 /base/system_monitor.cc
parentc1769d059bf8119de2df418d078310d382f470e6 (diff)
downloadchromium_src-67bdbe0e44d9af419bdfa7d658d270c8f1005178.zip
chromium_src-67bdbe0e44d9af419bdfa7d658d270c8f1005178.tar.gz
chromium_src-67bdbe0e44d9af419bdfa7d658d270c8f1005178.tar.bz2
Make the SystemMonitor observer list thread safe.
This is done by using the new observer_list_threadsafe. Review URL: http://codereview.chromium.org/4288 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4452 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor.cc')
-rw-r--r--base/system_monitor.cc51
1 files changed, 47 insertions, 4 deletions
diff --git a/base/system_monitor.cc b/base/system_monitor.cc
index 37dc48a..1d44e3c 100644
--- a/base/system_monitor.cc
+++ b/base/system_monitor.cc
@@ -3,19 +3,26 @@
// found in the LICENSE file.
#include "base/system_monitor.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
namespace base {
+// The amount of time (in ms) to wait before running the initial
+// battery check.
+static int kDelayedBatteryCheckMs = 10 * 1000;
+
SystemMonitor::SystemMonitor()
- : battery_in_use_(IsBatteryPower()),
+ : battery_in_use_(false),
suspended_(false) {
+ observer_list_ = new ObserverListThreadSafe<PowerObserver>();
}
void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
// Suppress duplicate notifications. Some platforms may
// send multiple notifications of the same event.
switch (event_id) {
- case PowerStateEvent:
+ case POWER_STATE_EVENT:
{
bool on_battery = IsBatteryPower();
if (on_battery != battery_in_use_) {
@@ -24,13 +31,13 @@ void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
}
}
break;
- case ResumeEvent:
+ case RESUME_EVENT:
if (suspended_) {
suspended_ = false;
NotifyResume();
}
break;
- case SuspendEvent:
+ case SUSPEND_EVENT:
if (!suspended_) {
suspended_ = true;
NotifySuspend();
@@ -39,4 +46,40 @@ void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
}
}
+void SystemMonitor::AddObserver(PowerObserver* obs) {
+ observer_list_->AddObserver(obs);
+}
+
+void SystemMonitor::RemoveObserver(PowerObserver* obs) {
+ observer_list_->RemoveObserver(obs);
+}
+
+void SystemMonitor::NotifyPowerStateChange() {
+ LOG(INFO) << L"PowerStateChange: "
+ << (BatteryPower() ? L"On" : L"Off") << L" battery";
+ observer_list_->Notify(&PowerObserver::OnPowerStateChange, this);
+}
+
+void SystemMonitor::NotifySuspend() {
+ LOG(INFO) << L"Power Suspending";
+ observer_list_->Notify(&PowerObserver::OnSuspend, this);
+}
+
+void SystemMonitor::NotifyResume() {
+ LOG(INFO) << L"Power Resuming";
+ observer_list_->Notify(&PowerObserver::OnResume, this);
+}
+
+void SystemMonitor::Start() {
+ DCHECK(MessageLoop::current()); // Can't call start too early.
+ SystemMonitor* monitor = Get();
+ monitor->delayed_battery_check_.Start(
+ TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor,
+ &SystemMonitor::BatteryCheck);
+}
+
+void SystemMonitor::BatteryCheck() {
+ ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
+}
+
} // namespace base