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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_
7
8#include <string>
9
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"
20
21namespace content {
22class WebUI;
23}
24
25namespace chromeos {
26
27class Authenticator;
28class LoginFailure;
29
30namespace test {
31class ScreenLockerTester;
32class ScreenLockerViewsTester;
33class WebUIScreenLockerTester;
34}  // namespace test
35
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.
39class ScreenLocker : public LoginStatusConsumer {
40 public:
41  explicit ScreenLocker(const UserList& users);
42
43  // Returns the default instance if it has been created.
44  static ScreenLocker* default_screen_locker() {
45    return screen_locker_;
46  }
47
48  bool locked() const { return locked_; }
49
50  // Initialize and show the screen locker.
51  void Init();
52
53  // LoginStatusConsumer implements:
54  virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;
55  virtual void OnLoginSuccess(const UserContext& user_context,
56                              bool pending_requests,
57                              bool using_oauth) OVERRIDE;
58
59  // Does actual unlocking once authentication is successful and all blocking
60  // animations are done.
61  void UnlockOnLoginSuccess();
62
63  // Authenticates the user with given |user_context|.
64  void Authenticate(const UserContext& user_context);
65
66  // Authenticates the only user with given |password|.
67  // Works only if there is only one logged in user in system.
68  // DEPRECATED: used only by tests.
69  void AuthenticateByPassword(const std::string& password);
70
71  // Close message bubble to clear error messages.
72  void ClearErrors();
73
74  // (Re)enable input field.
75  void EnableInput();
76
77  // Exit the chrome, which will sign out the current session.
78  void Signout();
79
80  // Disables all UI needed and shows error bubble with |message|.
81  // If |sign_out_only| is true then all other input except "Sign Out"
82  // button is blocked.
83  void ShowErrorMessage(int error_msg_id,
84                        HelpAppLauncher::HelpTopic help_topic_id,
85                        bool sign_out_only);
86
87  // Returns the screen locker's delegate.
88  ScreenLockerDelegate* delegate() const { return delegate_.get(); }
89
90  // Returns the users to authenticate.
91  const UserList& users() const { return users_; }
92
93  // Allow a LoginStatusConsumer to listen for
94  // the same login events that ScreenLocker does.
95  void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer);
96
97  // Returns WebUI associated with screen locker implementation or NULL if
98  // there isn't one.
99  content::WebUI* GetAssociatedWebUI();
100
101  // Initialize ScreenLocker class. It will listen to
102  // LOGIN_USER_CHANGED notification so that the screen locker accepts
103  // lock event only after a user is logged in.
104  static void InitClass();
105
106  // Show the screen locker.
107  static void Show();
108
109  // Hide the screen locker.
110  static void Hide();
111
112  // Returns the tester
113  static test::ScreenLockerTester* GetTester();
114
115 private:
116  friend class base::DeleteHelper<ScreenLocker>;
117  friend class test::ScreenLockerTester;
118  friend class test::ScreenLockerViewsTester;
119  friend class test::WebUIScreenLockerTester;
120  friend class ScreenLockerDelegate;
121
122  struct AuthenticationParametersCapture {
123    std::string username;
124    bool pending_requests;
125    bool using_oauth;
126  };
127
128  virtual ~ScreenLocker();
129
130  // Sets the authenticator.
131  void SetAuthenticator(Authenticator* authenticator);
132
133  // Called when the screen lock is ready.
134  void ScreenLockReady();
135
136  // Called when screen locker is safe to delete.
137  static void ScheduleDeletion();
138
139  // Returns true if |username| is found among logged in users.
140  bool IsUserLoggedIn(const std::string& username);
141
142  // ScreenLockerDelegate instance in use.
143  scoped_ptr<ScreenLockerDelegate> delegate_;
144
145  // Logged in users. First user in list is active user.
146  const UserList& users_;
147
148  // Used to authenticate the user to unlock.
149  scoped_refptr<Authenticator> authenticator_;
150
151  // True if the screen is locked, or false otherwise.  This changes
152  // from false to true, but will never change from true to
153  // false. Instead, ScreenLocker object gets deleted when unlocked.
154  bool locked_;
155
156  // Reference to the single instance of the screen locker object.
157  // This is used to make sure there is only one screen locker instance.
158  static ScreenLocker* screen_locker_;
159
160  // The time when the screen locker object is created.
161  base::Time start_time_;
162  // The time when the authentication is started.
163  base::Time authentication_start_time_;
164
165  // Delegate to forward all login status events to.
166  // Tests can use this to receive login status events.
167  LoginStatusConsumer* login_status_consumer_;
168
169  // Number of bad login attempts in a row.
170  int incorrect_passwords_count_;
171
172  // Copy of parameters passed to last call of OnLoginSuccess for usage in
173  // UnlockOnLoginSuccess().
174  scoped_ptr<AuthenticationParametersCapture> authentication_capture_;
175
176  base::WeakPtrFactory<ScreenLocker> weak_factory_;
177
178  DISALLOW_COPY_AND_ASSIGN(ScreenLocker);
179};
180
181}  // namespace chromeos
182
183#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_
184