Revert of [telemetry] Fix decorator hack for benchmark_smoke_unittest. (patchset...
[chromium-blink-merge.git] / components / user_manager / user_manager_base.h
blob58d64716c1ad5ee273973e5e2ca9605e8f94c6fc
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
20 #include "components/user_manager/user_type.h"
22 class PrefService;
23 class PrefRegistrySimple;
25 namespace base {
26 class ListValue;
27 class TaskRunner;
30 namespace user_manager {
32 class RemoveUserDelegate;
34 // Base implementation of the UserManager interface.
35 class USER_MANAGER_EXPORT UserManagerBase : public UserManager {
36 public:
37 // Creates UserManagerBase with |task_runner| for UI thread and
38 // |blocking_task_runner| for SequencedWorkerPool.
39 UserManagerBase(scoped_refptr<base::TaskRunner> task_runner,
40 scoped_refptr<base::TaskRunner> blocking_task_runner);
41 virtual ~UserManagerBase();
43 // Registers UserManagerBase preferences.
44 static void RegisterPrefs(PrefRegistrySimple* registry);
46 // UserManager implementation:
47 virtual void Shutdown() override;
48 virtual const UserList& GetUsers() const override;
49 virtual const UserList& GetLoggedInUsers() const override;
50 virtual const UserList& GetLRULoggedInUsers() const override;
51 virtual const std::string& GetOwnerEmail() const override;
52 virtual void UserLoggedIn(const std::string& user_id,
53 const std::string& user_id_hash,
54 bool browser_restart) override;
55 virtual void SwitchActiveUser(const std::string& user_id) override;
56 virtual void SwitchToLastActiveUser() override;
57 virtual void SessionStarted() override;
58 virtual void RemoveUser(const std::string& user_id,
59 RemoveUserDelegate* delegate) override;
60 virtual void RemoveUserFromList(const std::string& user_id) override;
61 virtual bool IsKnownUser(const std::string& user_id) const override;
62 virtual const User* FindUser(const std::string& user_id) const override;
63 virtual User* FindUserAndModify(const std::string& user_id) override;
64 virtual const User* GetLoggedInUser() const override;
65 virtual User* GetLoggedInUser() override;
66 virtual const User* GetActiveUser() const override;
67 virtual User* GetActiveUser() override;
68 virtual const User* GetPrimaryUser() const override;
69 virtual void SaveUserOAuthStatus(
70 const std::string& user_id,
71 User::OAuthTokenStatus oauth_token_status) override;
72 virtual void SaveForceOnlineSignin(const std::string& user_id,
73 bool force_online_signin) override;
74 virtual void SaveUserDisplayName(const std::string& user_id,
75 const base::string16& display_name) override;
76 virtual base::string16 GetUserDisplayName(
77 const std::string& user_id) const override;
78 virtual void SaveUserDisplayEmail(const std::string& user_id,
79 const std::string& display_email) override;
80 virtual std::string GetUserDisplayEmail(
81 const std::string& user_id) const override;
82 virtual void SaveUserType(const std::string& user_id,
83 const UserType& user_type) override;
84 virtual void UpdateUserAccountData(
85 const std::string& user_id,
86 const UserAccountData& account_data) override;
87 virtual bool IsCurrentUserOwner() const override;
88 virtual bool IsCurrentUserNew() const override;
89 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const override;
90 virtual bool CanCurrentUserLock() const override;
91 virtual bool IsUserLoggedIn() const override;
92 virtual bool IsLoggedInAsUserWithGaiaAccount() const override;
93 virtual bool IsLoggedInAsRegularSupervisedUser() const override;
94 virtual bool IsLoggedInAsDemoUser() const override;
95 virtual bool IsLoggedInAsPublicAccount() const override;
96 virtual bool IsLoggedInAsGuest() const override;
97 virtual bool IsLoggedInAsSupervisedUser() const override;
98 virtual bool IsLoggedInAsKioskApp() const override;
99 virtual bool IsLoggedInAsStub() const override;
100 virtual bool IsSessionStarted() const override;
101 virtual bool IsUserNonCryptohomeDataEphemeral(
102 const std::string& user_id) const override;
103 virtual void AddObserver(UserManager::Observer* obs) override;
104 virtual void RemoveObserver(UserManager::Observer* obs) override;
105 virtual void AddSessionStateObserver(
106 UserManager::UserSessionStateObserver* obs) override;
107 virtual void RemoveSessionStateObserver(
108 UserManager::UserSessionStateObserver* obs) override;
109 virtual void NotifyLocalStateChanged() override;
110 virtual void ChangeUserSupervisedStatus(User* user, bool is_supervised)
111 override;
113 // Helper function that copies users from |users_list| to |users_vector| and
114 // |users_set|. Duplicates and users already present in |existing_users| are
115 // skipped.
116 static void ParseUserList(const base::ListValue& users_list,
117 const std::set<std::string>& existing_users,
118 std::vector<std::string>* users_vector,
119 std::set<std::string>* users_set);
121 protected:
122 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
123 // that there is no user with same id.
124 virtual void AddUserRecord(User* user);
126 // Returns true if trusted device policies have successfully been retrieved
127 // and ephemeral users are enabled.
128 virtual bool AreEphemeralUsersEnabled() const = 0;
130 // Returns true if user may be removed.
131 virtual bool CanUserBeRemoved(const User* user) const;
133 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
134 // equals to active_user_, active_user_ is reset to NULL.
135 virtual void DeleteUser(User* user);
137 // Returns the locale used by the application.
138 virtual const std::string& GetApplicationLocale() const = 0;
140 // Returns "Local State" PrefService instance.
141 virtual PrefService* GetLocalState() const = 0;
143 // Loads |users_| from Local State if the list has not been loaded yet.
144 // Subsequent calls have no effect. Must be called on the UI thread.
145 void EnsureUsersLoaded();
147 // Handle OAuth token |status| change for |user_id|.
148 virtual void HandleUserOAuthTokenStatusChange(
149 const std::string& user_id,
150 User::OAuthTokenStatus status) const = 0;
152 // Returns true if device is enterprise managed.
153 virtual bool IsEnterpriseManaged() const = 0;
155 // Helper function that copies users from |users_list| to |users_vector| and
156 // |users_set|. Duplicates and users already present in |existing_users| are
157 // skipped.
158 // Loads public accounts from the Local state and fills in
159 // |public_sessions_set|.
160 virtual void LoadPublicAccounts(
161 std::set<std::string>* public_sessions_set) = 0;
163 // Notifies that user has logged in.
164 virtual void NotifyOnLogin();
166 // Notifies observers that another user was added to the session.
167 // If |user_switch_pending| is true this means that user has not been fully
168 // initialized yet like waiting for profile to be loaded.
169 virtual void NotifyUserAddedToSession(const User* added_user,
170 bool user_switch_pending);
172 // Performs any additional actions before user list is loaded.
173 virtual void PerformPreUserListLoadingActions() = 0;
175 // Performs any additional actions after user list is loaded.
176 virtual void PerformPostUserListLoadingActions() = 0;
178 // Performs any additional actions after UserLoggedIn() execution has been
179 // completed.
180 // |browser_restart| is true when reloading Chrome after crash to distinguish
181 // from normal sign in flow.
182 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
184 // Implementation for RemoveUser method. It is synchronous. It is called from
185 // RemoveUserInternal after owner check.
186 virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
187 RemoveUserDelegate* delegate);
189 // Removes a regular or supervised user from the user list.
190 // Returns the user if found or NULL otherwise.
191 // Also removes the user from the persistent user list.
192 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
194 // Implementation for RemoveUser method. This is an asynchronous part of the
195 // method, that verifies that owner will not get deleted, and calls
196 // |RemoveNonOwnerUserInternal|.
197 virtual void RemoveUserInternal(const std::string& user_email,
198 RemoveUserDelegate* delegate);
200 // Removes data stored or cached outside the user's cryptohome (wallpaper,
201 // avatar, OAuth token status, display name, display email).
202 virtual void RemoveNonCryptohomeData(const std::string& user_id);
204 // Check for a particular user type.
206 // Returns true if |user_id| represents demo app.
207 virtual bool IsDemoApp(const std::string& user_id) const = 0;
209 // Returns true if |user_id| represents kiosk app.
210 virtual bool IsKioskApp(const std::string& user_id) const = 0;
212 // Returns true if |user_id| represents public account that has been marked
213 // for deletion.
214 virtual bool IsPublicAccountMarkedForRemoval(
215 const std::string& user_id) const = 0;
217 // These methods are called when corresponding user type has signed in.
219 // Indicates that the demo account has just logged in.
220 virtual void DemoAccountLoggedIn() = 0;
222 // Indicates that a user just logged in as guest.
223 virtual void GuestUserLoggedIn();
225 // Indicates that a kiosk app robot just logged in.
226 virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
228 // Indicates that a user just logged into a public session.
229 virtual void PublicAccountUserLoggedIn(User* user) = 0;
231 // Indicates that a regular user just logged in.
232 virtual void RegularUserLoggedIn(const std::string& user_id);
234 // Indicates that a regular user just logged in as ephemeral.
235 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
237 // Indicates that a user just logged into a retail mode session.
238 virtual void RetailModeUserLoggedIn() = 0;
240 // Indicates that a supervised user just logged in.
241 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
243 // Getters/setters for private members.
245 virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
247 virtual bool GetEphemeralUsersEnabled() const;
248 virtual void SetEphemeralUsersEnabled(bool enabled);
250 virtual void SetIsCurrentUserNew(bool is_new);
252 virtual void SetOwnerEmail(std::string owner_user_id);
254 virtual const std::string& GetPendingUserSwitchID() const;
255 virtual void SetPendingUserSwitchID(std::string user_id);
257 // The logged-in user that is currently active in current session.
258 // NULL until a user has logged in, then points to one
259 // of the User instances in |users_|, the |guest_user_| instance or an
260 // ephemeral user instance.
261 User* active_user_;
263 // The primary user of the current session. It is recorded for the first
264 // signed-in user and does not change thereafter.
265 User* primary_user_;
267 // List of all known users. User instances are owned by |this|. Regular users
268 // are removed by |RemoveUserFromList|, public accounts by
269 // |UpdateAndCleanUpPublicAccounts|.
270 UserList users_;
272 private:
273 // Stages of loading user list from preferences. Some methods can have
274 // different behavior depending on stage.
275 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
277 // Returns a list of users who have logged into this device previously.
278 // Same as GetUsers but used if you need to modify User from that list.
279 UserList& GetUsersAndModify();
281 // Returns the user with the given email address if found in the persistent
282 // list. Returns |NULL| otherwise.
283 const User* FindUserInList(const std::string& user_id) const;
285 // Returns |true| if user with the given id is found in the persistent list.
286 // Returns |false| otherwise. Does not trigger user loading.
287 bool UserExistsInList(const std::string& user_id) const;
289 // Same as FindUserInList but returns non-const pointer to User object.
290 User* FindUserInListAndModify(const std::string& user_id);
292 // Reads user's oauth token status from local state preferences.
293 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
295 // Read a flag indicating whether online authentication against GAIA should
296 // be enforced during the user's next sign-in from local state preferences.
297 bool LoadForceOnlineSignin(const std::string& user_id) const;
299 // Notifies observers that merge session state had changed.
300 void NotifyMergeSessionStateChanged();
302 // Notifies observers that active user has changed.
303 void NotifyActiveUserChanged(const User* active_user);
305 // Notifies observers that active user_id hash has changed.
306 void NotifyActiveUserHashChanged(const std::string& hash);
308 // Update the global LoginState.
309 void UpdateLoginState();
311 // Insert |user| at the front of the LRU user list.
312 void SetLRUUser(User* user);
314 // Sends metrics in response to a user with gaia account (regular) logging in.
315 void SendGaiaUserLoginMetrics(const std::string& user_id);
317 // Sets account locale for user with id |user_id|.
318 virtual void UpdateUserAccountLocale(const std::string& user_id,
319 const std::string& locale);
321 // Updates user account after locale was resolved.
322 void DoUpdateAccountLocale(const std::string& user_id,
323 scoped_ptr<std::string> resolved_locale);
325 // Indicates stage of loading user from prefs.
326 UserLoadStage user_loading_stage_;
328 // List of all users that are logged in current session. These point to User
329 // instances in |users_|. Only one of them could be marked as active.
330 UserList logged_in_users_;
332 // A list of all users that are logged in the current session. In contrast to
333 // |logged_in_users|, the order of this list is least recently used so that
334 // the active user should always be the first one in the list.
335 UserList lru_logged_in_users_;
337 // True if SessionStarted() has been called.
338 bool session_started_;
340 // Cached flag of whether currently logged-in user is owner or not.
341 // May be accessed on different threads, requires locking.
342 bool is_current_user_owner_;
343 mutable base::Lock is_current_user_owner_lock_;
345 // Cached flag of whether the currently logged-in user existed before this
346 // login.
347 bool is_current_user_new_;
349 // Cached flag of whether the currently logged-in user is a regular user who
350 // logged in as ephemeral. Storage of persistent information is avoided for
351 // such users by not adding them to the persistent user list, not downloading
352 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
353 // to |false|.
354 bool is_current_user_ephemeral_regular_user_;
356 // Cached flag indicating whether the ephemeral user policy is enabled.
357 // Defaults to |false| if the value has not been read from trusted device
358 // policy yet.
359 bool ephemeral_users_enabled_;
361 // Cached name of device owner. Defaults to empty string if the value has not
362 // been read from trusted device policy yet.
363 std::string owner_email_;
365 ObserverList<UserManager::Observer> observer_list_;
367 // TODO(nkostylev): Merge with session state refactoring CL.
368 ObserverList<UserManager::UserSessionStateObserver>
369 session_state_observer_list_;
371 // Time at which this object was created.
372 base::TimeTicks manager_creation_time_;
374 // ID of the user just added to the session that needs to be activated
375 // as soon as user's profile is loaded.
376 std::string pending_user_switch_;
378 // ID of the user that was active in the previous session.
379 // Preference value is stored here before first user signs in
380 // because pref will be overidden once session restore starts.
381 std::string last_session_active_user_;
382 bool last_session_active_user_initialized_;
384 // TaskRunner for UI thread.
385 scoped_refptr<base::TaskRunner> task_runner_;
387 // TaskRunner for SequencedWorkerPool.
388 scoped_refptr<base::TaskRunner> blocking_task_runner_;
390 base::WeakPtrFactory<UserManagerBase> weak_factory_;
392 DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
395 } // namespace user_manager
397 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_