blob: a44f0b0efc51544301ea85be60d71ba6014d5952 [file] [log] [blame]
[email protected]e09155d2012-02-17 23:57:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bab1bceb2010-02-02 18:25:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]56f0ffc82010-02-04 21:30:375#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_
[email protected]bab1bceb2010-02-02 18:25:057
8#include <string>
[email protected]bab1bceb2010-02-02 18:25:059
[email protected]39db06702011-11-10 16:31:2410#include "chrome/browser/chromeos/login/user.h"
[email protected]1a2a6d52013-02-22 19:20:5311#include "chrome/browser/chromeos/login/user_flow.h"
[email protected]bab1bceb2010-02-02 18:25:0512
[email protected]b1de2c72013-02-06 02:45:4713class PrefRegistrySimple;
[email protected]c2a7e682011-03-16 13:03:1814
[email protected]8f0d8ecd12010-04-20 11:51:3015namespace chromeos {
[email protected]39db06702011-11-10 16:31:2416
[email protected]773a8142011-03-02 16:44:1117class RemoveUserDelegate;
[email protected]c4e418f2012-10-15 15:53:4518class UserImageManager;
[email protected]8f0d8ecd12010-04-20 11:51:3019
[email protected]eddc251a2012-03-06 15:44:1420// Base class for UserManagerImpl - provides a mechanism for discovering users
21// who have logged into this Chrome OS device before and updating that list.
22class UserManager {
[email protected]bab1bceb2010-02-02 18:25:0523 public:
[email protected]61a9fb32011-08-03 21:00:5124 // Interface that observers of UserManager must implement in order
25 // to receive notification when local state preferences is changed
26 class Observer {
27 public:
[email protected]64f784cc2013-02-16 02:14:2128 // Called when the local state preferences is changed.
[email protected]f8622a42013-06-07 14:12:3629 virtual void LocalStateChanged(UserManager* user_manager);
[email protected]61a9fb32011-08-03 21:00:5130
31 protected:
[email protected]7cad6b0d2013-04-25 20:29:3232 virtual ~Observer();
33 };
34
[email protected]cfad8752013-06-04 16:58:3435 // TODO(nkostylev): Refactor and move this observer out of UserManager.
36 // Observer interface that defines methods used to notify on user session /
37 // active user state changes. Default implementation is empty.
[email protected]7cad6b0d2013-04-25 20:29:3238 class UserSessionStateObserver {
39 public:
[email protected]cfad8752013-06-04 16:58:3440 // Called when active user has changed.
41 virtual void ActiveUserChanged(const User* active_user);
42
[email protected]7cad6b0d2013-04-25 20:29:3243 // Called right before notifying on user change so that those who rely
44 // on user_id hash would be accessing up-to-date value.
[email protected]cfad8752013-06-04 16:58:3445 virtual void ActiveUserHashChanged(const std::string& hash);
[email protected]7cad6b0d2013-04-25 20:29:3246
[email protected]2622feb82013-05-28 20:41:3847 // Called when UserManager finishes restoring user sessions after crash.
[email protected]cfad8752013-06-04 16:58:3448 virtual void PendingUserSessionsRestoreFinished();
[email protected]2622feb82013-05-28 20:41:3849
[email protected]7cad6b0d2013-04-25 20:29:3250 protected:
51 virtual ~UserSessionStateObserver();
[email protected]61a9fb32011-08-03 21:00:5152 };
53
[email protected]89dbb1772012-07-17 13:47:2554 // Username for stub login when not running on ChromeOS.
55 static const char kStubUser[];
56
[email protected]68e31692013-05-20 13:08:2757 // Magic e-mail addresses are bad. They exist here because some code already
58 // depends on them and it is hard to figure out what. Any user types added in
59 // the future should be identified by a new |UserType|, not a new magic e-mail
60 // address.
61 // Username for Guest session user.
62 static const char kGuestUserName[];
63
[email protected]a2774c382013-01-16 14:35:3864 // Domain that is used for all locally managed users.
65 static const char kLocallyManagedUserDomain[];
66
[email protected]68e31692013-05-20 13:08:2767 // The retail mode user has a magic, domainless e-mail address.
68 static const char kRetailModeUserName[];
69
[email protected]9a68d3a2013-04-22 16:26:5470 // Creates the singleton instance. This method is not thread-safe and must be
[email protected]eddc251a2012-03-06 15:44:1471 // called from the main UI thread.
[email protected]9a68d3a2013-04-22 16:26:5472 static void Initialize();
[email protected]eddc251a2012-03-06 15:44:1473
[email protected]9a68d3a2013-04-22 16:26:5474 // Checks whether the singleton instance has been created already. This method
75 // is not thread-safe and must be called from the main UI thread.
76 static bool IsInitialized();
77
78 // Shuts down the UserManager. After this method has been called, the
79 // singleton has unregistered itself as an observer but remains available so
80 // that other classes can access it during their shutdown. This method is not
81 // thread-safe and must be called from the main UI thread.
82 virtual void Shutdown() = 0;
83
84 // Destroys the singleton instance. Always call Shutdown() first. This method
85 // is not thread-safe and must be called from the main UI thread.
86 static void Destroy();
87
88 // Returns the singleton instance or |NULL| if the singleton has either not
89 // been created yet or is already destroyed. This method is not thread-safe
90 // and must be called from the main UI thread.
91 static UserManager* Get();
[email protected]79bac422013-04-22 15:44:2692
[email protected]eddc251a2012-03-06 15:44:1493 // Registers user manager preferences.
[email protected]b1de2c72013-02-06 02:45:4794 static void RegisterPrefs(PrefRegistrySimple* registry);
[email protected]eddc251a2012-03-06 15:44:1495
[email protected]d81240b2013-09-20 21:05:2896 // Returns true if multiple profiles support is allowed.
97 static bool IsMultipleProfilesAllowed();
98
[email protected]eddc251a2012-03-06 15:44:1499 virtual ~UserManager();
100
[email protected]c4e418f2012-10-15 15:53:45101 virtual UserImageManager* GetUserImageManager() = 0;
102
[email protected]8e85e9462012-03-13 11:23:23103 // Returns a list of users who have logged into this device previously. This
104 // is sorted by last login date with the most recent user at the beginning.
[email protected]eddc251a2012-03-06 15:44:14105 virtual const UserList& GetUsers() const = 0;
106
[email protected]04887162013-05-29 23:01:51107 // Returns list of users admitted for logging in into multiprofile session.
108 virtual UserList GetUsersAdmittedForMultiProfile() const = 0;
109
[email protected]e718e6f2013-04-15 16:01:59110 // Returns a list of users who are currently logged in.
111 virtual const UserList& GetLoggedInUsers() const = 0;
112
[email protected]c8d19f82013-05-18 09:09:41113 // Returns a list of users who are currently logged in in the LRU order -
114 // so the active user is the first one in the list. If there is no user logged
115 // in, the current user will be returned.
116 virtual const UserList& GetLRULoggedInUsers() = 0;
117
[email protected]8f484832013-09-18 02:52:56118 // Returns a list of users who can unlock the device.
119 virtual UserList GetUnlockUsers() const = 0;
120
[email protected]204c19c2013-09-01 23:27:46121 // Returns the email of the owner user. Returns an empty string if there is
122 // no owner for the device.
123 virtual const std::string& GetOwnerEmail() = 0;
124
[email protected]40429592013-03-29 17:52:33125 // Indicates that a user with the given |email| has just logged in. The
[email protected]8e85e9462012-03-13 11:23:23126 // persistent list is updated accordingly if the user is not ephemeral.
[email protected]503fc5b2012-06-14 17:52:12127 // |browser_restart| is true when reloading Chrome after crash to distinguish
128 // from normal sign in flow.
[email protected]40429592013-03-29 17:52:33129 // |username_hash| is used to identify homedir mount point.
130 virtual void UserLoggedIn(const std::string& email,
131 const std::string& username_hash,
132 bool browser_restart) = 0;
[email protected]eddc251a2012-03-06 15:44:14133
[email protected]e718e6f2013-04-15 16:01:59134 // Switches to active user identified by |email|. User has to be logged in.
135 virtual void SwitchActiveUser(const std::string& email) = 0;
136
[email protected]d4f22f22012-05-05 00:44:55137 // Called when browser session is started i.e. after
[email protected]fe7c4872012-05-10 20:06:03138 // browser_creator.LaunchBrowser(...) was called after user sign in.
[email protected]d4f22f22012-05-05 00:44:55139 // When user is at the image screen IsUserLoggedIn() will return true
[email protected]53114692013-09-13 22:07:18140 // but IsSessionStarted() will return false. During the kiosk splash screen,
141 // we perform additional initialization after the user is logged in but
142 // before the session has been started.
[email protected]d4f22f22012-05-05 00:44:55143 // Fires NOTIFICATION_SESSION_STARTED.
144 virtual void SessionStarted() = 0;
145
[email protected]0fbe5d62013-05-23 16:10:16146 // Usually is called when Chrome is restarted after a crash and there's an
147 // active session. First user (one that is passed with --login-user) Chrome
148 // session has been already restored at this point. This method asks session
149 // manager for all active user sessions, marks them as logged in
150 // and notifies observers.
151 virtual void RestoreActiveSessions() = 0;
152
[email protected]f024768b2013-08-13 20:06:41153 // Creates locally managed user with given |display_name| and|local_user_id|
154 // and persists that to user list. Also links this user identified by
155 // |sync_user_id| to manager with a |manager_id|.
156 // Returns created user, or existing user if there already
[email protected]c4e10a32013-01-24 15:05:13157 // was locally managed user with such display name.
[email protected]f024768b2013-08-13 20:06:41158 // TODO(antrim): Refactor into a single struct to have only 1 getter.
[email protected]c4e10a32013-01-24 15:05:13159 virtual const User* CreateLocallyManagedUserRecord(
[email protected]059b94f02013-05-31 09:26:34160 const std::string& manager_id,
[email protected]f024768b2013-08-13 20:06:41161 const std::string& local_user_id,
162 const std::string& sync_user_id,
[email protected]c4e10a32013-01-24 15:05:13163 const string16& display_name) = 0;
164
[email protected]e3ed9f802013-02-28 21:46:43165 // Generates unique username for locally managed user.
166 virtual std::string GenerateUniqueLocallyManagedUserId() = 0;
167
[email protected]eddc251a2012-03-06 15:44:14168 // Removes the user from the device. Note, it will verify that the given user
169 // isn't the owner, so calling this method for the owner will take no effect.
170 // Note, |delegate| can be NULL.
171 virtual void RemoveUser(const std::string& email,
172 RemoveUserDelegate* delegate) = 0;
173
174 // Removes the user from the persistent list only. Also removes the user's
175 // picture.
176 virtual void RemoveUserFromList(const std::string& email) = 0;
177
[email protected]8e85e9462012-03-13 11:23:23178 // Returns true if a user with the given email address is found in the
179 // persistent list or currently logged in as ephemeral.
[email protected]eddc251a2012-03-06 15:44:14180 virtual bool IsKnownUser(const std::string& email) const = 0;
181
[email protected]8e85e9462012-03-13 11:23:23182 // Returns the user with the given email address if found in the persistent
183 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
[email protected]eddc251a2012-03-06 15:44:14184 virtual const User* FindUser(const std::string& email) const = 0;
185
[email protected]c4e10a32013-01-24 15:05:13186 // Returns the locally managed user with the given |display_name| if found in
187 // the persistent list. Returns |NULL| otherwise.
188 virtual const User* FindLocallyManagedUser(
189 const string16& display_name) const = 0;
190
[email protected]0a5da5b2013-10-01 13:48:37191 // Returns the locally managed user with the given |sync_id| if found in
192 // the persistent list. Returns |NULL| otherwise.
193 virtual const User* FindLocallyManagedUserBySyncId(
194 const std::string& sync_id) const = 0;
195
[email protected]eddc251a2012-03-06 15:44:14196 // Returns the logged-in user.
[email protected]e718e6f2013-04-15 16:01:59197 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
198 // http://crbug.com/230852
[email protected]c4e418f2012-10-15 15:53:45199 virtual const User* GetLoggedInUser() const = 0;
200 virtual User* GetLoggedInUser() = 0;
[email protected]eddc251a2012-03-06 15:44:14201
[email protected]e718e6f2013-04-15 16:01:59202 // Returns the logged-in user that is currently active within this session.
203 // There could be multiple users logged in at the the same but for now
204 // we support only one of them being active.
205 virtual const User* GetActiveUser() const = 0;
206 virtual User* GetActiveUser() = 0;
207
[email protected]8f484832013-09-18 02:52:56208 // Returns the primary user of the current session. It is recorded for the
209 // first signed-in user and does not change thereafter.
210 virtual const User* GetPrimaryUser() const = 0;
211
[email protected]eddc251a2012-03-06 15:44:14212 // Saves user's oauth token status in local state preferences.
213 virtual void SaveUserOAuthStatus(
214 const std::string& username,
215 User::OAuthTokenStatus oauth_token_status) = 0;
216
[email protected]7aa538b2012-06-06 00:27:38217 // Saves user's displayed name in local state preferences.
218 // Ignored If there is no such user.
219 virtual void SaveUserDisplayName(const std::string& username,
220 const string16& display_name) = 0;
221
[email protected]c2b68c82013-09-24 02:49:39222 // Updates data upon User Account download.
223 virtual void UpdateUserAccountData(const std::string& username,
224 const string16& display_name,
225 const std::string& locale) = 0;
226
[email protected]7aa538b2012-06-06 00:27:38227 // Returns the display name for user |username| if it is known (was
228 // previously set by a |SaveUserDisplayName| call).
229 // Otherwise, returns an empty string.
230 virtual string16 GetUserDisplayName(
231 const std::string& username) const = 0;
232
[email protected]2f5b4832012-05-15 21:41:37233 // Saves user's displayed (non-canonical) email in local state preferences.
[email protected]eddc251a2012-03-06 15:44:14234 // Ignored If there is no such user.
235 virtual void SaveUserDisplayEmail(const std::string& username,
236 const std::string& display_email) = 0;
237
238 // Returns the display email for user |username| if it is known (was
239 // previously set by a |SaveUserDisplayEmail| call).
240 // Otherwise, returns |username| itself.
241 virtual std::string GetUserDisplayEmail(
242 const std::string& username) const = 0;
243
[email protected]f024768b2013-08-13 20:06:41244 // Returns sync_user_id for locally managed user with |managed_user_id| or
245 // empty string if such user is not found or it doesn't have
246 // sync_user_id defined.
247 virtual std::string GetManagedUserSyncId(
248 const std::string& managed_user_id) const = 0;
249
[email protected]059b94f02013-05-31 09:26:34250 // Returns the display name for manager of user |managed_user_id| if it is
251 // known (was previously set by a |SaveUserDisplayName| call).
252 // Otherwise, returns a manager id.
253 virtual string16 GetManagerDisplayNameForManagedUser(
254 const std::string& managed_user_id) const = 0;
255
256 // Returns the user id for manager of user |managed_user_id| if it is known
257 // (user is actually a managed user).
258 // Otherwise, returns an empty string.
259 virtual std::string GetManagerUserIdForManagedUser(
260 const std::string& managed_user_id) const = 0;
261
[email protected]6c3bdc22013-07-08 18:12:44262 // Returns the display email for manager of user |managed_user_id| if it is
263 // known (user is actually a managed user).
264 // Otherwise, returns an empty string.
265 virtual std::string GetManagerDisplayEmailForManagedUser(
266 const std::string& managed_user_id) const = 0;
267
[email protected]a43c12e2012-03-06 21:57:10268 // Returns true if current user is an owner.
269 virtual bool IsCurrentUserOwner() const = 0;
[email protected]eddc251a2012-03-06 15:44:14270
[email protected]a43c12e2012-03-06 21:57:10271 // Returns true if current user is not existing one (hasn't signed in before).
272 virtual bool IsCurrentUserNew() const = 0;
273
[email protected]bdee4042012-12-07 07:36:30274 // Returns true if data stored or cached for the current user outside that
275 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
276 // display email) is ephemeral.
277 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
[email protected]8e85e9462012-03-13 11:23:23278
[email protected]91545872012-11-21 13:58:27279 // Returns true if the current user's session can be locked (i.e. the user has
280 // a password with which to unlock the session).
281 virtual bool CanCurrentUserLock() const = 0;
282
[email protected]e718e6f2013-04-15 16:01:59283 // Returns true if at least one user has signed in.
[email protected]a43c12e2012-03-06 21:57:10284 virtual bool IsUserLoggedIn() const = 0;
[email protected]eddc251a2012-03-06 15:44:14285
[email protected]364aaef2012-12-04 12:18:13286 // Returns true if we're logged in as a regular user.
287 virtual bool IsLoggedInAsRegularUser() const = 0;
288
[email protected]eddc251a2012-03-06 15:44:14289 // Returns true if we're logged in as a demo user.
290 virtual bool IsLoggedInAsDemoUser() const = 0;
291
[email protected]4b9b73692012-11-01 06:35:55292 // Returns true if we're logged in as a public account.
293 virtual bool IsLoggedInAsPublicAccount() const = 0;
294
[email protected]eddc251a2012-03-06 15:44:14295 // Returns true if we're logged in as a Guest.
296 virtual bool IsLoggedInAsGuest() const = 0;
297
[email protected]a2774c382013-01-16 14:35:38298 // Returns true if we're logged in as a locally managed user.
299 virtual bool IsLoggedInAsLocallyManagedUser() const = 0;
300
[email protected]974bab52013-03-19 09:28:24301 // Returns true if we're logged in as a kiosk app.
302 virtual bool IsLoggedInAsKioskApp() const = 0;
303
[email protected]d4f22f22012-05-05 00:44:55304 // Returns true if we're logged in as the stub user used for testing on Linux.
[email protected]93cc27b2012-03-21 12:44:32305 virtual bool IsLoggedInAsStub() const = 0;
306
[email protected]d4f22f22012-05-05 00:44:55307 // Returns true if we're logged in and browser has been started i.e.
[email protected]fe7c4872012-05-10 20:06:03308 // browser_creator.LaunchBrowser(...) was called after sign in
[email protected]d4f22f22012-05-05 00:44:55309 // or restart after crash.
310 virtual bool IsSessionStarted() const = 0;
311
[email protected]2622feb82013-05-28 20:41:38312 // Returns true iff browser has been restarted after crash and UserManager
313 // finished restoring user sessions.
314 virtual bool UserSessionsRestored() const = 0;
315
[email protected]cf50d182012-12-15 08:37:07316 // Returns true when the browser has crashed and restarted during the current
317 // user's session.
318 virtual bool HasBrowserRestarted() const = 0;
319
[email protected]bdee4042012-12-07 07:36:30320 // Returns true if data stored or cached for the user with the given email
321 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
322 // status, display name, display email) is to be treated as ephemeral.
323 virtual bool IsUserNonCryptohomeDataEphemeral(
324 const std::string& email) const = 0;
[email protected]9b4976f2012-08-29 17:58:40325
[email protected]e3ed9f802013-02-28 21:46:43326 // Create a record about starting locally managed user creation transaction.
327 virtual void StartLocallyManagedUserCreationTransaction(
328 const string16& display_name) = 0;
329
330 // Add user id to locally managed user creation transaction record.
331 virtual void SetLocallyManagedUserCreationTransactionUserId(
332 const std::string& email) = 0;
333
334 // Remove locally managed user creation transaction record.
335 virtual void CommitLocallyManagedUserCreationTransaction() = 0;
336
[email protected]1a2a6d52013-02-22 19:20:53337 // Method that allows to set |flow| for user identified by |email|.
338 // Flow should be set before login attempt.
339 // Takes ownership of the |flow|, |flow| will be deleted in case of login
340 // failure.
341 virtual void SetUserFlow(const std::string& email, UserFlow* flow) = 0;
342
343 // Return user flow for current user. Returns instance of DefaultUserFlow if
344 // no flow was defined for current user, or user is not logged in.
345 // Returned value should not be cached.
346 virtual UserFlow* GetCurrentUserFlow() const = 0;
347
348 // Return user flow for user identified by |email|. Returns instance of
349 // DefaultUserFlow if no flow was defined for user.
350 // Returned value should not be cached.
351 virtual UserFlow* GetUserFlow(const std::string& email) const = 0;
352
[email protected]44426242013-09-13 22:35:34353 // Resets user flow for user identified by |email|.
[email protected]1a2a6d52013-02-22 19:20:53354 virtual void ResetUserFlow(const std::string& email) = 0;
355
[email protected]e282cc62013-03-30 17:39:43356 // Gets/sets chrome oauth client id and secret for kiosk app mode. The default
[email protected]44426242013-09-13 22:35:34357 // values can be overridden with kiosk auth file.
[email protected]e282cc62013-03-30 17:39:43358 virtual bool GetAppModeChromeClientOAuthInfo(
359 std::string* chrome_client_id,
360 std::string* chrome_client_secret) = 0;
361 virtual void SetAppModeChromeClientOAuthInfo(
362 const std::string& chrome_client_id,
363 const std::string& chrome_client_secret) = 0;
364
[email protected]eddc251a2012-03-06 15:44:14365 virtual void AddObserver(Observer* obs) = 0;
366 virtual void RemoveObserver(Observer* obs) = 0;
367
[email protected]7cad6b0d2013-04-25 20:29:32368 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
369 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
370
[email protected]eddc251a2012-03-06 15:44:14371 virtual void NotifyLocalStateChanged() = 0;
[email protected]9a68d3a2013-04-22 16:26:54372
[email protected]59c61c812013-06-22 00:38:14373 // Returns true if locally managed users allowed.
374 virtual bool AreLocallyManagedUsersAllowed() const = 0;
375
[email protected]44426242013-09-13 22:35:34376 // Returns profile dir for the user identified by |email|.
377 virtual base::FilePath GetUserProfileDir(const std::string& email) const = 0;
378
[email protected]9a68d3a2013-04-22 16:26:54379 private:
380 friend class ScopedUserManagerEnabler;
381
382 // Sets the singleton to the given |user_manager|, taking ownership. Returns
383 // the previous value of the singleton, passing ownership.
384 static UserManager* SetForTesting(UserManager* user_manager);
385};
386
[email protected]1cfc9a1f2013-06-13 20:15:30387// Helper class for unit tests. Initializes the UserManager singleton to the
388// given |user_manager| and tears it down again on destruction. If the singleton
389// had already been initialized, its previous value is restored after tearing
390// down |user_manager|.
[email protected]9a68d3a2013-04-22 16:26:54391class ScopedUserManagerEnabler {
392 public:
393 // Takes ownership of |user_manager|.
394 explicit ScopedUserManagerEnabler(UserManager* user_manager);
395 ~ScopedUserManagerEnabler();
396
397 private:
398 UserManager* previous_user_manager_;
399
400 DISALLOW_COPY_AND_ASSIGN(ScopedUserManagerEnabler);
401};
402
[email protected]1cfc9a1f2013-06-13 20:15:30403// Helper class for unit tests. Initializes the UserManager singleton on
404// construction and tears it down again on destruction.
[email protected]9a68d3a2013-04-22 16:26:54405class ScopedTestUserManager {
406 public:
407 ScopedTestUserManager();
408 ~ScopedTestUserManager();
409
410 private:
[email protected]9a68d3a2013-04-22 16:26:54411 DISALLOW_COPY_AND_ASSIGN(ScopedTestUserManager);
[email protected]bab1bceb2010-02-02 18:25:05412};
413
414} // namespace chromeos
415
[email protected]56f0ffc82010-02-04 21:30:37416#endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_