Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / signin_screen_handler.h
blob4e0c576bfc728a00e7ea75411f9bcec8b95f0f56
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_
8 #include <set>
9 #include <string>
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/login/wallpaper_manager.h"
23 #include "chrome/browser/chromeos/net/network_portal_detector.h"
24 #include "chrome/browser/chromeos/settings/cros_settings.h"
25 #include "chrome/browser/chromeos/system_key_event_listener.h"
26 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler.h"
27 #include "chrome/browser/ui/webui/chromeos/login/gaia_screen_handler.h"
28 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
29 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
30 #include "content/public/browser/notification_observer.h"
31 #include "content/public/browser/notification_registrar.h"
32 #include "content/public/browser/web_ui.h"
33 #include "net/base/net_errors.h"
35 namespace base {
36 class DictionaryValue;
37 class ListValue;
40 namespace chromeos {
42 class AuthenticatedUserEmailRetriever;
43 class CaptivePortalWindowProxy;
44 class CoreOobeActor;
45 class LocallyManagedUserCreationScreenHandler;
46 class NativeWindowDelegate;
47 class User;
48 struct UserContext;
50 // Helper class to pass initial parameters to the login screen.
51 class LoginScreenContext {
52 public:
53 LoginScreenContext();
54 explicit LoginScreenContext(const base::ListValue* args);
56 void set_email(const std::string& email) { email_ = email; }
57 const std::string& email() const { return email_; }
59 void set_oobe_ui(bool oobe_ui) { oobe_ui_ = oobe_ui; }
60 bool oobe_ui() const { return oobe_ui_; }
62 private:
63 void Init();
65 std::string email_;
66 bool oobe_ui_;
69 // An interface for WebUILoginDisplay to call SigninScreenHandler.
70 class LoginDisplayWebUIHandler {
71 public:
72 virtual void ClearAndEnablePassword() = 0;
73 virtual void ClearUserPodPassword() = 0;
74 virtual void OnLoginSuccess(const std::string& username) = 0;
75 virtual void OnUserRemoved(const std::string& username) = 0;
76 virtual void OnUserImageChanged(const User& user) = 0;
77 virtual void OnPreferencesChanged() = 0;
78 virtual void ResetSigninScreenHandlerDelegate() = 0;
79 virtual void ShowBannerMessage(const std::string& message) = 0;
80 virtual void ShowUserPodButton(const std::string& username,
81 const std::string& iconURL,
82 const base::Closure& click_callback) = 0;
83 virtual void ShowError(int login_attempts,
84 const std::string& error_text,
85 const std::string& help_link_text,
86 HelpAppLauncher::HelpTopic help_topic_id) = 0;
87 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) = 0;
88 virtual void ShowGaiaPasswordChanged(const std::string& username) = 0;
89 virtual void ShowSigninUI(const std::string& email) = 0;
90 virtual void ShowPasswordChangedDialog(bool show_password_error) = 0;
91 // Show sign-in screen for the given credentials.
92 virtual void ShowSigninScreenForCreds(const std::string& username,
93 const std::string& password) = 0;
94 protected:
95 virtual ~LoginDisplayWebUIHandler() {}
98 // An interface for SigninScreenHandler to call WebUILoginDisplay.
99 class SigninScreenHandlerDelegate {
100 public:
101 // Cancels current password changed flow.
102 virtual void CancelPasswordChangedFlow() = 0;
104 // Cancels user adding.
105 virtual void CancelUserAdding() = 0;
107 // Create a new Google account.
108 virtual void CreateAccount() = 0;
110 // Confirms sign up by provided credentials in |user_context|.
111 // Used for new user login via GAIA extension.
112 virtual void CompleteLogin(const UserContext& user_context) = 0;
114 // Sign in using username and password specified as a part of |user_context|.
115 // Used for both known and new users.
116 virtual void Login(const UserContext& user_context) = 0;
118 // Sign in into a retail mode session.
119 virtual void LoginAsRetailModeUser() = 0;
121 // Sign in into guest session.
122 virtual void LoginAsGuest() = 0;
124 // Sign in into the public account identified by |username|.
125 virtual void LoginAsPublicAccount(const std::string& username) = 0;
127 // Decrypt cryptohome using user provided |old_password|
128 // and migrate to new password.
129 virtual void MigrateUserData(const std::string& old_password) = 0;
131 // Load wallpaper for given |username|.
132 virtual void LoadWallpaper(const std::string& username) = 0;
134 // Loads the default sign-in wallpaper.
135 virtual void LoadSigninWallpaper() = 0;
137 // Notify the delegate when the sign-in UI is finished loading.
138 virtual void OnSigninScreenReady() = 0;
140 // Attempts to remove given user.
141 virtual void RemoveUser(const std::string& username) = 0;
143 // Ignore password change, remove existing cryptohome and
144 // force full sync of user data.
145 virtual void ResyncUserData() = 0;
147 // Shows Enterprise Enrollment screen.
148 virtual void ShowEnterpriseEnrollmentScreen() = 0;
150 // Shows Kiosk Enable screen.
151 virtual void ShowKioskEnableScreen() = 0;
153 // Shows Reset screen.
154 virtual void ShowResetScreen() = 0;
156 // Shows Reset screen.
157 virtual void ShowKioskAutolaunchScreen() = 0;
159 // Show wrong hwid screen.
160 virtual void ShowWrongHWIDScreen() = 0;
162 // Let the delegate know about the handler it is supposed to be using.
163 virtual void SetWebUIHandler(LoginDisplayWebUIHandler* webui_handler) = 0;
165 // Returns users list to be shown.
166 virtual const UserList& GetUsers() const = 0;
168 // Whether login as guest is available.
169 virtual bool IsShowGuest() const = 0;
171 // Whether login as guest is available.
172 virtual bool IsShowUsers() const = 0;
174 // Whether new user pod is available.
175 virtual bool IsShowNewUser() const = 0;
177 // Returns true if sign in is in progress.
178 virtual bool IsSigninInProgress() const = 0;
180 // Whether user sign in has completed.
181 virtual bool IsUserSigninCompleted() const = 0;
183 // Sets the displayed email for the next login attempt. If it succeeds,
184 // user's displayed email value will be updated to |email|.
185 virtual void SetDisplayEmail(const std::string& email) = 0;
187 // Signs out if the screen is currently locked.
188 virtual void Signout() = 0;
190 // Login to kiosk mode for app with |app_id|.
191 virtual void LoginAsKioskApp(const std::string& app_id) = 0;
193 protected:
194 virtual ~SigninScreenHandlerDelegate() {}
197 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
198 // and LoginDisplay.
199 class SigninScreenHandler
200 : public BaseScreenHandler,
201 public LoginDisplayWebUIHandler,
202 public SystemKeyEventListener::CapsLockObserver,
203 public content::NotificationObserver,
204 public NetworkStateInformer::NetworkStateInformerObserver,
205 public WallpaperManager::Observer {
206 public:
207 SigninScreenHandler(
208 const scoped_refptr<NetworkStateInformer>& network_state_informer,
209 ErrorScreenActor* error_screen_actor,
210 CoreOobeActor* core_oobe_actor,
211 GaiaScreenHandler* gaia_screen_handler);
212 virtual ~SigninScreenHandler();
214 // Shows the sign in screen.
215 void Show(const LoginScreenContext& context);
217 // Shows the login spinner UI for retail mode logins.
218 void ShowRetailModeLoginSpinner();
220 // Sets delegate to be used by the handler. It is guaranteed that valid
221 // delegate is set before Show() method will be called.
222 void SetDelegate(SigninScreenHandlerDelegate* delegate);
224 void SetNativeWindowDelegate(NativeWindowDelegate* native_window_delegate);
226 // NetworkStateInformer::NetworkStateInformerObserver implementation:
227 virtual void OnNetworkReady() OVERRIDE;
228 virtual void UpdateState(ErrorScreenActor::ErrorReason reason) OVERRIDE;
230 // Required Local State preferences.
231 static void RegisterPrefs(PrefRegistrySimple* registry);
233 void set_kiosk_enable_flow_aborted_callback_for_test(
234 const base::Closure& callback) {
235 kiosk_enable_flow_aborted_callback_for_test_ = callback;
238 // From WallpaperManager::Observer
239 virtual void OnWallpaperAnimationFinished(const std::string& email) OVERRIDE;
241 private:
242 enum UIState {
243 UI_STATE_UNKNOWN = 0,
244 UI_STATE_GAIA_SIGNIN,
245 UI_STATE_ACCOUNT_PICKER,
248 typedef base::hash_set<std::string> WebUIObservers;
250 friend class ReportDnsCacheClearedOnUIThread;
251 friend class LocallyManagedUserCreationScreenHandler;
253 void ShowImpl();
255 // Updates current UI of the signin screen according to |ui_state|
256 // argument. Optionally it can pass screen initialization data via
257 // |params| argument.
258 void UpdateUIState(UIState ui_state, base::DictionaryValue* params);
260 void UpdateStateInternal(ErrorScreenActor::ErrorReason reason,
261 bool force_update);
262 void SetupAndShowOfflineMessage(NetworkStateInformer::State state,
263 ErrorScreenActor::ErrorReason reason);
264 void HideOfflineMessage(NetworkStateInformer::State state,
265 ErrorScreenActor::ErrorReason reason);
266 void ReloadGaiaScreen();
268 // BaseScreenHandler implementation:
269 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
270 virtual void Initialize() OVERRIDE;
271 virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
273 // WebUIMessageHandler implementation:
274 virtual void RegisterMessages() OVERRIDE;
276 // LoginDisplayWebUIHandler implementation:
277 virtual void ClearAndEnablePassword() OVERRIDE;
278 virtual void ClearUserPodPassword() OVERRIDE;
279 virtual void OnLoginSuccess(const std::string& username) OVERRIDE;
280 virtual void OnUserRemoved(const std::string& username) OVERRIDE;
281 virtual void OnUserImageChanged(const User& user) OVERRIDE;
282 virtual void OnPreferencesChanged() OVERRIDE;
283 virtual void ResetSigninScreenHandlerDelegate() OVERRIDE;
284 virtual void ShowBannerMessage(const std::string& message) OVERRIDE;
285 virtual void ShowUserPodButton(const std::string& username,
286 const std::string& iconURL,
287 const base::Closure& click_callback) OVERRIDE;
288 virtual void ShowError(int login_attempts,
289 const std::string& error_text,
290 const std::string& help_link_text,
291 HelpAppLauncher::HelpTopic help_topic_id) OVERRIDE;
292 virtual void ShowGaiaPasswordChanged(const std::string& username) OVERRIDE;
293 virtual void ShowSigninUI(const std::string& email) OVERRIDE;
294 virtual void ShowPasswordChangedDialog(bool show_password_error) OVERRIDE;
295 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) OVERRIDE;
296 virtual void ShowSigninScreenForCreds(const std::string& username,
297 const std::string& password) OVERRIDE;
299 // SystemKeyEventListener::CapsLockObserver overrides.
300 virtual void OnCapsLockChange(bool enabled) OVERRIDE;
302 // content::NotificationObserver implementation:
303 virtual void Observe(int type,
304 const content::NotificationSource& source,
305 const content::NotificationDetails& details) OVERRIDE;
307 // Shows signin screen after dns cache and cookie cleanup operations finish.
308 void ShowSigninScreenIfReady();
310 // Tells webui to load authentication extension. |force| is used to force the
311 // extension reloading, if it has already been loaded. |silent_load| is true
312 // for cases when extension should be loaded in the background and it
313 // shouldn't grab the focus. |offline| is true when offline version of the
314 // extension should be used.
315 void LoadAuthExtension(bool force, bool silent_load, bool offline);
317 // Updates authentication extension. Called when device settings that affect
318 // sign-in (allow BWSI and allow whitelist) are changed.
319 void UserSettingsChanged();
320 void UpdateAddButtonStatus();
322 // Restore input focus to current user pod.
323 void RefocusCurrentPod();
325 // WebUI message handlers.
326 void HandleCompleteAuthentication(const std::string& email,
327 const std::string& password,
328 const std::string& auth_code);
329 void HandleCompleteLogin(const std::string& typed_email,
330 const std::string& password);
331 void HandleGetUsers();
332 void HandleAuthenticateUser(const std::string& username,
333 const std::string& password);
334 void HandleLaunchDemoUser();
335 void HandleLaunchIncognito();
336 void HandleLaunchPublicAccount(const std::string& username);
337 void HandleOfflineLogin(const base::ListValue* args);
338 void HandleShutdownSystem();
339 void HandleLoadWallpaper(const std::string& email);
340 void HandleRebootSystem();
341 void HandleRemoveUser(const std::string& email);
342 void HandleShowAddUser(const base::ListValue* args);
343 void HandleToggleEnrollmentScreen();
344 void HandleToggleKioskEnableScreen();
345 void HandleToggleResetScreen();
346 void HandleToggleKioskAutolaunchScreen();
347 void HandleLaunchHelpApp(double help_topic_id);
348 void HandleCreateAccount();
349 void HandleAccountPickerReady();
350 void HandleWallpaperReady();
351 void HandleLoginWebuiReady();
352 void HandleSignOutUser();
353 void HandleNetworkErrorShown();
354 void HandleOpenProxySettings();
355 void HandleLoginVisible(const std::string& source);
356 void HandleCancelPasswordChangedFlow();
357 void HandleCancelUserAdding();
358 void HandleMigrateUserData(const std::string& password);
359 void HandleResyncUserData();
360 void HandleLoginUIStateChanged(const std::string& source, bool new_value);
361 void HandleUnlockOnLoginSuccess();
362 void HandleLoginScreenUpdate();
363 void HandleShowLoadingTimeoutError();
364 void HandleUpdateOfflineLogin(bool offline_login_active);
365 void HandleShowLocallyManagedUserCreationScreen();
366 void HandleFocusPod(const std::string& user_id);
367 void HandleLaunchKioskApp(const std::string& app_id);
368 void HandleCustomButtonClicked(const std::string& username);
369 void HandleRetrieveAuthenticatedUserEmail(double attempt_token);
371 // Fills |user_dict| with information about |user|.
372 static void FillUserDictionary(User* user,
373 bool is_owner,
374 base::DictionaryValue* user_dict);
376 // Sends user list to account picker.
377 void SendUserList(bool animated);
379 // Kick off cookie / local storage cleanup.
380 void StartClearingCookies(const base::Closure& on_clear_callback);
381 void OnCookiesCleared(base::Closure on_clear_callback);
383 // Kick off DNS cache flushing.
384 void StartClearingDnsCache();
385 void OnDnsCleared();
387 // Decides whether an auth extension should be pre-loaded. If it should,
388 // pre-loads it.
389 void MaybePreloadAuthExtension();
391 // Returns true iff
392 // (i) log in is restricted to some user list,
393 // (ii) all users in the restricted list are present.
394 bool AllWhitelistedUsersPresent();
396 // Cancels password changed flow - switches back to login screen.
397 // Called as a callback after cookies are cleared.
398 void CancelPasswordChangedFlowInternal();
400 // Returns current visible screen.
401 OobeUI::Screen GetCurrentScreen() const;
403 // Returns true if current visible screen is the Gaia sign-in page.
404 bool IsGaiaVisible() const;
406 // Returns true if current visible screen is the error screen over
407 // Gaia sign-in page.
408 bool IsGaiaHiddenByError() const;
410 // Returns true if current screen is the error screen over signin
411 // screen.
412 bool IsSigninScreenHiddenByError() const;
414 // Returns true if guest signin is allowed.
415 bool IsGuestSigninAllowed() const;
417 // Returns true if offline login is allowed.
418 bool IsOfflineLoginAllowed() const;
420 // Attempts login for test.
421 void SubmitLoginFormForTest();
423 // Update current input method (namely keyboard layout) to LRU by this user.
424 void SetUserInputMethod(const std::string& username);
426 // Invoked when auto enrollment check is finished to decide whether to
427 // continue kiosk enable flow. Kiosk enable flow is resumed when
428 // |should_auto_enroll| is false.
429 void ContinueKioskEnableFlow(bool should_auto_enroll);
431 // Shows signin screen for |email|.
432 void OnShowAddUser(const std::string& email);
434 GaiaScreenHandler::FrameState FrameState() const;
435 net::Error FrameError() const;
437 // Current UI state of the signin screen.
438 UIState ui_state_;
440 // A delegate that glues this handler with backend LoginDisplay.
441 SigninScreenHandlerDelegate* delegate_;
443 // A delegate used to get gfx::NativeWindow.
444 NativeWindowDelegate* native_window_delegate_;
446 // Whether screen should be shown right after initialization.
447 bool show_on_init_;
449 // Keeps whether screen should be shown for OOBE.
450 bool oobe_ui_;
452 // Is focus still stolen from Gaia page?
453 bool focus_stolen_;
455 // Has Gaia page silent load been started for the current sign-in attempt?
456 bool gaia_silent_load_;
458 // The active network at the moment when Gaia page was preloaded.
459 std::string gaia_silent_load_network_;
461 // Is account picker being shown for the first time.
462 bool is_account_picker_showing_first_time_;
464 // True if dns cache cleanup is done.
465 bool dns_cleared_;
467 // True if DNS cache task is already running.
468 bool dns_clear_task_running_;
470 // True if cookie jar cleanup is done.
471 bool cookies_cleared_;
473 // Help application used for help dialogs.
474 scoped_refptr<HelpAppLauncher> help_app_;
476 // Network state informer used to keep signin screen up.
477 scoped_refptr<NetworkStateInformer> network_state_informer_;
479 // Email to pre-populate with.
480 std::string email_;
481 // Emails of the users, whose passwords have recently been changed.
482 std::set<std::string> password_changed_for_;
484 // Test credentials.
485 std::string test_user_;
486 std::string test_pass_;
487 bool test_expects_complete_login_;
489 base::WeakPtrFactory<SigninScreenHandler> weak_factory_;
491 // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
492 bool webui_visible_;
493 bool preferences_changed_delayed_;
495 ErrorScreenActor* error_screen_actor_;
496 CoreOobeActor* core_oobe_actor_;
498 bool is_first_update_state_call_;
499 bool offline_login_active_;
500 NetworkStateInformer::State last_network_state_;
502 base::CancelableClosure update_state_closure_;
503 base::CancelableClosure connecting_closure_;
505 content::NotificationRegistrar registrar_;
507 // Whether there is an auth UI pending. This flag is set on receiving
508 // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
509 // NOTIFICATION_AUTH_CANCELLED.
510 bool has_pending_auth_ui_;
512 scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
513 scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
515 bool wait_for_auto_enrollment_check_;
517 base::Closure kiosk_enable_flow_aborted_callback_for_test_;
519 // Map of callbacks run when the custom button on a user pod is clicked.
520 std::map<std::string, base::Closure> user_pod_button_callback_map_;
522 // Non-owning ptr.
523 // TODO (ygorshenin@): remove this dependency.
524 GaiaScreenHandler* gaia_screen_handler_;
526 // Helper that retrieves the authenticated user's e-mail address.
527 scoped_ptr<AuthenticatedUserEmailRetriever> email_retriever_;
529 DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler);
532 } // namespace chromeos
534 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_