86d4dda322d4bb42497e2afda2d2798936ffa76a
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / login / signin_screen_handler.h
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.
4
5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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 "chromeos/ime/ime_keyboard.h"
30 #include "chromeos/network/portal_detector/network_portal_detector.h"
31 #include "components/user_manager/user_manager.h"
32 #include "content/public/browser/notification_observer.h"
33 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/web_ui.h"
35 #include "net/base/net_errors.h"
36 #include "ui/events/event_handler.h"
37
38 namespace base {
39 class DictionaryValue;
40 class ListValue;
41 }
42
43 namespace chromeos {
44
45 class AuthenticatedUserEmailRetriever;
46 class CaptivePortalWindowProxy;
47 class CoreOobeActor;
48 class GaiaScreenHandler;
49 class NativeWindowDelegate;
50 class SupervisedUserCreationScreenHandler;
51 class User;
52 class UserContext;
53
54 // Helper class to pass initial parameters to the login screen.
55 class LoginScreenContext {
56  public:
57   LoginScreenContext();
58   explicit LoginScreenContext(const base::ListValue* args);
59
60   void set_email(const std::string& email) { email_ = email; }
61   const std::string& email() const { return email_; }
62
63   void set_oobe_ui(bool oobe_ui) { oobe_ui_ = oobe_ui; }
64   bool oobe_ui() const { return oobe_ui_; }
65
66  private:
67   void Init();
68
69   std::string email_;
70   bool oobe_ui_;
71 };
72
73 // An interface for WebUILoginDisplay to call SigninScreenHandler.
74 class LoginDisplayWebUIHandler {
75  public:
76   virtual void ClearAndEnablePassword() = 0;
77   virtual void ClearUserPodPassword() = 0;
78   virtual void OnUserRemoved(const std::string& username) = 0;
79   virtual void OnUserImageChanged(const user_manager::User& user) = 0;
80   virtual void OnPreferencesChanged() = 0;
81   virtual void ResetSigninScreenHandlerDelegate() = 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;
93   virtual void LoadUsers(const base::ListValue& users_list,
94                          bool show_guest) = 0;
95   virtual void SetPublicSessionDisplayName(const std::string& user_id,
96                                            const std::string& display_name) = 0;
97   virtual void SetPublicSessionLocales(const std::string& user_id,
98                                        scoped_ptr<base::ListValue> locales,
99                                        const std::string& default_locale,
100                                        bool multipleRecommendedLocales) = 0;
101
102  protected:
103   virtual ~LoginDisplayWebUIHandler() {}
104 };
105
106 // An interface for SigninScreenHandler to call WebUILoginDisplay.
107 class SigninScreenHandlerDelegate {
108  public:
109   // --------------- Password change flow methods.
110   // Cancels current password changed flow.
111   virtual void CancelPasswordChangedFlow() = 0;
112
113   // Decrypt cryptohome using user provided |old_password|
114   // and migrate to new password.
115   virtual void MigrateUserData(const std::string& old_password) = 0;
116
117   // Ignore password change, remove existing cryptohome and
118   // force full sync of user data.
119   virtual void ResyncUserData() = 0;
120
121   // --------------- Sign in/out methods.
122   // Sign in using username and password specified as a part of |user_context|.
123   // Used for both known and new users.
124   virtual void Login(const UserContext& user_context,
125                      const SigninSpecifics& specifics) = 0;
126
127   // Sign in as guest to create a new Google account.
128   virtual void CreateAccount() = 0;
129
130   // Returns true if sign in is in progress.
131   virtual bool IsSigninInProgress() const = 0;
132
133   // Signs out if the screen is currently locked.
134   virtual void Signout() = 0;
135
136   // --------------- Account creation methods.
137   // Confirms sign up by provided credentials in |user_context|.
138   // Used for new user login via GAIA extension.
139   virtual void CompleteLogin(const UserContext& user_context) = 0;
140
141   // --------------- Shared with login display methods.
142   // Notify the delegate when the sign-in UI is finished loading.
143   virtual void OnSigninScreenReady() = 0;
144
145   // Shows Enterprise Enrollment screen.
146   virtual void ShowEnterpriseEnrollmentScreen() = 0;
147
148   // Shows Kiosk Enable screen.
149   virtual void ShowKioskEnableScreen() = 0;
150
151   // Shows Reset screen.
152   virtual void ShowKioskAutolaunchScreen() = 0;
153
154   // Show wrong hwid screen.
155   virtual void ShowWrongHWIDScreen() = 0;
156
157   // Sets the displayed email for the next login attempt. If it succeeds,
158   // user's displayed email value will be updated to |email|.
159   virtual void SetDisplayEmail(const std::string& email) = 0;
160
161   // --------------- Rest of the methods.
162   // Cancels user adding.
163   virtual void CancelUserAdding() = 0;
164
165   // Load wallpaper for given |username|.
166   virtual void LoadWallpaper(const std::string& username) = 0;
167
168   // Loads the default sign-in wallpaper.
169   virtual void LoadSigninWallpaper() = 0;
170
171   // Attempts to remove given user.
172   virtual void RemoveUser(const std::string& username) = 0;
173
174   // Let the delegate know about the handler it is supposed to be using.
175   virtual void SetWebUIHandler(LoginDisplayWebUIHandler* webui_handler) = 0;
176
177   // Returns users list to be shown.
178   virtual const user_manager::UserList& GetUsers() const = 0;
179
180   // Whether login as guest is available.
181   virtual bool IsShowGuest() const = 0;
182
183   // Weather to show the user pods or only GAIA sign in.
184   // Public sessions are always shown.
185   virtual bool IsShowUsers() const = 0;
186
187   // Whether user sign in has completed.
188   virtual bool IsUserSigninCompleted() const = 0;
189
190   // Request to (re)load user list.
191   virtual void HandleGetUsers() = 0;
192
193   // Set authentication type (for easier unlocking).
194   virtual void SetAuthType(
195       const std::string& username,
196       ScreenlockBridge::LockHandler::AuthType auth_type) = 0;
197
198   // Get authentication type (for easier unlocking).
199   virtual ScreenlockBridge::LockHandler::AuthType GetAuthType(
200       const std::string& username) const = 0;
201
202  protected:
203   virtual ~SigninScreenHandlerDelegate() {}
204 };
205
206 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
207 // and LoginDisplay.
208 class SigninScreenHandler
209     : public BaseScreenHandler,
210       public LoginDisplayWebUIHandler,
211       public content::NotificationObserver,
212       public ScreenlockBridge::LockHandler,
213       public NetworkStateInformer::NetworkStateInformerObserver,
214       public input_method::ImeKeyboard::Observer {
215  public:
216   SigninScreenHandler(
217       const scoped_refptr<NetworkStateInformer>& network_state_informer,
218       ErrorScreenActor* error_screen_actor,
219       CoreOobeActor* core_oobe_actor,
220       GaiaScreenHandler* gaia_screen_handler);
221   virtual ~SigninScreenHandler();
222
223   // Shows the sign in screen.
224   void Show(const LoginScreenContext& context);
225
226   // Shows the login spinner UI for retail mode logins.
227   void ShowRetailModeLoginSpinner();
228
229   // Sets delegate to be used by the handler. It is guaranteed that valid
230   // delegate is set before Show() method will be called.
231   void SetDelegate(SigninScreenHandlerDelegate* delegate);
232
233   void SetNativeWindowDelegate(NativeWindowDelegate* native_window_delegate);
234
235   // NetworkStateInformer::NetworkStateInformerObserver implementation:
236   virtual void OnNetworkReady() OVERRIDE;
237   virtual void UpdateState(ErrorScreenActor::ErrorReason reason) OVERRIDE;
238
239   // Required Local State preferences.
240   static void RegisterPrefs(PrefRegistrySimple* registry);
241
242   void set_kiosk_enable_flow_aborted_callback_for_test(
243       const base::Closure& callback) {
244     kiosk_enable_flow_aborted_callback_for_test_ = callback;
245   }
246
247  private:
248   enum UIState {
249     UI_STATE_UNKNOWN = 0,
250     UI_STATE_GAIA_SIGNIN,
251     UI_STATE_ACCOUNT_PICKER,
252   };
253
254   friend class GaiaScreenHandler;
255   friend class ReportDnsCacheClearedOnUIThread;
256   friend class SupervisedUserCreationScreenHandler;
257
258   void ShowImpl();
259
260   // Updates current UI of the signin screen according to |ui_state|
261   // argument.  Optionally it can pass screen initialization data via
262   // |params| argument.
263   void UpdateUIState(UIState ui_state, base::DictionaryValue* params);
264
265   void UpdateStateInternal(ErrorScreenActor::ErrorReason reason,
266                            bool force_update);
267   void SetupAndShowOfflineMessage(NetworkStateInformer::State state,
268                                   ErrorScreenActor::ErrorReason reason);
269   void HideOfflineMessage(NetworkStateInformer::State state,
270                           ErrorScreenActor::ErrorReason reason);
271   void ReloadGaia(bool force_reload);
272
273   // BaseScreenHandler implementation:
274   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
275   virtual void Initialize() OVERRIDE;
276   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
277
278   // WebUIMessageHandler implementation:
279   virtual void RegisterMessages() OVERRIDE;
280
281   // LoginDisplayWebUIHandler implementation:
282   virtual void ClearAndEnablePassword() OVERRIDE;
283   virtual void ClearUserPodPassword() OVERRIDE;
284   virtual void OnUserRemoved(const std::string& username) OVERRIDE;
285   virtual void OnUserImageChanged(const user_manager::User& user) OVERRIDE;
286   virtual void OnPreferencesChanged() OVERRIDE;
287   virtual void ResetSigninScreenHandlerDelegate() 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;
298   virtual void LoadUsers(const base::ListValue& users_list,
299                          bool show_guest) OVERRIDE;
300   virtual void SetPublicSessionDisplayName(
301       const std::string& user_id,
302       const std::string& display_name) OVERRIDE;
303   virtual void SetPublicSessionLocales(
304       const std::string& user_id,
305       scoped_ptr<base::ListValue> locales,
306       const std::string& default_locale,
307       bool multipleRecommendedLocales) OVERRIDE;
308
309   // content::NotificationObserver implementation:
310   virtual void Observe(int type,
311                        const content::NotificationSource& source,
312                        const content::NotificationDetails& details) OVERRIDE;
313
314   // ScreenlockBridge::LockHandler implementation:
315   virtual void ShowBannerMessage(const base::string16& message) OVERRIDE;
316   virtual void ShowUserPodCustomIcon(
317       const std::string& username,
318       const ScreenlockBridge::UserPodCustomIconOptions& icon) OVERRIDE;
319   virtual void HideUserPodCustomIcon(const std::string& username) OVERRIDE;
320   virtual void EnableInput() OVERRIDE;
321   virtual void SetAuthType(const std::string& username,
322                            ScreenlockBridge::LockHandler::AuthType auth_type,
323                            const base::string16& initial_value) OVERRIDE;
324   virtual ScreenlockBridge::LockHandler::AuthType GetAuthType(
325       const std::string& username) const OVERRIDE;
326   virtual void Unlock(const std::string& user_email) OVERRIDE;
327
328   // Updates authentication extension. Called when device settings that affect
329   // sign-in (allow BWSI and allow whitelist) are changed.
330   void UserSettingsChanged();
331   void UpdateAddButtonStatus();
332
333   // Restore input focus to current user pod.
334   void RefocusCurrentPod();
335
336   // WebUI message handlers.
337   void HandleGetUsers();
338   void HandleAuthenticateUser(const std::string& username,
339                               const std::string& password);
340   void HandleAttemptUnlock(const std::string& username);
341   void HandleLaunchDemoUser();
342   void HandleLaunchIncognito();
343   void HandleLaunchPublicSession(const std::string& user_id,
344                                  const std::string& locale,
345                                  const std::string& input_method);
346   void HandleOfflineLogin(const base::ListValue* args);
347   void HandleShutdownSystem();
348   void HandleLoadWallpaper(const std::string& email);
349   void HandleRebootSystem();
350   void HandleRemoveUser(const std::string& email);
351   void HandleShowAddUser(const base::ListValue* args);
352   void HandleToggleEnrollmentScreen();
353   void HandleToggleKioskEnableScreen();
354   void HandleToggleResetScreen();
355   void HandleToggleKioskAutolaunchScreen();
356   void HandleCreateAccount();
357   void HandleAccountPickerReady();
358   void HandleWallpaperReady();
359   void HandleSignOutUser();
360   void HandleOpenProxySettings();
361   void HandleLoginVisible(const std::string& source);
362   void HandleCancelPasswordChangedFlow();
363   void HandleCancelUserAdding();
364   void HandleMigrateUserData(const std::string& password);
365   void HandleResyncUserData();
366   void HandleLoginUIStateChanged(const std::string& source, bool new_value);
367   void HandleUnlockOnLoginSuccess();
368   void HandleLoginScreenUpdate();
369   void HandleShowLoadingTimeoutError();
370   void HandleUpdateOfflineLogin(bool offline_login_active);
371   void HandleShowSupervisedUserCreationScreen();
372   void HandleFocusPod(const std::string& user_id);
373   void HandleLaunchKioskApp(const std::string& app_id, bool diagnostic_mode);
374   void HandleRetrieveAuthenticatedUserEmail(double attempt_token);
375   void HandleGetPublicSessionKeyboardLayouts(const std::string& user_id,
376                                              const std::string& locale);
377   void HandleCancelConsumerManagementEnrollment();
378
379   // Sends the list of |keyboard_layouts| available for the |locale| that is
380   // currently selected for the public session identified by |user_id|.
381   void SendPublicSessionKeyboardLayouts(
382       const std::string& user_id,
383       const std::string& locale,
384       scoped_ptr<base::ListValue> keyboard_layouts);
385
386   // Returns true iff
387   // (i)   log in is restricted to some user list,
388   // (ii)  all users in the restricted list are present.
389   bool AllWhitelistedUsersPresent();
390
391   // Cancels password changed flow - switches back to login screen.
392   // Called as a callback after cookies are cleared.
393   void CancelPasswordChangedFlowInternal();
394
395   // Returns current visible screen.
396   OobeUI::Screen GetCurrentScreen() const;
397
398   // Returns true if current visible screen is the Gaia sign-in page.
399   bool IsGaiaVisible() const;
400
401   // Returns true if current visible screen is the error screen over
402   // Gaia sign-in page.
403   bool IsGaiaHiddenByError() const;
404
405   // Returns true if current screen is the error screen over signin
406   // screen.
407   bool IsSigninScreenHiddenByError() const;
408
409   // Returns true if guest signin is allowed.
410   bool IsGuestSigninAllowed() const;
411
412   // Returns true if offline login is allowed.
413   bool IsOfflineLoginAllowed() const;
414
415   bool ShouldLoadGaia() const;
416
417   // Update current input method (namely keyboard layout) to LRU by this user.
418   void SetUserInputMethod(const std::string& username);
419
420   // Invoked when auto enrollment check progresses to decide whether to
421   // continue kiosk enable flow. Kiosk enable flow is resumed when
422   // |state| indicates that enrollment is not applicable.
423   void ContinueKioskEnableFlow(policy::AutoEnrollmentState state);
424
425   // Shows signin.
426   void OnShowAddUser();
427
428   GaiaScreenHandler::FrameState FrameState() const;
429   net::Error FrameError() const;
430
431   // input_method::ImeKeyboard::Observer implementation:
432   virtual void OnCapsLockChanged(bool enabled) OVERRIDE;
433
434   // Current UI state of the signin screen.
435   UIState ui_state_;
436
437   // A delegate that glues this handler with backend LoginDisplay.
438   SigninScreenHandlerDelegate* delegate_;
439
440   // A delegate used to get gfx::NativeWindow.
441   NativeWindowDelegate* native_window_delegate_;
442
443   // Whether screen should be shown right after initialization.
444   bool show_on_init_;
445
446   // Keeps whether screen should be shown for OOBE.
447   bool oobe_ui_;
448
449   // Is account picker being shown for the first time.
450   bool is_account_picker_showing_first_time_;
451
452   // Network state informer used to keep signin screen up.
453   scoped_refptr<NetworkStateInformer> network_state_informer_;
454
455   // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
456   bool webui_visible_;
457   bool preferences_changed_delayed_;
458
459   ErrorScreenActor* error_screen_actor_;
460   CoreOobeActor* core_oobe_actor_;
461
462   bool is_first_update_state_call_;
463   bool offline_login_active_;
464   NetworkStateInformer::State last_network_state_;
465
466   base::CancelableClosure update_state_closure_;
467   base::CancelableClosure connecting_closure_;
468
469   content::NotificationRegistrar registrar_;
470
471   // Whether there is an auth UI pending. This flag is set on receiving
472   // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
473   // NOTIFICATION_AUTH_CANCELLED.
474   bool has_pending_auth_ui_;
475
476   scoped_ptr<AutoEnrollmentController::ProgressCallbackList::Subscription>
477       auto_enrollment_progress_subscription_;
478
479   bool caps_lock_enabled_;
480
481   base::Closure kiosk_enable_flow_aborted_callback_for_test_;
482
483   // Non-owning ptr.
484   // TODO(ygorshenin@): remove this dependency.
485   GaiaScreenHandler* gaia_screen_handler_;
486
487   // Helper that retrieves the authenticated user's e-mail address.
488   scoped_ptr<AuthenticatedUserEmailRetriever> email_retriever_;
489
490   // Whether consumer management enrollment is in progress.
491   bool is_enrolling_consumer_management_;
492
493   base::WeakPtrFactory<SigninScreenHandler> weak_factory_;
494
495   DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler);
496 };
497
498 }  // namespace chromeos
499
500 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_