1 // Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_
10 #include "chrome/browser/chromeos/base/locale_util.h"
11 #include "chrome/browser/chromeos/login/user.h"
12 #include "chrome/browser/chromeos/login/user_flow.h"
14 class PrefRegistrySimple;
18 class MultiProfileUserController;
19 class RemoveUserDelegate;
20 class UserImageManager;
21 class SupervisedUserManager;
23 // Base class for UserManagerImpl - provides a mechanism for discovering users
24 // who have logged into this Chrome OS device before and updating that list.
27 // Interface that observers of UserManager must implement in order
28 // to receive notification when local state preferences is changed
31 // Called when the local state preferences is changed.
32 virtual void LocalStateChanged(UserManager* user_manager);
38 // TODO(nkostylev): Refactor and move this observer out of UserManager.
39 // Observer interface that defines methods used to notify on user session /
40 // active user state changes. Default implementation is empty.
41 class UserSessionStateObserver {
43 // Called when active user has changed.
44 virtual void ActiveUserChanged(const User* active_user);
46 // Called when another user got added to the existing session.
47 virtual void UserAddedToSession(const User* added_user);
49 // Called right before notifying on user change so that those who rely
50 // on user_id hash would be accessing up-to-date value.
51 virtual void ActiveUserHashChanged(const std::string& hash);
53 // Called when UserManager finishes restoring user sessions after crash.
54 virtual void PendingUserSessionsRestoreFinished();
57 virtual ~UserSessionStateObserver();
60 // Data retrieved from user account.
61 class UserAccountData {
63 UserAccountData(const base::string16& display_name,
64 const base::string16& given_name,
65 const std::string& locale);
67 const base::string16& display_name() const { return display_name_; }
68 const base::string16& given_name() const { return given_name_; }
69 const std::string& locale() const { return locale_; }
72 const base::string16 display_name_;
73 const base::string16 given_name_;
74 const std::string locale_;
76 DISALLOW_COPY_AND_ASSIGN(UserAccountData);
79 // Username for stub login when not running on ChromeOS.
80 static const char kStubUser[];
82 // Username for the login screen. It is only used to identify login screen
83 // tries to set default wallpaper. It is not a real user.
84 static const char kSignInUser[];
86 // Magic e-mail addresses are bad. They exist here because some code already
87 // depends on them and it is hard to figure out what. Any user types added in
88 // the future should be identified by a new |UserType|, not a new magic e-mail
90 // Username for Guest session user.
91 static const char kGuestUserName[];
93 // Domain that is used for all locally managed users.
94 static const char kLocallyManagedUserDomain[];
96 // The retail mode user has a magic, domainless e-mail address.
97 static const char kRetailModeUserName[];
99 // Creates the singleton instance. This method is not thread-safe and must be
100 // called from the main UI thread.
101 static void Initialize();
103 // Checks whether the singleton instance has been created already. This method
104 // is not thread-safe and must be called from the main UI thread.
105 static bool IsInitialized();
107 // Shuts down the UserManager. After this method has been called, the
108 // singleton has unregistered itself as an observer but remains available so
109 // that other classes can access it during their shutdown. This method is not
110 // thread-safe and must be called from the main UI thread.
111 virtual void Shutdown() = 0;
113 // Destroys the singleton instance. Always call Shutdown() first. This method
114 // is not thread-safe and must be called from the main UI thread.
115 static void Destroy();
117 // Returns the singleton instance or |NULL| if the singleton has either not
118 // been created yet or is already destroyed. This method is not thread-safe
119 // and must be called from the main UI thread.
120 static UserManager* Get();
122 // Registers user manager preferences.
123 static void RegisterPrefs(PrefRegistrySimple* registry);
125 // Returns true if multiple profiles support is allowed.
126 static bool IsMultipleProfilesAllowed();
128 virtual ~UserManager();
130 virtual MultiProfileUserController* GetMultiProfileUserController() = 0;
131 virtual UserImageManager* GetUserImageManager(const std::string& user_id) = 0;
132 virtual SupervisedUserManager* GetSupervisedUserManager() = 0;
134 // Returns a list of users who have logged into this device previously. This
135 // is sorted by last login date with the most recent user at the beginning.
136 virtual const UserList& GetUsers() const = 0;
138 // Returns list of users admitted for logging in into multiprofile session.
139 virtual UserList GetUsersAdmittedForMultiProfile() const = 0;
141 // Returns a list of users who are currently logged in.
142 virtual const UserList& GetLoggedInUsers() const = 0;
144 // Returns a list of users who are currently logged in in the LRU order -
145 // so the active user is the first one in the list. If there is no user logged
146 // in, the current user will be returned.
147 virtual const UserList& GetLRULoggedInUsers() = 0;
149 // Returns a list of users who can unlock the device.
150 // This list is based on policy and whether user is able to do unlock.
152 // * If user has primary-only policy then it is the only user in unlock users.
153 // * Otherwise all users with unrestricted policy are added to this list.
154 // All users that are unable to perform unlock are excluded from this list.
155 virtual UserList GetUnlockUsers() const = 0;
157 // Returns the email of the owner user. Returns an empty string if there is
158 // no owner for the device.
159 virtual const std::string& GetOwnerEmail() = 0;
161 // Indicates that a user with the given |user_id| has just logged in. The
162 // persistent list is updated accordingly if the user is not ephemeral.
163 // |browser_restart| is true when reloading Chrome after crash to distinguish
164 // from normal sign in flow.
165 // |username_hash| is used to identify homedir mount point.
166 virtual void UserLoggedIn(const std::string& user_id,
167 const std::string& username_hash,
168 bool browser_restart) = 0;
170 // Switches to active user identified by |user_id|. User has to be logged in.
171 virtual void SwitchActiveUser(const std::string& user_id) = 0;
173 // Called when browser session is started i.e. after
174 // browser_creator.LaunchBrowser(...) was called after user sign in.
175 // When user is at the image screen IsUserLoggedIn() will return true
176 // but IsSessionStarted() will return false. During the kiosk splash screen,
177 // we perform additional initialization after the user is logged in but
178 // before the session has been started.
179 // Fires NOTIFICATION_SESSION_STARTED.
180 virtual void SessionStarted() = 0;
182 // Usually is called when Chrome is restarted after a crash and there's an
183 // active session. First user (one that is passed with --login-user) Chrome
184 // session has been already restored at this point. This method asks session
185 // manager for all active user sessions, marks them as logged in
186 // and notifies observers.
187 virtual void RestoreActiveSessions() = 0;
189 // Removes the user from the device. Note, it will verify that the given user
190 // isn't the owner, so calling this method for the owner will take no effect.
191 // Note, |delegate| can be NULL.
192 virtual void RemoveUser(const std::string& user_id,
193 RemoveUserDelegate* delegate) = 0;
195 // Removes the user from the persistent list only. Also removes the user's
197 virtual void RemoveUserFromList(const std::string& user_id) = 0;
199 // Returns true if a user with the given user id is found in the persistent
200 // list or currently logged in as ephemeral.
201 virtual bool IsKnownUser(const std::string& user_id) const = 0;
203 // Returns the user with the given user id if found in the persistent
204 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
205 virtual const User* FindUser(const std::string& user_id) const = 0;
207 // Returns the user with the given user id if found in the persistent
208 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
209 // Same as FindUser but returns non-const pointer to User object.
210 virtual User* FindUserAndModify(const std::string& user_id) = 0;
212 // Returns the logged-in user.
213 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
214 // http://crbug.com/230852
215 virtual const User* GetLoggedInUser() const = 0;
216 virtual User* GetLoggedInUser() = 0;
218 // Returns the logged-in user that is currently active within this session.
219 // There could be multiple users logged in at the the same but for now
220 // we support only one of them being active.
221 virtual const User* GetActiveUser() const = 0;
222 virtual User* GetActiveUser() = 0;
224 // Returns the primary user of the current session. It is recorded for the
225 // first signed-in user and does not change thereafter.
226 virtual const User* GetPrimaryUser() const = 0;
228 // Returns NULL if User is not created.
229 virtual User* GetUserByProfile(Profile* profile) const = 0;
231 /// Returns NULL if profile for user is not found or is not fully loaded.
232 virtual Profile* GetProfileByUser(const User* user) const = 0;
234 // Saves user's oauth token status in local state preferences.
235 virtual void SaveUserOAuthStatus(
236 const std::string& user_id,
237 User::OAuthTokenStatus oauth_token_status) = 0;
239 // Saves a flag indicating whether online authentication against GAIA should
240 // be enforced during the user's next sign-in.
241 virtual void SaveForceOnlineSignin(const std::string& user_id,
242 bool force_online_signin) = 0;
244 // Saves user's displayed name in local state preferences.
245 // Ignored If there is no such user.
246 virtual void SaveUserDisplayName(const std::string& user_id,
247 const base::string16& display_name) = 0;
249 // Updates data upon User Account download.
250 virtual void UpdateUserAccountData(const std::string& user_id,
251 const UserAccountData& account_data) = 0;
253 // Returns the display name for user |user_id| if it is known (was
254 // previously set by a |SaveUserDisplayName| call).
255 // Otherwise, returns an empty string.
256 virtual base::string16 GetUserDisplayName(
257 const std::string& user_id) const = 0;
259 // Saves user's displayed (non-canonical) email in local state preferences.
260 // Ignored If there is no such user.
261 virtual void SaveUserDisplayEmail(const std::string& user_id,
262 const std::string& display_email) = 0;
264 // Returns the display email for user |user_id| if it is known (was
265 // previously set by a |SaveUserDisplayEmail| call).
266 // Otherwise, returns |user_id| itself.
267 virtual std::string GetUserDisplayEmail(
268 const std::string& user_id) const = 0;
270 // Returns true if current user is an owner.
271 virtual bool IsCurrentUserOwner() const = 0;
273 // Returns true if current user is not existing one (hasn't signed in before).
274 virtual bool IsCurrentUserNew() const = 0;
276 // Returns true if data stored or cached for the current user outside that
277 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
278 // display email) is ephemeral.
279 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
281 // Returns true if the current user's session can be locked (i.e. the user has
282 // a password with which to unlock the session).
283 virtual bool CanCurrentUserLock() const = 0;
285 // Returns true if at least one user has signed in.
286 virtual bool IsUserLoggedIn() const = 0;
288 // Returns true if we're logged in as a regular user.
289 virtual bool IsLoggedInAsRegularUser() const = 0;
291 // Returns true if we're logged in as a demo user.
292 virtual bool IsLoggedInAsDemoUser() const = 0;
294 // Returns true if we're logged in as a public account.
295 virtual bool IsLoggedInAsPublicAccount() const = 0;
297 // Returns true if we're logged in as a Guest.
298 virtual bool IsLoggedInAsGuest() const = 0;
300 // Returns true if we're logged in as a locally managed user.
301 virtual bool IsLoggedInAsLocallyManagedUser() const = 0;
303 // Returns true if we're logged in as a kiosk app.
304 virtual bool IsLoggedInAsKioskApp() const = 0;
306 // Returns true if we're logged in as the stub user used for testing on Linux.
307 virtual bool IsLoggedInAsStub() const = 0;
309 // Returns true if we're logged in and browser has been started i.e.
310 // browser_creator.LaunchBrowser(...) was called after sign in
311 // or restart after crash.
312 virtual bool IsSessionStarted() const = 0;
314 // Returns true iff browser has been restarted after crash and UserManager
315 // finished restoring user sessions.
316 virtual bool UserSessionsRestored() const = 0;
318 // Returns true when the browser has crashed and restarted during the current
320 virtual bool HasBrowserRestarted() const = 0;
322 // Returns true if data stored or cached for the user with the given user id
323 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
324 // status, display name, display email) is to be treated as ephemeral.
325 virtual bool IsUserNonCryptohomeDataEphemeral(
326 const std::string& user_id) const = 0;
328 // Method that allows to set |flow| for user identified by |user_id|.
329 // Flow should be set before login attempt.
330 // Takes ownership of the |flow|, |flow| will be deleted in case of login
332 virtual void SetUserFlow(const std::string& user_id, UserFlow* flow) = 0;
334 // Return user flow for current user. Returns instance of DefaultUserFlow if
335 // no flow was defined for current user, or user is not logged in.
336 // Returned value should not be cached.
337 virtual UserFlow* GetCurrentUserFlow() const = 0;
339 // Return user flow for user identified by |user_id|. Returns instance of
340 // DefaultUserFlow if no flow was defined for user.
341 // Returned value should not be cached.
342 virtual UserFlow* GetUserFlow(const std::string& user_id) const = 0;
344 // Resets user flow for user identified by |user_id|.
345 virtual void ResetUserFlow(const std::string& user_id) = 0;
347 // Gets/sets chrome oauth client id and secret for kiosk app mode. The default
348 // values can be overridden with kiosk auth file.
349 virtual bool GetAppModeChromeClientOAuthInfo(
350 std::string* chrome_client_id,
351 std::string* chrome_client_secret) = 0;
352 virtual void SetAppModeChromeClientOAuthInfo(
353 const std::string& chrome_client_id,
354 const std::string& chrome_client_secret) = 0;
356 virtual void AddObserver(Observer* obs) = 0;
357 virtual void RemoveObserver(Observer* obs) = 0;
359 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
360 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
362 virtual void NotifyLocalStateChanged() = 0;
364 // Returns true if locally managed users allowed.
365 virtual bool AreLocallyManagedUsersAllowed() const = 0;
367 // Returns profile dir for the user identified by |user_id|.
368 virtual base::FilePath GetUserProfileDir(const std::string& user_id)
371 // Changes browser locale (selects best suitable locale from different
372 // user settings). Returns true if callback will be called.
373 virtual bool RespectLocalePreference(
376 scoped_ptr<locale_util::SwitchLanguageCallback> callback) const = 0;
379 friend class ScopedUserManagerEnabler;
381 // Sets the singleton to the given |user_manager|, taking ownership. Returns
382 // the previous value of the singleton, passing ownership.
383 static UserManager* SetForTesting(UserManager* user_manager);
386 // Helper class for unit tests. Initializes the UserManager singleton to the
387 // given |user_manager| and tears it down again on destruction. If the singleton
388 // had already been initialized, its previous value is restored after tearing
389 // down |user_manager|.
390 class ScopedUserManagerEnabler {
392 // Takes ownership of |user_manager|.
393 explicit ScopedUserManagerEnabler(UserManager* user_manager);
394 ~ScopedUserManagerEnabler();
397 UserManager* previous_user_manager_;
399 DISALLOW_COPY_AND_ASSIGN(ScopedUserManagerEnabler);
402 // Helper class for unit tests. Initializes the UserManager singleton on
403 // construction and tears it down again on destruction.
404 class ScopedTestUserManager {
406 ScopedTestUserManager();
407 ~ScopedTestUserManager();
410 DISALLOW_COPY_AND_ASSIGN(ScopedTestUserManager);
413 } // namespace chromeos
415 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_