user_manager.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_H_
  5. #define COMPONENTS_USER_MANAGER_USER_MANAGER_H_
  6. #include <string>
  7. #include "base/callback_forward.h"
  8. #include "components/user_manager/user.h"
  9. #include "components/user_manager/user_manager_export.h"
  10. #include "components/user_manager/user_type.h"
  11. class AccountId;
  12. class PrefService;
  13. namespace user_manager {
  14. class ScopedUserManager;
  15. class RemoveUserDelegate;
  16. // A list pref of the the regular users known on this device, arranged in LRU
  17. // order, stored in local state.
  18. USER_MANAGER_EXPORT extern const char kRegularUsersPref[];
  19. enum class UserRemovalReason : int32_t {
  20. UNKNOWN = 0,
  21. LOCAL_USER_INITIATED = 1,
  22. REMOTE_ADMIN_INITIATED = 2,
  23. LOCAL_USER_INITIATED_ON_REQUIRED_UPDATE = 3,
  24. DEVICE_EPHEMERAL_USERS_ENABLED = 4,
  25. GAIA_REMOVED = 5
  26. };
  27. // Interface for UserManagerBase - that provides base implementation for
  28. // Chrome OS user management. Typical features:
  29. // * Get list of all know users (who have logged into this Chrome OS device)
  30. // * Keep track for logged in/LRU users, active user in multi-user session.
  31. // * Find/modify users, store user meta-data such as display name/email.
  32. class USER_MANAGER_EXPORT UserManager {
  33. public:
  34. // Interface that observers of UserManager must implement in order
  35. // to receive notification when local state preferences is changed
  36. class Observer {
  37. public:
  38. // Called when the local state preferences is changed.
  39. virtual void LocalStateChanged(UserManager* user_manager);
  40. // Called when the image of the given user is changed.
  41. virtual void OnUserImageChanged(const User& user);
  42. // Called when the user image enterprise state of the given user is changed.
  43. virtual void OnUserImageIsEnterpriseManagedChanged(
  44. const User& user,
  45. bool is_enterprise_managed);
  46. // Called when the profile image download for the given user fails or
  47. // user has the default profile image or no porfile image at all.
  48. virtual void OnUserProfileImageUpdateFailed(const User& user);
  49. // Called when the profile image for the given user is downloaded.
  50. // |profile_image| contains the downloaded profile image.
  51. virtual void OnUserProfileImageUpdated(const User& user,
  52. const gfx::ImageSkia& profile_image);
  53. // Called when any of the device cros settings which are responsible for
  54. // user sign in are changed.
  55. virtual void OnUsersSignInConstraintsChanged();
  56. // Called just before a user of the device will be removed.
  57. virtual void OnUserToBeRemoved(const AccountId& account_id);
  58. // Called just after a user of the device has been removed.
  59. virtual void OnUserRemoved(const AccountId& account_id,
  60. UserRemovalReason reason);
  61. protected:
  62. virtual ~Observer();
  63. };
  64. // TODO(xiyuan): Refactor and move this observer out of UserManager.
  65. // Observer interface that defines methods used to notify on user session /
  66. // active user state changes. Default implementation is empty.
  67. class UserSessionStateObserver {
  68. public:
  69. // Called when active user has changed.
  70. virtual void ActiveUserChanged(User* active_user);
  71. // Called when another user got added to the existing session.
  72. virtual void UserAddedToSession(const User* added_user);
  73. // Called right before notifying on user change so that those who rely
  74. // on account_id hash would be accessing up-to-date value.
  75. virtual void ActiveUserHashChanged(const std::string& hash);
  76. protected:
  77. virtual ~UserSessionStateObserver();
  78. };
  79. // Data retrieved from user account.
  80. class UserAccountData {
  81. public:
  82. UserAccountData(const std::u16string& display_name,
  83. const std::u16string& given_name,
  84. const std::string& locale);
  85. UserAccountData(const UserAccountData&) = delete;
  86. UserAccountData& operator=(const UserAccountData&) = delete;
  87. ~UserAccountData();
  88. const std::u16string& display_name() const { return display_name_; }
  89. const std::u16string& given_name() const { return given_name_; }
  90. const std::string& locale() const { return locale_; }
  91. private:
  92. const std::u16string display_name_;
  93. const std::u16string given_name_;
  94. const std::string locale_;
  95. };
  96. // Initializes UserManager instance to this. Normally should be called right
  97. // after creation so that user_manager::UserManager::Get() doesn't fail.
  98. // Tests could call this method if they are replacing existing UserManager
  99. // instance with their own test instance.
  100. virtual void Initialize();
  101. // Checks whether the UserManager instance has been created already.
  102. // This method is not thread-safe and must be called from the main UI thread.
  103. static bool IsInitialized();
  104. // Shuts down the UserManager. After this method has been called, the
  105. // singleton has unregistered itself as an observer but remains available so
  106. // that other classes can access it during their shutdown. This method is not
  107. // thread-safe and must be called from the main UI thread.
  108. virtual void Shutdown() = 0;
  109. // Sets UserManager instance to NULL. Always call Shutdown() first.
  110. // This method is not thread-safe and must be called from the main UI thread.
  111. void Destroy();
  112. // Returns UserManager instance or will crash if it is |NULL| (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. virtual ~UserManager();
  117. // Returns a list of users who have logged into this device previously. This
  118. // is sorted by last login date with the most recent user at the beginning.
  119. virtual const UserList& GetUsers() const = 0;
  120. // Returns list of users allowed for logging in into multi-profile session.
  121. // Users that have a policy that prevents them from being added to the
  122. // multi-profile session will still be part of this list as long as they
  123. // are regular users (i.e. not a public session/supervised etc.).
  124. // Returns an empty list in case when primary user is not a regular one or
  125. // has a policy that prohibits it to be part of multi-profile session.
  126. virtual UserList GetUsersAllowedForMultiProfile() const = 0;
  127. // Returns a list of users who are currently logged in.
  128. virtual const UserList& GetLoggedInUsers() const = 0;
  129. // Returns a list of users who are currently logged in in the LRU order -
  130. // so the active user is the first one in the list. If there is no user logged
  131. // in, the current user will be returned.
  132. virtual const UserList& GetLRULoggedInUsers() const = 0;
  133. // Returns a list of users who can unlock the device.
  134. // This list is based on policy and whether user is able to do unlock.
  135. // Policy:
  136. // * If user has primary-only policy then it is the only user in unlock users.
  137. // * Otherwise all users with unrestricted policy are added to this list.
  138. // All users that are unable to perform unlock are excluded from this list.
  139. virtual UserList GetUnlockUsers() const = 0;
  140. // Returns account Id of the owner user. Returns an empty Id if there is
  141. // no owner for the device.
  142. virtual const AccountId& GetOwnerAccountId() const = 0;
  143. // Returns account Id of the user that was active in the previous session.
  144. virtual const AccountId& GetLastSessionActiveAccountId() const = 0;
  145. // Indicates that a user with the given |account_id| has just logged in. The
  146. // persistent list is updated accordingly if the user is not ephemeral.
  147. // |browser_restart| is true when reloading Chrome after crash to distinguish
  148. // from normal sign in flow.
  149. // |username_hash| is used to identify homedir mount point.
  150. virtual void UserLoggedIn(const AccountId& account_id,
  151. const std::string& username_hash,
  152. bool browser_restart,
  153. bool is_child) = 0;
  154. // Switches to active user identified by |account_id|. User has to be logged
  155. // in.
  156. virtual void SwitchActiveUser(const AccountId& account_id) = 0;
  157. // Switches to the last active user (called after crash happens and session
  158. // restore has completed).
  159. virtual void SwitchToLastActiveUser() = 0;
  160. // Invoked by session manager to inform session start.
  161. virtual void OnSessionStarted() = 0;
  162. // Removes the user from the device while providing a reason for enterprise
  163. // reporting. Note, it will verify that the given user isn't the owner, so
  164. // calling this method for the owner will take no effect. Note, |delegate|
  165. // can be NULL.
  166. virtual void RemoveUser(const AccountId& account_id,
  167. UserRemovalReason reason,
  168. RemoveUserDelegate* delegate) = 0;
  169. // Removes the user from the persistent list only. Also removes the user's
  170. // picture.
  171. virtual void RemoveUserFromList(const AccountId& account_id) = 0;
  172. // Returns true if a user with the given account id is found in the persistent
  173. // list or currently logged in as ephemeral.
  174. virtual bool IsKnownUser(const AccountId& account_id) const = 0;
  175. // Returns the user with the given account id if found in the persistent
  176. // list or currently logged in as ephemeral. Returns |NULL| otherwise.
  177. virtual const User* FindUser(const AccountId& account_id) const = 0;
  178. // Returns the user with the given account id if found in the persistent
  179. // list or currently logged in as ephemeral. Returns |NULL| otherwise.
  180. // Same as FindUser but returns non-const pointer to User object.
  181. virtual User* FindUserAndModify(const AccountId& account_id) = 0;
  182. // Returns the logged-in user that is currently active within this session.
  183. // There could be multiple users logged in at the the same but for now
  184. // we support only one of them being active.
  185. virtual const User* GetActiveUser() const = 0;
  186. virtual User* GetActiveUser() = 0;
  187. // Returns the primary user of the current session. It is recorded for the
  188. // first signed-in user and does not change thereafter.
  189. virtual const User* GetPrimaryUser() const = 0;
  190. // Saves user's oauth token status in local state preferences.
  191. virtual void SaveUserOAuthStatus(
  192. const AccountId& account_id,
  193. User::OAuthTokenStatus oauth_token_status) = 0;
  194. // Saves a flag indicating whether online authentication against GAIA should
  195. // be enforced during the user's next sign-in.
  196. virtual void SaveForceOnlineSignin(const AccountId& account_id,
  197. bool force_online_signin) = 0;
  198. // Saves user's displayed name in local state preferences.
  199. // Ignored If there is no such user.
  200. virtual void SaveUserDisplayName(const AccountId& account_id,
  201. const std::u16string& display_name) = 0;
  202. // Updates data upon User Account download.
  203. virtual void UpdateUserAccountData(const AccountId& account_id,
  204. const UserAccountData& account_data) = 0;
  205. // Returns the display name for user |account_id| if it is known (was
  206. // previously set by a |SaveUserDisplayName| call).
  207. // Otherwise, returns an empty string.
  208. virtual std::u16string GetUserDisplayName(
  209. const AccountId& account_id) const = 0;
  210. // Saves user's displayed (non-canonical) email in local state preferences.
  211. // Ignored If there is no such user.
  212. virtual void SaveUserDisplayEmail(const AccountId& account_id,
  213. const std::string& display_email) = 0;
  214. // Returns stored user type or USER_TYPE_REGULAR by default.
  215. virtual UserType GetUserType(const AccountId& account_id) = 0;
  216. // Saves user's type for |user| into local state preferences.
  217. virtual void SaveUserType(const User* user) = 0;
  218. // Returns true if current user is an owner.
  219. virtual bool IsCurrentUserOwner() const = 0;
  220. // Returns true if current user is not existing one (hasn't signed in before).
  221. virtual bool IsCurrentUserNew() const = 0;
  222. // Returns true if data stored or cached for the current user outside that
  223. // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
  224. // display email) is ephemeral.
  225. virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
  226. // Returns true if data stored or cached for the current user inside that
  227. // user's cryptohome is ephemeral.
  228. virtual bool IsCurrentUserCryptohomeDataEphemeral() const = 0;
  229. // Returns true if the current user's session can be locked (i.e. the user has
  230. // a password with which to unlock the session).
  231. virtual bool CanCurrentUserLock() const = 0;
  232. // Returns true if at least one user has signed in.
  233. virtual bool IsUserLoggedIn() const = 0;
  234. // Returns true if we're logged in as a user with gaia account.
  235. virtual bool IsLoggedInAsUserWithGaiaAccount() const = 0;
  236. // Returns true if we're logged in as a child user.
  237. virtual bool IsLoggedInAsChildUser() const = 0;
  238. // Returns true if we're logged in as a public account.
  239. virtual bool IsLoggedInAsPublicAccount() const = 0;
  240. // Returns true if we're logged in as a Guest.
  241. virtual bool IsLoggedInAsGuest() const = 0;
  242. // Returns true if we're logged in as a kiosk app.
  243. virtual bool IsLoggedInAsKioskApp() const = 0;
  244. // Returns true if we're logged in as an ARC kiosk app.
  245. virtual bool IsLoggedInAsArcKioskApp() const = 0;
  246. // Returns true if we're logged in as a Web kiosk app.
  247. virtual bool IsLoggedInAsWebKioskApp() const = 0;
  248. // Returns true if we're logged in as chrome, ARC or Web kiosk app.
  249. virtual bool IsLoggedInAsAnyKioskApp() const = 0;
  250. // Returns true if we're logged in as the stub user used for testing on Linux.
  251. virtual bool IsLoggedInAsStub() const = 0;
  252. // Returns true if data stored or cached for the user with the given
  253. // |account_id|
  254. // address outside that user's cryptohome (wallpaper, avatar, OAuth token
  255. // status, display name, display email) is to be treated as ephemeral.
  256. virtual bool IsUserNonCryptohomeDataEphemeral(
  257. const AccountId& account_id) const = 0;
  258. virtual bool IsUserCryptohomeDataEphemeral(
  259. const AccountId& account_id) const = 0;
  260. virtual void AddObserver(Observer* obs) = 0;
  261. virtual void RemoveObserver(Observer* obs) = 0;
  262. virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
  263. virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
  264. virtual void NotifyLocalStateChanged() = 0;
  265. virtual void NotifyUserImageChanged(const User& user) = 0;
  266. virtual void NotifyUserImageIsEnterpriseManagedChanged(
  267. const User& user,
  268. bool is_enterprise_managed) = 0;
  269. virtual void NotifyUserProfileImageUpdateFailed(const User& user) = 0;
  270. virtual void NotifyUserProfileImageUpdated(
  271. const User& user,
  272. const gfx::ImageSkia& profile_image) = 0;
  273. virtual void NotifyUsersSignInConstraintsChanged() = 0;
  274. virtual void NotifyUserToBeRemoved(const AccountId& account_id) = 0;
  275. virtual void NotifyUserRemoved(const AccountId& account_id,
  276. UserRemovalReason reason) = 0;
  277. // Returns true if guest user is allowed.
  278. virtual bool IsGuestSessionAllowed() const = 0;
  279. // Returns true if the |user|, which has a GAIA account is allowed according
  280. // to device settings and policies.
  281. // Accept only users who has gaia account.
  282. virtual bool IsGaiaUserAllowed(const User& user) const = 0;
  283. // Returns true if |user| is allowed depending on device policies.
  284. // Accepted user types: USER_TYPE_REGULAR, USER_TYPE_GUEST, USER_TYPE_CHILD.
  285. virtual bool IsUserAllowed(const User& user) const = 0;
  286. // Returns true if trusted device policies have successfully been retrieved
  287. // and ephemeral users are enabled.
  288. virtual bool AreEphemeralUsersEnabled() const = 0;
  289. // Returns "Local State" PrefService instance.
  290. virtual PrefService* GetLocalState() const = 0;
  291. // Checks for platform-specific known users matching given |user_email| and
  292. // |gaia_id|. If data matches a known account, fills |out_account_id| with
  293. // account id and returns true.
  294. virtual bool GetPlatformKnownUserId(const std::string& user_email,
  295. const std::string& gaia_id,
  296. AccountId* out_account_id) const = 0;
  297. // Returns account id of the Guest user.
  298. virtual const AccountId& GetGuestAccountId() const = 0;
  299. // Returns true if this is first exec after boot.
  300. virtual bool IsFirstExecAfterBoot() const = 0;
  301. // Actually removes cryptohome.
  302. virtual void AsyncRemoveCryptohome(const AccountId& account_id) const = 0;
  303. // Returns true if |account_id| is Guest user.
  304. virtual bool IsGuestAccountId(const AccountId& account_id) const = 0;
  305. // Returns true if |account_id| is Stub user.
  306. virtual bool IsStubAccountId(const AccountId& account_id) const = 0;
  307. // Returns true if |account_id| is deprecated supervised.
  308. // TODO(crbug/1155729): Check it is not used anymore and remove it.
  309. virtual bool IsDeprecatedSupervisedAccountId(
  310. const AccountId& account_id) const = 0;
  311. virtual bool IsDeviceLocalAccountMarkedForRemoval(
  312. const AccountId& account_id) const = 0;
  313. // Returns true when the browser has crashed and restarted during the current
  314. // user's session.
  315. virtual bool HasBrowserRestarted() const = 0;
  316. // Returns image from resources bundle.
  317. virtual const gfx::ImageSkia& GetResourceImagekiaNamed(int id) const = 0;
  318. // Returns string from resources bundle.
  319. virtual std::u16string GetResourceStringUTF16(int string_id) const = 0;
  320. // Schedules CheckAndResolveLocale using given task runner and
  321. // |on_resolved_callback| as reply callback.
  322. virtual void ScheduleResolveLocale(
  323. const std::string& locale,
  324. base::OnceClosure on_resolved_callback,
  325. std::string* out_resolved_locale) const = 0;
  326. // Returns true if |image_index| is a valid default user image index.
  327. virtual bool IsValidDefaultUserImageId(int image_index) const = 0;
  328. UserType CalculateUserType(const AccountId& account_id,
  329. const User* user,
  330. bool browser_restart,
  331. bool is_child) const;
  332. protected:
  333. // Sets UserManager instance.
  334. static void SetInstance(UserManager* user_manager);
  335. // Pointer to the existing UserManager instance (if any).
  336. // Usually is set by calling Initialize(), reset by calling Destroy().
  337. // Not owned since specific implementation of UserManager should decide on its
  338. // own appropriate owner. For src/chrome implementation such place is
  339. // g_browser_process->platform_part().
  340. static UserManager* instance;
  341. private:
  342. friend class ScopedUserManager;
  343. // Same as Get() but doesn't won't crash is current instance is NULL.
  344. static UserManager* GetForTesting();
  345. // Sets UserManager instance to the given |user_manager|.
  346. // Returns the previous value of the instance.
  347. static UserManager* SetForTesting(UserManager* user_manager);
  348. };
  349. } // namespace user_manager
  350. #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_H_