1 // Copyright 2017 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 ASH_LOGIN_LOGIN_SCREEN_CONTROLLER_H_
6 #define ASH_LOGIN_LOGIN_SCREEN_CONTROLLER_H_
7 
8 #include <vector>
9 
10 #include "ash/ash_export.h"
11 #include "ash/login/security_token_request_controller.h"
12 #include "ash/login/ui/login_data_dispatcher.h"
13 #include "ash/public/cpp/kiosk_app_menu.h"
14 #include "ash/public/cpp/login_accelerators.h"
15 #include "ash/public/cpp/login_screen.h"
16 #include "ash/public/cpp/system_tray_focus_observer.h"
17 #include "base/macros.h"
18 #include "base/optional.h"
19 #include "base/time/time.h"
20 #include "ui/gfx/native_widget_types.h"
21 
22 class PrefRegistrySimple;
23 
24 namespace ash {
25 
26 class SystemTrayNotifier;
27 enum class SupervisedAction;
28 
29 // LoginScreenController implements LoginScreen and wraps the LoginScreenClient
30 // interface. This lets a consumer of ash provide a LoginScreenClient, which we
31 // will dispatch to if one has been provided to us. This could send requests to
32 // LoginScreenClient and also handle requests from the consumer via the
33 // LoginScreen interface.
34 class ASH_EXPORT LoginScreenController : public LoginScreen,
35                                          public KioskAppMenu,
36                                          public SystemTrayFocusObserver {
37  public:
38   // The current authentication stage. Used to get more verbose logging.
39   enum class AuthenticationStage {
40     kIdle,
41     kDoAuthenticate,
42     kUserCallback,
43   };
44 
45   using OnShownCallback = base::OnceCallback<void(bool did_show)>;
46   // Callback for authentication checks. |success| is nullopt if an
47   // authentication check did not run, otherwise it is true/false if auth
48   // succeeded/failed.
49   using OnAuthenticateCallback =
50       base::OnceCallback<void(base::Optional<bool> success)>;
51 
52   explicit LoginScreenController(SystemTrayNotifier* system_tray_notifier);
53   ~LoginScreenController() override;
54 
55   static void RegisterProfilePrefs(PrefRegistrySimple* registry, bool for_test);
56 
57   // Check to see if an authentication attempt is in-progress.
58   bool IsAuthenticating() const;
59 
60   // Hash the password and send AuthenticateUser request to LoginScreenClient.
61   // LoginScreenClient (in the chrome process) will do the authentication and
62   // request to show error messages in the screen if auth fails, or request to
63   // clear errors if auth succeeds.
64   void AuthenticateUserWithPasswordOrPin(const AccountId& account_id,
65                                          const std::string& password,
66                                          bool authenticated_by_pin,
67                                          OnAuthenticateCallback callback);
68   void AuthenticateUserWithEasyUnlock(const AccountId& account_id);
69   void AuthenticateUserWithChallengeResponse(const AccountId& account_id,
70                                              OnAuthenticateCallback callback);
71   bool ValidateParentAccessCode(const AccountId& account_id,
72                                 base::Time validation_time,
73                                 const std::string& code);
74   bool GetSecurityTokenPinRequestCanceled() const;
75   void HardlockPod(const AccountId& account_id);
76   void OnFocusPod(const AccountId& account_id);
77   void OnNoPodFocused();
78   void LoadWallpaper(const AccountId& account_id);
79   void SignOutUser();
80   void CancelAddUser();
81   void LoginAsGuest();
82   void OnMaxIncorrectPasswordAttempted(const AccountId& account_id);
83   void FocusLockScreenApps(bool reverse);
84   void ShowGaiaSignin(const AccountId& prefilled_account);
85   void OnRemoveUserWarningShown();
86   void RemoveUser(const AccountId& account_id);
87   void LaunchPublicSession(const AccountId& account_id,
88                            const std::string& locale,
89                            const std::string& input_method);
90   void RequestPublicSessionKeyboardLayouts(const AccountId& account_id,
91                                            const std::string& locale);
92   void HandleAccelerator(ash::LoginAcceleratorAction action);
93   void ShowAccountAccessHelpApp(gfx::NativeWindow parent_window);
94   void ShowParentAccessHelpApp(gfx::NativeWindow parent_window);
95   void ShowLockScreenNotificationSettings();
96   void FocusOobeDialog();
97   void NotifyUserActivity();
98 
99   // Enable or disable authentication for the debug overlay.
100   enum class ForceFailAuth { kOff, kImmediate, kDelayed };
set_force_fail_auth_for_debug_overlay(ForceFailAuth force_fail)101   void set_force_fail_auth_for_debug_overlay(ForceFailAuth force_fail) {
102     force_fail_auth_for_debug_overlay_ = force_fail;
103   }
104 
105   // LoginScreen:
106   void SetClient(LoginScreenClient* client) override;
107   LoginScreenModel* GetModel() override;
108   void ShowLockScreen() override;
109   void ShowLoginScreen() override;
110   void ShowKioskAppError(const std::string& message) override;
111   void FocusLoginShelf(bool reverse) override;
112   bool IsReadyForPassword() override;
113   void EnableAddUserButton(bool enable) override;
114   void EnableShutdownButton(bool enable) override;
115   void SetIsFirstSigninStep(bool is_first) override;
116   void ShowParentAccessButton(bool show) override;
117   void SetAllowLoginAsGuest(bool allow_guest) override;
118   std::unique_ptr<ScopedGuestButtonBlocker> GetScopedGuestButtonBlocker()
119       override;
120 
121   void RequestSecurityTokenPin(SecurityTokenPinRequest request) override;
122   void ClearSecurityTokenPinRequest() override;
123   bool SetLoginShelfGestureHandler(const base::string16& nudge_text,
124                                    const base::RepeatingClosure& fling_callback,
125                                    base::OnceClosure exit_callback) override;
126   void ClearLoginShelfGestureHandler() override;
127 
128   // KioskAppMenu:
129   void SetKioskApps(
130       const std::vector<KioskAppMenuEntry>& kiosk_apps,
131       const base::RepeatingCallback<void(const KioskAppMenuEntry&)>& launch_app,
132       const base::RepeatingClosure& on_show_menu) override;
133 
authentication_stage()134   AuthenticationStage authentication_stage() const {
135     return authentication_stage_;
136   }
137 
data_dispatcher()138   LoginDataDispatcher* data_dispatcher() { return &login_data_dispatcher_; }
139 
140   void NotifyLoginScreenShown();
141 
142  private:
143   void OnAuthenticateComplete(OnAuthenticateCallback callback, bool success);
144 
145   // Common code that is called when the login/lock screen is shown.
146   void OnShow();
147 
148   // SystemTrayFocusObserver:
149   void OnFocusLeavingSystemTray(bool reverse) override;
150 
151   LoginDataDispatcher login_data_dispatcher_;
152 
153   LoginScreenClient* client_ = nullptr;
154 
155   AuthenticationStage authentication_stage_ = AuthenticationStage::kIdle;
156 
157   SystemTrayNotifier* system_tray_notifier_;
158 
159   SecurityTokenRequestController security_token_request_controller_;
160 
161   // If set to false, all auth requests will forcibly fail.
162   ForceFailAuth force_fail_auth_for_debug_overlay_ = ForceFailAuth::kOff;
163 
164   base::WeakPtrFactory<LoginScreenController> weak_factory_{this};
165 
166   DISALLOW_COPY_AND_ASSIGN(LoginScreenController);
167 };
168 
169 }  // namespace ash
170 
171 #endif  // ASH_LOGIN_LOGIN_SCREEN_CONTROLLER_H_
172