1 // Copyright (c) 2012 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_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/callback_forward.h" 12 #include "base/compiler_specific.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/strings/string16.h" 17 #include "base/time/time.h" 18 #include "base/timer/timer.h" 19 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 20 #include "chrome/browser/chromeos/login/auth/login_performer.h" 21 #include "chrome/browser/chromeos/login/login_utils.h" 22 #include "chrome/browser/chromeos/login/ui/login_display.h" 23 #include "chrome/browser/chromeos/login/users/user.h" 24 #include "chrome/browser/chromeos/settings/cros_settings.h" 25 #include "chrome/browser/chromeos/settings/device_settings_service.h" 26 #include "content/public/browser/notification_observer.h" 27 #include "content/public/browser/notification_registrar.h" 28 #include "ui/gfx/rect.h" 29 #include "url/gurl.h" 30 31 namespace chromeos { 32 33 class CrosSettings; 34 class LoginDisplayHost; 35 class UserContext; 36 37 namespace login { 38 class NetworkStateHelper; 39 } 40 41 // ExistingUserController is used to handle login when someone has 42 // already logged into the machine. 43 // To use ExistingUserController create an instance of it and invoke Init. 44 // When Init is called it creates LoginDisplay instance which encapsulates 45 // all login UI implementation. 46 // ExistingUserController maintains it's own life cycle and deletes itself when 47 // the user logs in (or chooses to see other settings). 48 class ExistingUserController : public LoginDisplay::Delegate, 49 public content::NotificationObserver, 50 public LoginPerformer::Delegate, 51 public LoginUtils::Delegate { 52 public: 53 // All UI initialization is deferred till Init() call. 54 explicit ExistingUserController(LoginDisplayHost* host); 55 virtual ~ExistingUserController(); 56 57 // Returns the current existing user controller if it has been created. current_controller()58 static ExistingUserController* current_controller() { 59 return current_controller_; 60 } 61 62 // Creates and shows login UI for known users. 63 void Init(const UserList& users); 64 65 // Tells the controller to enter the Enterprise Enrollment screen when 66 // appropriate. 67 void DoAutoEnrollment(); 68 69 // Tells the controller to resume a pending login. 70 void ResumeLogin(); 71 72 // Start the public session auto-login timer. 73 void StartPublicSessionAutoLoginTimer(); 74 75 // Stop the public session auto-login timer when a login attempt begins. 76 void StopPublicSessionAutoLoginTimer(); 77 78 // LoginDisplay::Delegate: implementation 79 virtual void CancelPasswordChangedFlow() OVERRIDE; 80 virtual void CreateAccount() OVERRIDE; 81 virtual void CompleteLogin(const UserContext& user_context) OVERRIDE; 82 virtual base::string16 GetConnectedNetworkName() OVERRIDE; 83 virtual bool IsSigninInProgress() const OVERRIDE; 84 virtual void Login(const UserContext& user_context) OVERRIDE; 85 virtual void MigrateUserData(const std::string& old_password) OVERRIDE; 86 virtual void LoginAsRetailModeUser() OVERRIDE; 87 virtual void LoginAsGuest() OVERRIDE; 88 virtual void LoginAsPublicAccount(const std::string& username) OVERRIDE; 89 virtual void LoginAsKioskApp(const std::string& app_id, 90 bool diagnostic_mode) OVERRIDE; 91 virtual void OnSigninScreenReady() OVERRIDE; 92 virtual void OnUserSelected(const std::string& username) OVERRIDE; 93 virtual void OnStartEnterpriseEnrollment() OVERRIDE; 94 virtual void OnStartKioskEnableScreen() OVERRIDE; 95 virtual void OnStartKioskAutolaunchScreen() OVERRIDE; 96 virtual void ResetPublicSessionAutoLoginTimer() OVERRIDE; 97 virtual void ResyncUserData() OVERRIDE; 98 virtual void SetDisplayEmail(const std::string& email) OVERRIDE; 99 virtual void ShowWrongHWIDScreen() OVERRIDE; 100 virtual void Signout() OVERRIDE; 101 102 // content::NotificationObserver implementation. 103 virtual void Observe(int type, 104 const content::NotificationSource& source, 105 const content::NotificationDetails& details) OVERRIDE; 106 107 // Set a delegate that we will pass LoginStatusConsumer events to. 108 // Used for testing. set_login_status_consumer(LoginStatusConsumer * consumer)109 void set_login_status_consumer(LoginStatusConsumer* consumer) { 110 login_status_consumer_ = consumer; 111 } 112 113 // Returns the LoginDisplay created and owned by this controller. 114 // Used for testing. login_display()115 LoginDisplay* login_display() { 116 return login_display_.get(); 117 } 118 119 // Returns the LoginDisplayHost for this controller. login_display_host()120 LoginDisplayHost* login_display_host() { 121 return host_; 122 } 123 124 private: 125 friend class ExistingUserControllerTest; 126 friend class ExistingUserControllerAutoLoginTest; 127 friend class ExistingUserControllerPublicSessionTest; 128 friend class MockLoginPerformerDelegate; 129 130 // Retrieve public session auto-login policy and update the timer. 131 void ConfigurePublicSessionAutoLogin(); 132 133 // Trigger public session auto-login. 134 void OnPublicSessionAutoLoginTimerFire(); 135 136 // LoginPerformer::Delegate implementation: 137 virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE; 138 virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE; 139 virtual void OnOffTheRecordLoginSuccess() OVERRIDE; 140 virtual void OnPasswordChangeDetected() OVERRIDE; 141 virtual void WhiteListCheckFailed(const std::string& email) OVERRIDE; 142 virtual void PolicyLoadFailed() OVERRIDE; 143 virtual void OnOnlineChecked( 144 const std::string& username, bool success) OVERRIDE; 145 146 // LoginUtils::Delegate implementation: 147 virtual void OnProfilePrepared(Profile* profile) OVERRIDE; 148 149 // Called when device settings change. 150 void DeviceSettingsChanged(); 151 152 // Starts WizardController with the specified screen. 153 void ActivateWizard(const std::string& screen_name); 154 155 // Returns corresponding native window. 156 gfx::NativeWindow GetNativeWindow() const; 157 158 // Adds first-time login URLs. 159 void InitializeStartUrls() const; 160 161 // Show error message. |error_id| error message ID in resources. 162 // If |details| string is not empty, it specify additional error text 163 // provided by authenticator, it is not localized. 164 void ShowError(int error_id, const std::string& details); 165 166 // Shows Gaia page because password change was detected. 167 void ShowGaiaPasswordChanged(const std::string& username); 168 169 // Handles result of ownership check and starts enterprise or kiosk enrollment 170 // if applicable. 171 void OnEnrollmentOwnershipCheckCompleted( 172 DeviceSettingsService::OwnershipStatus status); 173 174 // Handles result of consumer kiosk configurability check and starts 175 // enable kiosk screen if applicable. 176 void OnConsumerKioskAutoLaunchCheckCompleted( 177 KioskAppManager::ConsumerKioskAutoLaunchStatus status); 178 179 // Enters the enterprise enrollment screen. |forced| is true if this is the 180 // result of an auto-enrollment check, and the user shouldn't be able to 181 // easily cancel the enrollment. In that case, |user| is the user name that 182 // first logged in. 183 void ShowEnrollmentScreen(bool forced, const std::string& user); 184 185 // Shows "reset device" screen. 186 void ShowResetScreen(); 187 188 // Shows kiosk feature enable screen. 189 void ShowKioskEnableScreen(); 190 191 // Shows "kiosk auto-launch permission" screen. 192 void ShowKioskAutolaunchScreen(); 193 194 // Shows "critical TPM error" screen. 195 void ShowTPMError(); 196 197 // Invoked to complete login. Login might be suspended if auto-enrollment 198 // has to be performed, and will resume once auto-enrollment completes. 199 void CompleteLoginInternal( 200 const UserContext& user_context, 201 DeviceSettingsService::OwnershipStatus ownership_status); 202 203 // Creates |login_performer_| if necessary and calls login() on it. 204 // The string arguments aren't passed by const reference because this is 205 // posted as |resume_login_callback_| and resets it. 206 void PerformLogin(const UserContext& user_context, 207 LoginPerformer::AuthorizationMode auth_mode); 208 set_login_performer_delegate(LoginPerformer::Delegate * d)209 void set_login_performer_delegate(LoginPerformer::Delegate* d) { 210 login_performer_delegate_.reset(d); 211 } 212 213 // Updates the |login_display_| attached to this controller. 214 void UpdateLoginDisplay(const UserList& users); 215 216 // Sends an accessibility alert event to extension listeners. 217 void SendAccessibilityAlert(const std::string& alert_text); 218 219 // Public session auto-login timer. 220 scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_; 221 222 // Public session auto-login timeout, in milliseconds. 223 int public_session_auto_login_delay_; 224 225 // Username for public session auto-login. 226 std::string public_session_auto_login_username_; 227 228 // Used to execute login operations. 229 scoped_ptr<LoginPerformer> login_performer_; 230 231 // Delegate for login performer to be overridden by tests. 232 // |this| is used if |login_performer_delegate_| is NULL. 233 scoped_ptr<LoginPerformer::Delegate> login_performer_delegate_; 234 235 // Delegate to forward all login status events to. 236 // Tests can use this to receive login status events. 237 LoginStatusConsumer* login_status_consumer_; 238 239 // Username of the last login attempt. 240 std::string last_login_attempt_username_; 241 242 // OOBE/login display host. 243 LoginDisplayHost* host_; 244 245 // Login UI implementation instance. 246 scoped_ptr<LoginDisplay> login_display_; 247 248 // Number of login attempts. Used to show help link when > 1 unsuccessful 249 // logins for the same user. 250 size_t num_login_attempts_; 251 252 // Pointer to the current instance of the controller to be used by 253 // automation tests. 254 static ExistingUserController* current_controller_; 255 256 // Interface to the signed settings store. 257 CrosSettings* cros_settings_; 258 259 // URL to append to start Guest mode with. 260 GURL guest_mode_url_; 261 262 // Used for notifications during the login process. 263 content::NotificationRegistrar registrar_; 264 265 // Factory of callbacks. 266 base::WeakPtrFactory<ExistingUserController> weak_factory_; 267 268 // The displayed email for the next login attempt set by |SetDisplayEmail|. 269 std::string display_email_; 270 271 // Whether offline login attempt failed. 272 bool offline_failed_; 273 274 // Whether login attempt is running. 275 bool is_login_in_progress_; 276 277 // Whether online login attempt succeeded. 278 std::string online_succeeded_for_; 279 280 // True if password has been changed for user who is completing sign in. 281 // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed(). 282 bool password_changed_; 283 284 // True if auto-enrollment should be performed before starting the user's 285 // session. 286 bool do_auto_enrollment_; 287 288 // Whether the sign-in UI is finished loading. 289 bool signin_screen_ready_; 290 291 // The username used for auto-enrollment, if it was triggered. 292 std::string auto_enrollment_username_; 293 294 // Callback to invoke to resume login, after auto-enrollment has completed. 295 base::Closure resume_login_callback_; 296 297 // Time when the signin screen was first displayed. Used to measure the time 298 // from showing the screen until a successful login is performed. 299 base::Time time_init_; 300 301 // Timer for the interval to wait for the reboot after TPM error UI was shown. 302 base::OneShotTimer<ExistingUserController> reboot_timer_; 303 304 scoped_ptr<login::NetworkStateHelper> network_state_helper_; 305 306 scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_; 307 scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_; 308 scoped_ptr<CrosSettings::ObserverSubscription> 309 allow_supervised_user_subscription_; 310 scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_; 311 scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_; 312 scoped_ptr<CrosSettings::ObserverSubscription> 313 local_account_auto_login_id_subscription_; 314 scoped_ptr<CrosSettings::ObserverSubscription> 315 local_account_auto_login_delay_subscription_; 316 317 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin); 318 319 DISALLOW_COPY_AND_ASSIGN(ExistingUserController); 320 }; 321 322 } // namespace chromeos 323 324 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_ 325