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/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/login_status_consumer.h"
18 #include "chrome/browser/chromeos/login/screen_locker_delegate.h"
19 #include "chrome/browser/chromeos/login/user.h"
20 #include "ui/base/accelerators/accelerator.h"
34 class ScreenlockIconProvider
;
37 class ScreenLockerTester
;
38 class ScreenLockerViewsTester
;
39 class WebUIScreenLockerTester
;
42 // ScreenLocker creates a ScreenLockerDelegate which will display the lock UI.
43 // As well, it takes care of authenticating the user and managing a global
44 // instance of itself which will be deleted when the system is unlocked.
45 class ScreenLocker
: public LoginStatusConsumer
{
47 explicit ScreenLocker(const UserList
& users
);
49 // Returns the default instance if it has been created.
50 static ScreenLocker
* default_screen_locker() {
51 return screen_locker_
;
54 bool locked() const { return locked_
; }
56 // Initialize and show the screen locker.
59 // LoginStatusConsumer implements:
60 virtual void OnLoginFailure(const chromeos::LoginFailure
& error
) OVERRIDE
;
61 virtual void OnLoginSuccess(const UserContext
& user_context
) OVERRIDE
;
63 // Does actual unlocking once authentication is successful and all blocking
64 // animations are done.
65 void UnlockOnLoginSuccess();
67 // Authenticates the user with given |user_context|.
68 void Authenticate(const UserContext
& user_context
);
70 // Authenticates the only user with given |password|.
71 // Works only if there is only one logged in user in system.
72 // DEPRECATED: used only by tests.
73 void AuthenticateByPassword(const std::string
& password
);
75 // Close message bubble to clear error messages.
78 // (Re)enable input field.
81 // Exit the chrome, which will sign out the current session.
84 // Displays |message| in a banner on the lock screen.
85 void ShowBannerMessage(const std::string
& message
);
87 // Shows a button inside the user pod on the lock screen with an icon.
88 void ShowUserPodButton(const std::string
& username
,
89 const gfx::Image
& icon
,
90 const base::Closure
& click_callback
);
92 // Disables all UI needed and shows error bubble with |message|.
93 // If |sign_out_only| is true then all other input except "Sign Out"
95 void ShowErrorMessage(int error_msg_id
,
96 HelpAppLauncher::HelpTopic help_topic_id
,
99 // Returns the screen locker's delegate.
100 ScreenLockerDelegate
* delegate() const { return delegate_
.get(); }
102 // Returns the users to authenticate.
103 const UserList
& users() const { return users_
; }
105 // Allow a LoginStatusConsumer to listen for
106 // the same login events that ScreenLocker does.
107 void SetLoginStatusConsumer(chromeos::LoginStatusConsumer
* consumer
);
109 // Returns WebUI associated with screen locker implementation or NULL if
111 content::WebUI
* GetAssociatedWebUI();
113 // Initialize ScreenLocker class. It will listen to
114 // LOGIN_USER_CHANGED notification so that the screen locker accepts
115 // lock event only after a user is logged in.
116 static void InitClass();
118 // Show the screen locker.
121 // Hide the screen locker.
124 // Returns the tester
125 static test::ScreenLockerTester
* GetTester();
128 friend class base::DeleteHelper
<ScreenLocker
>;
129 friend class test::ScreenLockerTester
;
130 friend class test::ScreenLockerViewsTester
;
131 friend class test::WebUIScreenLockerTester
;
132 friend class ScreenLockerDelegate
;
134 struct AuthenticationParametersCapture
{
135 UserContext user_context
;
138 virtual ~ScreenLocker();
140 // Sets the authenticator.
141 void SetAuthenticator(Authenticator
* authenticator
);
143 // Called when the screen lock is ready.
144 void ScreenLockReady();
146 // Called when screen locker is safe to delete.
147 static void ScheduleDeletion();
149 // Returns true if |username| is found among logged in users.
150 bool IsUserLoggedIn(const std::string
& username
);
152 // ScreenLockerDelegate instance in use.
153 scoped_ptr
<ScreenLockerDelegate
> delegate_
;
155 // Users that can unlock the device.
158 // Used to authenticate the user to unlock.
159 scoped_refptr
<Authenticator
> 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_SCREEN_LOCKER_H_