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 "components/user_manager/user.h"
11 #include "components/user_manager/user_manager_export.h"
12 #include "components/user_manager/user_type.h"
15 class ScopedUserManagerEnabler
;
18 namespace user_manager
{
20 class RemoveUserDelegate
;
22 // Interface for UserManagerBase - that provides base implementation for
23 // Chrome OS user management. Typical features:
24 // * Get list of all know users (who have logged into this Chrome OS device)
25 // * Keep track for logged in/LRU users, active user in multi-user session.
26 // * Find/modify users, store user meta-data such as display name/email.
27 class USER_MANAGER_EXPORT UserManager
{
29 // Interface that observers of UserManager must implement in order
30 // to receive notification when local state preferences is changed
33 // Called when the local state preferences is changed.
34 virtual void LocalStateChanged(UserManager
* user_manager
);
40 // TODO(nkostylev): Refactor and move this observer out of UserManager.
41 // Observer interface that defines methods used to notify on user session /
42 // active user state changes. Default implementation is empty.
43 class UserSessionStateObserver
{
45 // Called when active user has changed.
46 virtual void ActiveUserChanged(const User
* active_user
);
48 // Called when another user got added to the existing session.
49 virtual void UserAddedToSession(const User
* added_user
);
51 // Called right before notifying on user change so that those who rely
52 // on user_id hash would be accessing up-to-date value.
53 virtual void ActiveUserHashChanged(const std::string
& hash
);
55 // Called when child status has changed.
56 virtual void UserChangedChildStatus(User
* user
);
59 virtual ~UserSessionStateObserver();
62 // Data retrieved from user account.
63 class UserAccountData
{
65 UserAccountData(const base::string16
& display_name
,
66 const base::string16
& given_name
,
67 const std::string
& locale
);
69 const base::string16
& display_name() const { return display_name_
; }
70 const base::string16
& given_name() const { return given_name_
; }
71 const std::string
& locale() const { return locale_
; }
74 const base::string16 display_name_
;
75 const base::string16 given_name_
;
76 const std::string locale_
;
78 DISALLOW_COPY_AND_ASSIGN(UserAccountData
);
81 // Initializes UserManager instance to this. Normally should be called right
82 // after creation so that user_manager::UserManager::Get() doesn't fail.
83 // Tests could call this method if they are replacing existing UserManager
84 // instance with their own test instance.
87 // Checks whether the UserManager instance has been created already.
88 // This method is not thread-safe and must be called from the main UI thread.
89 static bool IsInitialized();
91 // Shuts down the UserManager. After this method has been called, the
92 // singleton has unregistered itself as an observer but remains available so
93 // that other classes can access it during their shutdown. This method is not
94 // thread-safe and must be called from the main UI thread.
95 virtual void Shutdown() = 0;
97 // Sets UserManager instance to NULL. Always call Shutdown() first.
98 // This method is not thread-safe and must be called from the main UI thread.
101 // Returns UserManager instance or will crash if it is |NULL| (has either not
102 // been created yet or is already destroyed). This method is not thread-safe
103 // and must be called from the main UI thread.
104 static UserManager
* Get();
106 virtual ~UserManager();
108 // Returns a list of users who have logged into this device previously. This
109 // is sorted by last login date with the most recent user at the beginning.
110 virtual const UserList
& GetUsers() const = 0;
112 // Returns list of users allowed for logging in into multi-profile session.
113 // Users that have a policy that prevents them from being added to the
114 // multi-profile session will still be part of this list as long as they
115 // are regular users (i.e. not a public session/supervised etc.).
116 // Returns an empty list in case when primary user is not a regular one or
117 // has a policy that prohibits it to be part of multi-profile session.
118 virtual UserList
GetUsersAllowedForMultiProfile() const = 0;
120 // Returns a list of users who are currently logged in.
121 virtual const UserList
& GetLoggedInUsers() const = 0;
123 // Returns a list of users who are currently logged in in the LRU order -
124 // so the active user is the first one in the list. If there is no user logged
125 // in, the current user will be returned.
126 virtual const UserList
& GetLRULoggedInUsers() const = 0;
128 // Returns a list of users who can unlock the device.
129 // This list is based on policy and whether user is able to do unlock.
131 // * If user has primary-only policy then it is the only user in unlock users.
132 // * Otherwise all users with unrestricted policy are added to this list.
133 // All users that are unable to perform unlock are excluded from this list.
134 virtual UserList
GetUnlockUsers() const = 0;
136 // Returns the email of the owner user. Returns an empty string if there is
137 // no owner for the device.
138 virtual const std::string
& GetOwnerEmail() const = 0;
140 // Indicates that a user with the given |user_id| has just logged in. The
141 // persistent list is updated accordingly if the user is not ephemeral.
142 // |browser_restart| is true when reloading Chrome after crash to distinguish
143 // from normal sign in flow.
144 // |username_hash| is used to identify homedir mount point.
145 virtual void UserLoggedIn(const std::string
& user_id
,
146 const std::string
& username_hash
,
147 bool browser_restart
) = 0;
149 // Switches to active user identified by |user_id|. User has to be logged in.
150 virtual void SwitchActiveUser(const std::string
& user_id
) = 0;
152 // Switches to the last active user (called after crash happens and session
153 // restore has completed).
154 virtual void SwitchToLastActiveUser() = 0;
156 // Called when browser session is started i.e. after
157 // browser_creator.LaunchBrowser(...) was called after user sign in.
158 // When user is at the image screen IsUserLoggedIn() will return true
159 // but IsSessionStarted() will return false. During the kiosk splash screen,
160 // we perform additional initialization after the user is logged in but
161 // before the session has been started.
162 // Fires NOTIFICATION_SESSION_STARTED.
163 virtual void SessionStarted() = 0;
165 // Removes the user from the device. Note, it will verify that the given user
166 // isn't the owner, so calling this method for the owner will take no effect.
167 // Note, |delegate| can be NULL.
168 virtual void RemoveUser(const std::string
& user_id
,
169 RemoveUserDelegate
* delegate
) = 0;
171 // Removes the user from the persistent list only. Also removes the user's
173 virtual void RemoveUserFromList(const std::string
& user_id
) = 0;
175 // Returns true if a user with the given user id is found in the persistent
176 // list or currently logged in as ephemeral.
177 virtual bool IsKnownUser(const std::string
& user_id
) const = 0;
179 // Returns the user with the given user id if found in the persistent
180 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
181 virtual const User
* FindUser(const std::string
& user_id
) const = 0;
183 // Returns the user with the given user id if found in the persistent
184 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
185 // Same as FindUser but returns non-const pointer to User object.
186 virtual User
* FindUserAndModify(const std::string
& user_id
) = 0;
188 // Returns the logged-in user.
189 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
190 // http://crbug.com/230852
191 virtual const User
* GetLoggedInUser() const = 0;
192 virtual User
* GetLoggedInUser() = 0;
194 // Returns the logged-in user that is currently active within this session.
195 // There could be multiple users logged in at the the same but for now
196 // we support only one of them being active.
197 virtual const User
* GetActiveUser() const = 0;
198 virtual User
* GetActiveUser() = 0;
200 // Returns the primary user of the current session. It is recorded for the
201 // first signed-in user and does not change thereafter.
202 virtual const User
* GetPrimaryUser() const = 0;
204 // Saves user's oauth token status in local state preferences.
205 virtual void SaveUserOAuthStatus(
206 const std::string
& user_id
,
207 User::OAuthTokenStatus oauth_token_status
) = 0;
209 // Saves a flag indicating whether online authentication against GAIA should
210 // be enforced during the user's next sign-in.
211 virtual void SaveForceOnlineSignin(const std::string
& user_id
,
212 bool force_online_signin
) = 0;
214 // Saves user's displayed name in local state preferences.
215 // Ignored If there is no such user.
216 virtual void SaveUserDisplayName(const std::string
& user_id
,
217 const base::string16
& display_name
) = 0;
219 // Updates data upon User Account download.
220 virtual void UpdateUserAccountData(const std::string
& user_id
,
221 const UserAccountData
& account_data
) = 0;
223 // Returns the display name for user |user_id| if it is known (was
224 // previously set by a |SaveUserDisplayName| call).
225 // Otherwise, returns an empty string.
226 virtual base::string16
GetUserDisplayName(
227 const std::string
& user_id
) const = 0;
229 // Saves user's displayed (non-canonical) email in local state preferences.
230 // Ignored If there is no such user.
231 virtual void SaveUserDisplayEmail(const std::string
& user_id
,
232 const std::string
& display_email
) = 0;
234 // Returns the display email for user |user_id| if it is known (was
235 // previously set by a |SaveUserDisplayEmail| call).
236 // Otherwise, returns |user_id| itself.
237 virtual std::string
GetUserDisplayEmail(const std::string
& user_id
) const = 0;
239 // Saves user's type for user |user_id| into local state preferences.
240 // Ignored If there is no such user.
241 virtual void SaveUserType(const std::string
& user_id
,
242 const UserType
& user_type
) = 0;
244 // Returns true if current user is an owner.
245 virtual bool IsCurrentUserOwner() const = 0;
247 // Returns true if current user is not existing one (hasn't signed in before).
248 virtual bool IsCurrentUserNew() const = 0;
250 // Returns true if data stored or cached for the current user outside that
251 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
252 // display email) is ephemeral.
253 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
255 // Returns true if the current user's session can be locked (i.e. the user has
256 // a password with which to unlock the session).
257 virtual bool CanCurrentUserLock() const = 0;
259 // Returns true if at least one user has signed in.
260 virtual bool IsUserLoggedIn() const = 0;
262 // Returns true if we're logged in as a user with gaia account.
263 virtual bool IsLoggedInAsUserWithGaiaAccount() const = 0;
265 // Returns true if we're logged in as a child user.
266 virtual bool IsLoggedInAsChildUser() const = 0;
268 // Returns true if we're logged in as a public account.
269 virtual bool IsLoggedInAsPublicAccount() const = 0;
271 // Returns true if we're logged in as a Guest.
272 virtual bool IsLoggedInAsGuest() const = 0;
274 // Returns true if we're logged in as a legacy supervised user.
275 virtual bool IsLoggedInAsSupervisedUser() const = 0;
277 // Returns true if we're logged in as a kiosk app.
278 virtual bool IsLoggedInAsKioskApp() const = 0;
280 // Returns true if we're logged in as the stub user used for testing on Linux.
281 virtual bool IsLoggedInAsStub() const = 0;
283 // Returns true if we're logged in and browser has been started i.e.
284 // browser_creator.LaunchBrowser(...) was called after sign in
285 // or restart after crash.
286 virtual bool IsSessionStarted() const = 0;
288 // Returns true if data stored or cached for the user with the given user id
289 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
290 // status, display name, display email) is to be treated as ephemeral.
291 virtual bool IsUserNonCryptohomeDataEphemeral(
292 const std::string
& user_id
) const = 0;
294 virtual void AddObserver(Observer
* obs
) = 0;
295 virtual void RemoveObserver(Observer
* obs
) = 0;
297 virtual void AddSessionStateObserver(UserSessionStateObserver
* obs
) = 0;
298 virtual void RemoveSessionStateObserver(UserSessionStateObserver
* obs
) = 0;
300 virtual void NotifyLocalStateChanged() = 0;
302 // Changes the child status and notifies observers.
303 virtual void ChangeUserChildStatus(User
* user
, bool is_child
) = 0;
306 // Returns true if supervised users allowed.
307 virtual bool AreSupervisedUsersAllowed() const = 0;
310 // Sets UserManager instance.
311 static void SetInstance(UserManager
* user_manager
);
313 // Pointer to the existing UserManager instance (if any).
314 // Usually is set by calling Initialize(), reset by calling Destroy().
315 // Not owned since specific implementation of UserManager should decide on its
316 // own appropriate owner. For src/chrome implementation such place is
317 // g_browser_process->platform_part().
318 static UserManager
* instance
;
321 friend class chromeos::ScopedUserManagerEnabler
;
323 // Same as Get() but doesn't won't crash is current instance is NULL.
324 static UserManager
* GetForTesting();
326 // Sets UserManager instance to the given |user_manager|.
327 // Returns the previous value of the instance.
328 static UserManager
* SetForTesting(UserManager
* user_manager
);
331 } // namespace user_manager
333 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_H_