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