power_monitor_source.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_
  5. #define BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_
  6. #include "base/base_export.h"
  7. #include "base/power_monitor/power_observer.h"
  8. #include "base/synchronization/lock.h"
  9. #include "build/build_config.h"
  10. namespace base {
  11. // Communicates power state changes to the power monitor.
  12. class BASE_EXPORT PowerMonitorSource {
  13. public:
  14. PowerMonitorSource();
  15. PowerMonitorSource(const PowerMonitorSource&) = delete;
  16. PowerMonitorSource& operator=(const PowerMonitorSource&) = delete;
  17. virtual ~PowerMonitorSource();
  18. // Normalized list of power events.
  19. enum PowerEvent {
  20. POWER_STATE_EVENT, // The Power status of the system has changed.
  21. SUSPEND_EVENT, // The system is being suspended.
  22. RESUME_EVENT // The system is being resumed.
  23. };
  24. // Reads the current DeviceThermalState, if available on the platform.
  25. // Otherwise, returns kUnknown.
  26. virtual PowerThermalObserver::DeviceThermalState GetCurrentThermalState();
  27. // Reads the initial operating system CPU speed limit, if available on the
  28. // platform. Otherwise returns PowerThermalObserver::kSpeedLimitMax.
  29. // Only called on the main thread in PowerMonitor::Initialize().
  30. // The actual speed limit value will be updated asynchronously via the
  31. // ProcessSpeedLimitEvent() if/when the value changes.
  32. virtual int GetInitialSpeedLimit();
  33. // Update the result of thermal state.
  34. virtual void SetCurrentThermalState(
  35. PowerThermalObserver::DeviceThermalState state);
  36. // Platform-specific method to check whether the system is currently
  37. // running on battery power.
  38. virtual bool IsOnBatteryPower() = 0;
  39. #if BUILDFLAG(IS_ANDROID)
  40. // Read and return the current remaining battery capacity (microampere-hours).
  41. virtual int GetRemainingBatteryCapacity();
  42. #endif // BUILDFLAG(IS_ANDROID)
  43. static const char* DeviceThermalStateToString(
  44. PowerThermalObserver::DeviceThermalState state);
  45. protected:
  46. friend class PowerMonitorTest;
  47. // Friend function that is allowed to access the protected ProcessPowerEvent.
  48. friend void ProcessPowerEventHelper(PowerEvent);
  49. friend void ProcessThermalEventHelper(
  50. PowerThermalObserver::DeviceThermalState);
  51. // Process*Event should only be called from a single thread, most likely
  52. // the UI thread or, in child processes, the IO thread.
  53. static void ProcessPowerEvent(PowerEvent event_id);
  54. static void ProcessThermalEvent(
  55. PowerThermalObserver::DeviceThermalState new_thermal_state);
  56. static void ProcessSpeedLimitEvent(int speed_limit);
  57. };
  58. } // namespace base
  59. #endif // BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_