Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / existing_user_controller.h
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/auth/login_performer.h"
21 #include "chrome/browser/chromeos/login/login_utils.h"
22 #include "chrome/browser/chromeos/login/ui/login_display.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #include "components/user_manager/user.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
31 namespace base {
32 class ListValue;
33 }
34
35 namespace chromeos {
36
37 class CrosSettings;
38 class LoginDisplayHost;
39 class UserContext;
40
41 namespace login {
42 class NetworkStateHelper;
43 }
44
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 {
56  public:
57   // All UI initialization is deferred till Init() call.
58   explicit ExistingUserController(LoginDisplayHost* host);
59   virtual ~ExistingUserController();
60
61   // Returns the current existing user controller if it has been created.
62   static ExistingUserController* current_controller() {
63     return current_controller_;
64   }
65
66   // Creates and shows login UI for known users.
67   void Init(const user_manager::UserList& users);
68
69   // Tells the controller to enter the Enterprise Enrollment screen when
70   // appropriate.
71   void DoAutoEnrollment();
72
73   // Tells the controller to resume a pending login.
74   void ResumeLogin();
75
76   // Start the public session auto-login timer.
77   void StartPublicSessionAutoLoginTimer();
78
79   // Stop the public session auto-login timer when a login attempt begins.
80   void StopPublicSessionAutoLoginTimer();
81
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;
100
101   void LoginAsRetailModeUser();
102   void LoginAsGuest();
103   void LoginAsPublicSession(const UserContext& user_context);
104   void LoginAsKioskApp(const std::string& app_id, bool diagnostic_mode);
105
106   // content::NotificationObserver implementation.
107   virtual void Observe(int type,
108                        const content::NotificationSource& source,
109                        const content::NotificationDetails& details) OVERRIDE;
110
111   // Set a delegate that we will pass AuthStatusConsumer events to.
112   // Used for testing.
113   void set_login_status_consumer(AuthStatusConsumer* consumer) {
114     auth_status_consumer_ = consumer;
115   }
116
117   // Returns the LoginDisplay created and owned by this controller.
118   // Used for testing.
119   LoginDisplay* login_display() {
120     return login_display_.get();
121   }
122
123   // Returns the LoginDisplayHost for this controller.
124   LoginDisplayHost* login_display_host() {
125     return host_;
126   }
127
128   // Returns value of LoginPerformer::auth_mode() (cached if performer is
129   // destroyed).
130   LoginPerformer::AuthorizationMode auth_mode() const;
131
132   // Returns value of LoginPerformer::password_changed() (cached if performer is
133   // destroyed).
134   bool password_changed() const;
135
136  private:
137   friend class ExistingUserControllerTest;
138   friend class ExistingUserControllerAutoLoginTest;
139   friend class ExistingUserControllerPublicSessionTest;
140   friend class MockLoginPerformerDelegate;
141
142   // Retrieve public session auto-login policy and update the timer.
143   void ConfigurePublicSessionAutoLogin();
144
145   // Trigger public session auto-login.
146   void OnPublicSessionAutoLoginTimerFire();
147
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;
157
158   // LoginUtils::Delegate implementation:
159   virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
160
161   // Called when device settings change.
162   void DeviceSettingsChanged();
163
164   // Starts WizardController with the specified screen.
165   void ActivateWizard(const std::string& screen_name);
166
167   // Returns corresponding native window.
168   gfx::NativeWindow GetNativeWindow() const;
169
170   // Adds first-time login URLs.
171   void InitializeStartUrls() const;
172
173   // Show error message. |error_id| error message ID in resources.
174   // If |details| string is not empty, it specify additional error text
175   // provided by authenticator, it is not localized.
176   void ShowError(int error_id, const std::string& details);
177
178   // Shows Gaia page because password change was detected.
179   void ShowGaiaPasswordChanged(const std::string& username);
180
181   // Handles result of ownership check and starts enterprise or kiosk enrollment
182   // if applicable.
183   void OnEnrollmentOwnershipCheckCompleted(
184       DeviceSettingsService::OwnershipStatus status);
185
186   // Handles result of consumer kiosk configurability check and starts
187   // enable kiosk screen if applicable.
188   void OnConsumerKioskAutoLaunchCheckCompleted(
189       KioskAppManager::ConsumerKioskAutoLaunchStatus status);
190
191   // Enters the enterprise enrollment screen. |forced| is true if this is the
192   // result of an auto-enrollment check, and the user shouldn't be able to
193   // easily cancel the enrollment. In that case, |user| is the user name that
194   // first logged in.
195   void ShowEnrollmentScreen(bool forced, const std::string& user);
196
197   // Shows "reset device" screen.
198   void ShowResetScreen();
199
200   // Shows kiosk feature enable screen.
201   void ShowKioskEnableScreen();
202
203   // Shows "kiosk auto-launch permission" screen.
204   void ShowKioskAutolaunchScreen();
205
206   // Shows "critical TPM error" screen.
207   void ShowTPMError();
208
209   // Invoked to complete login. Login might be suspended if auto-enrollment
210   // has to be performed, and will resume once auto-enrollment completes.
211   void CompleteLoginInternal(
212       const UserContext& user_context,
213       DeviceSettingsService::OwnershipStatus ownership_status);
214
215   // Creates |login_performer_| if necessary and calls login() on it.
216   // The string arguments aren't passed by const reference because this is
217   // posted as |resume_login_callback_| and resets it.
218   void PerformLogin(const UserContext& user_context,
219                     LoginPerformer::AuthorizationMode auth_mode);
220
221   // Updates the |login_display_| attached to this controller.
222   void UpdateLoginDisplay(const user_manager::UserList& users);
223
224   // Sends an accessibility alert event to extension listeners.
225   void SendAccessibilityAlert(const std::string& alert_text);
226
227   // Callback invoked when the keyboard layouts available for a public session
228   // have been retrieved. Selects the first layout from the list and continues
229   // login.
230   void SetPublicSessionKeyboardLayoutAndLogin(
231       const UserContext& user_context,
232       scoped_ptr<base::ListValue> keyboard_layouts);
233
234   // Starts the actual login process for a public session. Invoked when all
235   // preconditions have been verified.
236   void LoginAsPublicSessionInternal(const UserContext& user_context);
237
238   // Public session auto-login timer.
239   scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_;
240
241   // Public session auto-login timeout, in milliseconds.
242   int public_session_auto_login_delay_;
243
244   // Username for public session auto-login.
245   std::string public_session_auto_login_username_;
246
247   // Used to execute login operations.
248   scoped_ptr<LoginPerformer> login_performer_;
249
250   // Delegate to forward all authentication status events to.
251   // Tests can use this to receive authentication status events.
252   AuthStatusConsumer* auth_status_consumer_;
253
254   // Username of the last login attempt.
255   std::string last_login_attempt_username_;
256
257   // OOBE/login display host.
258   LoginDisplayHost* host_;
259
260   // Login UI implementation instance.
261   scoped_ptr<LoginDisplay> login_display_;
262
263   // Number of login attempts. Used to show help link when > 1 unsuccessful
264   // logins for the same user.
265   size_t num_login_attempts_;
266
267   // Pointer to the current instance of the controller to be used by
268   // automation tests.
269   static ExistingUserController* current_controller_;
270
271   // Interface to the signed settings store.
272   CrosSettings* cros_settings_;
273
274   // URL to append to start Guest mode with.
275   GURL guest_mode_url_;
276
277   // Used for notifications during the login process.
278   content::NotificationRegistrar registrar_;
279
280   // Factory of callbacks.
281   base::WeakPtrFactory<ExistingUserController> weak_factory_;
282
283   // The displayed email for the next login attempt set by |SetDisplayEmail|.
284   std::string display_email_;
285
286   // Whether offline login attempt failed.
287   bool offline_failed_;
288
289   // Whether login attempt is running.
290   bool is_login_in_progress_;
291
292   // Whether online login attempt succeeded.
293   std::string online_succeeded_for_;
294
295   // True if password has been changed for user who is completing sign in.
296   // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
297   bool password_changed_;
298
299   // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
300   // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
301   LoginPerformer::AuthorizationMode auth_mode_;
302
303   // True if auto-enrollment should be performed before starting the user's
304   // session.
305   bool do_auto_enrollment_;
306
307   // Whether the sign-in UI is finished loading.
308   bool signin_screen_ready_;
309
310   // The username used for auto-enrollment, if it was triggered.
311   std::string auto_enrollment_username_;
312
313   // Callback to invoke to resume login, after auto-enrollment has completed.
314   base::Closure resume_login_callback_;
315
316   // Time when the signin screen was first displayed. Used to measure the time
317   // from showing the screen until a successful login is performed.
318   base::Time time_init_;
319
320   // Timer for the interval to wait for the reboot after TPM error UI was shown.
321   base::OneShotTimer<ExistingUserController> reboot_timer_;
322
323   scoped_ptr<login::NetworkStateHelper> network_state_helper_;
324
325   scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_;
326   scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
327   scoped_ptr<CrosSettings::ObserverSubscription>
328       allow_supervised_user_subscription_;
329   scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
330   scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_;
331   scoped_ptr<CrosSettings::ObserverSubscription>
332       local_account_auto_login_id_subscription_;
333   scoped_ptr<CrosSettings::ObserverSubscription>
334       local_account_auto_login_delay_subscription_;
335
336   FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin);
337
338   DISALLOW_COPY_AND_ASSIGN(ExistingUserController);
339 };
340
341 }  // namespace chromeos
342
343 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_