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 SwitchToLastActiveUser() override;
56 virtual void SessionStarted() override;
57 virtual void RemoveUser(const std::string& user_id,
58 RemoveUserDelegate* delegate) override;
59 virtual void RemoveUserFromList(const std::string& user_id) override;
60 virtual bool IsKnownUser(const std::string& user_id) const override;
61 virtual const User* FindUser(const std::string& user_id) const override;
62 virtual User* FindUserAndModify(const std::string& user_id) override;
63 virtual const User* GetLoggedInUser() const override;
64 virtual User* GetLoggedInUser() override;
65 virtual const User* GetActiveUser() const override;
66 virtual User* GetActiveUser() override;
67 virtual const User* GetPrimaryUser() const override;
68 virtual void SaveUserOAuthStatus(
69 const std::string& user_id,
70 User::OAuthTokenStatus oauth_token_status) override;
71 virtual void SaveForceOnlineSignin(const std::string& user_id,
72 bool force_online_signin) override;
73 virtual void SaveUserDisplayName(const std::string& user_id,
74 const base::string16& display_name) override;
75 virtual base::string16 GetUserDisplayName(
76 const std::string& user_id) const override;
77 virtual void SaveUserDisplayEmail(const std::string& user_id,
78 const std::string& display_email) override;
79 virtual std::string GetUserDisplayEmail(
80 const std::string& user_id) const override;
81 virtual void UpdateUserAccountData(
82 const std::string& user_id,
83 const UserAccountData& account_data) override;
84 virtual bool IsCurrentUserOwner() const override;
85 virtual bool IsCurrentUserNew() const override;
86 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const override;
87 virtual bool CanCurrentUserLock() const override;
88 virtual bool IsUserLoggedIn() const override;
89 virtual bool IsLoggedInAsRegularUser() const override;
90 virtual bool IsLoggedInAsDemoUser() const override;
91 virtual bool IsLoggedInAsPublicAccount() const override;
92 virtual bool IsLoggedInAsGuest() const override;
93 virtual bool IsLoggedInAsSupervisedUser() const override;
94 virtual bool IsLoggedInAsKioskApp() const override;
95 virtual bool IsLoggedInAsStub() const override;
96 virtual bool IsSessionStarted() const override;
97 virtual bool IsUserNonCryptohomeDataEphemeral(
98 const std::string& user_id) const override;
99 virtual void AddObserver(UserManager::Observer* obs) override;
100 virtual void RemoveObserver(UserManager::Observer* obs) override;
101 virtual void AddSessionStateObserver(
102 UserManager::UserSessionStateObserver* obs) override;
103 virtual void RemoveSessionStateObserver(
104 UserManager::UserSessionStateObserver* obs) override;
105 virtual void NotifyLocalStateChanged() override;
106 virtual void ChangeUserSupervisedStatus(User* user, bool is_supervised)
109 // Helper function that copies users from |users_list| to |users_vector| and
110 // |users_set|. Duplicates and users already present in |existing_users| are
112 static void ParseUserList(const base::ListValue& users_list,
113 const std::set<std::string>& existing_users,
114 std::vector<std::string>* users_vector,
115 std::set<std::string>* users_set);
118 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
119 // that there is no user with same id.
120 virtual void AddUserRecord(User* user);
122 // Returns true if trusted device policies have successfully been retrieved
123 // and ephemeral users are enabled.
124 virtual bool AreEphemeralUsersEnabled() const = 0;
126 // Returns true if user may be removed.
127 virtual bool CanUserBeRemoved(const User* user) const;
129 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
130 // equals to active_user_, active_user_ is reset to NULL.
131 virtual void DeleteUser(User* user);
133 // Returns the locale used by the application.
134 virtual const std::string& GetApplicationLocale() const = 0;
136 // Returns "Local State" PrefService instance.
137 virtual PrefService* GetLocalState() const = 0;
139 // Loads |users_| from Local State if the list has not been loaded yet.
140 // Subsequent calls have no effect. Must be called on the UI thread.
141 void EnsureUsersLoaded();
143 // Handle OAuth token |status| change for |user_id|.
144 virtual void HandleUserOAuthTokenStatusChange(
145 const std::string& user_id,
146 User::OAuthTokenStatus status) const = 0;
148 // Returns true if device is enterprise managed.
149 virtual bool IsEnterpriseManaged() const = 0;
151 // Helper function that copies users from |users_list| to |users_vector| and
152 // |users_set|. Duplicates and users already present in |existing_users| are
154 // Loads public accounts from the Local state and fills in
155 // |public_sessions_set|.
156 virtual void LoadPublicAccounts(
157 std::set<std::string>* public_sessions_set) = 0;
159 // Notifies that user has logged in.
160 virtual void NotifyOnLogin();
162 // Notifies observers that another user was added to the session.
163 // If |user_switch_pending| is true this means that user has not been fully
164 // initialized yet like waiting for profile to be loaded.
165 virtual void NotifyUserAddedToSession(const User* added_user,
166 bool user_switch_pending);
168 // Performs any additional actions before user list is loaded.
169 virtual void PerformPreUserListLoadingActions() = 0;
171 // Performs any additional actions after user list is loaded.
172 virtual void PerformPostUserListLoadingActions() = 0;
174 // Performs any additional actions after UserLoggedIn() execution has been
176 // |browser_restart| is true when reloading Chrome after crash to distinguish
177 // from normal sign in flow.
178 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
180 // Implementation for RemoveUser method. It is synchronous. It is called from
181 // RemoveUserInternal after owner check.
182 virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
183 RemoveUserDelegate* delegate);
185 // Removes a regular or supervised user from the user list.
186 // Returns the user if found or NULL otherwise.
187 // Also removes the user from the persistent user list.
188 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
190 // Implementation for RemoveUser method. This is an asynchronous part of the
191 // method, that verifies that owner will not get deleted, and calls
192 // |RemoveNonOwnerUserInternal|.
193 virtual void RemoveUserInternal(const std::string& user_email,
194 RemoveUserDelegate* delegate);
196 // Removes data stored or cached outside the user's cryptohome (wallpaper,
197 // avatar, OAuth token status, display name, display email).
198 virtual void RemoveNonCryptohomeData(const std::string& user_id);
200 // Check for a particular user type.
202 // Returns true if |user_id| represents demo app.
203 virtual bool IsDemoApp(const std::string& user_id) const = 0;
205 // Returns true if |user_id| represents kiosk app.
206 virtual bool IsKioskApp(const std::string& user_id) const = 0;
208 // Returns true if |user_id| represents public account that has been marked
210 virtual bool IsPublicAccountMarkedForRemoval(
211 const std::string& user_id) const = 0;
213 // These methods are called when corresponding user type has signed in.
215 // Indicates that the demo account has just logged in.
216 virtual void DemoAccountLoggedIn() = 0;
218 // Indicates that a user just logged in as guest.
219 virtual void GuestUserLoggedIn();
221 // Indicates that a kiosk app robot just logged in.
222 virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
224 // Indicates that a user just logged into a public session.
225 virtual void PublicAccountUserLoggedIn(User* user) = 0;
227 // Indicates that a regular user just logged in.
228 virtual void RegularUserLoggedIn(const std::string& user_id);
230 // Indicates that a regular user just logged in as ephemeral.
231 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
233 // Indicates that a user just logged into a retail mode session.
234 virtual void RetailModeUserLoggedIn() = 0;
236 // Indicates that a supervised user just logged in.
237 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
239 // Getters/setters for private members.
241 virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
243 virtual bool GetEphemeralUsersEnabled() const;
244 virtual void SetEphemeralUsersEnabled(bool enabled);
246 virtual void SetIsCurrentUserNew(bool is_new);
248 virtual void SetOwnerEmail(std::string owner_user_id);
250 virtual const std::string& GetPendingUserSwitchID() const;
251 virtual void SetPendingUserSwitchID(std::string user_id);
253 // The logged-in user that is currently active in current session.
254 // NULL until a user has logged in, then points to one
255 // of the User instances in |users_|, the |guest_user_| instance or an
256 // ephemeral user instance.
259 // The primary user of the current session. It is recorded for the first
260 // signed-in user and does not change thereafter.
263 // List of all known users. User instances are owned by |this|. Regular users
264 // are removed by |RemoveUserFromList|, public accounts by
265 // |UpdateAndCleanUpPublicAccounts|.
269 // Stages of loading user list from preferences. Some methods can have
270 // different behavior depending on stage.
271 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
273 // Returns a list of users who have logged into this device previously.
274 // Same as GetUsers but used if you need to modify User from that list.
275 UserList& GetUsersAndModify();
277 // Returns the user with the given email address if found in the persistent
278 // list. Returns |NULL| otherwise.
279 const User* FindUserInList(const std::string& user_id) const;
281 // Returns |true| if user with the given id is found in the persistent list.
282 // Returns |false| otherwise. Does not trigger user loading.
283 bool UserExistsInList(const std::string& user_id) const;
285 // Same as FindUserInList but returns non-const pointer to User object.
286 User* FindUserInListAndModify(const std::string& user_id);
288 // Reads user's oauth token status from local state preferences.
289 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
291 // Read a flag indicating whether online authentication against GAIA should
292 // be enforced during the user's next sign-in from local state preferences.
293 bool LoadForceOnlineSignin(const std::string& user_id) const;
295 // Notifies observers that merge session state had changed.
296 void NotifyMergeSessionStateChanged();
298 // Notifies observers that active user has changed.
299 void NotifyActiveUserChanged(const User* active_user);
301 // Notifies observers that active user_id hash has changed.
302 void NotifyActiveUserHashChanged(const std::string& hash);
304 // Update the global LoginState.
305 void UpdateLoginState();
307 // Insert |user| at the front of the LRU user list.
308 void SetLRUUser(User* user);
310 // Sends metrics in response to a regular user logging in.
311 void SendRegularUserLoginMetrics(const std::string& user_id);
313 // Sets account locale for user with id |user_id|.
314 virtual void UpdateUserAccountLocale(const std::string& user_id,
315 const std::string& locale);
317 // Updates user account after locale was resolved.
318 void DoUpdateAccountLocale(const std::string& user_id,
319 scoped_ptr<std::string> resolved_locale);
321 // Indicates stage of loading user from prefs.
322 UserLoadStage user_loading_stage_;
324 // List of all users that are logged in current session. These point to User
325 // instances in |users_|. Only one of them could be marked as active.
326 UserList logged_in_users_;
328 // A list of all users that are logged in the current session. In contrast to
329 // |logged_in_users|, the order of this list is least recently used so that
330 // the active user should always be the first one in the list.
331 UserList lru_logged_in_users_;
333 // True if SessionStarted() has been called.
334 bool session_started_;
336 // Cached flag of whether currently logged-in user is owner or not.
337 // May be accessed on different threads, requires locking.
338 bool is_current_user_owner_;
339 mutable base::Lock is_current_user_owner_lock_;
341 // Cached flag of whether the currently logged-in user existed before this
343 bool is_current_user_new_;
345 // Cached flag of whether the currently logged-in user is a regular user who
346 // logged in as ephemeral. Storage of persistent information is avoided for
347 // such users by not adding them to the persistent user list, not downloading
348 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
350 bool is_current_user_ephemeral_regular_user_;
352 // Cached flag indicating whether the ephemeral user policy is enabled.
353 // Defaults to |false| if the value has not been read from trusted device
355 bool ephemeral_users_enabled_;
357 // Cached name of device owner. Defaults to empty string if the value has not
358 // been read from trusted device policy yet.
359 std::string owner_email_;
361 ObserverList<UserManager::Observer> observer_list_;
363 // TODO(nkostylev): Merge with session state refactoring CL.
364 ObserverList<UserManager::UserSessionStateObserver>
365 session_state_observer_list_;
367 // Time at which this object was created.
368 base::TimeTicks manager_creation_time_;
370 // ID of the user just added to the session that needs to be activated
371 // as soon as user's profile is loaded.
372 std::string pending_user_switch_;
374 // ID of the user that was active in the previous session.
375 // Preference value is stored here before first user signs in
376 // because pref will be overidden once session restore starts.
377 std::string last_session_active_user_;
378 bool last_session_active_user_initialized_;
380 // TaskRunner for UI thread.
381 scoped_refptr<base::TaskRunner> task_runner_;
383 // TaskRunner for SequencedWorkerPool.
384 scoped_refptr<base::TaskRunner> blocking_task_runner_;
386 base::WeakPtrFactory<UserManagerBase> weak_factory_;
388 DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
391 } // namespace user_manager
393 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_