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 #include "build/build_config.h" 9 10 namespace base { 11 12 PowerMonitorSource::PowerMonitorSource() = default; 13 PowerMonitorSource::~PowerMonitorSource() = default; 14 IsOnBatteryPower()15bool PowerMonitorSource::IsOnBatteryPower() { 16 AutoLock auto_lock(battery_lock_); 17 return on_battery_power_; 18 } 19 ProcessPowerEvent(PowerEvent event_id)20void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { 21 PowerMonitor* monitor = PowerMonitor::Get(); 22 if (!monitor) 23 return; 24 25 PowerMonitorSource* source = monitor->Source(); 26 27 // Suppress duplicate notifications. Some platforms may 28 // send multiple notifications of the same event. 29 switch (event_id) { 30 case POWER_STATE_EVENT: 31 { 32 bool new_on_battery_power = source->IsOnBatteryPowerImpl(); 33 bool changed = false; 34 35 { 36 AutoLock auto_lock(source->battery_lock_); 37 if (source->on_battery_power_ != new_on_battery_power) { 38 changed = true; 39 source->on_battery_power_ = new_on_battery_power; 40 } 41 } 42 43 if (changed) 44 monitor->NotifyPowerStateChange(new_on_battery_power); 45 } 46 break; 47 case RESUME_EVENT: 48 if (source->suspended_) { 49 source->suspended_ = false; 50 monitor->NotifyResume(); 51 } 52 break; 53 case SUSPEND_EVENT: 54 if (!source->suspended_) { 55 source->suspended_ = true; 56 monitor->NotifySuspend(); 57 } 58 break; 59 } 60 } 61 SetInitialOnBatteryPowerState(bool on_battery_power)62void PowerMonitorSource::SetInitialOnBatteryPowerState(bool on_battery_power) { 63 // Must only be called before a monitor exists, otherwise the caller should 64 // have just used a normal ProcessPowerEvent(POWER_STATE_EVENT) call. 65 DCHECK(!PowerMonitor::Get()); 66 on_battery_power_ = on_battery_power; 67 } 68 69 } // namespace base 70