OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "app/system_monitor.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/time.h" | |
10 | |
11 static SystemMonitor* g_system_monitor = NULL; | |
12 | |
13 #if defined(ENABLE_BATTERY_MONITORING) | |
14 // The amount of time (in ms) to wait before running the initial | |
15 // battery check. | |
16 static int kDelayedBatteryCheckMs = 10 * 1000; | |
17 #endif // defined(ENABLE_BATTERY_MONITORING) | |
18 | |
19 SystemMonitor::SystemMonitor() | |
20 : observer_list_(new ObserverListThreadSafe<PowerObserver>()), | |
21 battery_in_use_(false), | |
22 suspended_(false) { | |
23 DCHECK(!g_system_monitor); | |
24 g_system_monitor = this; | |
25 | |
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) | |
32 #if defined(OS_MACOSX) | |
33 PlatformInit(); | |
34 #endif | |
35 } | |
36 | |
37 SystemMonitor::~SystemMonitor() { | |
38 #if defined(OS_MACOSX) | |
39 PlatformDestroy(); | |
40 #endif | |
41 DCHECK_EQ(this, g_system_monitor); | |
42 g_system_monitor = NULL; | |
43 } | |
44 | |
45 // static | |
46 SystemMonitor* SystemMonitor::Get() { | |
47 return g_system_monitor; | |
48 } | |
49 | |
50 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | |
51 // Suppress duplicate notifications. Some platforms may | |
52 // send multiple notifications of the same event. | |
53 switch (event_id) { | |
54 case POWER_STATE_EVENT: | |
55 { | |
56 bool on_battery = IsBatteryPower(); | |
57 if (on_battery != battery_in_use_) { | |
58 battery_in_use_ = on_battery; | |
59 NotifyPowerStateChange(); | |
60 } | |
61 } | |
62 break; | |
63 case RESUME_EVENT: | |
64 if (suspended_) { | |
65 suspended_ = false; | |
66 NotifyResume(); | |
67 } | |
68 break; | |
69 case SUSPEND_EVENT: | |
70 if (!suspended_) { | |
71 suspended_ = true; | |
72 NotifySuspend(); | |
73 } | |
74 break; | |
75 } | |
76 } | |
77 | |
78 void SystemMonitor::AddObserver(PowerObserver* obs) { | |
79 observer_list_->AddObserver(obs); | |
80 } | |
81 | |
82 void SystemMonitor::RemoveObserver(PowerObserver* obs) { | |
83 observer_list_->RemoveObserver(obs); | |
84 } | |
85 | |
86 void SystemMonitor::NotifyPowerStateChange() { | |
87 VLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
88 << " battery"; | |
89 observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | |
90 } | |
91 | |
92 void SystemMonitor::NotifySuspend() { | |
93 VLOG(1) << "Power Suspending"; | |
94 observer_list_->Notify(&PowerObserver::OnSuspend); | |
95 } | |
96 | |
97 void SystemMonitor::NotifyResume() { | |
98 VLOG(1) << "Power Resuming"; | |
99 observer_list_->Notify(&PowerObserver::OnResume); | |
100 } | |
101 | |
102 void SystemMonitor::BatteryCheck() { | |
103 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | |
104 } | |
OLD | NEW |