fake_power_manager_client.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_FAKE_POWER_MANAGER_CLIENT_H_
  5. #define CHROMEOS_DBUS_POWER_FAKE_POWER_MANAGER_CLIENT_H_
  6. #include <memory>
  7. #include <queue>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback_forward.h"
  12. #include "base/component_export.h"
  13. #include "base/containers/circular_deque.h"
  14. #include "base/containers/flat_map.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/observer_list.h"
  17. #include "base/time/tick_clock.h"
  18. #include "base/time/time.h"
  19. #include "chromeos/dbus/power/power_manager_client.h"
  20. #include "chromeos/dbus/power_manager/backlight.pb.h"
  21. #include "chromeos/dbus/power_manager/policy.pb.h"
  22. #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
  23. #include "chromeos/dbus/power_manager/suspend.pb.h"
  24. #include "third_party/abseil-cpp/absl/types/optional.h"
  25. namespace base {
  26. class OneShotTimer;
  27. }
  28. namespace chromeos {
  29. // A fake implementation of PowerManagerClient. This remembers the policy passed
  30. // to SetPolicy() and the user of this class can inspect the last set policy by
  31. // get_policy().
  32. class COMPONENT_EXPORT(DBUS_POWER) FakePowerManagerClient
  33. : public PowerManagerClient {
  34. public:
  35. FakePowerManagerClient();
  36. FakePowerManagerClient(const FakePowerManagerClient&) = delete;
  37. FakePowerManagerClient& operator=(const FakePowerManagerClient&) = delete;
  38. ~FakePowerManagerClient() override;
  39. // Checks that FakePowerManagerClient was initialized and returns it.
  40. static FakePowerManagerClient* Get();
  41. const power_manager::PowerManagementPolicy& policy() { return policy_; }
  42. int num_request_restart_calls() const { return num_request_restart_calls_; }
  43. int num_request_shutdown_calls() const { return num_request_shutdown_calls_; }
  44. int num_set_policy_calls() const { return num_set_policy_calls_; }
  45. int num_set_is_projecting_calls() const {
  46. return num_set_is_projecting_calls_;
  47. }
  48. int num_wake_notification_calls() const {
  49. return num_wake_notification_calls_;
  50. }
  51. int num_pending_suspend_readiness_callbacks() const {
  52. return num_pending_suspend_readiness_callbacks_;
  53. }
  54. double screen_brightness_percent() const {
  55. return screen_brightness_percent_.value();
  56. }
  57. bool is_projecting() const { return is_projecting_; }
  58. bool have_video_activity_report() const {
  59. return !video_activity_reports_.empty();
  60. }
  61. bool backlights_forced_off() const { return backlights_forced_off_; }
  62. int num_set_backlights_forced_off_calls() const {
  63. return num_set_backlights_forced_off_calls_;
  64. }
  65. void set_enqueue_brightness_changes_on_backlights_forced_off(bool enqueue) {
  66. enqueue_brightness_changes_on_backlights_forced_off_ = enqueue;
  67. }
  68. const std::queue<power_manager::BacklightBrightnessChange>&
  69. pending_screen_brightness_changes() const {
  70. return pending_screen_brightness_changes_;
  71. }
  72. void set_user_activity_callback(base::RepeatingClosure callback) {
  73. user_activity_callback_ = std::move(callback);
  74. }
  75. void set_peripheral_battery_refresh_level(const std::string& address,
  76. int level) {
  77. peripheral_battery_refresh_levels_[address] = level;
  78. }
  79. void set_restart_callback(base::OnceClosure callback) {
  80. restart_callback_ = std::move(callback);
  81. }
  82. // PowerManagerClient overrides:
  83. void AddObserver(Observer* observer) override;
  84. void RemoveObserver(Observer* observer) override;
  85. bool HasObserver(const Observer* observer) const override;
  86. void SetRenderProcessManagerDelegate(
  87. base::WeakPtr<RenderProcessManagerDelegate> delegate) override;
  88. void DecreaseScreenBrightness(bool allow_off) override;
  89. void IncreaseScreenBrightness() override;
  90. void SetScreenBrightness(
  91. const power_manager::SetBacklightBrightnessRequest& request) override;
  92. void GetScreenBrightnessPercent(DBusMethodCallback<double> callback) override;
  93. void DecreaseKeyboardBrightness() override;
  94. void IncreaseKeyboardBrightness() override;
  95. void GetKeyboardBrightnessPercent(
  96. DBusMethodCallback<double> callback) override;
  97. void SetKeyboardBacklightToggledOff(bool toggled_off) override;
  98. void GetKeyboardBacklightToggledOff(
  99. DBusMethodCallback<bool> callback) override;
  100. const absl::optional<power_manager::PowerSupplyProperties>& GetLastStatus()
  101. override;
  102. void RequestStatusUpdate() override;
  103. void RequestAllPeripheralBatteryUpdate() override;
  104. void RequestThermalState() override;
  105. void RequestSuspend() override;
  106. void RequestRestart(power_manager::RequestRestartReason reason,
  107. const std::string& description) override;
  108. void RequestShutdown(power_manager::RequestShutdownReason reason,
  109. const std::string& description) override;
  110. void NotifyUserActivity(power_manager::UserActivityType type) override;
  111. void NotifyVideoActivity(bool is_fullscreen) override;
  112. void NotifyWakeNotification() override;
  113. void SetPolicy(const power_manager::PowerManagementPolicy& policy) override;
  114. void SetIsProjecting(bool is_projecting) override;
  115. void SetPowerSource(const std::string& id) override;
  116. void SetBacklightsForcedOff(bool forced_off) override;
  117. void GetBacklightsForcedOff(DBusMethodCallback<bool> callback) override;
  118. void GetSwitchStates(DBusMethodCallback<SwitchStates> callback) override;
  119. void GetInactivityDelays(
  120. DBusMethodCallback<power_manager::PowerManagementPolicy::Delays> callback)
  121. override;
  122. void BlockSuspend(const base::UnguessableToken& token,
  123. const std::string& debug_info) override;
  124. void UnblockSuspend(const base::UnguessableToken& token) override;
  125. bool SupportsAmbientColor() override;
  126. void CreateArcTimers(
  127. const std::string& tag,
  128. std::vector<std::pair<clockid_t, base::ScopedFD>> arc_timer_requests,
  129. DBusMethodCallback<std::vector<TimerId>> callback) override;
  130. void StartArcTimer(TimerId timer_id,
  131. base::TimeTicks absolute_expiration_time,
  132. VoidDBusMethodCallback callback) override;
  133. void DeleteArcTimers(const std::string& tag,
  134. VoidDBusMethodCallback callback) override;
  135. base::TimeDelta GetDarkSuspendDelayTimeout() override;
  136. void RefreshBluetoothBattery(const std::string& address) override;
  137. void SetExternalDisplayALSBrightness(bool enabled) override;
  138. void GetExternalDisplayALSBrightness(
  139. DBusMethodCallback<bool> callback) override;
  140. void ChargeNowForAdaptiveCharging() override;
  141. // Pops the first report from |video_activity_reports_|, returning whether the
  142. // activity was fullscreen or not. There must be at least one report.
  143. bool PopVideoActivityReport();
  144. // Emulates the power manager announcing that the system is starting or
  145. // completing a suspend attempt.
  146. void SendSuspendImminent(power_manager::SuspendImminent::Reason reason);
  147. void SendSuspendDone(base::TimeDelta sleep_duration = base::TimeDelta());
  148. void SendDarkSuspendImminent();
  149. // Emulates the power manager announcing that the system is changing the
  150. // screen or keyboard brightness.
  151. void SendScreenBrightnessChanged(
  152. const power_manager::BacklightBrightnessChange& proto);
  153. void SendKeyboardBrightnessChanged(
  154. const power_manager::BacklightBrightnessChange& proto);
  155. // Notifies observers about the screen idle state changing.
  156. void SendScreenIdleStateChanged(const power_manager::ScreenIdleState& proto);
  157. // Notifies observers that the power button has been pressed or released.
  158. void SendPowerButtonEvent(bool down, const base::TimeTicks& timestamp);
  159. // Sets |lid_state_| or |tablet_mode_| and notifies |observers_| about the
  160. // change.
  161. void SetLidState(LidState state, const base::TimeTicks& timestamp);
  162. void SetTabletMode(TabletMode mode, const base::TimeTicks& timestamp);
  163. // Sets |inactivity_delays_| and notifies |observers_| about the change.
  164. void SetInactivityDelays(
  165. const power_manager::PowerManagementPolicy::Delays& delays);
  166. // Updates |props_| and notifies observers of its changes.
  167. void UpdatePowerProperties(
  168. const power_manager::PowerSupplyProperties& power_props);
  169. // The PowerAPI requests system wake lock asynchronously. Test can run a
  170. // RunLoop and set the quit closure by this function to make sure the wake
  171. // lock has been created.
  172. void SetPowerPolicyQuitClosure(base::OnceClosure quit_closure);
  173. // Updates screen brightness to the first pending value in
  174. // |pending_screen_brightness_changes_|.
  175. // Returns whether the screen brightness change was applied - this will
  176. // return false if there are no pending brightness changes.
  177. bool ApplyPendingScreenBrightnessChange();
  178. // Returns time ticks from boot including time ticks spent during sleeping.
  179. base::TimeTicks GetCurrentBootTime();
  180. // Sets the screen brightness percent to be returned.
  181. // The nullopt |percent| means an error. In case of success,
  182. // |percent| must be in the range of [0, 100].
  183. void set_screen_brightness_percent(const absl::optional<double>& percent) {
  184. screen_brightness_percent_ = percent;
  185. }
  186. void set_keyboard_brightness_percent(const absl::optional<double>& percent) {
  187. keyboard_brightness_percent_ = percent;
  188. }
  189. void set_supports_ambient_color(bool supports_ambient_color) {
  190. supports_ambient_color_ = supports_ambient_color;
  191. }
  192. // Sets |tick_clock| to |tick_clock_|.
  193. void set_tick_clock(const base::TickClock* tick_clock) {
  194. tick_clock_ = tick_clock;
  195. }
  196. void simulate_start_arc_timer_failure(bool simulate) {
  197. simulate_start_arc_timer_failure_ = simulate;
  198. }
  199. private:
  200. // Notifies |observers_| that |props_| has been updated.
  201. void NotifyObservers();
  202. // Deletes all timers, if any, associated with |tag|.
  203. void DeleteArcTimersInternal(const std::string& tag);
  204. base::ObserverList<Observer>::Unchecked observers_;
  205. // Last policy passed to SetPolicy().
  206. power_manager::PowerManagementPolicy policy_;
  207. // Power status received from the power manager.
  208. absl::optional<power_manager::PowerSupplyProperties> props_;
  209. // Number of times that various methods have been called.
  210. int num_request_restart_calls_ = 0;
  211. int num_request_shutdown_calls_ = 0;
  212. int num_set_policy_calls_ = 0;
  213. int num_set_is_projecting_calls_ = 0;
  214. int num_set_backlights_forced_off_calls_ = 0;
  215. int num_wake_notification_calls_ = 0;
  216. // Number of pending suspend readiness callbacks.
  217. int num_pending_suspend_readiness_callbacks_ = 0;
  218. // Current screen brightness in the range [0.0, 100.0].
  219. absl::optional<double> screen_brightness_percent_;
  220. // Current keyboard brightness in the range [0.0, 100.0].
  221. absl::optional<double> keyboard_brightness_percent_;
  222. // Last screen brightness requested via SetScreenBrightness().
  223. // Unlike |screen_brightness_percent_|, this value will not be changed by
  224. // SetBacklightsForcedOff() method - a method that implicitly changes screen
  225. // brightness.
  226. // Initially set to an arbitrary non-null value.
  227. double requested_screen_brightness_percent_ = 80;
  228. // Last projecting state set in SetIsProjecting().
  229. bool is_projecting_ = false;
  230. // Display and keyboard backlights (if present) forced off state set in
  231. // SetBacklightsForcedOff().
  232. bool backlights_forced_off_ = false;
  233. // Whether screen brightness changes in SetBacklightsForcedOff() should be
  234. // enqueued.
  235. // If not set, SetBacklightsForcedOff() will update current screen
  236. // brightness and send a brightness change event (provided undimmed
  237. // brightness percent is set).
  238. // If set, brightness changes will be enqueued to
  239. // |pending_screen_brightness_changes_|, and will have to be applied
  240. // explicitly by calling ApplyPendingScreenBrightnessChange().
  241. bool enqueue_brightness_changes_on_backlights_forced_off_ = false;
  242. // Whether the device has an ambient color sensor. Can be set via
  243. // SetSupportsAmbientColor().
  244. bool supports_ambient_color_ = false;
  245. // Pending screen brightness changes caused by SetBacklightsForcedOff().
  246. // ApplyPendingScreenBrightnessChange() applies the first pending change.
  247. std::queue<power_manager::BacklightBrightnessChange>
  248. pending_screen_brightness_changes_;
  249. // Delays returned by GetInactivityDelays().
  250. power_manager::PowerManagementPolicy::Delays inactivity_delays_;
  251. // States returned by GetSwitchStates().
  252. LidState lid_state_ = LidState::OPEN;
  253. TabletMode tablet_mode_ = TabletMode::UNSUPPORTED;
  254. // Monotonically increasing timer id assigned to created timers.
  255. TimerId next_timer_id_ = 1;
  256. // Represents the timer and the timer expiration fd associated with a timer id
  257. // stored as the key. The fd is written to when the timer associated with the
  258. // clock expires.
  259. base::flat_map<TimerId,
  260. std::pair<std::unique_ptr<base::OneShotTimer>, base::ScopedFD>>
  261. arc_timers_;
  262. // Maps a client's tag to its list of timer ids.
  263. base::flat_map<std::string, std::vector<TimerId>> client_timer_ids_;
  264. // Video activity reports that we were requested to send, in the order they
  265. // were requested. True if fullscreen.
  266. base::circular_deque<bool> video_activity_reports_;
  267. // Delegate for managing power consumption of Chrome's renderer processes.
  268. base::WeakPtr<RenderProcessManagerDelegate> render_process_manager_delegate_;
  269. // If non-empty, called by SetPowerPolicy().
  270. base::OnceClosure power_policy_quit_closure_;
  271. // Callback that will be run, if set, when RequestRestart() is called.
  272. base::OnceClosure restart_callback_;
  273. // If non-empty, called by NotifyUserActivity().
  274. base::RepeatingClosure user_activity_callback_;
  275. // Clock to use to calculate time ticks. Used for ArcTimer related APIs.
  276. const base::TickClock* tick_clock_;
  277. // If set then |StartArcTimer| returns failure.
  278. bool simulate_start_arc_timer_failure_ = false;
  279. // Used in RefreshBluetoothBattery.
  280. base::flat_map<std::string, int> peripheral_battery_refresh_levels_;
  281. bool external_display_als_brightness_enabled_ = false;
  282. // Note: This should remain the last member so it'll be destroyed and
  283. // invalidate its weak pointers before any other members are destroyed.
  284. base::WeakPtrFactory<FakePowerManagerClient> weak_ptr_factory_{this};
  285. };
  286. } // namespace chromeos
  287. // TODO(https://crbug.com/1164001): remove when moved to ash.
  288. namespace ash {
  289. using ::chromeos::FakePowerManagerClient;
  290. }
  291. #endif // CHROMEOS_DBUS_POWER_FAKE_POWER_MANAGER_CLIENT_H_