Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 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/power_monitor/power_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/power_monitor/power_monitor_device_source.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 static PowerMonitor* g_power_monitor = NULL; | 11 static PowerMonitor* g_power_monitor = NULL; |
| 12 | 12 |
| 13 #if defined(ENABLE_BATTERY_MONITORING) | 13 // This constructor is provided to preserve backwards compatibility with the |
|
vandebo (ex-Chrome)
2013/07/02 22:53:12
Hmm, I wonder if we should just change the browser
| |
| 14 // The amount of time (in ms) to wait before running the initial | 14 // most common use case, where the power monitor is created on a UI thread to |
| 15 // battery check. | 15 // directly monitor system power events. |
| 16 static int kDelayedBatteryCheckMs = 10 * 1000; | |
| 17 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 18 | |
| 19 PowerMonitor::PowerMonitor() | 16 PowerMonitor::PowerMonitor() |
| 20 : observers_(new ObserverListThreadSafe<PowerObserver>()), | 17 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 21 battery_in_use_(false), | 18 source_(new PowerMonitorDeviceSource()) { |
| 22 suspended_(false) { | |
| 23 DCHECK(!g_power_monitor); | 19 DCHECK(!g_power_monitor); |
| 24 g_power_monitor = this; | 20 g_power_monitor = this; |
| 21 } | |
| 25 | 22 |
| 26 DCHECK(MessageLoop::current()); | 23 PowerMonitor::PowerMonitor(PowerMonitorSource* source) |
| 27 #if defined(ENABLE_BATTERY_MONITORING) | 24 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 28 delayed_battery_check_.Start(FROM_HERE, | 25 source_(source) { |
| 29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 26 DCHECK(!g_power_monitor); |
| 30 &PowerMonitor::BatteryCheck); | 27 g_power_monitor = this; |
| 31 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 32 #if defined(OS_MACOSX) | |
| 33 PlatformInit(); | |
| 34 #endif | |
| 35 } | 28 } |
| 36 | 29 |
| 37 PowerMonitor::~PowerMonitor() { | 30 PowerMonitor::~PowerMonitor() { |
| 38 #if defined(OS_MACOSX) | |
| 39 PlatformDestroy(); | |
| 40 #endif | |
| 41 DCHECK_EQ(this, g_power_monitor); | 31 DCHECK_EQ(this, g_power_monitor); |
| 42 g_power_monitor = NULL; | 32 g_power_monitor = NULL; |
| 43 } | 33 } |
| 44 | 34 |
| 45 // static | 35 // static |
| 46 PowerMonitor* PowerMonitor::Get() { | 36 PowerMonitor* PowerMonitor::Get() { |
| 47 return g_power_monitor; | 37 return g_power_monitor; |
| 48 } | 38 } |
| 49 | 39 |
| 50 void PowerMonitor::AddObserver(PowerObserver* obs) { | 40 void PowerMonitor::AddObserver(PowerObserver* obs) { |
| 51 observers_->AddObserver(obs); | 41 observers_->AddObserver(obs); |
| 52 } | 42 } |
| 53 | 43 |
| 54 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | 44 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
| 55 observers_->RemoveObserver(obs); | 45 observers_->RemoveObserver(obs); |
| 56 } | 46 } |
| 57 | 47 |
| 58 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | 48 PowerMonitorSource* PowerMonitor::Source() { |
| 59 // Suppress duplicate notifications. Some platforms may | 49 return source_.get(); |
| 60 // send multiple notifications of the same event. | |
| 61 switch (event_id) { | |
| 62 case POWER_STATE_EVENT: | |
| 63 { | |
| 64 bool on_battery = IsBatteryPower(); | |
| 65 if (on_battery != battery_in_use_) { | |
| 66 battery_in_use_ = on_battery; | |
| 67 NotifyPowerStateChange(); | |
| 68 } | |
| 69 } | |
| 70 break; | |
| 71 case RESUME_EVENT: | |
| 72 if (suspended_) { | |
| 73 suspended_ = false; | |
| 74 NotifyResume(); | |
| 75 } | |
| 76 break; | |
| 77 case SUSPEND_EVENT: | |
| 78 if (!suspended_) { | |
| 79 suspended_ = true; | |
| 80 NotifySuspend(); | |
| 81 } | |
| 82 break; | |
| 83 } | |
| 84 } | 50 } |
| 85 | 51 |
| 86 void PowerMonitor::BatteryCheck() { | 52 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
|
apatrick_chromium
2013/07/02 19:39:29
In the browser process, this is invoked on the UI
bajones
2013/07/02 20:35:26
I considered just adding the message handlers dire
vandebo (ex-Chrome)
2013/07/02 22:53:12
ObserverListThreadSafe explicitly says it's ok to
| |
| 87 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | 53 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
| 88 } | |
| 89 | |
| 90 void PowerMonitor::NotifyPowerStateChange() { | |
| 91 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
| 92 << " battery"; | 54 << " battery"; |
| 93 observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | 55 observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
| 94 } | 56 } |
| 95 | 57 |
| 96 void PowerMonitor::NotifySuspend() { | 58 void PowerMonitor::NotifySuspend() { |
| 97 DVLOG(1) << "Power Suspending"; | 59 DVLOG(1) << "Power Suspending"; |
| 98 observers_->Notify(&PowerObserver::OnSuspend); | 60 observers_->Notify(&PowerObserver::OnSuspend); |
| 99 } | 61 } |
| 100 | 62 |
| 101 void PowerMonitor::NotifyResume() { | 63 void PowerMonitor::NotifyResume() { |
| 102 DVLOG(1) << "Power Resuming"; | 64 DVLOG(1) << "Power Resuming"; |
| 103 observers_->Notify(&PowerObserver::OnResume); | 65 observers_->Notify(&PowerObserver::OnResume); |
| 104 } | 66 } |
| 105 | 67 |
| 106 } // namespace base | 68 } // namespace base |
| OLD | NEW |