1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "app/system_monitor.h"
7 #include "base/logging.h"
8 #include "base/message_loop.h"
11 static SystemMonitor
* g_system_monitor
= NULL
;
13 #if defined(ENABLE_BATTERY_MONITORING)
14 // The amount of time (in ms) to wait before running the initial
16 static int kDelayedBatteryCheckMs
= 10 * 1000;
17 #endif // defined(ENABLE_BATTERY_MONITORING)
19 SystemMonitor::SystemMonitor()
20 : observer_list_(new ObserverListThreadSafe
<PowerObserver
>()),
21 battery_in_use_(false),
23 DCHECK(!g_system_monitor
);
24 g_system_monitor
= this;
26 DCHECK(MessageLoop::current());
27 #if defined(ENABLE_BATTERY_MONITORING)
28 delayed_battery_check_
.Start(
29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs
), this,
30 &SystemMonitor::BatteryCheck
);
31 #endif // defined(ENABLE_BATTERY_MONITORING)
34 SystemMonitor::~SystemMonitor() {
35 DCHECK_EQ(this, g_system_monitor
);
36 g_system_monitor
= NULL
;
40 SystemMonitor
* SystemMonitor::Get() {
41 return g_system_monitor
;
44 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id
) {
45 // Suppress duplicate notifications. Some platforms may
46 // send multiple notifications of the same event.
48 case POWER_STATE_EVENT
:
50 bool on_battery
= IsBatteryPower();
51 if (on_battery
!= battery_in_use_
) {
52 battery_in_use_
= on_battery
;
53 NotifyPowerStateChange();
72 void SystemMonitor::AddObserver(PowerObserver
* obs
) {
73 observer_list_
->AddObserver(obs
);
76 void SystemMonitor::RemoveObserver(PowerObserver
* obs
) {
77 observer_list_
->RemoveObserver(obs
);
80 void SystemMonitor::NotifyPowerStateChange() {
81 LOG(INFO
) << L
"PowerStateChange: "
82 << (BatteryPower() ? L
"On" : L
"Off") << L
" battery";
83 observer_list_
->Notify(&PowerObserver::OnPowerStateChange
, BatteryPower());
86 void SystemMonitor::NotifySuspend() {
87 LOG(INFO
) << L
"Power Suspending";
88 observer_list_
->Notify(&PowerObserver::OnSuspend
);
91 void SystemMonitor::NotifyResume() {
92 LOG(INFO
) << L
"Power Resuming";
93 observer_list_
->Notify(&PowerObserver::OnResume
);
96 void SystemMonitor::BatteryCheck() {
97 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT
);