Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/power_monitor/power_monitor.h" |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/time.h" | |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
Keep base/time.h
Hongbo Min
2013/03/20 13:01:50
Done.
| |
| 12 | 6 |
| 13 namespace base { | 7 namespace base { |
| 14 | 8 |
| 15 static SystemMonitor* g_system_monitor = NULL; | 9 static PowerMonitor* g_power_monitor = NULL; |
| 16 | 10 |
| 17 #if defined(ENABLE_BATTERY_MONITORING) | 11 #if defined(ENABLE_BATTERY_MONITORING) |
| 18 // 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 |
| 19 // battery check. | 13 // battery check. |
| 20 static int kDelayedBatteryCheckMs = 10 * 1000; | 14 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 21 #endif // defined(ENABLE_BATTERY_MONITORING) | 15 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 22 | 16 |
| 23 SystemMonitor::SystemMonitor() | 17 PowerMonitor::PowerMonitor() |
| 24 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 18 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 25 devices_changed_observer_list_( | |
| 26 new ObserverListThreadSafe<DevicesChangedObserver>()), | |
| 27 battery_in_use_(false), | 19 battery_in_use_(false), |
| 28 suspended_(false) { | 20 suspended_(false) { |
| 29 DCHECK(!g_system_monitor); | 21 DCHECK(!g_power_monitor); |
| 30 g_system_monitor = this; | 22 g_power_monitor = this; |
| 31 | 23 |
| 32 DCHECK(MessageLoop::current()); | 24 DCHECK(MessageLoop::current()); |
| 33 #if defined(ENABLE_BATTERY_MONITORING) | 25 #if defined(ENABLE_BATTERY_MONITORING) |
| 34 delayed_battery_check_.Start(FROM_HERE, | 26 delayed_battery_check_.Start(FROM_HERE, |
| 35 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 27 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 36 &SystemMonitor::BatteryCheck); | 28 &PowerMonitor::BatteryCheck); |
| 37 #endif // defined(ENABLE_BATTERY_MONITORING) | 29 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 38 #if defined(OS_MACOSX) | 30 #if defined(OS_MACOSX) |
| 39 PlatformInit(); | 31 PlatformInit(); |
| 40 #endif | 32 #endif |
| 41 } | 33 } |
| 42 | 34 |
| 43 SystemMonitor::~SystemMonitor() { | 35 PowerMonitor::~PowerMonitor() { |
| 44 #if defined(OS_MACOSX) | 36 #if defined(OS_MACOSX) |
| 45 PlatformDestroy(); | 37 PlatformDestroy(); |
| 46 #endif | 38 #endif |
| 47 DCHECK_EQ(this, g_system_monitor); | 39 DCHECK_EQ(this, g_power_monitor); |
| 48 g_system_monitor = NULL; | 40 g_power_monitor = NULL; |
| 49 } | 41 } |
| 50 | 42 |
| 51 // static | 43 // static |
| 52 SystemMonitor* SystemMonitor::Get() { | 44 PowerMonitor* PowerMonitor::Get() { |
| 53 return g_system_monitor; | 45 return g_power_monitor; |
| 54 } | 46 } |
| 55 | 47 |
| 56 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | 48 void PowerMonitor::AddObserver(PowerObserver* obs) { |
| 49 observers_->AddObserver(obs); | |
| 50 } | |
| 51 | |
| 52 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | |
| 53 observers_->RemoveObserver(obs); | |
| 54 } | |
| 55 | |
| 56 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | |
| 57 // Suppress duplicate notifications. Some platforms may | 57 // Suppress duplicate notifications. Some platforms may |
| 58 // send multiple notifications of the same event. | 58 // send multiple notifications of the same event. |
| 59 switch (event_id) { | 59 switch (event_id) { |
| 60 case POWER_STATE_EVENT: | 60 case POWER_STATE_EVENT: |
| 61 { | 61 { |
| 62 bool on_battery = IsBatteryPower(); | 62 bool on_battery = IsBatteryPower(); |
| 63 if (on_battery != battery_in_use_) { | 63 if (on_battery != battery_in_use_) { |
| 64 battery_in_use_ = on_battery; | 64 battery_in_use_ = on_battery; |
| 65 NotifyPowerStateChange(); | 65 NotifyPowerStateChange(); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 break; | 68 break; |
| 69 case RESUME_EVENT: | 69 case RESUME_EVENT: |
| 70 if (suspended_) { | 70 if (suspended_) { |
| 71 suspended_ = false; | 71 suspended_ = false; |
| 72 NotifyResume(); | 72 NotifyResume(); |
| 73 } | 73 } |
| 74 break; | 74 break; |
| 75 case SUSPEND_EVENT: | 75 case SUSPEND_EVENT: |
| 76 if (!suspended_) { | 76 if (!suspended_) { |
| 77 suspended_ = true; | 77 suspended_ = true; |
| 78 NotifySuspend(); | 78 NotifySuspend(); |
| 79 } | 79 } |
| 80 break; | 80 break; |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { | 84 void PowerMonitor::NotifyPowerStateChange() { |
| 85 NotifyDevicesChanged(device_type); | |
| 86 } | |
| 87 | |
| 88 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { | |
| 89 power_observer_list_->AddObserver(obs); | |
| 90 } | |
| 91 | |
| 92 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { | |
| 93 power_observer_list_->RemoveObserver(obs); | |
| 94 } | |
| 95 | |
| 96 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { | |
| 97 devices_changed_observer_list_->AddObserver(obs); | |
| 98 } | |
| 99 | |
| 100 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { | |
| 101 devices_changed_observer_list_->RemoveObserver(obs); | |
| 102 } | |
| 103 | |
| 104 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { | |
| 105 DVLOG(1) << "DevicesChanged with device type " << device_type; | |
| 106 devices_changed_observer_list_->Notify( | |
| 107 &DevicesChangedObserver::OnDevicesChanged, device_type); | |
| 108 } | |
| 109 | |
| 110 void SystemMonitor::NotifyPowerStateChange() { | |
| 111 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 85 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 112 << " battery"; | 86 << " battery"; |
| 113 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, | 87 observers_->Notify(&PowerObserver::OnPowerStateChange, |
| 114 BatteryPower()); | 88 BatteryPower()); |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
nit: move up to previous line.
Hongbo Min
2013/03/20 13:01:50
Done.
| |
| 115 } | 89 } |
| 116 | 90 |
| 117 void SystemMonitor::NotifySuspend() { | 91 void PowerMonitor::NotifySuspend() { |
| 118 DVLOG(1) << "Power Suspending"; | 92 DVLOG(1) << "Power Suspending"; |
| 119 power_observer_list_->Notify(&PowerObserver::OnSuspend); | 93 observers_->Notify(&PowerObserver::OnSuspend); |
| 120 } | 94 } |
| 121 | 95 |
| 122 void SystemMonitor::NotifyResume() { | 96 void PowerMonitor::NotifyResume() { |
| 123 DVLOG(1) << "Power Resuming"; | 97 DVLOG(1) << "Power Resuming"; |
| 124 power_observer_list_->Notify(&PowerObserver::OnResume); | 98 observers_->Notify(&PowerObserver::OnResume); |
| 125 } | 99 } |
| 126 | 100 |
| 127 void SystemMonitor::BatteryCheck() { | 101 void PowerMonitor::BatteryCheck() { |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
Nit: definition order should match declaration ord
Hongbo Min
2013/03/20 13:01:50
Done.
| |
| 128 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 102 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 129 } | 103 } |
| 130 | 104 |
| 131 } // namespace base | 105 } // namespace base |
| 106 | |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
nit: remove blank line.
Hongbo Min
2013/03/20 13:01:50
Done.
| |
| OLD | NEW |