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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_
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/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #include "components/user_manager/user.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
28 #include "ui/gfx/rect.h"
38 class LoginDisplayHost;
42 class NetworkStateHelper;
45 // ExistingUserController is used to handle login when someone has
46 // already logged into the machine.
47 // To use ExistingUserController create an instance of it and invoke Init.
48 // When Init is called it creates LoginDisplay instance which encapsulates
49 // all login UI implementation.
50 // ExistingUserController maintains it's own life cycle and deletes itself when
51 // the user logs in (or chooses to see other settings).
52 class ExistingUserController : public LoginDisplay::Delegate,
53 public content::NotificationObserver,
54 public LoginPerformer::Delegate,
55 public LoginUtils::Delegate {
57 // All UI initialization is deferred till Init() call.
58 explicit ExistingUserController(LoginDisplayHost* host);
59 virtual ~ExistingUserController();
61 // Returns the current existing user controller if it has been created.
62 static ExistingUserController* current_controller() {
63 return current_controller_;
66 // Creates and shows login UI for known users.
67 void Init(const user_manager::UserList& users);
69 // Tells the controller to enter the Enterprise Enrollment screen when
71 void DoAutoEnrollment();
73 // Tells the controller to resume a pending login.
76 // Start the public session auto-login timer.
77 void StartPublicSessionAutoLoginTimer();
79 // Stop the public session auto-login timer when a login attempt begins.
80 void StopPublicSessionAutoLoginTimer();
82 // LoginDisplay::Delegate: implementation
83 virtual void CancelPasswordChangedFlow() OVERRIDE;
84 virtual void CreateAccount() OVERRIDE;
85 virtual void CompleteLogin(const UserContext& user_context) OVERRIDE;
86 virtual base::string16 GetConnectedNetworkName() OVERRIDE;
87 virtual bool IsSigninInProgress() const OVERRIDE;
88 virtual void Login(const UserContext& user_context,
89 const SigninSpecifics& specifics) OVERRIDE;
90 virtual void MigrateUserData(const std::string& old_password) OVERRIDE;
91 virtual void OnSigninScreenReady() OVERRIDE;
92 virtual void OnStartEnterpriseEnrollment() OVERRIDE;
93 virtual void OnStartKioskEnableScreen() OVERRIDE;
94 virtual void OnStartKioskAutolaunchScreen() OVERRIDE;
95 virtual void ResetPublicSessionAutoLoginTimer() OVERRIDE;
96 virtual void ResyncUserData() OVERRIDE;
97 virtual void SetDisplayEmail(const std::string& email) OVERRIDE;
98 virtual void ShowWrongHWIDScreen() OVERRIDE;
99 virtual void Signout() OVERRIDE;
101 void LoginAsRetailModeUser();
103 void LoginAsPublicSession(const UserContext& user_context);
104 void LoginAsKioskApp(const std::string& app_id, bool diagnostic_mode);
106 // content::NotificationObserver implementation.
107 virtual void Observe(int type,
108 const content::NotificationSource& source,
109 const content::NotificationDetails& details) OVERRIDE;
111 // Set a delegate that we will pass AuthStatusConsumer events to.
113 void set_login_status_consumer(AuthStatusConsumer* consumer) {
114 auth_status_consumer_ = consumer;
117 // Returns the LoginDisplay created and owned by this controller.
119 LoginDisplay* login_display() {
120 return login_display_.get();
123 // Returns the LoginDisplayHost for this controller.
124 LoginDisplayHost* login_display_host() {
128 // Returns value of LoginPerformer::auth_mode() (cached if performer is
130 LoginPerformer::AuthorizationMode auth_mode() const;
132 // Returns value of LoginPerformer::password_changed() (cached if performer is
134 bool password_changed() const;
137 friend class ExistingUserControllerTest;
138 friend class ExistingUserControllerAutoLoginTest;
139 friend class ExistingUserControllerPublicSessionTest;
140 friend class MockLoginPerformerDelegate;
142 // Retrieve public session auto-login policy and update the timer.
143 void ConfigurePublicSessionAutoLogin();
145 // Trigger public session auto-login.
146 void OnPublicSessionAutoLoginTimerFire();
148 // LoginPerformer::Delegate implementation:
149 virtual void OnAuthFailure(const AuthFailure& error) OVERRIDE;
150 virtual void OnAuthSuccess(const UserContext& user_context) OVERRIDE;
151 virtual void OnOffTheRecordAuthSuccess() OVERRIDE;
152 virtual void OnPasswordChangeDetected() OVERRIDE;
153 virtual void WhiteListCheckFailed(const std::string& email) OVERRIDE;
154 virtual void PolicyLoadFailed() OVERRIDE;
155 virtual void OnOnlineChecked(
156 const std::string& username, bool success) OVERRIDE;
158 // LoginUtils::Delegate implementation:
159 virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
161 // Called when device settings change.
162 void DeviceSettingsChanged();
164 // Starts WizardController with the specified screen.
165 void ActivateWizard(const std::string& screen_name);
167 // Returns corresponding native window.
168 gfx::NativeWindow GetNativeWindow() const;
170 // Adds first-time login URLs.
171 void InitializeStartUrls() const;
173 // Show error message. |error_id| error message ID in resources.
174 // If |details| string is not empty, it specify additional error text
175 // provided by authenticator, it is not localized.
176 void ShowError(int error_id, const std::string& details);
178 // Shows Gaia page because password change was detected.
179 void ShowGaiaPasswordChanged(const std::string& username);
181 // Handles result of ownership check and starts enterprise or kiosk enrollment
183 void OnEnrollmentOwnershipCheckCompleted(
184 DeviceSettingsService::OwnershipStatus status);
186 // Handles result of consumer kiosk configurability check and starts
187 // enable kiosk screen if applicable.
188 void OnConsumerKioskAutoLaunchCheckCompleted(
189 KioskAppManager::ConsumerKioskAutoLaunchStatus status);
191 // Enters the enterprise enrollment screen. |forced| is true if this is the
192 // result of an auto-enrollment check, and the user shouldn't be able to
193 // easily cancel the enrollment. In that case, |user| is the user name that
195 void ShowEnrollmentScreen(bool forced, const std::string& user);
197 // Shows "reset device" screen.
198 void ShowResetScreen();
200 // Shows kiosk feature enable screen.
201 void ShowKioskEnableScreen();
203 // Shows "kiosk auto-launch permission" screen.
204 void ShowKioskAutolaunchScreen();
206 // Shows "critical TPM error" screen.
209 // Invoked to complete login. Login might be suspended if auto-enrollment
210 // has to be performed, and will resume once auto-enrollment completes.
211 void CompleteLoginInternal(
212 const UserContext& user_context,
213 DeviceSettingsService::OwnershipStatus ownership_status);
215 // Creates |login_performer_| if necessary and calls login() on it.
216 // The string arguments aren't passed by const reference because this is
217 // posted as |resume_login_callback_| and resets it.
218 void PerformLogin(const UserContext& user_context,
219 LoginPerformer::AuthorizationMode auth_mode);
221 // Updates the |login_display_| attached to this controller.
222 void UpdateLoginDisplay(const user_manager::UserList& users);
224 // Sends an accessibility alert event to extension listeners.
225 void SendAccessibilityAlert(const std::string& alert_text);
227 // Callback invoked when the keyboard layouts available for a public session
228 // have been retrieved. Selects the first layout from the list and continues
230 void SetPublicSessionKeyboardLayoutAndLogin(
231 const UserContext& user_context,
232 scoped_ptr<base::ListValue> keyboard_layouts);
234 // Starts the actual login process for a public session. Invoked when all
235 // preconditions have been verified.
236 void LoginAsPublicSessionInternal(const UserContext& user_context);
238 // Public session auto-login timer.
239 scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_;
241 // Public session auto-login timeout, in milliseconds.
242 int public_session_auto_login_delay_;
244 // Username for public session auto-login.
245 std::string public_session_auto_login_username_;
247 // Used to execute login operations.
248 scoped_ptr<LoginPerformer> login_performer_;
250 // Delegate to forward all authentication status events to.
251 // Tests can use this to receive authentication status events.
252 AuthStatusConsumer* auth_status_consumer_;
254 // Username of the last login attempt.
255 std::string last_login_attempt_username_;
257 // OOBE/login display host.
258 LoginDisplayHost* host_;
260 // Login UI implementation instance.
261 scoped_ptr<LoginDisplay> login_display_;
263 // Number of login attempts. Used to show help link when > 1 unsuccessful
264 // logins for the same user.
265 size_t num_login_attempts_;
267 // Pointer to the current instance of the controller to be used by
269 static ExistingUserController* current_controller_;
271 // Interface to the signed settings store.
272 CrosSettings* cros_settings_;
274 // URL to append to start Guest mode with.
275 GURL guest_mode_url_;
277 // Used for notifications during the login process.
278 content::NotificationRegistrar registrar_;
280 // Factory of callbacks.
281 base::WeakPtrFactory<ExistingUserController> weak_factory_;
283 // The displayed email for the next login attempt set by |SetDisplayEmail|.
284 std::string display_email_;
286 // Whether offline login attempt failed.
287 bool offline_failed_;
289 // Whether login attempt is running.
290 bool is_login_in_progress_;
292 // Whether online login attempt succeeded.
293 std::string online_succeeded_for_;
295 // True if password has been changed for user who is completing sign in.
296 // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
297 bool password_changed_;
299 // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
300 // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
301 LoginPerformer::AuthorizationMode auth_mode_;
303 // True if auto-enrollment should be performed before starting the user's
305 bool do_auto_enrollment_;
307 // Whether the sign-in UI is finished loading.
308 bool signin_screen_ready_;
310 // The username used for auto-enrollment, if it was triggered.
311 std::string auto_enrollment_username_;
313 // Callback to invoke to resume login, after auto-enrollment has completed.
314 base::Closure resume_login_callback_;
316 // Time when the signin screen was first displayed. Used to measure the time
317 // from showing the screen until a successful login is performed.
318 base::Time time_init_;
320 // Timer for the interval to wait for the reboot after TPM error UI was shown.
321 base::OneShotTimer<ExistingUserController> reboot_timer_;
323 scoped_ptr<login::NetworkStateHelper> network_state_helper_;
325 scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_;
326 scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
327 scoped_ptr<CrosSettings::ObserverSubscription>
328 allow_supervised_user_subscription_;
329 scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
330 scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_;
331 scoped_ptr<CrosSettings::ObserverSubscription>
332 local_account_auto_login_id_subscription_;
333 scoped_ptr<CrosSettings::ObserverSubscription>
334 local_account_auto_login_delay_subscription_;
336 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin);
338 DISALLOW_COPY_AND_ASSIGN(ExistingUserController);
341 } // namespace chromeos
343 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_