1 // Copyright 2014 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_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequenced_task_runner_helpers.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/chromeos/login/help_app_launcher.h"
17 #include "chrome/browser/chromeos/login/lock/screen_locker_delegate.h"
18 #include "chrome/browser/chromeos/login/ui/login_display.h"
19 #include "chromeos/login/auth/auth_status_consumer.h"
20 #include "chromeos/login/auth/user_context.h"
21 #include "components/user_manager/user.h"
22 #include "ui/base/accelerators/accelerator.h"
23 #include "ui/base/ime/chromeos/input_method_manager.h"
36 class ExtendedAuthenticator
;
38 class ScreenlockIconProvider
;
41 class ScreenLockerTester
;
42 class ScreenLockerViewsTester
;
43 class WebUIScreenLockerTester
;
46 // ScreenLocker creates a ScreenLockerDelegate which will display the lock UI.
47 // As well, it takes care of authenticating the user and managing a global
48 // instance of itself which will be deleted when the system is unlocked.
49 class ScreenLocker
: public AuthStatusConsumer
{
51 explicit ScreenLocker(const user_manager::UserList
& users
);
53 // Returns the default instance if it has been created.
54 static ScreenLocker
* default_screen_locker() {
55 return screen_locker_
;
58 bool locked() const { return locked_
; }
60 // Initialize and show the screen locker.
63 // AuthStatusConsumer:
64 void OnAuthFailure(const chromeos::AuthFailure
& error
) override
;
65 void OnAuthSuccess(const UserContext
& user_context
) override
;
67 // Does actual unlocking once authentication is successful and all blocking
68 // animations are done.
69 void UnlockOnLoginSuccess();
71 // Authenticates the user with given |user_context|.
72 void Authenticate(const UserContext
& user_context
);
74 // Close message bubble to clear error messages.
77 // Exit the chrome, which will sign out the current session.
80 // (Re)enable input field.
83 // Disables all UI needed and shows error bubble with |message|.
84 // If |sign_out_only| is true then all other input except "Sign Out"
86 void ShowErrorMessage(int error_msg_id
,
87 HelpAppLauncher::HelpTopic help_topic_id
,
90 // Returns the screen locker's delegate.
91 ScreenLockerDelegate
* delegate() const { return delegate_
.get(); }
93 // Returns the users to authenticate.
94 const user_manager::UserList
& users() const { return users_
; }
96 // Allow a AuthStatusConsumer to listen for
97 // the same login events that ScreenLocker does.
98 void SetLoginStatusConsumer(chromeos::AuthStatusConsumer
* consumer
);
100 // Returns WebUI associated with screen locker implementation or NULL if
102 content::WebUI
* GetAssociatedWebUI();
104 // Initialize or uninitialize the ScreenLocker class. It listens to
105 // NOTIFICATION_SESSION_STARTED so that the screen locker accepts lock
106 // requests only after a user has logged in.
107 static void InitClass();
108 static void ShutDownClass();
110 // Handles a request from the session manager to lock the screen.
111 static void HandleLockScreenRequest();
113 // Show the screen locker.
116 // Hide the screen locker.
119 // Returns the tester
120 static test::ScreenLockerTester
* GetTester();
123 friend class base::DeleteHelper
<ScreenLocker
>;
124 friend class test::ScreenLockerTester
;
125 friend class test::ScreenLockerViewsTester
;
126 friend class test::WebUIScreenLockerTester
;
127 friend class ScreenLockerDelegate
;
129 struct AuthenticationParametersCapture
{
130 UserContext user_context
;
133 ~ScreenLocker() override
;
135 // Sets the authenticator.
136 void SetAuthenticator(Authenticator
* authenticator
);
138 // Called when the screen lock is ready.
139 void ScreenLockReady();
141 // Called when screen locker is safe to delete.
142 static void ScheduleDeletion();
144 // Returns true if |username| is found among logged in users.
145 bool IsUserLoggedIn(const std::string
& username
);
147 // Looks up user in unlock user list.
148 const user_manager::User
* FindUnlockUser(const std::string
& user_id
);
150 // ScreenLockerDelegate instance in use.
151 scoped_ptr
<ScreenLockerDelegate
> delegate_
;
153 // Users that can unlock the device.
154 user_manager::UserList users_
;
156 // Used to authenticate the user to unlock.
157 scoped_refptr
<Authenticator
> authenticator_
;
159 // Used to authenticate the user to unlock supervised users.
160 scoped_refptr
<ExtendedAuthenticator
> extended_authenticator_
;
162 // True if the screen is locked, or false otherwise. This changes
163 // from false to true, but will never change from true to
164 // false. Instead, ScreenLocker object gets deleted when unlocked.
167 // Reference to the single instance of the screen locker object.
168 // This is used to make sure there is only one screen locker instance.
169 static ScreenLocker
* screen_locker_
;
171 // The time when the screen locker object is created.
172 base::Time start_time_
;
173 // The time when the authentication is started.
174 base::Time authentication_start_time_
;
176 // Delegate to forward all login status events to.
177 // Tests can use this to receive login status events.
178 AuthStatusConsumer
* auth_status_consumer_
;
180 // Number of bad login attempts in a row.
181 int incorrect_passwords_count_
;
183 // Copy of parameters passed to last call of OnLoginSuccess for usage in
184 // UnlockOnLoginSuccess().
185 scoped_ptr
<AuthenticationParametersCapture
> authentication_capture_
;
187 // Provider for button icon set by the screenlockPrivate API.
188 scoped_ptr
<ScreenlockIconProvider
> screenlock_icon_provider_
;
190 scoped_refptr
<input_method::InputMethodManager::State
> saved_ime_state_
;
192 base::WeakPtrFactory
<ScreenLocker
> weak_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(ScreenLocker
);
197 } // namespace chromeos
199 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_