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