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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/download/internal/scheduler/battery_listener.cc
diff --git a/components/download/internal/scheduler/battery_listener.cc b/components/download/internal/scheduler/battery_listener.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b015ca15ffd608e5087edd41a088cbbbaad7fe50
--- /dev/null
+++ b/components/download/internal/scheduler/battery_listener.cc
@@ -0,0 +1,58 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/download/internal/scheduler/battery_listener.h"
+
+#include "base/power_monitor/power_monitor.h"
+
+namespace download {
+
+// Helper function that converts the battery status to battery requirement.
+SchedulingParams::BatteryRequirements ToBatteryRequirement(
+ bool on_battery_power) {
+ return on_battery_power
+ ? SchedulingParams::BatteryRequirements::BATTERY_INSENSITIVE
+ : SchedulingParams::BatteryRequirements::BATTERY_SENSITIVE;
+}
+
+BatteryListener::BatteryListener() = default;
+
+BatteryListener::~BatteryListener() {
+ Stop();
+}
+
+SchedulingParams::BatteryRequirements BatteryListener::CurrentBatteryStatus()
+ const {
+ return ToBatteryRequirement(base::PowerMonitor::Get()->IsOnBatteryPower());
+}
+
+void BatteryListener::Start() {
+ base::PowerMonitor* monitor = base::PowerMonitor::Get();
+ DCHECK(monitor);
+ monitor->AddObserver(this);
+}
+
+void BatteryListener::Stop() {
+ base::PowerMonitor::Get()->RemoveObserver(this);
+}
+
+void BatteryListener::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void BatteryListener::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void BatteryListener::OnPowerStateChange(bool on_battery_power) {
+ NotifyBatteryChange(ToBatteryRequirement(on_battery_power));
+}
+
+void BatteryListener::NotifyBatteryChange(
+ SchedulingParams::BatteryRequirements current_battery) {
+ for (auto& observer : observers_)
+ observer.OnBatteryChange(current_battery);
+}
+
+} // namespace download

Powered by Google App Engine
This is Rietveld 408576698