OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include "ash/wm/session_state_controller.h" | |
6 | |
7 #include "ash/ash_switches.h" | |
8 #include "ash/shell.h" | |
9 #include "ash/shell_delegate.h" | |
10 #include "ash/shell_window_ids.h" | |
11 #include "ash/wm/session_state_animator.h" | |
12 #include "base/command_line.h" | |
13 #include "ui/aura/root_window.h" | |
14 #include "ui/aura/shared/compound_event_filter.h" | |
15 | |
16 #if defined(OS_CHROMEOS) | |
17 #include "base/chromeos/chromeos_version.h" | |
18 #endif | |
19 | |
20 namespace ash { | |
21 | |
22 const int SessionStateController::kLockTimeoutMs = 400; | |
23 const int SessionStateController::kShutdownTimeoutMs = 400; | |
24 const int SessionStateController::kLockFailTimeoutMs = 4000; | |
25 const int SessionStateController::kLockToShutdownTimeoutMs = 150; | |
26 const int SessionStateController::kSlowCloseAnimMs = 400; | |
27 const int SessionStateController::kUndoSlowCloseAnimMs = 100; | |
28 const int SessionStateController::kFastCloseAnimMs = 150; | |
29 const int SessionStateController::kShutdownRequestDelayMs = 50; | |
30 | |
31 SessionStateController::TestApi::TestApi(SessionStateController* controller) | |
32 : controller_(controller) { | |
33 } | |
34 | |
35 SessionStateController::TestApi::~TestApi() { | |
36 } | |
37 | |
38 SessionStateController::SessionStateController() | |
39 : login_status_(user::LOGGED_IN_NONE), | |
40 unlocked_login_status_(user::LOGGED_IN_NONE), | |
41 shutting_down_(false), | |
42 shutdown_after_lock_(false), | |
43 animator_(new internal::SessionStateAnimator()) { | |
44 Shell::GetPrimaryRootWindow()->AddRootWindowObserver(this); | |
45 } | |
46 | |
47 SessionStateController::~SessionStateController() { | |
48 Shell::GetPrimaryRootWindow()->RemoveRootWindowObserver(this); | |
49 } | |
50 | |
51 void SessionStateController::SetDelegate( | |
52 SessionStateControllerDelegate* delegate) { | |
53 delegate_.reset(delegate); | |
54 } | |
55 | |
56 void SessionStateController::OnLoginStateChanged(user::LoginStatus status) { | |
57 login_status_ = status; | |
58 unlocked_login_status_ = user::LOGGED_IN_NONE; | |
59 } | |
60 | |
61 void SessionStateController::OnAppTerminating() { | |
62 // If we hear that Chrome is exiting but didn't request it ourselves, all we | |
63 // can really hope for is that we'll have time to clear the screen. | |
64 if (!shutting_down_) { | |
65 shutting_down_ = true; | |
66 Shell* shell = ash::Shell::GetInstance(); | |
67 shell->env_filter()->set_cursor_hidden_by_filter(false); | |
68 shell->cursor_manager()->ShowCursor(false); | |
69 animator_->ShowBlackLayer(); | |
70 animator_->StartAnimation( | |
71 internal::SessionStateAnimator::kAllContainersMask, | |
72 internal::SessionStateAnimator::ANIMATION_HIDE); | |
73 } | |
74 } | |
75 | |
76 void SessionStateController::OnLockStateChanged(bool locked) { | |
77 if (shutting_down_ || (IsLocked()) == locked) | |
78 return; | |
79 | |
80 if (!locked && login_status_ == user::LOGGED_IN_LOCKED) { | |
81 login_status_ = unlocked_login_status_; | |
82 unlocked_login_status_ = user::LOGGED_IN_NONE; | |
83 } else { | |
84 unlocked_login_status_ = login_status_; | |
85 login_status_ = user::LOGGED_IN_LOCKED; | |
86 } | |
87 | |
88 if (locked) { | |
89 animator_->StartAnimation( | |
90 internal::SessionStateAnimator::LOCK_SCREEN_CONTAINERS, | |
91 internal::SessionStateAnimator::ANIMATION_FADE_IN); | |
92 lock_timer_.Stop(); | |
93 lock_fail_timer_.Stop(); | |
94 | |
95 if (shutdown_after_lock_) { | |
96 shutdown_after_lock_ = false; | |
97 StartLockToShutdownTimer(); | |
98 } | |
99 } else { | |
100 animator_->StartAnimation( | |
101 internal::SessionStateAnimator::DESKTOP_BACKGROUND | | |
102 internal::SessionStateAnimator::LAUNCHER | | |
103 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
104 internal::SessionStateAnimator::ANIMATION_RESTORE); | |
105 animator_->DropBlackLayer(); | |
106 } | |
107 } | |
108 | |
109 void SessionStateController::OnStartingLock() { | |
110 if (shutting_down_ || login_status_ == user::LOGGED_IN_LOCKED) | |
111 return; | |
112 | |
113 // Ensure that the black layer is visible -- if the screen was locked via | |
114 // the wrench menu, we won't have already shown the black background | |
115 // as part of the slow-close animation. | |
116 animator_->ShowBlackLayer(); | |
117 | |
118 animator_->StartAnimation( | |
119 internal::SessionStateAnimator::LAUNCHER, | |
120 internal::SessionStateAnimator::ANIMATION_HIDE); | |
121 | |
122 animator_->StartAnimation( | |
123 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
124 internal::SessionStateAnimator::ANIMATION_FAST_CLOSE); | |
125 | |
126 // Hide the screen locker containers so we can make them fade in later. | |
127 animator_->StartAnimation( | |
128 internal::SessionStateAnimator::LOCK_SCREEN_CONTAINERS, | |
129 internal::SessionStateAnimator::ANIMATION_HIDE); | |
130 } | |
131 | |
132 void SessionStateController::StartLockAnimationAndLockImmediately() { | |
133 animator_->ShowBlackLayer(); | |
134 animator_->StartAnimation( | |
135 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
136 internal::SessionStateAnimator::ANIMATION_SLOW_CLOSE); | |
137 OnLockTimeout(); | |
138 } | |
139 | |
140 void SessionStateController::StartLockAnimation() { | |
141 shutdown_after_lock_ = true; | |
142 | |
143 animator_->ShowBlackLayer(); | |
144 animator_->StartAnimation( | |
145 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
146 internal::SessionStateAnimator::ANIMATION_SLOW_CLOSE); | |
147 StartLockTimer(); | |
148 } | |
149 | |
150 void SessionStateController::StartShutdownAnimation() { | |
151 animator_->ShowBlackLayer(); | |
152 animator_->StartAnimation( | |
153 internal::SessionStateAnimator::kAllContainersMask, | |
154 internal::SessionStateAnimator::ANIMATION_SLOW_CLOSE); | |
155 | |
156 StartPreShutdownAnimationTimer(); | |
157 } | |
158 | |
159 bool SessionStateController::IsEligibleForLock() { | |
160 return IsLoggedInAsNonGuest() && !IsLocked() && !LockRequested(); | |
161 } | |
162 | |
163 bool SessionStateController::IsLocked() { | |
164 return login_status_ == user::LOGGED_IN_LOCKED; | |
165 } | |
166 | |
167 bool SessionStateController::LockRequested() { | |
168 return lock_fail_timer_.IsRunning(); | |
169 } | |
170 | |
171 bool SessionStateController::ShutdownRequested() { | |
172 return shutting_down_; | |
173 } | |
174 | |
175 bool SessionStateController::CanCancelLockAnimation() { | |
176 return lock_timer_.IsRunning(); | |
177 } | |
178 | |
179 void SessionStateController::CancelLockAnimation() { | |
180 if (!CanCancelLockAnimation()) | |
181 return; | |
182 shutdown_after_lock_ = false; | |
183 animator_->StartAnimation( | |
184 internal::SessionStateAnimator::kAllContainersMask, | |
185 internal::SessionStateAnimator::ANIMATION_UNDO_SLOW_CLOSE); | |
186 animator_->ScheduleDropBlackLayer(); | |
187 lock_timer_.Stop(); | |
188 } | |
189 | |
190 void SessionStateController::CancelLockWithOtherAnimation() { | |
191 if (!CanCancelLockAnimation()) | |
192 return; | |
193 shutdown_after_lock_ = false; | |
194 animator_->StartAnimation( | |
195 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
196 internal::SessionStateAnimator::ANIMATION_UNDO_SLOW_CLOSE); | |
197 animator_->ScheduleDropBlackLayer(); | |
198 lock_timer_.Stop(); | |
199 } | |
200 | |
201 bool SessionStateController::CanCancelShutdownAnimation() { | |
202 return pre_shutdown_timer_.IsRunning() || | |
203 lock_to_shutdown_timer_.IsRunning(); | |
204 } | |
205 | |
206 void SessionStateController::CancelShutdownAnimation() { | |
207 if (!CanCancelShutdownAnimation()) | |
208 return; | |
209 if (lock_to_shutdown_timer_.IsRunning()) { | |
210 lock_to_shutdown_timer_.Stop(); | |
211 return; | |
212 } | |
213 | |
214 if (login_status_ == user::LOGGED_IN_LOCKED) { | |
215 // If we've already started shutdown transition at lock screen | |
216 // desktop background needs to be restored immediately. | |
217 animator_->StartAnimation( | |
218 internal::SessionStateAnimator::DESKTOP_BACKGROUND, | |
219 internal::SessionStateAnimator::ANIMATION_RESTORE); | |
220 animator_->StartAnimation( | |
221 internal::SessionStateAnimator::kAllLockScreenContainersMask, | |
222 internal::SessionStateAnimator::ANIMATION_UNDO_SLOW_CLOSE); | |
223 animator_->ScheduleDropBlackLayer(); | |
224 } else { | |
225 animator_->StartAnimation( | |
226 internal::SessionStateAnimator::kAllContainersMask, | |
227 internal::SessionStateAnimator::ANIMATION_UNDO_SLOW_CLOSE); | |
228 animator_->ScheduleDropBlackLayer(); | |
229 } | |
230 pre_shutdown_timer_.Stop(); | |
231 } | |
232 | |
233 void SessionStateController::RequestShutdown() { | |
234 if (!shutting_down_) | |
235 animator_->ShowBlackLayer(); | |
236 RequestShutdownImpl(); | |
237 } | |
238 | |
239 void SessionStateController::RequestShutdownImpl() { | |
240 DCHECK(!shutting_down_); | |
241 shutting_down_ = true; | |
242 | |
243 Shell* shell = ash::Shell::GetInstance(); | |
244 shell->env_filter()->set_cursor_hidden_by_filter(false); | |
245 shell->cursor_manager()->ShowCursor(false); | |
246 | |
247 animator_->ShowBlackLayer(); | |
248 if (login_status_ != user::LOGGED_IN_NONE) { | |
249 // Hide the other containers before starting the animation. | |
250 // ANIMATION_FAST_CLOSE will make the screen locker windows partially | |
251 // transparent, and we don't want the other windows to show through. | |
252 animator_->StartAnimation( | |
253 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS | | |
254 internal::SessionStateAnimator::LAUNCHER, | |
255 internal::SessionStateAnimator::ANIMATION_HIDE); | |
256 animator_->StartAnimation( | |
257 internal::SessionStateAnimator::kAllLockScreenContainersMask, | |
258 internal::SessionStateAnimator::ANIMATION_FAST_CLOSE); | |
259 } else { | |
260 animator_->StartAnimation( | |
261 internal::SessionStateAnimator::kAllContainersMask, | |
262 internal::SessionStateAnimator::ANIMATION_FAST_CLOSE); | |
263 } | |
264 StartRealShutdownTimer(); | |
265 } | |
266 | |
267 void SessionStateController::OnRootWindowHostCloseRequested( | |
268 const aura::RootWindow*) { | |
269 if(Shell::GetInstance() && Shell::GetInstance()->delegate()) | |
270 Shell::GetInstance()->delegate()->Exit(); | |
271 } | |
272 | |
273 bool SessionStateController::IsLoggedInAsNonGuest() const { | |
274 // TODO(mukai): think about kiosk mode. | |
275 return (login_status_ != user::LOGGED_IN_NONE) && | |
276 (login_status_ != user::LOGGED_IN_GUEST); | |
277 } | |
278 | |
279 void SessionStateController::StartLockTimer() { | |
280 lock_timer_.Stop(); | |
281 lock_timer_.Start(FROM_HERE, | |
282 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs), | |
283 this, &SessionStateController::OnLockTimeout); | |
284 } | |
285 | |
286 void SessionStateController::OnLockTimeout() { | |
287 delegate_->RequestLockScreen(); | |
288 lock_fail_timer_.Start( | |
289 FROM_HERE, | |
290 base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs), | |
291 this, &SessionStateController::OnLockFailTimeout); | |
292 } | |
293 | |
294 void SessionStateController::OnLockFailTimeout() { | |
295 DCHECK_NE(login_status_, user::LOGGED_IN_LOCKED); | |
296 // Undo lock animation. | |
297 animator_->StartAnimation( | |
298 internal::SessionStateAnimator::LAUNCHER | | |
299 internal::SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, | |
300 internal::SessionStateAnimator::ANIMATION_RESTORE); | |
301 animator_->DropBlackLayer(); | |
302 } | |
303 | |
304 void SessionStateController::StartLockToShutdownTimer() { | |
305 shutdown_after_lock_ = false; | |
306 lock_to_shutdown_timer_.Stop(); | |
307 lock_to_shutdown_timer_.Start( | |
308 FROM_HERE, | |
309 base::TimeDelta::FromMilliseconds(kLockToShutdownTimeoutMs), | |
310 this, &SessionStateController::OnLockToShutdownTimeout); | |
311 } | |
312 | |
313 | |
314 void SessionStateController::OnLockToShutdownTimeout() { | |
315 DCHECK_EQ(login_status_, user::LOGGED_IN_LOCKED); | |
316 StartShutdownAnimation(); | |
317 } | |
318 | |
319 void SessionStateController::StartPreShutdownAnimationTimer() { | |
320 pre_shutdown_timer_.Stop(); | |
321 pre_shutdown_timer_.Start( | |
322 FROM_HERE, | |
323 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs), | |
324 this, &SessionStateController::OnPreShutdownAnimationTimeout); | |
325 } | |
326 | |
327 void SessionStateController::OnPreShutdownAnimationTimeout() { | |
328 if (!shutting_down_) | |
329 RequestShutdownImpl(); | |
330 } | |
331 | |
332 void SessionStateController::StartRealShutdownTimer() { | |
333 real_shutdown_timer_.Start( | |
334 FROM_HERE, | |
335 base::TimeDelta::FromMilliseconds(kFastCloseAnimMs + | |
336 kShutdownRequestDelayMs), | |
337 this, &SessionStateController::OnRealShutdownTimeout); | |
338 } | |
339 | |
340 void SessionStateController::OnRealShutdownTimeout() { | |
341 DCHECK(shutting_down_); | |
342 #if defined(OS_CHROMEOS) | |
343 if (!base::chromeos::IsRunningOnChromeOS()) { | |
344 ShellDelegate* delegate = Shell::GetInstance()->delegate(); | |
345 if (delegate) { | |
346 delegate->Exit(); | |
347 return; | |
348 } | |
349 } | |
350 #endif | |
351 delegate_->RequestShutdown(); | |
352 } | |
353 | |
354 } // namespace ash | |
OLD | NEW |