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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequenced_task_runner_helpers.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/chromeos/login/help_app_launcher.h"
16 #include "chrome/browser/chromeos/login/login_status_consumer.h"
17 #include "chrome/browser/chromeos/login/screen_locker_delegate.h"
18 #include "chrome/browser/chromeos/login/user.h"
19 #include "ui/base/accelerators/accelerator.h"
31 class ScreenLockerTester;
32 class ScreenLockerViewsTester;
33 class WebUIScreenLockerTester;
36 // ScreenLocker creates a ScreenLockerDelegate which will display the lock UI.
37 // As well, it takes care of authenticating the user and managing a global
38 // instance of itself which will be deleted when the system is unlocked.
39 class ScreenLocker : public LoginStatusConsumer {
41 explicit ScreenLocker(const UserList& users);
43 // Returns the default instance if it has been created.
44 static ScreenLocker* default_screen_locker() {
45 return screen_locker_;
48 bool locked() const { return locked_; }
50 // Initialize and show the screen locker.
53 // LoginStatusConsumer implements:
54 virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;
55 virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE;
57 // Does actual unlocking once authentication is successful and all blocking
58 // animations are done.
59 void UnlockOnLoginSuccess();
61 // Authenticates the user with given |user_context|.
62 void Authenticate(const UserContext& user_context);
64 // Authenticates the only user with given |password|.
65 // Works only if there is only one logged in user in system.
66 // DEPRECATED: used only by tests.
67 void AuthenticateByPassword(const std::string& password);
69 // Close message bubble to clear error messages.
72 // (Re)enable input field.
75 // Exit the chrome, which will sign out the current session.
78 // Disables all UI needed and shows error bubble with |message|.
79 // If |sign_out_only| is true then all other input except "Sign Out"
81 void ShowErrorMessage(int error_msg_id,
82 HelpAppLauncher::HelpTopic help_topic_id,
85 // Returns the screen locker's delegate.
86 ScreenLockerDelegate* delegate() const { return delegate_.get(); }
88 // Returns the users to authenticate.
89 const UserList& users() const { return users_; }
91 // Allow a LoginStatusConsumer to listen for
92 // the same login events that ScreenLocker does.
93 void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer);
95 // Returns WebUI associated with screen locker implementation or NULL if
97 content::WebUI* GetAssociatedWebUI();
99 // Initialize ScreenLocker class. It will listen to
100 // LOGIN_USER_CHANGED notification so that the screen locker accepts
101 // lock event only after a user is logged in.
102 static void InitClass();
104 // Show the screen locker.
107 // Hide the screen locker.
110 // Returns the tester
111 static test::ScreenLockerTester* GetTester();
114 friend class base::DeleteHelper<ScreenLocker>;
115 friend class test::ScreenLockerTester;
116 friend class test::ScreenLockerViewsTester;
117 friend class test::WebUIScreenLockerTester;
118 friend class ScreenLockerDelegate;
120 struct AuthenticationParametersCapture {
121 UserContext user_context;
124 virtual ~ScreenLocker();
126 // Sets the authenticator.
127 void SetAuthenticator(Authenticator* authenticator);
129 // Called when the screen lock is ready.
130 void ScreenLockReady();
132 // Called when screen locker is safe to delete.
133 static void ScheduleDeletion();
135 // Returns true if |username| is found among logged in users.
136 bool IsUserLoggedIn(const std::string& username);
138 // ScreenLockerDelegate instance in use.
139 scoped_ptr<ScreenLockerDelegate> delegate_;
141 // Users that can unlock the device.
144 // Used to authenticate the user to unlock.
145 scoped_refptr<Authenticator> authenticator_;
147 // True if the screen is locked, or false otherwise. This changes
148 // from false to true, but will never change from true to
149 // false. Instead, ScreenLocker object gets deleted when unlocked.
152 // Reference to the single instance of the screen locker object.
153 // This is used to make sure there is only one screen locker instance.
154 static ScreenLocker* screen_locker_;
156 // The time when the screen locker object is created.
157 base::Time start_time_;
158 // The time when the authentication is started.
159 base::Time authentication_start_time_;
161 // Delegate to forward all login status events to.
162 // Tests can use this to receive login status events.
163 LoginStatusConsumer* login_status_consumer_;
165 // Number of bad login attempts in a row.
166 int incorrect_passwords_count_;
168 // Copy of parameters passed to last call of OnLoginSuccess for usage in
169 // UnlockOnLoginSuccess().
170 scoped_ptr<AuthenticationParametersCapture> authentication_capture_;
172 base::WeakPtrFactory<ScreenLocker> weak_factory_;
174 DISALLOW_COPY_AND_ASSIGN(ScreenLocker);
177 } // namespace chromeos
179 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_