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