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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_
7
8#include <string>
9
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/ime/input_method_manager.h"
20#include "chromeos/login/auth/auth_status_consumer.h"
21#include "chromeos/login/auth/user_context.h"
22#include "components/user_manager/user.h"
23#include "ui/base/accelerators/accelerator.h"
24
25namespace content {
26class WebUI;
27}
28
29namespace gfx {
30class Image;
31}
32
33namespace chromeos {
34
35class Authenticator;
36class ExtendedAuthenticator;
37class AuthFailure;
38class ScreenlockIconProvider;
39
40namespace test {
41class ScreenLockerTester;
42class ScreenLockerViewsTester;
43class WebUIScreenLockerTester;
44}  // namespace test
45
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.
49class ScreenLocker : public AuthStatusConsumer {
50 public:
51  explicit ScreenLocker(const user_manager::UserList& users);
52
53  // Returns the default instance if it has been created.
54  static ScreenLocker* default_screen_locker() {
55    return screen_locker_;
56  }
57
58  bool locked() const { return locked_; }
59
60  // Initialize and show the screen locker.
61  void Init();
62
63  // AuthStatusConsumer:
64  virtual void OnAuthFailure(const chromeos::AuthFailure& error) OVERRIDE;
65  virtual void OnAuthSuccess(const UserContext& user_context) OVERRIDE;
66
67  // Does actual unlocking once authentication is successful and all blocking
68  // animations are done.
69  void UnlockOnLoginSuccess();
70
71  // Authenticates the user with given |user_context|.
72  void Authenticate(const UserContext& user_context);
73
74  // Close message bubble to clear error messages.
75  void ClearErrors();
76
77  // Exit the chrome, which will sign out the current session.
78  void Signout();
79
80  // (Re)enable input field.
81  void EnableInput();
82
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"
85  // button is blocked.
86  void ShowErrorMessage(int error_msg_id,
87                        HelpAppLauncher::HelpTopic help_topic_id,
88                        bool sign_out_only);
89
90  // Returns the screen locker's delegate.
91  ScreenLockerDelegate* delegate() const { return delegate_.get(); }
92
93  // Returns the users to authenticate.
94  const user_manager::UserList& users() const { return users_; }
95
96  // Allow a AuthStatusConsumer to listen for
97  // the same login events that ScreenLocker does.
98  void SetLoginStatusConsumer(chromeos::AuthStatusConsumer* consumer);
99
100  // Returns WebUI associated with screen locker implementation or NULL if
101  // there isn't one.
102  content::WebUI* GetAssociatedWebUI();
103
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();
109
110  // Handles a request from the session manager to lock the screen.
111  static void HandleLockScreenRequest();
112
113  // Show the screen locker.
114  static void Show();
115
116  // Hide the screen locker.
117  static void Hide();
118
119  // Returns the tester
120  static test::ScreenLockerTester* GetTester();
121
122 private:
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;
128
129  struct AuthenticationParametersCapture {
130    UserContext user_context;
131  };
132
133  virtual ~ScreenLocker();
134
135  // Sets the authenticator.
136  void SetAuthenticator(Authenticator* authenticator);
137
138  // Called when the screen lock is ready.
139  void ScreenLockReady();
140
141  // Called when screen locker is safe to delete.
142  static void ScheduleDeletion();
143
144  // Returns true if |username| is found among logged in users.
145  bool IsUserLoggedIn(const std::string& username);
146
147  // Looks up user in unlock user list.
148  const user_manager::User* FindUnlockUser(const std::string& user_id);
149
150  // ScreenLockerDelegate instance in use.
151  scoped_ptr<ScreenLockerDelegate> delegate_;
152
153  // Users that can unlock the device.
154  user_manager::UserList users_;
155
156  // Used to authenticate the user to unlock.
157  scoped_refptr<Authenticator> authenticator_;
158
159  // Used to authenticate the user to unlock supervised users.
160  scoped_refptr<ExtendedAuthenticator> extended_authenticator_;
161
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.
165  bool locked_;
166
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_;
170
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_;
175
176  // Delegate to forward all login status events to.
177  // Tests can use this to receive login status events.
178  AuthStatusConsumer* auth_status_consumer_;
179
180  // Number of bad login attempts in a row.
181  int incorrect_passwords_count_;
182
183  // Copy of parameters passed to last call of OnLoginSuccess for usage in
184  // UnlockOnLoginSuccess().
185  scoped_ptr<AuthenticationParametersCapture> authentication_capture_;
186
187  // Provider for button icon set by the screenlockPrivate API.
188  scoped_ptr<ScreenlockIconProvider> screenlock_icon_provider_;
189
190  scoped_refptr<input_method::InputMethodManager::State> saved_ime_state_;
191
192  base::WeakPtrFactory<ScreenLocker> weak_factory_;
193
194  DISALLOW_COPY_AND_ASSIGN(ScreenLocker);
195};
196
197}  // namespace chromeos
198
199#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_LOCK_SCREEN_LOCKER_H_
200