QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / components / user_manager / user_manager.h
blob105125b29c5bf8775cc2263c1c427f579bee5e2c
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_
8 #include <string>
10 #include "components/user_manager/user.h"
11 #include "components/user_manager/user_id.h"
12 #include "components/user_manager/user_manager_export.h"
13 #include "components/user_manager/user_type.h"
15 namespace base {
16 class DictionaryValue;
19 namespace chromeos {
20 class ScopedUserManagerEnabler;
23 namespace user_manager {
25 class RemoveUserDelegate;
27 // Interface for UserManagerBase - that provides base implementation for
28 // Chrome OS user management. Typical features:
29 // * Get list of all know users (who have logged into this Chrome OS device)
30 // * Keep track for logged in/LRU users, active user in multi-user session.
31 // * Find/modify users, store user meta-data such as display name/email.
32 class USER_MANAGER_EXPORT UserManager {
33 public:
34 // Interface that observers of UserManager must implement in order
35 // to receive notification when local state preferences is changed
36 class Observer {
37 public:
38 // Called when the local state preferences is changed.
39 virtual void LocalStateChanged(UserManager* user_manager);
41 protected:
42 virtual ~Observer();
45 // TODO(nkostylev): Refactor and move this observer out of UserManager.
46 // Observer interface that defines methods used to notify on user session /
47 // active user state changes. Default implementation is empty.
48 class UserSessionStateObserver {
49 public:
50 // Called when active user has changed.
51 virtual void ActiveUserChanged(const User* active_user);
53 // Called when another user got added to the existing session.
54 virtual void UserAddedToSession(const User* added_user);
56 // Called right before notifying on user change so that those who rely
57 // on user_id hash would be accessing up-to-date value.
58 virtual void ActiveUserHashChanged(const std::string& hash);
60 // Called when child status has changed.
61 virtual void UserChangedChildStatus(User* user);
63 protected:
64 virtual ~UserSessionStateObserver();
67 // Data retrieved from user account.
68 class UserAccountData {
69 public:
70 UserAccountData(const base::string16& display_name,
71 const base::string16& given_name,
72 const std::string& locale);
73 ~UserAccountData();
74 const base::string16& display_name() const { return display_name_; }
75 const base::string16& given_name() const { return given_name_; }
76 const std::string& locale() const { return locale_; }
78 private:
79 const base::string16 display_name_;
80 const base::string16 given_name_;
81 const std::string locale_;
83 DISALLOW_COPY_AND_ASSIGN(UserAccountData);
86 // Initializes UserManager instance to this. Normally should be called right
87 // after creation so that user_manager::UserManager::Get() doesn't fail.
88 // Tests could call this method if they are replacing existing UserManager
89 // instance with their own test instance.
90 void Initialize();
92 // Checks whether the UserManager instance has been created already.
93 // This method is not thread-safe and must be called from the main UI thread.
94 static bool IsInitialized();
96 // Shuts down the UserManager. After this method has been called, the
97 // singleton has unregistered itself as an observer but remains available so
98 // that other classes can access it during their shutdown. This method is not
99 // thread-safe and must be called from the main UI thread.
100 virtual void Shutdown() = 0;
102 // Sets UserManager instance to NULL. Always call Shutdown() first.
103 // This method is not thread-safe and must be called from the main UI thread.
104 void Destroy();
106 // Returns UserManager instance or will crash if it is |NULL| (has either not
107 // been created yet or is already destroyed). This method is not thread-safe
108 // and must be called from the main UI thread.
109 static UserManager* Get();
111 virtual ~UserManager();
113 // Returns a list of users who have logged into this device previously. This
114 // is sorted by last login date with the most recent user at the beginning.
115 virtual const UserList& GetUsers() const = 0;
117 // Returns list of users allowed for logging in into multi-profile session.
118 // Users that have a policy that prevents them from being added to the
119 // multi-profile session will still be part of this list as long as they
120 // are regular users (i.e. not a public session/supervised etc.).
121 // Returns an empty list in case when primary user is not a regular one or
122 // has a policy that prohibits it to be part of multi-profile session.
123 virtual UserList GetUsersAllowedForMultiProfile() const = 0;
125 // Returns a list of users who are currently logged in.
126 virtual const UserList& GetLoggedInUsers() const = 0;
128 // Returns a list of users who are currently logged in in the LRU order -
129 // so the active user is the first one in the list. If there is no user logged
130 // in, the current user will be returned.
131 virtual const UserList& GetLRULoggedInUsers() const = 0;
133 // Returns a list of users who can unlock the device.
134 // This list is based on policy and whether user is able to do unlock.
135 // Policy:
136 // * If user has primary-only policy then it is the only user in unlock users.
137 // * Otherwise all users with unrestricted policy are added to this list.
138 // All users that are unable to perform unlock are excluded from this list.
139 virtual UserList GetUnlockUsers() const = 0;
141 // Returns the email of the owner user. Returns an empty string if there is
142 // no owner for the device.
143 virtual const std::string& GetOwnerEmail() const = 0;
145 // Indicates that a user with the given |user_id| has just logged in. The
146 // persistent list is updated accordingly if the user is not ephemeral.
147 // |browser_restart| is true when reloading Chrome after crash to distinguish
148 // from normal sign in flow.
149 // |username_hash| is used to identify homedir mount point.
150 virtual void UserLoggedIn(const std::string& user_id,
151 const std::string& username_hash,
152 bool browser_restart) = 0;
154 // Switches to active user identified by |user_id|. User has to be logged in.
155 virtual void SwitchActiveUser(const std::string& user_id) = 0;
157 // Switches to the last active user (called after crash happens and session
158 // restore has completed).
159 virtual void SwitchToLastActiveUser() = 0;
161 // Called when browser session is started i.e. after
162 // browser_creator.LaunchBrowser(...) was called after user sign in.
163 // When user is at the image screen IsUserLoggedIn() will return true
164 // but IsSessionStarted() will return false. During the kiosk splash screen,
165 // we perform additional initialization after the user is logged in but
166 // before the session has been started.
167 // Fires NOTIFICATION_SESSION_STARTED.
168 virtual void SessionStarted() = 0;
170 // Removes the user from the device. Note, it will verify that the given user
171 // isn't the owner, so calling this method for the owner will take no effect.
172 // Note, |delegate| can be NULL.
173 virtual void RemoveUser(const std::string& user_id,
174 RemoveUserDelegate* delegate) = 0;
176 // Removes the user from the persistent list only. Also removes the user's
177 // picture.
178 virtual void RemoveUserFromList(const std::string& user_id) = 0;
180 // Returns true if a user with the given user id is found in the persistent
181 // list or currently logged in as ephemeral.
182 virtual bool IsKnownUser(const std::string& user_id) const = 0;
184 // Returns the user with the given user id if found in the persistent
185 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
186 virtual const User* FindUser(const std::string& user_id) const = 0;
188 // Returns the user with the given user id if found in the persistent
189 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
190 // Same as FindUser but returns non-const pointer to User object.
191 virtual User* FindUserAndModify(const std::string& user_id) = 0;
193 // Returns the logged-in user.
194 // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
195 // http://crbug.com/230852
196 virtual const User* GetLoggedInUser() const = 0;
197 virtual User* GetLoggedInUser() = 0;
199 // Returns the logged-in user that is currently active within this session.
200 // There could be multiple users logged in at the the same but for now
201 // we support only one of them being active.
202 virtual const User* GetActiveUser() const = 0;
203 virtual User* GetActiveUser() = 0;
205 // Returns the primary user of the current session. It is recorded for the
206 // first signed-in user and does not change thereafter.
207 virtual const User* GetPrimaryUser() const = 0;
209 // Saves user's oauth token status in local state preferences.
210 virtual void SaveUserOAuthStatus(
211 const std::string& user_id,
212 User::OAuthTokenStatus oauth_token_status) = 0;
214 // Saves a flag indicating whether online authentication against GAIA should
215 // be enforced during the user's next sign-in.
216 virtual void SaveForceOnlineSignin(const std::string& user_id,
217 bool force_online_signin) = 0;
219 // Saves user's displayed name in local state preferences.
220 // Ignored If there is no such user.
221 virtual void SaveUserDisplayName(const std::string& user_id,
222 const base::string16& display_name) = 0;
224 // Updates data upon User Account download.
225 virtual void UpdateUserAccountData(const std::string& user_id,
226 const UserAccountData& account_data) = 0;
228 // Returns the display name for user |user_id| if it is known (was
229 // previously set by a |SaveUserDisplayName| call).
230 // Otherwise, returns an empty string.
231 virtual base::string16 GetUserDisplayName(
232 const std::string& user_id) const = 0;
234 // Saves user's displayed (non-canonical) email in local state preferences.
235 // Ignored If there is no such user.
236 virtual void SaveUserDisplayEmail(const std::string& user_id,
237 const std::string& display_email) = 0;
239 // Returns the display email for user |user_id| if it is known (was
240 // previously set by a |SaveUserDisplayEmail| call).
241 // Otherwise, returns |user_id| itself.
242 virtual std::string GetUserDisplayEmail(const std::string& user_id) const = 0;
244 // Saves user's type for user |user_id| into local state preferences.
245 // Ignored If there is no such user.
246 virtual void SaveUserType(const std::string& user_id,
247 const UserType& user_type) = 0;
249 // Returns true if current user is an owner.
250 virtual bool IsCurrentUserOwner() const = 0;
252 // Returns true if current user is not existing one (hasn't signed in before).
253 virtual bool IsCurrentUserNew() const = 0;
255 // Returns true if data stored or cached for the current user outside that
256 // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
257 // display email) is ephemeral.
258 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
260 // Returns true if the current user's session can be locked (i.e. the user has
261 // a password with which to unlock the session).
262 virtual bool CanCurrentUserLock() const = 0;
264 // Returns true if at least one user has signed in.
265 virtual bool IsUserLoggedIn() const = 0;
267 // Returns true if we're logged in as a user with gaia account.
268 virtual bool IsLoggedInAsUserWithGaiaAccount() const = 0;
270 // Returns true if we're logged in as a child user.
271 virtual bool IsLoggedInAsChildUser() const = 0;
273 // Returns true if we're logged in as a public account.
274 virtual bool IsLoggedInAsPublicAccount() const = 0;
276 // Returns true if we're logged in as a Guest.
277 virtual bool IsLoggedInAsGuest() const = 0;
279 // Returns true if we're logged in as a legacy supervised user.
280 virtual bool IsLoggedInAsSupervisedUser() const = 0;
282 // Returns true if we're logged in as a kiosk app.
283 virtual bool IsLoggedInAsKioskApp() const = 0;
285 // Returns true if we're logged in as the stub user used for testing on Linux.
286 virtual bool IsLoggedInAsStub() const = 0;
288 // Returns true if we're logged in and browser has been started i.e.
289 // browser_creator.LaunchBrowser(...) was called after sign in
290 // or restart after crash.
291 virtual bool IsSessionStarted() const = 0;
293 // Returns true if data stored or cached for the user with the given user id
294 // address outside that user's cryptohome (wallpaper, avatar, OAuth token
295 // status, display name, display email) is to be treated as ephemeral.
296 virtual bool IsUserNonCryptohomeDataEphemeral(
297 const std::string& user_id) const = 0;
299 virtual void AddObserver(Observer* obs) = 0;
300 virtual void RemoveObserver(Observer* obs) = 0;
302 virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
303 virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
305 virtual void NotifyLocalStateChanged() = 0;
307 // Changes the child status and notifies observers.
308 virtual void ChangeUserChildStatus(User* user, bool is_child) = 0;
311 // Returns true if supervised users allowed.
312 virtual bool AreSupervisedUsersAllowed() const = 0;
314 // Methods for storage/retrieval of per-user properties in Local State.
316 // Performs a lookup of properties associated with |user_id|. If found,
317 // returns |true| and fills |out_value|. |out_value| can be NULL, if
318 // only existence check is required.
319 virtual bool FindKnownUserPrefs(const UserID& user_id,
320 const base::DictionaryValue** out_value) = 0;
322 // Updates (or creates) properties associated with |user_id| based
323 // on |values|. |clear| defines if existing properties are cleared (|true|)
324 // or if it is just a incremental update (|false|).
325 virtual void UpdateKnownUserPrefs(const UserID& user_id,
326 const base::DictionaryValue& values,
327 bool clear) = 0;
329 // Returns true if |user_id| preference by |path| does exist,
330 // fills in |out_value|. Otherwise returns false.
331 virtual bool GetKnownUserStringPref(const UserID& user_id,
332 const std::string& path,
333 std::string* out_value) = 0;
335 // Updates user's identified by |user_id| string preference |path|.
336 virtual void SetKnownUserStringPref(const UserID& user_id,
337 const std::string& path,
338 const std::string& in_value) = 0;
340 // Returns true if |user_id| preference by |path| does exist,
341 // fills in |out_value|. Otherwise returns false.
342 virtual bool GetKnownUserBooleanPref(const UserID& user_id,
343 const std::string& path,
344 bool* out_value) = 0;
346 // Updates user's identified by |user_id| boolean preference |path|.
347 virtual void SetKnownUserBooleanPref(const UserID& user_id,
348 const std::string& path,
349 const bool in_value) = 0;
351 // Returns true if |user_id| preference by |path| does exist,
352 // fills in |out_value|. Otherwise returns false.
353 virtual bool GetKnownUserIntegerPref(const UserID& user_id,
354 const std::string& path,
355 int* out_value) = 0;
357 // Updates user's identified by |user_id| integer preference |path|.
358 virtual void SetKnownUserIntegerPref(const UserID& user_id,
359 const std::string& path,
360 const int in_value) = 0;
362 // Updates |gaia_id| for user with |user_id|.
363 // TODO(antrim): Update this once UserID contains GAIA ID.
364 virtual void UpdateGaiaID(const UserID& user_id,
365 const std::string& gaia_id) = 0;
367 // Find GAIA ID for user with |user_id|, fill in |out_value| and return true
368 // if GAIA ID was found or false otherwise.
369 // TODO(antrim): Update this once UserID contains GAIA ID.
370 virtual bool FindGaiaID(const UserID& user_id, std::string* out_value) = 0;
372 // Saves whether the user authenticates using SAML.
373 virtual void UpdateUsingSAML(const UserID& user_id,
374 const bool using_saml) = 0;
376 // Returns if SAML needs to be used for authentication of the user with
377 // |user_id|, if it is known (was set by a |UpdateUsingSaml| call). Otherwise
378 // returns false.
379 virtual bool FindUsingSAML(const UserID& user_id) = 0;
381 // Setter and getter for DeviceId known user string preference.
382 virtual void SetKnownUserDeviceId(const UserID& user_id,
383 const std::string& device_id) = 0;
384 virtual std::string GetKnownUserDeviceId(const UserID& user_id) = 0;
386 // Setter and getter for GAPSCookie known user string preference.
387 virtual void SetKnownUserGAPSCookie(const UserID& user_id,
388 const std::string& gaps_cookie) = 0;
390 virtual std::string GetKnownUserGAPSCookie(const UserID& user_id) = 0;
392 // Saves why the user has to go through re-auth flow.
393 virtual void UpdateReauthReason(const UserID& user_id,
394 const int reauth_reason) = 0;
396 // Returns the reason why the user with |user_id| has to go through the
397 // re-auth flow. Returns true if such a reason was recorded or false
398 // otherwise.
399 virtual bool FindReauthReason(const UserID& user_id, int* out_value) = 0;
401 protected:
402 // Sets UserManager instance.
403 static void SetInstance(UserManager* user_manager);
405 // Pointer to the existing UserManager instance (if any).
406 // Usually is set by calling Initialize(), reset by calling Destroy().
407 // Not owned since specific implementation of UserManager should decide on its
408 // own appropriate owner. For src/chrome implementation such place is
409 // g_browser_process->platform_part().
410 static UserManager* instance;
412 private:
413 friend class chromeos::ScopedUserManagerEnabler;
415 // Same as Get() but doesn't won't crash is current instance is NULL.
416 static UserManager* GetForTesting();
418 // Sets UserManager instance to the given |user_manager|.
419 // Returns the previous value of the instance.
420 static UserManager* SetForTesting(UserManager* user_manager);
423 } // namespace user_manager
425 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_H_