Update To 11.40.268.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/login_utils.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/rect.h"
30 #include "url/gurl.h"
31
32 namespace base {
33 class ListValue;
34 }
35
36 namespace chromeos {
37
38 class CrosSettings;
39 class LoginDisplayHost;
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   // 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 AuthStatusConsumer events to.
107   // Used for testing.
108   void set_login_status_consumer(AuthStatusConsumer* consumer) {
109     auth_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   // Returns value of LoginPerformer::auth_mode() (cached if performer is
124   // destroyed).
125   LoginPerformer::AuthorizationMode auth_mode() const;
126
127   // Returns value of LoginPerformer::password_changed() (cached if performer is
128   // destroyed).
129   bool password_changed() const;
130
131  private:
132   friend class ExistingUserControllerTest;
133   friend class ExistingUserControllerAutoLoginTest;
134   friend class ExistingUserControllerPublicSessionTest;
135   friend class MockLoginPerformerDelegate;
136
137   void LoginAsRetailModeUser();
138   void LoginAsGuest();
139   void LoginAsPublicSession(const UserContext& user_context);
140   void LoginAsKioskApp(const std::string& app_id, bool diagnostic_mode);
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,
160                                  bool browser_launched) override;
161
162   // Called when device settings change.
163   void DeviceSettingsChanged();
164
165   // Returns corresponding native window.
166   gfx::NativeWindow GetNativeWindow() const;
167
168   // Show error message. |error_id| error message ID in resources.
169   // If |details| string is not empty, it specify additional error text
170   // provided by authenticator, it is not localized.
171   void ShowError(int error_id, const std::string& details);
172
173   // Shows Gaia page because password change was detected.
174   void ShowGaiaPasswordChanged(const std::string& username);
175
176   // Handles result of ownership check and starts enterprise or kiosk enrollment
177   // if applicable.
178   void OnEnrollmentOwnershipCheckCompleted(
179       DeviceSettingsService::OwnershipStatus status);
180
181   // Handles result of consumer kiosk configurability check and starts
182   // enable kiosk screen if applicable.
183   void OnConsumerKioskAutoLaunchCheckCompleted(
184       KioskAppManager::ConsumerKioskAutoLaunchStatus status);
185
186   // Enters the enterprise enrollment screen. |forced| is true if this is the
187   // result of an auto-enrollment check, and the user shouldn't be able to
188   // easily cancel the enrollment. In that case, |user| is the user name that
189   // first logged in.
190   void ShowEnrollmentScreen(bool forced, const std::string& user);
191
192   // Shows "reset device" screen.
193   void ShowResetScreen();
194
195   // Shows kiosk feature enable screen.
196   void ShowKioskEnableScreen();
197
198   // Shows "kiosk auto-launch permission" screen.
199   void ShowKioskAutolaunchScreen();
200
201   // Shows "critical TPM error" screen.
202   void ShowTPMError();
203
204   // Invoked to complete login. Login might be suspended if auto-enrollment
205   // has to be performed, and will resume once auto-enrollment completes.
206   void CompleteLoginInternal(
207       const UserContext& user_context,
208       DeviceSettingsService::OwnershipStatus ownership_status);
209
210   // Creates |login_performer_| if necessary and calls login() on it.
211   // The string arguments aren't passed by const reference because this is
212   // posted as |resume_login_callback_| and resets it.
213   void PerformLogin(const UserContext& user_context,
214                     LoginPerformer::AuthorizationMode auth_mode);
215
216   // Updates the |login_display_| attached to this controller.
217   void UpdateLoginDisplay(const user_manager::UserList& users);
218
219   // Sends an accessibility alert event to extension listeners.
220   void SendAccessibilityAlert(const std::string& alert_text);
221
222   // Callback invoked when the keyboard layouts available for a public session
223   // have been retrieved. Selects the first layout from the list and continues
224   // login.
225   void SetPublicSessionKeyboardLayoutAndLogin(
226       const UserContext& user_context,
227       scoped_ptr<base::ListValue> keyboard_layouts);
228
229   // Starts the actual login process for a public session. Invoked when all
230   // preconditions have been verified.
231   void LoginAsPublicSessionInternal(const UserContext& user_context);
232
233   // Performs sets of actions right prior to login has been started.
234   void PerformPreLoginActions(const UserContext& user_context);
235
236   // Performs set of actions when login has been completed or has been
237   // cancelled. If |start_public_session_timer| is true than public session
238   // auto-login timer is started.
239   void PerformLoginFinishedActions(bool start_public_session_timer);
240
241   // Public session auto-login timer.
242   scoped_ptr<base::OneShotTimer<ExistingUserController> > auto_login_timer_;
243
244   // Public session auto-login timeout, in milliseconds.
245   int public_session_auto_login_delay_;
246
247   // Username for public session auto-login.
248   std::string public_session_auto_login_username_;
249
250   // Used to execute login operations.
251   scoped_ptr<LoginPerformer> login_performer_;
252
253   // Delegate to forward all authentication status events to.
254   // Tests can use this to receive authentication status events.
255   AuthStatusConsumer* auth_status_consumer_;
256
257   // Username of the last login attempt.
258   std::string last_login_attempt_username_;
259
260   // Auth flow of the last login attempt.
261   UserContext::AuthFlow last_login_attempt_auth_flow_;
262
263   // OOBE/login display host.
264   LoginDisplayHost* host_;
265
266   // Login UI implementation instance.
267   scoped_ptr<LoginDisplay> login_display_;
268
269   // Number of login attempts. Used to show help link when > 1 unsuccessful
270   // logins for the same user.
271   size_t num_login_attempts_;
272
273   // Pointer to the current instance of the controller to be used by
274   // automation tests.
275   static ExistingUserController* current_controller_;
276
277   // Interface to the signed settings store.
278   CrosSettings* cros_settings_;
279
280   // URL to append to start Guest mode with.
281   GURL guest_mode_url_;
282
283   // Used for notifications during the login process.
284   content::NotificationRegistrar registrar_;
285
286   // The displayed email for the next login attempt set by |SetDisplayEmail|.
287   std::string display_email_;
288
289   // Whether offline login attempt failed.
290   bool offline_failed_;
291
292   // Whether login attempt is running.
293   bool is_login_in_progress_;
294
295   // Whether online login attempt succeeded.
296   std::string online_succeeded_for_;
297
298   // True if password has been changed for user who is completing sign in.
299   // Set in OnLoginSuccess. Before that use LoginPerformer::password_changed().
300   bool password_changed_;
301
302   // Set in OnLoginSuccess. Before that use LoginPerformer::auth_mode().
303   // Initialized with AUTH_MODE_EXTENSION as more restricted mode.
304   LoginPerformer::AuthorizationMode auth_mode_;
305
306   // True if auto-enrollment should be performed before starting the user's
307   // session.
308   bool do_auto_enrollment_;
309
310   // Whether the sign-in UI is finished loading.
311   bool signin_screen_ready_;
312
313   // The username used for auto-enrollment, if it was triggered.
314   std::string auto_enrollment_username_;
315
316   // Callback to invoke to resume login, after auto-enrollment has completed.
317   base::Closure resume_login_callback_;
318
319   // Time when the signin screen was first displayed. Used to measure the time
320   // from showing the screen until a successful login is performed.
321   base::Time time_init_;
322
323   // Timer for the interval to wait for the reboot after TPM error UI was shown.
324   base::OneShotTimer<ExistingUserController> reboot_timer_;
325
326   scoped_ptr<login::NetworkStateHelper> network_state_helper_;
327
328   scoped_ptr<CrosSettings::ObserverSubscription> show_user_names_subscription_;
329   scoped_ptr<CrosSettings::ObserverSubscription> allow_new_user_subscription_;
330   scoped_ptr<CrosSettings::ObserverSubscription>
331       allow_supervised_user_subscription_;
332   scoped_ptr<CrosSettings::ObserverSubscription> allow_guest_subscription_;
333   scoped_ptr<CrosSettings::ObserverSubscription> users_subscription_;
334   scoped_ptr<CrosSettings::ObserverSubscription>
335       local_account_auto_login_id_subscription_;
336   scoped_ptr<CrosSettings::ObserverSubscription>
337       local_account_auto_login_delay_subscription_;
338
339   FRIEND_TEST_ALL_PREFIXES(ExistingUserControllerTest, ExistingUserLogin);
340
341   // Factory of callbacks.
342   base::WeakPtrFactory<ExistingUserController> weak_factory_;
343
344   DISALLOW_COPY_AND_ASSIGN(ExistingUserController);
345 };
346
347 }  // namespace chromeos
348
349 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_EXISTING_USER_CONTROLLER_H_