ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / signin_screen_handler.h
blobe1353c0f31efa9f884b4ee1fe02aa734cd91010d
1 // Copyright (c) 2013 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_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "chrome/browser/chromeos/login/screens/network_error_model.h"
20 #include "chrome/browser/chromeos/login/signin_specifics.h"
21 #include "chrome/browser/chromeos/login/ui/login_display.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chrome/browser/signin/screenlock_bridge.h"
24 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler.h"
25 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
26 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
27 #include "chrome/browser/ui/webui/chromeos/touch_view_controller_delegate.h"
28 #include "chromeos/network/portal_detector/network_portal_detector.h"
29 #include "components/user_manager/user_manager.h"
30 #include "content/public/browser/notification_observer.h"
31 #include "content/public/browser/notification_registrar.h"
32 #include "content/public/browser/web_ui.h"
33 #include "net/base/net_errors.h"
34 #include "ui/base/ime/chromeos/ime_keyboard.h"
35 #include "ui/base/ime/chromeos/input_method_manager.h"
36 #include "ui/events/event_handler.h"
38 class EasyUnlockService;
40 namespace base {
41 class DictionaryValue;
42 class ListValue;
45 namespace chromeos {
47 class CaptivePortalWindowProxy;
48 class CoreOobeActor;
49 class ErrorScreensHistogramHelper;
50 class GaiaScreenHandler;
51 class NativeWindowDelegate;
52 class SupervisedUserCreationScreenHandler;
53 class User;
54 class UserContext;
56 // Helper class to pass initial parameters to the login screen.
57 class LoginScreenContext {
58 public:
59 LoginScreenContext();
60 explicit LoginScreenContext(const base::ListValue* args);
62 void set_email(const std::string& email) { email_ = email; }
63 const std::string& email() const { return email_; }
65 void set_oobe_ui(bool oobe_ui) { oobe_ui_ = oobe_ui; }
66 bool oobe_ui() const { return oobe_ui_; }
68 private:
69 void Init();
71 std::string email_;
72 bool oobe_ui_;
75 // An interface for WebUILoginDisplay to call SigninScreenHandler.
76 class LoginDisplayWebUIHandler {
77 public:
78 virtual void ClearAndEnablePassword() = 0;
79 virtual void ClearUserPodPassword() = 0;
80 virtual void OnUserRemoved(const std::string& username) = 0;
81 virtual void OnUserImageChanged(const user_manager::User& user) = 0;
82 virtual void OnPreferencesChanged() = 0;
83 virtual void ResetSigninScreenHandlerDelegate() = 0;
84 virtual void ShowError(int login_attempts,
85 const std::string& error_text,
86 const std::string& help_link_text,
87 HelpAppLauncher::HelpTopic help_topic_id) = 0;
88 virtual void ShowErrorScreen(LoginDisplay::SigninError error_id) = 0;
89 virtual void ShowGaiaPasswordChanged(const std::string& username) = 0;
90 virtual void ShowSigninUI(const std::string& email) = 0;
91 virtual void ShowPasswordChangedDialog(bool show_password_error) = 0;
92 // Show sign-in screen for the given credentials.
93 virtual void ShowSigninScreenForCreds(const std::string& username,
94 const std::string& password) = 0;
95 virtual void LoadUsers(const base::ListValue& users_list,
96 bool show_guest) = 0;
97 protected:
98 virtual ~LoginDisplayWebUIHandler() {}
101 // An interface for SigninScreenHandler to call WebUILoginDisplay.
102 class SigninScreenHandlerDelegate {
103 public:
104 // --------------- Password change flow methods.
105 // Cancels current password changed flow.
106 virtual void CancelPasswordChangedFlow() = 0;
108 // Decrypt cryptohome using user provided |old_password|
109 // and migrate to new password.
110 virtual void MigrateUserData(const std::string& old_password) = 0;
112 // Ignore password change, remove existing cryptohome and
113 // force full sync of user data.
114 virtual void ResyncUserData() = 0;
116 // --------------- Sign in/out methods.
117 // Sign in using username and password specified as a part of |user_context|.
118 // Used for both known and new users.
119 virtual void Login(const UserContext& user_context,
120 const SigninSpecifics& specifics) = 0;
122 // Sign in as guest to create a new Google account.
123 virtual void CreateAccount() = 0;
125 // Returns true if sign in is in progress.
126 virtual bool IsSigninInProgress() const = 0;
128 // Signs out if the screen is currently locked.
129 virtual void Signout() = 0;
131 // --------------- Account creation methods.
132 // Confirms sign up by provided credentials in |user_context|.
133 // Used for new user login via GAIA extension.
134 virtual void CompleteLogin(const UserContext& user_context) = 0;
136 // --------------- Shared with login display methods.
137 // Notify the delegate when the sign-in UI is finished loading.
138 virtual void OnSigninScreenReady() = 0;
140 // Shows Enterprise Enrollment screen.
141 virtual void ShowEnterpriseEnrollmentScreen() = 0;
143 // Shows Enable Developer Features screen.
144 virtual void ShowEnableDebuggingScreen() = 0;
146 // Shows Kiosk Enable screen.
147 virtual void ShowKioskEnableScreen() = 0;
149 // Shows Reset screen.
150 virtual void ShowKioskAutolaunchScreen() = 0;
152 // Show wrong hwid screen.
153 virtual void ShowWrongHWIDScreen() = 0;
155 // Sets the displayed email for the next login attempt. If it succeeds,
156 // user's displayed email value will be updated to |email|.
157 virtual void SetDisplayEmail(const std::string& email) = 0;
159 // --------------- Rest of the methods.
160 // Cancels user adding.
161 virtual void CancelUserAdding() = 0;
163 // Load wallpaper for given |username|.
164 virtual void LoadWallpaper(const std::string& username) = 0;
166 // Loads the default sign-in wallpaper.
167 virtual void LoadSigninWallpaper() = 0;
169 // Attempts to remove given user.
170 virtual void RemoveUser(const std::string& username) = 0;
172 // Let the delegate know about the handler it is supposed to be using.
173 virtual void SetWebUIHandler(LoginDisplayWebUIHandler* webui_handler) = 0;
175 // Returns users list to be shown.
176 virtual const user_manager::UserList& GetUsers() const = 0;
178 // Whether login as guest is available.
179 virtual bool IsShowGuest() const = 0;
181 // Weather to show the user pods or only GAIA sign in.
182 // Public sessions are always shown.
183 virtual bool IsShowUsers() const = 0;
185 // Whether user sign in has completed.
186 virtual bool IsUserSigninCompleted() const = 0;
188 // Request to (re)load user list.
189 virtual void HandleGetUsers() = 0;
191 protected:
192 virtual ~SigninScreenHandlerDelegate() {}
195 // A class that handles the WebUI hooks in sign-in screen in OobeDisplay
196 // and LoginDisplay.
197 class SigninScreenHandler
198 : public BaseScreenHandler,
199 public LoginDisplayWebUIHandler,
200 public content::NotificationObserver,
201 public NetworkStateInformer::NetworkStateInformerObserver,
202 public input_method::ImeKeyboard::Observer,
203 public TouchViewControllerDelegate::Observer,
204 public OobeUI::Observer {
205 public:
206 SigninScreenHandler(
207 const scoped_refptr<NetworkStateInformer>& network_state_informer,
208 NetworkErrorModel* network_error_model,
209 CoreOobeActor* core_oobe_actor,
210 GaiaScreenHandler* gaia_screen_handler);
211 ~SigninScreenHandler() override;
213 static std::string GetUserLRUInputMethod(const std::string& username);
215 // Update current input method (namely keyboard layout) in the given IME state
216 // to LRU by this user.
217 static void SetUserInputMethod(
218 const std::string& username,
219 input_method::InputMethodManager::State* ime_state);
221 // Shows the sign in screen.
222 void Show(const LoginScreenContext& context);
224 // Sets delegate to be used by the handler. It is guaranteed that valid
225 // delegate is set before Show() method will be called.
226 void SetDelegate(SigninScreenHandlerDelegate* delegate);
228 void SetNativeWindowDelegate(NativeWindowDelegate* native_window_delegate);
230 // NetworkStateInformer::NetworkStateInformerObserver implementation:
231 void OnNetworkReady() override;
232 void UpdateState(NetworkError::ErrorReason reason) override;
234 // Required Local State preferences.
235 static void RegisterPrefs(PrefRegistrySimple* registry);
237 // OobeUI::Observer implemetation.
238 void OnCurrentScreenChanged(OobeUI::Screen current_screen,
239 OobeUI::Screen new_screen) override;
241 void SetFocusPODCallbackForTesting(base::Closure callback);
243 // To avoid spurious error messages on flaky networks, the offline message is
244 // only shown if the network is offline for a threshold number of seconds.
245 // This method reduces the threshold to zero, allowing the offline message to
246 // show instantaneously in tests.
247 void ZeroOfflineTimeoutForTesting();
249 private:
250 enum UIState {
251 UI_STATE_UNKNOWN = 0,
252 UI_STATE_GAIA_SIGNIN,
253 UI_STATE_ACCOUNT_PICKER,
256 friend class GaiaScreenHandler;
257 friend class ReportDnsCacheClearedOnUIThread;
258 friend class SupervisedUserCreationScreenHandler;
260 void ShowImpl();
262 // Updates current UI of the signin screen according to |ui_state|
263 // argument. Optionally it can pass screen initialization data via
264 // |params| argument.
265 void UpdateUIState(UIState ui_state, base::DictionaryValue* params);
267 void UpdateStateInternal(NetworkError::ErrorReason reason, bool force_update);
268 void SetupAndShowOfflineMessage(NetworkStateInformer::State state,
269 NetworkError::ErrorReason reason);
270 void HideOfflineMessage(NetworkStateInformer::State state,
271 NetworkError::ErrorReason reason);
272 void ReloadGaia(bool force_reload);
274 // BaseScreenHandler implementation:
275 void DeclareLocalizedValues(
276 ::login::LocalizedValuesBuilder* builder) override;
277 void Initialize() override;
278 gfx::NativeWindow GetNativeWindow() override;
280 // WebUIMessageHandler implementation:
281 void RegisterMessages() override;
283 // LoginDisplayWebUIHandler implementation:
284 void ClearAndEnablePassword() override;
285 void ClearUserPodPassword() override;
286 void OnUserRemoved(const std::string& username) override;
287 void OnUserImageChanged(const user_manager::User& user) override;
288 void OnPreferencesChanged() override;
289 void ResetSigninScreenHandlerDelegate() override;
290 void ShowError(int login_attempts,
291 const std::string& error_text,
292 const std::string& help_link_text,
293 HelpAppLauncher::HelpTopic help_topic_id) override;
294 void ShowGaiaPasswordChanged(const std::string& username) override;
295 void ShowSigninUI(const std::string& email) override;
296 void ShowPasswordChangedDialog(bool show_password_error) override;
297 void ShowErrorScreen(LoginDisplay::SigninError error_id) override;
298 void ShowSigninScreenForCreds(const std::string& username,
299 const std::string& password) override;
300 void LoadUsers(const base::ListValue& users_list, bool show_guest) override;
302 // content::NotificationObserver implementation:
303 void Observe(int type,
304 const content::NotificationSource& source,
305 const content::NotificationDetails& details) override;
307 // TouchViewControllerDelegate::Observer implementation:
308 void OnMaximizeModeStarted() override;
309 void OnMaximizeModeEnded() override;
311 // Updates authentication extension. Called when device settings that affect
312 // sign-in (allow BWSI and allow whitelist) are changed.
313 void UserSettingsChanged();
314 void UpdateAddButtonStatus();
316 // Restore input focus to current user pod.
317 void RefocusCurrentPod();
319 // WebUI message handlers.
320 void HandleGetUsers();
321 void HandleAuthenticateUser(const std::string& username,
322 const std::string& password);
323 void HandleAttemptUnlock(const std::string& username);
324 void HandleLaunchIncognito();
325 void HandleLaunchPublicSession(const std::string& user_id,
326 const std::string& locale,
327 const std::string& input_method);
328 void HandleOfflineLogin(const base::ListValue* args);
329 void HandleShutdownSystem();
330 void HandleLoadWallpaper(const std::string& email);
331 void HandleRebootSystem();
332 void HandleRemoveUser(const std::string& email);
333 void HandleShowAddUser(const base::ListValue* args);
334 void HandleToggleEnrollmentScreen();
335 void HandleToggleEnableDebuggingScreen();
336 void HandleToggleKioskEnableScreen();
337 void HandleToggleResetScreen();
338 void HandleToggleKioskAutolaunchScreen();
339 void HandleCreateAccount();
340 void HandleAccountPickerReady();
341 void HandleWallpaperReady();
342 void HandleSignOutUser();
343 void HandleOpenProxySettings();
344 void HandleLoginVisible(const std::string& source);
345 void HandleCancelPasswordChangedFlow();
346 void HandleCancelUserAdding();
347 void HandleMigrateUserData(const std::string& password);
348 void HandleResyncUserData();
349 void HandleLoginUIStateChanged(const std::string& source, bool new_value);
350 void HandleUnlockOnLoginSuccess();
351 void HandleLoginScreenUpdate();
352 void HandleShowLoadingTimeoutError();
353 void HandleUpdateOfflineLogin(bool offline_login_active);
354 void HandleShowSupervisedUserCreationScreen();
355 void HandleFocusPod(const std::string& user_id);
356 void HandleHardlockPod(const std::string& user_id);
357 void HandleLaunchKioskApp(const std::string& app_id, bool diagnostic_mode);
358 void HandleGetPublicSessionKeyboardLayouts(const std::string& user_id,
359 const std::string& locale);
360 void HandleCancelConsumerManagementEnrollment();
361 void HandleGetTouchViewState();
362 void HandleSwitchToEmbeddedSignin();
364 // Sends the list of |keyboard_layouts| available for the |locale| that is
365 // currently selected for the public session identified by |user_id|.
366 void SendPublicSessionKeyboardLayouts(
367 const std::string& user_id,
368 const std::string& locale,
369 scoped_ptr<base::ListValue> keyboard_layouts);
371 // Returns true iff
372 // (i) log in is restricted to some user list,
373 // (ii) all users in the restricted list are present.
374 bool AllWhitelistedUsersPresent();
376 // Cancels password changed flow - switches back to login screen.
377 // Called as a callback after cookies are cleared.
378 void CancelPasswordChangedFlowInternal();
380 // Returns current visible screen.
381 OobeUI::Screen GetCurrentScreen() const;
383 // Returns true if current visible screen is the Gaia sign-in page.
384 bool IsGaiaVisible() const;
386 // Returns true if current visible screen is the error screen over
387 // Gaia sign-in page.
388 bool IsGaiaHiddenByError() const;
390 // Returns true if current screen is the error screen over signin
391 // screen.
392 bool IsSigninScreenHiddenByError() const;
394 // Returns true if guest signin is allowed.
395 bool IsGuestSigninAllowed() const;
397 // Returns true if offline login is allowed.
398 bool IsOfflineLoginAllowed() const;
400 bool ShouldLoadGaia() const;
402 // Shows signin.
403 void OnShowAddUser();
405 net::Error FrameError() const;
407 // input_method::ImeKeyboard::Observer implementation:
408 void OnCapsLockChanged(bool enabled) override;
410 // Returns OobeUI object of NULL.
411 OobeUI* GetOobeUI() const;
413 // Gets the easy unlock service associated with the user. Can return NULL if
414 // user cannot be found, or there is not associated service.
415 EasyUnlockService* GetEasyUnlockServiceForUser(
416 const std::string& username) const;
418 // Current UI state of the signin screen.
419 UIState ui_state_ = UI_STATE_UNKNOWN;
421 // A delegate that glues this handler with backend LoginDisplay.
422 SigninScreenHandlerDelegate* delegate_ = nullptr;
424 // A delegate used to get gfx::NativeWindow.
425 NativeWindowDelegate* native_window_delegate_ = nullptr;
427 // Whether screen should be shown right after initialization.
428 bool show_on_init_ = false;
430 // Keeps whether screen should be shown for OOBE.
431 bool oobe_ui_ = false;
433 // Is account picker being shown for the first time.
434 bool is_account_picker_showing_first_time_ = false;
436 // Network state informer used to keep signin screen up.
437 scoped_refptr<NetworkStateInformer> network_state_informer_;
439 // Set to true once |LOGIN_WEBUI_VISIBLE| notification is observed.
440 bool webui_visible_ = false;
441 bool preferences_changed_delayed_ = false;
443 NetworkErrorModel* network_error_model_;
444 CoreOobeActor* core_oobe_actor_;
446 bool is_first_update_state_call_ = false;
447 bool offline_login_active_ = false;
448 NetworkStateInformer::State last_network_state_ =
449 NetworkStateInformer::UNKNOWN;
451 base::CancelableClosure update_state_closure_;
452 base::CancelableClosure connecting_closure_;
454 content::NotificationRegistrar registrar_;
456 // Whether there is an auth UI pending. This flag is set on receiving
457 // NOTIFICATION_AUTH_NEEDED and reset on either NOTIFICATION_AUTH_SUPPLIED or
458 // NOTIFICATION_AUTH_CANCELLED.
459 bool has_pending_auth_ui_ = false;
461 bool caps_lock_enabled_;
463 // Non-owning ptr.
464 // TODO(antrim@): remove this dependency.
465 GaiaScreenHandler* gaia_screen_handler_;
467 // Maximized mode controller delegate.
468 scoped_ptr<TouchViewControllerDelegate> max_mode_delegate_;
470 // Whether consumer management enrollment is in progress.
471 bool is_enrolling_consumer_management_ = false;
473 // Input Method Engine state used at signin screen.
474 scoped_refptr<input_method::InputMethodManager::State> ime_state_;
476 // This callback captures "focusPod finished" event for tests.
477 base::Closure test_focus_pod_callback_;
479 // True if SigninScreenHandler has already been added to OobeUI observers.
480 bool oobe_ui_observer_added_ = false;
482 bool zero_offline_timeout_for_test_ = false;
484 scoped_ptr<ErrorScreensHistogramHelper> histogram_helper_;
486 base::WeakPtrFactory<SigninScreenHandler> weak_factory_;
488 DISALLOW_COPY_AND_ASSIGN(SigninScreenHandler);
491 } // namespace chromeos
493 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_SIGNIN_SCREEN_HANDLER_H_