Choose right profile to load without switching active user
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user_manager.h
blobbcaaee9b9c3460af905321d064d4c280509a91df
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_
8 #include <string>
10 #include "chrome/browser/chromeos/login/user.h"
11 #include "chrome/browser/chromeos/login/user_flow.h"
13 class PrefRegistrySimple;
15 namespace chromeos {
17 class RemoveUserDelegate;
18 class UserImageManager;
20 // Base class for UserManagerImpl - provides a mechanism for discovering users
21 // who have logged into this Chrome OS device before and updating that list.
22 class UserManager {
23 public:
24 // Interface that observers of UserManager must implement in order
25 // to receive notification when local state preferences is changed
26 class Observer {
27 public:
28 // Called when the local state preferences is changed.
29 virtual void LocalStateChanged(UserManager* user_manager);
31 protected:
32 virtual ~Observer();
35 // 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.
38 class UserSessionStateObserver {
39 public:
40 // Called when active user has changed.
41 virtual void ActiveUserChanged(const User* active_user);
43 // Called right before notifying on user change so that those who rely
44 // on user_id hash would be accessing up-to-date value.
45 virtual void ActiveUserHashChanged(const std::string& hash);
47 // Called when UserManager finishes restoring user sessions after crash.
48 virtual void PendingUserSessionsRestoreFinished();
50 protected:
51 virtual ~UserSessionStateObserver();
54 // Username for stub login when not running on ChromeOS.
55 static const char kStubUser[];
57 // 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[];
64 // Domain that is used for all locally managed users.
65 static const char kLocallyManagedUserDomain[];
67 // The retail mode user has a magic, domainless e-mail address.
68 static const char kRetailModeUserName[];
70 // Creates the singleton instance. This method is not thread-safe and must be
71 // called from the main UI thread.
72 static void Initialize();
74 // 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();
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;
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();
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();
93 // Registers user manager preferences.
94 static void RegisterPrefs(PrefRegistrySimple* registry);
96 virtual ~UserManager();
98 virtual UserImageManager* GetUserImageManager() = 0;
100 // Returns a list of users who have logged into this device previously. This
101 // is sorted by last login date with the most recent user at the beginning.
102 virtual const UserList& GetUsers() const = 0;
104 // Returns list of users admitted for logging in into multiprofile session.
105 virtual UserList GetUsersAdmittedForMultiProfile() const = 0;
107 // Returns a list of users who are currently logged in.
108 virtual const UserList& GetLoggedInUsers() const = 0;
110 // Returns a list of users who are currently logged in in the LRU order -
111 // so the active user is the first one in the list. If there is no user logged
112 // in, the current user will be returned.
113 virtual const UserList& GetLRULoggedInUsers() = 0;
115 // Returns the email of the owner user. Returns an empty string if there is
116 // no owner for the device.
117 virtual const std::string& GetOwnerEmail() = 0;
119 // Indicates that a user with the given |email| has just logged in. The
120 // persistent list is updated accordingly if the user is not ephemeral.
121 // |browser_restart| is true when reloading Chrome after crash to distinguish
122 // from normal sign in flow.
123 // |username_hash| is used to identify homedir mount point.
124 virtual void UserLoggedIn(const std::string& email,
125 const std::string& username_hash,
126 bool browser_restart) = 0;
128 // Switches to active user identified by |email|. User has to be logged in.
129 virtual void SwitchActiveUser(const std::string& email) = 0;
131 // Called when browser session is started i.e. after
132 // browser_creator.LaunchBrowser(...) was called after user sign in.
133 // When user is at the image screen IsUserLoggedIn() will return true
134 // but IsSessionStarted() will return false. During the kiosk splash screen,
135 // we perform additional initialization after the user is logged in but
136 // before the session has been started.
137 // Fires NOTIFICATION_SESSION_STARTED.
138 virtual void SessionStarted() = 0;
140 // Usually is called when Chrome is restarted after a crash and there's an
141 // active session. First user (one that is passed with --login-user) Chrome
142 // session has been already restored at this point. This method asks session
143 // manager for all active user sessions, marks them as logged in
144 // and notifies observers.
145 virtual void RestoreActiveSessions() = 0;
147 // Creates locally managed user with given |display_name| and|local_user_id|
148 // and persists that to user list. Also links this user identified by
149 // |sync_user_id| to manager with a |manager_id|.
150 // Returns created user, or existing user if there already
151 // was locally managed user with such display name.
152 // TODO(antrim): Refactor into a single struct to have only 1 getter.
153 virtual const User* CreateLocallyManagedUserRecord(
154 const std::string& manager_id,
155 const std::string& local_user_id,
156 const std::string& sync_user_id,
157 const string16& display_name) = 0;
159 // Generates unique username for locally managed user.
160 virtual std::string GenerateUniqueLocallyManagedUserId() = 0;
162 // Removes the user from the device. Note, it will verify that the given user
163 // isn't the owner, so calling this method for the owner will take no effect.
164 // Note, |delegate| can be NULL.
165 virtual void RemoveUser(const std::string& email,
166 RemoveUserDelegate* delegate) = 0;
168 // Removes the user from the persistent list only. Also removes the user's
169 // picture.
170 virtual void RemoveUserFromList(const std::string& email) = 0;
172 // Returns true if a user with the given email address is found in the
173 // persistent list or currently logged in as ephemeral.
174 virtual bool IsKnownUser(const std::string& email) const = 0;
176 // Returns the user with the given email address if found in the persistent
177 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
178 virtual const User* FindUser(const std::string& email) const = 0;
180 // Returns the locally managed user with the given |display_name| if found in
181 // the persistent list. Returns |NULL| otherwise.
182 virtual const User* FindLocallyManagedUser(
183 const string16& display_name) const = 0;
185 // Returns the logged-in user.
186 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
187 // http://crbug.com/230852
188 virtual const User* GetLoggedInUser() const = 0;
189 virtual User* GetLoggedInUser() = 0;
191 // Returns the logged-in user that is currently active within this session.
192 // There could be multiple users logged in at the the same but for now
193 // we support only one of them being active.
194 virtual const User* GetActiveUser() const = 0;
195 virtual User* GetActiveUser() = 0;
197 // Saves user's oauth token status in local state preferences.
198 virtual void SaveUserOAuthStatus(
199 const std::string& username,
200 User::OAuthTokenStatus oauth_token_status) = 0;
202 // Saves user's displayed name in local state preferences.
203 // Ignored If there is no such user.
204 virtual void SaveUserDisplayName(const std::string& username,
205 const string16& display_name) = 0;
207 // Returns the display name for user |username| if it is known (was
208 // previously set by a |SaveUserDisplayName| call).
209 // Otherwise, returns an empty string.
210 virtual string16 GetUserDisplayName(
211 const std::string& username) const = 0;
213 // Saves user's displayed (non-canonical) email in local state preferences.
214 // Ignored If there is no such user.
215 virtual void SaveUserDisplayEmail(const std::string& username,
216 const std::string& display_email) = 0;
218 // Returns the display email for user |username| if it is known (was
219 // previously set by a |SaveUserDisplayEmail| call).
220 // Otherwise, returns |username| itself.
221 virtual std::string GetUserDisplayEmail(
222 const std::string& username) const = 0;
224 // Returns sync_user_id for locally managed user with |managed_user_id| or
225 // empty string if such user is not found or it doesn't have
226 // sync_user_id defined.
227 virtual std::string GetManagedUserSyncId(
228 const std::string& managed_user_id) const = 0;
230 // Returns the display name for manager of user |managed_user_id| if it is
231 // known (was previously set by a |SaveUserDisplayName| call).
232 // Otherwise, returns a manager id.
233 virtual string16 GetManagerDisplayNameForManagedUser(
234 const std::string& managed_user_id) const = 0;
236 // Returns the user id for manager of user |managed_user_id| if it is known
237 // (user is actually a managed user).
238 // Otherwise, returns an empty string.
239 virtual std::string GetManagerUserIdForManagedUser(
240 const std::string& managed_user_id) const = 0;
242 // Returns the display email for manager of user |managed_user_id| if it is
243 // known (user is actually a managed user).
244 // Otherwise, returns an empty string.
245 virtual std::string GetManagerDisplayEmailForManagedUser(
246 const std::string& managed_user_id) const = 0;
248 // Returns true if current user is an owner.
249 virtual bool IsCurrentUserOwner() const = 0;
251 // Returns true if current user is not existing one (hasn't signed in before).
252 virtual bool IsCurrentUserNew() const = 0;
254 // Returns true if data stored or cached for the current user outside that
255 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
256 // display email) is ephemeral.
257 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
259 // Returns true if the current user's session can be locked (i.e. the user has
260 // a password with which to unlock the session).
261 virtual bool CanCurrentUserLock() const = 0;
263 // Returns true if at least one user has signed in.
264 virtual bool IsUserLoggedIn() const = 0;
266 // Returns true if we're logged in as a regular user.
267 virtual bool IsLoggedInAsRegularUser() const = 0;
269 // Returns true if we're logged in as a demo user.
270 virtual bool IsLoggedInAsDemoUser() const = 0;
272 // Returns true if we're logged in as a public account.
273 virtual bool IsLoggedInAsPublicAccount() const = 0;
275 // Returns true if we're logged in as a Guest.
276 virtual bool IsLoggedInAsGuest() const = 0;
278 // Returns true if we're logged in as a locally managed user.
279 virtual bool IsLoggedInAsLocallyManagedUser() const = 0;
281 // Returns true if we're logged in as a kiosk app.
282 virtual bool IsLoggedInAsKioskApp() const = 0;
284 // Returns true if we're logged in as the stub user used for testing on Linux.
285 virtual bool IsLoggedInAsStub() const = 0;
287 // Returns true if we're logged in and browser has been started i.e.
288 // browser_creator.LaunchBrowser(...) was called after sign in
289 // or restart after crash.
290 virtual bool IsSessionStarted() const = 0;
292 // Returns true iff browser has been restarted after crash and UserManager
293 // finished restoring user sessions.
294 virtual bool UserSessionsRestored() const = 0;
296 // Returns true when the browser has crashed and restarted during the current
297 // user's session.
298 virtual bool HasBrowserRestarted() const = 0;
300 // Returns true if data stored or cached for the user with the given email
301 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
302 // status, display name, display email) is to be treated as ephemeral.
303 virtual bool IsUserNonCryptohomeDataEphemeral(
304 const std::string& email) const = 0;
306 // Create a record about starting locally managed user creation transaction.
307 virtual void StartLocallyManagedUserCreationTransaction(
308 const string16& display_name) = 0;
310 // Add user id to locally managed user creation transaction record.
311 virtual void SetLocallyManagedUserCreationTransactionUserId(
312 const std::string& email) = 0;
314 // Remove locally managed user creation transaction record.
315 virtual void CommitLocallyManagedUserCreationTransaction() = 0;
317 // Method that allows to set |flow| for user identified by |email|.
318 // Flow should be set before login attempt.
319 // Takes ownership of the |flow|, |flow| will be deleted in case of login
320 // failure.
321 virtual void SetUserFlow(const std::string& email, UserFlow* flow) = 0;
323 // Return user flow for current user. Returns instance of DefaultUserFlow if
324 // no flow was defined for current user, or user is not logged in.
325 // Returned value should not be cached.
326 virtual UserFlow* GetCurrentUserFlow() const = 0;
328 // Return user flow for user identified by |email|. Returns instance of
329 // DefaultUserFlow if no flow was defined for user.
330 // Returned value should not be cached.
331 virtual UserFlow* GetUserFlow(const std::string& email) const = 0;
333 // Resets user flow for user identified by |email|.
334 virtual void ResetUserFlow(const std::string& email) = 0;
336 // Gets/sets chrome oauth client id and secret for kiosk app mode. The default
337 // values can be overridden with kiosk auth file.
338 virtual bool GetAppModeChromeClientOAuthInfo(
339 std::string* chrome_client_id,
340 std::string* chrome_client_secret) = 0;
341 virtual void SetAppModeChromeClientOAuthInfo(
342 const std::string& chrome_client_id,
343 const std::string& chrome_client_secret) = 0;
345 virtual void AddObserver(Observer* obs) = 0;
346 virtual void RemoveObserver(Observer* obs) = 0;
348 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
349 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
351 virtual void NotifyLocalStateChanged() = 0;
353 // Returns true if locally managed users allowed.
354 virtual bool AreLocallyManagedUsersAllowed() const = 0;
356 // Returns profile dir for the user identified by |email|.
357 virtual base::FilePath GetUserProfileDir(const std::string& email) const = 0;
359 private:
360 friend class ScopedUserManagerEnabler;
362 // Sets the singleton to the given |user_manager|, taking ownership. Returns
363 // the previous value of the singleton, passing ownership.
364 static UserManager* SetForTesting(UserManager* user_manager);
367 // Helper class for unit tests. Initializes the UserManager singleton to the
368 // given |user_manager| and tears it down again on destruction. If the singleton
369 // had already been initialized, its previous value is restored after tearing
370 // down |user_manager|.
371 class ScopedUserManagerEnabler {
372 public:
373 // Takes ownership of |user_manager|.
374 explicit ScopedUserManagerEnabler(UserManager* user_manager);
375 ~ScopedUserManagerEnabler();
377 private:
378 UserManager* previous_user_manager_;
380 DISALLOW_COPY_AND_ASSIGN(ScopedUserManagerEnabler);
383 // Helper class for unit tests. Initializes the UserManager singleton on
384 // construction and tears it down again on destruction.
385 class ScopedTestUserManager {
386 public:
387 ScopedTestUserManager();
388 ~ScopedTestUserManager();
390 private:
391 DISALLOW_COPY_AND_ASSIGN(ScopedTestUserManager);
394 } // namespace chromeos
396 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_