• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_POWER_MONITOR_SOURCE_H_
6 #define BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_
7 
8 #include "base/base_export.h"
9 #include "base/power_monitor/power_observer.h"
10 #include "base/synchronization/lock.h"
11 #include "build/build_config.h"
12 
13 namespace base {
14 
15 // Communicates power state changes to the power monitor.
16 class BASE_EXPORT PowerMonitorSource {
17  public:
18   PowerMonitorSource();
19 
20   PowerMonitorSource(const PowerMonitorSource&) = delete;
21   PowerMonitorSource& operator=(const PowerMonitorSource&) = delete;
22 
23   virtual ~PowerMonitorSource();
24 
25   // Normalized list of power events.
26   enum PowerEvent {
27     POWER_STATE_EVENT,  // The Power status of the system has changed.
28     SUSPEND_EVENT,      // The system is being suspended.
29     RESUME_EVENT        // The system is being resumed.
30   };
31 
32   // Reads the current DeviceThermalState, if available on the platform.
33   // Otherwise, returns kUnknown.
34   virtual PowerThermalObserver::DeviceThermalState GetCurrentThermalState()
35       const;
36 
37   // Reads the initial operating system CPU speed limit, if available on the
38   // platform. Otherwise returns PowerThermalObserver::kSpeedLimitMax.
39   // Only called on the main thread in PowerMonitor::Initialize().
40   // The actual speed limit value will be updated asynchronously via the
41   // ProcessSpeedLimitEvent() if/when the value changes.
42   virtual int GetInitialSpeedLimit() const;
43 
44   // Update the result of thermal state.
45   virtual void SetCurrentThermalState(
46       PowerThermalObserver::DeviceThermalState state);
47 
48   // Platform-specific method to determine the battery power status.
49   virtual PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus()
50       const = 0;
51 
52 #if BUILDFLAG(IS_ANDROID)
53   // Read and return the current remaining battery capacity (microampere-hours).
54   virtual int GetRemainingBatteryCapacity() const;
55 #endif  // BUILDFLAG(IS_ANDROID)
56 
57   static const char* DeviceThermalStateToString(
58       PowerThermalObserver::DeviceThermalState state);
59 
60  protected:
61   friend class PowerMonitorTest;
62 
63   // Friend function that is allowed to access the protected ProcessPowerEvent.
64   friend void ProcessPowerEventHelper(PowerEvent);
65   friend void ProcessThermalEventHelper(
66       PowerThermalObserver::DeviceThermalState);
67 
68   // Process*Event should only be called from a single thread, most likely
69   // the UI thread or, in child processes, the IO thread.
70   static void ProcessPowerEvent(PowerEvent event_id);
71   static void ProcessThermalEvent(
72       PowerThermalObserver::DeviceThermalState new_thermal_state);
73   static void ProcessSpeedLimitEvent(int speed_limit);
74 };
75 
76 }  // namespace base
77 
78 #endif  // BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_
79