Home | History | Annotate | Download | only in lock
      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/auth/login_status_consumer.h"
     17 #include "chrome/browser/chromeos/login/auth/user_context.h"
     18 #include "chrome/browser/chromeos/login/help_app_launcher.h"
     19 #include "chrome/browser/chromeos/login/lock/screen_locker_delegate.h"
     20 #include "chrome/browser/chromeos/login/ui/login_display.h"
     21 #include "chrome/browser/chromeos/login/users/user.h"
     22 #include "ui/base/accelerators/accelerator.h"
     23 
     24 namespace content {
     25 class WebUI;
     26 }
     27 
     28 namespace gfx {
     29 class Image;
     30 }
     31 
     32 namespace chromeos {
     33 
     34 class Authenticator;
     35 class ExtendedAuthenticator;
     36 class LoginFailure;
     37 class ScreenlockIconProvider;
     38 
     39 namespace test {
     40 class ScreenLockerTester;
     41 class ScreenLockerViewsTester;
     42 class WebUIScreenLockerTester;
     43 }  // namespace test
     44 
     45 // ScreenLocker creates a ScreenLockerDelegate which will display the lock UI.
     46 // As well, it takes care of authenticating the user and managing a global
     47 // instance of itself which will be deleted when the system is unlocked.
     48 class ScreenLocker : public LoginStatusConsumer {
     49  public:
     50   explicit ScreenLocker(const UserList& users);
     51 
     52   // Returns the default instance if it has been created.
     53   static ScreenLocker* default_screen_locker() {
     54     return screen_locker_;
     55   }
     56 
     57   bool locked() const { return locked_; }
     58 
     59   // Initialize and show the screen locker.
     60   void Init();
     61 
     62   // LoginStatusConsumer:
     63   virtual void OnLoginFailure(const chromeos::LoginFailure& error) OVERRIDE;
     64   virtual void OnLoginSuccess(const UserContext& user_context) OVERRIDE;
     65 
     66   // Does actual unlocking once authentication is successful and all blocking
     67   // animations are done.
     68   void UnlockOnLoginSuccess();
     69 
     70   // Authenticates the user with given |user_context|.
     71   void Authenticate(const UserContext& user_context);
     72 
     73   // Close message bubble to clear error messages.
     74   void ClearErrors();
     75 
     76   // Exit the chrome, which will sign out the current session.
     77   void Signout();
     78 
     79   // (Re)enable input field.
     80   void EnableInput();
     81 
     82   // Disables all UI needed and shows error bubble with |message|.
     83   // If |sign_out_only| is true then all other input except "Sign Out"
     84   // button is blocked.
     85   void ShowErrorMessage(int error_msg_id,
     86                         HelpAppLauncher::HelpTopic help_topic_id,
     87                         bool sign_out_only);
     88 
     89   // Returns the screen locker's delegate.
     90   ScreenLockerDelegate* delegate() const { return delegate_.get(); }
     91 
     92   // Returns the users to authenticate.
     93   const UserList& users() const { return users_; }
     94 
     95   // Allow a LoginStatusConsumer to listen for
     96   // the same login events that ScreenLocker does.
     97   void SetLoginStatusConsumer(chromeos::LoginStatusConsumer* consumer);
     98 
     99   // Returns WebUI associated with screen locker implementation or NULL if
    100   // there isn't one.
    101   content::WebUI* GetAssociatedWebUI();
    102 
    103   // Initialize or uninitialize the ScreenLocker class. It listens to
    104   // NOTIFICATION_SESSION_STARTED so that the screen locker accepts lock
    105   // requests only after a user has logged in.
    106   static void InitClass();
    107   static void ShutDownClass();
    108 
    109   // Handles a request from the session manager to lock the screen.
    110   static void HandleLockScreenRequest();
    111 
    112   // Show the screen locker.
    113   static void Show();
    114 
    115   // Hide the screen locker.
    116   static void Hide();
    117 
    118   // Returns the tester
    119   static test::ScreenLockerTester* GetTester();
    120 
    121  private:
    122   friend class base::DeleteHelper<ScreenLocker>;
    123   friend class test::ScreenLockerTester;
    124   friend class test::ScreenLockerViewsTester;
    125   friend class test::WebUIScreenLockerTester;
    126   friend class ScreenLockerDelegate;
    127 
    128   struct AuthenticationParametersCapture {
    129     UserContext user_context;
    130   };
    131 
    132   virtual ~ScreenLocker();
    133 
    134   // Sets the authenticator.
    135   void SetAuthenticator(Authenticator* authenticator);
    136 
    137   // Called when the screen lock is ready.
    138   void ScreenLockReady();
    139 
    140   // Called when screen locker is safe to delete.
    141   static void ScheduleDeletion();
    142 
    143   // Returns true if |username| is found among logged in users.
    144   bool IsUserLoggedIn(const std::string& username);
    145 
    146   // Looks up user in unlock user list.
    147   const User* FindUnlockUser(const std::string& user_id);
    148 
    149   // ScreenLockerDelegate instance in use.
    150   scoped_ptr<ScreenLockerDelegate> delegate_;
    151 
    152   // Users that can unlock the device.
    153   UserList users_;
    154 
    155   // Used to authenticate the user to unlock.
    156   scoped_refptr<Authenticator> authenticator_;
    157 
    158   // Used to authenticate the user to unlock supervised users.
    159   scoped_refptr<ExtendedAuthenticator> extended_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_LOCK_SCREEN_LOCKER_H_
    197