Home | History | Annotate | Download | only in login
      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 <set>
      9 #include <string>
     10 
     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"
     28 
     29 namespace base {
     30 class DictionaryValue;
     31 class ListValue;
     32 }
     33 
     34 namespace chromeos {
     35 
     36 class CaptivePortalWindowProxy;
     37 class CoreOobeActor;
     38 class LocallyManagedUserCreationScreenHandler;
     39 class NativeWindowDelegate;
     40 class User;
     41 struct UserContext;
     42 
     43 // An interface for WebUILoginDisplay to call SigninScreenHandler.
     44 class LoginDisplayWebUIHandler {
     45  public:
     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;
     64  protected:
     65   virtual ~LoginDisplayWebUIHandler() {}
     66 };
     67 
     68 // An interface for SigninScreenHandler to call WebUILoginDisplay.
     69 class SigninScreenHandlerDelegate {
     70  public:
     71   // Cancels current password changed flow.
     72   virtual void CancelPasswordChangedFlow() = 0;
     73 
     74   // Cancels user adding.
     75   virtual void CancelUserAdding() = 0;
     76 
     77   // Create a new Google account.
     78   virtual void CreateAccount() = 0;
     79 
     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;
     83 
     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;
     87 
     88   // Sign in into a retail mode session.
     89   virtual void LoginAsRetailModeUser() = 0;
     90 
     91   // Sign in into guest session.
     92   virtual void LoginAsGuest() = 0;
     93 
     94   // Sign in into the public account identified by |username|.
     95   virtual void LoginAsPublicAccount(const std::string& username) = 0;
     96 
     97   // Decrypt cryptohome using user provided |old_password|
     98   // and migrate to new password.
     99   virtual void MigrateUserData(const std::string& old_password) = 0;
    100 
    101   // Load wallpaper for given |username|.
    102   virtual void LoadWallpaper(const std::string& username) = 0;
    103 
    104   // Loads the default sign-in wallpaper.
    105   virtual void LoadSigninWallpaper() = 0;
    106 
    107   // Notify the delegate when the sign-in UI is finished loading.
    108   virtual void OnSigninScreenReady() = 0;
    109 
    110   // Attempts to remove given user.
    111   virtual void RemoveUser(const std::string& username) = 0;
    112 
    113   // Ignore password change, remove existing cryptohome and
    114   // force full sync of user data.
    115   virtual void ResyncUserData() = 0;
    116 
    117   // Shows Enterprise Enrollment screen.
    118   virtual void ShowEnterpriseEnrollmentScreen() = 0;
    119 
    120   // Shows Kiosk Enable screen.
    121   virtual void ShowKioskEnableScreen() = 0;
    122 
    123   // Shows Reset screen.
    124   virtual void ShowResetScreen() = 0;
    125 
    126   // Shows Reset screen.
    127   virtual void ShowKioskAutolaunchScreen() = 0;
    128 
    129   // Show wrong hwid screen.
    130   virtual void ShowWrongHWIDScreen() = 0;
    131 
    132   // Let the delegate know about the handler it is supposed to be using.
    133   virtual void SetWebUIHandler(LoginDisplayWebUIHandler* webui_handler) = 0;
    134 
    135   // Returns users list to be shown.
    136   virtual const UserList& GetUsers() const = 0;
    137 
    138   // Whether login as guest is available.
    139   virtual bool IsShowGuest() const = 0;
    140 
    141   // Whether login as guest is available.
    142   virtual bool IsShowUsers() const = 0;
    143 
    144   // Whether new user pod is available.
    145   virtual bool IsShowNewUser() const = 0;
    146 
    147   // Returns true if sign in is in progress.
    148   virtual bool IsSigninInProgress() const = 0;
    149 
    150   // Whether user sign in has completed.
    151   virtual bool IsUserSigninCompleted() const = 0;
    152 
    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;
    156 
    157   // Signs out if the screen is currently locked.
    158   virtual void Signout() = 0;
    159 
    160  protected:
    161   virtual ~SigninScreenHandlerDelegate() {}
    162 };
    163 
    164 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
    165 // and LoginDisplay.
    166 class SigninScreenHandler
    167     : public BaseScreenHandler,
    168       public LoginDisplayWebUIHandler,
    169       public SystemKeyEventListener::CapsLockObserver,
    170       public content::NotificationObserver,
    171       public NetworkStateInformer::NetworkStateInformerObserver {
    172  public:
    173   SigninScreenHandler(
    174       const scoped_refptr<NetworkStateInformer>& network_state_informer,
    175       ErrorScreenActor* error_screen_actor,
    176       CoreOobeActor* core_oobe_actor);
    177   virtual ~SigninScreenHandler();
    178 
    179   // Shows the sign in screen. |oobe_ui| indicates whether the signin
    180   // screen is for OOBE or usual sign-in flow.
    181   void Show(bool oobe_ui);
    182 
    183   // Shows the login spinner UI for retail mode logins.
    184   void ShowRetailModeLoginSpinner();
    185 
    186   // Sets delegate to be used by the handler. It is guaranteed that valid
    187   // delegate is set before Show() method will be called.
    188   void SetDelegate(SigninScreenHandlerDelegate* delegate);
    189 
    190   void SetNativeWindowDelegate(NativeWindowDelegate* native_window_delegate);
    191 
    192   // NetworkStateInformer::NetworkStateInformerObserver implementation:
    193   virtual void OnNetworkReady() OVERRIDE;
    194   virtual void UpdateState(ErrorScreenActor::ErrorReason reason) OVERRIDE;
    195 
    196   // Required Local State preferences.
    197   static void RegisterPrefs(PrefRegistrySimple* registry);
    198 
    199  private:
    200   enum UIState {
    201     UI_STATE_UNKNOWN = 0,
    202     UI_STATE_GAIA_SIGNIN,
    203     UI_STATE_ACCOUNT_PICKER,
    204   };
    205 
    206   enum FrameState {
    207     FRAME_STATE_UNKNOWN = 0,
    208     FRAME_STATE_LOADING,
    209     FRAME_STATE_LOADED,
    210     FRAME_STATE_ERROR
    211   };
    212 
    213   typedef base::hash_set<std::string> WebUIObservers;
    214 
    215   friend class ReportDnsCacheClearedOnUIThread;
    216   friend class LocallyManagedUserCreationScreenHandler;
    217 
    218   // Updates current UI of the signin screen according to |ui_state|
    219   // argument.  Optionally it can pass screen initialization data via
    220   // |params| argument.
    221   void UpdateUIState(UIState ui_state, DictionaryValue* params);
    222 
    223   void UpdateStateInternal(ErrorScreenActor::ErrorReason reason,
    224                            bool force_update);
    225   void SetupAndShowOfflineMessage(NetworkStateInformer::State state,
    226                                   ErrorScreenActor::ErrorReason reason);
    227   void HideOfflineMessage(NetworkStateInformer::State state,
    228                           ErrorScreenActor::ErrorReason reason);
    229   void ReloadGaiaScreen();
    230 
    231   // BaseScreenHandler implementation:
    232   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
    233   virtual void Initialize() OVERRIDE;
    234   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
    235 
    236   // WebUIMessageHandler implementation:
    237   virtual void RegisterMessages() OVERRIDE;
    238 
    239   // BaseLoginUIHandler implementation:
    240   virtual void ClearAndEnablePassword() OVERRIDE;
    241   virtual void ClearUserPodPassword() OVERRIDE;
    242   virtual void OnLoginSuccess(const std::string& username) OVERRIDE;
    243   virtual void OnUserRemoved(const std::string& username) OVERRIDE;
    244   virtual void OnUserImageChanged(const User& user) OVERRIDE;
    245   virtual void OnPreferencesChanged() OVERRIDE;
    246   virtual void ResetSigninScreenHandlerDelegate() OVERRIDE;
    247   virtual void ShowError(int login_attempts,
    248                          const std::string& error_text,
    249                          const std::string& help_link_text,
    250                          HelpAppLauncher::HelpTopic help_topic_id) OVERRIDE;
    251   virtual void ShowGaiaPasswordChanged(const std::string& username) OVERRIDE;
    252   virtual void ShowSigninUI(const std::string& email) OVERRIDE;
    253   virtual void ShowPasswordChangedDialog(bool show_password_error) OVERRIDE;
    254   virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) OVERRIDE;
    255   virtual void ShowSigninScreenForCreds(const std::string& username,
    256                                         const std::string& password) OVERRIDE;
    257 
    258   // SystemKeyEventListener::CapsLockObserver overrides.
    259   virtual void OnCapsLockChange(bool enabled) OVERRIDE;
    260 
    261   // content::NotificationObserver implementation:
    262   virtual void Observe(int type,
    263                        const content::NotificationSource& source,
    264                        const content::NotificationDetails& details) OVERRIDE;
    265 
    266   // Shows signin screen after dns cache and cookie cleanup operations finish.
    267   void ShowSigninScreenIfReady();
    268 
    269   // Tells webui to load authentication extension. |force| is used to force the
    270   // extension reloading, if it has already been loaded. |silent_load| is true
    271   // for cases when extension should be loaded in the background and it
    272   // shouldn't grab the focus. |offline| is true when offline version of the
    273   // extension should be used.
    274   void LoadAuthExtension(bool force, bool silent_load, bool offline);
    275 
    276   // Updates authentication extension. Called when device settings that affect
    277   // sign-in (allow BWSI and allow whitelist) are changed.
    278   void UpdateAuthExtension();
    279   void UpdateAddButtonStatus();
    280 
    281   // Fill |params| that are passed to JS..
    282   void UpdateAuthParams(DictionaryValue* params);
    283 
    284   // Restore input focus to current user pod.
    285   void RefocusCurrentPod();
    286 
    287   // WebUI message handlers.
    288   void HandleCompleteAuthentication(const std::string& email,
    289                                     const std::string& password,
    290                                     const std::string& auth_code);
    291   void HandleCompleteLogin(const std::string& typed_email,
    292                            const std::string& password);
    293   void HandleGetUsers();
    294   void HandleAuthenticateUser(const std::string& username,
    295                               const std::string& password);
    296   void HandleLaunchDemoUser();
    297   void HandleLaunchIncognito();
    298   void HandleLaunchPublicAccount(const std::string& username);
    299   void HandleOfflineLogin(const base::ListValue* args);
    300   void HandleShutdownSystem();
    301   void HandleLoadWallpaper(const std::string& email);
    302   void HandleRebootSystem();
    303   void HandleRemoveUser(const std::string& email);
    304   void HandleShowAddUser(const base::ListValue* args);
    305   void HandleToggleEnrollmentScreen();
    306   void HandleToggleKioskEnableScreen();
    307   void HandleToggleResetScreen();
    308   void HandleToggleKioskAutolaunchScreen();
    309   void HandleLaunchHelpApp(double help_topic_id);
    310   void HandleCreateAccount();
    311   void HandleAccountPickerReady();
    312   void HandleWallpaperReady();
    313   void HandleLoginWebuiReady();
    314   void HandleSignOutUser();
    315   void HandleNetworkErrorShown();
    316   void HandleOpenProxySettings();
    317   void HandleLoginVisible(const std::string& source);
    318   void HandleCancelPasswordChangedFlow();
    319   void HandleCancelUserAdding();
    320   void HandleMigrateUserData(const std::string& password);
    321   void HandleResyncUserData();
    322   void HandleLoginUIStateChanged(const std::string& source, bool new_value);
    323   void HandleUnlockOnLoginSuccess();
    324   void HandleLoginScreenUpdate();
    325   void HandleFrameLoadingCompleted(int status);
    326   void HandleShowLoadingTimeoutError();
    327   void HandleUpdateOfflineLogin(bool offline_login_active);
    328   void HandleShowLocallyManagedUserCreationScreen();
    329 
    330   // Fills |user_dict| with information about |user|.
    331   static void FillUserDictionary(User* user,
    332                                  bool is_owner,
    333                                  DictionaryValue* user_dict);
    334 
    335   // Sends user list to account picker.
    336   void SendUserList(bool animated);
    337 
    338   // Kick off cookie / local storage cleanup.
    339   void StartClearingCookies(const base::Closure& on_clear_callback);
    340   void OnCookiesCleared(base::Closure on_clear_callback);
    341 
    342   // Kick off DNS cache flushing.
    343   void StartClearingDnsCache();
    344   void OnDnsCleared();
    345 
    346   // Decides whether an auth extension should be pre-loaded. If it should,
    347   // pre-loads it.
    348   void MaybePreloadAuthExtension();
    349 
    350   // Returns true iff
    351   // (i)   log in is restricted to some user list,
    352   // (ii)  all users in the restricted list are present.
    353   bool AllWhitelistedUsersPresent();
    354 
    355   // Cancels password changed flow - switches back to login screen.
    356   // Called as a callback after cookies are cleared.
    357   void CancelPasswordChangedFlowInternal();
    358 
    359   // Returns current visible screen.
    360   OobeUI::Screen GetCurrentScreen() const;
    361 
    362   // Returns true if current visible screen is the Gaia sign-in page.
    363   bool IsGaiaVisible() const;
    364 
    365   // Returns true if current visible screen is the error screen over
    366   // Gaia sign-in page.
    367   bool IsGaiaHiddenByError() const;
    368 
    369   // Returns true if current screen is the error screen over signin
    370   // screen.
    371   bool IsSigninScreenHiddenByError() const;
    372 
    373   // Returns true if guest signin is allowed.
    374   bool IsGuestSigninAllowed() const;
    375 
    376   // Returns true if offline login is allowed.
    377   bool IsOfflineLoginAllowed() const;
    378 
    379   // Attempts login for test.
    380   void SubmitLoginFormForTest();
    381 
    382   // Update current input method (namely keyboard layout) to LRU by this user.
    383   void SetUserInputMethod(const std::string& username);
    384 
    385   // Current UI state of the signin screen.
    386   UIState ui_state_;
    387 
    388   // Current state of Gaia frame.
    389   FrameState frame_state_;
    390 
    391   // Latest Gaia frame error.
    392   net::Error frame_error_;
    393 
    394   // A delegate that glues this handler with backend LoginDisplay.
    395   SigninScreenHandlerDelegate* delegate_;
    396 
    397   // A delegate used to get gfx::NativeWindow.
    398   NativeWindowDelegate* native_window_delegate_;
    399 
    400   // Whether screen should be shown right after initialization.
    401   bool show_on_init_;
    402 
    403   // Keeps whether screen should be shown for OOBE.
    404   bool oobe_ui_;
    405 
    406   // Is focus still stolen from Gaia page?
    407   bool focus_stolen_;
    408 
    409   // Has Gaia page silent load been started for the current sign-in attempt?
    410   bool gaia_silent_load_;
    411 
    412   // The active network at the moment when Gaia page was preloaded.
    413   std::string gaia_silent_load_network_;
    414 
    415   // Is account picker being shown for the first time.
    416   bool is_account_picker_showing_first_time_;
    417 
    418   // True if dns cache cleanup is done.
    419   bool dns_cleared_;
    420 
    421   // True if DNS cache task is already running.
    422   bool dns_clear_task_running_;
    423 
    424   // True if cookie jar cleanup is done.
    425   bool cookies_cleared_;
    426 
    427   // Help application used for help dialogs.
    428   scoped_refptr<HelpAppLauncher> help_app_;
    429 
    430   // Network state informer used to keep signin screen up.
    431   scoped_refptr<NetworkStateInformer> network_state_informer_;
    432 
    433   // Email to pre-populate with.
    434   std::string email_;
    435   // Emails of the users, whose passwords have recently been changed.
    436   std::set<std::string> password_changed_for_;
    437 
    438   // Test credentials.
    439   std::string test_user_;
    440   std::string test_pass_;
    441   bool test_expects_complete_login_;
    442 
    443   base::WeakPtrFactory<SigninScreenHandler> weak_factory_;
    444 
    445   // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
    446   bool webui_visible_;
    447   bool preferences_changed_delayed_;
    448 
    449   ErrorScreenActor* error_screen_actor_;
    450   CoreOobeActor* core_oobe_actor_;
    451 
    452   bool is_first_update_state_call_;
    453   bool offline_login_active_;
    454   NetworkStateInformer::State last_network_state_;
    455 
    456   base::CancelableClosure update_state_closure_;
    457   base::CancelableClosure connecting_closure_;
    458 
    459   content::NotificationRegistrar registrar_;
    460 
    461   // Whether there is an auth UI pending. This flag is set on receiving
    462   // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
    463   // NOTIFICATION_AUTH_CANCELLED.
    464   bool has_pending_auth_ui_;
    465 
    466   DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler);
    467 };
    468 
    469 }  // namespace chromeos
    470 
    471 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
    472