existing_user_controller.h revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_
7
8#include <string>
9
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_display.h"
21#include "chrome/browser/chromeos/login/login_performer.h"
22#include "chrome/browser/chromeos/login/login_utils.h"
23#include "chrome/browser/chromeos/login/user.h"
24#include "chrome/browser/chromeos/settings/cros_settings.h"
25#include "chrome/browser/chromeos/settings/device_settings_service.h"
26#include "content/public/browser/notification_observer.h"
27#include "content/public/browser/notification_registrar.h"
28#include "ui/gfx/rect.h"
29#include "url/gurl.h"
30
31namespace chromeos {
32
33class CrosSettings;
34class LoginDisplayHost;
35
36namespace login {
37class NetworkStateHelper;
38}
39
40// ExistingUserController is used to handle login when someone has
41// already logged into the machine.
42// To use ExistingUserController create an instance of it and invoke Init.
43// When Init is called it creates LoginDisplay instance which encapsulates
44// all login UI implementation.
45// ExistingUserController maintains it's own life cycle and deletes itself when
46// the user logs in (or chooses to see other settings).
47class ExistingUserController : public LoginDisplay::Delegate,
48                               public content::NotificationObserver,
49                               public LoginPerformer::Delegate,
50                               public LoginUtils::Delegate {
51 public:
52  // All UI initialization is deferred till Init() call.
53  explicit ExistingUserController(LoginDisplayHost* host);
54  virtual ~ExistingUserController();
55
56  // Returns the current existing user controller if it has been created.
57  static ExistingUserController* current_controller() {
58    return current_controller_;
59  }
60
61  // Creates and shows login UI for known users.
62  void Init(const UserList& users);
63
64  // Tells the controller to enter the Enterprise Enrollment screen when
65  // appropriate.
66  void DoAutoEnrollment();
67
68  // Tells the controller to resume a pending login.
69  void ResumeLogin();
70
71  // Start the public session auto-login timer.
72  void StartPublicSessionAutoLoginTimer();
73
74  // Stop the public session auto-login timer when a login attempt begins.
75  void StopPublicSessionAutoLoginTimer();
76
77  // LoginDisplay::Delegate: implementation
78  virtual void CancelPasswordChangedFlow() OVERRIDE;
79  virtual void CreateAccount() OVERRIDE;
80  virtual void CompleteLogin(const UserContext& user_context) OVERRIDE;
81  virtual string16 GetConnectedNetworkName() OVERRIDE;
82  virtual bool IsSigninInProgress() const OVERRIDE;
83  virtual void Login(const UserContext& user_context) OVERRIDE;
84  virtual void MigrateUserData(const std::string& old_password) OVERRIDE;
85  virtual void LoginAsRetailModeUser() OVERRIDE;
86  virtual void LoginAsGuest() OVERRIDE;
87  virtual void LoginAsPublicAccount(const std::string& username) OVERRIDE;
88  virtual void LoginAsKioskApp(const std::string& app_id) OVERRIDE;
89  virtual void OnSigninScreenReady() OVERRIDE;
90  virtual void OnUserSelected(const std::string& username) OVERRIDE;
91  virtual void OnStartEnterpriseEnrollment() OVERRIDE;
92  virtual void OnStartKioskEnableScreen() OVERRIDE;
93  virtual void OnStartDeviceReset() 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;
100
101  // content::NotificationObserver implementation.
102  virtual void Observe(int type,
103                       const content::NotificationSource& source,
104                       const content::NotificationDetails& details) OVERRIDE;
105
106  // Set a delegate that we will pass LoginStatusConsumer events to.
107  // Used for testing.
108  void set_login_status_consumer(LoginStatusConsumer* consumer) {
109    login_status_consumer_ = consumer;
110  }
111
112  // Returns the LoginDisplay created and owned by this controller.
113  // Used for testing.
114  LoginDisplay* login_display() {
115    return login_display_.get();
116  }
117
118  // Returns the LoginDisplayHost for this controller.
119  LoginDisplayHost* login_display_host() {
120    return host_;
121  }
122
123 private:
124  friend class ExistingUserControllerTest;
125  friend class ExistingUserControllerAutoLoginTest;
126  friend class ExistingUserControllerPublicSessionTest;
127  friend class MockLoginPerformerDelegate;
128
129  // Retrieve public session auto-login policy and update the timer.
130  void ConfigurePublicSessionAutoLogin();
131
132  // Trigger public session auto-login.
133  void OnPublicSessionAutoLoginTimerFire();
134
135  // LoginPerformer::Delegate implementation:
136  virtual void OnLoginFailure(const LoginFailure& error) OVERRIDE;
137  virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE;
138  virtual void OnOffTheRecordLoginSuccess() OVERRIDE;
139  virtual void OnPasswordChangeDetected() OVERRIDE;
140  virtual void WhiteListCheckFailed(const std::string& email) OVERRIDE;
141  virtual void PolicyLoadFailed() OVERRIDE;
142  virtual void OnOnlineChecked(
143      const std::string& username, bool success) OVERRIDE;
144
145  // LoginUtils::Delegate implementation:
146  virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
147
148  // Called when device settings change.
149  void DeviceSettingsChanged();
150
151  // Starts WizardController with the specified screen.
152  void ActivateWizard(const std::string& screen_name);
153
154  // Returns corresponding native window.
155  gfx::NativeWindow GetNativeWindow() const;
156
157  // Adds first-time login URLs.
158  void InitializeStartUrls() const;
159
160  // Show error message. |error_id| error message ID in resources.
161  // If |details| string is not empty, it specify additional error text
162  // provided by authenticator, it is not localized.
163  void ShowError(int error_id, const std::string& details);
164
165  // Shows Gaia page because password change was detected.
166  void ShowGaiaPasswordChanged(const std::string& username);
167
168  // Handles result of ownership check and starts enterprise or kiosk enrollment
169  // if applicable.
170  void OnEnrollmentOwnershipCheckCompleted(
171      DeviceSettingsService::OwnershipStatus status);
172
173  // Handles result of consumer kiosk configurability check and starts
174  // enable kiosk screen if applicable.
175  void OnConsumerKioskModeCheckCompleted(
176      KioskAppManager::ConsumerKioskModeStatus status);
177
178  // Enters the enterprise enrollment screen. |forced| is true if this is the
179  // result of an auto-enrollment check, and the user shouldn't be able to
180  // easily cancel the enrollment. In that case, |user| is the user name that
181  // first logged in.
182  void ShowEnrollmentScreen(bool forced, const std::string& user);
183
184  // Shows "reset device" screen.
185  void ShowResetScreen();
186
187  // Shows kiosk feature enable screen.
188  void ShowKioskEnableScreen();
189
190  // Shows "kiosk auto-launch permission" screen.
191  void ShowKioskAutolaunchScreen();
192
193  // Shows "critical TPM error" screen.
194  void ShowTPMError();
195
196  // Invoked to complete login. Login might be suspended if auto-enrollment
197  // has to be performed, and will resume once auto-enrollment completes.
198  void CompleteLoginInternal(
199      const UserContext& user_context,
200      DeviceSettingsService::OwnershipStatus ownership_status);
201
202  // Creates |login_performer_| if necessary and calls login() on it.
203  // The string arguments aren't passed by const reference because this is
204  // posted as |resume_login_callback_| and resets it.
205  void PerformLogin(const UserContext& user_context,
206                    LoginPerformer::AuthorizationMode auth_mode);
207
208  void set_login_performer_delegate(LoginPerformer::Delegate* d) {
209    login_performer_delegate_.reset(d);
210  }
211
212  // Updates the |login_display_| attached to this controller.
213  void UpdateLoginDisplay(const UserList& users);
214
215  // Public session auto-login timer.
216  scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_;
217
218  // Public session auto-login timeout, in milliseconds.
219  int public_session_auto_login_delay_;
220
221  // Username for public session auto-login.
222  std::string public_session_auto_login_username_;
223
224  // Used to execute login operations.
225  scoped_ptr<LoginPerformer> login_performer_;
226
227  // Delegate for login performer to be overridden by tests.
228  // |this| is used if |login_performer_delegate_| is NULL.
229  scoped_ptr<LoginPerformer::Delegate> login_performer_delegate_;
230
231  // Delegate to forward all login status events to.
232  // Tests can use this to receive login status events.
233  LoginStatusConsumer* login_status_consumer_;
234
235  // Username of the last login attempt.
236  std::string last_login_attempt_username_;
237
238  // OOBE/login display host.
239  LoginDisplayHost* host_;
240
241  // Login UI implementation instance.
242  scoped_ptr<LoginDisplay> login_display_;
243
244  // Number of login attempts. Used to show help link when > 1 unsuccessful
245  // logins for the same user.
246  size_t num_login_attempts_;
247
248  // Pointer to the current instance of the controller to be used by
249  // automation tests.
250  static ExistingUserController* current_controller_;
251
252  // Interface to the signed settings store.
253  CrosSettings* cros_settings_;
254
255  // URL to append to start Guest mode with.
256  GURL guest_mode_url_;
257
258  // Used for notifications during the login process.
259  content::NotificationRegistrar registrar_;
260
261  // Factory of callbacks.
262  base::WeakPtrFactory<ExistingUserController> weak_factory_;
263
264  // The displayed email for the next login attempt set by |SetDisplayEmail|.
265  std::string display_email_;
266
267  // Whether offline login attempt failed.
268  bool offline_failed_;
269
270  // Whether login attempt is running.
271  bool is_login_in_progress_;
272
273  // Whether online login attempt succeeded.
274  std::string online_succeeded_for_;
275
276  // True if password has been changed for user who is completing sign in.
277  // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
278  bool password_changed_;
279
280  // True if auto-enrollment should be performed before starting the user's
281  // session.
282  bool do_auto_enrollment_;
283
284  // Whether the sign-in UI is finished loading.
285  bool signin_screen_ready_;
286
287  // The username used for auto-enrollment, if it was triggered.
288  std::string auto_enrollment_username_;
289
290  // Callback to invoke to resume login, after auto-enrollment has completed.
291  base::Closure resume_login_callback_;
292
293  // Time when the signin screen was first displayed. Used to measure the time
294  // from showing the screen until a successful login is performed.
295  base::Time time_init_;
296
297  // Timer for the interval to wait for the reboot after TPM error UI was shown.
298  base::OneShotTimer<ExistingUserController> reboot_timer_;
299
300  scoped_ptr<login::NetworkStateHelper> network_state_helper_;
301
302  scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_;
303  scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
304  scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
305  scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_;
306  scoped_ptr<CrosSettings::ObserverSubscription>
307      local_account_auto_login_id_subscription_;
308  scoped_ptr<CrosSettings::ObserverSubscription>
309      local_account_auto_login_delay_subscription_;
310
311  FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin);
312
313  DISALLOW_COPY_AND_ASSIGN(ExistingUserController);
314};
315
316}  // namespace chromeos
317
318#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_
319