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_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "chrome/browser/chromeos/login/enrollment/auto_enrollment_controller.h"
20 #include "chrome/browser/chromeos/login/screens/error_screen_actor.h"
21 #include "chrome/browser/chromeos/login/signin_specifics.h"
22 #include "chrome/browser/chromeos/login/ui/login_display.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/signin/screenlock_bridge.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 "chrome/browser/ui/webui/chromeos/touch_view_controller_delegate.h"
30 #include "chromeos/ime/ime_keyboard.h"
31 #include "chromeos/ime/input_method_manager.h"
32 #include "chromeos/network/portal_detector/network_portal_detector.h"
33 #include "components/user_manager/user_manager.h"
34 #include "content/public/browser/notification_observer.h"
35 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/browser/web_ui.h"
37 #include "net/base/net_errors.h"
38 #include "ui/events/event_handler.h"
41 class DictionaryValue;
47 class AuthenticatedUserEmailRetriever;
48 class CaptivePortalWindowProxy;
50 class GaiaScreenHandler;
51 class NativeWindowDelegate;
52 class SupervisedUserCreationScreenHandler;
56 // Helper class to pass initial parameters to the login screen.
57 class LoginScreenContext {
60 explicit LoginScreenContext(const base::ListValue* args);
62 void set_email(const std::string& email) { email_ = email; }
63 const std::string& email() const { return email_; }
65 void set_oobe_ui(bool oobe_ui) { oobe_ui_ = oobe_ui; }
66 bool oobe_ui() const { return oobe_ui_; }
75 // An interface for WebUILoginDisplay to call SigninScreenHandler.
76 class LoginDisplayWebUIHandler {
78 virtual void ClearAndEnablePassword() = 0;
79 virtual void ClearUserPodPassword() = 0;
80 virtual void OnUserRemoved(const std::string& username) = 0;
81 virtual void OnUserImageChanged(const user_manager::User& user) = 0;
82 virtual void OnPreferencesChanged() = 0;
83 virtual void ResetSigninScreenHandlerDelegate() = 0;
84 virtual void ShowError(int login_attempts,
85 const std::string& error_text,
86 const std::string& help_link_text,
87 HelpAppLauncher::HelpTopic help_topic_id) = 0;
88 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) = 0;
89 virtual void ShowGaiaPasswordChanged(const std::string& username) = 0;
90 virtual void ShowSigninUI(const std::string& email) = 0;
91 virtual void ShowPasswordChangedDialog(bool show_password_error) = 0;
92 // Show sign-in screen for the given credentials.
93 virtual void ShowSigninScreenForCreds(const std::string& username,
94 const std::string& password) = 0;
95 virtual void LoadUsers(const base::ListValue& users_list,
97 virtual void SetPublicSessionDisplayName(const std::string& user_id,
98 const std::string& display_name) = 0;
99 virtual void SetPublicSessionLocales(const std::string& user_id,
100 scoped_ptr<base::ListValue> locales,
101 const std::string& default_locale,
102 bool multipleRecommendedLocales) = 0;
105 virtual ~LoginDisplayWebUIHandler() {}
108 // An interface for SigninScreenHandler to call WebUILoginDisplay.
109 class SigninScreenHandlerDelegate {
111 // --------------- Password change flow methods.
112 // Cancels current password changed flow.
113 virtual void CancelPasswordChangedFlow() = 0;
115 // Decrypt cryptohome using user provided |old_password|
116 // and migrate to new password.
117 virtual void MigrateUserData(const std::string& old_password) = 0;
119 // Ignore password change, remove existing cryptohome and
120 // force full sync of user data.
121 virtual void ResyncUserData() = 0;
123 // --------------- Sign in/out methods.
124 // Sign in using username and password specified as a part of |user_context|.
125 // Used for both known and new users.
126 virtual void Login(const UserContext& user_context,
127 const SigninSpecifics& specifics) = 0;
129 // Sign in as guest to create a new Google account.
130 virtual void CreateAccount() = 0;
132 // Returns true if sign in is in progress.
133 virtual bool IsSigninInProgress() const = 0;
135 // Signs out if the screen is currently locked.
136 virtual void Signout() = 0;
138 // --------------- Account creation methods.
139 // Confirms sign up by provided credentials in |user_context|.
140 // Used for new user login via GAIA extension.
141 virtual void CompleteLogin(const UserContext& user_context) = 0;
143 // --------------- Shared with login display methods.
144 // Notify the delegate when the sign-in UI is finished loading.
145 virtual void OnSigninScreenReady() = 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 ShowKioskAutolaunchScreen() = 0;
156 // Show wrong hwid screen.
157 virtual void ShowWrongHWIDScreen() = 0;
159 // Sets the displayed email for the next login attempt. If it succeeds,
160 // user's displayed email value will be updated to |email|.
161 virtual void SetDisplayEmail(const std::string& email) = 0;
163 // --------------- Rest of the methods.
164 // Cancels user adding.
165 virtual void CancelUserAdding() = 0;
167 // Load wallpaper for given |username|.
168 virtual void LoadWallpaper(const std::string& username) = 0;
170 // Loads the default sign-in wallpaper.
171 virtual void LoadSigninWallpaper() = 0;
173 // Attempts to remove given user.
174 virtual void RemoveUser(const std::string& username) = 0;
176 // Let the delegate know about the handler it is supposed to be using.
177 virtual void SetWebUIHandler(LoginDisplayWebUIHandler* webui_handler) = 0;
179 // Returns users list to be shown.
180 virtual const user_manager::UserList& GetUsers() const = 0;
182 // Whether login as guest is available.
183 virtual bool IsShowGuest() const = 0;
185 // Weather to show the user pods or only GAIA sign in.
186 // Public sessions are always shown.
187 virtual bool IsShowUsers() const = 0;
189 // Whether user sign in has completed.
190 virtual bool IsUserSigninCompleted() const = 0;
192 // Request to (re)load user list.
193 virtual void HandleGetUsers() = 0;
195 // Set authentication type (for easier unlocking).
196 virtual void SetAuthType(
197 const std::string& username,
198 ScreenlockBridge::LockHandler::AuthType auth_type) = 0;
200 // Get authentication type (for easier unlocking).
201 virtual ScreenlockBridge::LockHandler::AuthType GetAuthType(
202 const std::string& username) const = 0;
205 virtual ~SigninScreenHandlerDelegate() {}
208 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
210 class SigninScreenHandler
211 : public BaseScreenHandler,
212 public LoginDisplayWebUIHandler,
213 public content::NotificationObserver,
214 public ScreenlockBridge::LockHandler,
215 public NetworkStateInformer::NetworkStateInformerObserver,
216 public input_method::ImeKeyboard::Observer,
217 public TouchViewControllerDelegate::Observer,
218 public OobeUI::Observer {
221 const scoped_refptr<NetworkStateInformer>& network_state_informer,
222 ErrorScreenActor* error_screen_actor,
223 CoreOobeActor* core_oobe_actor,
224 GaiaScreenHandler* gaia_screen_handler);
225 virtual ~SigninScreenHandler();
227 // Shows the sign in screen.
228 void Show(const LoginScreenContext& context);
230 // Shows the login spinner UI for retail mode logins.
231 void ShowRetailModeLoginSpinner();
233 // Sets delegate to be used by the handler. It is guaranteed that valid
234 // delegate is set before Show() method will be called.
235 void SetDelegate(SigninScreenHandlerDelegate* delegate);
237 void SetNativeWindowDelegate(NativeWindowDelegate* native_window_delegate);
239 // NetworkStateInformer::NetworkStateInformerObserver implementation:
240 virtual void OnNetworkReady() OVERRIDE;
241 virtual void UpdateState(ErrorScreenActor::ErrorReason reason) OVERRIDE;
243 // Required Local State preferences.
244 static void RegisterPrefs(PrefRegistrySimple* registry);
246 void set_kiosk_enable_flow_aborted_callback_for_test(
247 const base::Closure& callback) {
248 kiosk_enable_flow_aborted_callback_for_test_ = callback;
251 // OobeUI::Observer implemetation.
252 virtual void OnCurrentScreenChanged(OobeUI::Screen current_screen,
253 OobeUI::Screen new_screen) OVERRIDE;
255 // Returns least used user login input method.
256 std::string GetUserLRUInputMethod(const std::string& username) const;
258 void SetFocusPODCallbackForTesting(base::Closure callback);
262 UI_STATE_UNKNOWN = 0,
263 UI_STATE_GAIA_SIGNIN,
264 UI_STATE_ACCOUNT_PICKER,
267 friend class GaiaScreenHandler;
268 friend class ReportDnsCacheClearedOnUIThread;
269 friend class SupervisedUserCreationScreenHandler;
273 // Updates current UI of the signin screen according to |ui_state|
274 // argument. Optionally it can pass screen initialization data via
275 // |params| argument.
276 void UpdateUIState(UIState ui_state, base::DictionaryValue* params);
278 void UpdateStateInternal(ErrorScreenActor::ErrorReason reason,
280 void SetupAndShowOfflineMessage(NetworkStateInformer::State state,
281 ErrorScreenActor::ErrorReason reason);
282 void HideOfflineMessage(NetworkStateInformer::State state,
283 ErrorScreenActor::ErrorReason reason);
284 void ReloadGaia(bool force_reload);
286 // BaseScreenHandler implementation:
287 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
288 virtual void Initialize() OVERRIDE;
289 virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
291 // WebUIMessageHandler implementation:
292 virtual void RegisterMessages() OVERRIDE;
294 // LoginDisplayWebUIHandler implementation:
295 virtual void ClearAndEnablePassword() OVERRIDE;
296 virtual void ClearUserPodPassword() OVERRIDE;
297 virtual void OnUserRemoved(const std::string& username) OVERRIDE;
298 virtual void OnUserImageChanged(const user_manager::User& user) OVERRIDE;
299 virtual void OnPreferencesChanged() OVERRIDE;
300 virtual void ResetSigninScreenHandlerDelegate() OVERRIDE;
301 virtual void ShowError(int login_attempts,
302 const std::string& error_text,
303 const std::string& help_link_text,
304 HelpAppLauncher::HelpTopic help_topic_id) OVERRIDE;
305 virtual void ShowGaiaPasswordChanged(const std::string& username) OVERRIDE;
306 virtual void ShowSigninUI(const std::string& email) OVERRIDE;
307 virtual void ShowPasswordChangedDialog(bool show_password_error) OVERRIDE;
308 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) OVERRIDE;
309 virtual void ShowSigninScreenForCreds(const std::string& username,
310 const std::string& password) OVERRIDE;
311 virtual void LoadUsers(const base::ListValue& users_list,
312 bool show_guest) OVERRIDE;
313 virtual void SetPublicSessionDisplayName(
314 const std::string& user_id,
315 const std::string& display_name) OVERRIDE;
316 virtual void SetPublicSessionLocales(
317 const std::string& user_id,
318 scoped_ptr<base::ListValue> locales,
319 const std::string& default_locale,
320 bool multipleRecommendedLocales) OVERRIDE;
322 // content::NotificationObserver implementation:
323 virtual void Observe(int type,
324 const content::NotificationSource& source,
325 const content::NotificationDetails& details) OVERRIDE;
327 // ScreenlockBridge::LockHandler implementation:
328 virtual void ShowBannerMessage(const base::string16& message) OVERRIDE;
329 virtual void ShowUserPodCustomIcon(
330 const std::string& username,
331 const ScreenlockBridge::UserPodCustomIconOptions& icon) OVERRIDE;
332 virtual void HideUserPodCustomIcon(const std::string& username) OVERRIDE;
333 virtual void EnableInput() OVERRIDE;
334 virtual void SetAuthType(const std::string& username,
335 ScreenlockBridge::LockHandler::AuthType auth_type,
336 const base::string16& initial_value) OVERRIDE;
337 virtual ScreenlockBridge::LockHandler::AuthType GetAuthType(
338 const std::string& username) const OVERRIDE;
339 virtual void Unlock(const std::string& user_email) OVERRIDE;
340 virtual void AttemptEasySignin(const std::string& user_email,
341 const std::string& secret,
342 const std::string& key_label) OVERRIDE;
344 // TouchViewControllerDelegate::Observer implementation:
345 virtual void OnMaximizeModeStarted() OVERRIDE;
346 virtual void OnMaximizeModeEnded() OVERRIDE;
348 // Updates authentication extension. Called when device settings that affect
349 // sign-in (allow BWSI and allow whitelist) are changed.
350 void UserSettingsChanged();
351 void UpdateAddButtonStatus();
353 // Restore input focus to current user pod.
354 void RefocusCurrentPod();
356 // WebUI message handlers.
357 void HandleGetUsers();
358 void HandleAuthenticateUser(const std::string& username,
359 const std::string& password);
360 void HandleAttemptUnlock(const std::string& username);
361 void HandleLaunchDemoUser();
362 void HandleLaunchIncognito();
363 void HandleLaunchPublicSession(const std::string& user_id,
364 const std::string& locale,
365 const std::string& input_method);
366 void HandleOfflineLogin(const base::ListValue* args);
367 void HandleShutdownSystem();
368 void HandleLoadWallpaper(const std::string& email);
369 void HandleRebootSystem();
370 void HandleRemoveUser(const std::string& email);
371 void HandleShowAddUser(const base::ListValue* args);
372 void HandleToggleEnrollmentScreen();
373 void HandleToggleKioskEnableScreen();
374 void HandleToggleResetScreen();
375 void HandleToggleKioskAutolaunchScreen();
376 void HandleCreateAccount();
377 void HandleAccountPickerReady();
378 void HandleWallpaperReady();
379 void HandleSignOutUser();
380 void HandleOpenProxySettings();
381 void HandleLoginVisible(const std::string& source);
382 void HandleCancelPasswordChangedFlow();
383 void HandleCancelUserAdding();
384 void HandleMigrateUserData(const std::string& password);
385 void HandleResyncUserData();
386 void HandleLoginUIStateChanged(const std::string& source, bool new_value);
387 void HandleUnlockOnLoginSuccess();
388 void HandleLoginScreenUpdate();
389 void HandleShowLoadingTimeoutError();
390 void HandleUpdateOfflineLogin(bool offline_login_active);
391 void HandleShowSupervisedUserCreationScreen();
392 void HandleFocusPod(const std::string& user_id);
393 void HandleHardlockPod(const std::string& user_id);
394 void HandleLaunchKioskApp(const std::string& app_id, bool diagnostic_mode);
395 void HandleRetrieveAuthenticatedUserEmail(double attempt_token);
396 void HandleGetPublicSessionKeyboardLayouts(const std::string& user_id,
397 const std::string& locale);
398 void HandleCancelConsumerManagementEnrollment();
399 void HandleGetTouchViewState();
401 // Sends the list of |keyboard_layouts| available for the |locale| that is
402 // currently selected for the public session identified by |user_id|.
403 void SendPublicSessionKeyboardLayouts(
404 const std::string& user_id,
405 const std::string& locale,
406 scoped_ptr<base::ListValue> keyboard_layouts);
409 // (i) log in is restricted to some user list,
410 // (ii) all users in the restricted list are present.
411 bool AllWhitelistedUsersPresent();
413 // Cancels password changed flow - switches back to login screen.
414 // Called as a callback after cookies are cleared.
415 void CancelPasswordChangedFlowInternal();
417 // Returns current visible screen.
418 OobeUI::Screen GetCurrentScreen() const;
420 // Returns true if current visible screen is the Gaia sign-in page.
421 bool IsGaiaVisible() const;
423 // Returns true if current visible screen is the error screen over
424 // Gaia sign-in page.
425 bool IsGaiaHiddenByError() const;
427 // Returns true if current screen is the error screen over signin
429 bool IsSigninScreenHiddenByError() const;
431 // Returns true if guest signin is allowed.
432 bool IsGuestSigninAllowed() const;
434 // Returns true if offline login is allowed.
435 bool IsOfflineLoginAllowed() const;
437 bool ShouldLoadGaia() const;
439 // Update current input method (namely keyboard layout) in the given IME state
440 // to LRU by this user.
441 void SetUserInputMethod(const std::string& username,
442 input_method::InputMethodManager::State* ime_state);
444 // Invoked when auto enrollment check progresses to decide whether to
445 // continue kiosk enable flow. Kiosk enable flow is resumed when
446 // |state| indicates that enrollment is not applicable.
447 void ContinueKioskEnableFlow(policy::AutoEnrollmentState state);
450 void OnShowAddUser();
452 GaiaScreenHandler::FrameState FrameState() const;
453 net::Error FrameError() const;
455 // input_method::ImeKeyboard::Observer implementation:
456 virtual void OnCapsLockChanged(bool enabled) OVERRIDE;
458 // Returns OobeUI object of NULL.
459 OobeUI* GetOobeUI() const;
461 // Current UI state of the signin screen.
464 // A delegate that glues this handler with backend LoginDisplay.
465 SigninScreenHandlerDelegate* delegate_;
467 // A delegate used to get gfx::NativeWindow.
468 NativeWindowDelegate* native_window_delegate_;
470 // Whether screen should be shown right after initialization.
473 // Keeps whether screen should be shown for OOBE.
476 // Is account picker being shown for the first time.
477 bool is_account_picker_showing_first_time_;
479 // Network state informer used to keep signin screen up.
480 scoped_refptr<NetworkStateInformer> network_state_informer_;
482 // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
484 bool preferences_changed_delayed_;
486 ErrorScreenActor* error_screen_actor_;
487 CoreOobeActor* core_oobe_actor_;
489 bool is_first_update_state_call_;
490 bool offline_login_active_;
491 NetworkStateInformer::State last_network_state_;
493 base::CancelableClosure update_state_closure_;
494 base::CancelableClosure connecting_closure_;
496 content::NotificationRegistrar registrar_;
498 // Whether there is an auth UI pending. This flag is set on receiving
499 // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
500 // NOTIFICATION_AUTH_CANCELLED.
501 bool has_pending_auth_ui_;
503 scoped_ptr<AutoEnrollmentController::ProgressCallbackList::Subscription>
504 auto_enrollment_progress_subscription_;
506 bool caps_lock_enabled_;
508 base::Closure kiosk_enable_flow_aborted_callback_for_test_;
511 // TODO(ygorshenin@): remove this dependency.
512 GaiaScreenHandler* gaia_screen_handler_;
514 // Helper that retrieves the authenticated user's e-mail address.
515 scoped_ptr<AuthenticatedUserEmailRetriever> email_retriever_;
517 // Maximized mode controller delegate.
518 scoped_ptr<TouchViewControllerDelegate> max_mode_delegate_;
520 // Whether consumer management enrollment is in progress.
521 bool is_enrolling_consumer_management_;
523 // Input Method Engine state used at signin screen.
524 scoped_refptr<input_method::InputMethodManager::State> ime_state_;
526 // This callback captures "focusPod finished" event for tests.
527 base::Closure test_focus_pod_callback_;
529 // True if SigninScreenHandler has already been added to OobeUI observers.
530 bool oobe_ui_observer_added_;
532 base::WeakPtrFactory<SigninScreenHandler> weak_factory_;
534 DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler);
537 } // namespace chromeos
539 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_