power_event_observer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ASH_SYSTEM_POWER_POWER_EVENT_OBSERVER_H_
  5. #define ASH_SYSTEM_POWER_POWER_EVENT_OBSERVER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/login_status.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/unguessable_token.h"
  12. #include "chromeos/dbus/power/power_manager_client.h"
  13. namespace ui {
  14. class CompositorObserver;
  15. }
  16. namespace ash {
  17. class LockOnSuspendUsage;
  18. // A class that observes power-management-related events - in particular, it
  19. // observes the device suspend state and updates display states accordingly.
  20. // When the device suspends, it suspends all displays and stops compositing.
  21. // On resume, displays are resumed, and compositing is started again.
  22. // During suspend, it ensures compositing is not stopped prematurely if the
  23. // screen is being locked during suspend - display compositing will not be
  24. // stopped before:
  25. // 1. lock screen window is shown
  26. // 2. wallpaper changes due to screen lock are finished
  27. // 3. the compositor goes through at least two compositing cycles after the
  28. // screen lock
  29. // This is done to ensure that displays have picked up frames from after the
  30. // screen was locked. Without this, displays might initially show
  31. // pre-screen-lock frames when resumed.
  32. // For example, see https://crbug.com/807511.
  33. class ASH_EXPORT PowerEventObserver
  34. : public chromeos::PowerManagerClient::Observer,
  35. public SessionObserver {
  36. public:
  37. // This class registers/unregisters itself as an observer in ctor/dtor.
  38. PowerEventObserver();
  39. PowerEventObserver(const PowerEventObserver&) = delete;
  40. PowerEventObserver& operator=(const PowerEventObserver&) = delete;
  41. ~PowerEventObserver() override;
  42. // Called by the WebUIScreenLocker when all the lock screen animations have
  43. // completed. This really should be implemented via an observer but since
  44. // ash/ isn't allowed to depend on chrome/ we need to have the
  45. // WebUIScreenLocker reach into ash::Shell to make this call.
  46. void OnLockAnimationsComplete();
  47. // chromeos::PowerManagerClient::Observer overrides:
  48. void SuspendImminent(power_manager::SuspendImminent::Reason reason) override;
  49. void SuspendDoneEx(const power_manager::SuspendDone& proto) override;
  50. void LidEventReceived(chromeos::PowerManagerClient::LidState state,
  51. base::TimeTicks timestamp) override;
  52. // SessionObserver overrides:
  53. void OnLoginStatusChanged(LoginStatus login_status) override;
  54. void OnLockStateChanged(bool locked) override;
  55. // Sets whether the device is projecting (docked). This is used in along with
  56. // lid state to lock the device.
  57. void SetIsProjecting(bool is_projecting);
  58. private:
  59. friend class PowerEventObserverTestApi;
  60. // Locks device when lid is closed, and device is not projecting (docked), if
  61. // user/policy settings configured.
  62. void MaybeLockOnLidClose(bool is_projecting);
  63. enum class LockState {
  64. // Screen lock has not been requested, nor detected.
  65. kUnlocked,
  66. // Screen lock has been requested, or detected, but screen lock has not
  67. // reported that it finished showing.
  68. kLocking,
  69. // Screen has been locked, but all compositors might not have yet picked up
  70. // locked screen state - |compositor_watcher_| is observing compositors,
  71. // waiting for them to become ready to suspend.
  72. kLockedCompositingPending,
  73. // Screen is locked, and displays have picked up lock screen changes - it
  74. // should be safe to stop compositing and start suspend at this time.
  75. kLocked,
  76. };
  77. // Sets all root window compositors' visibility to true.
  78. void StartRootWindowCompositors();
  79. // Sets all root window compositors' visibility to false, and then suspends
  80. // displays. It will run unblock suspend via |block_suspend_token_| once
  81. // displays are suspended. This should only be called when it's safe to stop
  82. // compositing - either if the screen is not expected to get locked, or all
  83. // compositors have gone through compositing cycle after the screen was
  84. // locked.
  85. void StopCompositingAndSuspendDisplays();
  86. // If any of the root windows have pending wallpaper animations, it stops
  87. // them - this is used to stop wallpaper animations during suspend, and thus
  88. // improve the suspend time (given that suspend will be delayed until the
  89. // wallpaper animations finish).
  90. void EndPendingWallpaperAnimations();
  91. // Callback run by |compositor_watcher_| when it detects that composting
  92. // can be stopped for all root windows when device suspends.
  93. void OnCompositorsReadyForSuspend();
  94. LockState lock_state_ = LockState::kUnlocked;
  95. chromeos::PowerManagerClient::LidState lid_state_ =
  96. chromeos::PowerManagerClient::LidState::OPEN;
  97. ScopedSessionObserver session_observer_;
  98. // Whether the device is suspending.
  99. bool suspend_in_progress_ = false;
  100. // Used to observe compositing state after screen lock to detect when display
  101. // compositors are in state in which it's safe to proceed with suspend.
  102. std::unique_ptr<ui::CompositorObserver> compositor_watcher_;
  103. // Token set when device suspend is delayed due to a screen lock - suspend
  104. // should be continued when the screen lock finishes showing and display
  105. // compositors pick up screen lock changes. All compositors should be stopped
  106. // prior to unblocking and clearing this - call
  107. // StopCompositingAndSuspendDisplays(). This will only be set while the device
  108. // is suspending.
  109. base::UnguessableToken block_suspend_token_;
  110. std::unique_ptr<LockOnSuspendUsage> lock_on_suspend_usage_;
  111. };
  112. } // namespace ash
  113. #endif // ASH_SYSTEM_POWER_POWER_EVENT_OBSERVER_H_