Chromium Code Reviews| 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 void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { | |
| 25 PowerMonitor* monitor = PowerMonitor::Get(); | |
| 26 if (!monitor) | |
| 27 return; | |
| 28 | |
| 29 PowerMonitorSource* source = monitor->Source(); | |
| 30 | |
| 31 // Suppress duplicate notifications. Some platforms may | |
| 32 // send multiple notifications of the same event. | |
| 33 switch (event_id) { | |
| 34 case POWER_STATE_EVENT: | |
| 35 { | |
| 36 bool new_on_battery_power = source->IsOnBatteryPowerImpl(); | |
| 37 bool changed = false; | |
| 38 | |
| 39 source->lock_.Acquire(); | |
|
piman
2013/07/15 20:51:30
nit: if you want, it's a common pattern to create
| |
| 40 if (source->on_battery_power_ != new_on_battery_power) { | |
| 41 changed = true; | |
| 42 source->on_battery_power_ = new_on_battery_power; | |
| 43 } | |
| 44 source->lock_.Release(); | |
| 45 | |
| 46 if (changed) | |
| 47 monitor->NotifyPowerStateChange(new_on_battery_power); | |
| 48 } | |
| 49 break; | |
| 50 case RESUME_EVENT: | |
| 51 if (source->suspended_) { | |
| 52 source->suspended_ = false; | |
| 53 monitor->NotifyResume(); | |
| 54 } | |
| 55 break; | |
| 56 case SUSPEND_EVENT: | |
| 57 if (!source->suspended_) { | |
| 58 source->suspended_ = true; | |
| 59 monitor->NotifySuspend(); | |
| 60 } | |
| 61 break; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 } // namespace base | |
| OLD | NEW |