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"
14 class ScopedUserManagerEnabler
;
17 namespace user_manager
{
19 class RemoveUserDelegate
;
21 // Interface for UserManagerBase - that provides base implementation for
22 // Chrome OS user management. Typical features:
23 // * Get list of all know users (who have logged into this Chrome OS device)
24 // * Keep track for logged in/LRU users, active user in multi-user session.
25 // * Find/modify users, store user meta-data such as display name/email.
26 class USER_MANAGER_EXPORT UserManager
{
28 // Interface that observers of UserManager must implement in order
29 // to receive notification when local state preferences is changed
32 // Called when the local state preferences is changed.
33 virtual void LocalStateChanged(UserManager
* user_manager
);
39 // TODO(nkostylev): Refactor and move this observer out of UserManager.
40 // Observer interface that defines methods used to notify on user session /
41 // active user state changes. Default implementation is empty.
42 class UserSessionStateObserver
{
44 // Called when active user has changed.
45 virtual void ActiveUserChanged(const User
* active_user
);
47 // Called when another user got added to the existing session.
48 virtual void UserAddedToSession(const User
* added_user
);
50 // Called right before notifying on user change so that those who rely
51 // on user_id hash would be accessing up-to-date value.
52 virtual void ActiveUserHashChanged(const std::string
& hash
);
55 virtual ~UserSessionStateObserver();
58 // Data retrieved from user account.
59 class UserAccountData
{
61 UserAccountData(const base::string16
& display_name
,
62 const base::string16
& given_name
,
63 const std::string
& locale
);
65 const base::string16
& display_name() const { return display_name_
; }
66 const base::string16
& given_name() const { return given_name_
; }
67 const std::string
& locale() const { return locale_
; }
70 const base::string16 display_name_
;
71 const base::string16 given_name_
;
72 const std::string locale_
;
74 DISALLOW_COPY_AND_ASSIGN(UserAccountData
);
77 // Initializes UserManager instance to this. Normally should be called right
78 // after creation so that user_manager::UserManager::Get() doesn't fail.
79 // Tests could call this method if they are replacing existing UserManager
80 // instance with their own test instance.
83 // Checks whether the UserManager instance has been created already.
84 // This method is not thread-safe and must be called from the main UI thread.
85 static bool IsInitialized();
87 // Shuts down the UserManager. After this method has been called, the
88 // singleton has unregistered itself as an observer but remains available so
89 // that other classes can access it during their shutdown. This method is not
90 // thread-safe and must be called from the main UI thread.
91 virtual void Shutdown() = 0;
93 // Sets UserManager instance to NULL. Always call Shutdown() first.
94 // This method is not thread-safe and must be called from the main UI thread.
97 // Returns UserManager instance or will crash if it is |NULL| (has either not
98 // been created yet or is already destroyed). This method is not thread-safe
99 // and must be called from the main UI thread.
100 static UserManager
* Get();
102 virtual ~UserManager();
104 // Returns a list of users who have logged into this device previously. This
105 // is sorted by last login date with the most recent user at the beginning.
106 virtual const UserList
& GetUsers() const = 0;
108 // Returns list of users admitted for logging in into multi-profile session.
109 // Users that have a policy that prevents them from being added to the
110 // multi-profile session will still be part of this list as long as they
111 // are regular users (i.e. not a public session/supervised etc.).
112 // Returns an empty list in case when primary user is not a regular one or
113 // has a policy that prohibids it to be part of multi-profile session.
114 virtual UserList
GetUsersAdmittedForMultiProfile() const = 0;
116 // Returns a list of users who are currently logged in.
117 virtual const UserList
& GetLoggedInUsers() const = 0;
119 // Returns a list of users who are currently logged in in the LRU order -
120 // so the active user is the first one in the list. If there is no user logged
121 // in, the current user will be returned.
122 virtual const UserList
& GetLRULoggedInUsers() const = 0;
124 // Returns a list of users who can unlock the device.
125 // This list is based on policy and whether user is able to do unlock.
127 // * If user has primary-only policy then it is the only user in unlock users.
128 // * Otherwise all users with unrestricted policy are added to this list.
129 // All users that are unable to perform unlock are excluded from this list.
130 virtual UserList
GetUnlockUsers() const = 0;
132 // Returns the email of the owner user. Returns an empty string if there is
133 // no owner for the device.
134 virtual const std::string
& GetOwnerEmail() const = 0;
136 // Indicates that a user with the given |user_id| has just logged in. The
137 // persistent list is updated accordingly if the user is not ephemeral.
138 // |browser_restart| is true when reloading Chrome after crash to distinguish
139 // from normal sign in flow.
140 // |username_hash| is used to identify homedir mount point.
141 virtual void UserLoggedIn(const std::string
& user_id
,
142 const std::string
& username_hash
,
143 bool browser_restart
) = 0;
145 // Switches to active user identified by |user_id|. User has to be logged in.
146 virtual void SwitchActiveUser(const std::string
& user_id
) = 0;
148 // Called when browser session is started i.e. after
149 // browser_creator.LaunchBrowser(...) was called after user sign in.
150 // When user is at the image screen IsUserLoggedIn() will return true
151 // but IsSessionStarted() will return false. During the kiosk splash screen,
152 // we perform additional initialization after the user is logged in but
153 // before the session has been started.
154 // Fires NOTIFICATION_SESSION_STARTED.
155 virtual void SessionStarted() = 0;
157 // Removes the user from the device. Note, it will verify that the given user
158 // isn't the owner, so calling this method for the owner will take no effect.
159 // Note, |delegate| can be NULL.
160 virtual void RemoveUser(const std::string
& user_id
,
161 RemoveUserDelegate
* delegate
) = 0;
163 // Removes the user from the persistent list only. Also removes the user's
165 virtual void RemoveUserFromList(const std::string
& user_id
) = 0;
167 // Returns true if a user with the given user id is found in the persistent
168 // list or currently logged in as ephemeral.
169 virtual bool IsKnownUser(const std::string
& user_id
) const = 0;
171 // Returns the user with the given user id if found in the persistent
172 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
173 virtual const User
* FindUser(const std::string
& user_id
) const = 0;
175 // Returns the user with the given user id if found in the persistent
176 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
177 // Same as FindUser but returns non-const pointer to User object.
178 virtual User
* FindUserAndModify(const std::string
& user_id
) = 0;
180 // Returns the logged-in user.
181 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
182 // http://crbug.com/230852
183 virtual const User
* GetLoggedInUser() const = 0;
184 virtual User
* GetLoggedInUser() = 0;
186 // Returns the logged-in user that is currently active within this session.
187 // There could be multiple users logged in at the the same but for now
188 // we support only one of them being active.
189 virtual const User
* GetActiveUser() const = 0;
190 virtual User
* GetActiveUser() = 0;
192 // Returns the primary user of the current session. It is recorded for the
193 // first signed-in user and does not change thereafter.
194 virtual const User
* GetPrimaryUser() const = 0;
196 // Saves user's oauth token status in local state preferences.
197 virtual void SaveUserOAuthStatus(
198 const std::string
& user_id
,
199 User::OAuthTokenStatus oauth_token_status
) = 0;
201 // Saves a flag indicating whether online authentication against GAIA should
202 // be enforced during the user's next sign-in.
203 virtual void SaveForceOnlineSignin(const std::string
& user_id
,
204 bool force_online_signin
) = 0;
206 // Saves user's displayed name in local state preferences.
207 // Ignored If there is no such user.
208 virtual void SaveUserDisplayName(const std::string
& user_id
,
209 const base::string16
& display_name
) = 0;
211 // Updates data upon User Account download.
212 virtual void UpdateUserAccountData(const std::string
& user_id
,
213 const UserAccountData
& account_data
) = 0;
215 // Returns the display name for user |user_id| if it is known (was
216 // previously set by a |SaveUserDisplayName| call).
217 // Otherwise, returns an empty string.
218 virtual base::string16
GetUserDisplayName(
219 const std::string
& user_id
) const = 0;
221 // Saves user's displayed (non-canonical) email in local state preferences.
222 // Ignored If there is no such user.
223 virtual void SaveUserDisplayEmail(const std::string
& user_id
,
224 const std::string
& display_email
) = 0;
226 // Returns the display email for user |user_id| if it is known (was
227 // previously set by a |SaveUserDisplayEmail| call).
228 // Otherwise, returns |user_id| itself.
229 virtual std::string
GetUserDisplayEmail(const std::string
& user_id
) const = 0;
231 // Returns true if current user is an owner.
232 virtual bool IsCurrentUserOwner() const = 0;
234 // Returns true if current user is not existing one (hasn't signed in before).
235 virtual bool IsCurrentUserNew() const = 0;
237 // Returns true if data stored or cached for the current user outside that
238 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
239 // display email) is ephemeral.
240 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
242 // Returns true if the current user's session can be locked (i.e. the user has
243 // a password with which to unlock the session).
244 virtual bool CanCurrentUserLock() const = 0;
246 // Returns true if at least one user has signed in.
247 virtual bool IsUserLoggedIn() const = 0;
249 // Returns true if we're logged in as a regular user.
250 virtual bool IsLoggedInAsRegularUser() const = 0;
252 // Returns true if we're logged in as a demo user.
253 virtual bool IsLoggedInAsDemoUser() const = 0;
255 // Returns true if we're logged in as a public account.
256 virtual bool IsLoggedInAsPublicAccount() const = 0;
258 // Returns true if we're logged in as a Guest.
259 virtual bool IsLoggedInAsGuest() const = 0;
261 // Returns true if we're logged in as a supervised user.
262 virtual bool IsLoggedInAsSupervisedUser() const = 0;
264 // Returns true if we're logged in as a kiosk app.
265 virtual bool IsLoggedInAsKioskApp() const = 0;
267 // Returns true if we're logged in as the stub user used for testing on Linux.
268 virtual bool IsLoggedInAsStub() const = 0;
270 // Returns true if we're logged in and browser has been started i.e.
271 // browser_creator.LaunchBrowser(...) was called after sign in
272 // or restart after crash.
273 virtual bool IsSessionStarted() const = 0;
275 // Returns true if data stored or cached for the user with the given user id
276 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
277 // status, display name, display email) is to be treated as ephemeral.
278 virtual bool IsUserNonCryptohomeDataEphemeral(
279 const std::string
& user_id
) const = 0;
281 virtual void AddObserver(Observer
* obs
) = 0;
282 virtual void RemoveObserver(Observer
* obs
) = 0;
284 virtual void AddSessionStateObserver(UserSessionStateObserver
* obs
) = 0;
285 virtual void RemoveSessionStateObserver(UserSessionStateObserver
* obs
) = 0;
287 virtual void NotifyLocalStateChanged() = 0;
289 // Returns true if supervised users allowed.
290 virtual bool AreSupervisedUsersAllowed() const = 0;
293 // Sets UserManager instance.
294 static void SetInstance(UserManager
* user_manager
);
296 // Pointer to the existing UserManager instance (if any).
297 // Usually is set by calling Initialize(), reset by calling Destroy().
298 // Not owned since specific implementation of UserManager should decide on its
299 // own appropriate owner. For src/chrome implementation such place is
300 // g_browser_process->platform_part().
301 static UserManager
* instance
;
304 friend class chromeos::ScopedUserManagerEnabler
;
306 // Same as Get() but doesn't won't crash is current instance is NULL.
307 static UserManager
* GetForTesting();
309 // Sets UserManager instance to the given |user_manager|.
310 // Returns the previous value of the instance.
311 static UserManager
* SetForTesting(UserManager
* user_manager
);
314 } // namespace user_manager
316 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_H_