• 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 
36   // Reads the initial operating system CPU speed limit, if available on the
37   // platform. Otherwise returns PowerThermalObserver::kSpeedLimitMax.
38   // Only called on the main thread in PowerMonitor::Initialize().
39   // The actual speed limit value will be updated asynchronously via the
40   // ProcessSpeedLimitEvent() if/when the value changes.
41   virtual int GetInitialSpeedLimit();
42 
43   // Update the result of thermal state.
44   virtual void SetCurrentThermalState(
45       PowerThermalObserver::DeviceThermalState state);
46 
47   // Platform-specific method to check whether the system is currently
48   // running on battery power.
49   virtual bool IsOnBatteryPower() = 0;
50 
51 #if BUILDFLAG(IS_ANDROID)
52   // Read and return the current remaining battery capacity (microampere-hours).
53   virtual int GetRemainingBatteryCapacity();
54 #endif  // BUILDFLAG(IS_ANDROID)
55 
56   static const char* DeviceThermalStateToString(
57       PowerThermalObserver::DeviceThermalState state);
58 
59  protected:
60   friend class PowerMonitorTest;
61 
62   // Friend function that is allowed to access the protected ProcessPowerEvent.
63   friend void ProcessPowerEventHelper(PowerEvent);
64   friend void ProcessThermalEventHelper(
65       PowerThermalObserver::DeviceThermalState);
66 
67   // Process*Event should only be called from a single thread, most likely
68   // the UI thread or, in child processes, the IO thread.
69   static void ProcessPowerEvent(PowerEvent event_id);
70   static void ProcessThermalEvent(
71       PowerThermalObserver::DeviceThermalState new_thermal_state);
72   static void ProcessSpeedLimitEvent(int speed_limit);
73 };
74 
75 }  // namespace base
76 
77 #endif  // BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_
78