1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/system_monitor.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 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 +#include "base/system_monitor.h" 1.9 +#include "base/logging.h" 1.10 +#include "base/message_loop.h" 1.11 + 1.12 +namespace base { 1.13 + 1.14 +#if defined(ENABLE_BATTERY_MONITORING) 1.15 +// The amount of time (in ms) to wait before running the initial 1.16 +// battery check. 1.17 +static int kDelayedBatteryCheckMs = 10 * 1000; 1.18 +#endif // defined(ENABLE_BATTERY_MONITORING) 1.19 + 1.20 +SystemMonitor::SystemMonitor() 1.21 + : battery_in_use_(false), 1.22 + suspended_(false) { 1.23 + observer_list_ = new ObserverListThreadSafe<PowerObserver>(); 1.24 +} 1.25 + 1.26 +void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { 1.27 + // Suppress duplicate notifications. Some platforms may 1.28 + // send multiple notifications of the same event. 1.29 + switch (event_id) { 1.30 + case POWER_STATE_EVENT: 1.31 + { 1.32 + bool on_battery = IsBatteryPower(); 1.33 + if (on_battery != battery_in_use_) { 1.34 + battery_in_use_ = on_battery; 1.35 + NotifyPowerStateChange(); 1.36 + } 1.37 + } 1.38 + break; 1.39 + case RESUME_EVENT: 1.40 + if (suspended_) { 1.41 + suspended_ = false; 1.42 + NotifyResume(); 1.43 + } 1.44 + break; 1.45 + case SUSPEND_EVENT: 1.46 + if (!suspended_) { 1.47 + suspended_ = true; 1.48 + NotifySuspend(); 1.49 + } 1.50 + break; 1.51 + } 1.52 +} 1.53 + 1.54 +void SystemMonitor::AddObserver(PowerObserver* obs) { 1.55 + observer_list_->AddObserver(obs); 1.56 +} 1.57 + 1.58 +void SystemMonitor::RemoveObserver(PowerObserver* obs) { 1.59 + observer_list_->RemoveObserver(obs); 1.60 +} 1.61 + 1.62 +void SystemMonitor::NotifyPowerStateChange() { 1.63 + CHROMIUM_LOG(INFO) << "PowerStateChange: " 1.64 + << (BatteryPower() ? "On" : "Off") << " battery"; 1.65 + observer_list_->Notify(&PowerObserver::OnPowerStateChange, this); 1.66 +} 1.67 + 1.68 +void SystemMonitor::NotifySuspend() { 1.69 + CHROMIUM_LOG(INFO) << "Power Suspending"; 1.70 + observer_list_->Notify(&PowerObserver::OnSuspend, this); 1.71 +} 1.72 + 1.73 +void SystemMonitor::NotifyResume() { 1.74 + CHROMIUM_LOG(INFO) << "Power Resuming"; 1.75 + observer_list_->Notify(&PowerObserver::OnResume, this); 1.76 +} 1.77 + 1.78 +void SystemMonitor::Start() { 1.79 +#if defined(ENABLE_BATTERY_MONITORING) 1.80 + DCHECK(MessageLoop::current()); // Can't call start too early. 1.81 + SystemMonitor* monitor = Get(); 1.82 + monitor->delayed_battery_check_.Start( 1.83 + TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor, 1.84 + &SystemMonitor::BatteryCheck); 1.85 +#endif // defined(ENABLE_BATTERY_MONITORING) 1.86 +} 1.87 + 1.88 +void SystemMonitor::BatteryCheck() { 1.89 + ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 1.90 +} 1.91 + 1.92 +} // namespace base