Skip to content

Commit

Permalink
[Merge M110] Fix battery saver mode upon startup on Mac
Browse files Browse the repository at this point in the history
With this change, the initial plugged-in state is queryied on startup so
that the power saver mode can be correctly initialized.

Test: Manually opened Chrome while unplugged and observed battery saver
mode enabled.

(cherry picked from commit e17e887)

Bug: 1404171
Change-Id: If09ef16157ae7708feefef01db6bf16a20984394
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4134033
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1089366}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4143762
Commit-Queue: Anthony Vallée-Dubois <anthonyvd@chromium.org>
Cr-Commit-Position: refs/branch-heads/5481@{#158}
Cr-Branched-From: 130f3e4-refs/heads/main@{#1084008}
  • Loading branch information
plmonette-zz authored and Chromium LUCI CQ committed Jan 6, 2023
1 parent 0d85eed commit 36c8840
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions base/power_monitor/power_monitor_device_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
PowerThermalObserver::DeviceThermalState GetCurrentThermalState() override;
int GetInitialSpeedLimit() override;

// Retrieves the current battery state to update `is_on_battery_`.
void GetBatteryState();
void OnBatteryStateReceived(
const absl::optional<BatteryLevelProvider::BatteryState>& battery_state);

// Reference to the system IOPMrootDomain port.
io_connect_t power_manager_port_ = IO_OBJECT_NULL;

Expand Down
39 changes: 22 additions & 17 deletions base/power_monitor/power_monitor_device_source_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
return thermal_state_observer_->GetCurrentSpeedLimit();
}

void PowerMonitorDeviceSource::GetBatteryState() {
DCHECK(battery_level_provider_);
// base::Unretained is safe because the callback is immediately invoked
// inside `BatteryLevelProvider::GetBatteryState()`.
battery_level_provider_->GetBatteryState(
base::BindOnce(&PowerMonitorDeviceSource::OnBatteryStateReceived,
base::Unretained(this)));
}

void PowerMonitorDeviceSource::OnBatteryStateReceived(
const absl::optional<BatteryLevelProvider::BatteryState>& battery_state) {
is_on_battery_ =
battery_state.has_value() && !battery_state->is_external_power_connected;
PowerMonitorSource::ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
}

void PowerMonitorDeviceSource::PlatformInit() {
power_manager_port_ = IORegisterForSystemPower(
this,
Expand All @@ -42,24 +58,13 @@
kCFRunLoopCommonModes);

battery_level_provider_ = BatteryLevelProvider::Create();

// Create and add the power-source-change event source to the runloop.
// Get the initial state for `is_on_battery_` and register for all future
// power-source-change events.
GetBatteryState();
// base::Unretained is safe because `this` owns `power_source_event_source_`,
// which exclusively owns the callback.
power_source_event_source_.Start(base::BindRepeating(
[](PowerMonitorDeviceSource* self,
BatteryLevelProvider* battery_level_provider) {
battery_level_provider->GetBatteryState(base::BindOnce(
[](PowerMonitorDeviceSource* self,
const absl::optional<BatteryLevelProvider::BatteryState>&
battery_state) {
self->is_on_battery_ =
battery_state.has_value() &&
!battery_state->is_external_power_connected;
PowerMonitorSource::ProcessPowerEvent(
PowerMonitorSource::POWER_STATE_EVENT);
},
Unretained(self)));
},
Unretained(this), battery_level_provider_.get()));
&PowerMonitorDeviceSource::GetBatteryState, base::Unretained(this)));

thermal_state_observer_ = std::make_unique<ThermalStateObserverMac>(
BindRepeating(&PowerMonitorSource::ProcessThermalEvent),
Expand Down

0 comments on commit 36c8840

Please sign in to comment.