1 // Copyright (c) 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 CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "chrome/browser/chromeos/login/help_app_launcher.h"
19 #include "chrome/browser/chromeos/login/login_display.h"
20 #include "chrome/browser/chromeos/login/screens/error_screen_actor.h"
21 #include "chrome/browser/chromeos/login/user_manager.h"
22 #include "chrome/browser/chromeos/net/network_portal_detector.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/system_key_event_listener.h"
25 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler.h"
26 #include "chrome/browser/ui/webui/chromeos/login/gaia_screen_handler.h"
27 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
28 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
29 #include "content/public/browser/notification_observer.h"
30 #include "content/public/browser/notification_registrar.h"
31 #include "content/public/browser/web_ui.h"
32 #include "net/base/net_errors.h"
35 class DictionaryValue
;
41 class AuthenticatedUserEmailRetriever
;
42 class CaptivePortalWindowProxy
;
44 class LocallyManagedUserCreationScreenHandler
;
45 class NativeWindowDelegate
;
49 // Helper class to pass initial parameters to the login screen.
50 class LoginScreenContext
{
53 explicit LoginScreenContext(const base::ListValue
* args
);
55 void set_email(const std::string
& email
) { email_
= email
; }
56 const std::string
& email() const { return email_
; }
58 void set_oobe_ui(bool oobe_ui
) { oobe_ui_
= oobe_ui
; }
59 bool oobe_ui() const { return oobe_ui_
; }
68 // An interface for WebUILoginDisplay to call SigninScreenHandler.
69 class LoginDisplayWebUIHandler
{
71 virtual void ClearAndEnablePassword() = 0;
72 virtual void ClearUserPodPassword() = 0;
73 virtual void OnLoginSuccess(const std::string
& username
) = 0;
74 virtual void OnUserRemoved(const std::string
& username
) = 0;
75 virtual void OnUserImageChanged(const User
& user
) = 0;
76 virtual void OnPreferencesChanged() = 0;
77 virtual void ResetSigninScreenHandlerDelegate() = 0;
78 virtual void ShowBannerMessage(const std::string
& message
) = 0;
79 virtual void ShowUserPodButton(const std::string
& username
,
80 const std::string
& iconURL
,
81 const base::Closure
& click_callback
) = 0;
82 virtual void ShowError(int login_attempts
,
83 const std::string
& error_text
,
84 const std::string
& help_link_text
,
85 HelpAppLauncher::HelpTopic help_topic_id
) = 0;
86 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id
) = 0;
87 virtual void ShowGaiaPasswordChanged(const std::string
& username
) = 0;
88 virtual void ShowSigninUI(const std::string
& email
) = 0;
89 virtual void ShowPasswordChangedDialog(bool show_password_error
) = 0;
90 // Show sign-in screen for the given credentials.
91 virtual void ShowSigninScreenForCreds(const std::string
& username
,
92 const std::string
& password
) = 0;
94 virtual ~LoginDisplayWebUIHandler() {}
97 // An interface for SigninScreenHandler to call WebUILoginDisplay.
98 class SigninScreenHandlerDelegate
{
100 // Cancels current password changed flow.
101 virtual void CancelPasswordChangedFlow() = 0;
103 // Cancels user adding.
104 virtual void CancelUserAdding() = 0;
106 // Create a new Google account.
107 virtual void CreateAccount() = 0;
109 // Confirms sign up by provided credentials in |user_context|.
110 // Used for new user login via GAIA extension.
111 virtual void CompleteLogin(const UserContext
& user_context
) = 0;
113 // Sign in using username and password specified as a part of |user_context|.
114 // Used for both known and new users.
115 virtual void Login(const UserContext
& user_context
) = 0;
117 // Sign in into a retail mode session.
118 virtual void LoginAsRetailModeUser() = 0;
120 // Sign in into guest session.
121 virtual void LoginAsGuest() = 0;
123 // Sign in into the public account identified by |username|.
124 virtual void LoginAsPublicAccount(const std::string
& username
) = 0;
126 // Decrypt cryptohome using user provided |old_password|
127 // and migrate to new password.
128 virtual void MigrateUserData(const std::string
& old_password
) = 0;
130 // Load wallpaper for given |username|.
131 virtual void LoadWallpaper(const std::string
& username
) = 0;
133 // Loads the default sign-in wallpaper.
134 virtual void LoadSigninWallpaper() = 0;
136 // Notify the delegate when the sign-in UI is finished loading.
137 virtual void OnSigninScreenReady() = 0;
139 // Attempts to remove given user.
140 virtual void RemoveUser(const std::string
& username
) = 0;
142 // Ignore password change, remove existing cryptohome and
143 // force full sync of user data.
144 virtual void ResyncUserData() = 0;
146 // Shows Enterprise Enrollment screen.
147 virtual void ShowEnterpriseEnrollmentScreen() = 0;
149 // Shows Kiosk Enable screen.
150 virtual void ShowKioskEnableScreen() = 0;
152 // Shows Reset screen.
153 virtual void ShowResetScreen() = 0;
155 // Shows Reset screen.
156 virtual void ShowKioskAutolaunchScreen() = 0;
158 // Show wrong hwid screen.
159 virtual void ShowWrongHWIDScreen() = 0;
161 // Let the delegate know about the handler it is supposed to be using.
162 virtual void SetWebUIHandler(LoginDisplayWebUIHandler
* webui_handler
) = 0;
164 // Returns users list to be shown.
165 virtual const UserList
& GetUsers() const = 0;
167 // Whether login as guest is available.
168 virtual bool IsShowGuest() const = 0;
170 // Whether login as guest is available.
171 virtual bool IsShowUsers() const = 0;
173 // Whether new user pod is available.
174 virtual bool IsShowNewUser() const = 0;
176 // Returns true if sign in is in progress.
177 virtual bool IsSigninInProgress() const = 0;
179 // Whether user sign in has completed.
180 virtual bool IsUserSigninCompleted() const = 0;
182 // Sets the displayed email for the next login attempt. If it succeeds,
183 // user's displayed email value will be updated to |email|.
184 virtual void SetDisplayEmail(const std::string
& email
) = 0;
186 // Signs out if the screen is currently locked.
187 virtual void Signout() = 0;
189 // Login to kiosk mode for app with |app_id|.
190 virtual void LoginAsKioskApp(const std::string
& app_id
,
191 bool diagnostic_mode
) = 0;
194 virtual ~SigninScreenHandlerDelegate() {}
197 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
199 class SigninScreenHandler
200 : public BaseScreenHandler
,
201 public LoginDisplayWebUIHandler
,
202 public SystemKeyEventListener::CapsLockObserver
,
203 public content::NotificationObserver
,
204 public NetworkStateInformer::NetworkStateInformerObserver
{
207 const scoped_refptr
<NetworkStateInformer
>& network_state_informer
,
208 ErrorScreenActor
* error_screen_actor
,
209 CoreOobeActor
* core_oobe_actor
,
210 GaiaScreenHandler
* gaia_screen_handler
);
211 virtual ~SigninScreenHandler();
213 // Shows the sign in screen.
214 void Show(const LoginScreenContext
& context
);
216 // Shows the login spinner UI for retail mode logins.
217 void ShowRetailModeLoginSpinner();
219 // Sets delegate to be used by the handler. It is guaranteed that valid
220 // delegate is set before Show() method will be called.
221 void SetDelegate(SigninScreenHandlerDelegate
* delegate
);
223 void SetNativeWindowDelegate(NativeWindowDelegate
* native_window_delegate
);
225 // NetworkStateInformer::NetworkStateInformerObserver implementation:
226 virtual void OnNetworkReady() OVERRIDE
;
227 virtual void UpdateState(ErrorScreenActor::ErrorReason reason
) OVERRIDE
;
229 // Required Local State preferences.
230 static void RegisterPrefs(PrefRegistrySimple
* registry
);
232 void set_kiosk_enable_flow_aborted_callback_for_test(
233 const base::Closure
& callback
) {
234 kiosk_enable_flow_aborted_callback_for_test_
= callback
;
239 UI_STATE_UNKNOWN
= 0,
240 UI_STATE_GAIA_SIGNIN
,
241 UI_STATE_ACCOUNT_PICKER
,
244 friend class ReportDnsCacheClearedOnUIThread
;
245 friend class LocallyManagedUserCreationScreenHandler
;
249 // Updates current UI of the signin screen according to |ui_state|
250 // argument. Optionally it can pass screen initialization data via
251 // |params| argument.
252 void UpdateUIState(UIState ui_state
, base::DictionaryValue
* params
);
254 void UpdateStateInternal(ErrorScreenActor::ErrorReason reason
,
256 void SetupAndShowOfflineMessage(NetworkStateInformer::State state
,
257 ErrorScreenActor::ErrorReason reason
);
258 void HideOfflineMessage(NetworkStateInformer::State state
,
259 ErrorScreenActor::ErrorReason reason
);
260 void ReloadGaiaScreen();
262 // BaseScreenHandler implementation:
263 virtual void DeclareLocalizedValues(LocalizedValuesBuilder
* builder
) OVERRIDE
;
264 virtual void Initialize() OVERRIDE
;
265 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
267 // WebUIMessageHandler implementation:
268 virtual void RegisterMessages() OVERRIDE
;
270 // LoginDisplayWebUIHandler implementation:
271 virtual void ClearAndEnablePassword() OVERRIDE
;
272 virtual void ClearUserPodPassword() OVERRIDE
;
273 virtual void OnLoginSuccess(const std::string
& username
) OVERRIDE
;
274 virtual void OnUserRemoved(const std::string
& username
) OVERRIDE
;
275 virtual void OnUserImageChanged(const User
& user
) OVERRIDE
;
276 virtual void OnPreferencesChanged() OVERRIDE
;
277 virtual void ResetSigninScreenHandlerDelegate() OVERRIDE
;
278 virtual void ShowBannerMessage(const std::string
& message
) OVERRIDE
;
279 virtual void ShowUserPodButton(const std::string
& username
,
280 const std::string
& iconURL
,
281 const base::Closure
& click_callback
) OVERRIDE
;
282 virtual void ShowError(int login_attempts
,
283 const std::string
& error_text
,
284 const std::string
& help_link_text
,
285 HelpAppLauncher::HelpTopic help_topic_id
) OVERRIDE
;
286 virtual void ShowGaiaPasswordChanged(const std::string
& username
) OVERRIDE
;
287 virtual void ShowSigninUI(const std::string
& email
) OVERRIDE
;
288 virtual void ShowPasswordChangedDialog(bool show_password_error
) OVERRIDE
;
289 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id
) OVERRIDE
;
290 virtual void ShowSigninScreenForCreds(const std::string
& username
,
291 const std::string
& password
) OVERRIDE
;
293 // SystemKeyEventListener::CapsLockObserver overrides.
294 virtual void OnCapsLockChange(bool enabled
) OVERRIDE
;
296 // content::NotificationObserver implementation:
297 virtual void Observe(int type
,
298 const content::NotificationSource
& source
,
299 const content::NotificationDetails
& details
) OVERRIDE
;
301 // Shows signin screen after dns cache and cookie cleanup operations finish.
302 void ShowSigninScreenIfReady();
304 // Tells webui to load authentication extension. |force| is used to force the
305 // extension reloading, if it has already been loaded. |silent_load| is true
306 // for cases when extension should be loaded in the background and it
307 // shouldn't grab the focus. |offline| is true when offline version of the
308 // extension should be used.
309 void LoadAuthExtension(bool force
, bool silent_load
, bool offline
);
311 // Updates authentication extension. Called when device settings that affect
312 // sign-in (allow BWSI and allow whitelist) are changed.
313 void UserSettingsChanged();
314 void UpdateAddButtonStatus();
316 // Restore input focus to current user pod.
317 void RefocusCurrentPod();
319 // WebUI message handlers.
320 void HandleCompleteAuthentication(const std::string
& email
,
321 const std::string
& password
,
322 const std::string
& auth_code
);
323 void HandleCompleteLogin(const std::string
& typed_email
,
324 const std::string
& password
,
326 void HandleGetUsers();
327 void HandleUsingSAMLAPI();
328 void HandleScrapedPasswordCount(int password_count
);
329 void HandleScrapedPasswordVerificationFailed();
330 void HandleAuthenticateUser(const std::string
& username
,
331 const std::string
& password
);
332 void HandleLaunchDemoUser();
333 void HandleLaunchIncognito();
334 void HandleLaunchPublicAccount(const std::string
& username
);
335 void HandleOfflineLogin(const base::ListValue
* args
);
336 void HandleShutdownSystem();
337 void HandleLoadWallpaper(const std::string
& email
);
338 void HandleRebootSystem();
339 void HandleRemoveUser(const std::string
& email
);
340 void HandleShowAddUser(const base::ListValue
* args
);
341 void HandleToggleEnrollmentScreen();
342 void HandleToggleKioskEnableScreen();
343 void HandleToggleResetScreen();
344 void HandleToggleKioskAutolaunchScreen();
345 void HandleLaunchHelpApp(double help_topic_id
);
346 void HandleCreateAccount();
347 void HandleAccountPickerReady();
348 void HandleWallpaperReady();
349 void HandleLoginWebuiReady();
350 void HandleSignOutUser();
351 void HandleNetworkErrorShown();
352 void HandleOpenProxySettings();
353 void HandleLoginVisible(const std::string
& source
);
354 void HandleCancelPasswordChangedFlow();
355 void HandleCancelUserAdding();
356 void HandleMigrateUserData(const std::string
& password
);
357 void HandleResyncUserData();
358 void HandleLoginUIStateChanged(const std::string
& source
, bool new_value
);
359 void HandleUnlockOnLoginSuccess();
360 void HandleLoginScreenUpdate();
361 void HandleShowLoadingTimeoutError();
362 void HandleUpdateOfflineLogin(bool offline_login_active
);
363 void HandleShowLocallyManagedUserCreationScreen();
364 void HandleFocusPod(const std::string
& user_id
);
365 void HandleLaunchKioskApp(const std::string
& app_id
, bool diagnostic_mode
);
366 void HandleCustomButtonClicked(const std::string
& username
);
367 void HandleRetrieveAuthenticatedUserEmail(double attempt_token
);
369 // Fills |user_dict| with information about |user|.
370 static void FillUserDictionary(User
* user
,
372 bool is_signin_to_add
,
373 base::DictionaryValue
* user_dict
);
375 // Sends user list to account picker.
376 void SendUserList(bool animated
);
378 // Kick off cookie / local storage cleanup.
379 void StartClearingCookies(const base::Closure
& on_clear_callback
);
380 void OnCookiesCleared(base::Closure on_clear_callback
);
382 // Kick off DNS cache flushing.
383 void StartClearingDnsCache();
386 // Decides whether an auth extension should be pre-loaded. If it should,
388 void MaybePreloadAuthExtension();
391 // (i) log in is restricted to some user list,
392 // (ii) all users in the restricted list are present.
393 bool AllWhitelistedUsersPresent();
395 // Cancels password changed flow - switches back to login screen.
396 // Called as a callback after cookies are cleared.
397 void CancelPasswordChangedFlowInternal();
399 // Returns current visible screen.
400 OobeUI::Screen
GetCurrentScreen() const;
402 // Returns true if current visible screen is the Gaia sign-in page.
403 bool IsGaiaVisible() const;
405 // Returns true if current visible screen is the error screen over
406 // Gaia sign-in page.
407 bool IsGaiaHiddenByError() const;
409 // Returns true if current screen is the error screen over signin
411 bool IsSigninScreenHiddenByError() const;
413 // Returns true if guest signin is allowed.
414 bool IsGuestSigninAllowed() const;
416 // Returns true if offline login is allowed.
417 bool IsOfflineLoginAllowed() const;
419 // Attempts login for test.
420 void SubmitLoginFormForTest();
422 // Update current input method (namely keyboard layout) to LRU by this user.
423 void SetUserInputMethod(const std::string
& username
);
425 // Invoked when auto enrollment check is finished to decide whether to
426 // continue kiosk enable flow. Kiosk enable flow is resumed when
427 // |should_auto_enroll| is false.
428 void ContinueKioskEnableFlow(bool should_auto_enroll
);
430 // Shows signin screen for |email|.
431 void OnShowAddUser(const std::string
& email
);
433 // Updates the member variable and UMA histogram indicating whether the
434 // principals API was used during SAML login.
435 void SetSAMLPrincipalsAPIUsed(bool api_used
);
437 GaiaScreenHandler::FrameState
FrameState() const;
438 net::Error
FrameError() const;
440 // Current UI state of the signin screen.
443 // A delegate that glues this handler with backend LoginDisplay.
444 SigninScreenHandlerDelegate
* delegate_
;
446 // A delegate used to get gfx::NativeWindow.
447 NativeWindowDelegate
* native_window_delegate_
;
449 // Whether screen should be shown right after initialization.
452 // Keeps whether screen should be shown for OOBE.
455 // Is focus still stolen from Gaia page?
458 // Has Gaia page silent load been started for the current sign-in attempt?
459 bool gaia_silent_load_
;
461 // The active network at the moment when Gaia page was preloaded.
462 std::string gaia_silent_load_network_
;
464 // Is account picker being shown for the first time.
465 bool is_account_picker_showing_first_time_
;
467 // True if dns cache cleanup is done.
470 // True if DNS cache task is already running.
471 bool dns_clear_task_running_
;
473 // True if cookie jar cleanup is done.
474 bool cookies_cleared_
;
476 // Help application used for help dialogs.
477 scoped_refptr
<HelpAppLauncher
> help_app_
;
479 // Network state informer used to keep signin screen up.
480 scoped_refptr
<NetworkStateInformer
> network_state_informer_
;
482 // Email to pre-populate with.
484 // Emails of the users, whose passwords have recently been changed.
485 std::set
<std::string
> password_changed_for_
;
487 // If the user authenticated via SAML, this indicates whether the principals
489 bool using_saml_api_
;
492 std::string test_user_
;
493 std::string test_pass_
;
494 bool test_expects_complete_login_
;
496 base::WeakPtrFactory
<SigninScreenHandler
> weak_factory_
;
498 // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
500 bool preferences_changed_delayed_
;
502 ErrorScreenActor
* error_screen_actor_
;
503 CoreOobeActor
* core_oobe_actor_
;
505 bool is_first_update_state_call_
;
506 bool offline_login_active_
;
507 NetworkStateInformer::State last_network_state_
;
509 base::CancelableClosure update_state_closure_
;
510 base::CancelableClosure connecting_closure_
;
512 content::NotificationRegistrar registrar_
;
514 // Whether there is an auth UI pending. This flag is set on receiving
515 // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
516 // NOTIFICATION_AUTH_CANCELLED.
517 bool has_pending_auth_ui_
;
519 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_new_user_subscription_
;
520 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_guest_subscription_
;
522 bool wait_for_auto_enrollment_check_
;
524 base::Closure kiosk_enable_flow_aborted_callback_for_test_
;
526 // Map of callbacks run when the custom button on a user pod is clicked.
527 std::map
<std::string
, base::Closure
> user_pod_button_callback_map_
;
530 // TODO (ygorshenin@): remove this dependency.
531 GaiaScreenHandler
* gaia_screen_handler_
;
533 // Helper that retrieves the authenticated user's e-mail address.
534 scoped_ptr
<AuthenticatedUserEmailRetriever
> email_retriever_
;
536 DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler
);
539 } // namespace chromeos
541 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_