Center distiller results on desktop
[chromium-blink-merge.git] / components / user_manager / user_manager_base.h
blobd61f8fbd370eda7a71f74f2189569f725e9733fc
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 <map>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
24 class PrefService;
25 class PrefRegistrySimple;
27 namespace base {
28 class DictionaryValue;
29 class ListValue;
30 class TaskRunner;
33 namespace user_manager {
35 class RemoveUserDelegate;
37 // Base implementation of the UserManager interface.
38 class USER_MANAGER_EXPORT UserManagerBase : public UserManager {
39 public:
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 void UpdateGaiaID(const UserID& user_id, const std::string& gaia_id) override;
127 bool FindGaiaID(const UserID& user_id, std::string* out_value) override;
128 void UpdateUsingSAML(const std::string& user_id,
129 const bool using_saml) override;
130 bool FindUsingSAML(const std::string& user_id) override;
131 void SetKnownUserDeviceId(const UserID& user_id,
132 const std::string& device_id) override;
133 std::string GetKnownUserDeviceId(const UserID& user_id) override;
135 virtual void SetIsCurrentUserNew(bool is_new);
137 // TODO(xiyuan): Figure out a better way to expose this info.
138 virtual bool HasPendingBootstrap(const std::string& user_id) const;
140 // Helper function that copies users from |users_list| to |users_vector| and
141 // |users_set|. Duplicates and users already present in |existing_users| are
142 // skipped.
143 static void ParseUserList(const base::ListValue& users_list,
144 const std::set<std::string>& existing_users,
145 std::vector<std::string>* users_vector,
146 std::set<std::string>* users_set);
148 // Returns true if trusted device policies have successfully been retrieved
149 // and ephemeral users are enabled.
150 virtual bool AreEphemeralUsersEnabled() const = 0;
152 protected:
153 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
154 // that there is no user with same id.
155 virtual void AddUserRecord(User* user);
157 // Returns true if user may be removed.
158 virtual bool CanUserBeRemoved(const User* user) const;
160 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
161 // equals to active_user_, active_user_ is reset to NULL.
162 virtual void DeleteUser(User* user);
164 // Returns the locale used by the application.
165 virtual const std::string& GetApplicationLocale() const = 0;
167 // Returns "Local State" PrefService instance.
168 virtual PrefService* GetLocalState() const = 0;
170 // Loads |users_| from Local State if the list has not been loaded yet.
171 // Subsequent calls have no effect. Must be called on the UI thread.
172 virtual void EnsureUsersLoaded();
174 // Handle OAuth token |status| change for |user_id|.
175 virtual void HandleUserOAuthTokenStatusChange(
176 const std::string& user_id,
177 User::OAuthTokenStatus status) const = 0;
179 // Returns true if device is enterprise managed.
180 virtual bool IsEnterpriseManaged() const = 0;
182 // Helper function that copies users from |users_list| to |users_vector| and
183 // |users_set|. Duplicates and users already present in |existing_users| are
184 // skipped.
185 // Loads public accounts from the Local state and fills in
186 // |public_sessions_set|.
187 virtual void LoadPublicAccounts(
188 std::set<std::string>* public_sessions_set) = 0;
190 // Notifies that user has logged in.
191 virtual void NotifyOnLogin();
193 // Notifies observers that another user was added to the session.
194 // If |user_switch_pending| is true this means that user has not been fully
195 // initialized yet like waiting for profile to be loaded.
196 virtual void NotifyUserAddedToSession(const User* added_user,
197 bool user_switch_pending);
199 // Performs any additional actions before user list is loaded.
200 virtual void PerformPreUserListLoadingActions() = 0;
202 // Performs any additional actions after user list is loaded.
203 virtual void PerformPostUserListLoadingActions() = 0;
205 // Performs any additional actions after UserLoggedIn() execution has been
206 // completed.
207 // |browser_restart| is true when reloading Chrome after crash to distinguish
208 // from normal sign in flow.
209 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
211 // Implementation for RemoveUser method. It is synchronous. It is called from
212 // RemoveUserInternal after owner check.
213 virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
214 RemoveUserDelegate* delegate);
216 // Removes a regular or supervised user from the user list.
217 // Returns the user if found or NULL otherwise.
218 // Also removes the user from the persistent user list.
219 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
221 // Implementation for RemoveUser method. This is an asynchronous part of the
222 // method, that verifies that owner will not get deleted, and calls
223 // |RemoveNonOwnerUserInternal|.
224 virtual void RemoveUserInternal(const std::string& user_email,
225 RemoveUserDelegate* delegate);
227 // Removes data stored or cached outside the user's cryptohome (wallpaper,
228 // avatar, OAuth token status, display name, display email).
229 virtual void RemoveNonCryptohomeData(const std::string& user_id);
231 // Check for a particular user type.
233 // Returns true if |user_id| represents demo app.
234 virtual bool IsDemoApp(const std::string& user_id) const = 0;
236 // Returns true if |user_id| represents kiosk app.
237 virtual bool IsKioskApp(const std::string& user_id) const = 0;
239 // Returns true if |user_id| represents public account that has been marked
240 // for deletion.
241 virtual bool IsPublicAccountMarkedForRemoval(
242 const std::string& user_id) const = 0;
244 // These methods are called when corresponding user type has signed in.
246 // Indicates that the demo account has just logged in.
247 virtual void DemoAccountLoggedIn() = 0;
249 // Indicates that a user just logged in as guest.
250 virtual void GuestUserLoggedIn();
252 // Indicates that a kiosk app robot just logged in.
253 virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
255 // Indicates that a user just logged into a public session.
256 virtual void PublicAccountUserLoggedIn(User* user) = 0;
258 // Indicates that a regular user just logged in.
259 virtual void RegularUserLoggedIn(const std::string& user_id);
261 // Indicates that a regular user just logged in as ephemeral.
262 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
264 // Indicates that a supervised user just logged in.
265 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
267 // Getters/setters for private members.
269 virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
271 virtual bool GetEphemeralUsersEnabled() const;
272 virtual void SetEphemeralUsersEnabled(bool enabled);
274 virtual void SetOwnerEmail(std::string owner_user_id);
276 virtual const std::string& GetPendingUserSwitchID() const;
277 virtual void SetPendingUserSwitchID(std::string user_id);
279 // The logged-in user that is currently active in current session.
280 // NULL until a user has logged in, then points to one
281 // of the User instances in |users_|, the |guest_user_| instance or an
282 // ephemeral user instance.
283 User* active_user_;
285 // The primary user of the current session. It is recorded for the first
286 // signed-in user and does not change thereafter.
287 User* primary_user_;
289 // List of all known users. User instances are owned by |this|. Regular users
290 // are removed by |RemoveUserFromList|, public accounts by
291 // |UpdateAndCleanUpPublicAccounts|.
292 UserList users_;
294 // List of all users that are logged in current session. These point to User
295 // instances in |users_|. Only one of them could be marked as active.
296 UserList logged_in_users_;
298 // A list of all users that are logged in the current session. In contrast to
299 // |logged_in_users|, the order of this list is least recently used so that
300 // the active user should always be the first one in the list.
301 UserList lru_logged_in_users_;
303 private:
304 // Stages of loading user list from preferences. Some methods can have
305 // different behavior depending on stage.
306 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
308 // Returns a list of users who have logged into this device previously.
309 // Same as GetUsers but used if you need to modify User from that list.
310 UserList& GetUsersAndModify();
312 // Returns the user with the given email address if found in the persistent
313 // list. Returns |NULL| otherwise.
314 const User* FindUserInList(const std::string& user_id) const;
316 // Returns |true| if user with the given id is found in the persistent list.
317 // Returns |false| otherwise. Does not trigger user loading.
318 bool UserExistsInList(const std::string& user_id) const;
320 // Same as FindUserInList but returns non-const pointer to User object.
321 User* FindUserInListAndModify(const std::string& user_id);
323 // Reads user's oauth token status from local state preferences.
324 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
326 // Read a flag indicating whether online authentication against GAIA should
327 // be enforced during the user's next sign-in from local state preferences.
328 bool LoadForceOnlineSignin(const std::string& user_id) const;
330 // Notifies observers that merge session state had changed.
331 void NotifyMergeSessionStateChanged();
333 // Notifies observers that active user has changed.
334 void NotifyActiveUserChanged(const User* active_user);
336 // Notifies observers that active user_id hash has changed.
337 void NotifyActiveUserHashChanged(const std::string& hash);
339 // Update the global LoginState.
340 void UpdateLoginState();
342 // Insert |user| at the front of the LRU user list.
343 void SetLRUUser(User* user);
345 // Sends metrics in response to a user with gaia account (regular) logging in.
346 void SendGaiaUserLoginMetrics(const std::string& user_id);
348 // Sets account locale for user with id |user_id|.
349 virtual void UpdateUserAccountLocale(const std::string& user_id,
350 const std::string& locale);
352 // Updates user account after locale was resolved.
353 void DoUpdateAccountLocale(const std::string& user_id,
354 scoped_ptr<std::string> resolved_locale);
356 // Removes all user preferences associated with |user_id|.
357 void RemoveKnownUserPrefs(const UserID& user_id);
359 // Indicates stage of loading user from prefs.
360 UserLoadStage user_loading_stage_;
362 // True if SessionStarted() has been called.
363 bool session_started_;
365 // Cached flag of whether currently logged-in user is owner or not.
366 // May be accessed on different threads, requires locking.
367 bool is_current_user_owner_;
368 mutable base::Lock is_current_user_owner_lock_;
370 // Cached flag of whether the currently logged-in user existed before this
371 // login.
372 bool is_current_user_new_;
374 // Cached flag of whether the currently logged-in user is a regular user who
375 // logged in as ephemeral. Storage of persistent information is avoided for
376 // such users by not adding them to the persistent user list, not downloading
377 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
378 // to |false|.
379 bool is_current_user_ephemeral_regular_user_;
381 // Cached flag indicating whether the ephemeral user policy is enabled.
382 // Defaults to |false| if the value has not been read from trusted device
383 // policy yet.
384 bool ephemeral_users_enabled_;
386 // Cached name of device owner. Defaults to empty string if the value has not
387 // been read from trusted device policy yet.
388 std::string owner_email_;
390 ObserverList<UserManager::Observer> observer_list_;
392 // TODO(nkostylev): Merge with session state refactoring CL.
393 ObserverList<UserManager::UserSessionStateObserver>
394 session_state_observer_list_;
396 // Time at which this object was created.
397 base::TimeTicks manager_creation_time_;
399 // ID of the user just added to the session that needs to be activated
400 // as soon as user's profile is loaded.
401 std::string pending_user_switch_;
403 // ID of the user that was active in the previous session.
404 // Preference value is stored here before first user signs in
405 // because pref will be overidden once session restore starts.
406 std::string last_session_active_user_;
407 bool last_session_active_user_initialized_;
409 // TaskRunner for UI thread.
410 scoped_refptr<base::TaskRunner> task_runner_;
412 // TaskRunner for SequencedWorkerPool.
413 scoped_refptr<base::TaskRunner> blocking_task_runner_;
415 base::WeakPtrFactory<UserManagerBase> weak_factory_;
417 DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
420 } // namespace user_manager
422 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_