power_policy_controller.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright (c) 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 CHROMEOS_DBUS_POWER_POWER_POLICY_CONTROLLER_H_
  5. #define CHROMEOS_DBUS_POWER_POWER_POLICY_CONTROLLER_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/values.h"
  11. #include "chromeos/dbus/power/power_manager_client.h"
  12. #include "chromeos/dbus/power_manager/policy.pb.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace chromeos {
  15. // PowerPolicyController is responsible for sending Chrome's assorted power
  16. // management preferences to the Chrome OS power manager.
  17. class COMPONENT_EXPORT(DBUS_POWER) PowerPolicyController
  18. : public PowerManagerClient::Observer {
  19. public:
  20. using PeakShiftDayConfig =
  21. power_manager::PowerManagementPolicy::PeakShiftDayConfig;
  22. using AdvancedBatteryChargeModeDayConfig =
  23. power_manager::PowerManagementPolicy::AdvancedBatteryChargeModeDayConfig;
  24. // Sets the global instance. Must be called before any calls to Get().
  25. static void Initialize(PowerManagerClient* power_manager_client);
  26. // Returns true if the global instance has been initialized.
  27. static bool IsInitialized();
  28. // Destroys the global instance.
  29. static void Shutdown();
  30. // Returns the global instance. Initialize() must be called first.
  31. static PowerPolicyController* Get();
  32. PowerPolicyController(const PowerPolicyController&) = delete;
  33. PowerPolicyController& operator=(const PowerPolicyController&) = delete;
  34. // Reasons why a wake lock may be added.
  35. // TODO(derat): Remove this enum in favor of device::mojom::WakeLockReason
  36. // once this class has been moved to the device service:
  37. // https://crbug.com/702449
  38. enum WakeLockReason {
  39. REASON_AUDIO_PLAYBACK,
  40. REASON_VIDEO_PLAYBACK,
  41. REASON_OTHER,
  42. };
  43. // Note: Do not change these values; they are used by preferences.
  44. enum Action {
  45. ACTION_SUSPEND = 0,
  46. ACTION_STOP_SESSION = 1,
  47. ACTION_SHUT_DOWN = 2,
  48. ACTION_DO_NOTHING = 3,
  49. };
  50. // Values of various power-management-related preferences.
  51. struct PrefValues {
  52. PrefValues();
  53. ~PrefValues();
  54. // -1 is interpreted as "unset" by powerd, resulting in powerd's default
  55. // delays being used instead. There are no similarly-interpreted values for
  56. // the other fields, unfortunately (but the default values would only reach
  57. // powerd if Chrome failed to override them with the pref-assigned values).
  58. int ac_screen_dim_delay_ms = -1;
  59. int ac_quick_dim_delay_ms = -1;
  60. int ac_screen_off_delay_ms = -1;
  61. int ac_screen_lock_delay_ms = -1;
  62. int ac_quick_lock_delay_ms = -1;
  63. int ac_idle_warning_delay_ms = -1;
  64. int ac_idle_delay_ms = -1;
  65. int battery_screen_dim_delay_ms = -1;
  66. int battery_quick_dim_delay_ms = -1;
  67. int battery_screen_off_delay_ms = -1;
  68. int battery_screen_lock_delay_ms = -1;
  69. int battery_quick_lock_delay_ms = -1;
  70. int battery_idle_warning_delay_ms = -1;
  71. int battery_idle_delay_ms = -1;
  72. Action ac_idle_action = ACTION_SUSPEND;
  73. Action battery_idle_action = ACTION_SUSPEND;
  74. Action lid_closed_action = ACTION_SUSPEND;
  75. bool use_audio_activity = true;
  76. bool use_video_activity = true;
  77. double ac_brightness_percent = -1.0;
  78. double battery_brightness_percent = -1.0;
  79. bool allow_wake_locks = true;
  80. bool allow_screen_wake_locks = true;
  81. bool enable_auto_screen_lock = false;
  82. double presentation_screen_dim_delay_factor = 1.0;
  83. double user_activity_screen_dim_delay_factor = 1.0;
  84. bool wait_for_initial_user_activity = false;
  85. bool force_nonzero_brightness_for_user_activity = true;
  86. bool fast_suspend_when_backlights_forced_off = true;
  87. bool peak_shift_enabled = false;
  88. int peak_shift_battery_threshold = -1;
  89. std::vector<PeakShiftDayConfig> peak_shift_day_configs;
  90. bool advanced_battery_charge_mode_enabled = false;
  91. std::vector<AdvancedBatteryChargeModeDayConfig>
  92. advanced_battery_charge_mode_day_configs;
  93. bool boot_on_ac = false;
  94. bool usb_power_share = true;
  95. power_manager::PowerManagementPolicy::BatteryChargeMode::Mode
  96. battery_charge_mode =
  97. power_manager::PowerManagementPolicy::BatteryChargeMode::ADAPTIVE;
  98. int custom_charge_start = -1;
  99. int custom_charge_stop = -1;
  100. // Only set send_feedback_if_undimmed in policy proto if this field is set.
  101. absl::optional<bool> send_feedback_if_undimmed;
  102. // Only set adaptive_charging_enabled in policy proto if this field is set.
  103. absl::optional<bool> adaptive_charging_enabled;
  104. double adaptive_charging_min_probability = -1.0;
  105. int adaptive_charging_hold_percent = -1;
  106. };
  107. // Converts |base::Value::Dict| to |std::vector<PeakShiftDayConfig>| and
  108. // returns true if there are no missing fields and errors.
  109. static bool GetPeakShiftDayConfigs(
  110. const base::Value::Dict& value,
  111. std::vector<PeakShiftDayConfig>* configs_out);
  112. // Converts |base::Value::Dict| to
  113. // |std::vector<AdvancedBatteryChargeModeDayConfig>| and returns true if there
  114. // are no missing fields and errors.
  115. static bool GetAdvancedBatteryChargeModeDayConfigs(
  116. const base::Value::Dict& value,
  117. std::vector<AdvancedBatteryChargeModeDayConfig>* configs_out);
  118. // Saves appropriate value to |mode_out| and returns true if there is mapping
  119. // between battery charge mode int and enum value.
  120. static bool GetBatteryChargeModeFromInteger(
  121. int mode,
  122. power_manager::PowerManagementPolicy::BatteryChargeMode::Mode* mode_out);
  123. // Returns a string describing |policy|. Useful for comparisons in tests.
  124. static std::string GetPolicyDebugString(
  125. const power_manager::PowerManagementPolicy& policy);
  126. // Returns a string describing |PeakShift| part of |policy|. Useful for
  127. // comparisons in tests.
  128. static std::string GetPeakShiftPolicyDebugString(
  129. const power_manager::PowerManagementPolicy& policy);
  130. // Returns a string describing |AdvancedBatteryChargeMode| part of |policy|.
  131. // Useful for comparisons in tests.
  132. static std::string GetAdvancedBatteryChargeModePolicyDebugString(
  133. const power_manager::PowerManagementPolicy& policy);
  134. // Delay in milliseconds between the screen being turned off and the screen
  135. // being locked. Used if the |enable_auto_screen_lock| pref is set but
  136. // |*_screen_lock_delay_ms| are unset or set to higher values than what this
  137. // constant would imply.
  138. static const int kScreenLockAfterOffDelayMs;
  139. // String added to a PowerManagementPolicy |reason| field if the policy has
  140. // been modified by preferences.
  141. static const char kPrefsReason[];
  142. bool honor_screen_wake_locks_for_test() const {
  143. return honor_screen_wake_locks_;
  144. }
  145. // Updates |prefs_policy_| with |values| and sends an updated policy.
  146. void ApplyPrefs(const PrefValues& values);
  147. // Registers a request to temporarily prevent the screen from getting dimmed
  148. // or turned off or the system from suspending in response to user inactivity
  149. // and sends an updated policy. |description| is a human-readable description
  150. // of the reason the lock was created. Returns a unique ID that can be passed
  151. // to RemoveWakeLock() later.
  152. // See the comment above WakeLock::Type for descriptions of the lock types.
  153. int AddScreenWakeLock(WakeLockReason reason, const std::string& description);
  154. int AddDimWakeLock(WakeLockReason reason, const std::string& description);
  155. int AddSystemWakeLock(WakeLockReason reason, const std::string& description);
  156. // Unregisters a request previously created via an Add*WakeLock() call
  157. // and sends an updated policy.
  158. void RemoveWakeLock(int id);
  159. // Adjusts policy while Chrome is exiting. The lid-closed action
  160. // is overridden to ensure that the system doesn't suspend or shut
  161. // down.
  162. void NotifyChromeIsExiting();
  163. // Adjusts policy when the display is forced off in response to the
  164. // user tapping the power button, or when it's no longer forced off.
  165. void HandleBacklightsForcedOffForPowerButton(bool forced_off);
  166. // Adjusts policy when the migration of a user homedir to a new
  167. // encryption format starts or stops. While migration is active,
  168. // the lid-closed action is overridden to ensure the system
  169. // doesn't shut down.
  170. void SetEncryptionMigrationActive(bool active);
  171. // PowerManagerClient::Observer implementation:
  172. void PowerManagerRestarted() override;
  173. void ScreenBrightnessChanged(
  174. const power_manager::BacklightBrightnessChange& change) override;
  175. // Returns the maximum time set by policy for the screen to lock when idle
  176. // between AC and battery power sources. Returns zero if screen autolock is
  177. // disabled by policy.
  178. // If delay is set to zero, Google Chrome OS does not lock the screen when
  179. // the user becomes idle.
  180. // Note: The actual screen lock delay on the OS may differ as it takes other
  181. // factors into account, like wake locks.
  182. base::TimeDelta GetMaxPolicyAutoScreenLockDelay();
  183. private:
  184. explicit PowerPolicyController(PowerManagerClient* client);
  185. ~PowerPolicyController() override;
  186. // Details about a wake lock added via Add*WakeLock().
  187. // SCREEN and DIM will keep the screen on and prevent it from locking.
  188. // SCREEN will also prevent it from dimming. SYSTEM will prevent idle
  189. // suspends, but the screen will turn off and lock normally.
  190. struct WakeLock {
  191. // TODO(derat): Remove this enum in favor of device::mojom::WakeLockType
  192. // once this class has been moved to the device service:
  193. // https://crbug.com/702449
  194. enum Type {
  195. TYPE_SCREEN,
  196. TYPE_DIM,
  197. TYPE_SYSTEM,
  198. };
  199. WakeLock(Type type, WakeLockReason reason, const std::string& description);
  200. ~WakeLock();
  201. const Type type;
  202. const WakeLockReason reason;
  203. const std::string description;
  204. };
  205. using WakeLockMap = std::map<int, WakeLock>;
  206. // Helper method for AddScreenWakeLock() and AddSystemWakeLock().
  207. int AddWakeLockInternal(WakeLock::Type type,
  208. WakeLockReason reason,
  209. const std::string& description);
  210. // Sends a policy based on |prefs_policy_| to the power manager.
  211. void SendCurrentPolicy();
  212. PowerManagerClient* client_; // weak
  213. // Policy derived from values passed to ApplyPrefs().
  214. power_manager::PowerManagementPolicy prefs_policy_;
  215. // Was ApplyPrefs() called?
  216. bool prefs_were_set_ = false;
  217. // Maps from an ID representing a request to prevent the screen from
  218. // getting dimmed or turned off or to prevent the system from suspending
  219. // to details about the request.
  220. WakeLockMap wake_locks_;
  221. // Should |wake_locks_| be honored?
  222. bool honor_wake_locks_ = true;
  223. // If wake locks are honored, should TYPE_SCREEN or TYPE_DIM entries in
  224. // |wake_locks_| be honored?
  225. // If false, screen wake locks are just treated as TYPE_SYSTEM instead.
  226. bool honor_screen_wake_locks_ = true;
  227. // Next ID to be used by an Add*WakeLock() request.
  228. int next_wake_lock_id_ = 1;
  229. // True if Chrome is in the process of exiting.
  230. bool chrome_is_exiting_ = false;
  231. // True if the screen is currently forced off due to the user having tapped
  232. // the power button.
  233. bool backlights_forced_off_for_power_button_ = false;
  234. // True if suspend delays should be shortened when
  235. // |backlights_forced_off_for_power_button_| is true. Set from prefs.
  236. bool fast_suspend_when_backlights_forced_off_ = true;
  237. // True if a user homedir is in the process of migrating encryption formats.
  238. bool encryption_migration_active_ = false;
  239. // Whether brightness policy value was overridden by a user adjustment in the
  240. // current user session.
  241. bool per_session_brightness_override_ = false;
  242. // Indicates if screen autolock is enabled or not by policy.
  243. bool auto_screen_lock_enabled_ = false;
  244. };
  245. } // namespace chromeos
  246. // TODO(https://crbug.com/1164001): remove after the //chrome/browser/chromeos
  247. // source migration is finished.
  248. namespace ash {
  249. using ::chromeos::PowerPolicyController;
  250. }
  251. #endif // CHROMEOS_DBUS_POWER_POWER_POLICY_CONTROLLER_H_