Commit 0f6f4933 authored by Jiahe Zhang's avatar Jiahe Zhang Committed by Commit Bot

Implement PowerMonitorDeviceSource::GetCurrentThermalState on ChromeOS

On ChromeOS, PowerMonitorDeviceSource::GetCurrentThermalState will
always return PowerObserver::DeviceThermalState::kUnknown now, because
PowerManagerClientImpl will miss the first thermal event sent by the
Thermal Hinting API when constructed and there isn't a cache of the
last state.

In this CL, the GetCurrentThermalState is implemented for ChromeOS using
GetThermalState API added by
https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2495080

BUG=chromium:1081959
TEST=Manually test in chrome://device-log to see if there's
"GetThermalState" response after startup.

Change-Id: I9bbf5416b0ba76a015a08d2d4c91ea732704a541
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2498240
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830944}
parent fd355a99
......@@ -53,11 +53,19 @@ bool PowerMonitor::IsProcessSuspended() {
return g_is_process_suspended.load(std::memory_order_relaxed);
}
// static
PowerObserver::DeviceThermalState PowerMonitor::GetCurrentThermalState() {
DCHECK(IsInitialized());
return GetInstance()->source_->GetCurrentThermalState();
}
// static
void PowerMonitor::SetCurrentThermalState(
PowerObserver::DeviceThermalState state) {
DCHECK(IsInitialized());
GetInstance()->source_->SetCurrentThermalState(state);
}
#if defined(OS_ANDROID)
int PowerMonitor::GetRemainingBatteryCapacity() {
DCHECK(IsInitialized());
......
......@@ -61,6 +61,9 @@ class BASE_EXPORT PowerMonitor {
// May only be called if the PowerMonitor has been initialized.
static PowerObserver::DeviceThermalState GetCurrentThermalState();
// Update the result of thermal state.
static void SetCurrentThermalState(PowerObserver::DeviceThermalState state);
#if defined(OS_ANDROID)
// Read and return the current remaining battery capacity (microampere-hours).
// Only supported with a device power source (i.e. not in child processes in
......
......@@ -49,6 +49,11 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
static void HandleSystemSuspending();
static void HandleSystemResumed();
static void ThermalEventReceived(PowerObserver::DeviceThermalState state);
// These two methods is used for handling thermal state update requests, such
// as asking for initial state when starting lisitening to thermal change.
PowerObserver::DeviceThermalState GetCurrentThermalState() override;
void SetCurrentThermalState(PowerObserver::DeviceThermalState state) override;
#endif
private:
......@@ -125,6 +130,10 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
PowerMessageWindow power_message_window_;
#endif
#if defined(OS_CHROMEOS)
PowerObserver::DeviceThermalState current_thermal_state_ =
PowerObserver::DeviceThermalState::kUnknown;
#endif
DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource);
};
......
......@@ -40,7 +40,22 @@ bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
// static
void PowerMonitorDeviceSource::ThermalEventReceived(
PowerObserver::DeviceThermalState state) {
if (!PowerMonitor::IsInitialized()) {
PowerMonitor::Initialize(std::make_unique<PowerMonitorDeviceSource>());
}
PowerMonitor::SetCurrentThermalState(state);
ProcessThermalEvent(state);
}
PowerObserver::DeviceThermalState
PowerMonitorDeviceSource::GetCurrentThermalState() {
return current_thermal_state_;
}
void PowerMonitorDeviceSource::SetCurrentThermalState(
PowerObserver::DeviceThermalState state) {
current_thermal_state_ = state;
}
} // namespace base
......@@ -21,6 +21,9 @@ PowerObserver::DeviceThermalState PowerMonitorSource::GetCurrentThermalState() {
return PowerObserver::DeviceThermalState::kUnknown;
}
void PowerMonitorSource::SetCurrentThermalState(
PowerObserver::DeviceThermalState state) {}
#if defined(OS_ANDROID)
int PowerMonitorSource::GetRemainingBatteryCapacity() {
return 0;
......
......@@ -36,6 +36,9 @@ class BASE_EXPORT PowerMonitorSource {
// Otherwise, returns kUnknown.
virtual PowerObserver::DeviceThermalState GetCurrentThermalState();
// Update the result of thermal state.
virtual void SetCurrentThermalState(PowerObserver::DeviceThermalState state);
#if defined(OS_ANDROID)
// Read and return the current remaining battery capacity (microampere-hours).
virtual int GetRemainingBatteryCapacity();
......
......@@ -175,6 +175,8 @@ void FakePowerManagerClient::RequestStatusUpdate() {
weak_ptr_factory_.GetWeakPtr()));
}
void FakePowerManagerClient::RequestThermalState() {}
void FakePowerManagerClient::RequestSuspend() {}
void FakePowerManagerClient::RequestRestart(
......
......@@ -102,6 +102,7 @@ class COMPONENT_EXPORT(DBUS_POWER) FakePowerManagerClient
const base::Optional<power_manager::PowerSupplyProperties>& GetLastStatus()
override;
void RequestStatusUpdate() override;
void RequestThermalState() override;
void RequestSuspend() override;
void RequestRestart(power_manager::RequestRestartReason reason,
const std::string& description) override;
......
......@@ -18,8 +18,8 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/power_monitor/power_observer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
......@@ -238,6 +238,7 @@ class PowerManagerClientImpl : public PowerManagerClient {
RegisterSuspendDelays();
RequestStatusUpdate();
RequestThermalState();
CheckAmbientColorSupport();
}
......@@ -348,6 +349,16 @@ class PowerManagerClientImpl : public PowerManagerClient {
weak_ptr_factory_.GetWeakPtr()));
}
void RequestThermalState() override {
POWER_LOG(USER) << "RequestThermalState";
dbus::MethodCall method_call(power_manager::kPowerManagerInterface,
power_manager::kGetThermalStateMethod);
power_manager_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&PowerManagerClientImpl::OnGetCurrentThermalStateMethod,
weak_ptr_factory_.GetWeakPtr()));
}
void RequestSuspend() override {
POWER_LOG(USER) << "RequestSuspend";
SimpleMethodCallToPowerManager(power_manager::kRequestSuspendMethod);
......@@ -745,6 +756,29 @@ class PowerManagerClientImpl : public PowerManagerClient {
}
}
void OnGetCurrentThermalStateMethod(dbus::Response* response) {
if (!response) {
POWER_LOG(ERROR) << "Error calling "
<< power_manager::kGetThermalStateMethod;
return;
}
dbus::MessageReader reader(response);
power_manager::ThermalEvent protobuf;
if (!reader.PopArrayOfBytesAsProto(&protobuf)) {
POWER_LOG(ERROR) << "Unable to decode "
<< power_manager::kGetThermalStateMethod << " response";
return;
}
POWER_LOG(USER) << "Got " << power_manager::kGetThermalStateMethod
<< " response:"
<< " thermal_state=" << protobuf.thermal_state()
<< " timestamp=" << protobuf.timestamp();
base::PowerMonitorDeviceSource::ThermalEventReceived(
GetThermalStateFromProtoEnum(protobuf.thermal_state()));
}
void OnGetPowerSupplyPropertiesMethod(dbus::Response* response) {
// This is the last callback to run after all the initialization in |Init|.
// Notify all observers that the initialization is complete.
......
......@@ -17,6 +17,7 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/power_monitor/power_observer.h"
#include "base/time/time.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/power_manager/policy.pb.h"
......@@ -229,6 +230,9 @@ class COMPONENT_EXPORT(DBUS_POWER) PowerManagerClient {
// will be called asynchronously.
virtual void RequestStatusUpdate() = 0;
// Requests the current thermal state.
virtual void RequestThermalState() = 0;
// Requests suspend of the system.
virtual void RequestSuspend() = 0;
......
......@@ -270,6 +270,10 @@ class PowerManagerClientTest : public testing::Test {
_, _))
.WillRepeatedly(
Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay));
// Init should request the current thermal state
EXPECT_CALL(
*proxy_,
DoCallMethod(HasMember(power_manager::kGetThermalStateMethod), _, _));
// Init should also request a fresh power status.
EXPECT_CALL(
*proxy_,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment