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/login_utils.h"
21 #include "chrome/browser/chromeos/login/ui/login_display.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chrome/browser/chromeos/settings/device_settings_service.h"
24 #include "chromeos/login/auth/login_performer.h"
25 #include "chromeos/login/auth/user_context.h"
26 #include "components/user_manager/user.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "ui/gfx/rect.h"
39 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 // content::NotificationObserver implementation.
102 virtual void Observe(int type,
103 const content::NotificationSource& source,
104 const content::NotificationDetails& details) override;
106 // Set a delegate that we will pass AuthStatusConsumer events to.
108 void set_login_status_consumer(AuthStatusConsumer* consumer) {
109 auth_status_consumer_ = consumer;
112 // Returns the LoginDisplay created and owned by this controller.
114 LoginDisplay* login_display() {
115 return login_display_.get();
118 // Returns the LoginDisplayHost for this controller.
119 LoginDisplayHost* login_display_host() {
123 // Returns value of LoginPerformer::auth_mode() (cached if performer is
125 LoginPerformer::AuthorizationMode auth_mode() const;
127 // Returns value of LoginPerformer::password_changed() (cached if performer is
129 bool password_changed() const;
132 friend class ExistingUserControllerTest;
133 friend class ExistingUserControllerAutoLoginTest;
134 friend class ExistingUserControllerPublicSessionTest;
135 friend class MockLoginPerformerDelegate;
137 void LoginAsRetailModeUser();
139 void LoginAsPublicSession(const UserContext& user_context);
140 void LoginAsKioskApp(const std::string& app_id, bool diagnostic_mode);
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,
160 bool browser_launched) override;
162 // Called when device settings change.
163 void DeviceSettingsChanged();
165 // Returns corresponding native window.
166 gfx::NativeWindow GetNativeWindow() const;
168 // Show error message. |error_id| error message ID in resources.
169 // If |details| string is not empty, it specify additional error text
170 // provided by authenticator, it is not localized.
171 void ShowError(int error_id, const std::string& details);
173 // Shows Gaia page because password change was detected.
174 void ShowGaiaPasswordChanged(const std::string& username);
176 // Handles result of ownership check and starts enterprise or kiosk enrollment
178 void OnEnrollmentOwnershipCheckCompleted(
179 DeviceSettingsService::OwnershipStatus status);
181 // Handles result of consumer kiosk configurability check and starts
182 // enable kiosk screen if applicable.
183 void OnConsumerKioskAutoLaunchCheckCompleted(
184 KioskAppManager::ConsumerKioskAutoLaunchStatus status);
186 // Enters the enterprise enrollment screen. |forced| is true if this is the
187 // result of an auto-enrollment check, and the user shouldn't be able to
188 // easily cancel the enrollment. In that case, |user| is the user name that
190 void ShowEnrollmentScreen(bool forced, const std::string& user);
192 // Shows "reset device" screen.
193 void ShowResetScreen();
195 // Shows kiosk feature enable screen.
196 void ShowKioskEnableScreen();
198 // Shows "kiosk auto-launch permission" screen.
199 void ShowKioskAutolaunchScreen();
201 // Shows "critical TPM error" screen.
204 // Invoked to complete login. Login might be suspended if auto-enrollment
205 // has to be performed, and will resume once auto-enrollment completes.
206 void CompleteLoginInternal(
207 const UserContext& user_context,
208 DeviceSettingsService::OwnershipStatus ownership_status);
210 // Creates |login_performer_| if necessary and calls login() on it.
211 // The string arguments aren't passed by const reference because this is
212 // posted as |resume_login_callback_| and resets it.
213 void PerformLogin(const UserContext& user_context,
214 LoginPerformer::AuthorizationMode auth_mode);
216 // Updates the |login_display_| attached to this controller.
217 void UpdateLoginDisplay(const user_manager::UserList& users);
219 // Sends an accessibility alert event to extension listeners.
220 void SendAccessibilityAlert(const std::string& alert_text);
222 // Callback invoked when the keyboard layouts available for a public session
223 // have been retrieved. Selects the first layout from the list and continues
225 void SetPublicSessionKeyboardLayoutAndLogin(
226 const UserContext& user_context,
227 scoped_ptr<base::ListValue> keyboard_layouts);
229 // Starts the actual login process for a public session. Invoked when all
230 // preconditions have been verified.
231 void LoginAsPublicSessionInternal(const UserContext& user_context);
233 // Performs sets of actions right prior to login has been started.
234 void PerformPreLoginActions(const UserContext& user_context);
236 // Performs set of actions when login has been completed or has been
237 // cancelled. If |start_public_session_timer| is true than public session
238 // auto-login timer is started.
239 void PerformLoginFinishedActions(bool start_public_session_timer);
241 // Public session auto-login timer.
242 scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_;
244 // Public session auto-login timeout, in milliseconds.
245 int public_session_auto_login_delay_;
247 // Username for public session auto-login.
248 std::string public_session_auto_login_username_;
250 // Used to execute login operations.
251 scoped_ptr<LoginPerformer> login_performer_;
253 // Delegate to forward all authentication status events to.
254 // Tests can use this to receive authentication status events.
255 AuthStatusConsumer* auth_status_consumer_;
257 // Username of the last login attempt.
258 std::string last_login_attempt_username_;
260 // Auth flow of the last login attempt.
261 UserContext::AuthFlow last_login_attempt_auth_flow_;
263 // OOBE/login display host.
264 LoginDisplayHost* host_;
266 // Login UI implementation instance.
267 scoped_ptr<LoginDisplay> login_display_;
269 // Number of login attempts. Used to show help link when > 1 unsuccessful
270 // logins for the same user.
271 size_t num_login_attempts_;
273 // Pointer to the current instance of the controller to be used by
275 static ExistingUserController* current_controller_;
277 // Interface to the signed settings store.
278 CrosSettings* cros_settings_;
280 // URL to append to start Guest mode with.
281 GURL guest_mode_url_;
283 // Used for notifications during the login process.
284 content::NotificationRegistrar registrar_;
286 // The displayed email for the next login attempt set by |SetDisplayEmail|.
287 std::string display_email_;
289 // Whether offline login attempt failed.
290 bool offline_failed_;
292 // Whether login attempt is running.
293 bool is_login_in_progress_;
295 // Whether online login attempt succeeded.
296 std::string online_succeeded_for_;
298 // True if password has been changed for user who is completing sign in.
299 // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
300 bool password_changed_;
302 // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
303 // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
304 LoginPerformer::AuthorizationMode auth_mode_;
306 // True if auto-enrollment should be performed before starting the user's
308 bool do_auto_enrollment_;
310 // Whether the sign-in UI is finished loading.
311 bool signin_screen_ready_;
313 // The username used for auto-enrollment, if it was triggered.
314 std::string auto_enrollment_username_;
316 // Callback to invoke to resume login, after auto-enrollment has completed.
317 base::Closure resume_login_callback_;
319 // Time when the signin screen was first displayed. Used to measure the time
320 // from showing the screen until a successful login is performed.
321 base::Time time_init_;
323 // Timer for the interval to wait for the reboot after TPM error UI was shown.
324 base::OneShotTimer<ExistingUserController> reboot_timer_;
326 scoped_ptr<login::NetworkStateHelper> network_state_helper_;
328 scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_;
329 scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
330 scoped_ptr<CrosSettings::ObserverSubscription>
331 allow_supervised_user_subscription_;
332 scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
333 scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_;
334 scoped_ptr<CrosSettings::ObserverSubscription>
335 local_account_auto_login_id_subscription_;
336 scoped_ptr<CrosSettings::ObserverSubscription>
337 local_account_auto_login_delay_subscription_;
339 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin);
341 // Factory of callbacks.
342 base::WeakPtrFactory<ExistingUserController> weak_factory_;
344 DISALLOW_COPY_AND_ASSIGN(ExistingUserController);
347 } // namespace chromeos
349 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_