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 // Getters/setters for private members.
281 virtual void SetCurrentUserIsOwner(bool is_current_user_owner
);
283 virtual bool GetEphemeralUsersEnabled() const;
284 virtual void SetEphemeralUsersEnabled(bool enabled
);
286 virtual void SetOwnerEmail(std::string owner_user_id
);
288 virtual const std::string
& GetPendingUserSwitchID() const;
289 virtual void SetPendingUserSwitchID(std::string user_id
);
291 // The logged-in user that is currently active in current session.
292 // NULL until a user has logged in, then points to one
293 // of the User instances in |users_|, the |guest_user_| instance or an
294 // ephemeral user instance.
297 // The primary user of the current session. It is recorded for the first
298 // signed-in user and does not change thereafter.
301 // List of all known users. User instances are owned by |this|. Regular users
302 // are removed by |RemoveUserFromList|, public accounts by
303 // |UpdateAndCleanUpPublicAccounts|.
306 // List of all users that are logged in current session. These point to User
307 // instances in |users_|. Only one of them could be marked as active.
308 UserList logged_in_users_
;
310 // A list of all users that are logged in the current session. In contrast to
311 // |logged_in_users|, the order of this list is least recently used so that
312 // the active user should always be the first one in the list.
313 UserList lru_logged_in_users_
;
316 // Stages of loading user list from preferences. Some methods can have
317 // different behavior depending on stage.
318 enum UserLoadStage
{ STAGE_NOT_LOADED
= 0, STAGE_LOADING
, STAGE_LOADED
};
320 // Returns a list of users who have logged into this device previously.
321 // Same as GetUsers but used if you need to modify User from that list.
322 UserList
& GetUsersAndModify();
324 // Returns the user with the given email address if found in the persistent
325 // list. Returns |NULL| otherwise.
326 const User
* FindUserInList(const std::string
& user_id
) const;
328 // Returns |true| if user with the given id is found in the persistent list.
329 // Returns |false| otherwise. Does not trigger user loading.
330 bool UserExistsInList(const std::string
& user_id
) const;
332 // Same as FindUserInList but returns non-const pointer to User object.
333 User
* FindUserInListAndModify(const std::string
& user_id
);
335 // Reads user's oauth token status from local state preferences.
336 User::OAuthTokenStatus
LoadUserOAuthStatus(const std::string
& user_id
) const;
338 // Read a flag indicating whether online authentication against GAIA should
339 // be enforced during the user's next sign-in from local state preferences.
340 bool LoadForceOnlineSignin(const std::string
& user_id
) const;
342 // Notifies observers that merge session state had changed.
343 void NotifyMergeSessionStateChanged();
345 // Notifies observers that active user has changed.
346 void NotifyActiveUserChanged(const User
* active_user
);
348 // Notifies observers that active user_id hash has changed.
349 void NotifyActiveUserHashChanged(const std::string
& hash
);
351 // Update the global LoginState.
352 void UpdateLoginState();
354 // Insert |user| at the front of the LRU user list.
355 void SetLRUUser(User
* user
);
357 // Sends metrics in response to a user with gaia account (regular) logging in.
358 void SendGaiaUserLoginMetrics(const std::string
& user_id
);
360 // Sets account locale for user with id |user_id|.
361 virtual void UpdateUserAccountLocale(const std::string
& user_id
,
362 const std::string
& locale
);
364 // Updates user account after locale was resolved.
365 void DoUpdateAccountLocale(const std::string
& user_id
,
366 scoped_ptr
<std::string
> resolved_locale
);
368 // Removes all user preferences associated with |user_id|.
369 void RemoveKnownUserPrefs(const UserID
& user_id
);
371 // Indicates stage of loading user from prefs.
372 UserLoadStage user_loading_stage_
;
374 // True if SessionStarted() has been called.
375 bool session_started_
;
377 // Cached flag of whether currently logged-in user is owner or not.
378 // May be accessed on different threads, requires locking.
379 bool is_current_user_owner_
;
380 mutable base::Lock is_current_user_owner_lock_
;
382 // Cached flag of whether the currently logged-in user existed before this
384 bool is_current_user_new_
;
386 // Cached flag of whether the currently logged-in user is a regular user who
387 // logged in as ephemeral. Storage of persistent information is avoided for
388 // such users by not adding them to the persistent user list, not downloading
389 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
391 bool is_current_user_ephemeral_regular_user_
;
393 // Cached flag indicating whether the ephemeral user policy is enabled.
394 // Defaults to |false| if the value has not been read from trusted device
396 bool ephemeral_users_enabled_
;
398 // Cached name of device owner. Defaults to empty string if the value has not
399 // been read from trusted device policy yet.
400 std::string owner_email_
;
402 base::ObserverList
<UserManager::Observer
> observer_list_
;
404 // TODO(nkostylev): Merge with session state refactoring CL.
405 base::ObserverList
<UserManager::UserSessionStateObserver
>
406 session_state_observer_list_
;
408 // Time at which this object was created.
409 base::TimeTicks manager_creation_time_
;
411 // ID of the user just added to the session that needs to be activated
412 // as soon as user's profile is loaded.
413 std::string pending_user_switch_
;
415 // ID of the user that was active in the previous session.
416 // Preference value is stored here before first user signs in
417 // because pref will be overidden once session restore starts.
418 std::string last_session_active_user_
;
419 bool last_session_active_user_initialized_
;
421 // TaskRunner for UI thread.
422 scoped_refptr
<base::TaskRunner
> task_runner_
;
424 // TaskRunner for SequencedWorkerPool.
425 scoped_refptr
<base::TaskRunner
> blocking_task_runner_
;
427 base::WeakPtrFactory
<UserManagerBase
> weak_factory_
;
429 DISALLOW_COPY_AND_ASSIGN(UserManagerBase
);
432 } // namespace user_manager
434 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_