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_BASE_H_
6 #define COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
13 #include "base/basictypes.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/synchronization/lock.h"
17 #include "base/time/time.h"
18 #include "components/user_manager/user.h"
19 #include "components/user_manager/user_id.h"
20 #include "components/user_manager/user_manager.h"
21 #include "components/user_manager/user_manager_export.h"
22 #include "components/user_manager/user_type.h"
25 class PrefRegistrySimple
;
28 class DictionaryValue
;
33 namespace user_manager
{
35 class RemoveUserDelegate
;
37 // Base implementation of the UserManager interface.
38 class USER_MANAGER_EXPORT UserManagerBase
: public UserManager
{
40 // Creates UserManagerBase with |task_runner| for UI thread and
41 // |blocking_task_runner| for SequencedWorkerPool.
42 UserManagerBase(scoped_refptr
<base::TaskRunner
> task_runner
,
43 scoped_refptr
<base::TaskRunner
> blocking_task_runner
);
44 ~UserManagerBase() override
;
46 // Registers UserManagerBase preferences.
47 static void RegisterPrefs(PrefRegistrySimple
* registry
);
49 // UserManager implementation:
50 void Shutdown() override
;
51 const UserList
& GetUsers() const override
;
52 const UserList
& GetLoggedInUsers() const override
;
53 const UserList
& GetLRULoggedInUsers() const override
;
54 const std::string
& GetOwnerEmail() const override
;
55 void UserLoggedIn(const std::string
& user_id
,
56 const std::string
& user_id_hash
,
57 bool browser_restart
) override
;
58 void SwitchActiveUser(const std::string
& user_id
) override
;
59 void SwitchToLastActiveUser() override
;
60 void SessionStarted() override
;
61 void RemoveUser(const std::string
& user_id
,
62 RemoveUserDelegate
* delegate
) override
;
63 void RemoveUserFromList(const std::string
& user_id
) override
;
64 bool IsKnownUser(const std::string
& user_id
) const override
;
65 const User
* FindUser(const std::string
& user_id
) const override
;
66 User
* FindUserAndModify(const std::string
& user_id
) override
;
67 const User
* GetLoggedInUser() const override
;
68 User
* GetLoggedInUser() override
;
69 const User
* GetActiveUser() const override
;
70 User
* GetActiveUser() override
;
71 const User
* GetPrimaryUser() const override
;
72 void SaveUserOAuthStatus(const std::string
& user_id
,
73 User::OAuthTokenStatus oauth_token_status
) override
;
74 void SaveForceOnlineSignin(const std::string
& user_id
,
75 bool force_online_signin
) override
;
76 void SaveUserDisplayName(const std::string
& user_id
,
77 const base::string16
& display_name
) override
;
78 base::string16
GetUserDisplayName(const std::string
& user_id
) const override
;
79 void SaveUserDisplayEmail(const std::string
& user_id
,
80 const std::string
& display_email
) override
;
81 std::string
GetUserDisplayEmail(const std::string
& user_id
) const override
;
82 void SaveUserType(const std::string
& user_id
,
83 const UserType
& user_type
) override
;
84 void UpdateUserAccountData(const std::string
& user_id
,
85 const UserAccountData
& account_data
) override
;
86 bool IsCurrentUserOwner() const override
;
87 bool IsCurrentUserNew() const override
;
88 bool IsCurrentUserNonCryptohomeDataEphemeral() const override
;
89 bool CanCurrentUserLock() const override
;
90 bool IsUserLoggedIn() const override
;
91 bool IsLoggedInAsUserWithGaiaAccount() const override
;
92 bool IsLoggedInAsChildUser() const override
;
93 bool IsLoggedInAsPublicAccount() const override
;
94 bool IsLoggedInAsGuest() const override
;
95 bool IsLoggedInAsSupervisedUser() const override
;
96 bool IsLoggedInAsKioskApp() const override
;
97 bool IsLoggedInAsStub() const override
;
98 bool IsSessionStarted() const override
;
99 bool IsUserNonCryptohomeDataEphemeral(
100 const std::string
& user_id
) const override
;
101 void AddObserver(UserManager::Observer
* obs
) override
;
102 void RemoveObserver(UserManager::Observer
* obs
) override
;
103 void AddSessionStateObserver(
104 UserManager::UserSessionStateObserver
* obs
) override
;
105 void RemoveSessionStateObserver(
106 UserManager::UserSessionStateObserver
* obs
) override
;
107 void NotifyLocalStateChanged() override
;
108 void ChangeUserChildStatus(User
* user
, bool is_child
) override
;
109 bool FindKnownUserPrefs(const UserID
& user_id
,
110 const base::DictionaryValue
** out_value
) override
;
111 void UpdateKnownUserPrefs(const UserID
& user_id
,
112 const base::DictionaryValue
& values
,
113 bool clear
) override
;
114 bool GetKnownUserStringPref(const UserID
& user_id
,
115 const std::string
& path
,
116 std::string
* out_value
) override
;
117 void SetKnownUserStringPref(const UserID
& user_id
,
118 const std::string
& path
,
119 const std::string
& in_value
) override
;
120 bool GetKnownUserBooleanPref(const UserID
& user_id
,
121 const std::string
& path
,
122 bool* out_value
) override
;
123 void SetKnownUserBooleanPref(const UserID
& user_id
,
124 const std::string
& path
,
125 const bool in_value
) override
;
126 bool GetKnownUserIntegerPref(const UserID
& user_id
,
127 const std::string
& path
,
128 int* out_value
) override
;
129 void SetKnownUserIntegerPref(const UserID
& user_id
,
130 const std::string
& path
,
131 const int in_value
) override
;
132 void UpdateGaiaID(const UserID
& user_id
, const std::string
& gaia_id
) override
;
133 bool FindGaiaID(const UserID
& user_id
, std::string
* out_value
) override
;
134 void UpdateUsingSAML(const std::string
& user_id
,
135 const bool using_saml
) override
;
136 bool FindUsingSAML(const std::string
& user_id
) override
;
137 void SetKnownUserDeviceId(const UserID
& user_id
,
138 const std::string
& device_id
) override
;
139 std::string
GetKnownUserDeviceId(const UserID
& user_id
) override
;
140 void SetKnownUserGAPSCookie(const UserID
& user_id
,
141 const std::string
& gaps_cookie
) override
;
142 std::string
GetKnownUserGAPSCookie(const UserID
& user_id
) override
;
143 void UpdateReauthReason(const std::string
& user_id
,
144 const int reauth_reason
) override
;
145 bool FindReauthReason(const std::string
& user_id
, int* out_value
) override
;
147 virtual void SetIsCurrentUserNew(bool is_new
);
149 // TODO(xiyuan): Figure out a better way to expose this info.
150 virtual bool HasPendingBootstrap(const std::string
& user_id
) const;
152 // Helper function that copies users from |users_list| to |users_vector| and
153 // |users_set|. Duplicates and users already present in |existing_users| are
155 static void ParseUserList(const base::ListValue
& users_list
,
156 const std::set
<std::string
>& existing_users
,
157 std::vector
<std::string
>* users_vector
,
158 std::set
<std::string
>* users_set
);
160 // Returns true if trusted device policies have successfully been retrieved
161 // and ephemeral users are enabled.
162 virtual bool AreEphemeralUsersEnabled() const = 0;
165 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
166 // that there is no user with same id.
167 virtual void AddUserRecord(User
* user
);
169 // Returns true if user may be removed.
170 virtual bool CanUserBeRemoved(const User
* user
) const;
172 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
173 // equals to active_user_, active_user_ is reset to NULL.
174 virtual void DeleteUser(User
* user
);
176 // Returns the locale used by the application.
177 virtual const std::string
& GetApplicationLocale() const = 0;
179 // Returns "Local State" PrefService instance.
180 virtual PrefService
* GetLocalState() const = 0;
182 // Loads |users_| from Local State if the list has not been loaded yet.
183 // Subsequent calls have no effect. Must be called on the UI thread.
184 virtual void EnsureUsersLoaded();
186 // Handle OAuth token |status| change for |user_id|.
187 virtual void HandleUserOAuthTokenStatusChange(
188 const std::string
& user_id
,
189 User::OAuthTokenStatus status
) const = 0;
191 // Returns true if device is enterprise managed.
192 virtual bool IsEnterpriseManaged() const = 0;
194 // Helper function that copies users from |users_list| to |users_vector| and
195 // |users_set|. Duplicates and users already present in |existing_users| are
197 // Loads public accounts from the Local state and fills in
198 // |public_sessions_set|.
199 virtual void LoadPublicAccounts(
200 std::set
<std::string
>* public_sessions_set
) = 0;
202 // Notifies that user has logged in.
203 virtual void NotifyOnLogin();
205 // Notifies observers that another user was added to the session.
206 // If |user_switch_pending| is true this means that user has not been fully
207 // initialized yet like waiting for profile to be loaded.
208 virtual void NotifyUserAddedToSession(const User
* added_user
,
209 bool user_switch_pending
);
211 // Performs any additional actions before user list is loaded.
212 virtual void PerformPreUserListLoadingActions() = 0;
214 // Performs any additional actions after user list is loaded.
215 virtual void PerformPostUserListLoadingActions() = 0;
217 // Performs any additional actions after UserLoggedIn() execution has been
219 // |browser_restart| is true when reloading Chrome after crash to distinguish
220 // from normal sign in flow.
221 virtual void PerformPostUserLoggedInActions(bool browser_restart
) = 0;
223 // Implementation for RemoveUser method. It is synchronous. It is called from
224 // RemoveUserInternal after owner check.
225 virtual void RemoveNonOwnerUserInternal(const std::string
& user_email
,
226 RemoveUserDelegate
* delegate
);
228 // Removes a regular or supervised user from the user list.
229 // Returns the user if found or NULL otherwise.
230 // Also removes the user from the persistent user list.
231 User
* RemoveRegularOrSupervisedUserFromList(const std::string
& user_id
);
233 // Implementation for RemoveUser method. This is an asynchronous part of the
234 // method, that verifies that owner will not get deleted, and calls
235 // |RemoveNonOwnerUserInternal|.
236 virtual void RemoveUserInternal(const std::string
& user_email
,
237 RemoveUserDelegate
* delegate
);
239 // Removes data stored or cached outside the user's cryptohome (wallpaper,
240 // avatar, OAuth token status, display name, display email).
241 virtual void RemoveNonCryptohomeData(const std::string
& user_id
);
243 // Check for a particular user type.
245 // Returns true if |user_id| represents demo app.
246 virtual bool IsDemoApp(const std::string
& user_id
) const = 0;
248 // Returns true if |user_id| represents kiosk app.
249 virtual bool IsKioskApp(const std::string
& user_id
) const = 0;
251 // Returns true if |user_id| represents public account that has been marked
253 virtual bool IsPublicAccountMarkedForRemoval(
254 const std::string
& user_id
) const = 0;
256 // These methods are called when corresponding user type has signed in.
258 // Indicates that the demo account has just logged in.
259 virtual void DemoAccountLoggedIn() = 0;
261 // Indicates that a user just logged in as guest.
262 virtual void GuestUserLoggedIn();
264 // Indicates that a kiosk app robot just logged in.
265 virtual void KioskAppLoggedIn(const std::string
& app_id
) = 0;
267 // Indicates that a user just logged into a public session.
268 virtual void PublicAccountUserLoggedIn(User
* user
) = 0;
270 // Indicates that a regular user just logged in.
271 virtual void RegularUserLoggedIn(const std::string
& user_id
);
273 // Indicates that a regular user just logged in as ephemeral.
274 virtual void RegularUserLoggedInAsEphemeral(const std::string
& user_id
);
276 // Indicates that a supervised user just logged in.
277 virtual void SupervisedUserLoggedIn(const std::string
& user_id
) = 0;
279 // Should be called when regular user was removed.
280 virtual void OnUserRemoved(const std::string
& user_id
) = 0;
282 // Getters/setters for private members.
284 virtual void SetCurrentUserIsOwner(bool is_current_user_owner
);
286 virtual bool GetEphemeralUsersEnabled() const;
287 virtual void SetEphemeralUsersEnabled(bool enabled
);
289 virtual void SetOwnerEmail(std::string owner_user_id
);
291 virtual const std::string
& GetPendingUserSwitchID() const;
292 virtual void SetPendingUserSwitchID(std::string user_id
);
294 // The logged-in user that is currently active in current session.
295 // NULL until a user has logged in, then points to one
296 // of the User instances in |users_|, the |guest_user_| instance or an
297 // ephemeral user instance.
300 // The primary user of the current session. It is recorded for the first
301 // signed-in user and does not change thereafter.
304 // List of all known users. User instances are owned by |this|. Regular users
305 // are removed by |RemoveUserFromList|, public accounts by
306 // |UpdateAndCleanUpPublicAccounts|.
309 // List of all users that are logged in current session. These point to User
310 // instances in |users_|. Only one of them could be marked as active.
311 UserList logged_in_users_
;
313 // A list of all users that are logged in the current session. In contrast to
314 // |logged_in_users|, the order of this list is least recently used so that
315 // the active user should always be the first one in the list.
316 UserList lru_logged_in_users_
;
319 // Stages of loading user list from preferences. Some methods can have
320 // different behavior depending on stage.
321 enum UserLoadStage
{ STAGE_NOT_LOADED
= 0, STAGE_LOADING
, STAGE_LOADED
};
323 // Returns a list of users who have logged into this device previously.
324 // Same as GetUsers but used if you need to modify User from that list.
325 UserList
& GetUsersAndModify();
327 // Returns the user with the given email address if found in the persistent
328 // list. Returns |NULL| otherwise.
329 const User
* FindUserInList(const std::string
& user_id
) const;
331 // Returns |true| if user with the given id is found in the persistent list.
332 // Returns |false| otherwise. Does not trigger user loading.
333 bool UserExistsInList(const std::string
& user_id
) const;
335 // Same as FindUserInList but returns non-const pointer to User object.
336 User
* FindUserInListAndModify(const std::string
& user_id
);
338 // Reads user's oauth token status from local state preferences.
339 User::OAuthTokenStatus
LoadUserOAuthStatus(const std::string
& user_id
) const;
341 // Read a flag indicating whether online authentication against GAIA should
342 // be enforced during the user's next sign-in from local state preferences.
343 bool LoadForceOnlineSignin(const std::string
& user_id
) const;
345 // Notifies observers that merge session state had changed.
346 void NotifyMergeSessionStateChanged();
348 // Notifies observers that active user has changed.
349 void NotifyActiveUserChanged(const User
* active_user
);
351 // Notifies observers that active user_id hash has changed.
352 void NotifyActiveUserHashChanged(const std::string
& hash
);
354 // Update the global LoginState.
355 void UpdateLoginState();
357 // Insert |user| at the front of the LRU user list.
358 void SetLRUUser(User
* user
);
360 // Sends metrics in response to a user with gaia account (regular) logging in.
361 void SendGaiaUserLoginMetrics(const std::string
& user_id
);
363 // Sets account locale for user with id |user_id|.
364 virtual void UpdateUserAccountLocale(const std::string
& user_id
,
365 const std::string
& locale
);
367 // Updates user account after locale was resolved.
368 void DoUpdateAccountLocale(const std::string
& user_id
,
369 scoped_ptr
<std::string
> resolved_locale
);
371 // Removes all user preferences associated with |user_id|.
372 void RemoveKnownUserPrefs(const UserID
& user_id
);
374 // Indicates stage of loading user from prefs.
375 UserLoadStage user_loading_stage_
;
377 // True if SessionStarted() has been called.
378 bool session_started_
;
380 // Cached flag of whether currently logged-in user is owner or not.
381 // May be accessed on different threads, requires locking.
382 bool is_current_user_owner_
;
383 mutable base::Lock is_current_user_owner_lock_
;
385 // Cached flag of whether the currently logged-in user existed before this
387 bool is_current_user_new_
;
389 // Cached flag of whether the currently logged-in user is a regular user who
390 // logged in as ephemeral. Storage of persistent information is avoided for
391 // such users by not adding them to the persistent user list, not downloading
392 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
394 bool is_current_user_ephemeral_regular_user_
;
396 // Cached flag indicating whether the ephemeral user policy is enabled.
397 // Defaults to |false| if the value has not been read from trusted device
399 bool ephemeral_users_enabled_
;
401 // Cached name of device owner. Defaults to empty string if the value has not
402 // been read from trusted device policy yet.
403 std::string owner_email_
;
405 base::ObserverList
<UserManager::Observer
> observer_list_
;
407 // TODO(nkostylev): Merge with session state refactoring CL.
408 base::ObserverList
<UserManager::UserSessionStateObserver
>
409 session_state_observer_list_
;
411 // Time at which this object was created.
412 base::TimeTicks manager_creation_time_
;
414 // ID of the user just added to the session that needs to be activated
415 // as soon as user's profile is loaded.
416 std::string pending_user_switch_
;
418 // ID of the user that was active in the previous session.
419 // Preference value is stored here before first user signs in
420 // because pref will be overidden once session restore starts.
421 std::string last_session_active_user_
;
422 bool last_session_active_user_initialized_
;
424 // TaskRunner for UI thread.
425 scoped_refptr
<base::TaskRunner
> task_runner_
;
427 // TaskRunner for SequencedWorkerPool.
428 scoped_refptr
<base::TaskRunner
> blocking_task_runner_
;
430 base::WeakPtrFactory
<UserManagerBase
> weak_factory_
;
432 DISALLOW_COPY_AND_ASSIGN(UserManagerBase
);
435 } // namespace user_manager
437 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_