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_
12 #include "base/basictypes.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/synchronization/lock.h"
16 #include "base/time/time.h"
17 #include "components/user_manager/user.h"
18 #include "components/user_manager/user_manager.h"
19 #include "components/user_manager/user_manager_export.h"
22 class PrefRegistrySimple
;
29 namespace user_manager
{
31 class RemoveUserDelegate
;
33 // Base implementation of the UserManager interface.
34 class USER_MANAGER_EXPORT UserManagerBase
: public UserManager
{
36 // Creates UserManagerBase with |task_runner| for UI thread and
37 // |blocking_task_runner| for SequencedWorkerPool.
38 UserManagerBase(scoped_refptr
<base::TaskRunner
> task_runner
,
39 scoped_refptr
<base::TaskRunner
> blocking_task_runner
);
40 virtual ~UserManagerBase();
42 // Registers UserManagerBase preferences.
43 static void RegisterPrefs(PrefRegistrySimple
* registry
);
45 // UserManager implementation:
46 virtual void Shutdown() OVERRIDE
;
47 virtual const UserList
& GetUsers() const OVERRIDE
;
48 virtual const UserList
& GetLoggedInUsers() const OVERRIDE
;
49 virtual const UserList
& GetLRULoggedInUsers() const OVERRIDE
;
50 virtual const std::string
& GetOwnerEmail() const OVERRIDE
;
51 virtual void UserLoggedIn(const std::string
& user_id
,
52 const std::string
& user_id_hash
,
53 bool browser_restart
) OVERRIDE
;
54 virtual void SwitchActiveUser(const std::string
& user_id
) OVERRIDE
;
55 virtual void SessionStarted() OVERRIDE
;
56 virtual void RemoveUser(const std::string
& user_id
,
57 RemoveUserDelegate
* delegate
) OVERRIDE
;
58 virtual void RemoveUserFromList(const std::string
& user_id
) OVERRIDE
;
59 virtual bool IsKnownUser(const std::string
& user_id
) const OVERRIDE
;
60 virtual const User
* FindUser(const std::string
& user_id
) const OVERRIDE
;
61 virtual User
* FindUserAndModify(const std::string
& user_id
) OVERRIDE
;
62 virtual const User
* GetLoggedInUser() const OVERRIDE
;
63 virtual User
* GetLoggedInUser() OVERRIDE
;
64 virtual const User
* GetActiveUser() const OVERRIDE
;
65 virtual User
* GetActiveUser() OVERRIDE
;
66 virtual const User
* GetPrimaryUser() const OVERRIDE
;
67 virtual void SaveUserOAuthStatus(
68 const std::string
& user_id
,
69 User::OAuthTokenStatus oauth_token_status
) OVERRIDE
;
70 virtual void SaveForceOnlineSignin(const std::string
& user_id
,
71 bool force_online_signin
) OVERRIDE
;
72 virtual void SaveUserDisplayName(const std::string
& user_id
,
73 const base::string16
& display_name
) OVERRIDE
;
74 virtual base::string16
GetUserDisplayName(
75 const std::string
& user_id
) const OVERRIDE
;
76 virtual void SaveUserDisplayEmail(const std::string
& user_id
,
77 const std::string
& display_email
) OVERRIDE
;
78 virtual std::string
GetUserDisplayEmail(
79 const std::string
& user_id
) const OVERRIDE
;
80 virtual void UpdateUserAccountData(
81 const std::string
& user_id
,
82 const UserAccountData
& account_data
) OVERRIDE
;
83 virtual bool IsCurrentUserOwner() const OVERRIDE
;
84 virtual bool IsCurrentUserNew() const OVERRIDE
;
85 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const OVERRIDE
;
86 virtual bool CanCurrentUserLock() const OVERRIDE
;
87 virtual bool IsUserLoggedIn() const OVERRIDE
;
88 virtual bool IsLoggedInAsRegularUser() const OVERRIDE
;
89 virtual bool IsLoggedInAsDemoUser() const OVERRIDE
;
90 virtual bool IsLoggedInAsPublicAccount() const OVERRIDE
;
91 virtual bool IsLoggedInAsGuest() const OVERRIDE
;
92 virtual bool IsLoggedInAsSupervisedUser() const OVERRIDE
;
93 virtual bool IsLoggedInAsKioskApp() const OVERRIDE
;
94 virtual bool IsLoggedInAsStub() const OVERRIDE
;
95 virtual bool IsSessionStarted() const OVERRIDE
;
96 virtual bool IsUserNonCryptohomeDataEphemeral(
97 const std::string
& user_id
) const OVERRIDE
;
98 virtual void AddObserver(UserManager::Observer
* obs
) OVERRIDE
;
99 virtual void RemoveObserver(UserManager::Observer
* obs
) OVERRIDE
;
100 virtual void AddSessionStateObserver(
101 UserManager::UserSessionStateObserver
* obs
) OVERRIDE
;
102 virtual void RemoveSessionStateObserver(
103 UserManager::UserSessionStateObserver
* obs
) OVERRIDE
;
104 virtual void NotifyLocalStateChanged() OVERRIDE
;
106 // Helper function that copies users from |users_list| to |users_vector| and
107 // |users_set|. Duplicates and users already present in |existing_users| are
109 static void ParseUserList(const base::ListValue
& users_list
,
110 const std::set
<std::string
>& existing_users
,
111 std::vector
<std::string
>* users_vector
,
112 std::set
<std::string
>* users_set
);
115 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
116 // that there is no user with same id.
117 virtual void AddUserRecord(User
* user
);
119 // Returns true if trusted device policies have successfully been retrieved
120 // and ephemeral users are enabled.
121 virtual bool AreEphemeralUsersEnabled() const = 0;
123 // Returns true if user may be removed.
124 virtual bool CanUserBeRemoved(const User
* user
) const;
126 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
127 // equals to active_user_, active_user_ is reset to NULL.
128 virtual void DeleteUser(User
* user
);
130 // Returns the locale used by the application.
131 virtual const std::string
& GetApplicationLocale() const = 0;
133 // Returns "Local State" PrefService instance.
134 virtual PrefService
* GetLocalState() const = 0;
136 // Loads |users_| from Local State if the list has not been loaded yet.
137 // Subsequent calls have no effect. Must be called on the UI thread.
138 void EnsureUsersLoaded();
140 // Handle OAuth token |status| change for |user_id|.
141 virtual void HandleUserOAuthTokenStatusChange(
142 const std::string
& user_id
,
143 User::OAuthTokenStatus status
) const = 0;
145 // Returns true if device is enterprise managed.
146 virtual bool IsEnterpriseManaged() const = 0;
148 // Helper function that copies users from |users_list| to |users_vector| and
149 // |users_set|. Duplicates and users already present in |existing_users| are
151 // Loads public accounts from the Local state and fills in
152 // |public_sessions_set|.
153 virtual void LoadPublicAccounts(
154 std::set
<std::string
>* public_sessions_set
) = 0;
156 // Notifies that user has logged in.
157 virtual void NotifyOnLogin();
159 // Notifies observers that another user was added to the session.
160 // If |user_switch_pending| is true this means that user has not been fully
161 // initialized yet like waiting for profile to be loaded.
162 virtual void NotifyUserAddedToSession(const User
* added_user
,
163 bool user_switch_pending
);
165 // Performs any additional actions before user list is loaded.
166 virtual void PerformPreUserListLoadingActions() = 0;
168 // Performs any additional actions after user list is loaded.
169 virtual void PerformPostUserListLoadingActions() = 0;
171 // Performs any additional actions after UserLoggedIn() execution has been
173 // |browser_restart| is true when reloading Chrome after crash to distinguish
174 // from normal sign in flow.
175 virtual void PerformPostUserLoggedInActions(bool browser_restart
) = 0;
177 // Implementation for RemoveUser method. It is synchronous. It is called from
178 // RemoveUserInternal after owner check.
179 virtual void RemoveNonOwnerUserInternal(const std::string
& user_email
,
180 RemoveUserDelegate
* delegate
);
182 // Removes a regular or supervised user from the user list.
183 // Returns the user if found or NULL otherwise.
184 // Also removes the user from the persistent user list.
185 User
* RemoveRegularOrSupervisedUserFromList(const std::string
& user_id
);
187 // Implementation for RemoveUser method. This is an asynchronous part of the
188 // method, that verifies that owner will not get deleted, and calls
189 // |RemoveNonOwnerUserInternal|.
190 virtual void RemoveUserInternal(const std::string
& user_email
,
191 RemoveUserDelegate
* delegate
);
193 // Removes data stored or cached outside the user's cryptohome (wallpaper,
194 // avatar, OAuth token status, display name, display email).
195 virtual void RemoveNonCryptohomeData(const std::string
& user_id
);
197 // Check for a particular user type.
199 // Returns true if |user_id| represents demo app.
200 virtual bool IsDemoApp(const std::string
& user_id
) const = 0;
202 // Returns true if |user_id| represents kiosk app.
203 virtual bool IsKioskApp(const std::string
& user_id
) const = 0;
205 // Returns true if |user_id| represents public account that has been marked
207 virtual bool IsPublicAccountMarkedForRemoval(
208 const std::string
& user_id
) const = 0;
210 // These methods are called when corresponding user type has signed in.
212 // Indicates that the demo account has just logged in.
213 virtual void DemoAccountLoggedIn() = 0;
215 // Indicates that a user just logged in as guest.
216 virtual void GuestUserLoggedIn();
218 // Indicates that a kiosk app robot just logged in.
219 virtual void KioskAppLoggedIn(const std::string
& app_id
) = 0;
221 // Indicates that a user just logged into a public session.
222 virtual void PublicAccountUserLoggedIn(User
* user
) = 0;
224 // Indicates that a regular user just logged in.
225 virtual void RegularUserLoggedIn(const std::string
& user_id
);
227 // Indicates that a regular user just logged in as ephemeral.
228 virtual void RegularUserLoggedInAsEphemeral(const std::string
& user_id
);
230 // Indicates that a user just logged into a retail mode session.
231 virtual void RetailModeUserLoggedIn() = 0;
233 // Indicates that a supervised user just logged in.
234 virtual void SupervisedUserLoggedIn(const std::string
& user_id
) = 0;
236 // Getters/setters for private members.
238 virtual void SetCurrentUserIsOwner(bool is_current_user_owner
);
240 virtual bool GetEphemeralUsersEnabled() const;
241 virtual void SetEphemeralUsersEnabled(bool enabled
);
243 virtual void SetIsCurrentUserNew(bool is_new
);
245 virtual void SetOwnerEmail(std::string owner_user_id
);
247 virtual const std::string
& GetPendingUserSwitchID() const;
248 virtual void SetPendingUserSwitchID(std::string user_id
);
250 // The logged-in user that is currently active in current session.
251 // NULL until a user has logged in, then points to one
252 // of the User instances in |users_|, the |guest_user_| instance or an
253 // ephemeral user instance.
256 // The primary user of the current session. It is recorded for the first
257 // signed-in user and does not change thereafter.
260 // List of all known users. User instances are owned by |this|. Regular users
261 // are removed by |RemoveUserFromList|, public accounts by
262 // |UpdateAndCleanUpPublicAccounts|.
266 // Stages of loading user list from preferences. Some methods can have
267 // different behavior depending on stage.
268 enum UserLoadStage
{ STAGE_NOT_LOADED
= 0, STAGE_LOADING
, STAGE_LOADED
};
270 // Returns a list of users who have logged into this device previously.
271 // Same as GetUsers but used if you need to modify User from that list.
272 UserList
& GetUsersAndModify();
274 // Returns the user with the given email address if found in the persistent
275 // list. Returns |NULL| otherwise.
276 const User
* FindUserInList(const std::string
& user_id
) const;
278 // Returns |true| if user with the given id is found in the persistent list.
279 // Returns |false| otherwise. Does not trigger user loading.
280 const bool UserExistsInList(const std::string
& user_id
) const;
282 // Same as FindUserInList but returns non-const pointer to User object.
283 User
* FindUserInListAndModify(const std::string
& user_id
);
285 // Reads user's oauth token status from local state preferences.
286 User::OAuthTokenStatus
LoadUserOAuthStatus(const std::string
& user_id
) const;
288 // Read a flag indicating whether online authentication against GAIA should
289 // be enforced during the user's next sign-in from local state preferences.
290 bool LoadForceOnlineSignin(const std::string
& user_id
) const;
292 // Notifies observers that merge session state had changed.
293 void NotifyMergeSessionStateChanged();
295 // Notifies observers that active user has changed.
296 void NotifyActiveUserChanged(const User
* active_user
);
298 // Notifies observers that active user_id hash has changed.
299 void NotifyActiveUserHashChanged(const std::string
& hash
);
301 // Update the global LoginState.
302 void UpdateLoginState();
304 // Insert |user| at the front of the LRU user list.
305 void SetLRUUser(User
* user
);
307 // Sends metrics in response to a regular user logging in.
308 void SendRegularUserLoginMetrics(const std::string
& user_id
);
310 // Sets account locale for user with id |user_id|.
311 virtual void UpdateUserAccountLocale(const std::string
& user_id
,
312 const std::string
& locale
);
314 // Updates user account after locale was resolved.
315 void DoUpdateAccountLocale(const std::string
& user_id
,
316 scoped_ptr
<std::string
> resolved_locale
);
318 // Indicates stage of loading user from prefs.
319 UserLoadStage user_loading_stage_
;
321 // List of all users that are logged in current session. These point to User
322 // instances in |users_|. Only one of them could be marked as active.
323 UserList logged_in_users_
;
325 // A list of all users that are logged in the current session. In contrast to
326 // |logged_in_users|, the order of this list is least recently used so that
327 // the active user should always be the first one in the list.
328 UserList lru_logged_in_users_
;
330 // True if SessionStarted() has been called.
331 bool session_started_
;
333 // Cached flag of whether currently logged-in user is owner or not.
334 // May be accessed on different threads, requires locking.
335 bool is_current_user_owner_
;
336 mutable base::Lock is_current_user_owner_lock_
;
338 // Cached flag of whether the currently logged-in user existed before this
340 bool is_current_user_new_
;
342 // Cached flag of whether the currently logged-in user is a regular user who
343 // logged in as ephemeral. Storage of persistent information is avoided for
344 // such users by not adding them to the persistent user list, not downloading
345 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
347 bool is_current_user_ephemeral_regular_user_
;
349 // Cached flag indicating whether the ephemeral user policy is enabled.
350 // Defaults to |false| if the value has not been read from trusted device
352 bool ephemeral_users_enabled_
;
354 // Cached name of device owner. Defaults to empty string if the value has not
355 // been read from trusted device policy yet.
356 std::string owner_email_
;
358 ObserverList
<UserManager::Observer
> observer_list_
;
360 // TODO(nkostylev): Merge with session state refactoring CL.
361 ObserverList
<UserManager::UserSessionStateObserver
>
362 session_state_observer_list_
;
364 // Time at which this object was created.
365 base::TimeTicks manager_creation_time_
;
367 // ID of the user just added to the session that needs to be activated
368 // as soon as user's profile is loaded.
369 std::string pending_user_switch_
;
371 // TaskRunner for UI thread.
372 scoped_refptr
<base::TaskRunner
> task_runner_
;
374 // TaskRunner for SequencedWorkerPool.
375 scoped_refptr
<base::TaskRunner
> blocking_task_runner_
;
377 base::WeakPtrFactory
<UserManagerBase
> weak_factory_
;
379 DISALLOW_COPY_AND_ASSIGN(UserManagerBase
);
382 } // namespace user_manager
384 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_