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