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/auth/login_status_consumer.h"
17 #include "chrome/browser/chromeos/login/auth/user_context.h"
18 #include "chrome/browser/chromeos/login/help_app_launcher.h"
19 #include "chrome/browser/chromeos/login/lock/screen_locker_delegate.h"
20 #include "chrome/browser/chromeos/login/ui/login_display.h"
21 #include "chrome/browser/chromeos/login/users/user.h"
22 #include "ui/base/accelerators/accelerator.h"
35 class ExtendedAuthenticator;
37 class ScreenlockIconProvider;
40 class ScreenLockerTester;
41 class ScreenLockerViewsTester;
42 class WebUIScreenLockerTester;
45 // ScreenLocker creates a ScreenLockerDelegate which will display the lock UI.
46 // As well, it takes care of authenticating the user and managing a global
47 // instance of itself which will be deleted when the system is unlocked.
48 class ScreenLocker : public LoginStatusConsumer {
50 explicit ScreenLocker(const UserList& users);
52 // Returns the default instance if it has been created.
53 static ScreenLocker* default_screen_locker() {
54 return screen_locker_;
57 bool locked() const { return locked_; }
59 // Initialize and show the screen locker.
62 // LoginStatusConsumer:
63 virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;
64 virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE;
66 // Does actual unlocking once authentication is successful and all blocking
67 // animations are done.
68 void UnlockOnLoginSuccess();
70 // Authenticates the user with given |user_context|.
71 void Authenticate(const UserContext& user_context);
73 // Close message bubble to clear error messages.
76 // Exit the chrome, which will sign out the current session.
79 // (Re)enable input field.
82 // Disables all UI needed and shows error bubble with |message|.
83 // If |sign_out_only| is true then all other input except "Sign Out"
85 void ShowErrorMessage(int error_msg_id,
86 HelpAppLauncher::HelpTopic help_topic_id,
89 // Returns the screen locker's delegate.
90 ScreenLockerDelegate* delegate() const { return delegate_.get(); }
92 // Returns the users to authenticate.
93 const UserList& users() const { return users_; }
95 // Allow a LoginStatusConsumer to listen for
96 // the same login events that ScreenLocker does.
97 void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer);
99 // Returns WebUI associated with screen locker implementation or NULL if
101 content::WebUI* GetAssociatedWebUI();
103 // Initialize or uninitialize the ScreenLocker class. It listens to
104 // NOTIFICATION_SESSION_STARTED so that the screen locker accepts lock
105 // requests only after a user has logged in.
106 static void InitClass();
107 static void ShutDownClass();
109 // Handles a request from the session manager to lock the screen.
110 static void HandleLockScreenRequest();
112 // Show the screen locker.
115 // Hide the screen locker.
118 // Returns the tester
119 static test::ScreenLockerTester* GetTester();
122 friend class base::DeleteHelper<ScreenLocker>;
123 friend class test::ScreenLockerTester;
124 friend class test::ScreenLockerViewsTester;
125 friend class test::WebUIScreenLockerTester;
126 friend class ScreenLockerDelegate;
128 struct AuthenticationParametersCapture {
129 UserContext user_context;
132 virtual ~ScreenLocker();
134 // Sets the authenticator.
135 void SetAuthenticator(Authenticator* authenticator);
137 // Called when the screen lock is ready.
138 void ScreenLockReady();
140 // Called when screen locker is safe to delete.
141 static void ScheduleDeletion();
143 // Returns true if |username| is found among logged in users.
144 bool IsUserLoggedIn(const std::string& username);
146 // Looks up user in unlock user list.
147 const User* FindUnlockUser(const std::string& user_id);
149 // ScreenLockerDelegate instance in use.
150 scoped_ptr<ScreenLockerDelegate> delegate_;
152 // Users that can unlock the device.
155 // Used to authenticate the user to unlock.
156 scoped_refptr<Authenticator> authenticator_;
158 // Used to authenticate the user to unlock supervised users.
159 scoped_refptr<ExtendedAuthenticator> extended_authenticator_;
161 // True if the screen is locked, or false otherwise. This changes
162 // from false to true, but will never change from true to
163 // false. Instead, ScreenLocker object gets deleted when unlocked.
166 // Reference to the single instance of the screen locker object.
167 // This is used to make sure there is only one screen locker instance.
168 static ScreenLocker* screen_locker_;
170 // The time when the screen locker object is created.
171 base::Time start_time_;
172 // The time when the authentication is started.
173 base::Time authentication_start_time_;
175 // Delegate to forward all login status events to.
176 // Tests can use this to receive login status events.
177 LoginStatusConsumer* login_status_consumer_;
179 // Number of bad login attempts in a row.
180 int incorrect_passwords_count_;
182 // Copy of parameters passed to last call of OnLoginSuccess for usage in
183 // UnlockOnLoginSuccess().
184 scoped_ptr<AuthenticationParametersCapture> authentication_capture_;
186 // Provider for button icon set by the screenlockPrivate API.
187 scoped_ptr<ScreenlockIconProvider> screenlock_icon_provider_;
189 base::WeakPtrFactory<ScreenLocker> weak_factory_;
191 DISALLOW_COPY_AND_ASSIGN(ScreenLocker);
194 } // namespace chromeos
196 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_