OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/power_monitor/power_monitor_source.h" |
| 6 |
| 7 #include "base/power_monitor/power_monitor.h" |
| 8 |
| 9 namespace base { |
| 10 |
| 11 PowerMonitorSource::PowerMonitorSource() |
| 12 : on_battery_power_(false), |
| 13 suspended_(false) { |
| 14 } |
| 15 |
| 16 PowerMonitorSource::~PowerMonitorSource() { |
| 17 } |
| 18 |
| 19 bool PowerMonitorSource::IsOnBatteryPower() { |
| 20 AutoLock auto_lock(lock_); |
| 21 return on_battery_power_; |
| 22 } |
| 23 |
| 24 bool PowerMonitorSource::UpdateOnBatteryPower() { |
| 25 AutoLock auto_lock(lock_); |
| 26 bool old_on_battery_power = on_battery_power_; |
| 27 on_battery_power_ = IsOnBatteryPowerImpl(); |
| 28 return old_on_battery_power != on_battery_power_; |
| 29 } |
| 30 |
| 31 void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { |
| 32 PowerMonitor* monitor = PowerMonitor::Get(); |
| 33 if (!monitor) |
| 34 return; |
| 35 |
| 36 PowerMonitorSource* source = monitor->Source(); |
| 37 |
| 38 // Suppress duplicate notifications. Some platforms may |
| 39 // send multiple notifications of the same event. |
| 40 switch (event_id) { |
| 41 case POWER_STATE_EVENT: |
| 42 if (source->UpdateOnBatteryPower()) |
| 43 monitor->NotifyPowerStateChange(source->IsOnBatteryPower()); |
| 44 break; |
| 45 case RESUME_EVENT: |
| 46 if (source->suspended_) { |
| 47 source->suspended_ = false; |
| 48 monitor->NotifyResume(); |
| 49 } |
| 50 break; |
| 51 case SUSPEND_EVENT: |
| 52 if (!source->suspended_) { |
| 53 source->suspended_ = true; |
| 54 monitor->NotifySuspend(); |
| 55 } |
| 56 break; |
| 57 } |
| 58 } |
| 59 |
| 60 } // namespace base |
OLD | NEW |