| 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" | 6 #include "base/logging.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 #if defined(ENABLE_BATTERY_MONITORING) |
| 11 // The amount of time (in ms) to wait before running the initial | 12 // The amount of time (in ms) to wait before running the initial |
| 12 // battery check. | 13 // battery check. |
| 13 static int kDelayedBatteryCheckMs = 10 * 1000; | 14 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 15 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 14 | 16 |
| 15 SystemMonitor::SystemMonitor() | 17 SystemMonitor::SystemMonitor() |
| 16 : battery_in_use_(false), | 18 : battery_in_use_(false), |
| 17 suspended_(false) { | 19 suspended_(false) { |
| 18 observer_list_ = new ObserverListThreadSafe<PowerObserver>(); | 20 observer_list_ = new ObserverListThreadSafe<PowerObserver>(); |
| 19 } | 21 } |
| 20 | 22 |
| 21 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | 23 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { |
| 22 // Suppress duplicate notifications. Some platforms may | 24 // Suppress duplicate notifications. Some platforms may |
| 23 // send multiple notifications of the same event. | 25 // send multiple notifications of the same event. |
| 24 switch (event_id) { | 26 switch (event_id) { |
| 25 case POWER_STATE_EVENT: | 27 case POWER_STATE_EVENT: |
| 26 { | 28 { |
| 27 bool on_battery = IsBatteryPower(); | 29 bool on_battery = IsBatteryPower(); |
| 28 if (on_battery != battery_in_use_) { | 30 if (on_battery != battery_in_use_) { |
| 29 battery_in_use_ = on_battery; | 31 battery_in_use_ = on_battery; |
| 30 NotifyPowerStateChange(); | 32 NotifyPowerStateChange(); |
| 31 } | 33 } |
| 32 } | 34 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 48 | 50 |
| 49 void SystemMonitor::AddObserver(PowerObserver* obs) { | 51 void SystemMonitor::AddObserver(PowerObserver* obs) { |
| 50 observer_list_->AddObserver(obs); | 52 observer_list_->AddObserver(obs); |
| 51 } | 53 } |
| 52 | 54 |
| 53 void SystemMonitor::RemoveObserver(PowerObserver* obs) { | 55 void SystemMonitor::RemoveObserver(PowerObserver* obs) { |
| 54 observer_list_->RemoveObserver(obs); | 56 observer_list_->RemoveObserver(obs); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void SystemMonitor::NotifyPowerStateChange() { | 59 void SystemMonitor::NotifyPowerStateChange() { |
| 58 LOG(INFO) << L"PowerStateChange: " | 60 LOG(INFO) << L"PowerStateChange: " |
| 59 << (BatteryPower() ? L"On" : L"Off") << L" battery"; | 61 << (BatteryPower() ? L"On" : L"Off") << L" battery"; |
| 60 observer_list_->Notify(&PowerObserver::OnPowerStateChange, this); | 62 observer_list_->Notify(&PowerObserver::OnPowerStateChange, this); |
| 61 } | 63 } |
| 62 | 64 |
| 63 void SystemMonitor::NotifySuspend() { | 65 void SystemMonitor::NotifySuspend() { |
| 64 LOG(INFO) << L"Power Suspending"; | 66 LOG(INFO) << L"Power Suspending"; |
| 65 observer_list_->Notify(&PowerObserver::OnSuspend, this); | 67 observer_list_->Notify(&PowerObserver::OnSuspend, this); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void SystemMonitor::NotifyResume() { | 70 void SystemMonitor::NotifyResume() { |
| 69 LOG(INFO) << L"Power Resuming"; | 71 LOG(INFO) << L"Power Resuming"; |
| 70 observer_list_->Notify(&PowerObserver::OnResume, this); | 72 observer_list_->Notify(&PowerObserver::OnResume, this); |
| 71 } | 73 } |
| 72 | 74 |
| 73 void SystemMonitor::Start() { | 75 void SystemMonitor::Start() { |
| 76 #if defined(ENABLE_BATTERY_MONITORING) |
| 74 DCHECK(MessageLoop::current()); // Can't call start too early. | 77 DCHECK(MessageLoop::current()); // Can't call start too early. |
| 75 SystemMonitor* monitor = Get(); | 78 SystemMonitor* monitor = Get(); |
| 76 monitor->delayed_battery_check_.Start( | 79 monitor->delayed_battery_check_.Start( |
| 77 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor, | 80 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor, |
| 78 &SystemMonitor::BatteryCheck); | 81 &SystemMonitor::BatteryCheck); |
| 82 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 79 } | 83 } |
| 80 | 84 |
| 81 void SystemMonitor::BatteryCheck() { | 85 void SystemMonitor::BatteryCheck() { |
| 82 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 86 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 83 } | 87 } |
| 84 | 88 |
| 85 } // namespace base | 89 } // namespace base |
| OLD | NEW |