blob: ba7cd793bb4a97b4003da49060e9dad3e43fe028 [file] [log] [blame]
[email protected]babc1482014-08-02 05:44:131// 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.
4
[email protected]4d390782014-08-15 09:22:585#ifndef COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
6#define COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
[email protected]babc1482014-08-02 05:44:137
8#include <set>
9#include <string>
10#include <vector>
11
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"
[email protected]babc1482014-08-02 05:44:1317#include "components/user_manager/user.h"
[email protected]4d390782014-08-15 09:22:5818#include "components/user_manager/user_manager.h"
19#include "components/user_manager/user_manager_export.h"
merkulovab82b7132014-11-17 11:06:5020#include "components/user_manager/user_type.h"
[email protected]babc1482014-08-02 05:44:1321
22class PrefService;
23class PrefRegistrySimple;
24
[email protected]4d390782014-08-15 09:22:5825namespace base {
26class ListValue;
27class TaskRunner;
28}
29
30namespace user_manager {
[email protected]babc1482014-08-02 05:44:1331
32class RemoveUserDelegate;
33
34// Base implementation of the UserManager interface.
[email protected]4d390782014-08-15 09:22:5835class USER_MANAGER_EXPORT UserManagerBase : public UserManager {
[email protected]babc1482014-08-02 05:44:1336 public:
[email protected]ac58eaf72014-08-19 13:06:4237 // Creates UserManagerBase with |task_runner| for UI thread and
38 // |blocking_task_runner| for SequencedWorkerPool.
[email protected]4d390782014-08-15 09:22:5839 UserManagerBase(scoped_refptr<base::TaskRunner> task_runner,
40 scoped_refptr<base::TaskRunner> blocking_task_runner);
dchengbd5bd4f2015-01-23 16:01:3841 ~UserManagerBase() override;
[email protected]babc1482014-08-02 05:44:1342
43 // Registers UserManagerBase preferences.
44 static void RegisterPrefs(PrefRegistrySimple* registry);
45
46 // UserManager implementation:
dchengbd5bd4f2015-01-23 16:01:3847 void Shutdown() override;
48 const UserList& GetUsers() const override;
49 const UserList& GetLoggedInUsers() const override;
50 const UserList& GetLRULoggedInUsers() const override;
51 const std::string& GetOwnerEmail() const override;
52 void UserLoggedIn(const std::string& user_id,
53 const std::string& user_id_hash,
54 bool browser_restart) override;
55 void SwitchActiveUser(const std::string& user_id) override;
56 void SwitchToLastActiveUser() override;
57 void SessionStarted() override;
58 void RemoveUser(const std::string& user_id,
59 RemoveUserDelegate* delegate) override;
60 void RemoveUserFromList(const std::string& user_id) override;
61 bool IsKnownUser(const std::string& user_id) const override;
62 const User* FindUser(const std::string& user_id) const override;
63 User* FindUserAndModify(const std::string& user_id) override;
64 const User* GetLoggedInUser() const override;
65 User* GetLoggedInUser() override;
66 const User* GetActiveUser() const override;
67 User* GetActiveUser() override;
68 const User* GetPrimaryUser() const override;
69 void SaveUserOAuthStatus(const std::string& user_id,
70 User::OAuthTokenStatus oauth_token_status) override;
71 void SaveForceOnlineSignin(const std::string& user_id,
72 bool force_online_signin) override;
73 void SaveUserDisplayName(const std::string& user_id,
74 const base::string16& display_name) override;
75 base::string16 GetUserDisplayName(const std::string& user_id) const override;
76 void SaveUserDisplayEmail(const std::string& user_id,
77 const std::string& display_email) override;
78 std::string GetUserDisplayEmail(const std::string& user_id) const override;
79 void SaveUserType(const std::string& user_id,
80 const UserType& user_type) override;
81 void UpdateUserAccountData(const std::string& user_id,
82 const UserAccountData& account_data) override;
83 bool IsCurrentUserOwner() const override;
84 bool IsCurrentUserNew() const override;
85 bool IsCurrentUserNonCryptohomeDataEphemeral() const override;
86 bool CanCurrentUserLock() const override;
87 bool IsUserLoggedIn() const override;
88 bool IsLoggedInAsUserWithGaiaAccount() const override;
89 bool IsLoggedInAsChildUser() const override;
90 bool IsLoggedInAsPublicAccount() const override;
91 bool IsLoggedInAsGuest() const override;
92 bool IsLoggedInAsSupervisedUser() const override;
93 bool IsLoggedInAsKioskApp() const override;
94 bool IsLoggedInAsStub() const override;
95 bool IsSessionStarted() const override;
96 bool IsUserNonCryptohomeDataEphemeral(
mostynbfe59f482014-10-06 15:04:4697 const std::string& user_id) const override;
dchengbd5bd4f2015-01-23 16:01:3898 void AddObserver(UserManager::Observer* obs) override;
99 void RemoveObserver(UserManager::Observer* obs) override;
100 void AddSessionStateObserver(
mostynbfe59f482014-10-06 15:04:46101 UserManager::UserSessionStateObserver* obs) override;
dchengbd5bd4f2015-01-23 16:01:38102 void RemoveSessionStateObserver(
mostynbfe59f482014-10-06 15:04:46103 UserManager::UserSessionStateObserver* obs) override;
dchengbd5bd4f2015-01-23 16:01:38104 void NotifyLocalStateChanged() override;
105 void ChangeUserChildStatus(User* user, bool is_child) override;
[email protected]babc1482014-08-02 05:44:13106
107 // Helper function that copies users from |users_list| to |users_vector| and
108 // |users_set|. Duplicates and users already present in |existing_users| are
109 // skipped.
110 static void ParseUserList(const base::ListValue& users_list,
111 const std::set<std::string>& existing_users,
112 std::vector<std::string>* users_vector,
113 std::set<std::string>* users_set);
114
115 protected:
[email protected]babc1482014-08-02 05:44:13116 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
117 // that there is no user with same id.
[email protected]4d390782014-08-15 09:22:58118 virtual void AddUserRecord(User* user);
[email protected]babc1482014-08-02 05:44:13119
120 // Returns true if trusted device policies have successfully been retrieved
121 // and ephemeral users are enabled.
122 virtual bool AreEphemeralUsersEnabled() const = 0;
123
124 // Returns true if user may be removed.
[email protected]4d390782014-08-15 09:22:58125 virtual bool CanUserBeRemoved(const User* user) const;
[email protected]babc1482014-08-02 05:44:13126
127 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
128 // equals to active_user_, active_user_ is reset to NULL.
[email protected]4d390782014-08-15 09:22:58129 virtual void DeleteUser(User* user);
[email protected]babc1482014-08-02 05:44:13130
131 // Returns the locale used by the application.
132 virtual const std::string& GetApplicationLocale() const = 0;
133
134 // Returns "Local State" PrefService instance.
135 virtual PrefService* GetLocalState() const = 0;
136
137 // Loads |users_| from Local State if the list has not been loaded yet.
138 // Subsequent calls have no effect. Must be called on the UI thread.
139 void EnsureUsersLoaded();
140
[email protected]4d390782014-08-15 09:22:58141 // Handle OAuth token |status| change for |user_id|.
142 virtual void HandleUserOAuthTokenStatusChange(
143 const std::string& user_id,
144 User::OAuthTokenStatus status) const = 0;
145
[email protected]babc1482014-08-02 05:44:13146 // Returns true if device is enterprise managed.
147 virtual bool IsEnterpriseManaged() const = 0;
148
149 // Helper function that copies users from |users_list| to |users_vector| and
150 // |users_set|. Duplicates and users already present in |existing_users| are
151 // skipped.
152 // Loads public accounts from the Local state and fills in
153 // |public_sessions_set|.
154 virtual void LoadPublicAccounts(
155 std::set<std::string>* public_sessions_set) = 0;
156
157 // Notifies that user has logged in.
158 virtual void NotifyOnLogin();
159
160 // Notifies observers that another user was added to the session.
161 // If |user_switch_pending| is true this means that user has not been fully
162 // initialized yet like waiting for profile to be loaded.
[email protected]4d390782014-08-15 09:22:58163 virtual void NotifyUserAddedToSession(const User* added_user,
[email protected]babc1482014-08-02 05:44:13164 bool user_switch_pending);
165
166 // Performs any additional actions before user list is loaded.
167 virtual void PerformPreUserListLoadingActions() = 0;
168
169 // Performs any additional actions after user list is loaded.
170 virtual void PerformPostUserListLoadingActions() = 0;
171
172 // Performs any additional actions after UserLoggedIn() execution has been
173 // completed.
174 // |browser_restart| is true when reloading Chrome after crash to distinguish
175 // from normal sign in flow.
176 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
177
178 // Implementation for RemoveUser method. It is synchronous. It is called from
179 // RemoveUserInternal after owner check.
180 virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
181 RemoveUserDelegate* delegate);
182
183 // Removes a regular or supervised user from the user list.
184 // Returns the user if found or NULL otherwise.
185 // Also removes the user from the persistent user list.
[email protected]4d390782014-08-15 09:22:58186 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
[email protected]babc1482014-08-02 05:44:13187
188 // Implementation for RemoveUser method. This is an asynchronous part of the
189 // method, that verifies that owner will not get deleted, and calls
190 // |RemoveNonOwnerUserInternal|.
191 virtual void RemoveUserInternal(const std::string& user_email,
192 RemoveUserDelegate* delegate);
193
194 // Removes data stored or cached outside the user's cryptohome (wallpaper,
195 // avatar, OAuth token status, display name, display email).
196 virtual void RemoveNonCryptohomeData(const std::string& user_id);
197
198 // Check for a particular user type.
199
200 // Returns true if |user_id| represents demo app.
201 virtual bool IsDemoApp(const std::string& user_id) const = 0;
202
203 // Returns true if |user_id| represents kiosk app.
204 virtual bool IsKioskApp(const std::string& user_id) const = 0;
205
206 // Returns true if |user_id| represents public account that has been marked
207 // for deletion.
208 virtual bool IsPublicAccountMarkedForRemoval(
209 const std::string& user_id) const = 0;
210
211 // These methods are called when corresponding user type has signed in.
212
213 // Indicates that the demo account has just logged in.
214 virtual void DemoAccountLoggedIn() = 0;
215
216 // Indicates that a user just logged in as guest.
217 virtual void GuestUserLoggedIn();
218
219 // Indicates that a kiosk app robot just logged in.
220 virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
221
222 // Indicates that a user just logged into a public session.
[email protected]4d390782014-08-15 09:22:58223 virtual void PublicAccountUserLoggedIn(User* user) = 0;
[email protected]babc1482014-08-02 05:44:13224
225 // Indicates that a regular user just logged in.
226 virtual void RegularUserLoggedIn(const std::string& user_id);
227
228 // Indicates that a regular user just logged in as ephemeral.
229 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
230
[email protected]babc1482014-08-02 05:44:13231 // Indicates that a supervised user just logged in.
232 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
233
234 // Getters/setters for private members.
235
236 virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
237
238 virtual bool GetEphemeralUsersEnabled() const;
239 virtual void SetEphemeralUsersEnabled(bool enabled);
240
241 virtual void SetIsCurrentUserNew(bool is_new);
242
243 virtual void SetOwnerEmail(std::string owner_user_id);
244
245 virtual const std::string& GetPendingUserSwitchID() const;
246 virtual void SetPendingUserSwitchID(std::string user_id);
247
248 // The logged-in user that is currently active in current session.
249 // NULL until a user has logged in, then points to one
250 // of the User instances in |users_|, the |guest_user_| instance or an
251 // ephemeral user instance.
[email protected]4d390782014-08-15 09:22:58252 User* active_user_;
[email protected]babc1482014-08-02 05:44:13253
254 // The primary user of the current session. It is recorded for the first
255 // signed-in user and does not change thereafter.
[email protected]4d390782014-08-15 09:22:58256 User* primary_user_;
[email protected]babc1482014-08-02 05:44:13257
258 // List of all known users. User instances are owned by |this|. Regular users
259 // are removed by |RemoveUserFromList|, public accounts by
260 // |UpdateAndCleanUpPublicAccounts|.
[email protected]4d390782014-08-15 09:22:58261 UserList users_;
[email protected]babc1482014-08-02 05:44:13262
263 private:
264 // Stages of loading user list from preferences. Some methods can have
265 // different behavior depending on stage.
266 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
267
268 // Returns a list of users who have logged into this device previously.
269 // Same as GetUsers but used if you need to modify User from that list.
[email protected]4d390782014-08-15 09:22:58270 UserList& GetUsersAndModify();
[email protected]babc1482014-08-02 05:44:13271
272 // Returns the user with the given email address if found in the persistent
273 // list. Returns |NULL| otherwise.
[email protected]4d390782014-08-15 09:22:58274 const User* FindUserInList(const std::string& user_id) const;
[email protected]babc1482014-08-02 05:44:13275
276 // Returns |true| if user with the given id is found in the persistent list.
277 // Returns |false| otherwise. Does not trigger user loading.
mukai9fa77612014-10-10 00:23:26278 bool UserExistsInList(const std::string& user_id) const;
[email protected]babc1482014-08-02 05:44:13279
280 // Same as FindUserInList but returns non-const pointer to User object.
[email protected]4d390782014-08-15 09:22:58281 User* FindUserInListAndModify(const std::string& user_id);
[email protected]babc1482014-08-02 05:44:13282
283 // Reads user's oauth token status from local state preferences.
[email protected]4d390782014-08-15 09:22:58284 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
[email protected]babc1482014-08-02 05:44:13285
286 // Read a flag indicating whether online authentication against GAIA should
287 // be enforced during the user's next sign-in from local state preferences.
288 bool LoadForceOnlineSignin(const std::string& user_id) const;
289
290 // Notifies observers that merge session state had changed.
291 void NotifyMergeSessionStateChanged();
292
293 // Notifies observers that active user has changed.
[email protected]4d390782014-08-15 09:22:58294 void NotifyActiveUserChanged(const User* active_user);
[email protected]babc1482014-08-02 05:44:13295
296 // Notifies observers that active user_id hash has changed.
297 void NotifyActiveUserHashChanged(const std::string& hash);
298
299 // Update the global LoginState.
300 void UpdateLoginState();
301
302 // Insert |user| at the front of the LRU user list.
[email protected]4d390782014-08-15 09:22:58303 void SetLRUUser(User* user);
[email protected]babc1482014-08-02 05:44:13304
merkulovac3ae44d2014-11-17 09:35:07305 // Sends metrics in response to a user with gaia account (regular) logging in.
306 void SendGaiaUserLoginMetrics(const std::string& user_id);
[email protected]babc1482014-08-02 05:44:13307
308 // Sets account locale for user with id |user_id|.
309 virtual void UpdateUserAccountLocale(const std::string& user_id,
310 const std::string& locale);
311
312 // Updates user account after locale was resolved.
313 void DoUpdateAccountLocale(const std::string& user_id,
[email protected]ac58eaf72014-08-19 13:06:42314 scoped_ptr<std::string> resolved_locale);
[email protected]babc1482014-08-02 05:44:13315
316 // Indicates stage of loading user from prefs.
317 UserLoadStage user_loading_stage_;
318
319 // List of all users that are logged in current session. These point to User
320 // instances in |users_|. Only one of them could be marked as active.
[email protected]4d390782014-08-15 09:22:58321 UserList logged_in_users_;
[email protected]babc1482014-08-02 05:44:13322
323 // A list of all users that are logged in the current session. In contrast to
324 // |logged_in_users|, the order of this list is least recently used so that
325 // the active user should always be the first one in the list.
[email protected]4d390782014-08-15 09:22:58326 UserList lru_logged_in_users_;
[email protected]babc1482014-08-02 05:44:13327
328 // True if SessionStarted() has been called.
329 bool session_started_;
330
331 // Cached flag of whether currently logged-in user is owner or not.
332 // May be accessed on different threads, requires locking.
333 bool is_current_user_owner_;
334 mutable base::Lock is_current_user_owner_lock_;
335
336 // Cached flag of whether the currently logged-in user existed before this
337 // login.
338 bool is_current_user_new_;
339
340 // Cached flag of whether the currently logged-in user is a regular user who
341 // logged in as ephemeral. Storage of persistent information is avoided for
342 // such users by not adding them to the persistent user list, not downloading
343 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
344 // to |false|.
345 bool is_current_user_ephemeral_regular_user_;
346
347 // Cached flag indicating whether the ephemeral user policy is enabled.
348 // Defaults to |false| if the value has not been read from trusted device
349 // policy yet.
350 bool ephemeral_users_enabled_;
351
352 // Cached name of device owner. Defaults to empty string if the value has not
353 // been read from trusted device policy yet.
354 std::string owner_email_;
355
356 ObserverList<UserManager::Observer> observer_list_;
357
358 // TODO(nkostylev): Merge with session state refactoring CL.
359 ObserverList<UserManager::UserSessionStateObserver>
360 session_state_observer_list_;
361
362 // Time at which this object was created.
363 base::TimeTicks manager_creation_time_;
364
365 // ID of the user just added to the session that needs to be activated
366 // as soon as user's profile is loaded.
367 std::string pending_user_switch_;
368
nkostylev5df7e992014-09-26 09:03:47369 // ID of the user that was active in the previous session.
370 // Preference value is stored here before first user signs in
371 // because pref will be overidden once session restore starts.
372 std::string last_session_active_user_;
373 bool last_session_active_user_initialized_;
374
[email protected]ac58eaf72014-08-19 13:06:42375 // TaskRunner for UI thread.
[email protected]4d390782014-08-15 09:22:58376 scoped_refptr<base::TaskRunner> task_runner_;
[email protected]ac58eaf72014-08-19 13:06:42377
378 // TaskRunner for SequencedWorkerPool.
[email protected]4d390782014-08-15 09:22:58379 scoped_refptr<base::TaskRunner> blocking_task_runner_;
380
[email protected]babc1482014-08-02 05:44:13381 base::WeakPtrFactory<UserManagerBase> weak_factory_;
382
383 DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
384};
385
[email protected]4d390782014-08-15 09:22:58386} // namespace user_manager
[email protected]babc1482014-08-02 05:44:13387
[email protected]4d390782014-08-15 09:22:58388#endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_