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/timer.h"
18 #include "ui/aura/window_tree_host_observer.h"
32 class LockStateControllerTest
;
33 class PowerButtonControllerTest
;
36 // Performs system-related functions on behalf of LockStateController.
37 class ASH_EXPORT LockStateControllerDelegate
{
39 LockStateControllerDelegate() {}
40 virtual ~LockStateControllerDelegate() {}
42 virtual void RequestLockScreen() = 0;
43 virtual void RequestShutdown() = 0;
46 DISALLOW_COPY_AND_ASSIGN(LockStateControllerDelegate
);
49 // Displays onscreen animations and locks or suspends the system in response to
50 // the power button being pressed or released.
53 // * StartLockAnimation (bool shutdown after lock) - starts lock that can be
55 // * StartLockAnimationAndLockImmediately (bool shutdown after lock) - starts
56 // uninterruptible lock animation.
57 // This leads to call of either StartImmediatePreLockAnimation or
58 // StartCancellablePreLockAnimation. Once they complete
59 // PreLockAnimationFinished is called, and system lock is requested.
60 // Once system locks and lock UI is created, OnLockStateChanged is called, and
61 // StartPostLockAnimation is called. In PostLockAnimationFinished two
62 // things happen : EVENT_LOCK_ANIMATION_FINISHED notification is sent (it
63 // triggers third part of animation within lock UI), and check for continuing to
67 // WebUI does first part of animation, and calls OnLockScreenHide(callback) that
68 // triggers StartUnlockAnimationBeforeUIDestroyed(callback). Once callback is
69 // called at the end of the animation, lock UI is deleted, system unlocks, and
70 // OnLockStateChanged is called. It leads to
71 // StartUnlockAnimationAfterUIDestroyed.
72 class ASH_EXPORT LockStateController
: public aura::WindowTreeHostObserver
,
73 public ShellObserver
{
75 // Amount of time that the power button needs to be held before we lock the
77 static const int kLockTimeoutMs
;
79 // Amount of time that the power button needs to be held before we shut down.
80 static const int kShutdownTimeoutMs
;
82 // Amount of time to wait for our lock requests to be honored before giving
84 static const int kLockFailTimeoutMs
;
86 // When the button has been held continuously from the unlocked state, amount
87 // of time that we wait after the screen locker window is shown before
88 // starting the pre-shutdown animation.
89 static const int kLockToShutdownTimeoutMs
;
91 // Additional time (beyond kFastCloseAnimMs) to wait after starting the
92 // fast-close shutdown animation before actually requesting shutdown, to give
93 // the animation time to finish.
94 static const int kShutdownRequestDelayMs
;
96 // Helper class used by tests to access internal state.
97 class ASH_EXPORT TestApi
{
99 explicit TestApi(LockStateController
* controller
);
103 bool lock_fail_timer_is_running() const {
104 return controller_
->lock_fail_timer_
.IsRunning();
106 bool lock_to_shutdown_timer_is_running() const {
107 return controller_
->lock_to_shutdown_timer_
.IsRunning();
109 bool shutdown_timer_is_running() const {
110 return controller_
->pre_shutdown_timer_
.IsRunning();
112 bool real_shutdown_timer_is_running() const {
113 return controller_
->real_shutdown_timer_
.IsRunning();
115 bool is_animating_lock() const {
116 return controller_
->animating_lock_
;
118 bool is_lock_cancellable() const {
119 return controller_
->CanCancelLockAnimation();
122 void trigger_lock_fail_timeout() {
123 controller_
->OnLockFailTimeout();
124 controller_
->lock_fail_timer_
.Stop();
126 void trigger_lock_to_shutdown_timeout() {
127 controller_
->OnLockToShutdownTimeout();
128 controller_
->lock_to_shutdown_timer_
.Stop();
130 void trigger_shutdown_timeout() {
131 controller_
->OnPreShutdownAnimationTimeout();
132 controller_
->pre_shutdown_timer_
.Stop();
134 void trigger_real_shutdown_timeout() {
135 controller_
->OnRealPowerTimeout();
136 controller_
->real_shutdown_timer_
.Stop();
140 LockStateController
* controller_
; // not owned
142 DISALLOW_COPY_AND_ASSIGN(TestApi
);
145 LockStateController();
146 ~LockStateController() override
;
148 void SetDelegate(scoped_ptr
<LockStateControllerDelegate
> delegate
);
150 void AddObserver(LockStateObserver
* observer
);
151 void RemoveObserver(LockStateObserver
* observer
);
152 bool HasObserver(const LockStateObserver
* observer
) const;
154 // Starts locking (with slow animation) that can be cancelled.
155 // After locking and |kLockToShutdownTimeoutMs| StartShutdownAnimation()
156 // will be called unless CancelShutdownAnimation() is called, if
157 // |shutdown_after_lock| is true.
158 void StartLockAnimation(bool shutdown_after_lock
);
160 // Starts shutting down (with slow animation) that can be cancelled.
161 void StartShutdownAnimation();
163 // Starts usual lock animation, but locks immediately. After locking and
164 // |kLockToShutdownTimeoutMs| StartShutdownAnimation() will be called unless
165 // CancelShutdownAnimation() is called, if |shutdown_after_lock| is true.
166 void StartLockAnimationAndLockImmediately(bool shutdown_after_lock
);
168 // Returns true if we have requested system to lock, but haven't received
170 bool LockRequested();
172 // Returns true if we are shutting down.
173 bool ShutdownRequested();
175 // Returns true if we are within cancellable lock timeframe.
176 bool CanCancelLockAnimation();
178 // Cancels locking and reverts lock animation.
179 void CancelLockAnimation();
181 // Returns true if we are within cancellable shutdown timeframe.
182 bool CanCancelShutdownAnimation();
184 // Cancels shutting down and reverts shutdown animation.
185 void CancelShutdownAnimation();
187 // Called when Chrome gets a request to display the lock screen.
188 void OnStartingLock();
190 // Displays the shutdown animation and requests a system shutdown or system
191 // restart depending on the the state of the |RebootOnShutdown| device policy.
192 void RequestShutdown();
194 // Called when ScreenLocker is ready to close, but not yet destroyed.
195 // Can be used to display "hiding" animations on unlock.
196 // |callback| will be called when all animations are done.
197 void OnLockScreenHide(base::Closure
& callback
);
199 // Sets up the callback that should be called once lock animation is finished.
200 // Callback is guaranteed to be called once and then discarded.
201 void SetLockScreenDisplayedCallback(const base::Closure
& callback
);
203 // aura::WindowTreeHostObserver override:
204 void OnHostCloseRequested(const aura::WindowTreeHost
* host
) override
;
206 // ShellObserver overrides:
207 void OnLoginStateChanged(user::LoginStatus status
) override
;
208 void OnAppTerminating() override
;
209 void OnLockStateChanged(bool locked
) override
;
211 void set_animator_for_test(SessionStateAnimator
* animator
) {
212 animator_
.reset(animator
);
216 friend class test::PowerButtonControllerTest
;
217 friend class test::LockStateControllerTest
;
219 struct UnlockedStateProperties
{
220 bool background_is_hidden
;
223 // Reverts the pre-lock animation, reports the error.
224 void OnLockFailTimeout();
226 // Starts timer for gap between lock and shutdown.
227 void StartLockToShutdownTimer();
229 // Calls StartShutdownAnimation().
230 void OnLockToShutdownTimeout();
232 // Starts timer for undoable shutdown animation.
233 void StartPreShutdownAnimationTimer();
235 // Calls StartRealShutdownTimer().
236 void OnPreShutdownAnimationTimeout();
238 // Starts timer for final shutdown animation.
239 // If |with_animation_time| is true, it will also include time of "fade to
240 // white" shutdown animation.
241 void StartRealShutdownTimer(bool with_animation_time
);
243 // Request that the machine be shut down.
244 void OnRealPowerTimeout();
246 // Starts shutdown animation that can be cancelled and starts pre-shutdown
248 void StartCancellableShutdownAnimation();
250 // If |request_lock_on_completion| is true, a lock request will be sent
251 // after the pre-lock animation completes. (The pre-lock animation is
252 // also displayed in response to already-in-progress lock requests; in
253 // these cases an additional lock request is undesirable.)
254 void StartImmediatePreLockAnimation(bool request_lock_on_completion
);
255 void StartCancellablePreLockAnimation();
256 void CancelPreLockAnimation();
257 void StartPostLockAnimation();
258 // This method calls |callback| when animation completes.
259 void StartUnlockAnimationBeforeUIDestroyed(base::Closure
&callback
);
260 void StartUnlockAnimationAfterUIDestroyed();
262 // These methods are called when corresponding animation completes.
263 void LockAnimationCancelled();
264 void PreLockAnimationFinished(bool request_lock
);
265 void PostLockAnimationFinished();
266 void UnlockAnimationAfterUIDestroyedFinished();
268 // Stores properties of UI that have to be temporarily modified while locking.
269 void StoreUnlockedProperties();
270 void RestoreUnlockedProperties();
272 // Fades in background layer with |speed| if it was hidden in unlocked state.
273 void AnimateBackgroundAppearanceIfNecessary(
274 ash::SessionStateAnimator::AnimationSpeed speed
,
275 SessionStateAnimator::AnimationSequence
* animation_sequence
);
277 // Fades out background layer with |speed| if it was hidden in unlocked state.
278 void AnimateBackgroundHidingIfNecessary(
279 ash::SessionStateAnimator::AnimationSpeed speed
,
280 SessionStateAnimator::AnimationSequence
* animation_sequence
);
282 scoped_ptr
<SessionStateAnimator
> animator_
;
284 scoped_ptr
<LockStateControllerDelegate
> delegate_
;
286 ObserverList
<LockStateObserver
> observers_
;
288 // The current login status, or original login status from before we locked.
289 user::LoginStatus login_status_
;
291 // Current lock status.
292 bool system_is_locked_
;
294 // Are we in the process of shutting the machine down?
297 // Indicates whether controller should proceed to (cancellable) shutdown after
299 bool shutdown_after_lock_
;
301 // Indicates that controller displays lock animation.
302 bool animating_lock_
;
304 // Indicates that lock animation can be undone.
305 bool can_cancel_lock_animation_
;
307 scoped_ptr
<UnlockedStateProperties
> unlocked_properties_
;
309 // Started when we request that the screen be locked. When it fires, we
310 // assume that our request got dropped.
311 base::OneShotTimer
<LockStateController
> lock_fail_timer_
;
313 // Started when the screen is locked while the power button is held. Adds a
314 // delay between the appearance of the lock screen and the beginning of the
315 // pre-shutdown animation.
316 base::OneShotTimer
<LockStateController
> lock_to_shutdown_timer_
;
318 // Started when we begin displaying the pre-shutdown animation. When it
319 // fires, we start the shutdown animation and get ready to request shutdown.
320 base::OneShotTimer
<LockStateController
> pre_shutdown_timer_
;
322 // Started when we display the shutdown animation. When it fires, we actually
323 // request shutdown. Gives the animation time to complete before Chrome, X,
324 // etc. are shut down.
325 base::OneShotTimer
<LockStateController
> real_shutdown_timer_
;
327 base::Closure lock_screen_displayed_callback_
;
329 base::WeakPtrFactory
<LockStateController
> weak_ptr_factory_
;
331 DISALLOW_COPY_AND_ASSIGN(LockStateController
);
336 #endif // ASH_WM_LOCK_STATE_CONTROLLER_H_