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