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 COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_ 6 #define COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_ 7 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/observer_list.h" 15 #include "base/synchronization/lock.h" 16 #include "base/time/time.h" 17 #include "components/user_manager/user.h" 18 #include "components/user_manager/user_manager.h" 19 #include "components/user_manager/user_manager_export.h" 20 21 class PrefService; 22 class PrefRegistrySimple; 23 24 namespace base { 25 class ListValue; 26 class TaskRunner; 27 } 28 29 namespace user_manager { 30 31 class RemoveUserDelegate; 32 33 // Base implementation of the UserManager interface. 34 class USER_MANAGER_EXPORT UserManagerBase : public UserManager { 35 public: 36 // Creates UserManagerBase with |task_runner| for UI thread and 37 // |blocking_task_runner| for SequencedWorkerPool. 38 UserManagerBase(scoped_refptr<base::TaskRunner> task_runner, 39 scoped_refptr<base::TaskRunner> blocking_task_runner); 40 virtual ~UserManagerBase(); 41 42 // Registers UserManagerBase preferences. 43 static void RegisterPrefs(PrefRegistrySimple* registry); 44 45 // UserManager implementation: 46 virtual void Shutdown() OVERRIDE; 47 virtual const UserList& GetUsers() const OVERRIDE; 48 virtual const UserList& GetLoggedInUsers() const OVERRIDE; 49 virtual const UserList& GetLRULoggedInUsers() const OVERRIDE; 50 virtual const std::string& GetOwnerEmail() const OVERRIDE; 51 virtual void UserLoggedIn(const std::string& user_id, 52 const std::string& user_id_hash, 53 bool browser_restart) OVERRIDE; 54 virtual void SwitchActiveUser(const std::string& user_id) OVERRIDE; 55 virtual void SwitchToLastActiveUser() OVERRIDE; 56 virtual void SessionStarted() OVERRIDE; 57 virtual void RemoveUser(const std::string& user_id, 58 RemoveUserDelegate* delegate) OVERRIDE; 59 virtual void RemoveUserFromList(const std::string& user_id) OVERRIDE; 60 virtual bool IsKnownUser(const std::string& user_id) const OVERRIDE; 61 virtual const User* FindUser(const std::string& user_id) const OVERRIDE; 62 virtual User* FindUserAndModify(const std::string& user_id) OVERRIDE; 63 virtual const User* GetLoggedInUser() const OVERRIDE; 64 virtual User* GetLoggedInUser() OVERRIDE; 65 virtual const User* GetActiveUser() const OVERRIDE; 66 virtual User* GetActiveUser() OVERRIDE; 67 virtual const User* GetPrimaryUser() const OVERRIDE; 68 virtual void SaveUserOAuthStatus( 69 const std::string& user_id, 70 User::OAuthTokenStatus oauth_token_status) OVERRIDE; 71 virtual void SaveForceOnlineSignin(const std::string& user_id, 72 bool force_online_signin) OVERRIDE; 73 virtual void SaveUserDisplayName(const std::string& user_id, 74 const base::string16& display_name) OVERRIDE; 75 virtual base::string16 GetUserDisplayName( 76 const std::string& user_id) const OVERRIDE; 77 virtual void SaveUserDisplayEmail(const std::string& user_id, 78 const std::string& display_email) OVERRIDE; 79 virtual std::string GetUserDisplayEmail( 80 const std::string& user_id) const OVERRIDE; 81 virtual void UpdateUserAccountData( 82 const std::string& user_id, 83 const UserAccountData& account_data) OVERRIDE; 84 virtual bool IsCurrentUserOwner() const OVERRIDE; 85 virtual bool IsCurrentUserNew() const OVERRIDE; 86 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const OVERRIDE; 87 virtual bool CanCurrentUserLock() const OVERRIDE; 88 virtual bool IsUserLoggedIn() const OVERRIDE; 89 virtual bool IsLoggedInAsRegularUser() const OVERRIDE; 90 virtual bool IsLoggedInAsDemoUser() const OVERRIDE; 91 virtual bool IsLoggedInAsPublicAccount() const OVERRIDE; 92 virtual bool IsLoggedInAsGuest() const OVERRIDE; 93 virtual bool IsLoggedInAsSupervisedUser() const OVERRIDE; 94 virtual bool IsLoggedInAsKioskApp() const OVERRIDE; 95 virtual bool IsLoggedInAsStub() const OVERRIDE; 96 virtual bool IsSessionStarted() const OVERRIDE; 97 virtual bool IsUserNonCryptohomeDataEphemeral( 98 const std::string& user_id) const OVERRIDE; 99 virtual void AddObserver(UserManager::Observer* obs) OVERRIDE; 100 virtual void RemoveObserver(UserManager::Observer* obs) OVERRIDE; 101 virtual void AddSessionStateObserver( 102 UserManager::UserSessionStateObserver* obs) OVERRIDE; 103 virtual void RemoveSessionStateObserver( 104 UserManager::UserSessionStateObserver* obs) OVERRIDE; 105 virtual void NotifyLocalStateChanged() OVERRIDE; 106 virtual void ForceUpdateState() OVERRIDE; 107 108 // Helper function that copies users from |users_list| to |users_vector| and 109 // |users_set|. Duplicates and users already present in |existing_users| are 110 // skipped. 111 static void ParseUserList(const base::ListValue& users_list, 112 const std::set<std::string>& existing_users, 113 std::vector<std::string>* users_vector, 114 std::set<std::string>* users_set); 115 116 protected: 117 // Adds |user| to users list, and adds it to front of LRU list. It is assumed 118 // that there is no user with same id. 119 virtual void AddUserRecord(User* user); 120 121 // Returns true if trusted device policies have successfully been retrieved 122 // and ephemeral users are enabled. 123 virtual bool AreEphemeralUsersEnabled() const = 0; 124 125 // Returns true if user may be removed. 126 virtual bool CanUserBeRemoved(const User* user) const; 127 128 // A wrapper around C++ delete operator. Deletes |user|, and when |user| 129 // equals to active_user_, active_user_ is reset to NULL. 130 virtual void DeleteUser(User* user); 131 132 // Returns the locale used by the application. 133 virtual const std::string& GetApplicationLocale() const = 0; 134 135 // Returns "Local State" PrefService instance. 136 virtual PrefService* GetLocalState() const = 0; 137 138 // Loads |users_| from Local State if the list has not been loaded yet. 139 // Subsequent calls have no effect. Must be called on the UI thread. 140 void EnsureUsersLoaded(); 141 142 // Handle OAuth token |status| change for |user_id|. 143 virtual void HandleUserOAuthTokenStatusChange( 144 const std::string& user_id, 145 User::OAuthTokenStatus status) const = 0; 146 147 // Returns true if device is enterprise managed. 148 virtual bool IsEnterpriseManaged() const = 0; 149 150 // Helper function that copies users from |users_list| to |users_vector| and 151 // |users_set|. Duplicates and users already present in |existing_users| are 152 // skipped. 153 // Loads public accounts from the Local state and fills in 154 // |public_sessions_set|. 155 virtual void LoadPublicAccounts( 156 std::set<std::string>* public_sessions_set) = 0; 157 158 // Notifies that user has logged in. 159 virtual void NotifyOnLogin(); 160 161 // Notifies observers that another user was added to the session. 162 // If |user_switch_pending| is true this means that user has not been fully 163 // initialized yet like waiting for profile to be loaded. 164 virtual void NotifyUserAddedToSession(const User* added_user, 165 bool user_switch_pending); 166 167 // Performs any additional actions before user list is loaded. 168 virtual void PerformPreUserListLoadingActions() = 0; 169 170 // Performs any additional actions after user list is loaded. 171 virtual void PerformPostUserListLoadingActions() = 0; 172 173 // Performs any additional actions after UserLoggedIn() execution has been 174 // completed. 175 // |browser_restart| is true when reloading Chrome after crash to distinguish 176 // from normal sign in flow. 177 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0; 178 179 // Implementation for RemoveUser method. It is synchronous. It is called from 180 // RemoveUserInternal after owner check. 181 virtual void RemoveNonOwnerUserInternal(const std::string& user_email, 182 RemoveUserDelegate* delegate); 183 184 // Removes a regular or supervised user from the user list. 185 // Returns the user if found or NULL otherwise. 186 // Also removes the user from the persistent user list. 187 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id); 188 189 // Implementation for RemoveUser method. This is an asynchronous part of the 190 // method, that verifies that owner will not get deleted, and calls 191 // |RemoveNonOwnerUserInternal|. 192 virtual void RemoveUserInternal(const std::string& user_email, 193 RemoveUserDelegate* delegate); 194 195 // Removes data stored or cached outside the user's cryptohome (wallpaper, 196 // avatar, OAuth token status, display name, display email). 197 virtual void RemoveNonCryptohomeData(const std::string& user_id); 198 199 // Check for a particular user type. 200 201 // Returns true if |user_id| represents demo app. 202 virtual bool IsDemoApp(const std::string& user_id) const = 0; 203 204 // Returns true if |user_id| represents kiosk app. 205 virtual bool IsKioskApp(const std::string& user_id) const = 0; 206 207 // Returns true if |user_id| represents public account that has been marked 208 // for deletion. 209 virtual bool IsPublicAccountMarkedForRemoval( 210 const std::string& user_id) const = 0; 211 212 // These methods are called when corresponding user type has signed in. 213 214 // Indicates that the demo account has just logged in. 215 virtual void DemoAccountLoggedIn() = 0; 216 217 // Indicates that a user just logged in as guest. 218 virtual void GuestUserLoggedIn(); 219 220 // Indicates that a kiosk app robot just logged in. 221 virtual void KioskAppLoggedIn(const std::string& app_id) = 0; 222 223 // Indicates that a user just logged into a public session. 224 virtual void PublicAccountUserLoggedIn(User* user) = 0; 225 226 // Indicates that a regular user just logged in. 227 virtual void RegularUserLoggedIn(const std::string& user_id); 228 229 // Indicates that a regular user just logged in as ephemeral. 230 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id); 231 232 // Indicates that a user just logged into a retail mode session. 233 virtual void RetailModeUserLoggedIn() = 0; 234 235 // Indicates that a supervised user just logged in. 236 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0; 237 238 // Getters/setters for private members. 239 240 virtual void SetCurrentUserIsOwner(bool is_current_user_owner); 241 242 virtual bool GetEphemeralUsersEnabled() const; 243 virtual void SetEphemeralUsersEnabled(bool enabled); 244 245 virtual void SetIsCurrentUserNew(bool is_new); 246 247 virtual void SetOwnerEmail(std::string owner_user_id); 248 249 virtual const std::string& GetPendingUserSwitchID() const; 250 virtual void SetPendingUserSwitchID(std::string user_id); 251 252 // The logged-in user that is currently active in current session. 253 // NULL until a user has logged in, then points to one 254 // of the User instances in |users_|, the |guest_user_| instance or an 255 // ephemeral user instance. 256 User* active_user_; 257 258 // The primary user of the current session. It is recorded for the first 259 // signed-in user and does not change thereafter. 260 User* primary_user_; 261 262 // List of all known users. User instances are owned by |this|. Regular users 263 // are removed by |RemoveUserFromList|, public accounts by 264 // |UpdateAndCleanUpPublicAccounts|. 265 UserList users_; 266 267 private: 268 // Stages of loading user list from preferences. Some methods can have 269 // different behavior depending on stage. 270 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED }; 271 272 // Returns a list of users who have logged into this device previously. 273 // Same as GetUsers but used if you need to modify User from that list. 274 UserList& GetUsersAndModify(); 275 276 // Returns the user with the given email address if found in the persistent 277 // list. Returns |NULL| otherwise. 278 const User* FindUserInList(const std::string& user_id) const; 279 280 // Returns |true| if user with the given id is found in the persistent list. 281 // Returns |false| otherwise. Does not trigger user loading. 282 const bool UserExistsInList(const std::string& user_id) const; 283 284 // Same as FindUserInList but returns non-const pointer to User object. 285 User* FindUserInListAndModify(const std::string& user_id); 286 287 // Reads user's oauth token status from local state preferences. 288 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const; 289 290 // Read a flag indicating whether online authentication against GAIA should 291 // be enforced during the user's next sign-in from local state preferences. 292 bool LoadForceOnlineSignin(const std::string& user_id) const; 293 294 // Notifies observers that merge session state had changed. 295 void NotifyMergeSessionStateChanged(); 296 297 // Notifies observers that active user has changed. 298 void NotifyActiveUserChanged(const User* active_user); 299 300 // Notifies observers that active user_id hash has changed. 301 void NotifyActiveUserHashChanged(const std::string& hash); 302 303 // Update the global LoginState. 304 void UpdateLoginState(); 305 306 // Insert |user| at the front of the LRU user list. 307 void SetLRUUser(User* user); 308 309 // Sends metrics in response to a regular user logging in. 310 void SendRegularUserLoginMetrics(const std::string& user_id); 311 312 // Sets account locale for user with id |user_id|. 313 virtual void UpdateUserAccountLocale(const std::string& user_id, 314 const std::string& locale); 315 316 // Updates user account after locale was resolved. 317 void DoUpdateAccountLocale(const std::string& user_id, 318 scoped_ptr<std::string> resolved_locale); 319 320 // Indicates stage of loading user from prefs. 321 UserLoadStage user_loading_stage_; 322 323 // List of all users that are logged in current session. These point to User 324 // instances in |users_|. Only one of them could be marked as active. 325 UserList logged_in_users_; 326 327 // A list of all users that are logged in the current session. In contrast to 328 // |logged_in_users|, the order of this list is least recently used so that 329 // the active user should always be the first one in the list. 330 UserList lru_logged_in_users_; 331 332 // True if SessionStarted() has been called. 333 bool session_started_; 334 335 // Cached flag of whether currently logged-in user is owner or not. 336 // May be accessed on different threads, requires locking. 337 bool is_current_user_owner_; 338 mutable base::Lock is_current_user_owner_lock_; 339 340 // Cached flag of whether the currently logged-in user existed before this 341 // login. 342 bool is_current_user_new_; 343 344 // Cached flag of whether the currently logged-in user is a regular user who 345 // logged in as ephemeral. Storage of persistent information is avoided for 346 // such users by not adding them to the persistent user list, not downloading 347 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults 348 // to |false|. 349 bool is_current_user_ephemeral_regular_user_; 350 351 // Cached flag indicating whether the ephemeral user policy is enabled. 352 // Defaults to |false| if the value has not been read from trusted device 353 // policy yet. 354 bool ephemeral_users_enabled_; 355 356 // Cached name of device owner. Defaults to empty string if the value has not 357 // been read from trusted device policy yet. 358 std::string owner_email_; 359 360 ObserverList<UserManager::Observer> observer_list_; 361 362 // TODO(nkostylev): Merge with session state refactoring CL. 363 ObserverList<UserManager::UserSessionStateObserver> 364 session_state_observer_list_; 365 366 // Time at which this object was created. 367 base::TimeTicks manager_creation_time_; 368 369 // ID of the user just added to the session that needs to be activated 370 // as soon as user's profile is loaded. 371 std::string pending_user_switch_; 372 373 // ID of the user that was active in the previous session. 374 // Preference value is stored here before first user signs in 375 // because pref will be overidden once session restore starts. 376 std::string last_session_active_user_; 377 bool last_session_active_user_initialized_; 378 379 // TaskRunner for UI thread. 380 scoped_refptr<base::TaskRunner> task_runner_; 381 382 // TaskRunner for SequencedWorkerPool. 383 scoped_refptr<base::TaskRunner> blocking_task_runner_; 384 385 base::WeakPtrFactory<UserManagerBase> weak_factory_; 386 387 DISALLOW_COPY_AND_ASSIGN(UserManagerBase); 388 }; 389 390 } // namespace user_manager 391 392 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_ 393