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/callback.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/chromeos/login/help_app_launcher.h"
17 #include "chrome/browser/chromeos/login/login_display.h"
18 #include "chrome/browser/chromeos/login/screens/error_screen_actor.h"
19 #include "chrome/browser/chromeos/login/user_manager.h"
20 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
21 #include "chrome/browser/chromeos/net/network_portal_detector.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chrome/browser/chromeos/system_key_event_listener.h"
24 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler.h"
25 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
26 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/web_ui.h"
30 #include "net/base/net_errors.h"
33 class DictionaryValue
;
39 class CaptivePortalWindowProxy
;
41 class LocallyManagedUserCreationScreenHandler
;
42 class NativeWindowDelegate
;
46 // An interface for WebUILoginDisplay to call SigninScreenHandler.
47 class LoginDisplayWebUIHandler
{
49 virtual void ClearAndEnablePassword() = 0;
50 virtual void ClearUserPodPassword() = 0;
51 virtual void OnLoginSuccess(const std::string
& username
) = 0;
52 virtual void OnUserRemoved(const std::string
& username
) = 0;
53 virtual void OnUserImageChanged(const User
& user
) = 0;
54 virtual void OnPreferencesChanged() = 0;
55 virtual void ResetSigninScreenHandlerDelegate() = 0;
56 virtual void ShowError(int login_attempts
,
57 const std::string
& error_text
,
58 const std::string
& help_link_text
,
59 HelpAppLauncher::HelpTopic help_topic_id
) = 0;
60 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id
) = 0;
61 virtual void ShowGaiaPasswordChanged(const std::string
& username
) = 0;
62 virtual void ShowSigninUI(const std::string
& email
) = 0;
63 virtual void ShowPasswordChangedDialog(bool show_password_error
) = 0;
64 // Show sign-in screen for the given credentials.
65 virtual void ShowSigninScreenForCreds(const std::string
& username
,
66 const std::string
& password
) = 0;
68 virtual ~LoginDisplayWebUIHandler() {}
71 // An interface for SigninScreenHandler to call WebUILoginDisplay.
72 class SigninScreenHandlerDelegate
{
74 // Cancels current password changed flow.
75 virtual void CancelPasswordChangedFlow() = 0;
77 // Cancels user adding.
78 virtual void CancelUserAdding() = 0;
80 // Create a new Google account.
81 virtual void CreateAccount() = 0;
83 // Confirms sign up by provided credentials in |user_context|.
84 // Used for new user login via GAIA extension.
85 virtual void CompleteLogin(const UserContext
& user_context
) = 0;
87 // Sign in using username and password specified as a part of |user_context|.
88 // Used for both known and new users.
89 virtual void Login(const UserContext
& user_context
) = 0;
91 // Sign in into a retail mode session.
92 virtual void LoginAsRetailModeUser() = 0;
94 // Sign in into guest session.
95 virtual void LoginAsGuest() = 0;
97 // Sign in into the public account identified by |username|.
98 virtual void LoginAsPublicAccount(const std::string
& username
) = 0;
100 // Decrypt cryptohome using user provided |old_password|
101 // and migrate to new password.
102 virtual void MigrateUserData(const std::string
& old_password
) = 0;
104 // Load wallpaper for given |username|.
105 virtual void LoadWallpaper(const std::string
& username
) = 0;
107 // Loads the default sign-in wallpaper.
108 virtual void LoadSigninWallpaper() = 0;
110 // Notify the delegate when the sign-in UI is finished loading.
111 virtual void OnSigninScreenReady() = 0;
113 // Attempts to remove given user.
114 virtual void RemoveUser(const std::string
& username
) = 0;
116 // Ignore password change, remove existing cryptohome and
117 // force full sync of user data.
118 virtual void ResyncUserData() = 0;
120 // Shows Enterprise Enrollment screen.
121 virtual void ShowEnterpriseEnrollmentScreen() = 0;
123 // Shows Kiosk Enable screen.
124 virtual void ShowKioskEnableScreen() = 0;
126 // Shows Reset screen.
127 virtual void ShowResetScreen() = 0;
129 // Shows Reset screen.
130 virtual void ShowKioskAutolaunchScreen() = 0;
132 // Show wrong hwid screen.
133 virtual void ShowWrongHWIDScreen() = 0;
135 // Let the delegate know about the handler it is supposed to be using.
136 virtual void SetWebUIHandler(LoginDisplayWebUIHandler
* webui_handler
) = 0;
138 // Returns users list to be shown.
139 virtual const UserList
& GetUsers() const = 0;
141 // Whether login as guest is available.
142 virtual bool IsShowGuest() const = 0;
144 // Whether login as guest is available.
145 virtual bool IsShowUsers() const = 0;
147 // Whether new user pod is available.
148 virtual bool IsShowNewUser() const = 0;
150 // Returns true if sign in is in progress.
151 virtual bool IsSigninInProgress() const = 0;
153 // Whether user sign in has completed.
154 virtual bool IsUserSigninCompleted() const = 0;
156 // Sets the displayed email for the next login attempt. If it succeeds,
157 // user's displayed email value will be updated to |email|.
158 virtual void SetDisplayEmail(const std::string
& email
) = 0;
160 // Signs out if the screen is currently locked.
161 virtual void Signout() = 0;
163 // Login to kiosk mode for app with |app_id|.
164 virtual void LoginAsKioskApp(const std::string
& app_id
) = 0;
167 virtual ~SigninScreenHandlerDelegate() {}
170 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
172 class SigninScreenHandler
173 : public BaseScreenHandler
,
174 public LoginDisplayWebUIHandler
,
175 public SystemKeyEventListener::CapsLockObserver
,
176 public content::NotificationObserver
,
177 public NetworkStateInformer::NetworkStateInformerObserver
,
178 public WallpaperManager::Observer
{
181 const scoped_refptr
<NetworkStateInformer
>& network_state_informer
,
182 ErrorScreenActor
* error_screen_actor
,
183 CoreOobeActor
* core_oobe_actor
);
184 virtual ~SigninScreenHandler();
186 // Shows the sign in screen. |oobe_ui| indicates whether the signin
187 // screen is for OOBE or usual sign-in flow.
188 void Show(bool oobe_ui
);
190 // Shows the login spinner UI for retail mode logins.
191 void ShowRetailModeLoginSpinner();
193 // Sets delegate to be used by the handler. It is guaranteed that valid
194 // delegate is set before Show() method will be called.
195 void SetDelegate(SigninScreenHandlerDelegate
* delegate
);
197 void SetNativeWindowDelegate(NativeWindowDelegate
* native_window_delegate
);
199 // NetworkStateInformer::NetworkStateInformerObserver implementation:
200 virtual void OnNetworkReady() OVERRIDE
;
201 virtual void UpdateState(ErrorScreenActor::ErrorReason reason
) OVERRIDE
;
203 // Required Local State preferences.
204 static void RegisterPrefs(PrefRegistrySimple
* registry
);
206 // From WallpaperManager::Observer
207 virtual void OnWallpaperAnimationFinished(const std::string
& email
) OVERRIDE
;
209 void set_kiosk_enable_flow_aborted_callback_for_test(
210 const base::Closure
& callback
) {
211 kiosk_enable_flow_aborted_callback_for_test_
= callback
;
216 UI_STATE_UNKNOWN
= 0,
217 UI_STATE_GAIA_SIGNIN
,
218 UI_STATE_ACCOUNT_PICKER
,
222 FRAME_STATE_UNKNOWN
= 0,
228 typedef base::hash_set
<std::string
> WebUIObservers
;
230 friend class ReportDnsCacheClearedOnUIThread
;
231 friend class LocallyManagedUserCreationScreenHandler
;
233 // Updates current UI of the signin screen according to |ui_state|
234 // argument. Optionally it can pass screen initialization data via
235 // |params| argument.
236 void UpdateUIState(UIState ui_state
, DictionaryValue
* params
);
238 void UpdateStateInternal(ErrorScreenActor::ErrorReason reason
,
240 void SetupAndShowOfflineMessage(NetworkStateInformer::State state
,
241 ErrorScreenActor::ErrorReason reason
);
242 void HideOfflineMessage(NetworkStateInformer::State state
,
243 ErrorScreenActor::ErrorReason reason
);
244 void ReloadGaiaScreen();
246 // BaseScreenHandler implementation:
247 virtual void DeclareLocalizedValues(LocalizedValuesBuilder
* builder
) OVERRIDE
;
248 virtual void Initialize() OVERRIDE
;
249 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
251 // WebUIMessageHandler implementation:
252 virtual void RegisterMessages() OVERRIDE
;
254 // BaseLoginUIHandler implementation:
255 virtual void ClearAndEnablePassword() OVERRIDE
;
256 virtual void ClearUserPodPassword() OVERRIDE
;
257 virtual void OnLoginSuccess(const std::string
& username
) OVERRIDE
;
258 virtual void OnUserRemoved(const std::string
& username
) OVERRIDE
;
259 virtual void OnUserImageChanged(const User
& user
) OVERRIDE
;
260 virtual void OnPreferencesChanged() OVERRIDE
;
261 virtual void ResetSigninScreenHandlerDelegate() OVERRIDE
;
262 virtual void ShowError(int login_attempts
,
263 const std::string
& error_text
,
264 const std::string
& help_link_text
,
265 HelpAppLauncher::HelpTopic help_topic_id
) OVERRIDE
;
266 virtual void ShowGaiaPasswordChanged(const std::string
& username
) OVERRIDE
;
267 virtual void ShowSigninUI(const std::string
& email
) OVERRIDE
;
268 virtual void ShowPasswordChangedDialog(bool show_password_error
) OVERRIDE
;
269 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id
) OVERRIDE
;
270 virtual void ShowSigninScreenForCreds(const std::string
& username
,
271 const std::string
& password
) OVERRIDE
;
273 // SystemKeyEventListener::CapsLockObserver overrides.
274 virtual void OnCapsLockChange(bool enabled
) OVERRIDE
;
276 // content::NotificationObserver implementation:
277 virtual void Observe(int type
,
278 const content::NotificationSource
& source
,
279 const content::NotificationDetails
& details
) OVERRIDE
;
281 // Shows signin screen after dns cache and cookie cleanup operations finish.
282 void ShowSigninScreenIfReady();
284 // Tells webui to load authentication extension. |force| is used to force the
285 // extension reloading, if it has already been loaded. |silent_load| is true
286 // for cases when extension should be loaded in the background and it
287 // shouldn't grab the focus. |offline| is true when offline version of the
288 // extension should be used.
289 void LoadAuthExtension(bool force
, bool silent_load
, bool offline
);
291 // Updates authentication extension. Called when device settings that affect
292 // sign-in (allow BWSI and allow whitelist) are changed.
293 void UserSettingsChanged();
294 void UpdateAuthExtension();
295 void UpdateAddButtonStatus();
297 // Fill |params| that are passed to JS..
298 void UpdateAuthParams(DictionaryValue
* params
);
300 // Restore input focus to current user pod.
301 void RefocusCurrentPod();
303 // WebUI message handlers.
304 void HandleCompleteAuthentication(const std::string
& email
,
305 const std::string
& password
,
306 const std::string
& auth_code
);
307 void HandleCompleteLogin(const std::string
& typed_email
,
308 const std::string
& password
);
309 void HandleGetUsers();
310 void HandleAuthenticateUser(const std::string
& username
,
311 const std::string
& password
);
312 void HandleLaunchDemoUser();
313 void HandleLaunchIncognito();
314 void HandleLaunchPublicAccount(const std::string
& username
);
315 void HandleOfflineLogin(const base::ListValue
* args
);
316 void HandleShutdownSystem();
317 void HandleLoadWallpaper(const std::string
& email
);
318 void HandleRebootSystem();
319 void HandleRemoveUser(const std::string
& email
);
320 void HandleShowAddUser(const base::ListValue
* args
);
321 void HandleToggleEnrollmentScreen();
322 void HandleToggleKioskEnableScreen();
323 void HandleToggleResetScreen();
324 void HandleToggleKioskAutolaunchScreen();
325 void HandleLaunchHelpApp(double help_topic_id
);
326 void HandleCreateAccount();
327 void HandleAccountPickerReady();
328 void HandleWallpaperReady();
329 void HandleLoginWebuiReady();
330 void HandleSignOutUser();
331 void HandleNetworkErrorShown();
332 void HandleOpenProxySettings();
333 void HandleLoginVisible(const std::string
& source
);
334 void HandleCancelPasswordChangedFlow();
335 void HandleCancelUserAdding();
336 void HandleMigrateUserData(const std::string
& password
);
337 void HandleResyncUserData();
338 void HandleLoginUIStateChanged(const std::string
& source
, bool new_value
);
339 void HandleUnlockOnLoginSuccess();
340 void HandleLoginScreenUpdate();
341 void HandleFrameLoadingCompleted(int status
);
342 void HandleShowLoadingTimeoutError();
343 void HandleUpdateOfflineLogin(bool offline_login_active
);
344 void HandleShowLocallyManagedUserCreationScreen();
345 void HandleFocusPod(const std::string
& user_id
);
346 void HandleLaunchKioskApp(const std::string
& app_id
);
348 // Fills |user_dict| with information about |user|.
349 static void FillUserDictionary(User
* user
,
351 DictionaryValue
* user_dict
);
353 // Sends user list to account picker.
354 void SendUserList(bool animated
);
356 // Kick off cookie / local storage cleanup.
357 void StartClearingCookies(const base::Closure
& on_clear_callback
);
358 void OnCookiesCleared(base::Closure on_clear_callback
);
360 // Kick off DNS cache flushing.
361 void StartClearingDnsCache();
364 // Decides whether an auth extension should be pre-loaded. If it should,
366 void MaybePreloadAuthExtension();
369 // (i) log in is restricted to some user list,
370 // (ii) all users in the restricted list are present.
371 bool AllWhitelistedUsersPresent();
373 // Cancels password changed flow - switches back to login screen.
374 // Called as a callback after cookies are cleared.
375 void CancelPasswordChangedFlowInternal();
377 // Returns current visible screen.
378 OobeUI::Screen
GetCurrentScreen() const;
380 // Returns true if current visible screen is the Gaia sign-in page.
381 bool IsGaiaVisible() const;
383 // Returns true if current visible screen is the error screen over
384 // Gaia sign-in page.
385 bool IsGaiaHiddenByError() const;
387 // Returns true if current screen is the error screen over signin
389 bool IsSigninScreenHiddenByError() const;
391 // Returns true if guest signin is allowed.
392 bool IsGuestSigninAllowed() const;
394 // Returns true if offline login is allowed.
395 bool IsOfflineLoginAllowed() const;
397 // Attempts login for test.
398 void SubmitLoginFormForTest();
400 // Update current input method (namely keyboard layout) to LRU by this user.
401 void SetUserInputMethod(const std::string
& username
);
403 // Invoked when auto enrollment check is finished to decide whether to
404 // continue kiosk enable flow. Kiosk enable flow is resumed when
405 // |should_auto_enroll| is false.
406 void ContinueKioskEnableFlow(bool should_auto_enroll
);
408 // Current UI state of the signin screen.
411 // Current state of Gaia frame.
412 FrameState frame_state_
;
414 // Latest Gaia frame error.
415 net::Error frame_error_
;
417 // A delegate that glues this handler with backend LoginDisplay.
418 SigninScreenHandlerDelegate
* delegate_
;
420 // A delegate used to get gfx::NativeWindow.
421 NativeWindowDelegate
* native_window_delegate_
;
423 // Whether screen should be shown right after initialization.
426 // Keeps whether screen should be shown for OOBE.
429 // Is focus still stolen from Gaia page?
432 // Has Gaia page silent load been started for the current sign-in attempt?
433 bool gaia_silent_load_
;
435 // The active network at the moment when Gaia page was preloaded.
436 std::string gaia_silent_load_network_
;
438 // Is account picker being shown for the first time.
439 bool is_account_picker_showing_first_time_
;
441 // True if dns cache cleanup is done.
444 // True if DNS cache task is already running.
445 bool dns_clear_task_running_
;
447 // True if cookie jar cleanup is done.
448 bool cookies_cleared_
;
450 // Help application used for help dialogs.
451 scoped_refptr
<HelpAppLauncher
> help_app_
;
453 // Network state informer used to keep signin screen up.
454 scoped_refptr
<NetworkStateInformer
> network_state_informer_
;
456 // Email to pre-populate with.
458 // Emails of the users, whose passwords have recently been changed.
459 std::set
<std::string
> password_changed_for_
;
462 std::string test_user_
;
463 std::string test_pass_
;
464 bool test_expects_complete_login_
;
466 base::WeakPtrFactory
<SigninScreenHandler
> weak_factory_
;
468 // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
470 bool preferences_changed_delayed_
;
472 ErrorScreenActor
* error_screen_actor_
;
473 CoreOobeActor
* core_oobe_actor_
;
475 bool is_first_update_state_call_
;
476 bool offline_login_active_
;
477 NetworkStateInformer::State last_network_state_
;
479 base::CancelableClosure update_state_closure_
;
480 base::CancelableClosure connecting_closure_
;
482 content::NotificationRegistrar registrar_
;
484 // Whether there is an auth UI pending. This flag is set on receiving
485 // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
486 // NOTIFICATION_AUTH_CANCELLED.
487 bool has_pending_auth_ui_
;
489 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_new_user_subscription_
;
490 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_guest_subscription_
;
492 bool wait_for_auto_enrollment_check_
;
494 base::Closure kiosk_enable_flow_aborted_callback_for_test_
;
496 DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler
);
499 } // namespace chromeos
501 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_