1 // Copyright 2020 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 #ifndef BASE_POWER_MONITOR_THERMAL_STATE_OBSERVER_MAC_H_ 6 #define BASE_POWER_MONITOR_THERMAL_STATE_OBSERVER_MAC_H_ 7 8 #include <dispatch/dispatch.h> 9 10 #include <memory> 11 12 #include <IOKit/pwr_mgt/IOPMLib.h> 13 #include "base/base_export.h" 14 #include "base/functional/callback.h" 15 #include "base/gtest_prod_util.h" 16 #include "base/power_monitor/power_observer.h" 17 18 namespace base { 19 20 // This class is used to listen for the thermal state change notification 21 // NSProcessInfoThermalStateDidChangeNotification, routing it to 22 // PowerMonitorSource. 23 class BASE_EXPORT ThermalStateObserverMac { 24 public: 25 using StateUpdateCallback = 26 base::RepeatingCallback<void(PowerThermalObserver::DeviceThermalState)>; 27 using SpeedLimitUpdateCallback = base::RepeatingCallback<void(int)>; 28 29 ThermalStateObserverMac( 30 StateUpdateCallback state_update_callback, 31 SpeedLimitUpdateCallback speed_limit_update_callback, 32 // This optional argument is overridden from tests because Apple software 33 // doesn't seem to permit injecting notifications in their domains. 34 // NOTE: this must be a statically allocated string as the pointer value 35 // is stored internally. 36 const char* power_notification_key = kIOPMCPUPowerNotificationKey); 37 ~ThermalStateObserverMac(); 38 39 PowerThermalObserver::DeviceThermalState GetCurrentThermalState(); 40 int GetCurrentSpeedLimit(); 41 42 private: 43 FRIEND_TEST_ALL_PREFIXES(ThermalStateObserverMacTest, StateChange); 44 PowerThermalObserver::DeviceThermalState state_for_testing_ = 45 PowerThermalObserver::DeviceThermalState::kUnknown; 46 47 const char* const power_notification_key_; 48 int speed_limit_notification_token_ = 0; 49 50 struct ObjCStorage; 51 std::unique_ptr<ObjCStorage> objc_storage_; 52 }; 53 54 } // namespace base 55 56 #endif // BASE_POWER_MONITOR_THERMAL_STATE_OBSERVER_MAC_H_ 57