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.
5 #ifndef COMPONENTS_USER_MANAGER_USER_MANAGER_H_
6 #define COMPONENTS_USER_MANAGER_USER_MANAGER_H_
10 #include "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/strings/string16.h"
13 #include "components/user_manager/user.h"
14 #include "components/user_manager/user_manager_export.h"
15 #include "components/user_manager/user_type.h"
20 namespace user_manager {
22 class ScopedUserManager;
23 class RemoveUserDelegate;
25 // Interface for UserManagerBase - that provides base implementation for
26 // Chrome OS user management. Typical features:
27 // * Get list of all know users (who have logged into this Chrome OS device)
28 // * Keep track for logged in/LRU users, active user in multi-user session.
29 // * Find/modify users, store user meta-data such as display name/email.
30 class USER_MANAGER_EXPORT UserManager {
32 // Interface that observers of UserManager must implement in order
33 // to receive notification when local state preferences is changed
36 // Called when the local state preferences is changed.
37 virtual void LocalStateChanged(UserManager* user_manager);
39 // Called when the image of the given user is changed.
40 virtual void OnUserImageChanged(const User& user);
42 // Called when the profile image download for the given user fails or
43 // user has the default profile image or no porfile image at all.
44 virtual void OnUserProfileImageUpdateFailed(const User& user);
46 // Called when the profile image for the given user is downloaded.
47 // |profile_image| contains the downloaded profile image.
48 virtual void OnUserProfileImageUpdated(const User& user,
49 const gfx::ImageSkia& profile_image);
51 // Called when the child status of the given user has changed.
52 virtual void OnChildStatusChanged(const User& user);
54 // Called when any of the device cros settings which are responsible for
55 // user sign in are changed.
56 virtual void OnUsersSignInConstraintsChanged();
62 // TODO(xiyuan): Refactor and move this observer out of UserManager.
63 // Observer interface that defines methods used to notify on user session /
64 // active user state changes. Default implementation is empty.
65 class UserSessionStateObserver {
67 // Called when active user has changed.
68 virtual void ActiveUserChanged(const User* active_user);
70 // Called when another user got added to the existing session.
71 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);
78 virtual ~UserSessionStateObserver();
81 // Data retrieved from user account.
82 class UserAccountData {
84 UserAccountData(const base::string16& display_name,
85 const base::string16& given_name,
86 const std::string& locale);
88 const base::string16& display_name() const { return display_name_; }
89 const base::string16& given_name() const { return given_name_; }
90 const std::string& locale() const { return locale_; }
93 const base::string16 display_name_;
94 const base::string16 given_name_;
95 const std::string locale_;
97 DISALLOW_COPY_AND_ASSIGN(UserAccountData);
100 // Initializes UserManager instance to this. Normally should be called right
101 // after creation so that user_manager::UserManager::Get() doesn't fail.
102 // Tests could call this method if they are replacing existing UserManager
103 // instance with their own test instance.
104 virtual void Initialize();
106 // Checks whether the UserManager instance has been created already.
107 // This method is not thread-safe and must be called from the main UI thread.
108 static bool IsInitialized();
110 // Shuts down the UserManager. After this method has been called, the
111 // singleton has unregistered itself as an observer but remains available so
112 // that other classes can access it during their shutdown. This method is not
113 // thread-safe and must be called from the main UI thread.
114 virtual void Shutdown() = 0;
116 // Sets UserManager instance to NULL. Always call Shutdown() first.
117 // This method is not thread-safe and must be called from the main UI thread.
120 // Returns UserManager instance or will crash if it is |NULL| (has either not
121 // been created yet or is already destroyed). This method is not thread-safe
122 // and must be called from the main UI thread.
123 static UserManager* Get();
125 virtual ~UserManager();
127 // Returns a list of users who have logged into this device previously. This
128 // is sorted by last login date with the most recent user at the beginning.
129 virtual const UserList& GetUsers() const = 0;
131 // Returns list of users allowed for logging in into multi-profile session.
132 // Users that have a policy that prevents them from being added to the
133 // multi-profile session will still be part of this list as long as they
134 // are regular users (i.e. not a public session/supervised etc.).
135 // Returns an empty list in case when primary user is not a regular one or
136 // has a policy that prohibits it to be part of multi-profile session.
137 virtual UserList GetUsersAllowedForMultiProfile() const = 0;
139 // Returns a list of users who are currently logged in.
140 virtual const UserList& GetLoggedInUsers() const = 0;
142 // Returns a list of users who are currently logged in in the LRU order -
143 // so the active user is the first one in the list. If there is no user logged
144 // in, the current user will be returned.
145 virtual const UserList& GetLRULoggedInUsers() const = 0;
147 // Returns a list of users who can unlock the device.
148 // This list is based on policy and whether user is able to do unlock.
150 // * If user has primary-only policy then it is the only user in unlock users.
151 // * Otherwise all users with unrestricted policy are added to this list.
152 // All users that are unable to perform unlock are excluded from this list.
153 virtual UserList GetUnlockUsers() const = 0;
155 // Returns account Id of the owner user. Returns an empty Id if there is
156 // no owner for the device.
157 virtual const AccountId& GetOwnerAccountId() const = 0;
159 // Indicates that a user with the given |account_id| has just logged in. The
160 // persistent list is updated accordingly if the user is not ephemeral.
161 // |browser_restart| is true when reloading Chrome after crash to distinguish
162 // from normal sign in flow.
163 // |username_hash| is used to identify homedir mount point.
164 virtual void UserLoggedIn(const AccountId& account_id,
165 const std::string& username_hash,
166 bool browser_restart,
169 // Switches to active user identified by |account_id|. User has to be logged
171 virtual void SwitchActiveUser(const AccountId& account_id) = 0;
173 // Switches to the last active user (called after crash happens and session
174 // restore has completed).
175 virtual void SwitchToLastActiveUser() = 0;
177 // Invoked by session manager to inform session start.
178 virtual void OnSessionStarted() = 0;
180 // Invoked once profile initialization has been completed. This allows various
181 // subsystems (for example, policy framework) to skip an expensive online
182 // initialization process, and also allows the signin screen to force an
183 // online signin if it knows that profile initialization has not yet
184 // completed. |user| is the User associated with the profile that has
185 // completed initialization.
186 virtual void OnProfileInitialized(User* user) = 0;
188 // Removes the user from the device. Note, it will verify that the given user
189 // isn't the owner, so calling this method for the owner will take no effect.
190 // Note, |delegate| can be NULL.
191 virtual void RemoveUser(const AccountId& account_id,
192 RemoveUserDelegate* delegate) = 0;
194 // Removes the user from the persistent list only. Also removes the user's
196 virtual void RemoveUserFromList(const AccountId& account_id) = 0;
198 // Returns true if a user with the given account id is found in the persistent
199 // list or currently logged in as ephemeral.
200 virtual bool IsKnownUser(const AccountId& account_id) const = 0;
202 // Returns the user with the given account id if found in the persistent
203 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
204 virtual const User* FindUser(const AccountId& account_id) const = 0;
206 // Returns the user with the given account id if found in the persistent
207 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
208 // Same as FindUser but returns non-const pointer to User object.
209 virtual User* FindUserAndModify(const AccountId& account_id) = 0;
211 // Returns the logged-in user that is currently active within this session.
212 // There could be multiple users logged in at the the same but for now
213 // we support only one of them being active.
214 virtual const User* GetActiveUser() const = 0;
215 virtual User* GetActiveUser() = 0;
217 // Returns the primary user of the current session. It is recorded for the
218 // first signed-in user and does not change thereafter.
219 virtual const User* GetPrimaryUser() const = 0;
221 // Saves user's oauth token status in local state preferences.
222 virtual void SaveUserOAuthStatus(
223 const AccountId& account_id,
224 User::OAuthTokenStatus oauth_token_status) = 0;
226 // Saves a flag indicating whether online authentication against GAIA should
227 // be enforced during the user's next sign-in.
228 virtual void SaveForceOnlineSignin(const AccountId& account_id,
229 bool force_online_signin) = 0;
231 // Saves user's displayed name in local state preferences.
232 // Ignored If there is no such user.
233 virtual void SaveUserDisplayName(const AccountId& account_id,
234 const base::string16& display_name) = 0;
236 // Updates data upon User Account download.
237 virtual void UpdateUserAccountData(const AccountId& account_id,
238 const UserAccountData& account_data) = 0;
240 // Returns the display name for user |account_id| if it is known (was
241 // previously set by a |SaveUserDisplayName| call).
242 // Otherwise, returns an empty string.
243 virtual base::string16 GetUserDisplayName(
244 const AccountId& account_id) const = 0;
246 // Saves user's displayed (non-canonical) email in local state preferences.
247 // Ignored If there is no such user.
248 virtual void SaveUserDisplayEmail(const AccountId& account_id,
249 const std::string& display_email) = 0;
251 // Returns the display email for user |account_id| if it is known (was
252 // previously set by a |SaveUserDisplayEmail| call).
253 // Otherwise, returns |account_id| itself.
254 virtual std::string GetUserDisplayEmail(
255 const AccountId& account_id) const = 0;
257 // Saves user's type for |user| into local state preferences.
258 virtual void SaveUserType(const User* user) = 0;
260 // Returns true if current user is an owner.
261 virtual bool IsCurrentUserOwner() const = 0;
263 // Returns true if current user is not existing one (hasn't signed in before).
264 virtual bool IsCurrentUserNew() const = 0;
266 // Returns true if data stored or cached for the current user outside that
267 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
268 // display email) is ephemeral.
269 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
271 // Returns true if data stored or cached for the current user inside that
272 // user's cryptohome is ephemeral.
273 virtual bool IsCurrentUserCryptohomeDataEphemeral() const = 0;
275 // Returns true if the current user's session can be locked (i.e. the user has
276 // a password with which to unlock the session).
277 virtual bool CanCurrentUserLock() const = 0;
279 // Returns true if at least one user has signed in.
280 virtual bool IsUserLoggedIn() const = 0;
282 // Returns true if we're logged in as a user with gaia account.
283 virtual bool IsLoggedInAsUserWithGaiaAccount() const = 0;
285 // Returns true if we're logged in as a child user.
286 virtual bool IsLoggedInAsChildUser() const = 0;
288 // Returns true if we're logged in as a public account.
289 virtual bool IsLoggedInAsPublicAccount() const = 0;
291 // Returns true if we're logged in as a Guest.
292 virtual bool IsLoggedInAsGuest() const = 0;
294 // Returns true if we're logged in as a legacy supervised user.
295 virtual bool IsLoggedInAsSupervisedUser() const = 0;
297 // Returns true if we're logged in as a kiosk app.
298 virtual bool IsLoggedInAsKioskApp() const = 0;
300 // Returns true if we're logged in as a ARC kiosk app.
301 virtual bool IsLoggedInAsArcKioskApp() const = 0;
303 // Returns true if we're logged in as the stub user used for testing on Linux.
304 virtual bool IsLoggedInAsStub() const = 0;
306 // Returns true if data stored or cached for the user with the given
308 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
309 // status, display name, display email) is to be treated as ephemeral.
310 virtual bool IsUserNonCryptohomeDataEphemeral(
311 const AccountId& account_id) const = 0;
313 virtual bool IsUserCryptohomeDataEphemeral(
314 const AccountId& account_id) const = 0;
316 virtual void AddObserver(Observer* obs) = 0;
317 virtual void RemoveObserver(Observer* obs) = 0;
319 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
320 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
322 virtual void NotifyLocalStateChanged() = 0;
323 virtual void NotifyUserImageChanged(const User& user) = 0;
324 virtual void NotifyUserProfileImageUpdateFailed(const User& user) = 0;
325 virtual void NotifyUserProfileImageUpdated(
327 const gfx::ImageSkia& profile_image) = 0;
328 virtual void NotifyUsersSignInConstraintsChanged() = 0;
330 // Resets this profile to be regarded as if it has never been initialized
331 // before. Used on profile wipe.
332 virtual void ResetProfileEverInitialized(const AccountId& account_id) = 0;
334 // Returns true if supervised users allowed.
335 virtual bool AreSupervisedUsersAllowed() const = 0;
337 // Returns true if guest user is allowed.
338 virtual bool IsGuestSessionAllowed() const = 0;
340 // Returns true if the |user|, which has a GAIA account is allowed according
341 // to device settings and policies.
342 // Accept only users who has gaia account.
343 virtual bool IsGaiaUserAllowed(const User& user) const = 0;
345 // Returns true if |user| is allowed depending on device policies.
346 // Accepted user types: USER_TYPE_REGULAR, USER_TYPE_GUEST,
347 // USER_TYPE_SUPERVISED, USER_TYPE_CHILD.
348 virtual bool IsUserAllowed(const User& user) const = 0;
350 // Returns "Local State" PrefService instance.
351 virtual PrefService* GetLocalState() const = 0;
353 // Checks for platform-specific known users matching given |user_email| and
354 // |gaia_id|. If data matches a known account, fills |out_account_id| with
355 // account id and returns true.
356 virtual bool GetPlatformKnownUserId(const std::string& user_email,
357 const std::string& gaia_id,
358 AccountId* out_account_id) const = 0;
360 // Returns account id of the Guest user.
361 virtual const AccountId& GetGuestAccountId() const = 0;
363 // Returns true if this is first exec after boot.
364 virtual bool IsFirstExecAfterBoot() const = 0;
366 // Actually removes cryptohome.
367 virtual void AsyncRemoveCryptohome(const AccountId& account_id) const = 0;
369 // Returns true if |account_id| is Guest user.
370 virtual bool IsGuestAccountId(const AccountId& account_id) const = 0;
372 // Returns true if |account_id| is Stub user.
373 virtual bool IsStubAccountId(const AccountId& account_id) const = 0;
375 // Returns true if |account_id| is supervised.
376 virtual bool IsSupervisedAccountId(const AccountId& account_id) const = 0;
378 virtual bool IsDeviceLocalAccountMarkedForRemoval(
379 const AccountId& account_id) const = 0;
381 // Returns true when the browser has crashed and restarted during the current
383 virtual bool HasBrowserRestarted() const = 0;
385 // Returns image from resources bundle.
386 virtual const gfx::ImageSkia& GetResourceImagekiaNamed(int id) const = 0;
388 // Returns string from resources bundle.
389 virtual base::string16 GetResourceStringUTF16(int string_id) const = 0;
391 // Schedules CheckAndResolveLocale using given task runner and
392 // |on_resolved_callback| as reply callback.
393 virtual void ScheduleResolveLocale(
394 const std::string& locale,
395 base::OnceClosure on_resolved_callback,
396 std::string* out_resolved_locale) const = 0;
398 // Returns true if |image_index| is a valid default user image index.
399 virtual bool IsValidDefaultUserImageId(int image_index) const = 0;
401 UserType CalculateUserType(const AccountId& account_id,
403 bool browser_restart,
404 bool is_child) const;
407 // Sets UserManager instance.
408 static void SetInstance(UserManager* user_manager);
410 // Pointer to the existing UserManager instance (if any).
411 // Usually is set by calling Initialize(), reset by calling Destroy().
412 // Not owned since specific implementation of UserManager should decide on its
413 // own appropriate owner. For src/chrome implementation such place is
414 // g_browser_process->platform_part().
415 static UserManager* instance;
418 friend class ScopedUserManager;
420 // Same as Get() but doesn't won't crash is current instance is NULL.
421 static UserManager* GetForTesting();
423 // Sets UserManager instance to the given |user_manager|.
424 // Returns the previous value of the instance.
425 static UserManager* SetForTesting(UserManager* user_manager);
428 // TODO(xiyuan): Move this along with UserSessionStateObserver
429 class USER_MANAGER_EXPORT ScopedUserSessionStateObserver {
431 explicit ScopedUserSessionStateObserver(
432 UserManager::UserSessionStateObserver* observer);
433 ~ScopedUserSessionStateObserver();
436 UserManager::UserSessionStateObserver* const observer_;
438 DISALLOW_COPY_AND_ASSIGN(ScopedUserSessionStateObserver);
441 } // namespace user_manager
443 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_H_