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 5 #ifndef ASH_WM_LOCK_STATE_CONTROLLER_H_ 6 #define ASH_WM_LOCK_STATE_CONTROLLER_H_ 7 8 #include <memory> 9 10 #include "ash/ash_export.h" 11 #include "ash/public/cpp/session/session_observer.h" 12 #include "ash/shutdown_reason.h" 13 #include "ash/wallpaper/wallpaper_constants.h" 14 #include "ash/wm/lock_state_observer.h" 15 #include "ash/wm/session_state_animator.h" 16 #include "base/macros.h" 17 #include "base/memory/weak_ptr.h" 18 #include "base/observer_list.h" 19 #include "base/optional.h" 20 #include "base/time/time.h" 21 #include "base/timer/elapsed_timer.h" 22 #include "base/timer/timer.h" 23 #include "ui/aura/window_tree_host_observer.h" 24 25 namespace ash { 26 27 class ShutdownController; 28 enum class ShutdownReason; 29 30 // Displays onscreen animations and locks or suspends the system in response to 31 // the power button being pressed or released. 32 // Lock workflow: 33 // Entry point: 34 // * StartLockAnimation (bool shutdown after lock) - starts lock that can be 35 // cancelled. 36 // Once it completes, PreLockAnimationFinished is called, and system lock is 37 // requested. Once system locks and lock UI is created, OnLockStateChanged is 38 // called, and StartPostLockAnimation is called. In PostLockAnimationFinished 39 // two things happen : EVENT_LOCK_ANIMATION_FINISHED notification is sent (it 40 // triggers third part of animation within lock UI), and check for continuing to 41 // shutdown is made. 42 // 43 // Unlock workflow: 44 // WebUI does first part of animation, and calls OnLockScreenHide(callback) that 45 // triggers StartUnlockAnimationBeforeUIDestroyed(callback). Once callback is 46 // called at the end of the animation, lock UI is deleted, system unlocks, and 47 // OnLockStateChanged is called. It leads to 48 // StartUnlockAnimationAfterUIDestroyed. 49 class ASH_EXPORT LockStateController : public aura::WindowTreeHostObserver, 50 public SessionObserver { 51 public: 52 // A bitfield mask including NON_LOCK_SCREEN_CONTAINERS and LAUNCHER, used for 53 // pre-lock hiding animation. 54 static const int kPreLockContainersMask; 55 56 explicit LockStateController(ShutdownController* shutdown_controller); 57 ~LockStateController() override; 58 59 void AddObserver(LockStateObserver* observer); 60 void RemoveObserver(LockStateObserver* observer); 61 62 // Starts locking (with slow pre-lock animation) that can be cancelled. 63 void StartLockAnimation(); 64 65 // Starts shutting down (with slow animation) that can be cancelled. 66 void StartShutdownAnimation(ShutdownReason reason); 67 68 // Starts locking without slow animation. 69 void LockWithoutAnimation(); 70 71 // Returns true if we have requested system to lock, but haven't received 72 // confirmation yet. 73 bool LockRequested(); 74 75 // Returns true if we are shutting down. 76 bool ShutdownRequested(); 77 78 // Cancels locking and reverts lock animation. 79 void CancelLockAnimation(); 80 81 // Returns true if we are within cancellable shutdown timeframe. 82 bool CanCancelShutdownAnimation(); 83 84 // Cancels shutting down and reverts shutdown animation. 85 void CancelShutdownAnimation(); 86 87 // Displays the shutdown animation and requests a system shutdown or system 88 // restart depending on the the state of the |RebootOnShutdown| device policy. 89 void RequestShutdown(ShutdownReason reason); 90 91 // Called when ScreenLocker is ready to close, but not yet destroyed. 92 // Can be used to display "hiding" animations on unlock. 93 // |callback| will be called when all animations are done. 94 void OnLockScreenHide(base::OnceClosure callback); 95 96 // Sets up the callback that should be called once lock animation is finished. 97 // Callback is guaranteed to be called once and then discarded. 98 void SetLockScreenDisplayedCallback(base::OnceClosure callback); 99 100 // aura::WindowTreeHostObserver override: 101 void OnHostCloseRequested(aura::WindowTreeHost* host) override; 102 103 // SessionObserver overrides: 104 void OnChromeTerminating() override; 105 void OnLockStateChanged(bool locked) override; 106 set_animator_for_test(SessionStateAnimator * animator)107 void set_animator_for_test(SessionStateAnimator* animator) { 108 animator_.reset(animator); 109 } 110 111 private: 112 friend class LockStateControllerTestApi; 113 114 struct UnlockedStateProperties { 115 bool wallpaper_is_hidden; 116 }; 117 118 // Reverts the pre-lock animation, reports the error. 119 void OnLockFailTimeout(); 120 121 // Starts timer for undoable shutdown animation. 122 void StartPreShutdownAnimationTimer(); 123 124 // Calls StartRealShutdownTimer(). 125 void OnPreShutdownAnimationTimeout(); 126 127 // Starts timer for final shutdown animation. 128 // If |with_animation_time| is true, it will also include time of "fade to 129 // white" shutdown animation. 130 void StartRealShutdownTimer(bool with_animation_time); 131 132 // Request that the machine be shut down. 133 void OnRealPowerTimeout(); 134 135 void PreLockAnimation(SessionStateAnimator::AnimationSpeed speed, 136 bool request_lock_on_completion); 137 void StartPostLockAnimation(); 138 // This method calls |callback| when animation completes. 139 void StartUnlockAnimationBeforeUIDestroyed(base::OnceClosure callback); 140 void StartUnlockAnimationAfterUIDestroyed(); 141 142 // These methods are called when corresponding animation completes. 143 void LockAnimationCancelled(); 144 void PreLockAnimationFinished(bool request_lock); 145 void PostLockAnimationFinished(); 146 void UnlockAnimationAfterUIDestroyedFinished(); 147 148 // Stores properties of UI that have to be temporarily modified while locking. 149 void StoreUnlockedProperties(); 150 void RestoreUnlockedProperties(); 151 152 // Fades in wallpaper layer with |speed| if it was hidden in unlocked state. 153 void AnimateWallpaperAppearanceIfNecessary( 154 SessionStateAnimator::AnimationSpeed speed, 155 SessionStateAnimator::AnimationSequence* animation_sequence); 156 157 // Fades out wallpaper layer with |speed| if it was hidden in unlocked state. 158 void AnimateWallpaperHidingIfNecessary( 159 SessionStateAnimator::AnimationSpeed speed, 160 SessionStateAnimator::AnimationSequence* animation_sequence); 161 162 // Notifies observers. 163 void OnLockStateEvent(LockStateObserver::EventType event); 164 165 std::unique_ptr<SessionStateAnimator> animator_; 166 167 // Current lock status. 168 bool system_is_locked_ = false; 169 170 // Are we in the process of shutting the machine down? 171 bool shutting_down_ = false; 172 173 // The reason (e.g. user action) for a pending shutdown. 174 base::Optional<ShutdownReason> shutdown_reason_; 175 176 // Indicates whether controller should proceed to (cancellable) shutdown after 177 // locking. 178 bool shutdown_after_lock_ = false; 179 180 // Indicates that controller displays lock animation. 181 bool animating_lock_ = false; 182 183 // Indicates whether post lock animation should be immediate. 184 bool post_lock_immediate_animation_ = false; 185 186 std::unique_ptr<UnlockedStateProperties> unlocked_properties_; 187 188 // How long has it been since the request to lock the screen? 189 std::unique_ptr<base::ElapsedTimer> lock_duration_timer_; 190 191 // Controller used to trigger the actual shutdown. 192 ShutdownController* shutdown_controller_; 193 194 // Started when we request that the screen be locked. When it fires, we 195 // assume that our request got dropped. 196 base::OneShotTimer lock_fail_timer_; 197 198 // Started when we begin displaying the pre-shutdown animation. When it 199 // fires, we start the shutdown animation and get ready to request shutdown. 200 base::OneShotTimer pre_shutdown_timer_; 201 202 // Started when we display the shutdown animation. When it fires, we actually 203 // request shutdown. Gives the animation time to complete before Chrome, X, 204 // etc. are shut down. 205 base::OneShotTimer real_shutdown_timer_; 206 207 base::OnceClosure lock_screen_displayed_callback_; 208 209 ScopedSessionObserver scoped_session_observer_; 210 211 // The wallpaper blur before entering lock state. Used to restore the 212 // wallpaper blur after exiting lock state. 213 float saved_blur_; 214 215 base::ObserverList<LockStateObserver>::Unchecked observers_; 216 217 base::WeakPtrFactory<LockStateController> weak_ptr_factory_{this}; 218 219 DISALLOW_COPY_AND_ASSIGN(LockStateController); 220 }; 221 222 } // namespace ash 223 224 #endif // ASH_WM_LOCK_STATE_CONTROLLER_H_ 225