Commit 1e56c551 authored by Miguel Casas-Sanchez's avatar Miguel Casas-Sanchez Committed by Commit Bot

base/power_monitor: Add method to query thermal state

wip wip

Add PowerMonitor::GetCurrentThermalState()

Bug: 1071431
Change-Id: Id3083e06f16fd2f05e685e77ddee94e274158758
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2248729Reviewed-by: default avatarHenrik Boström <hbos@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Miguel Casas <mcasas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781322}
parent 98721c01
...@@ -2743,6 +2743,7 @@ test("base_unittests") { ...@@ -2743,6 +2743,7 @@ test("base_unittests") {
"parameter_pack_unittest.cc", "parameter_pack_unittest.cc",
"path_service_unittest.cc", "path_service_unittest.cc",
"pickle_unittest.cc", "pickle_unittest.cc",
"power_monitor/power_monitor_device_source_unittest.cc",
"power_monitor/power_monitor_unittest.cc", "power_monitor/power_monitor_unittest.cc",
"process/environment_internal_unittest.cc", "process/environment_internal_unittest.cc",
"process/memory_unittest.cc", "process/memory_unittest.cc",
......
...@@ -52,6 +52,11 @@ bool PowerMonitor::IsProcessSuspended() { ...@@ -52,6 +52,11 @@ bool PowerMonitor::IsProcessSuspended() {
return g_is_process_suspended.load(std::memory_order_relaxed); return g_is_process_suspended.load(std::memory_order_relaxed);
} }
PowerObserver::DeviceThermalState PowerMonitor::GetCurrentThermalState() {
DCHECK(IsInitialized());
return GetInstance()->source_->GetCurrentThermalState();
}
void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) {
DCHECK(IsInitialized()); DCHECK(IsInitialized());
DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off")
......
...@@ -56,6 +56,10 @@ class BASE_EXPORT PowerMonitor { ...@@ -56,6 +56,10 @@ class BASE_EXPORT PowerMonitor {
// what is the real power state. // what is the real power state.
static bool IsProcessSuspended(); static bool IsProcessSuspended();
// Read the current DeviceThermalState if known. Can be called on any thread.
// May only be called if the PowerMonitor has been initialized.
static PowerObserver::DeviceThermalState GetCurrentThermalState();
// Uninitializes the PowerMonitor. Should be called at the end of any unit // Uninitializes the PowerMonitor. Should be called at the end of any unit
// test that mocks out the PowerMonitor, to avoid affecting subsequent tests. // test that mocks out the PowerMonitor, to avoid affecting subsequent tests.
// There must be no live PowerObservers when invoked. Safe to call even if the // There must be no live PowerObservers when invoked. Safe to call even if the
......
...@@ -47,6 +47,8 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { ...@@ -47,6 +47,8 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
#endif #endif
private: private:
friend class PowerMonitorDeviceSourceTest;
#if defined(OS_WIN) #if defined(OS_WIN)
// Represents a message-only window for power message handling on Windows. // Represents a message-only window for power message handling on Windows.
// Only allow PowerMonitor to create it. // Only allow PowerMonitor to create it.
...@@ -86,6 +88,9 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { ...@@ -86,6 +88,9 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
bool IsOnBatteryPowerImpl() override; bool IsOnBatteryPowerImpl() override;
#if defined(OS_MACOSX) && !defined(OS_IOS) #if defined(OS_MACOSX) && !defined(OS_IOS)
// PowerMonitorSource:
PowerObserver::DeviceThermalState GetCurrentThermalState() override;
// Reference to the system IOPMrootDomain port. // Reference to the system IOPMrootDomain port.
io_connect_t power_manager_port_ = IO_OBJECT_NULL; io_connect_t power_manager_port_ = IO_OBJECT_NULL;
......
...@@ -51,6 +51,14 @@ bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() { ...@@ -51,6 +51,14 @@ bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
return true; return true;
} }
PowerObserver::DeviceThermalState
PowerMonitorDeviceSource::GetCurrentThermalState() {
if (@available(macOS 10.10.3, *)) {
return thermal_state_observer_->GetCurrentThermalState();
};
return PowerObserver::DeviceThermalState::kUnknown;
}
namespace { namespace {
void BatteryEventCallback(void*) { void BatteryEventCallback(void*) {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_source.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
using DeviceThermalState = base::PowerObserver::DeviceThermalState;
namespace base {
class PowerMonitorDeviceSourceTest : public testing::Test {
public:
PowerMonitorDeviceSourceTest() = default;
~PowerMonitorDeviceSourceTest() override = default;
DeviceThermalState GetCurrentThermalState() {
return power_monitor_device_source_.GetCurrentThermalState();
}
PowerMonitorDeviceSource power_monitor_device_source_;
};
TEST_F(PowerMonitorDeviceSourceTest, GetCurrentThermalState) {
const DeviceThermalState current_state = GetCurrentThermalState();
#if defined(OS_MACOSX) && !defined(OS_IOS)
// We cannot make assumptions on |current_state|. Print it out to use the var.
DVLOG(1) << PowerMonitorSource::DeviceThermalStateToString(current_state);
#else
EXPECT_EQ(current_state, DeviceThermalState::kUnknown);
#endif
}
} // namespace base
...@@ -17,6 +17,10 @@ bool PowerMonitorSource::IsOnBatteryPower() { ...@@ -17,6 +17,10 @@ bool PowerMonitorSource::IsOnBatteryPower() {
return on_battery_power_; return on_battery_power_;
} }
PowerObserver::DeviceThermalState PowerMonitorSource::GetCurrentThermalState() {
return PowerObserver::DeviceThermalState::kUnknown;
}
// static // static
void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) {
if (!PowerMonitor::IsInitialized()) if (!PowerMonitor::IsInitialized())
......
...@@ -31,6 +31,10 @@ class BASE_EXPORT PowerMonitorSource { ...@@ -31,6 +31,10 @@ class BASE_EXPORT PowerMonitorSource {
// Is the computer currently on battery power. Can be called on any thread. // Is the computer currently on battery power. Can be called on any thread.
bool IsOnBatteryPower(); bool IsOnBatteryPower();
// Reads the current DeviceThermalState, if available on the platform.
// Otherwise, returns kUnknown.
virtual PowerObserver::DeviceThermalState GetCurrentThermalState();
static const char* DeviceThermalStateToString( static const char* DeviceThermalStateToString(
PowerObserver::DeviceThermalState state); PowerObserver::DeviceThermalState state);
......
...@@ -102,6 +102,7 @@ TEST_F(PowerMonitorTest, ThermalThrottling) { ...@@ -102,6 +102,7 @@ TEST_F(PowerMonitorTest, ThermalThrottling) {
for (const auto state : kThermalStates) { for (const auto state : kThermalStates) {
source()->GenerateThermalThrottlingEvent(state); source()->GenerateThermalThrottlingEvent(state);
EXPECT_EQ(state, source()->GetCurrentThermalState());
EXPECT_EQ(observer.last_thermal_state(), state); EXPECT_EQ(observer.last_thermal_state(), state);
} }
......
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
namespace base { namespace base {
// This class is used to listen for the thermal state change notification from // This class is used to listen for the thermal state change notification
// NSProcessInfoThermalStateDidChangeNotification via a fully owned // NSProcessInfoThermalStateDidChangeNotification, routing it to
// ThermalStateObserverDelegate, routing the notification to PowerMonitorSource. // PowerMonitorSource.
class BASE_EXPORT ThermalStateObserverMac { class BASE_EXPORT ThermalStateObserverMac {
public: public:
using StateUpdateCallback = using StateUpdateCallback =
...@@ -25,6 +25,8 @@ class BASE_EXPORT ThermalStateObserverMac { ...@@ -25,6 +25,8 @@ class BASE_EXPORT ThermalStateObserverMac {
explicit ThermalStateObserverMac(StateUpdateCallback state_update_callback); explicit ThermalStateObserverMac(StateUpdateCallback state_update_callback);
~ThermalStateObserverMac(); ~ThermalStateObserverMac();
PowerObserver::DeviceThermalState GetCurrentThermalState();
private: private:
FRIEND_TEST_ALL_PREFIXES(ThermalStateObserverMacTest, StateChange); FRIEND_TEST_ALL_PREFIXES(ThermalStateObserverMacTest, StateChange);
PowerObserver::DeviceThermalState state_for_testing_ = PowerObserver::DeviceThermalState state_for_testing_ =
......
...@@ -63,4 +63,13 @@ ThermalStateObserverMac::~ThermalStateObserverMac() { ...@@ -63,4 +63,13 @@ ThermalStateObserverMac::~ThermalStateObserverMac() {
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
removeObserver:thermal_state_update_observer_]; removeObserver:thermal_state_update_observer_];
} }
PowerObserver::DeviceThermalState
ThermalStateObserverMac::GetCurrentThermalState() NS_AVAILABLE_MAC(10_10_3) {
if (state_for_testing_ != PowerObserver::DeviceThermalState::kUnknown)
return state_for_testing_;
NSProcessInfoThermalState nsinfo_state =
[[NSProcessInfo processInfo] thermalState];
return NSProcessInfoThermalStateToDeviceThermalState(nsinfo_state);
}
} }
...@@ -11,14 +11,18 @@ ...@@ -11,14 +11,18 @@
namespace base { namespace base {
PowerMonitorTestSource::PowerMonitorTestSource() PowerMonitorTestSource::PowerMonitorTestSource() {
: test_on_battery_power_(false) {
DCHECK(MessageLoopCurrent::Get()) DCHECK(MessageLoopCurrent::Get())
<< "PowerMonitorTestSource requires a MessageLoop."; << "PowerMonitorTestSource requires a MessageLoop.";
} }
PowerMonitorTestSource::~PowerMonitorTestSource() = default; PowerMonitorTestSource::~PowerMonitorTestSource() = default;
PowerObserver::DeviceThermalState
PowerMonitorTestSource::GetCurrentThermalState() {
return current_thermal_state_;
}
void PowerMonitorTestSource::GeneratePowerStateEvent(bool on_battery_power) { void PowerMonitorTestSource::GeneratePowerStateEvent(bool on_battery_power) {
test_on_battery_power_ = on_battery_power; test_on_battery_power_ = on_battery_power;
ProcessPowerEvent(POWER_STATE_EVENT); ProcessPowerEvent(POWER_STATE_EVENT);
...@@ -42,6 +46,7 @@ bool PowerMonitorTestSource::IsOnBatteryPowerImpl() { ...@@ -42,6 +46,7 @@ bool PowerMonitorTestSource::IsOnBatteryPowerImpl() {
void PowerMonitorTestSource::GenerateThermalThrottlingEvent( void PowerMonitorTestSource::GenerateThermalThrottlingEvent(
PowerObserver::DeviceThermalState new_thermal_state) { PowerObserver::DeviceThermalState new_thermal_state) {
ProcessThermalEvent(new_thermal_state); ProcessThermalEvent(new_thermal_state);
current_thermal_state_ = new_thermal_state;
RunLoop().RunUntilIdle(); RunLoop().RunUntilIdle();
} }
......
...@@ -14,6 +14,7 @@ class PowerMonitorTestSource : public PowerMonitorSource { ...@@ -14,6 +14,7 @@ class PowerMonitorTestSource : public PowerMonitorSource {
public: public:
PowerMonitorTestSource(); PowerMonitorTestSource();
~PowerMonitorTestSource() override; ~PowerMonitorTestSource() override;
PowerObserver::DeviceThermalState GetCurrentThermalState() override;
void GeneratePowerStateEvent(bool on_battery_power); void GeneratePowerStateEvent(bool on_battery_power);
void GenerateSuspendEvent(); void GenerateSuspendEvent();
...@@ -24,7 +25,9 @@ class PowerMonitorTestSource : public PowerMonitorSource { ...@@ -24,7 +25,9 @@ class PowerMonitorTestSource : public PowerMonitorSource {
protected: protected:
bool IsOnBatteryPowerImpl() override; bool IsOnBatteryPowerImpl() override;
bool test_on_battery_power_; bool test_on_battery_power_ = false;
PowerObserver::DeviceThermalState current_thermal_state_ =
PowerObserver::DeviceThermalState::kUnknown;
}; };
class PowerMonitorTestObserver : public PowerObserver { class PowerMonitorTestObserver : public PowerObserver {
......
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