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/session/user_session_manager.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/geometry/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 UserSessionManagerDelegate
{
57 // All UI initialization is deferred till Init() call.
58 explicit ExistingUserController(LoginDisplayHost
* host
);
59 ~ExistingUserController() override
;
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 // Start the public session auto-login timer.
70 void StartPublicSessionAutoLoginTimer();
72 // Stop the public session auto-login timer when a login attempt begins.
73 void StopPublicSessionAutoLoginTimer();
75 // LoginDisplay::Delegate: implementation
76 void CancelPasswordChangedFlow() override
;
77 void CreateAccount() override
;
78 void CompleteLogin(const UserContext
& user_context
) override
;
79 base::string16
GetConnectedNetworkName() override
;
80 bool IsSigninInProgress() const override
;
81 void Login(const UserContext
& user_context
,
82 const SigninSpecifics
& specifics
) override
;
83 void MigrateUserData(const std::string
& old_password
) override
;
84 void OnSigninScreenReady() override
;
85 void OnStartEnterpriseEnrollment() override
;
86 void OnStartEnableDebuggingScreen() override
;
87 void OnStartKioskEnableScreen() override
;
88 void OnStartKioskAutolaunchScreen() override
;
89 void ResetPublicSessionAutoLoginTimer() override
;
90 void ResyncUserData() override
;
91 void SetDisplayEmail(const std::string
& email
) override
;
92 void ShowWrongHWIDScreen() override
;
93 void Signout() override
;
95 // content::NotificationObserver implementation.
96 void Observe(int type
,
97 const content::NotificationSource
& source
,
98 const content::NotificationDetails
& details
) override
;
100 // Set a delegate that we will pass AuthStatusConsumer events to.
102 void set_login_status_consumer(AuthStatusConsumer
* consumer
) {
103 auth_status_consumer_
= consumer
;
106 // Returns the LoginDisplay created and owned by this controller.
108 LoginDisplay
* login_display() {
109 return login_display_
.get();
112 // Returns the LoginDisplayHost for this controller.
113 LoginDisplayHost
* login_display_host() {
117 // Returns value of LoginPerformer::auth_mode() (cached if performer is
119 LoginPerformer::AuthorizationMode
auth_mode() const;
121 // Returns value of LoginPerformer::password_changed() (cached if performer is
123 bool password_changed() const;
126 friend class ExistingUserControllerTest
;
127 friend class ExistingUserControllerAutoLoginTest
;
128 friend class ExistingUserControllerPublicSessionTest
;
129 friend class MockLoginPerformerDelegate
;
132 void LoginAsPublicSession(const UserContext
& user_context
);
133 void LoginAsKioskApp(const std::string
& app_id
, bool diagnostic_mode
);
135 // Retrieve public session auto-login policy and update the timer.
136 void ConfigurePublicSessionAutoLogin();
138 // Trigger public session auto-login.
139 void OnPublicSessionAutoLoginTimerFire();
141 // LoginPerformer::Delegate implementation:
142 void OnAuthFailure(const AuthFailure
& error
) override
;
143 void OnAuthSuccess(const UserContext
& user_context
) override
;
144 void OnOffTheRecordAuthSuccess() override
;
145 void OnPasswordChangeDetected() override
;
146 void WhiteListCheckFailed(const std::string
& email
) override
;
147 void PolicyLoadFailed() override
;
148 void OnOnlineChecked(const std::string
& username
, bool success
) override
;
150 // UserSessionManagerDelegate implementation:
151 void OnProfilePrepared(Profile
* profile
, bool browser_launched
) override
;
153 // Called when device settings change.
154 void DeviceSettingsChanged();
156 // Returns corresponding native window.
157 gfx::NativeWindow
GetNativeWindow() const;
159 // Show error message. |error_id| error message ID in resources.
160 // If |details| string is not empty, it specify additional error text
161 // provided by authenticator, it is not localized.
162 void ShowError(int error_id
, const std::string
& details
);
164 // Shows Gaia page because password change was detected.
165 void ShowGaiaPasswordChanged(const std::string
& username
);
167 // Handles result of ownership check and starts enterprise or kiosk enrollment
169 void OnEnrollmentOwnershipCheckCompleted(
170 DeviceSettingsService::OwnershipStatus status
);
172 // Handles result of consumer kiosk configurability check and starts
173 // enable kiosk screen if applicable.
174 void OnConsumerKioskAutoLaunchCheckCompleted(
175 KioskAppManager::ConsumerKioskAutoLaunchStatus status
);
177 // Enters the enterprise enrollment screen.
178 void ShowEnrollmentScreen();
180 // Shows "reset device" screen.
181 void ShowResetScreen();
183 // Shows "enable developer features" screen.
184 void ShowEnableDebuggingScreen();
186 // Shows kiosk feature enable screen.
187 void ShowKioskEnableScreen();
189 // Shows "kiosk auto-launch permission" screen.
190 void ShowKioskAutolaunchScreen();
192 // Shows "critical TPM error" screen.
195 // Creates |login_performer_| if necessary and calls login() on it.
196 void PerformLogin(const UserContext
& user_context
,
197 LoginPerformer::AuthorizationMode auth_mode
);
199 // Updates the |login_display_| attached to this controller.
200 void UpdateLoginDisplay(const user_manager::UserList
& users
);
202 // Sends an accessibility alert event to extension listeners.
203 void SendAccessibilityAlert(const std::string
& alert_text
);
205 // Callback invoked when the keyboard layouts available for a public session
206 // have been retrieved. Selects the first layout from the list and continues
208 void SetPublicSessionKeyboardLayoutAndLogin(
209 const UserContext
& user_context
,
210 scoped_ptr
<base::ListValue
> keyboard_layouts
);
212 // Starts the actual login process for a public session. Invoked when all
213 // preconditions have been verified.
214 void LoginAsPublicSessionInternal(const UserContext
& user_context
);
216 // Performs sets of actions right prior to login has been started.
217 void PerformPreLoginActions(const UserContext
& user_context
);
219 // Performs set of actions when login has been completed or has been
220 // cancelled. If |start_public_session_timer| is true than public session
221 // auto-login timer is started.
222 void PerformLoginFinishedActions(bool start_public_session_timer
);
224 // Invokes |continuation| after verifying that the device is not disabled.
225 void ContinueLoginIfDeviceNotDisabled(const base::Closure
& continuation
);
227 // Signs in as a new user. This is a continuation of CompleteLogin() that gets
228 // invoked after it has been verified that the device is not disabled.
229 void DoCompleteLogin(const UserContext
& user_context
);
231 // Signs in as a known user. This is a continuation of Login() that gets
232 // invoked after it has been verified that the device is not disabled.
233 void DoLogin(const UserContext
& user_context
,
234 const SigninSpecifics
& specifics
);
236 // Public session auto-login timer.
237 scoped_ptr
<base::OneShotTimer
<ExistingUserController
> > auto_login_timer_
;
239 // Public session auto-login timeout, in milliseconds.
240 int public_session_auto_login_delay_
;
242 // Username for public session auto-login.
243 std::string public_session_auto_login_username_
;
245 // Used to execute login operations.
246 scoped_ptr
<LoginPerformer
> login_performer_
;
248 // Delegate to forward all authentication status events to.
249 // Tests can use this to receive authentication status events.
250 AuthStatusConsumer
* auth_status_consumer_
;
252 // Username of the last login attempt.
253 std::string last_login_attempt_username_
;
255 // OOBE/login display host.
256 LoginDisplayHost
* host_
;
258 // Login UI implementation instance.
259 scoped_ptr
<LoginDisplay
> login_display_
;
261 // Number of login attempts. Used to show help link when > 1 unsuccessful
262 // logins for the same user.
263 size_t num_login_attempts_
;
265 // Pointer to the current instance of the controller to be used by
267 static ExistingUserController
* current_controller_
;
269 // Interface to the signed settings store.
270 CrosSettings
* cros_settings_
;
272 // URL to append to start Guest mode with.
273 GURL guest_mode_url_
;
275 // Used for notifications during the login process.
276 content::NotificationRegistrar registrar_
;
278 // The displayed email for the next login attempt set by |SetDisplayEmail|.
279 std::string display_email_
;
281 // Whether offline login attempt failed.
282 bool offline_failed_
;
284 // Whether login attempt is running.
285 bool is_login_in_progress_
;
287 // Whether online login attempt succeeded.
288 std::string online_succeeded_for_
;
290 // True if password has been changed for user who is completing sign in.
291 // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
292 bool password_changed_
;
294 // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
295 // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
296 LoginPerformer::AuthorizationMode auth_mode_
;
297 // Whether the sign-in UI is finished loading.
298 bool signin_screen_ready_
;
300 // Time when the signin screen was first displayed. Used to measure the time
301 // from showing the screen until a successful login is performed.
302 base::Time time_init_
;
304 // Timer for the interval to wait for the reboot after TPM error UI was shown.
305 base::OneShotTimer
<ExistingUserController
> reboot_timer_
;
307 scoped_ptr
<login::NetworkStateHelper
> network_state_helper_
;
309 scoped_ptr
<CrosSettings::ObserverSubscription
> show_user_names_subscription_
;
310 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_new_user_subscription_
;
311 scoped_ptr
<CrosSettings::ObserverSubscription
>
312 allow_supervised_user_subscription_
;
313 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_guest_subscription_
;
314 scoped_ptr
<CrosSettings::ObserverSubscription
> users_subscription_
;
315 scoped_ptr
<CrosSettings::ObserverSubscription
>
316 local_account_auto_login_id_subscription_
;
317 scoped_ptr
<CrosSettings::ObserverSubscription
>
318 local_account_auto_login_delay_subscription_
;
320 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest
, ExistingUserLogin
);
322 // Factory of callbacks.
323 base::WeakPtrFactory
<ExistingUserController
> weak_factory_
;
325 DISALLOW_COPY_AND_ASSIGN(ExistingUserController
);
328 } // namespace chromeos
330 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_