diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-26 19:48:34 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-26 19:48:34 +0000 |
commit | 097ae5a93a863b604fd6dbd939bed0c2be1afed2 (patch) | |
tree | 61177362b1c1bd2d1b2438acc50ccb2c061b176e /app/system_monitor.cc | |
parent | 902c971382790170852fe5b5d22d1b79adb31056 (diff) | |
download | chromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.zip chromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.tar.gz chromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.tar.bz2 |
Make SystemMonitor not a Singleton and move it out of base
SystemMonitor makes an assumption that through its lifetime a MessageLoop exists and stays the same. It is difficult and error-prone to satisfy that when it is a Singleton. It has caused problems in the past.
Additionally, extract HighResoltionTimerManager out of time_win.cc, eliminating yet another Singleton.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/431008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33214 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/system_monitor.cc')
-rw-r--r-- | app/system_monitor.cc | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/app/system_monitor.cc b/app/system_monitor.cc new file mode 100644 index 0000000..b0e715a --- /dev/null +++ b/app/system_monitor.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2009 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. + +#include "app/system_monitor.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/time.h" + +static SystemMonitor* g_system_monitor = NULL; + +#if defined(ENABLE_BATTERY_MONITORING) +// The amount of time (in ms) to wait before running the initial +// battery check. +static int kDelayedBatteryCheckMs = 10 * 1000; +#endif // defined(ENABLE_BATTERY_MONITORING) + +SystemMonitor::SystemMonitor() + : observer_list_(new ObserverListThreadSafe<PowerObserver>()), + battery_in_use_(false), + suspended_(false) { + DCHECK(!g_system_monitor); + g_system_monitor = this; + + DCHECK(MessageLoop::current()); +#if defined(ENABLE_BATTERY_MONITORING) + delayed_battery_check_.Start( + base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, + &SystemMonitor::BatteryCheck); +#endif // defined(ENABLE_BATTERY_MONITORING) +} + +SystemMonitor::~SystemMonitor() { + DCHECK_EQ(this, g_system_monitor); + g_system_monitor = NULL; +} + +// static +SystemMonitor* SystemMonitor::Get() { + return g_system_monitor; +} + +void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { + // Suppress duplicate notifications. Some platforms may + // send multiple notifications of the same event. + switch (event_id) { + case POWER_STATE_EVENT: + { + bool on_battery = IsBatteryPower(); + if (on_battery != battery_in_use_) { + battery_in_use_ = on_battery; + NotifyPowerStateChange(); + } + } + break; + case RESUME_EVENT: + if (suspended_) { + suspended_ = false; + NotifyResume(); + } + break; + case SUSPEND_EVENT: + if (!suspended_) { + suspended_ = true; + NotifySuspend(); + } + break; + } +} + +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, BatteryPower()); +} + +void SystemMonitor::NotifySuspend() { + LOG(INFO) << L"Power Suspending"; + observer_list_->Notify(&PowerObserver::OnSuspend); +} + +void SystemMonitor::NotifyResume() { + LOG(INFO) << L"Power Resuming"; + observer_list_->Notify(&PowerObserver::OnResume); +} + +void SystemMonitor::BatteryCheck() { + ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); +} |