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/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" 21 22 namespace content { 23 class WebUI; 24 } 25 26 namespace gfx { 27 class Image; 28 } 29 30 namespace chromeos { 31 32 class Authenticator; 33 class LoginFailure; 34 class ScreenlockIconProvider; 35 36 namespace test { 37 class ScreenLockerTester; 38 class ScreenLockerViewsTester; 39 class WebUIScreenLockerTester; 40 } // namespace test 41 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 { 46 public: 47 explicit ScreenLocker(const UserList& users); 48 49 // Returns the default instance if it has been created. default_screen_locker()50 static ScreenLocker* default_screen_locker() { 51 return screen_locker_; 52 } 53 locked()54 bool locked() const { return locked_; } 55 56 // Initialize and show the screen locker. 57 void Init(); 58 59 // LoginStatusConsumer implements: 60 virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE; 61 virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE; 62 63 // Does actual unlocking once authentication is successful and all blocking 64 // animations are done. 65 void UnlockOnLoginSuccess(); 66 67 // Authenticates the user with given |user_context|. 68 void Authenticate(const UserContext& user_context); 69 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); 74 75 // Close message bubble to clear error messages. 76 void ClearErrors(); 77 78 // (Re)enable input field. 79 void EnableInput(); 80 81 // Exit the chrome, which will sign out the current session. 82 void Signout(); 83 84 // Displays |message| in a banner on the lock screen. 85 void ShowBannerMessage(const std::string& message); 86 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); 91 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" 94 // button is blocked. 95 void ShowErrorMessage(int error_msg_id, 96 HelpAppLauncher::HelpTopic help_topic_id, 97 bool sign_out_only); 98 99 // Returns the screen locker's delegate. delegate()100 ScreenLockerDelegate* delegate() const { return delegate_.get(); } 101 102 // Returns the users to authenticate. users()103 const UserList& users() const { return users_; } 104 105 // Allow a LoginStatusConsumer to listen for 106 // the same login events that ScreenLocker does. 107 void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer); 108 109 // Returns WebUI associated with screen locker implementation or NULL if 110 // there isn't one. 111 content::WebUI* GetAssociatedWebUI(); 112 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(); 117 118 // Show the screen locker. 119 static void Show(); 120 121 // Hide the screen locker. 122 static void Hide(); 123 124 // Returns the tester 125 static test::ScreenLockerTester* GetTester(); 126 127 private: 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; 133 134 struct AuthenticationParametersCapture { 135 UserContext user_context; 136 }; 137 138 virtual ~ScreenLocker(); 139 140 // Sets the authenticator. 141 void SetAuthenticator(Authenticator* authenticator); 142 143 // Called when the screen lock is ready. 144 void ScreenLockReady(); 145 146 // Called when screen locker is safe to delete. 147 static void ScheduleDeletion(); 148 149 // Returns true if |username| is found among logged in users. 150 bool IsUserLoggedIn(const std::string& username); 151 152 // ScreenLockerDelegate instance in use. 153 scoped_ptr<ScreenLockerDelegate> delegate_; 154 155 // Users that can unlock the device. 156 UserList users_; 157 158 // Used to authenticate the user to unlock. 159 scoped_refptr<Authenticator> authenticator_; 160 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. 164 bool locked_; 165 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_; 169 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_; 174 175 // Delegate to forward all login status events to. 176 // Tests can use this to receive login status events. 177 LoginStatusConsumer* login_status_consumer_; 178 179 // Number of bad login attempts in a row. 180 int incorrect_passwords_count_; 181 182 // Copy of parameters passed to last call of OnLoginSuccess for usage in 183 // UnlockOnLoginSuccess(). 184 scoped_ptr<AuthenticationParametersCapture> authentication_capture_; 185 186 // Provider for button icon set by the screenlockPrivate API. 187 scoped_ptr<ScreenlockIconProvider> screenlock_icon_provider_; 188 189 base::WeakPtrFactory<ScreenLocker> weak_factory_; 190 191 DISALLOW_COPY_AND_ASSIGN(ScreenLocker); 192 }; 193 194 } // namespace chromeos 195 196 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SCREEN_LOCKER_H_ 197