Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / components / user_manager / user_manager_base.h
blob38ada4f4338d727baaa711817c4e90165202ab63
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;
113 bool GetKnownUserStringPref(const UserID& user_id,
114 const std::string& path,
115 std::string* out_value) override;
116 void SetKnownUserStringPref(const UserID& user_id,
117 const std::string& path,
118 const std::string& in_value) override;
119 void UpdateGaiaID(const UserID& user_id, const std::string& gaia_id) override;
120 bool FindGaiaID(const UserID& user_id, std::string* out_value) override;
122 virtual void SetIsCurrentUserNew(bool is_new);
124 // TODO(xiyuan): Figure out a better way to expose this info.
125 virtual bool HasPendingBootstrap(const std::string& user_id) const;
127 // Helper function that copies users from |users_list| to |users_vector| and
128 // |users_set|. Duplicates and users already present in |existing_users| are
129 // skipped.
130 static void ParseUserList(const base::ListValue& users_list,
131 const std::set<std::string>& existing_users,
132 std::vector<std::string>* users_vector,
133 std::set<std::string>* users_set);
135 protected:
136 // Adds |user| to users list, and adds it to front of LRU list. It is assumed
137 // that there is no user with same id.
138 virtual void AddUserRecord(User* user);
140 // Returns true if trusted device policies have successfully been retrieved
141 // and ephemeral users are enabled.
142 virtual bool AreEphemeralUsersEnabled() const = 0;
144 // Returns true if user may be removed.
145 virtual bool CanUserBeRemoved(const User* user) const;
147 // A wrapper around C++ delete operator. Deletes |user|, and when |user|
148 // equals to active_user_, active_user_ is reset to NULL.
149 virtual void DeleteUser(User* user);
151 // Returns the locale used by the application.
152 virtual const std::string& GetApplicationLocale() const = 0;
154 // Returns "Local State" PrefService instance.
155 virtual PrefService* GetLocalState() const = 0;
157 // Loads |users_| from Local State if the list has not been loaded yet.
158 // Subsequent calls have no effect. Must be called on the UI thread.
159 virtual void EnsureUsersLoaded();
161 // Handle OAuth token |status| change for |user_id|.
162 virtual void HandleUserOAuthTokenStatusChange(
163 const std::string& user_id,
164 User::OAuthTokenStatus status) const = 0;
166 // Returns true if device is enterprise managed.
167 virtual bool IsEnterpriseManaged() const = 0;
169 // Helper function that copies users from |users_list| to |users_vector| and
170 // |users_set|. Duplicates and users already present in |existing_users| are
171 // skipped.
172 // Loads public accounts from the Local state and fills in
173 // |public_sessions_set|.
174 virtual void LoadPublicAccounts(
175 std::set<std::string>* public_sessions_set) = 0;
177 // Notifies that user has logged in.
178 virtual void NotifyOnLogin();
180 // Notifies observers that another user was added to the session.
181 // If |user_switch_pending| is true this means that user has not been fully
182 // initialized yet like waiting for profile to be loaded.
183 virtual void NotifyUserAddedToSession(const User* added_user,
184 bool user_switch_pending);
186 // Performs any additional actions before user list is loaded.
187 virtual void PerformPreUserListLoadingActions() = 0;
189 // Performs any additional actions after user list is loaded.
190 virtual void PerformPostUserListLoadingActions() = 0;
192 // Performs any additional actions after UserLoggedIn() execution has been
193 // completed.
194 // |browser_restart| is true when reloading Chrome after crash to distinguish
195 // from normal sign in flow.
196 virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
198 // Implementation for RemoveUser method. It is synchronous. It is called from
199 // RemoveUserInternal after owner check.
200 virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
201 RemoveUserDelegate* delegate);
203 // Removes a regular or supervised user from the user list.
204 // Returns the user if found or NULL otherwise.
205 // Also removes the user from the persistent user list.
206 User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
208 // Implementation for RemoveUser method. This is an asynchronous part of the
209 // method, that verifies that owner will not get deleted, and calls
210 // |RemoveNonOwnerUserInternal|.
211 virtual void RemoveUserInternal(const std::string& user_email,
212 RemoveUserDelegate* delegate);
214 // Removes data stored or cached outside the user's cryptohome (wallpaper,
215 // avatar, OAuth token status, display name, display email).
216 virtual void RemoveNonCryptohomeData(const std::string& user_id);
218 // Check for a particular user type.
220 // Returns true if |user_id| represents demo app.
221 virtual bool IsDemoApp(const std::string& user_id) const = 0;
223 // Returns true if |user_id| represents kiosk app.
224 virtual bool IsKioskApp(const std::string& user_id) const = 0;
226 // Returns true if |user_id| represents public account that has been marked
227 // for deletion.
228 virtual bool IsPublicAccountMarkedForRemoval(
229 const std::string& user_id) const = 0;
231 // These methods are called when corresponding user type has signed in.
233 // Indicates that the demo account has just logged in.
234 virtual void DemoAccountLoggedIn() = 0;
236 // Indicates that a user just logged in as guest.
237 virtual void GuestUserLoggedIn();
239 // Indicates that a kiosk app robot just logged in.
240 virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
242 // Indicates that a user just logged into a public session.
243 virtual void PublicAccountUserLoggedIn(User* user) = 0;
245 // Indicates that a regular user just logged in.
246 virtual void RegularUserLoggedIn(const std::string& user_id);
248 // Indicates that a regular user just logged in as ephemeral.
249 virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
251 // Indicates that a supervised user just logged in.
252 virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
254 // Getters/setters for private members.
256 virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
258 virtual bool GetEphemeralUsersEnabled() const;
259 virtual void SetEphemeralUsersEnabled(bool enabled);
261 virtual void SetOwnerEmail(std::string owner_user_id);
263 virtual const std::string& GetPendingUserSwitchID() const;
264 virtual void SetPendingUserSwitchID(std::string user_id);
266 // The logged-in user that is currently active in current session.
267 // NULL until a user has logged in, then points to one
268 // of the User instances in |users_|, the |guest_user_| instance or an
269 // ephemeral user instance.
270 User* active_user_;
272 // The primary user of the current session. It is recorded for the first
273 // signed-in user and does not change thereafter.
274 User* primary_user_;
276 // List of all known users. User instances are owned by |this|. Regular users
277 // are removed by |RemoveUserFromList|, public accounts by
278 // |UpdateAndCleanUpPublicAccounts|.
279 UserList users_;
281 // List of all users that are logged in current session. These point to User
282 // instances in |users_|. Only one of them could be marked as active.
283 UserList logged_in_users_;
285 // A list of all users that are logged in the current session. In contrast to
286 // |logged_in_users|, the order of this list is least recently used so that
287 // the active user should always be the first one in the list.
288 UserList lru_logged_in_users_;
290 private:
291 // Stages of loading user list from preferences. Some methods can have
292 // different behavior depending on stage.
293 enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
295 // Returns a list of users who have logged into this device previously.
296 // Same as GetUsers but used if you need to modify User from that list.
297 UserList& GetUsersAndModify();
299 // Returns the user with the given email address if found in the persistent
300 // list. Returns |NULL| otherwise.
301 const User* FindUserInList(const std::string& user_id) const;
303 // Returns |true| if user with the given id is found in the persistent list.
304 // Returns |false| otherwise. Does not trigger user loading.
305 bool UserExistsInList(const std::string& user_id) const;
307 // Same as FindUserInList but returns non-const pointer to User object.
308 User* FindUserInListAndModify(const std::string& user_id);
310 // Reads user's oauth token status from local state preferences.
311 User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
313 // Read a flag indicating whether online authentication against GAIA should
314 // be enforced during the user's next sign-in from local state preferences.
315 bool LoadForceOnlineSignin(const std::string& user_id) const;
317 // Notifies observers that merge session state had changed.
318 void NotifyMergeSessionStateChanged();
320 // Notifies observers that active user has changed.
321 void NotifyActiveUserChanged(const User* active_user);
323 // Notifies observers that active user_id hash has changed.
324 void NotifyActiveUserHashChanged(const std::string& hash);
326 // Update the global LoginState.
327 void UpdateLoginState();
329 // Insert |user| at the front of the LRU user list.
330 void SetLRUUser(User* user);
332 // Sends metrics in response to a user with gaia account (regular) logging in.
333 void SendGaiaUserLoginMetrics(const std::string& user_id);
335 // Sets account locale for user with id |user_id|.
336 virtual void UpdateUserAccountLocale(const std::string& user_id,
337 const std::string& locale);
339 // Updates user account after locale was resolved.
340 void DoUpdateAccountLocale(const std::string& user_id,
341 scoped_ptr<std::string> resolved_locale);
343 // Removes all user preferences associated with |user_id|.
344 void RemoveKnownUserPrefs(const UserID& user_id);
346 // Indicates stage of loading user from prefs.
347 UserLoadStage user_loading_stage_;
349 // True if SessionStarted() has been called.
350 bool session_started_;
352 // Cached flag of whether currently logged-in user is owner or not.
353 // May be accessed on different threads, requires locking.
354 bool is_current_user_owner_;
355 mutable base::Lock is_current_user_owner_lock_;
357 // Cached flag of whether the currently logged-in user existed before this
358 // login.
359 bool is_current_user_new_;
361 // Cached flag of whether the currently logged-in user is a regular user who
362 // logged in as ephemeral. Storage of persistent information is avoided for
363 // such users by not adding them to the persistent user list, not downloading
364 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
365 // to |false|.
366 bool is_current_user_ephemeral_regular_user_;
368 // Cached flag indicating whether the ephemeral user policy is enabled.
369 // Defaults to |false| if the value has not been read from trusted device
370 // policy yet.
371 bool ephemeral_users_enabled_;
373 // Cached name of device owner. Defaults to empty string if the value has not
374 // been read from trusted device policy yet.
375 std::string owner_email_;
377 ObserverList<UserManager::Observer> observer_list_;
379 // TODO(nkostylev): Merge with session state refactoring CL.
380 ObserverList<UserManager::UserSessionStateObserver>
381 session_state_observer_list_;
383 // Time at which this object was created.
384 base::TimeTicks manager_creation_time_;
386 // ID of the user just added to the session that needs to be activated
387 // as soon as user's profile is loaded.
388 std::string pending_user_switch_;
390 // ID of the user that was active in the previous session.
391 // Preference value is stored here before first user signs in
392 // because pref will be overidden once session restore starts.
393 std::string last_session_active_user_;
394 bool last_session_active_user_initialized_;
396 // TaskRunner for UI thread.
397 scoped_refptr<base::TaskRunner> task_runner_;
399 // TaskRunner for SequencedWorkerPool.
400 scoped_refptr<base::TaskRunner> blocking_task_runner_;
402 base::WeakPtrFactory<UserManagerBase> weak_factory_;
404 DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
407 } // namespace user_manager
409 #endif // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_