Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2188)

Unified Diff: base/system_monitor.cc

Issue 4288: Make the SystemMonitor observer list thread safe.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/system_monitor.h ('k') | base/system_monitor_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/system_monitor.cc
===================================================================
--- base/system_monitor.cc (revision 4449)
+++ base/system_monitor.cc (working copy)
@@ -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 @@
}
}
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,39 @@
}
}
+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() {
+ SystemMonitor* monitor = Get();
+ monitor->delayed_battery_check_.Start(
+ TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor,
+ &SystemMonitor::BatteryCheck);
+}
+
+void SystemMonitor::BatteryCheck() {
+ ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
+}
+
} // namespace base
« no previous file with comments | « base/system_monitor.h ('k') | base/system_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698