OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/system_monitor.h" | 5 #include "base/system_monitor.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/message_loop.h" |
6 | 8 |
7 namespace base { | 9 namespace base { |
8 | 10 |
| 11 // The amount of time (in ms) to wait before running the initial |
| 12 // battery check. |
| 13 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 14 |
9 SystemMonitor::SystemMonitor() | 15 SystemMonitor::SystemMonitor() |
10 : battery_in_use_(IsBatteryPower()), | 16 : battery_in_use_(false), |
11 suspended_(false) { | 17 suspended_(false) { |
| 18 observer_list_ = new ObserverListThreadSafe<PowerObserver>(); |
12 } | 19 } |
13 | 20 |
14 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | 21 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { |
15 // Suppress duplicate notifications. Some platforms may | 22 // Suppress duplicate notifications. Some platforms may |
16 // send multiple notifications of the same event. | 23 // send multiple notifications of the same event. |
17 switch (event_id) { | 24 switch (event_id) { |
18 case PowerStateEvent: | 25 case POWER_STATE_EVENT: |
19 { | 26 { |
20 bool on_battery = IsBatteryPower(); | 27 bool on_battery = IsBatteryPower(); |
21 if (on_battery != battery_in_use_) { | 28 if (on_battery != battery_in_use_) { |
22 battery_in_use_ = on_battery; | 29 battery_in_use_ = on_battery; |
23 NotifyPowerStateChange(); | 30 NotifyPowerStateChange(); |
24 } | 31 } |
25 } | 32 } |
26 break; | 33 break; |
27 case ResumeEvent: | 34 case RESUME_EVENT: |
28 if (suspended_) { | 35 if (suspended_) { |
29 suspended_ = false; | 36 suspended_ = false; |
30 NotifyResume(); | 37 NotifyResume(); |
31 } | 38 } |
32 break; | 39 break; |
33 case SuspendEvent: | 40 case SUSPEND_EVENT: |
34 if (!suspended_) { | 41 if (!suspended_) { |
35 suspended_ = true; | 42 suspended_ = true; |
36 NotifySuspend(); | 43 NotifySuspend(); |
37 } | 44 } |
38 break; | 45 break; |
39 } | 46 } |
40 } | 47 } |
41 | 48 |
| 49 void SystemMonitor::AddObserver(PowerObserver* obs) { |
| 50 observer_list_->AddObserver(obs); |
| 51 } |
| 52 |
| 53 void SystemMonitor::RemoveObserver(PowerObserver* obs) { |
| 54 observer_list_->RemoveObserver(obs); |
| 55 } |
| 56 |
| 57 void SystemMonitor::NotifyPowerStateChange() { |
| 58 LOG(INFO) << L"PowerStateChange: " |
| 59 << (BatteryPower() ? L"On" : L"Off") << L" battery"; |
| 60 observer_list_->Notify(&PowerObserver::OnPowerStateChange, this); |
| 61 } |
| 62 |
| 63 void SystemMonitor::NotifySuspend() { |
| 64 LOG(INFO) << L"Power Suspending"; |
| 65 observer_list_->Notify(&PowerObserver::OnSuspend, this); |
| 66 } |
| 67 |
| 68 void SystemMonitor::NotifyResume() { |
| 69 LOG(INFO) << L"Power Resuming"; |
| 70 observer_list_->Notify(&PowerObserver::OnResume, this); |
| 71 } |
| 72 |
| 73 void SystemMonitor::Start() { |
| 74 SystemMonitor* monitor = Get(); |
| 75 monitor->delayed_battery_check_.Start( |
| 76 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor, |
| 77 &SystemMonitor::BatteryCheck); |
| 78 } |
| 79 |
| 80 void SystemMonitor::BatteryCheck() { |
| 81 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 82 } |
| 83 |
42 } // namespace base | 84 } // namespace base |
OLD | NEW |