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.
5 #ifndef ASH_WM_LOCK_STATE_CONTROLLER_H_
6 #define ASH_WM_LOCK_STATE_CONTROLLER_H_
8 #include "ash/ash_export.h"
9 #include "ash/shell_observer.h"
10 #include "ash/wm/lock_state_observer.h"
11 #include "ash/wm/session_state_animator.h"
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "base/timer/elapsed_timer.h"
18 #include "base/timer/timer.h"
19 #include "ui/aura/window_tree_host_observer.h"
33 class LockStateControllerTest
;
34 class PowerButtonControllerTest
;
37 // Performs system-related functions on behalf of LockStateController.
38 class ASH_EXPORT LockStateControllerDelegate
{
40 LockStateControllerDelegate() {}
41 virtual ~LockStateControllerDelegate() {}
43 virtual void RequestLockScreen() = 0;
44 virtual void RequestShutdown() = 0;
47 DISALLOW_COPY_AND_ASSIGN(LockStateControllerDelegate
);
50 // Displays onscreen animations and locks or suspends the system in response to
51 // the power button being pressed or released.
54 // * StartLockAnimation (bool shutdown after lock) - starts lock that can be
56 // * StartLockAnimationAndLockImmediately (bool shutdown after lock) - starts
57 // uninterruptible lock animation.
58 // This leads to call of either StartImmediatePreLockAnimation or
59 // StartCancellablePreLockAnimation. Once they complete
60 // PreLockAnimationFinished is called, and system lock is requested.
61 // Once system locks and lock UI is created, OnLockStateChanged is called, and
62 // StartPostLockAnimation is called. In PostLockAnimationFinished two
63 // things happen : EVENT_LOCK_ANIMATION_FINISHED notification is sent (it
64 // triggers third part of animation within lock UI), and check for continuing to
68 // WebUI does first part of animation, and calls OnLockScreenHide(callback) that
69 // triggers StartUnlockAnimationBeforeUIDestroyed(callback). Once callback is
70 // called at the end of the animation, lock UI is deleted, system unlocks, and
71 // OnLockStateChanged is called. It leads to
72 // StartUnlockAnimationAfterUIDestroyed.
73 class ASH_EXPORT LockStateController
: public aura::WindowTreeHostObserver
,
74 public ShellObserver
{
76 // Amount of time that the power button needs to be held before we lock the
78 static const int kLockTimeoutMs
;
80 // Amount of time that the power button needs to be held before we shut down.
81 static const int kShutdownTimeoutMs
;
83 // Amount of time to wait for our lock requests to be honored before giving
85 static const int kLockFailTimeoutMs
;
87 // When the button has been held continuously from the unlocked state, amount
88 // of time that we wait after the screen locker window is shown before
89 // starting the pre-shutdown animation.
90 static const int kLockToShutdownTimeoutMs
;
92 // Additional time (beyond kFastCloseAnimMs) to wait after starting the
93 // fast-close shutdown animation before actually requesting shutdown, to give
94 // the animation time to finish.
95 static const int kShutdownRequestDelayMs
;
97 // Helper class used by tests to access internal state.
98 class ASH_EXPORT TestApi
{
100 explicit TestApi(LockStateController
* controller
);
104 bool lock_fail_timer_is_running() const {
105 return controller_
->lock_fail_timer_
.IsRunning();
107 bool lock_to_shutdown_timer_is_running() const {
108 return controller_
->lock_to_shutdown_timer_
.IsRunning();
110 bool shutdown_timer_is_running() const {
111 return controller_
->pre_shutdown_timer_
.IsRunning();
113 bool real_shutdown_timer_is_running() const {
114 return controller_
->real_shutdown_timer_
.IsRunning();
116 bool is_animating_lock() const {
117 return controller_
->animating_lock_
;
119 bool is_lock_cancellable() const {
120 return controller_
->CanCancelLockAnimation();
123 void trigger_lock_fail_timeout() {
124 controller_
->OnLockFailTimeout();
125 controller_
->lock_fail_timer_
.Stop();
127 void trigger_lock_to_shutdown_timeout() {
128 controller_
->OnLockToShutdownTimeout();
129 controller_
->lock_to_shutdown_timer_
.Stop();
131 void trigger_shutdown_timeout() {
132 controller_
->OnPreShutdownAnimationTimeout();
133 controller_
->pre_shutdown_timer_
.Stop();
135 void trigger_real_shutdown_timeout() {
136 controller_
->OnRealPowerTimeout();
137 controller_
->real_shutdown_timer_
.Stop();
141 LockStateController
* controller_
; // not owned
143 DISALLOW_COPY_AND_ASSIGN(TestApi
);
146 LockStateController();
147 ~LockStateController() override
;
149 void SetDelegate(scoped_ptr
<LockStateControllerDelegate
> delegate
);
151 void AddObserver(LockStateObserver
* observer
);
152 void RemoveObserver(LockStateObserver
* observer
);
153 bool HasObserver(const LockStateObserver
* observer
) const;
155 // Starts locking (with slow animation) that can be cancelled.
156 // After locking and |kLockToShutdownTimeoutMs| StartShutdownAnimation()
157 // will be called unless CancelShutdownAnimation() is called, if
158 // |shutdown_after_lock| is true.
159 void StartLockAnimation(bool shutdown_after_lock
);
161 // Starts shutting down (with slow animation) that can be cancelled.
162 void StartShutdownAnimation();
164 // Starts usual lock animation, but locks immediately. After locking and
165 // |kLockToShutdownTimeoutMs| StartShutdownAnimation() will be called unless
166 // CancelShutdownAnimation() is called, if |shutdown_after_lock| is true.
167 void StartLockAnimationAndLockImmediately(bool shutdown_after_lock
);
169 // Returns true if we have requested system to lock, but haven't received
171 bool LockRequested();
173 // Returns true if we are shutting down.
174 bool ShutdownRequested();
176 // Returns true if we are within cancellable lock timeframe.
177 bool CanCancelLockAnimation();
179 // Cancels locking and reverts lock animation.
180 void CancelLockAnimation();
182 // Returns true if we are within cancellable shutdown timeframe.
183 bool CanCancelShutdownAnimation();
185 // Cancels shutting down and reverts shutdown animation.
186 void CancelShutdownAnimation();
188 // Called when Chrome gets a request to display the lock screen.
189 void OnStartingLock();
191 // Displays the shutdown animation and requests a system shutdown or system
192 // restart depending on the the state of the |RebootOnShutdown| device policy.
193 void RequestShutdown();
195 // Called when ScreenLocker is ready to close, but not yet destroyed.
196 // Can be used to display "hiding" animations on unlock.
197 // |callback| will be called when all animations are done.
198 void OnLockScreenHide(base::Closure
& callback
);
200 // Sets up the callback that should be called once lock animation is finished.
201 // Callback is guaranteed to be called once and then discarded.
202 void SetLockScreenDisplayedCallback(const base::Closure
& callback
);
204 // aura::WindowTreeHostObserver override:
205 void OnHostCloseRequested(const aura::WindowTreeHost
* host
) override
;
207 // ShellObserver overrides:
208 void OnLoginStateChanged(user::LoginStatus status
) override
;
209 void OnAppTerminating() override
;
210 void OnLockStateChanged(bool locked
) override
;
212 void set_animator_for_test(SessionStateAnimator
* animator
) {
213 animator_
.reset(animator
);
217 friend class test::PowerButtonControllerTest
;
218 friend class test::LockStateControllerTest
;
220 struct UnlockedStateProperties
{
221 bool background_is_hidden
;
224 // Reverts the pre-lock animation, reports the error.
225 void OnLockFailTimeout();
227 // Starts timer for gap between lock and shutdown.
228 void StartLockToShutdownTimer();
230 // Calls StartShutdownAnimation().
231 void OnLockToShutdownTimeout();
233 // Starts timer for undoable shutdown animation.
234 void StartPreShutdownAnimationTimer();
236 // Calls StartRealShutdownTimer().
237 void OnPreShutdownAnimationTimeout();
239 // Starts timer for final shutdown animation.
240 // If |with_animation_time| is true, it will also include time of "fade to
241 // white" shutdown animation.
242 void StartRealShutdownTimer(bool with_animation_time
);
244 // Request that the machine be shut down.
245 void OnRealPowerTimeout();
247 // Starts shutdown animation that can be cancelled and starts pre-shutdown
249 void StartCancellableShutdownAnimation();
251 // If |request_lock_on_completion| is true, a lock request will be sent
252 // after the pre-lock animation completes. (The pre-lock animation is
253 // also displayed in response to already-in-progress lock requests; in
254 // these cases an additional lock request is undesirable.)
255 void StartImmediatePreLockAnimation(bool request_lock_on_completion
);
256 void StartCancellablePreLockAnimation();
257 void CancelPreLockAnimation();
258 void StartPostLockAnimation();
259 // This method calls |callback| when animation completes.
260 void StartUnlockAnimationBeforeUIDestroyed(base::Closure
&callback
);
261 void StartUnlockAnimationAfterUIDestroyed();
263 // These methods are called when corresponding animation completes.
264 void LockAnimationCancelled();
265 void PreLockAnimationFinished(bool request_lock
);
266 void PostLockAnimationFinished();
267 void UnlockAnimationAfterUIDestroyedFinished();
269 // Stores properties of UI that have to be temporarily modified while locking.
270 void StoreUnlockedProperties();
271 void RestoreUnlockedProperties();
273 // Fades in background layer with |speed| if it was hidden in unlocked state.
274 void AnimateBackgroundAppearanceIfNecessary(
275 ash::SessionStateAnimator::AnimationSpeed speed
,
276 SessionStateAnimator::AnimationSequence
* animation_sequence
);
278 // Fades out background layer with |speed| if it was hidden in unlocked state.
279 void AnimateBackgroundHidingIfNecessary(
280 ash::SessionStateAnimator::AnimationSpeed speed
,
281 SessionStateAnimator::AnimationSequence
* animation_sequence
);
283 scoped_ptr
<SessionStateAnimator
> animator_
;
285 scoped_ptr
<LockStateControllerDelegate
> delegate_
;
287 base::ObserverList
<LockStateObserver
> observers_
;
289 // The current login status, or original login status from before we locked.
290 user::LoginStatus login_status_
;
292 // Current lock status.
293 bool system_is_locked_
;
295 // Are we in the process of shutting the machine down?
298 // Indicates whether controller should proceed to (cancellable) shutdown after
300 bool shutdown_after_lock_
;
302 // Indicates that controller displays lock animation.
303 bool animating_lock_
;
305 // Indicates that lock animation can be undone.
306 bool can_cancel_lock_animation_
;
308 scoped_ptr
<UnlockedStateProperties
> unlocked_properties_
;
310 // How long has it been since the request to lock the screen?
311 scoped_ptr
<base::ElapsedTimer
> lock_duration_timer_
;
313 // Started when we request that the screen be locked. When it fires, we
314 // assume that our request got dropped.
315 base::OneShotTimer
<LockStateController
> lock_fail_timer_
;
317 // Started when the screen is locked while the power button is held. Adds a
318 // delay between the appearance of the lock screen and the beginning of the
319 // pre-shutdown animation.
320 base::OneShotTimer
<LockStateController
> lock_to_shutdown_timer_
;
322 // Started when we begin displaying the pre-shutdown animation. When it
323 // fires, we start the shutdown animation and get ready to request shutdown.
324 base::OneShotTimer
<LockStateController
> pre_shutdown_timer_
;
326 // Started when we display the shutdown animation. When it fires, we actually
327 // request shutdown. Gives the animation time to complete before Chrome, X,
328 // etc. are shut down.
329 base::OneShotTimer
<LockStateController
> real_shutdown_timer_
;
331 base::Closure lock_screen_displayed_callback_
;
333 base::WeakPtrFactory
<LockStateController
> weak_ptr_factory_
;
335 DISALLOW_COPY_AND_ASSIGN(LockStateController
);
340 #endif // ASH_WM_LOCK_STATE_CONTROLLER_H_