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
;
152 // UserSessionManagerDelegate implementation:
153 void OnProfilePrepared(Profile
* profile
, bool browser_launched
) override
;
155 // Called when device settings change.
156 void DeviceSettingsChanged();
158 // Returns corresponding native window.
159 gfx::NativeWindow
GetNativeWindow() const;
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
);
166 // Handles result of ownership check and starts enterprise or kiosk enrollment
168 void OnEnrollmentOwnershipCheckCompleted(
169 DeviceSettingsService::OwnershipStatus status
);
171 // Handles result of consumer kiosk configurability check and starts
172 // enable kiosk screen if applicable.
173 void OnConsumerKioskAutoLaunchCheckCompleted(
174 KioskAppManager::ConsumerKioskAutoLaunchStatus status
);
176 // Enters the enterprise enrollment screen.
177 void ShowEnrollmentScreen();
179 // Shows "reset device" screen.
180 void ShowResetScreen();
182 // Shows "enable developer features" screen.
183 void ShowEnableDebuggingScreen();
185 // Shows kiosk feature enable screen.
186 void ShowKioskEnableScreen();
188 // Shows "kiosk auto-launch permission" screen.
189 void ShowKioskAutolaunchScreen();
191 // Shows "critical TPM error" screen.
194 // Creates |login_performer_| if necessary and calls login() on it.
195 void PerformLogin(const UserContext
& user_context
,
196 LoginPerformer::AuthorizationMode auth_mode
);
198 // Updates the |login_display_| attached to this controller.
199 void UpdateLoginDisplay(const user_manager::UserList
& users
);
201 // Sends an accessibility alert event to extension listeners.
202 void SendAccessibilityAlert(const std::string
& alert_text
);
204 // Callback invoked when the keyboard layouts available for a public session
205 // have been retrieved. Selects the first layout from the list and continues
207 void SetPublicSessionKeyboardLayoutAndLogin(
208 const UserContext
& user_context
,
209 scoped_ptr
<base::ListValue
> keyboard_layouts
);
211 // Starts the actual login process for a public session. Invoked when all
212 // preconditions have been verified.
213 void LoginAsPublicSessionInternal(const UserContext
& user_context
);
215 // Performs sets of actions right prior to login has been started.
216 void PerformPreLoginActions(const UserContext
& user_context
);
218 // Performs set of actions when login has been completed or has been
219 // cancelled. If |start_public_session_timer| is true than public session
220 // auto-login timer is started.
221 void PerformLoginFinishedActions(bool start_public_session_timer
);
223 // Invokes |continuation| after verifying that the device is not disabled.
224 void ContinueLoginIfDeviceNotDisabled(const base::Closure
& continuation
);
226 // Signs in as a new user. This is a continuation of CompleteLogin() that gets
227 // invoked after it has been verified that the device is not disabled.
228 void DoCompleteLogin(const UserContext
& user_context
);
230 // Signs in as a known user. This is a continuation of Login() that gets
231 // invoked after it has been verified that the device is not disabled.
232 void DoLogin(const UserContext
& user_context
,
233 const SigninSpecifics
& specifics
);
235 // Callback invoked when |bootstrap_user_context_initializer_| has finished.
236 void OnBootstrapUserContextInitialized(bool success
,
237 const UserContext
& user_context
);
239 // Callback invoked when |oauth2_token_initializer_| has finished.
240 void OnOAuth2TokensFetched(bool success
, const UserContext
& user_context
);
242 // Public session auto-login timer.
243 scoped_ptr
<base::OneShotTimer
<ExistingUserController
> > auto_login_timer_
;
245 // Public session auto-login timeout, in milliseconds.
246 int public_session_auto_login_delay_
;
248 // Username for public session auto-login.
249 std::string public_session_auto_login_username_
;
251 // Used to execute login operations.
252 scoped_ptr
<LoginPerformer
> login_performer_
;
254 // Delegate to forward all authentication status events to.
255 // Tests can use this to receive authentication status events.
256 AuthStatusConsumer
* auth_status_consumer_
;
258 // Username of the last login attempt.
259 std::string last_login_attempt_username_
;
261 // OOBE/login display host.
262 LoginDisplayHost
* host_
;
264 // Login UI implementation instance.
265 scoped_ptr
<LoginDisplay
> login_display_
;
267 // Number of login attempts. Used to show help link when > 1 unsuccessful
268 // logins for the same user.
269 size_t num_login_attempts_
;
271 // Pointer to the current instance of the controller to be used by
273 static ExistingUserController
* current_controller_
;
275 // Interface to the signed settings store.
276 CrosSettings
* cros_settings_
;
278 // URL to append to start Guest mode with.
279 GURL guest_mode_url_
;
281 // Used for notifications during the login process.
282 content::NotificationRegistrar registrar_
;
284 // The displayed email for the next login attempt set by |SetDisplayEmail|.
285 std::string display_email_
;
287 // Whether login attempt is running.
288 bool is_login_in_progress_
;
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 scoped_ptr
<BootstrapUserContextInitializer
>
321 bootstrap_user_context_initializer_
;
323 scoped_ptr
<OAuth2TokenInitializer
> oauth2_token_initializer_
;
325 FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest
, ExistingUserLogin
);
327 // Factory of callbacks.
328 base::WeakPtrFactory
<ExistingUserController
> weak_factory_
;
330 DISALLOW_COPY_AND_ASSIGN(ExistingUserController
);
333 } // namespace chromeos
335 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_