user_manager_base.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #ifndef COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
  5. #define COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/feature_list.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/observer_list.h"
  16. #include "base/synchronization/lock.h"
  17. #include "base/time/time.h"
  18. #include "base/values.h"
  19. #include "components/account_id/account_id.h"
  20. #include "components/user_manager/remove_user_delegate.h"
  21. #include "components/user_manager/user.h"
  22. #include "components/user_manager/user_manager.h"
  23. #include "components/user_manager/user_manager_export.h"
  24. #include "components/user_manager/user_type.h"
  25. class PrefRegistrySimple;
  26. namespace base {
  27. class SingleThreadTaskRunner;
  28. }
  29. namespace user_manager {
  30. // Base implementation of the UserManager interface.
  31. class USER_MANAGER_EXPORT UserManagerBase : public UserManager {
  32. public:
  33. // These enum values represent a legacy supervised user's (LSU) status on the
  34. // sign in screen.
  35. // TODO(crbug/1155729): Remove once all LSUs deleted in the wild. LSUs were
  36. // first hidden on the login screen in M74. Assuming a five year AUE, we
  37. // should stop supporting devices with LSUs by 2024.
  38. // These values are logged to UMA. Entries should not be renumbered and
  39. // numeric values should never be reused. Please keep in sync with
  40. // "LegacySupervisedUserStatus" in src/tools/metrics/histograms/enums.xml.
  41. enum class LegacySupervisedUserStatus {
  42. // Non-LSU Gaia user displayed on login screen.
  43. kGaiaUserDisplayed = 0,
  44. // LSU hidden on login screen. Expect this count to decline to zero over
  45. // time as we delete LSUs.
  46. kLSUHidden = 1,
  47. // LSU attempted to delete cryptohome. Expect this count to decline to zero
  48. // over time as we delete LSUs.
  49. kLSUDeleted = 2,
  50. // Add future entires above this comment, in sync with
  51. // "LegacySupervisedUserStatus" in src/tools/metrics/histograms/enums.xml.
  52. // Update kMaxValue to the last value.
  53. kMaxValue = kLSUDeleted
  54. };
  55. // Creates UserManagerBase with |task_runner| for UI thread.
  56. explicit UserManagerBase(
  57. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  58. UserManagerBase(const UserManagerBase&) = delete;
  59. UserManagerBase& operator=(const UserManagerBase&) = delete;
  60. ~UserManagerBase() override;
  61. // Histogram for tracking the number of deprecated legacy supervised user
  62. // cryptohomes remaining in the wild.
  63. static const char kLegacySupervisedUsersHistogramName[];
  64. // Feature that removes legacy supervised users.
  65. static const base::Feature kRemoveLegacySupervisedUsersOnStartup;
  66. // Registers UserManagerBase preferences.
  67. static void RegisterPrefs(PrefRegistrySimple* registry);
  68. // UserManager implementation:
  69. void Shutdown() override;
  70. const UserList& GetUsers() const override;
  71. const UserList& GetLoggedInUsers() const override;
  72. const UserList& GetLRULoggedInUsers() const override;
  73. const AccountId& GetOwnerAccountId() const override;
  74. const AccountId& GetLastSessionActiveAccountId() const override;
  75. void UserLoggedIn(const AccountId& account_id,
  76. const std::string& user_id_hash,
  77. bool browser_restart,
  78. bool is_child) override;
  79. void SwitchActiveUser(const AccountId& account_id) override;
  80. void SwitchToLastActiveUser() override;
  81. void OnSessionStarted() override;
  82. void RemoveUser(const AccountId& account_id,
  83. UserRemovalReason reason,
  84. RemoveUserDelegate* delegate) override;
  85. void RemoveUserFromList(const AccountId& account_id) override;
  86. bool IsKnownUser(const AccountId& account_id) const override;
  87. const User* FindUser(const AccountId& account_id) const override;
  88. User* FindUserAndModify(const AccountId& account_id) override;
  89. const User* GetActiveUser() const override;
  90. User* GetActiveUser() override;
  91. const User* GetPrimaryUser() const override;
  92. void SaveUserOAuthStatus(const AccountId& account_id,
  93. User::OAuthTokenStatus oauth_token_status) override;
  94. void SaveForceOnlineSignin(const AccountId& account_id,
  95. bool force_online_signin) override;
  96. void SaveUserDisplayName(const AccountId& account_id,
  97. const std::u16string& display_name) override;
  98. std::u16string GetUserDisplayName(const AccountId& account_id) const override;
  99. void SaveUserDisplayEmail(const AccountId& account_id,
  100. const std::string& display_email) override;
  101. UserType GetUserType(const AccountId& account_id) override;
  102. void SaveUserType(const User* user) override;
  103. void UpdateUserAccountData(const AccountId& account_id,
  104. const UserAccountData& account_data) override;
  105. bool IsCurrentUserOwner() const override;
  106. bool IsCurrentUserNew() const override;
  107. bool IsCurrentUserNonCryptohomeDataEphemeral() const override;
  108. bool IsCurrentUserCryptohomeDataEphemeral() const override;
  109. bool CanCurrentUserLock() const override;
  110. bool IsUserLoggedIn() const override;
  111. bool IsLoggedInAsUserWithGaiaAccount() const override;
  112. bool IsLoggedInAsChildUser() const override;
  113. bool IsLoggedInAsPublicAccount() const override;
  114. bool IsLoggedInAsGuest() const override;
  115. bool IsLoggedInAsKioskApp() const override;
  116. bool IsLoggedInAsArcKioskApp() const override;
  117. bool IsLoggedInAsWebKioskApp() const override;
  118. bool IsLoggedInAsAnyKioskApp() const override;
  119. bool IsLoggedInAsStub() const override;
  120. bool IsUserNonCryptohomeDataEphemeral(
  121. const AccountId& account_id) const override;
  122. bool IsUserCryptohomeDataEphemeral(
  123. const AccountId& account_id) const override;
  124. void AddObserver(UserManager::Observer* obs) override;
  125. void RemoveObserver(UserManager::Observer* obs) override;
  126. void AddSessionStateObserver(
  127. UserManager::UserSessionStateObserver* obs) override;
  128. void RemoveSessionStateObserver(
  129. UserManager::UserSessionStateObserver* obs) override;
  130. void NotifyLocalStateChanged() override;
  131. void NotifyUserImageChanged(const User& user) override;
  132. void NotifyUserImageIsEnterpriseManagedChanged(
  133. const User& user,
  134. bool is_enterprise_managed) override;
  135. void NotifyUserProfileImageUpdateFailed(const User& user) override;
  136. void NotifyUserProfileImageUpdated(
  137. const User& user,
  138. const gfx::ImageSkia& profile_image) override;
  139. void NotifyUsersSignInConstraintsChanged() override;
  140. void NotifyUserToBeRemoved(const AccountId& account_id) override;
  141. void NotifyUserRemoved(const AccountId& account_id,
  142. UserRemovalReason reason) override;
  143. void Initialize() override;
  144. // This method updates "User was added to the device in this session nad is
  145. // not full initialized yet" flag.
  146. virtual void SetIsCurrentUserNew(bool is_new);
  147. // Helper function that converts users from |users_list| to |users_vector| and
  148. // |users_set|. Duplicates and users already present in |existing_users| are
  149. // skipped.
  150. void ParseUserList(const base::Value::List& users_list,
  151. const std::set<AccountId>& existing_users,
  152. std::vector<AccountId>* users_vector,
  153. std::set<AccountId>* users_set);
  154. void AddUserRecordForTesting(User* user) {
  155. return AddUserRecord(user);
  156. }
  157. // Returns true if device is enterprise managed.
  158. virtual bool IsEnterpriseManaged() const = 0;
  159. protected:
  160. // Adds |user| to users list, and adds it to front of LRU list. It is assumed
  161. // that there is no user with same id.
  162. virtual void AddUserRecord(User* user);
  163. // Returns true if user may be removed.
  164. virtual bool CanUserBeRemoved(const User* user) const;
  165. // A wrapper around C++ delete operator. Deletes |user|, and when |user|
  166. // equals to active_user_, active_user_ is reset to NULL.
  167. virtual void DeleteUser(User* user);
  168. // Returns the locale used by the application.
  169. virtual const std::string& GetApplicationLocale() const = 0;
  170. // Loads |users_| from Local State if the list has not been loaded yet.
  171. // Subsequent calls have no effect. Must be called on the UI thread.
  172. virtual void EnsureUsersLoaded();
  173. // Loads device local accounts from the Local state and fills in
  174. // |device_local_accounts_set|.
  175. virtual void LoadDeviceLocalAccounts(
  176. std::set<AccountId>* device_local_accounts_set) = 0;
  177. // Notifies observers that active user has changed.
  178. void NotifyActiveUserChanged(User* active_user);
  179. // Notifies that user has logged in.
  180. virtual void NotifyOnLogin();
  181. // Notifies observers that another user was added to the session.
  182. // If |user_switch_pending| is true this means that user has not been fully
  183. // initialized yet like waiting for profile to be loaded.
  184. virtual void NotifyUserAddedToSession(const User* added_user,
  185. bool user_switch_pending);
  186. // Performs any additional actions after user list is loaded.
  187. virtual void PerformPostUserListLoadingActions() = 0;
  188. // Performs any additional actions after UserLoggedIn() execution has been
  189. // completed.
  190. // |browser_restart| is true when reloading Chrome after crash to distinguish
  191. // from normal sign in flow.
  192. virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
  193. // Implementation for RemoveUser method. It is synchronous. It is called from
  194. // RemoveUserInternal after owner check.
  195. // Pass |account_id| by value here to avoid use-after-free. Original
  196. // |account_id| could be destroyed during the user removal.
  197. virtual void RemoveNonOwnerUserInternal(AccountId account_id,
  198. UserRemovalReason reason,
  199. RemoveUserDelegate* delegate);
  200. // Removes a regular or supervised user from the user list.
  201. // Returns the user if found or NULL otherwise.
  202. // Also removes the user from the persistent user list.
  203. // |notify| is true when OnUserRemoved() should be triggered,
  204. // meaning that the user won't be added after the removal.
  205. User* RemoveRegularOrSupervisedUserFromList(const AccountId& account_id,
  206. bool notify);
  207. // Implementation for RemoveUser method. This is an asynchronous part of the
  208. // method, that verifies that owner will not get deleted, and calls
  209. // |RemoveNonOwnerUserInternal|.
  210. virtual void RemoveUserInternal(const AccountId& account_id,
  211. UserRemovalReason reason,
  212. RemoveUserDelegate* delegate);
  213. // Removes data stored or cached outside the user's cryptohome (wallpaper,
  214. // avatar, OAuth token status, display name, display email).
  215. virtual void RemoveNonCryptohomeData(const AccountId& account_id);
  216. // Check for a particular user type.
  217. // These methods are called when corresponding user type has signed in.
  218. // Indicates that a user just logged in as guest.
  219. virtual void GuestUserLoggedIn();
  220. // Indicates that a kiosk app robot just logged in.
  221. virtual void KioskAppLoggedIn(User* user) = 0;
  222. // Indicates that a user just logged into a public session.
  223. virtual void PublicAccountUserLoggedIn(User* user) = 0;
  224. // Indicates that a regular user just logged in.
  225. virtual void RegularUserLoggedIn(const AccountId& account_id,
  226. const UserType user_type);
  227. // Indicates that a regular user just logged in as ephemeral.
  228. virtual void RegularUserLoggedInAsEphemeral(const AccountId& account_id,
  229. const UserType user_type);
  230. // Should be called when regular user was removed.
  231. virtual void OnUserRemoved(const AccountId& account_id) = 0;
  232. // Update the global LoginState.
  233. virtual void UpdateLoginState(const User* active_user,
  234. const User* primary_user,
  235. bool is_current_user_owner) const = 0;
  236. // Getters/setters for private members.
  237. virtual bool GetEphemeralUsersEnabled() const;
  238. virtual void SetEphemeralUsersEnabled(bool enabled);
  239. virtual void SetOwnerId(const AccountId& owner_account_id);
  240. virtual const AccountId& GetPendingUserSwitchID() const;
  241. virtual void SetPendingUserSwitchId(const AccountId& account_id);
  242. // The logged-in user that is currently active in current session.
  243. // NULL until a user has logged in, then points to one
  244. // of the User instances in |users_|, the |guest_user_| instance or an
  245. // ephemeral user instance.
  246. raw_ptr<User> active_user_ = nullptr;
  247. // The primary user of the current session. It is recorded for the first
  248. // signed-in user and does not change thereafter.
  249. raw_ptr<User> primary_user_ = nullptr;
  250. // List of all known users. User instances are owned by |this|. Regular users
  251. // are removed by |RemoveUserFromList|, device local accounts by
  252. // |UpdateAndCleanUpDeviceLocalAccounts|.
  253. UserList users_;
  254. // List of all users that are logged in current session. These point to User
  255. // instances in |users_|. Only one of them could be marked as active.
  256. UserList logged_in_users_;
  257. // A list of all users that are logged in the current session. In contrast to
  258. // |logged_in_users|, the order of this list is least recently used so that
  259. // the active user should always be the first one in the list.
  260. UserList lru_logged_in_users_;
  261. private:
  262. // Stages of loading user list from preferences. Some methods can have
  263. // different behavior depending on stage.
  264. enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
  265. // Returns a list of users who have logged into this device previously.
  266. // Same as GetUsers but used if you need to modify User from that list.
  267. UserList& GetUsersAndModify();
  268. // Returns the user with the given email address if found in the persistent
  269. // list. Returns |NULL| otherwise.
  270. const User* FindUserInList(const AccountId& account_id) const;
  271. // Returns |true| if user with the given id is found in the persistent list.
  272. // Returns |false| otherwise. Does not trigger user loading.
  273. bool UserExistsInList(const AccountId& account_id) const;
  274. // Same as FindUserInList but returns non-const pointer to User object.
  275. User* FindUserInListAndModify(const AccountId& account_id);
  276. // Reads user's oauth token status from local state preferences.
  277. User::OAuthTokenStatus LoadUserOAuthStatus(const AccountId& account_id) const;
  278. // Read a flag indicating whether online authentication against GAIA should
  279. // be enforced during the user's next sign-in from local state preferences.
  280. bool LoadForceOnlineSignin(const AccountId& account_id) const;
  281. // Read a flag indicating whether session initialization has completed at
  282. // least once.
  283. bool LoadSessionInitialized(const AccountId& account_id) const;
  284. // Notifies observers that merge session state had changed.
  285. void NotifyMergeSessionStateChanged();
  286. // Notifies observers that active account_id hash has changed.
  287. void NotifyActiveUserHashChanged(const std::string& hash);
  288. // Call UpdateLoginState.
  289. void CallUpdateLoginState();
  290. // Insert |user| at the front of the LRU user list.
  291. void SetLRUUser(User* user);
  292. // Sends metrics in response to a user with gaia account (regular) logging in.
  293. void SendGaiaUserLoginMetrics(const AccountId& account_id);
  294. // Sets account locale for user with id |account_id|.
  295. virtual void UpdateUserAccountLocale(const AccountId& account_id,
  296. const std::string& locale);
  297. // Updates user account after locale was resolved.
  298. void DoUpdateAccountLocale(const AccountId& account_id,
  299. std::unique_ptr<std::string> resolved_locale);
  300. void RemoveLegacySupervisedUser(const AccountId& account_id);
  301. // Indicates stage of loading user from prefs.
  302. UserLoadStage user_loading_stage_ = STAGE_NOT_LOADED;
  303. // Cached flag of whether the currently logged-in user existed before this
  304. // login.
  305. bool is_current_user_new_ = false;
  306. // Cached flag of whether the currently logged-in user is a regular user who
  307. // logged in as ephemeral. Storage of persistent information is avoided for
  308. // such users by not adding them to the persistent user list, not downloading
  309. // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
  310. // to |false|.
  311. bool is_current_user_ephemeral_regular_user_ = false;
  312. // Cached flag indicating whether the ephemeral user policy is enabled.
  313. // Defaults to |false| if the value has not been read from trusted device
  314. // policy yet.
  315. bool ephemeral_users_enabled_ = false;
  316. // Cached name of device owner. Defaults to empty if the value has not
  317. // been read from trusted device policy yet.
  318. AccountId owner_account_id_ = EmptyAccountId();
  319. base::ObserverList<UserManager::Observer>::Unchecked observer_list_;
  320. // TODO(nkostylev): Merge with session state refactoring CL.
  321. base::ObserverList<UserManager::UserSessionStateObserver>::Unchecked
  322. session_state_observer_list_;
  323. // Time at which this object was created.
  324. base::TimeTicks manager_creation_time_ = base::TimeTicks::Now();
  325. // ID of the user just added to the session that needs to be activated
  326. // as soon as user's profile is loaded.
  327. AccountId pending_user_switch_ = EmptyAccountId();
  328. // ID of the user that was active in the previous session.
  329. // Preference value is stored here before first user signs in
  330. // because pref will be overidden once session restore starts.
  331. AccountId last_session_active_account_id_ = EmptyAccountId();
  332. bool last_session_active_account_id_initialized_ = false;
  333. // TaskRunner for UI thread.
  334. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  335. base::WeakPtrFactory<UserManagerBase> weak_factory_{this};
  336. };
  337. } // namespace user_manager
  338. #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_