Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1758)

Side by Side Diff: components/download/internal/scheduler/battery_listener.cc

Issue 2867073004: Battery listener for download service in native code. (Closed)
Patch Set: Work on feedbacks. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/download/internal/scheduler/battery_listener.h"
6
7 #include "base/power_monitor/power_monitor.h"
8
9 namespace download {
10
11 // Helper function that converts the battery status to battery requirement.
12 SchedulingParams::BatteryRequirements ToBatteryRequirement(
13 bool on_battery_power) {
14 return on_battery_power
15 ? SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE
16 : SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;
17 }
18
19 BatteryListener::BatteryListener() = default;
20
21 BatteryListener::~BatteryListener() {
22 Stop();
23 }
24
25 SchedulingParams::BatteryRequirements BatteryListener::CurrentBatteryStatus()
26 const {
27 return ToBatteryRequirement(base::PowerMonitor::Get()->IsOnBatteryPower());
28 }
29
30 void BatteryListener::Start() {
31 base::PowerMonitor* monitor = base::PowerMonitor::Get();
32 DCHECK(monitor);
33 monitor->AddObserver(this);
34 }
35
36 void BatteryListener::Stop() {
37 base::PowerMonitor::Get()->RemoveObserver(this);
38 }
39
40 void BatteryListener::AddObserver(Observer* observer) {
41 observers_.AddObserver(observer);
42 }
43
44 void BatteryListener::RemoveObserver(Observer* observer) {
45 observers_.RemoveObserver(observer);
46 }
47
48 void BatteryListener::OnPowerStateChange(bool on_battery_power) {
49 NotifyBatteryChange(ToBatteryRequirement(on_battery_power));
50 }
51
52 void BatteryListener::NotifyBatteryChange(
53 SchedulingParams::BatteryRequirements current_battery) {
54 for (auto& observer : observers_)
55 observer.OnBatteryChange(current_battery);
56 }
57
58 } // namespace download
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698