Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_power_observer.cc
blob087fe21e37d9a49dd029ae9a7a39d014dba72b2f
1 // Copyright 2015 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.
5 #include "content/browser/background_sync/background_sync_power_observer.h"
7 #include "base/power_monitor/power_monitor.h"
8 #include "base/thread_task_runner_handle.h"
10 namespace content {
12 BackgroundSyncPowerObserver::BackgroundSyncPowerObserver(
13 const base::Closure& power_callback)
14 : observing_power_monitor_(false),
15 on_battery_(true),
16 power_changed_callback_(power_callback) {
17 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
18 if (power_monitor) {
19 observing_power_monitor_ = true;
20 on_battery_ = power_monitor->IsOnBatteryPower();
21 power_monitor->AddObserver(this);
25 BackgroundSyncPowerObserver::~BackgroundSyncPowerObserver() {
26 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
28 if (power_monitor)
29 power_monitor->RemoveObserver(this);
32 bool BackgroundSyncPowerObserver::PowerSufficient(
33 SyncPowerState power_state) const {
34 DCHECK(observing_power_monitor_);
35 DCHECK(base::PowerMonitor::Get());
37 switch (power_state) {
38 case POWER_STATE_AUTO:
39 // TODO(jkarlin): Also check for device status, such as power saving mode
40 // or user preferences. crbug.com/482088.
41 return true;
42 case POWER_STATE_AVOID_DRAINING:
43 return !on_battery_;
46 NOTREACHED();
47 return false;
50 void BackgroundSyncPowerObserver::OnPowerStateChange(bool on_battery_power) {
51 DCHECK(observing_power_monitor_);
52 DCHECK(base::PowerMonitor::Get());
54 if (on_battery_ == on_battery_power)
55 return;
57 on_battery_ = on_battery_power;
58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
59 power_changed_callback_);
62 } // namespace content