Commit ff277570 authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

android: Move battery metrics impl from chrome/ to components/

This moves the majority of AndroidBatteryMetrics from chrome/ to
components/, so that we can reuse it for WebView/WebLayer in the future.

The only aspect that remains in chrome/ is the detection of the app's
visibility state, since this will have to be done some other way for
WebView.

Bug: 1102048
Change-Id: Ia8391fd5efd80411ac970c559cdf5db988ba9ea4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485794
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819432}
parent be4d2b1c
......@@ -2413,8 +2413,6 @@ static_library("browser") {
"android/background_task_scheduler/chrome_background_task_factory.h",
"android/background_task_scheduler/proxy_native_task.cc",
"android/background_task_scheduler/proxy_native_task.h",
"android/battery/android_battery_metrics.cc",
"android/battery/android_battery_metrics.h",
"android/bookmarks/bookmark_bridge.cc",
"android/bookmarks/bookmark_bridge.h",
"android/bookmarks/partner_bookmarks_reader.cc",
......@@ -3186,6 +3184,7 @@ static_library("browser") {
"//components/payments/content/android",
"//components/payments/content/android:jni_headers",
"//components/permissions/android:native",
"//components/power_metrics",
"//components/query_tiles",
"//components/resources:android_resources",
"//components/resources:components_resources",
......
......@@ -216,6 +216,7 @@ include_rules = [
"+components/permissions",
"+components/pdf/browser",
"+components/policy",
"+components/power_metrics",
"+components/pref_registry",
"+components/prefs",
"+components/prerender",
......
......@@ -22,10 +22,30 @@ BatteryMetrics::BatteryMonitorBinder& GetBinderOverride() {
return *binder;
}
#if defined(OS_ANDROID)
bool IsAppVisible(base::android::ApplicationState state) {
return state == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES;
}
#endif // defined(OS_ANDROID)
} // namespace
BatteryMetrics::BatteryMetrics() {
StartRecording();
#if defined(OS_ANDROID)
// On Android, also track the battery capacity drain while Chrome is the
// foreground activity.
app_state_listener_ =
base::android::ApplicationStatusListener::New(base::BindRepeating(
[](BatteryMetrics* metrics, base::android::ApplicationState state) {
metrics->android_metrics_.OnAppVisibilityChanged(
IsAppVisible(state));
},
base::Unretained(this)));
android_metrics_.OnAppVisibilityChanged(
IsAppVisible(base::android::ApplicationStatusListener::GetState()));
#endif // defined(OS_ANDROID)
}
BatteryMetrics::~BatteryMetrics() = default;
......
......@@ -17,7 +17,8 @@
#include "services/device/public/mojom/battery_status.mojom-forward.h"
#if defined(OS_ANDROID)
#include "chrome/browser/android/battery/android_battery_metrics.h"
#include "base/android/application_status_listener.h"
#include "components/power_metrics/android_battery_metrics.h"
#endif // defined(OS_ANDROID)
// Records metrics around battery usage on all platforms. Connects to
......@@ -55,7 +56,8 @@ class BatteryMetrics {
mojo::Remote<device::mojom::BatteryMonitor> battery_monitor_;
#if defined(OS_ANDROID)
AndroidBatteryMetrics android_metrics_;
power_metrics::AndroidBatteryMetrics android_metrics_;
std::unique_ptr<base::android::ApplicationStatusListener> app_state_listener_;
#endif // defined(OS_ANDROID)
SEQUENCE_CHECKER(sequence_checker_);
......
# Copyright 2020 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.
if (is_android) {
static_library("power_metrics") {
sources = [
"android_battery_metrics.cc",
"android_battery_metrics.h",
]
deps = [
"//base",
"//net",
]
}
}
include_rules = [
"+net/android",
]
eseckler@chromium.org
skyostil@chromium.org
khokhlov@google.com
# COMPONENT: Speed>Metrics
......@@ -2,17 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/battery/android_battery_metrics.h"
#include "components/power_metrics/android_battery_metrics.h"
#include "base/android/radio_utils.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_macros.h"
#include "base/optional.h"
#include "base/power_monitor/power_monitor.h"
#include "net/android/network_library.h"
#include "net/android/traffic_stats.h"
namespace power_metrics {
namespace {
void Report30SecondRadioUsage(int64_t tx_bytes, int64_t rx_bytes) {
......@@ -115,10 +117,7 @@ void ReportAveragedDrain(int capacity_consumed,
constexpr base::TimeDelta AndroidBatteryMetrics::kMetricsInterval;
AndroidBatteryMetrics::AndroidBatteryMetrics()
: app_state_listener_(base::android::ApplicationStatusListener::New(
base::BindRepeating(&AndroidBatteryMetrics::OnAppStateChanged,
base::Unretained(this)))),
app_state_(base::android::ApplicationStatusListener::GetState()),
: app_visible_(false),
on_battery_power_(base::PowerMonitor::IsOnBatteryPower()) {
base::PowerMonitor::AddObserver(this);
UpdateMetricsEnabled();
......@@ -128,10 +127,9 @@ AndroidBatteryMetrics::~AndroidBatteryMetrics() {
base::PowerMonitor::RemoveObserver(this);
}
void AndroidBatteryMetrics::OnAppStateChanged(
base::android::ApplicationState state) {
void AndroidBatteryMetrics::OnAppVisibilityChanged(bool visible) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
app_state_ = state;
app_visible_ = visible;
UpdateMetricsEnabled();
}
......@@ -144,12 +142,10 @@ void AndroidBatteryMetrics::OnPowerStateChange(bool on_battery_power) {
void AndroidBatteryMetrics::UpdateMetricsEnabled() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// We want to attribute battery drain to Chrome while it is in the foreground.
// Battery drain will only be reflected in remaining battery capacity when the
// device is not on a charger.
bool should_be_enabled =
app_state_ == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES &&
on_battery_power_;
// We want to attribute battery drain to chromium while the embedding app is
// visible. Battery drain will only be reflected in remaining battery capacity
// when the device is not on a charger.
bool should_be_enabled = app_visible_ && on_battery_power_;
if (should_be_enabled && !metrics_timer_.IsRunning()) {
// Capture first capacity measurement and enable the repeating timer.
......@@ -237,3 +233,5 @@ void AndroidBatteryMetrics::CaptureAndReportMetrics() {
bool AndroidBatteryMetrics::IsMeasuringDrainExclusively() const {
return observed_capacity_drops_ >= 2;
}
} // namespace power_metrics
......@@ -2,44 +2,47 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_
#define CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_
#ifndef COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
#define COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
#include "base/android/application_status_listener.h"
#include "base/macros.h"
#include "base/power_monitor/power_observer.h"
#include "base/sequence_checker.h"
#include "base/timer/timer.h"
// Records metrics around battery usage on Android.
namespace power_metrics {
// Records metrics around battery usage on Android. The metrics are only tracked
// while the device is not charging and the app is visible (embedder should call
// OnAppVisibilityChanged()). This class is not thread-safe.
class AndroidBatteryMetrics : public base::PowerObserver {
public:
AndroidBatteryMetrics();
~AndroidBatteryMetrics() override;
// base::PowerObserver implementation:
void OnPowerStateChange(bool on_battery_power) override;
// Should be called by the embedder when the embedder app becomes visible or
// invisible.
void OnAppVisibilityChanged(bool visible);
private:
// Called by base::android::ApplicationStatusListener.
void OnAppStateChanged(base::android::ApplicationState);
// base::PowerObserver implementation:
void OnPowerStateChange(bool on_battery_power) override;
void UpdateMetricsEnabled();
void CaptureAndReportMetrics();
void UpdateAndReportRadio();
// Whether or not we've seen at least two consecutive capacity drops while
// Chrome was the foreground app. Battery drain reported prior to this could
// the embedding app was visible. Battery drain reported prior to this could
// be caused by a different app.
bool IsMeasuringDrainExclusively() const;
// Battery drain is captured and reported periodically in this interval while
// the device is on battery power and Chrome is the foreground activity.
// the device is on battery power and the app is visible.
static constexpr base::TimeDelta kMetricsInterval =
base::TimeDelta::FromSeconds(30);
std::unique_ptr<base::android::ApplicationStatusListener> app_state_listener_;
base::android::ApplicationState app_state_;
bool app_visible_;
bool on_battery_power_;
int last_remaining_capacity_uah_ = 0;
int64_t last_tx_bytes_ = -1;
......@@ -47,8 +50,7 @@ class AndroidBatteryMetrics : public base::PowerObserver {
base::RepeatingTimer metrics_timer_;
int skipped_timers_ = 0;
// Number of consecutive charge drops seen while the app has been in the
// foreground.
// Number of consecutive charge drops seen while the app has been visible.
int observed_capacity_drops_ = 0;
SEQUENCE_CHECKER(sequence_checker_);
......@@ -56,4 +58,6 @@ class AndroidBatteryMetrics : public base::PowerObserver {
DISALLOW_COPY_AND_ASSIGN(AndroidBatteryMetrics);
};
#endif // CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_
} // namespace power_metrics
#endif // COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment