| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/system_monitor.h" | 5 #include "base/system_monitor/system_monitor.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 static SystemMonitor* g_system_monitor = NULL; | 13 static SystemMonitor* g_system_monitor = NULL; |
| 14 | 14 |
| 15 #if defined(ENABLE_BATTERY_MONITORING) | 15 #if defined(ENABLE_BATTERY_MONITORING) |
| 16 // The amount of time (in ms) to wait before running the initial | 16 // The amount of time (in ms) to wait before running the initial |
| 17 // battery check. | 17 // battery check. |
| 18 static int kDelayedBatteryCheckMs = 10 * 1000; | 18 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 19 #endif // defined(ENABLE_BATTERY_MONITORING) | 19 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 20 | 20 |
| 21 SystemMonitor::SystemMonitor() | 21 SystemMonitor::SystemMonitor() |
| 22 : observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 22 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
| 23 device_change_observer_list_( |
| 24 new ObserverListThreadSafe<DeviceChangeObserver>()), |
| 23 battery_in_use_(false), | 25 battery_in_use_(false), |
| 24 suspended_(false) { | 26 suspended_(false) { |
| 25 DCHECK(!g_system_monitor); | 27 DCHECK(!g_system_monitor); |
| 26 g_system_monitor = this; | 28 g_system_monitor = this; |
| 27 | 29 |
| 28 DCHECK(MessageLoop::current()); | 30 DCHECK(MessageLoop::current()); |
| 29 #if defined(ENABLE_BATTERY_MONITORING) | 31 #if defined(ENABLE_BATTERY_MONITORING) |
| 30 delayed_battery_check_.Start(FROM_HERE, | 32 delayed_battery_check_.Start(FROM_HERE, |
| 31 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 33 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 32 &SystemMonitor::BatteryCheck); | 34 &SystemMonitor::BatteryCheck); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 break; | 72 break; |
| 71 case SUSPEND_EVENT: | 73 case SUSPEND_EVENT: |
| 72 if (!suspended_) { | 74 if (!suspended_) { |
| 73 suspended_ = true; | 75 suspended_ = true; |
| 74 NotifySuspend(); | 76 NotifySuspend(); |
| 75 } | 77 } |
| 76 break; | 78 break; |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 | 81 |
| 82 void SystemMonitor::ProcessDeviceChange() { |
| 83 NotifyDeviceChange(); |
| 84 } |
| 85 |
| 80 void SystemMonitor::AddObserver(PowerObserver* obs) { | 86 void SystemMonitor::AddObserver(PowerObserver* obs) { |
| 81 observer_list_->AddObserver(obs); | 87 power_observer_list_->AddObserver(obs); |
| 82 } | 88 } |
| 83 | 89 |
| 84 void SystemMonitor::RemoveObserver(PowerObserver* obs) { | 90 void SystemMonitor::RemoveObserver(PowerObserver* obs) { |
| 85 observer_list_->RemoveObserver(obs); | 91 power_observer_list_->RemoveObserver(obs); |
| 92 } |
| 93 |
| 94 void SystemMonitor::AddObserver(DeviceChangeObserver* obs) { |
| 95 device_change_observer_list_->AddObserver(obs); |
| 96 } |
| 97 |
| 98 void SystemMonitor::RemoveObserver(DeviceChangeObserver* obs) { |
| 99 device_change_observer_list_->RemoveObserver(obs); |
| 100 } |
| 101 |
| 102 void SystemMonitor::NotifyDeviceChange() { |
| 103 DVLOG(1) << "DeviceChange"; |
| 104 device_change_observer_list_->Notify(&DeviceChangeObserver::OnDeviceChange); |
| 86 } | 105 } |
| 87 | 106 |
| 88 void SystemMonitor::NotifyPowerStateChange() { | 107 void SystemMonitor::NotifyPowerStateChange() { |
| 89 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 108 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 90 << " battery"; | 109 << " battery"; |
| 91 observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | 110 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, |
| 111 BatteryPower()); |
| 92 } | 112 } |
| 93 | 113 |
| 94 void SystemMonitor::NotifySuspend() { | 114 void SystemMonitor::NotifySuspend() { |
| 95 DVLOG(1) << "Power Suspending"; | 115 DVLOG(1) << "Power Suspending"; |
| 96 observer_list_->Notify(&PowerObserver::OnSuspend); | 116 power_observer_list_->Notify(&PowerObserver::OnSuspend); |
| 97 } | 117 } |
| 98 | 118 |
| 99 void SystemMonitor::NotifyResume() { | 119 void SystemMonitor::NotifyResume() { |
| 100 DVLOG(1) << "Power Resuming"; | 120 DVLOG(1) << "Power Resuming"; |
| 101 observer_list_->Notify(&PowerObserver::OnResume); | 121 power_observer_list_->Notify(&PowerObserver::OnResume); |
| 102 } | 122 } |
| 103 | 123 |
| 104 void SystemMonitor::BatteryCheck() { | 124 void SystemMonitor::BatteryCheck() { |
| 105 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 125 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 106 } | 126 } |
| 107 | 127 |
| 108 } // namespace base | 128 } // namespace base |
| OLD | NEW |