Home | History | Annotate | Download | only in power_monitor
      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(battery_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         {
     40           AutoLock auto_lock(source->battery_lock_);
     41           if (source->on_battery_power_ != new_on_battery_power) {
     42               changed = true;
     43               source->on_battery_power_ = new_on_battery_power;
     44           }
     45         }
     46 
     47         if (changed)
     48           monitor->NotifyPowerStateChange(new_on_battery_power);
     49       }
     50       break;
     51     case RESUME_EVENT:
     52       if (source->suspended_) {
     53         source->suspended_ = false;
     54         monitor->NotifyResume();
     55       }
     56       break;
     57     case SUSPEND_EVENT:
     58       if (!source->suspended_) {
     59         source->suspended_ = true;
     60         monitor->NotifySuspend();
     61       }
     62       break;
     63   }
     64 }
     65 
     66 }  // namespace base
     67