• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
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 "base/power_monitor/battery_level_provider.h"
6 
7 #include "base/power_monitor/power_monitor_buildflags.h"
8 #include "base/ranges/algorithm.h"
9 
10 namespace base {
11 
12 #if !BUILDFLAG(HAS_BATTERY_LEVEL_PROVIDER_IMPL)
Create()13 std::unique_ptr<BatteryLevelProvider> BatteryLevelProvider::Create() {
14 #if BUILDFLAG(IS_CHROMEOS_ASH)
15   // TODO(crbug.com/40871810): ChromeOS doesn't define
16   // `HAS_BATTERY_LEVEL_PROVIDER_IMPL` but still supplies its own
17   // `BatteryLevelProvider`
18   NOTREACHED();
19 #else
20   return nullptr;
21 #endif
22 }
23 #endif
24 
MakeBatteryState(const std::vector<BatteryDetails> & battery_details)25 BatteryLevelProvider::BatteryState BatteryLevelProvider::MakeBatteryState(
26     const std::vector<BatteryDetails>& battery_details) {
27   BatteryState state;
28 
29   state.battery_count = static_cast<int>(battery_details.size());
30   state.is_external_power_connected =
31       battery_details.size() == 0 ||
32       base::ranges::any_of(battery_details, [](const BatteryDetails& details) {
33         return details.is_external_power_connected;
34       });
35 
36   // Only populate the following fields if there is one battery detail.
37   if (battery_details.size() == 1) {
38     state.current_capacity = battery_details.front().current_capacity;
39     state.full_charged_capacity = battery_details.front().full_charged_capacity;
40     state.voltage_mv = battery_details.front().voltage_mv;
41     state.charge_unit = battery_details.front().charge_unit;
42 #if BUILDFLAG(IS_WIN)
43     state.battery_discharge_granularity =
44         battery_details.front().battery_discharge_granularity;
45 #endif  // BUILDFLAG(IS_WIN)
46   }
47   state.capture_time = base::TimeTicks::Now();
48 
49   return state;
50 }
51 
52 }  // namespace base
53