0

[4/N] Remove IsOnBatteryPower and replace it with GetBatteryPowerStatus

This CL is part of a series of a CLs to expose the tristate enum
to all consumers, for more context please see https://docs.google.com/document/d/1znskYZJyfhbGmyu7OQC4-A58zdrl9_xyZT7E-jTbyXE/.

This CL removes IsOnBatteryPower from PowerMonitorSource and replaces
it with GetBatteryPowerStatus. It changes all the necassary files to
make that complete.

Bug: 339859756
Change-Id: I76a4b322333f4caacba3069c09ff10b01f025294
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5823390
Reviewed-by: Francois Pierre Doray <fdoray@chromium.org>
Reviewed-by: Colin Blundell <blundell@chromium.org>
Reviewed-by: Alex Gough <ajgo@chromium.org>
Commit-Queue: Sayed Elabady <elabadysayed@chromium.org>
Reviewed-by: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1352655}
This commit is contained in:
elabadysayed
2024-09-09 11:15:34 +00:00
committed by Chromium LUCI CQ
parent 1f8ea943e0
commit 46d24d5c24
33 changed files with 268 additions and 123 deletions

@ -8,11 +8,6 @@
namespace base {
bool PowerMonitorDeviceSource::IsOnBatteryPower() const {
return GetBatteryPowerStatus() ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
PowerMonitorDeviceSource::PowerMonitorDeviceSource() {
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
PlatformInit();

@ -107,17 +107,12 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
void* message_argument);
#endif // BUILDFLAG(IS_MAC)
// Platform-specific method to check whether the system is currently
// running on battery power. Returns true if running on batteries,
// false otherwise.
bool IsOnBatteryPower() const final;
// Platform-specific method to check whether the system is currently
// running on battery power. Returns kBatteryPower if running on battery,
// kExternalPower if running on external power or kUnknown if the power
// state is unknown (for example, during early process lifetime when the
// state hasn't been obtained yet).
PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const;
PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const override;
#if BUILDFLAG(IS_ANDROID)
PowerThermalObserver::DeviceThermalState GetCurrentThermalState()

@ -45,19 +45,9 @@ class BASE_EXPORT PowerMonitorSource {
virtual void SetCurrentThermalState(
PowerThermalObserver::DeviceThermalState state);
// Platform-specific method to check whether the system is currently
// running on battery power.
// TODO(339859756): remove this method and make `GetBatteryPowerStatus`
// virtual.
virtual bool IsOnBatteryPower() const = 0;
// TOOD(339859756): using `kUnknown` here is temporary and should be cleaned
// up once we remove `IsOnBatteryPower`.
PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const {
return IsOnBatteryPower()
? PowerStateObserver::BatteryPowerStatus::kBatteryPower
: PowerStateObserver::BatteryPowerStatus::kUnknown;
}
// Platform-specific method to determine the battery power status.
virtual PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const = 0;
#if BUILDFLAG(IS_ANDROID)
// Read and return the current remaining battery capacity (microampere-hours).

@ -84,10 +84,10 @@ TEST_F(PowerMonitorTest, PowerNotifications) {
// Pretend the device has gone off battery power
source().GeneratePowerStateEvent(
PowerStateObserver::BatteryPowerStatus::kUnknown);
PowerStateObserver::BatteryPowerStatus::kExternalPower);
EXPECT_EQ(observers[0].power_state_changes(), 2);
EXPECT_EQ(observers[0].last_power_status(),
PowerStateObserver::BatteryPowerStatus::kUnknown);
PowerStateObserver::BatteryPowerStatus::kExternalPower);
// Repeated indications the device is off battery power should be suppressed.
source().GeneratePowerStateEvent(

@ -21,8 +21,7 @@ class PowerMonitorTestSource : public PowerMonitorSource {
// Retrieve current states.
PowerThermalObserver::DeviceThermalState GetCurrentThermalState()
const override;
PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const;
bool IsOnBatteryPower() const override;
PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const override;
// Sends asynchronous notifications to registered observers.
void Suspend();
@ -88,11 +87,6 @@ PowerMonitorTestSource::GetBatteryPowerStatus() const {
return test_power_status_;
}
bool PowerMonitorTestSource::IsOnBatteryPower() const {
return test_power_status_ ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
void PowerMonitorTestSource::GenerateThermalThrottlingEvent(
PowerThermalObserver::DeviceThermalState new_thermal_state) {
ProcessThermalEvent(new_thermal_state);

@ -16,9 +16,5 @@ void FakePowerMonitorSource::SetBatteryPowerStatus(
battery_power_status_ = battery_power_status;
ProcessPowerEvent(POWER_STATE_EVENT);
}
bool FakePowerMonitorSource::IsOnBatteryPower() const {
return GetBatteryPowerStatus() ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
} // namespace performance_manager

@ -13,10 +13,10 @@ namespace performance_manager {
class FakePowerMonitorSource : public base::PowerMonitorSource {
public:
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const;
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override;
void SetBatteryPowerStatus(
base::PowerStateObserver::BatteryPowerStatus battery_power_status);
bool IsOnBatteryPower() const override;
private:
base::PowerStateObserver::BatteryPowerStatus battery_power_status_ =

@ -5,6 +5,8 @@
#ifndef COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_SCHEDULER_BATTERY_STATUS_LISTENER_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_SCHEDULER_BATTERY_STATUS_LISTENER_H_
#include "base/power_monitor/power_observer.h"
namespace download {
// Interface to monitor device battery status.
@ -13,7 +15,8 @@ class BatteryStatusListener {
class Observer {
public:
// Called when device charging state changed.
virtual void OnPowerStateChange(bool on_battery_power) = 0;
virtual void OnPowerStateChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) = 0;
protected:
virtual ~Observer() {}
@ -24,7 +27,8 @@ class BatteryStatusListener {
virtual int GetBatteryPercentage() = 0;
// Is the device is using battery power instead of charging.
virtual bool IsOnBatteryPower() const = 0;
virtual base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const = 0;
// Start/Stop to listen to battery status changes.
virtual void Start(Observer* observer) = 0;

@ -22,9 +22,9 @@ int BatteryStatusListenerImpl::GetBatteryPercentage() {
return battery_percentage_;
}
bool BatteryStatusListenerImpl::IsOnBatteryPower() const {
return (base::PowerMonitor::GetInstance()->GetBatteryPowerStatus() ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
base::PowerStateObserver::BatteryPowerStatus
BatteryStatusListenerImpl::GetBatteryPowerStatus() const {
return base::PowerMonitor::GetInstance()->GetBatteryPowerStatus();
}
void BatteryStatusListenerImpl::Start(Observer* observer) {
@ -60,9 +60,7 @@ void BatteryStatusListenerImpl::UpdateBatteryPercentage(bool force) {
void BatteryStatusListenerImpl::OnBatteryPowerStatusChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
if (observer_)
observer_->OnPowerStateChange(
battery_power_status ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
observer_->OnPowerStateChange(battery_power_status);
}
} // namespace download

@ -34,7 +34,8 @@ class BatteryStatusListenerImpl : public BatteryStatusListener,
private:
// BatteryStatusListener implementation.
int GetBatteryPercentage() override;
bool IsOnBatteryPower() const override;
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override;
void Start(Observer* observer) override;
void Stop() override;

@ -14,8 +14,9 @@ int BatteryStatusListenerMac::GetBatteryPercentage() {
return 100;
}
bool BatteryStatusListenerMac::IsOnBatteryPower() const {
return false;
base::PowerStateObserver::BatteryPowerStatus
BatteryStatusListenerMac::GetBatteryPowerStatus() const {
return base::PowerStateObserver::BatteryPowerStatus::kUnknown;
}
void BatteryStatusListenerMac::Start(Observer* observer) {}

@ -25,7 +25,8 @@ class BatteryStatusListenerMac : public BatteryStatusListener {
private:
// BatteryStatusListener implementation.
int GetBatteryPercentage() override;
bool IsOnBatteryPower() const override;
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override;
void Start(Observer* observer) override;
void Stop() override;
};

@ -13,6 +13,7 @@ namespace download {
enum class BatteryStatus {
CHARGING = 0,
NOT_CHARGING = 1,
UNKNOWN = 2,
};
// NetworkStatus should mostly one to one map to

@ -10,10 +10,18 @@ namespace download {
namespace {
// Converts |on_battery_power| to battery status.
BatteryStatus ToBatteryStatus(bool on_battery_power) {
return on_battery_power ? BatteryStatus::NOT_CHARGING
: BatteryStatus::CHARGING;
// Converts |battery_power_status| to battery status.
BatteryStatus ToBatteryStatus(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
switch (battery_power_status) {
case base::PowerStateObserver::BatteryPowerStatus::kBatteryPower:
return BatteryStatus::NOT_CHARGING;
case base::PowerStateObserver::BatteryPowerStatus::kExternalPower:
// TODO(339859756): We return CHARGING for kUnknown to preserve the old
// behavior.
case base::PowerStateObserver::BatteryPowerStatus::kUnknown:
return BatteryStatus::CHARGING;
}
}
// Converts a ConnectionType to NetworkStatus.
@ -92,13 +100,13 @@ void DeviceStatusListener::StartAfterDelay() {
DCHECK(battery_listener_);
battery_listener_->Start(this);
status_.battery_status =
ToBatteryStatus(battery_listener_->IsOnBatteryPower());
ToBatteryStatus(battery_listener_->GetBatteryPowerStatus());
// Listen to network status changes.
network_listener_->Start(this);
status_.battery_status =
ToBatteryStatus(battery_listener_->IsOnBatteryPower());
ToBatteryStatus(battery_listener_->GetBatteryPowerStatus());
status_.network_status =
ToNetworkStatus(network_listener_->GetConnectionType());
pending_network_status_ = status_.network_status;
@ -159,8 +167,9 @@ void DeviceStatusListener::OnNetworkChanged(
}
}
void DeviceStatusListener::OnPowerStateChange(bool on_battery_power) {
status_.battery_status = ToBatteryStatus(on_battery_power);
void DeviceStatusListener::OnPowerStateChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
status_.battery_status = ToBatteryStatus(battery_power_status);
NotifyStatusChange();
}

@ -75,7 +75,8 @@ class DeviceStatusListener : public NetworkStatusListener::Observer,
void StartAfterDelay();
// BatteryStatusListener::Observer implementation.
void OnPowerStateChange(bool on_battery_power) override;
void OnPowerStateChange(base::PowerStateObserver::BatteryPowerStatus
battery_power_status) override;
// Notifies the observer about device status change.
void NotifyStatusChange();

@ -47,11 +47,12 @@ PowerMonitorDeviceSourceLinux::~PowerMonitorDeviceSourceLinux() {
ShutdownBus();
}
bool PowerMonitorDeviceSourceLinux::IsOnBatteryPower() const {
base::PowerStateObserver::BatteryPowerStatus
PowerMonitorDeviceSourceLinux::GetBatteryPowerStatus() const {
// TODO(crbug.com/40836663): Use org.freedesktop.UPower to check for
// OnBattery. One possibility is to connect to the DeviceService's
// BatteryMonitor.
return false;
return base::PowerStateObserver::BatteryPowerStatus::kUnknown;
}
void PowerMonitorDeviceSourceLinux::ShutdownBus() {

@ -27,7 +27,8 @@ class PowerMonitorDeviceSourceLinux : public base::PowerMonitorSource {
~PowerMonitorDeviceSourceLinux() override;
// base::PowerMonitorSource:
bool IsOnBatteryPower() const override;
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override;
private:
void ShutdownBus();

@ -74,7 +74,10 @@ class MockPowerMonitorSource : public base::PowerMonitorSource {
~MockPowerMonitorSource() override { *leak_guard_ = false; }
bool IsOnBatteryPower() const override { return false; }
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override {
return base::PowerStateObserver::BatteryPowerStatus::kUnknown;
}
private:
// An external flag to signal as to whether or not this object is still

@ -6,6 +6,7 @@
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/power_monitor/power_observer.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
@ -37,12 +38,15 @@ namespace content {
namespace {
void VerifyPowerStateInChildProcess(mojom::PowerMonitorTest* power_monitor_test,
bool expected_state) {
void VerifyPowerStateInChildProcess(
mojom::PowerMonitorTest* power_monitor_test,
base::PowerStateObserver::BatteryPowerStatus expected_state) {
base::RunLoop run_loop;
power_monitor_test->QueryNextState(base::BindOnce(
[](base::RunLoop* loop, bool expected_state, bool on_battery_power) {
EXPECT_EQ(expected_state, on_battery_power);
[](base::RunLoop* loop,
base::PowerStateObserver::BatteryPowerStatus expected_state,
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
EXPECT_EQ(expected_state, battery_power_status);
loop->Quit();
},
&run_loop, expected_state));
@ -69,18 +73,20 @@ class MockPowerMonitorMessageBroadcaster : public device::mojom::PowerMonitor {
pending_power_monitor_client) override {
mojo::Remote<device::mojom::PowerMonitorClient> power_monitor_client(
std::move(pending_power_monitor_client));
power_monitor_client->PowerStateChange(on_battery_power_);
power_monitor_client->PowerStateChange(battery_power_status_);
clients_.Add(std::move(power_monitor_client));
}
void OnPowerStateChange(bool on_battery_power) {
on_battery_power_ = on_battery_power;
void OnPowerStateChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
battery_power_status_ = battery_power_status;
for (auto& client : clients_)
client->PowerStateChange(on_battery_power);
client->PowerStateChange(battery_power_status);
}
private:
bool on_battery_power_ = false;
base::PowerStateObserver::BatteryPowerStatus battery_power_status_ =
base::PowerStateObserver::BatteryPowerStatus::kUnknown;
mojo::ReceiverSet<device::mojom::PowerMonitor> receivers_;
mojo::RemoteSet<device::mojom::PowerMonitorClient> clients_;
@ -160,8 +166,9 @@ class PowerMonitorTest : public ContentBrowserTest {
int request_count_from_utility() { return request_count_from_utility_; }
int request_count_from_gpu() { return request_count_from_gpu_; }
void SimulatePowerStateChange(bool on_battery_power) {
power_monitor_message_broadcaster_.OnPowerStateChange(on_battery_power);
void SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
power_monitor_message_broadcaster_.OnPowerStateChange(battery_power_status);
}
private:
@ -233,13 +240,26 @@ IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestRendererProcess) {
// power state change.
power_monitor_renderer.FlushForTesting();
SimulatePowerStateChange(true);
// Verify renderer process on_battery_power changed to true.
VerifyPowerStateInChildProcess(power_monitor_renderer.get(), true);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
// Verify renderer process battery_power_status changed to battery power.
VerifyPowerStateInChildProcess(
power_monitor_renderer.get(),
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
SimulatePowerStateChange(false);
// Verify renderer process on_battery_power changed to false.
VerifyPowerStateInChildProcess(power_monitor_renderer.get(), false);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
// Verify renderer process battery_power_status changed to external power.
VerifyPowerStateInChildProcess(
power_monitor_renderer.get(),
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kUnknown);
// Verify renderer process battery_power_status becomes unknown.
VerifyPowerStateInChildProcess(
power_monitor_renderer.get(),
base::PowerStateObserver::BatteryPowerStatus::kUnknown);
}
IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestUtilityProcess) {
@ -256,13 +276,19 @@ IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestUtilityProcess) {
// power state change.
power_monitor_utility.FlushForTesting();
SimulatePowerStateChange(true);
// Verify utility process on_battery_power changed to true.
VerifyPowerStateInChildProcess(power_monitor_utility.get(), true);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
// Verify renderer process battery_power_status changed to battery power.
VerifyPowerStateInChildProcess(
power_monitor_utility.get(),
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
SimulatePowerStateChange(false);
// Verify utility process on_battery_power changed to false.
VerifyPowerStateInChildProcess(power_monitor_utility.get(), false);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
// Verify renderer process battery_power_status changed to external power.
VerifyPowerStateInChildProcess(
power_monitor_utility.get(),
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
}
IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestGpuProcess) {
@ -287,13 +313,19 @@ IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestGpuProcess) {
// power state change.
power_monitor_gpu.FlushForTesting();
SimulatePowerStateChange(true);
// Verify gpu process on_battery_power changed to true.
VerifyPowerStateInChildProcess(power_monitor_gpu.get(), true);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
// Verify gpu process battery_power_status changed to battery power.
VerifyPowerStateInChildProcess(
power_monitor_gpu.get(),
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
SimulatePowerStateChange(false);
// Verify gpu process on_battery_power changed to false.
VerifyPowerStateInChildProcess(power_monitor_gpu.get(), false);
SimulatePowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
// Verify gpu process battery_power_status changed to external power.
VerifyPowerStateInChildProcess(
power_monitor_gpu.get(),
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
}
} // namespace

@ -1047,6 +1047,7 @@ mojom("content_browsertests_mojom") {
"common/render_frame_test_helper.mojom",
]
public_deps = [
"//mojo/public/mojom/base",
"//sandbox/policy/mojom",
"//third_party/blink/public/mojom/tokens",
]

@ -3,6 +3,8 @@
// found in the LICENSE file.
module content.mojom;
import "mojo/public/mojom/base/battery_power_status.mojom";
import "sandbox/policy/mojom/sandbox.mojom";
// This interface is only for the sake of browser test to query current power
@ -12,5 +14,6 @@ interface PowerMonitorTest {
// The power state is reported immediatelly if there is already a change
// available to notify, otherwise wait until a change happen. Overlapping
// calls to QueryNextState are prohibited.
QueryNextState() => (bool on_battery_power);
QueryNextState()
=> (mojo_base.mojom.BatteryPowerStatus battery_power_status);
};

@ -46,9 +46,7 @@ void PowerMonitorTestImpl::OnBatteryPowerStatusChange(
}
void PowerMonitorTestImpl::ReportState() {
std::move(callback_).Run(
battery_power_status_ ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
std::move(callback_).Run(battery_power_status_);
need_to_report_ = false;
}

@ -56,6 +56,8 @@ component("typemap_traits") {
output_name = "mojo_base_typemap_traits"
sources = [
"battery_power_status_traits.cc",
"battery_power_status_traits.h",
"big_string_mojom_traits.cc",
"big_string_mojom_traits.h",
"memory_allocator_dump_cross_process_uid_mojom_traits.cc",

@ -0,0 +1,47 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/public/cpp/base/battery_power_status_traits.h"
#include "base/notreached.h"
#include "base/power_monitor/power_observer.h"
namespace mojo {
// static
mojo_base::mojom::BatteryPowerStatus
EnumTraits<mojo_base::mojom::BatteryPowerStatus,
base::PowerStateObserver::BatteryPowerStatus>::
ToMojom(base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
switch (battery_power_status) {
case base::PowerStateObserver::BatteryPowerStatus::kBatteryPower:
return mojo_base::mojom::BatteryPowerStatus::kBatteryPower;
case base::PowerStateObserver::BatteryPowerStatus::kExternalPower:
return mojo_base::mojom::BatteryPowerStatus::kExternalPower;
case base::PowerStateObserver::BatteryPowerStatus::kUnknown:
return mojo_base::mojom::BatteryPowerStatus::kUnknown;
}
NOTREACHED();
}
// static
bool EnumTraits<mojo_base::mojom::BatteryPowerStatus,
base::PowerStateObserver::BatteryPowerStatus>::
FromMojom(mojo_base::mojom::BatteryPowerStatus input,
base::PowerStateObserver::BatteryPowerStatus* out) {
switch (input) {
case mojo_base::mojom::BatteryPowerStatus::kBatteryPower:
*out = base::PowerStateObserver::BatteryPowerStatus::kBatteryPower;
return true;
case mojo_base::mojom::BatteryPowerStatus::kExternalPower:
*out = base::PowerStateObserver::BatteryPowerStatus::kExternalPower;
return true;
case mojo_base::mojom::BatteryPowerStatus::kUnknown:
*out = base::PowerStateObserver::BatteryPowerStatus::kUnknown;
return true;
}
return false;
}
} // namespace mojo

@ -0,0 +1,26 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_PUBLIC_CPP_BASE_BATTERY_POWER_STATUS_TRAITS_H_
#define MOJO_PUBLIC_CPP_BASE_BATTERY_POWER_STATUS_TRAITS_H_
#include "base/component_export.h"
#include "base/power_monitor/power_observer.h"
#include "mojo/public/mojom/base/battery_power_status.mojom-shared.h"
namespace mojo {
template <>
struct COMPONENT_EXPORT(MOJO_BASE_TRAITS)
EnumTraits<mojo_base::mojom::BatteryPowerStatus,
base::PowerStateObserver::BatteryPowerStatus> {
static mojo_base::mojom::BatteryPowerStatus ToMojom(
base::PowerStateObserver::BatteryPowerStatus battery_power_status);
static bool FromMojom(mojo_base::mojom::BatteryPowerStatus input,
base::PowerStateObserver::BatteryPowerStatus* out);
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BASE_BATTERY_POWER_STATUS_TRAITS_H_

@ -12,6 +12,7 @@ mojom_component("base") {
sources = [
"absl_status.mojom",
"application_state.mojom",
"battery_power_status.mojom",
"big_buffer.mojom",
"big_string.mojom",
"binder.mojom",
@ -548,6 +549,20 @@ mojom_component("base") {
"//mojo/public/cpp/base:typemap_traits",
]
},
{
types = [
{
mojom = "mojo_base.mojom.BatteryPowerStatus"
cpp = "::base::PowerStateObserver::BatteryPowerStatus"
},
]
traits_headers =
[ "//mojo/public/cpp/base/battery_power_status_traits.h" ]
traits_public_deps = [
"//base",
"//mojo/public/cpp/base:typemap_traits",
]
},
]
if (is_android) {

@ -0,0 +1,14 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file
module mojo_base.mojom;
// Represents the possible states of the battery: running on battery power,
// charging from an external power source, or an unknown state.
// This should be in sync with PowerStateObserver::BatteryPowerStatus enum.
enum BatteryPowerStatus {
kUnknown = 0,
kBatteryPower = 1,
kExternalPower = 2,
};

@ -50,10 +50,7 @@ void PowerMonitorMessageBroadcaster::AddClient(
// new client if battery power status isn't unknown;
if (battery_power_status_ !=
base::PowerStateObserver::BatteryPowerStatus::kUnknown) {
clients_.Get(element_id)
->PowerStateChange(
battery_power_status ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
clients_.Get(element_id)->PowerStateChange(battery_power_status_);
}
}
@ -61,9 +58,7 @@ void PowerMonitorMessageBroadcaster::OnBatteryPowerStatusChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
battery_power_status_ = battery_power_status;
for (auto& client : clients_)
client->PowerStateChange(
battery_power_status_ ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
client->PowerStateChange(battery_power_status_);
}
void PowerMonitorMessageBroadcaster::OnSuspend() {

@ -23,7 +23,8 @@ class MockClient : public PowerMonitorBroadcastSource::Client {
~MockClient() override = default;
// Implement device::mojom::PowerMonitorClient
void PowerStateChange(bool on_battery_power) override {
void PowerStateChange(base::PowerStateObserver::BatteryPowerStatus
battery_power_status) override {
power_state_changes_++;
if (service_connected_)
std::move(service_connected_).Run();

@ -7,8 +7,8 @@
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/sequenced_task_runner.h"
#include "mojo/public/cpp/base/battery_power_status_traits.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace device {
PowerMonitorBroadcastSource::PowerMonitorBroadcastSource(
@ -35,8 +35,9 @@ void PowerMonitorBroadcastSource::Init(
}
}
bool PowerMonitorBroadcastSource::IsOnBatteryPower() const {
return client_->last_reported_on_battery_power_state();
base::PowerStateObserver::BatteryPowerStatus
PowerMonitorBroadcastSource::GetBatteryPowerStatus() const {
return client_->last_reported_battery_power_status();
}
PowerMonitorBroadcastSource::Client::Client() = default;
@ -50,8 +51,8 @@ void PowerMonitorBroadcastSource::Client::Init(
}
void PowerMonitorBroadcastSource::Client::PowerStateChange(
bool on_battery_power) {
last_reported_on_battery_power_state_ = on_battery_power;
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
last_reported_battery_power_status_ = battery_power_status;
ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
}

@ -63,20 +63,23 @@ class PowerMonitorBroadcastSource : public base::PowerMonitorSource {
void Init(mojo::PendingRemote<mojom::PowerMonitor> remote_monitor);
bool last_reported_on_battery_power_state() const {
return last_reported_on_battery_power_state_;
base::PowerStateObserver::BatteryPowerStatus
last_reported_battery_power_status() const {
return last_reported_battery_power_status_;
}
// device::mojom::PowerMonitorClient implementation
// TODO(b/339859756): Change this to broadcast enum instead of boolean.
void PowerStateChange(bool on_battery_power) override;
void PowerStateChange(base::PowerStateObserver::BatteryPowerStatus
battery_power_status) override;
void Suspend() override;
void Resume() override;
private:
mojo::Receiver<device::mojom::PowerMonitorClient> receiver_{this};
bool last_reported_on_battery_power_state_ = false;
base::PowerStateObserver::BatteryPowerStatus
last_reported_battery_power_status_ =
base::PowerStateObserver::BatteryPowerStatus::kUnknown;
};
// This constructor is used by test code to mock the Client class.
@ -86,7 +89,8 @@ class PowerMonitorBroadcastSource : public base::PowerMonitorSource {
Client* client_for_testing() const { return client_.get(); }
bool IsOnBatteryPower() const override;
base::PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
const override;
std::unique_ptr<Client> client_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;

@ -82,29 +82,41 @@ TEST_F(PowerMonitorBroadcastSourceTest, PowerMessageReceiveBroadcast) {
EXPECT_EQ(observer.resumes(), 1);
// Pretend the device has gone on battery power
client()->PowerStateChange(true);
client()->PowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer.power_state_changes(), 1);
EXPECT_EQ(observer.last_power_status(),
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
// Repeated indications the device is on battery power should be suppressed.
client()->PowerStateChange(true);
client()->PowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer.power_state_changes(), 1);
// Pretend the device has gone off battery power
client()->PowerStateChange(false);
client()->PowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer.power_state_changes(), 2);
EXPECT_EQ(observer.last_power_status(),
base::PowerStateObserver::BatteryPowerStatus::kUnknown);
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
// Repeated indications the device is off battery power should be suppressed.
client()->PowerStateChange(false);
client()->PowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kExternalPower);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer.power_state_changes(), 2);
// Sending unknown signal should be propagated properly.
client()->PowerStateChange(
base::PowerStateObserver::BatteryPowerStatus::kUnknown);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer.power_state_changes(), 3);
EXPECT_EQ(observer.last_power_status(),
base::PowerStateObserver::BatteryPowerStatus::kUnknown);
power_monitor->RemovePowerSuspendObserver(&observer);
power_monitor->RemovePowerStateObserver(&observer);
}

@ -4,15 +4,18 @@
module device.mojom;
import "mojo/public/mojom/base/battery_power_status.mojom";
interface PowerMonitor {
// Add a client that will be notified on PowerStateChange, Suspend and Resume.
AddClient(pending_remote<PowerMonitorClient> client);
};
// Client that broadcasts the battery power status to the current process.
interface PowerMonitorClient {
// Notification of a change in power status of the computer, such
// as from switching between battery and A/C power.
PowerStateChange(bool on_battery_power);
// as from switching between battery and A/C power or becoming unknown.
PowerStateChange(mojo_base.mojom.BatteryPowerStatus battery_power_status);
// Notification that the system is suspending.
Suspend();