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