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"
38 class BootstrapUserContextInitializer
;
40 class LoginDisplayHost
;
41 class OAuth2TokenInitializer
;
44 class NetworkStateHelper
;
47 // ExistingUserController is used to handle login when someone has
48 // already logged into the machine.
49 // To use ExistingUserController create an instance of it and invoke Init.
50 // When Init is called it creates LoginDisplay instance which encapsulates
51 // all login UI implementation.
52 // ExistingUserController maintains it's own life cycle and deletes itself when
53 // the user logs in (or chooses to see other settings).
54 class ExistingUserController
: public LoginDisplay::Delegate
,
55 public content::NotificationObserver
,
56 public LoginPerformer::Delegate
,
57 public UserSessionManagerDelegate
{
59 // All UI initialization is deferred till Init() call.
60 explicit ExistingUserController(LoginDisplayHost
* host
);
61 ~ExistingUserController() override
;
63 // Returns the current existing user controller if it has been created.
64 static ExistingUserController
* current_controller() {
65 return current_controller_
;
68 // Creates and shows login UI for known users.
69 void Init(const user_manager::UserList
& users
);
71 // Start the public session auto-login timer.
72 void StartPublicSessionAutoLoginTimer();
74 // Stop the public session auto-login timer when a login attempt begins.
75 void StopPublicSessionAutoLoginTimer();
77 // LoginDisplay::Delegate: implementation
78 void CancelPasswordChangedFlow() override
;
79 void CreateAccount() override
;
80 void CompleteLogin(const UserContext
& user_context
) override
;
81 base::string16
GetConnectedNetworkName() override
;
82 bool IsSigninInProgress() const override
;
83 void Login(const UserContext
& user_context
,
84 const SigninSpecifics
& specifics
) override
;
85 void MigrateUserData(const std::string
& old_password
) override
;
86 void OnSigninScreenReady() override
;
87 void OnStartEnterpriseEnrollment() override
;
88 void OnStartEnableDebuggingScreen() override
;
89 void OnStartKioskEnableScreen() override
;
90 void OnStartKioskAutolaunchScreen() override
;
91 void ResetPublicSessionAutoLoginTimer() override
;
92 void ResyncUserData() override
;
93 void SetDisplayEmail(const std::string
& email
) override
;
94 void ShowWrongHWIDScreen() override
;
95 void Signout() override
;
96 bool IsUserWhitelisted(const std::string
& user_id
) override
;
98 // content::NotificationObserver implementation.
99 void Observe(int type
,
100 const content::NotificationSource
& source
,
101 const content::NotificationDetails
& details
) override
;
103 // Set a delegate that we will pass AuthStatusConsumer events to.
105 void set_login_status_consumer(AuthStatusConsumer
* consumer
) {
106 auth_status_consumer_
= consumer
;
109 // Returns the LoginDisplay created and owned by this controller.
111 LoginDisplay
* login_display() {
112 return login_display_
.get();
115 // Returns the LoginDisplayHost for this controller.
116 LoginDisplayHost
* login_display_host() {
120 // Returns value of LoginPerformer::auth_mode() (cached if performer is
122 LoginPerformer::AuthorizationMode
auth_mode() const;
124 // Returns value of LoginPerformer::password_changed() (cached if performer is
126 bool password_changed() const;
129 friend class ExistingUserControllerTest
;
130 friend class ExistingUserControllerAutoLoginTest
;
131 friend class ExistingUserControllerPublicSessionTest
;
132 friend class MockLoginPerformerDelegate
;
135 void LoginAsPublicSession(const UserContext
& user_context
);
136 void LoginAsKioskApp(const std::string
& app_id
, bool diagnostic_mode
);
138 // Retrieve public session auto-login policy and update the timer.
139 void ConfigurePublicSessionAutoLogin();
141 // Trigger public session auto-login.
142 void OnPublicSessionAutoLoginTimerFire();
144 // LoginPerformer::Delegate implementation:
145 void OnAuthFailure(const AuthFailure
& error
) override
;
146 void OnAuthSuccess(const UserContext
& user_context
) override
;
147 void OnOffTheRecordAuthSuccess() override
;
148 void OnPasswordChangeDetected() override
;
149 void WhiteListCheckFailed(const std::string
& email
) override
;
150 void PolicyLoadFailed() override
;
151 void OnOnlineChecked(const std::string
& username
, bool success
) override
;
153 // UserSessionManagerDelegate implementation:
154 void OnProfilePrepared(Profile
* profile
, bool browser_launched
) override
;
156 // Called when device settings change.
157 void DeviceSettingsChanged();
159 // Returns corresponding native window.
160 gfx::NativeWindow
GetNativeWindow() const;
162 // Show error message. |error_id| error message ID in resources.
163 // If |details| string is not empty, it specify additional error text
164 // provided by authenticator, it is not localized.
165 void ShowError(int error_id
, const std::string
& details
);
167 // Shows Gaia page because password change was detected.
168 void ShowGaiaPasswordChanged(const std::string
& username
);
170 // Handles result of ownership check and starts enterprise or kiosk enrollment
172 void OnEnrollmentOwnershipCheckCompleted(
173 DeviceSettingsService::OwnershipStatus status
);
175 // Handles result of consumer kiosk configurability check and starts
176 // enable kiosk screen if applicable.
177 void OnConsumerKioskAutoLaunchCheckCompleted(
178 KioskAppManager::ConsumerKioskAutoLaunchStatus status
);
180 // Enters the enterprise enrollment screen.
181 void ShowEnrollmentScreen();
183 // Shows "reset device" screen.
184 void ShowResetScreen();
186 // Shows "enable developer features" screen.
187 void ShowEnableDebuggingScreen();
189 // Shows kiosk feature enable screen.
190 void ShowKioskEnableScreen();
192 // Shows "kiosk auto-launch permission" screen.
193 void ShowKioskAutolaunchScreen();
195 // Shows "critical TPM error" screen.
198 // Creates |login_performer_| if necessary and calls login() on it.
199 void PerformLogin(const UserContext
& user_context
,
200 LoginPerformer::AuthorizationMode auth_mode
);
202 // Updates the |login_display_| attached to this controller.
203 void UpdateLoginDisplay(const user_manager::UserList
& users
);
205 // Sends an accessibility alert event to extension listeners.
206 void SendAccessibilityAlert(const std::string
& alert_text
);
208 // Callback invoked when the keyboard layouts available for a public session
209 // have been retrieved. Selects the first layout from the list and continues
211 void SetPublicSessionKeyboardLayoutAndLogin(
212 const UserContext
& user_context
,
213 scoped_ptr
<base::ListValue
> keyboard_layouts
);
215 // Starts the actual login process for a public session. Invoked when all
216 // preconditions have been verified.
217 void LoginAsPublicSessionInternal(const UserContext
& user_context
);
219 // Performs sets of actions right prior to login has been started.
220 void PerformPreLoginActions(const UserContext
& user_context
);
222 // Performs set of actions when login has been completed or has been
223 // cancelled. If |start_public_session_timer| is true than public session
224 // auto-login timer is started.
225 void PerformLoginFinishedActions(bool start_public_session_timer
);
227 // Invokes |continuation| after verifying that the device is not disabled.
228 void ContinueLoginIfDeviceNotDisabled(const base::Closure
& continuation
);
230 // Signs in as a new user. This is a continuation of CompleteLogin() that gets
231 // invoked after it has been verified that the device is not disabled.
232 void DoCompleteLogin(const UserContext
& user_context
);
234 // Signs in as a known user. This is a continuation of Login() that gets
235 // invoked after it has been verified that the device is not disabled.
236 void DoLogin(const UserContext
& user_context
,
237 const SigninSpecifics
& specifics
);
239 // Callback invoked when |bootstrap_user_context_initializer_| has finished.
240 void OnBootstrapUserContextInitialized(bool success
,
241 const UserContext
& user_context
);
243 // Callback invoked when |oauth2_token_initializer_| has finished.
244 void OnOAuth2TokensFetched(bool success
, const UserContext
& user_context
);
246 // Public session auto-login timer.
247 scoped_ptr
<base::OneShotTimer
<ExistingUserController
> > auto_login_timer_
;
249 // Public session auto-login timeout, in milliseconds.
250 int public_session_auto_login_delay_
;
252 // Username for public session auto-login.
253 std::string public_session_auto_login_username_
;
255 // Used to execute login operations.
256 scoped_ptr
<LoginPerformer
> login_performer_
;
258 // Delegate to forward all authentication status events to.
259 // Tests can use this to receive authentication status events.
260 AuthStatusConsumer
* auth_status_consumer_
;
262 // Username of the last login attempt.
263 std::string last_login_attempt_username_
;
265 // OOBE/login display host.
266 LoginDisplayHost
* host_
;
268 // Login UI implementation instance.
269 scoped_ptr
<LoginDisplay
> login_display_
;
271 // Number of login attempts. Used to show help link when > 1 unsuccessful
272 // logins for the same user.
273 size_t num_login_attempts_
;
275 // Pointer to the current instance of the controller to be used by
277 static ExistingUserController
* current_controller_
;
279 // Interface to the signed settings store.
280 CrosSettings
* cros_settings_
;
282 // URL to append to start Guest mode with.
283 GURL guest_mode_url_
;
285 // Used for notifications during the login process.
286 content::NotificationRegistrar registrar_
;
288 // The displayed email for the next login attempt set by |SetDisplayEmail|.
289 std::string display_email_
;
291 // Whether offline login attempt failed.
292 bool offline_failed_
;
294 // Whether login attempt is running.
295 bool is_login_in_progress_
;
297 // Whether online login attempt succeeded.
298 std::string online_succeeded_for_
;
300 // True if password has been changed for user who is completing sign in.
301 // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
302 bool password_changed_
;
304 // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
305 // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
306 LoginPerformer::AuthorizationMode auth_mode_
;
307 // Whether the sign-in UI is finished loading.
308 bool signin_screen_ready_
;
310 // Time when the signin screen was first displayed. Used to measure the time
311 // from showing the screen until a successful login is performed.
312 base::Time time_init_
;
314 // Timer for the interval to wait for the reboot after TPM error UI was shown.
315 base::OneShotTimer
<ExistingUserController
> reboot_timer_
;
317 scoped_ptr
<login::NetworkStateHelper
> network_state_helper_
;
319 scoped_ptr
<CrosSettings::ObserverSubscription
> show_user_names_subscription_
;
320 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_new_user_subscription_
;
321 scoped_ptr
<CrosSettings::ObserverSubscription
>
322 allow_supervised_user_subscription_
;
323 scoped_ptr
<CrosSettings::ObserverSubscription
> allow_guest_subscription_
;
324 scoped_ptr
<CrosSettings::ObserverSubscription
> users_subscription_
;
325 scoped_ptr
<CrosSettings::ObserverSubscription
>
326 local_account_auto_login_id_subscription_
;
327 scoped_ptr
<CrosSettings::ObserverSubscription
>
328 local_account_auto_login_delay_subscription_
;
330 scoped_ptr
<BootstrapUserContextInitializer
>
331 bootstrap_user_context_initializer_
;
333 scoped_ptr
<OAuth2TokenInitializer
> oauth2_token_initializer_
;
335 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest
, ExistingUserLogin
);
337 // Factory of callbacks.
338 base::WeakPtrFactory
<ExistingUserController
> weak_factory_
;
340 DISALLOW_COPY_AND_ASSIGN(ExistingUserController
);
343 } // namespace chromeos
345 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_