chromium/chrome/browser/metrics/power/battery_discharge_reporter.cc

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/power/battery_discharge_reporter.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/metrics/power/power_metrics.h"
#include "chrome/browser/metrics/power/process_metrics_recorder_util.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario.h"

#if BUILDFLAG(IS_MAC)
#include "base/metrics/histogram_functions.h"
#endif  // BUILDFLAG(IS_MAC)

namespace {

bool IsWithinTolerance(base::TimeDelta value,
                       base::TimeDelta expected,
                       base::TimeDelta tolerance) {}

}  // namespace

BatteryDischargeReporter::BatteryDischargeReporter(
    base::BatteryStateSampler* battery_state_sampler,
    UsageScenarioDataStore* battery_usage_scenario_data_store)
    :{}

BatteryDischargeReporter::~BatteryDischargeReporter() = default;

void BatteryDischargeReporter::OnBatteryStateSampled(
    const std::optional<base::BatteryLevelProvider::BatteryState>&
        battery_state) {}

void BatteryDischargeReporter::ReportOneMinuteInterval(
    base::TimeDelta interval_duration,
    const std::optional<base::BatteryLevelProvider::BatteryState>&
        battery_state) {}

#if BUILDFLAG(IS_WIN)
void BatteryDischargeReporter::ReportTenMinutesInterval(
    base::TimeDelta interval_duration,
    const std::optional<base::BatteryLevelProvider::BatteryState>&
        battery_state) {
  auto battery_discharge = GetBatteryDischargeDuringInterval(
      ten_minutes_interval_start_battery_state_, battery_state,
      interval_duration);

  // Intervals are expected to be approximately 10 minutes long. Exclude samples
  // when the interval length deviates significantly from that value, as that
  // could indicate that the system went to sleep. The tolerance is the same as
  // the one used for 1 minute intervals.
  if (battery_discharge.mode == BatteryDischargeMode::kDischarging &&
      !IsWithinTolerance(interval_duration, base::Minutes(10),
                         base::Seconds(1))) {
    battery_discharge.mode = BatteryDischargeMode::kInvalidInterval;
  }

  ReportBatteryHistogramsTenMinutesInterval(interval_duration,
                                            battery_discharge);
}
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_MAC)
void BatteryDischargeReporter::RecordIOPMPowerSourceSampleEventDelta(
    base::TimeDelta sampling_event_delta) {
  // The delta is expected to be almost always 60 seconds. Split the buckets for
  // 0.2s granularity (10s interval with 50 buckets + 1 underflow bucket + 1
  // overflow bucket) around that value.
  base::HistogramBase* histogram = base::LinearHistogram::FactoryTimeGet(
      "Power.IOPMPowerSource.SamplingEventDelta",
      /*min=*/base::Seconds(55), /*max=*/base::Seconds(65), /*buckets=*/52,
      base::HistogramBase::kUmaTargetedHistogramFlag);
  histogram->AddTime(sampling_event_delta);

  // Same as the above but using a range that starts from zero and significantly
  // goes beyond the expected mean time of |sampling_event_delta| (which is 60
  // seconds.).
  base::UmaHistogramMediumTimes(
      "Power.IOPMPowerSource.SamplingEventDelta.MediumTimes",
      sampling_event_delta);
}
#endif  // BUILDFLAG(IS_MAC)