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