- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / screen_locker.h
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
21 namespace content {
22 class WebUI;
23 }
24
25 namespace chromeos {
26
27 class Authenticator;
28 class LoginFailure;
29
30 namespace test {
31 class ScreenLockerTester;
32 class ScreenLockerViewsTester;
33 class 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.
39 class 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) OVERRIDE;
56
57   // Does actual unlocking once authentication is successful and all blocking
58   // animations are done.
59   void UnlockOnLoginSuccess();
60
61   // Authenticates the user with given |user_context|.
62   void Authenticate(const UserContext& user_context);
63
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);
68
69   // Close message bubble to clear error messages.
70   void ClearErrors();
71
72   // (Re)enable input field.
73   void EnableInput();
74
75   // Exit the chrome, which will sign out the current session.
76   void Signout();
77
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"
80   // button is blocked.
81   void ShowErrorMessage(int error_msg_id,
82                         HelpAppLauncher::HelpTopic help_topic_id,
83                         bool sign_out_only);
84
85   // Returns the screen locker's delegate.
86   ScreenLockerDelegate* delegate() const { return delegate_.get(); }
87
88   // Returns the users to authenticate.
89   const UserList& users() const { return users_; }
90
91   // Allow a LoginStatusConsumer to listen for
92   // the same login events that ScreenLocker does.
93   void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer);
94
95   // Returns WebUI associated with screen locker implementation or NULL if
96   // there isn't one.
97   content::WebUI* GetAssociatedWebUI();
98
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();
103
104   // Show the screen locker.
105   static void Show();
106
107   // Hide the screen locker.
108   static void Hide();
109
110   // Returns the tester
111   static test::ScreenLockerTester* GetTester();
112
113  private:
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;
119
120   struct AuthenticationParametersCapture {
121     UserContext user_context;
122   };
123
124   virtual ~ScreenLocker();
125
126   // Sets the authenticator.
127   void SetAuthenticator(Authenticator* authenticator);
128
129   // Called when the screen lock is ready.
130   void ScreenLockReady();
131
132   // Called when screen locker is safe to delete.
133   static void ScheduleDeletion();
134
135   // Returns true if |username| is found among logged in users.
136   bool IsUserLoggedIn(const std::string& username);
137
138   // ScreenLockerDelegate instance in use.
139   scoped_ptr<ScreenLockerDelegate> delegate_;
140
141   // Users that can unlock the device.
142   UserList users_;
143
144   // Used to authenticate the user to unlock.
145   scoped_refptr<Authenticator> authenticator_;
146
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.
150   bool locked_;
151
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_;
155
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_;
160
161   // Delegate to forward all login status events to.
162   // Tests can use this to receive login status events.
163   LoginStatusConsumer* login_status_consumer_;
164
165   // Number of bad login attempts in a row.
166   int incorrect_passwords_count_;
167
168   // Copy of parameters passed to last call of OnLoginSuccess for usage in
169   // UnlockOnLoginSuccess().
170   scoped_ptr<AuthenticationParametersCapture> authentication_capture_;
171
172   base::WeakPtrFactory<ScreenLocker> weak_factory_;
173
174   DISALLOW_COPY_AND_ASSIGN(ScreenLocker);
175 };
176
177 }  // namespace chromeos
178
179 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_