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"
12 BackgroundSyncPowerObserver::BackgroundSyncPowerObserver(
13 const base::Closure
& power_callback
)
14 : observing_power_monitor_(false),
16 power_changed_callback_(power_callback
) {
17 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
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();
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.
42 case POWER_STATE_AVOID_DRAINING
:
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
)
57 on_battery_
= on_battery_power
;
58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
59 power_changed_callback_
);
62 } // namespace content