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: #include "base/system_monitor.h" michael@0: #include "base/logging.h" michael@0: #include "base/message_loop.h" michael@0: michael@0: namespace base { michael@0: michael@0: #if defined(ENABLE_BATTERY_MONITORING) michael@0: // The amount of time (in ms) to wait before running the initial michael@0: // battery check. michael@0: static int kDelayedBatteryCheckMs = 10 * 1000; michael@0: #endif // defined(ENABLE_BATTERY_MONITORING) michael@0: michael@0: SystemMonitor::SystemMonitor() michael@0: : battery_in_use_(false), michael@0: suspended_(false) { michael@0: observer_list_ = new ObserverListThreadSafe(); michael@0: } michael@0: michael@0: void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { michael@0: // Suppress duplicate notifications. Some platforms may michael@0: // send multiple notifications of the same event. michael@0: switch (event_id) { michael@0: case POWER_STATE_EVENT: michael@0: { michael@0: bool on_battery = IsBatteryPower(); michael@0: if (on_battery != battery_in_use_) { michael@0: battery_in_use_ = on_battery; michael@0: NotifyPowerStateChange(); michael@0: } michael@0: } michael@0: break; michael@0: case RESUME_EVENT: michael@0: if (suspended_) { michael@0: suspended_ = false; michael@0: NotifyResume(); michael@0: } michael@0: break; michael@0: case SUSPEND_EVENT: michael@0: if (!suspended_) { michael@0: suspended_ = true; michael@0: NotifySuspend(); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void SystemMonitor::AddObserver(PowerObserver* obs) { michael@0: observer_list_->AddObserver(obs); michael@0: } michael@0: michael@0: void SystemMonitor::RemoveObserver(PowerObserver* obs) { michael@0: observer_list_->RemoveObserver(obs); michael@0: } michael@0: michael@0: void SystemMonitor::NotifyPowerStateChange() { michael@0: CHROMIUM_LOG(INFO) << "PowerStateChange: " michael@0: << (BatteryPower() ? "On" : "Off") << " battery"; michael@0: observer_list_->Notify(&PowerObserver::OnPowerStateChange, this); michael@0: } michael@0: michael@0: void SystemMonitor::NotifySuspend() { michael@0: CHROMIUM_LOG(INFO) << "Power Suspending"; michael@0: observer_list_->Notify(&PowerObserver::OnSuspend, this); michael@0: } michael@0: michael@0: void SystemMonitor::NotifyResume() { michael@0: CHROMIUM_LOG(INFO) << "Power Resuming"; michael@0: observer_list_->Notify(&PowerObserver::OnResume, this); michael@0: } michael@0: michael@0: void SystemMonitor::Start() { michael@0: #if defined(ENABLE_BATTERY_MONITORING) michael@0: DCHECK(MessageLoop::current()); // Can't call start too early. michael@0: SystemMonitor* monitor = Get(); michael@0: monitor->delayed_battery_check_.Start( michael@0: TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor, michael@0: &SystemMonitor::BatteryCheck); michael@0: #endif // defined(ENABLE_BATTERY_MONITORING) michael@0: } michael@0: michael@0: void SystemMonitor::BatteryCheck() { michael@0: ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); michael@0: } michael@0: michael@0: } // namespace base