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_USER_MANAGER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_ 7 8 #include <string> 9 10 #include "chrome/browser/chromeos/login/user.h" 11 #include "chrome/browser/chromeos/login/user_flow.h" 12 13 class PrefRegistrySimple; 14 15 namespace chromeos { 16 17 class RemoveUserDelegate; 18 class UserImageManager; 19 20 // Base class for UserManagerImpl - provides a mechanism for discovering users 21 // who have logged into this Chrome OS device before and updating that list. 22 class UserManager { 23 public: 24 // Status of merge sessions process which is responsible for exchanging 25 // user OAuth2 refresh token for GAIA cookies. 26 enum MergeSessionState { 27 // Session merge hasn't started yet. 28 MERGE_STATUS_NOT_STARTED, 29 // Session merge is in process. 30 MERGE_STATUS_IN_PROCESS, 31 // Session merge is completed. 32 MERGE_STATUS_DONE, 33 }; 34 35 // Interface that observers of UserManager must implement in order 36 // to receive notification when local state preferences is changed 37 class Observer { 38 public: 39 // Called when the local state preferences is changed. 40 virtual void LocalStateChanged(UserManager* user_manager); 41 42 // Called when merge session state is changed. 43 virtual void MergeSessionStateChanged(MergeSessionState state); 44 45 protected: 46 virtual ~Observer(); 47 }; 48 49 // TODO(nkostylev): Refactor and move this observer out of UserManager. 50 // Observer interface that defines methods used to notify on user session / 51 // active user state changes. Default implementation is empty. 52 class UserSessionStateObserver { 53 public: 54 // Called when active user has changed. 55 virtual void ActiveUserChanged(const User* active_user); 56 57 // Called right before notifying on user change so that those who rely 58 // on user_id hash would be accessing up-to-date value. 59 virtual void ActiveUserHashChanged(const std::string& hash); 60 61 // Called when UserManager finishes restoring user sessions after crash. 62 virtual void PendingUserSessionsRestoreFinished(); 63 64 protected: 65 virtual ~UserSessionStateObserver(); 66 }; 67 68 // Username for stub login when not running on ChromeOS. 69 static const char kStubUser[]; 70 71 // Magic e-mail addresses are bad. They exist here because some code already 72 // depends on them and it is hard to figure out what. Any user types added in 73 // the future should be identified by a new |UserType|, not a new magic e-mail 74 // address. 75 // Username for Guest session user. 76 static const char kGuestUserName[]; 77 78 // Domain that is used for all locally managed users. 79 static const char kLocallyManagedUserDomain[]; 80 81 // The retail mode user has a magic, domainless e-mail address. 82 static const char kRetailModeUserName[]; 83 84 // Creates the singleton instance. This method is not thread-safe and must be 85 // called from the main UI thread. 86 static void Initialize(); 87 88 // Checks whether the singleton instance has been created already. This method 89 // is not thread-safe and must be called from the main UI thread. 90 static bool IsInitialized(); 91 92 // Shuts down the UserManager. After this method has been called, the 93 // singleton has unregistered itself as an observer but remains available so 94 // that other classes can access it during their shutdown. This method is not 95 // thread-safe and must be called from the main UI thread. 96 virtual void Shutdown() = 0; 97 98 // Destroys the singleton instance. Always call Shutdown() first. This method 99 // is not thread-safe and must be called from the main UI thread. 100 static void Destroy(); 101 102 // Returns the singleton instance or |NULL| if the singleton has either not 103 // been created yet or is already destroyed. This method is not thread-safe 104 // and must be called from the main UI thread. 105 static UserManager* Get(); 106 107 // Registers user manager preferences. 108 static void RegisterPrefs(PrefRegistrySimple* registry); 109 110 virtual ~UserManager(); 111 112 virtual UserImageManager* GetUserImageManager() = 0; 113 114 // Returns a list of users who have logged into this device previously. This 115 // is sorted by last login date with the most recent user at the beginning. 116 virtual const UserList& GetUsers() const = 0; 117 118 // Returns list of users admitted for logging in into multiprofile session. 119 virtual UserList GetUsersAdmittedForMultiProfile() const = 0; 120 121 // Returns a list of users who are currently logged in. 122 virtual const UserList& GetLoggedInUsers() const = 0; 123 124 // Returns a list of users who are currently logged in in the LRU order - 125 // so the active user is the first one in the list. If there is no user logged 126 // in, the current user will be returned. 127 virtual const UserList& GetLRULoggedInUsers() = 0; 128 129 // Indicates that a user with the given |email| has just logged in. The 130 // persistent list is updated accordingly if the user is not ephemeral. 131 // |browser_restart| is true when reloading Chrome after crash to distinguish 132 // from normal sign in flow. 133 // |username_hash| is used to identify homedir mount point. 134 virtual void UserLoggedIn(const std::string& email, 135 const std::string& username_hash, 136 bool browser_restart) = 0; 137 138 // Switches to active user identified by |email|. User has to be logged in. 139 virtual void SwitchActiveUser(const std::string& email) = 0; 140 141 // Called when browser session is started i.e. after 142 // browser_creator.LaunchBrowser(...) was called after user sign in. 143 // When user is at the image screen IsUserLoggedIn() will return true 144 // but SessionStarted() will return false. 145 // Fires NOTIFICATION_SESSION_STARTED. 146 virtual void SessionStarted() = 0; 147 148 // Usually is called when Chrome is restarted after a crash and there's an 149 // active session. First user (one that is passed with --login-user) Chrome 150 // session has been already restored at this point. This method asks session 151 // manager for all active user sessions, marks them as logged in 152 // and notifies observers. 153 virtual void RestoreActiveSessions() = 0; 154 155 // Creates locally managed user with given |display_name| and|local_user_id| 156 // and persists that to user list. Also links this user identified by 157 // |sync_user_id| to manager with a |manager_id|. 158 // Returns created user, or existing user if there already 159 // was locally managed user with such display name. 160 // TODO(antrim): Refactor into a single struct to have only 1 getter. 161 virtual const User* CreateLocallyManagedUserRecord( 162 const std::string& manager_id, 163 const std::string& local_user_id, 164 const std::string& sync_user_id, 165 const string16& display_name) = 0; 166 167 // Generates unique username for locally managed user. 168 virtual std::string GenerateUniqueLocallyManagedUserId() = 0; 169 170 // Removes the user from the device. Note, it will verify that the given user 171 // isn't the owner, so calling this method for the owner will take no effect. 172 // Note, |delegate| can be NULL. 173 virtual void RemoveUser(const std::string& email, 174 RemoveUserDelegate* delegate) = 0; 175 176 // Removes the user from the persistent list only. Also removes the user's 177 // picture. 178 virtual void RemoveUserFromList(const std::string& email) = 0; 179 180 // Returns true if a user with the given email address is found in the 181 // persistent list or currently logged in as ephemeral. 182 virtual bool IsKnownUser(const std::string& email) const = 0; 183 184 // Returns the user with the given email address if found in the persistent 185 // list or currently logged in as ephemeral. Returns |NULL| otherwise. 186 virtual const User* FindUser(const std::string& email) const = 0; 187 188 // Returns the locally managed user with the given |display_name| if found in 189 // the persistent list. Returns |NULL| otherwise. 190 virtual const User* FindLocallyManagedUser( 191 const string16& display_name) const = 0; 192 193 // Returns the logged-in user. 194 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser(). 195 // http://crbug.com/230852 196 virtual const User* GetLoggedInUser() const = 0; 197 virtual User* GetLoggedInUser() = 0; 198 199 // Returns the logged-in user that is currently active within this session. 200 // There could be multiple users logged in at the the same but for now 201 // we support only one of them being active. 202 virtual const User* GetActiveUser() const = 0; 203 virtual User* GetActiveUser() = 0; 204 205 // Saves user's oauth token status in local state preferences. 206 virtual void SaveUserOAuthStatus( 207 const std::string& username, 208 User::OAuthTokenStatus oauth_token_status) = 0; 209 210 // Saves user's displayed name in local state preferences. 211 // Ignored If there is no such user. 212 virtual void SaveUserDisplayName(const std::string& username, 213 const string16& display_name) = 0; 214 215 // Returns the display name for user |username| if it is known (was 216 // previously set by a |SaveUserDisplayName| call). 217 // Otherwise, returns an empty string. 218 virtual string16 GetUserDisplayName( 219 const std::string& username) const = 0; 220 221 // Saves user's displayed (non-canonical) email in local state preferences. 222 // Ignored If there is no such user. 223 virtual void SaveUserDisplayEmail(const std::string& username, 224 const std::string& display_email) = 0; 225 226 // Returns the display email for user |username| if it is known (was 227 // previously set by a |SaveUserDisplayEmail| call). 228 // Otherwise, returns |username| itself. 229 virtual std::string GetUserDisplayEmail( 230 const std::string& username) const = 0; 231 232 // Returns sync_user_id for locally managed user with |managed_user_id| or 233 // empty string if such user is not found or it doesn't have 234 // sync_user_id defined. 235 virtual std::string GetManagedUserSyncId( 236 const std::string& managed_user_id) const = 0; 237 238 // Returns the display name for manager of user |managed_user_id| if it is 239 // known (was previously set by a |SaveUserDisplayName| call). 240 // Otherwise, returns a manager id. 241 virtual string16 GetManagerDisplayNameForManagedUser( 242 const std::string& managed_user_id) const = 0; 243 244 // Returns the user id for manager of user |managed_user_id| if it is known 245 // (user is actually a managed user). 246 // Otherwise, returns an empty string. 247 virtual std::string GetManagerUserIdForManagedUser( 248 const std::string& managed_user_id) const = 0; 249 250 // Returns the display email for manager of user |managed_user_id| if it is 251 // known (user is actually a managed user). 252 // Otherwise, returns an empty string. 253 virtual std::string GetManagerDisplayEmailForManagedUser( 254 const std::string& managed_user_id) const = 0; 255 256 // Returns true if current user is an owner. 257 virtual bool IsCurrentUserOwner() const = 0; 258 259 // Returns true if current user is not existing one (hasn't signed in before). 260 virtual bool IsCurrentUserNew() const = 0; 261 262 // Returns true if data stored or cached for the current user outside that 263 // user's cryptohome (wallpaper, avatar, OAuth token status, display name, 264 // display email) is ephemeral. 265 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0; 266 267 // Returns true if the current user's session can be locked (i.e. the user has 268 // a password with which to unlock the session). 269 virtual bool CanCurrentUserLock() const = 0; 270 271 // Returns true if at least one user has signed in. 272 virtual bool IsUserLoggedIn() const = 0; 273 274 // Returns true if we're logged in as a regular user. 275 virtual bool IsLoggedInAsRegularUser() const = 0; 276 277 // Returns true if we're logged in as a demo user. 278 virtual bool IsLoggedInAsDemoUser() const = 0; 279 280 // Returns true if we're logged in as a public account. 281 virtual bool IsLoggedInAsPublicAccount() const = 0; 282 283 // Returns true if we're logged in as a Guest. 284 virtual bool IsLoggedInAsGuest() const = 0; 285 286 // Returns true if we're logged in as a locally managed user. 287 virtual bool IsLoggedInAsLocallyManagedUser() const = 0; 288 289 // Returns true if we're logged in as a kiosk app. 290 virtual bool IsLoggedInAsKioskApp() const = 0; 291 292 // Returns true if we're logged in as the stub user used for testing on Linux. 293 virtual bool IsLoggedInAsStub() const = 0; 294 295 // Returns true if we're logged in and browser has been started i.e. 296 // browser_creator.LaunchBrowser(...) was called after sign in 297 // or restart after crash. 298 virtual bool IsSessionStarted() const = 0; 299 300 // Returns true iff browser has been restarted after crash and UserManager 301 // finished restoring user sessions. 302 virtual bool UserSessionsRestored() const = 0; 303 304 // Returns merge session status. 305 virtual MergeSessionState GetMergeSessionState() const = 0; 306 307 // Changes merge session status. 308 virtual void SetMergeSessionState(MergeSessionState status) = 0; 309 310 // Returns true when the browser has crashed and restarted during the current 311 // user's session. 312 virtual bool HasBrowserRestarted() const = 0; 313 314 // Returns true if data stored or cached for the user with the given email 315 // address outside that user's cryptohome (wallpaper, avatar, OAuth token 316 // status, display name, display email) is to be treated as ephemeral. 317 virtual bool IsUserNonCryptohomeDataEphemeral( 318 const std::string& email) const = 0; 319 320 // Create a record about starting locally managed user creation transaction. 321 virtual void StartLocallyManagedUserCreationTransaction( 322 const string16& display_name) = 0; 323 324 // Add user id to locally managed user creation transaction record. 325 virtual void SetLocallyManagedUserCreationTransactionUserId( 326 const std::string& email) = 0; 327 328 // Remove locally managed user creation transaction record. 329 virtual void CommitLocallyManagedUserCreationTransaction() = 0; 330 331 // Method that allows to set |flow| for user identified by |email|. 332 // Flow should be set before login attempt. 333 // Takes ownership of the |flow|, |flow| will be deleted in case of login 334 // failure. 335 virtual void SetUserFlow(const std::string& email, UserFlow* flow) = 0; 336 337 // Return user flow for current user. Returns instance of DefaultUserFlow if 338 // no flow was defined for current user, or user is not logged in. 339 // Returned value should not be cached. 340 virtual UserFlow* GetCurrentUserFlow() const = 0; 341 342 // Return user flow for user identified by |email|. Returns instance of 343 // DefaultUserFlow if no flow was defined for user. 344 // Returned value should not be cached. 345 virtual UserFlow* GetUserFlow(const std::string& email) const = 0; 346 347 // Resets user flow fo user idenitified by |email|. 348 virtual void ResetUserFlow(const std::string& email) = 0; 349 350 // Gets/sets chrome oauth client id and secret for kiosk app mode. The default 351 // values can be overriden with kiosk auth file. 352 virtual bool GetAppModeChromeClientOAuthInfo( 353 std::string* chrome_client_id, 354 std::string* chrome_client_secret) = 0; 355 virtual void SetAppModeChromeClientOAuthInfo( 356 const std::string& chrome_client_id, 357 const std::string& chrome_client_secret) = 0; 358 359 virtual void AddObserver(Observer* obs) = 0; 360 virtual void RemoveObserver(Observer* obs) = 0; 361 362 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0; 363 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0; 364 365 virtual void NotifyLocalStateChanged() = 0; 366 367 // Returns true if locally managed users allowed. 368 virtual bool AreLocallyManagedUsersAllowed() const = 0; 369 370 private: 371 friend class ScopedUserManagerEnabler; 372 373 // Sets the singleton to the given |user_manager|, taking ownership. Returns 374 // the previous value of the singleton, passing ownership. 375 static UserManager* SetForTesting(UserManager* user_manager); 376 }; 377 378 // Helper class for unit tests. Initializes the UserManager singleton to the 379 // given |user_manager| and tears it down again on destruction. If the singleton 380 // had already been initialized, its previous value is restored after tearing 381 // down |user_manager|. 382 class ScopedUserManagerEnabler { 383 public: 384 // Takes ownership of |user_manager|. 385 explicit ScopedUserManagerEnabler(UserManager* user_manager); 386 ~ScopedUserManagerEnabler(); 387 388 private: 389 UserManager* previous_user_manager_; 390 391 DISALLOW_COPY_AND_ASSIGN(ScopedUserManagerEnabler); 392 }; 393 394 // Helper class for unit tests. Initializes the UserManager singleton on 395 // construction and tears it down again on destruction. 396 class ScopedTestUserManager { 397 public: 398 ScopedTestUserManager(); 399 ~ScopedTestUserManager(); 400 401 private: 402 DISALLOW_COPY_AND_ASSIGN(ScopedTestUserManager); 403 }; 404 405 } // namespace chromeos 406 407 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_ 408