1 // Copyright (c) 2013 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 CHROMEOS_LOGIN_LOGIN_STATE_H_ 6 #define CHROMEOS_LOGIN_LOGIN_STATE_H_ 7 8 #include "base/basictypes.h" 9 #include "base/observer_list.h" 10 #include "chromeos/chromeos_export.h" 11 12 namespace chromeos { 13 14 // Tracks the login state of chrome, accessible to Ash and other chromeos code. 15 class CHROMEOS_EXPORT LoginState { 16 public: 17 enum LoggedInState { 18 LOGGED_IN_NONE, // Not logged in 19 LOGGED_IN_SAFE_MODE, // Not logged in and login not allowed for non-owners 20 LOGGED_IN_ACTIVE // A user has logged in 21 }; 22 23 enum LoggedInUserType { 24 LOGGED_IN_USER_NONE, // User is not logged in 25 LOGGED_IN_USER_REGULAR, // A regular user is logged in 26 LOGGED_IN_USER_OWNER, // The owner of the device is logged in 27 LOGGED_IN_USER_GUEST, // A guest is logged in (i.e. incognito) 28 LOGGED_IN_USER_RETAIL_MODE, // Is in retail mode 29 LOGGED_IN_USER_PUBLIC_ACCOUNT, // A user is logged in to a public session. 30 LOGGED_IN_USER_SUPERVISED, // A supervised user is logged in 31 LOGGED_IN_USER_KIOSK_APP // Is in kiosk app mode 32 }; 33 34 class Observer { 35 public: 36 // Called when either the login state or the logged in user type changes. 37 virtual void LoggedInStateChanged() = 0; 38 39 protected: ~Observer()40 virtual ~Observer() {} 41 }; 42 43 // Manage singleton instance. 44 static void Initialize(); 45 static void Shutdown(); 46 static LoginState* Get(); 47 static bool IsInitialized(); 48 49 // Add/remove observers. 50 void AddObserver(Observer* observer); 51 void RemoveObserver(Observer* observer); 52 53 // Sets the logged in state, user type, and primary user hash when the 54 // primary user initialy logs in. Also notifies observers. 55 void SetLoggedInStateAndPrimaryUser( 56 LoggedInState state, 57 LoggedInUserType type, 58 const std::string& primary_user_hash); 59 60 // Sets the logged in state and user type. Also notifies observers. Used 61 // in tests or situations where there is no primary user (e.g. from the 62 // login screen). 63 void SetLoggedInState(LoggedInState state, LoggedInUserType type); 64 65 // Gets the logged in user type. 66 LoggedInUserType GetLoggedInUserType() const; 67 68 // Returns true if a user is considered to be logged in. 69 bool IsUserLoggedIn() const; 70 71 // Returns true if |logged_in_state_| is safe mode (i.e. the user is not yet 72 // logged in, and only the owner will be allowed to log in). 73 bool IsInSafeMode() const; 74 75 // Returns true if logged in to a guest session. 76 bool IsGuestSessionUser() const; 77 78 // Returns true if logged in to a public session. 79 bool IsPublicSessionUser() const; 80 81 // Returns true if logged in as a kiosk app. 82 bool IsKioskApp() const; 83 84 // Whether a network profile is created for the user. 85 bool UserHasNetworkProfile() const; 86 87 // Returns true if the user is an authenticated user (i.e. the user is not 88 // using an anonymous session like public or guest session) 89 bool IsUserAuthenticated() const; 90 91 // Returns true if the user is authenticated by logging into Google account 92 // (i.e. not using an anonymous nor supervised session). 93 bool IsUserGaiaAuthenticated() const; 94 set_always_logged_in(bool always_logged_in)95 void set_always_logged_in(bool always_logged_in) { 96 always_logged_in_ = always_logged_in; 97 } 98 primary_user_hash()99 const std::string& primary_user_hash() const { return primary_user_hash_; } 100 101 private: 102 LoginState(); 103 virtual ~LoginState(); 104 105 void NotifyObservers(); 106 107 LoggedInState logged_in_state_; 108 LoggedInUserType logged_in_user_type_; 109 std::string primary_user_hash_; 110 ObserverList<Observer> observer_list_; 111 112 // If true, it always thinks the current status as logged in. Set to true by 113 // default running on a Linux desktop without flags and test cases. To test 114 // behaviors with a specific login state, call set_always_logged_in(false). 115 bool always_logged_in_; 116 117 DISALLOW_COPY_AND_ASSIGN(LoginState); 118 }; 119 120 } // namespace chromeos 121 122 #endif // CHROMEOS_LOGIN_LOGIN_STATE_H_ 123