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_IMPL_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_USER_MANAGER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/containers/hash_tables.h" | |
14 #include "base/memory/linked_ptr.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/observer_list.h" | |
17 #include "base/synchronization/lock.h" | |
18 #include "base/time/time.h" | |
19 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.h" | |
20 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller_dele
gate.h" | |
21 #include "chrome/browser/chromeos/login/users/user_manager.h" | |
22 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | |
23 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h" | |
24 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h" | |
25 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
26 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
27 #include "components/user_manager/user.h" | |
28 #include "content/public/browser/notification_observer.h" | |
29 #include "content/public/browser/notification_registrar.h" | |
30 | |
31 class PrefService; | |
32 class ProfileSyncService; | |
33 | |
34 namespace policy { | |
35 struct DeviceLocalAccount; | |
36 } | |
37 | |
38 namespace chromeos { | |
39 | |
40 class MultiProfileUserController; | |
41 class RemoveUserDelegate; | |
42 class SupervisedUserManagerImpl; | |
43 class SessionLengthLimiter; | |
44 | |
45 // Implementation of the UserManager. | |
46 class UserManagerImpl | |
47 : public UserManager, | |
48 public content::NotificationObserver, | |
49 public policy::CloudExternalDataPolicyObserver::Delegate, | |
50 public policy::DeviceLocalAccountPolicyService::Observer, | |
51 public MultiProfileUserControllerDelegate { | |
52 public: | |
53 virtual ~UserManagerImpl(); | |
54 | |
55 // UserManager implementation: | |
56 virtual void Shutdown() OVERRIDE; | |
57 virtual MultiProfileUserController* GetMultiProfileUserController() OVERRIDE; | |
58 virtual UserImageManager* GetUserImageManager( | |
59 const std::string& user_id) OVERRIDE; | |
60 virtual SupervisedUserManager* GetSupervisedUserManager() OVERRIDE; | |
61 virtual const user_manager::UserList& GetUsers() const OVERRIDE; | |
62 virtual user_manager::UserList GetUsersAdmittedForMultiProfile() | |
63 const OVERRIDE; | |
64 virtual const user_manager::UserList& GetLoggedInUsers() const OVERRIDE; | |
65 virtual const user_manager::UserList& GetLRULoggedInUsers() OVERRIDE; | |
66 virtual user_manager::UserList GetUnlockUsers() const OVERRIDE; | |
67 virtual const std::string& GetOwnerEmail() OVERRIDE; | |
68 virtual void UserLoggedIn(const std::string& user_id, | |
69 const std::string& user_id_hash, | |
70 bool browser_restart) OVERRIDE; | |
71 virtual void SwitchActiveUser(const std::string& user_id) OVERRIDE; | |
72 virtual void SessionStarted() OVERRIDE; | |
73 virtual void RemoveUser(const std::string& user_id, | |
74 RemoveUserDelegate* delegate) OVERRIDE; | |
75 virtual void RemoveUserFromList(const std::string& user_id) OVERRIDE; | |
76 virtual bool IsKnownUser(const std::string& user_id) const OVERRIDE; | |
77 virtual const user_manager::User* FindUser( | |
78 const std::string& user_id) const OVERRIDE; | |
79 virtual user_manager::User* FindUserAndModify( | |
80 const std::string& user_id) OVERRIDE; | |
81 virtual const user_manager::User* GetLoggedInUser() const OVERRIDE; | |
82 virtual user_manager::User* GetLoggedInUser() OVERRIDE; | |
83 virtual const user_manager::User* GetActiveUser() const OVERRIDE; | |
84 virtual user_manager::User* GetActiveUser() OVERRIDE; | |
85 virtual const user_manager::User* GetPrimaryUser() const OVERRIDE; | |
86 virtual void SaveUserOAuthStatus( | |
87 const std::string& user_id, | |
88 user_manager::User::OAuthTokenStatus oauth_token_status) OVERRIDE; | |
89 virtual void SaveForceOnlineSignin(const std::string& user_id, | |
90 bool force_online_signin) OVERRIDE; | |
91 virtual void SaveUserDisplayName(const std::string& user_id, | |
92 const base::string16& display_name) OVERRIDE; | |
93 virtual base::string16 GetUserDisplayName( | |
94 const std::string& user_id) const OVERRIDE; | |
95 virtual void SaveUserDisplayEmail(const std::string& user_id, | |
96 const std::string& display_email) OVERRIDE; | |
97 virtual std::string GetUserDisplayEmail( | |
98 const std::string& user_id) const OVERRIDE; | |
99 virtual void UpdateUserAccountData( | |
100 const std::string& user_id, | |
101 const UserAccountData& account_data) OVERRIDE; | |
102 virtual bool IsCurrentUserOwner() const OVERRIDE; | |
103 virtual bool IsCurrentUserNew() const OVERRIDE; | |
104 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const OVERRIDE; | |
105 virtual bool CanCurrentUserLock() const OVERRIDE; | |
106 virtual bool IsUserLoggedIn() const OVERRIDE; | |
107 virtual bool IsLoggedInAsRegularUser() const OVERRIDE; | |
108 virtual bool IsLoggedInAsDemoUser() const OVERRIDE; | |
109 virtual bool IsLoggedInAsPublicAccount() const OVERRIDE; | |
110 virtual bool IsLoggedInAsGuest() const OVERRIDE; | |
111 virtual bool IsLoggedInAsSupervisedUser() const OVERRIDE; | |
112 virtual bool IsLoggedInAsKioskApp() const OVERRIDE; | |
113 virtual bool IsLoggedInAsStub() const OVERRIDE; | |
114 virtual bool IsSessionStarted() const OVERRIDE; | |
115 virtual bool IsUserNonCryptohomeDataEphemeral( | |
116 const std::string& user_id) const OVERRIDE; | |
117 virtual void AddObserver(UserManager::Observer* obs) OVERRIDE; | |
118 virtual void RemoveObserver(UserManager::Observer* obs) OVERRIDE; | |
119 virtual void AddSessionStateObserver( | |
120 UserManager::UserSessionStateObserver* obs) OVERRIDE; | |
121 virtual void RemoveSessionStateObserver( | |
122 UserManager::UserSessionStateObserver* obs) OVERRIDE; | |
123 virtual void NotifyLocalStateChanged() OVERRIDE; | |
124 | |
125 virtual UserFlow* GetCurrentUserFlow() const OVERRIDE; | |
126 virtual UserFlow* GetUserFlow(const std::string& user_id) const OVERRIDE; | |
127 virtual void SetUserFlow(const std::string& user_id, UserFlow* flow) OVERRIDE; | |
128 virtual void ResetUserFlow(const std::string& user_id) OVERRIDE; | |
129 virtual bool AreSupervisedUsersAllowed() const OVERRIDE; | |
130 | |
131 // content::NotificationObserver implementation. | |
132 virtual void Observe(int type, | |
133 const content::NotificationSource& source, | |
134 const content::NotificationDetails& details) OVERRIDE; | |
135 | |
136 // policy::CloudExternalDataPolicyObserver::Delegate: | |
137 virtual void OnExternalDataSet(const std::string& policy, | |
138 const std::string& user_id) OVERRIDE; | |
139 virtual void OnExternalDataCleared(const std::string& policy, | |
140 const std::string& user_id) OVERRIDE; | |
141 virtual void OnExternalDataFetched(const std::string& policy, | |
142 const std::string& user_id, | |
143 scoped_ptr<std::string> data) OVERRIDE; | |
144 | |
145 // policy::DeviceLocalAccountPolicyService::Observer implementation. | |
146 virtual void OnPolicyUpdated(const std::string& user_id) OVERRIDE; | |
147 virtual void OnDeviceLocalAccountsChanged() OVERRIDE; | |
148 | |
149 void StopPolicyObserverForTesting(); | |
150 | |
151 private: | |
152 friend class SupervisedUserManagerImpl; | |
153 friend class UserManager; | |
154 friend class WallpaperManager; | |
155 friend class UserManagerTest; | |
156 friend class WallpaperManagerTest; | |
157 | |
158 typedef base::hash_map<std::string, | |
159 linked_ptr<UserImageManager> > UserImageManagerMap; | |
160 | |
161 // Stages of loading user list from preferences. Some methods can have | |
162 // different behavior depending on stage. | |
163 enum UserLoadStage { | |
164 STAGE_NOT_LOADED = 0, | |
165 STAGE_LOADING, | |
166 STAGE_LOADED | |
167 }; | |
168 | |
169 UserManagerImpl(); | |
170 | |
171 // Loads |users_| from Local State if the list has not been loaded yet. | |
172 // Subsequent calls have no effect. Must be called on the UI thread. | |
173 void EnsureUsersLoaded(); | |
174 | |
175 // Retrieves trusted device policies and removes users from the persistent | |
176 // list if ephemeral users are enabled. Schedules a callback to itself if | |
177 // trusted device policies are not yet available. | |
178 void RetrieveTrustedDevicePolicies(); | |
179 | |
180 // Returns true if trusted device policies have successfully been retrieved | |
181 // and ephemeral users are enabled. | |
182 bool AreEphemeralUsersEnabled() const; | |
183 | |
184 // Returns a list of users who have logged into this device previously. | |
185 // Same as GetUsers but used if you need to modify User from that list. | |
186 user_manager::UserList& GetUsersAndModify(); | |
187 | |
188 // Returns the user with the given email address if found in the persistent | |
189 // list. Returns |NULL| otherwise. | |
190 const user_manager::User* FindUserInList(const std::string& user_id) const; | |
191 | |
192 // Returns |true| if user with the given id is found in the persistent list. | |
193 // Returns |false| otherwise. Does not trigger user loading. | |
194 const bool UserExistsInList(const std::string& user_id) const; | |
195 | |
196 // Same as FindUserInList but returns non-const pointer to User object. | |
197 user_manager::User* FindUserInListAndModify(const std::string& user_id); | |
198 | |
199 // Indicates that a user just logged in as guest. | |
200 void GuestUserLoggedIn(); | |
201 | |
202 // Indicates that a regular user just logged in. | |
203 void RegularUserLoggedIn(const std::string& user_id); | |
204 | |
205 // Indicates that a regular user just logged in as ephemeral. | |
206 void RegularUserLoggedInAsEphemeral(const std::string& user_id); | |
207 | |
208 // Indicates that a supervised user just logged in. | |
209 void SupervisedUserLoggedIn(const std::string& user_id); | |
210 | |
211 // Indicates that a user just logged into a public session. | |
212 void PublicAccountUserLoggedIn(user_manager::User* user); | |
213 | |
214 // Indicates that a kiosk app robot just logged in. | |
215 void KioskAppLoggedIn(const std::string& app_id); | |
216 | |
217 // Indicates that the demo account has just logged in. | |
218 void DemoAccountLoggedIn(); | |
219 | |
220 // Indicates that a user just logged into a retail mode session. | |
221 void RetailModeUserLoggedIn(); | |
222 | |
223 // Notifies that user has logged in. | |
224 // Sends NOTIFICATION_LOGIN_USER_CHANGED notification. | |
225 void NotifyOnLogin(); | |
226 | |
227 // Reads user's oauth token status from local state preferences. | |
228 user_manager::User::OAuthTokenStatus LoadUserOAuthStatus( | |
229 const std::string& user_id) const; | |
230 | |
231 // Read a flag indicating whether online authentication against GAIA should | |
232 // be enforced during the user's next sign-in from local state preferences. | |
233 bool LoadForceOnlineSignin(const std::string& user_id) const; | |
234 | |
235 void SetCurrentUserIsOwner(bool is_current_user_owner); | |
236 | |
237 // Updates current user ownership on UI thread. | |
238 void UpdateOwnership(); | |
239 | |
240 // Removes data stored or cached outside the user's cryptohome (wallpaper, | |
241 // avatar, OAuth token status, display name, display email). | |
242 void RemoveNonCryptohomeData(const std::string& user_id); | |
243 | |
244 // Removes a regular or supervised user from the user list. | |
245 // Returns the user if found or NULL otherwise. | |
246 // Also removes the user from the persistent user list. | |
247 user_manager::User* RemoveRegularOrSupervisedUserFromList( | |
248 const std::string& user_id); | |
249 | |
250 // If data for a public account is marked as pending removal and the user is | |
251 // no longer logged into that account, removes the data. | |
252 void CleanUpPublicAccountNonCryptohomeDataPendingRemoval(); | |
253 | |
254 // Removes data belonging to public accounts that are no longer found on the | |
255 // user list. If the user is currently logged into one of these accounts, the | |
256 // data for that account is not removed immediately but marked as pending | |
257 // removal after logout. | |
258 void CleanUpPublicAccountNonCryptohomeData( | |
259 const std::vector<std::string>& old_public_accounts); | |
260 | |
261 // Replaces the list of public accounts with those found in | |
262 // |device_local_accounts|. Ensures that data belonging to accounts no longer | |
263 // on the list is removed. Returns |true| if the list has changed. | |
264 // Public accounts are defined by policy. This method is called whenever an | |
265 // updated list of public accounts is received from policy. | |
266 bool UpdateAndCleanUpPublicAccounts( | |
267 const std::vector<policy::DeviceLocalAccount>& device_local_accounts); | |
268 | |
269 // Updates the display name for public account |username| from policy settings | |
270 // associated with that username. | |
271 void UpdatePublicAccountDisplayName(const std::string& user_id); | |
272 | |
273 // Notifies the UI about a change to the user list. | |
274 void NotifyUserListChanged(); | |
275 | |
276 // Notifies observers that merge session state had changed. | |
277 void NotifyMergeSessionStateChanged(); | |
278 | |
279 // Notifies observers that active user has changed. | |
280 void NotifyActiveUserChanged(const user_manager::User* active_user); | |
281 | |
282 // Notifies observers that another user was added to the session. | |
283 void NotifyUserAddedToSession(const user_manager::User* added_user); | |
284 | |
285 // Notifies observers that active user_id hash has changed. | |
286 void NotifyActiveUserHashChanged(const std::string& hash); | |
287 | |
288 // Lazily creates default user flow. | |
289 UserFlow* GetDefaultUserFlow() const; | |
290 | |
291 // Update the global LoginState. | |
292 void UpdateLoginState(); | |
293 | |
294 // Insert |user| at the front of the LRU user list. | |
295 void SetLRUUser(user_manager::User* user); | |
296 | |
297 // Adds |user| to users list, and adds it to front of LRU list. It is assumed | |
298 // that there is no user with same id. | |
299 void AddUserRecord(user_manager::User* user); | |
300 | |
301 // Sends metrics in response to a regular user logging in. | |
302 void SendRegularUserLoginMetrics(const std::string& user_id); | |
303 | |
304 // Implementation for RemoveUser method. This is an asynchronous part of the | |
305 // method, that verifies that owner will not get deleted, and calls | |
306 // |RemoveNonOwnerUserInternal|. | |
307 void RemoveUserInternal(const std::string& user_email, | |
308 RemoveUserDelegate* delegate); | |
309 | |
310 // Implementation for RemoveUser method. It is synchronous. It is called from | |
311 // RemoveUserInternal after owner check. | |
312 void RemoveNonOwnerUserInternal(const std::string& user_email, | |
313 RemoveUserDelegate* delegate); | |
314 | |
315 // MultiProfileUserControllerDelegate implementation: | |
316 virtual void OnUserNotAllowed(const std::string& user_email) OVERRIDE; | |
317 | |
318 // Sets account locale for user with id |user_id|. | |
319 virtual void UpdateUserAccountLocale(const std::string& user_id, | |
320 const std::string& locale); | |
321 | |
322 // Updates user account after locale was resolved. | |
323 void DoUpdateAccountLocale(const std::string& user_id, | |
324 const std::string& resolved_locale); | |
325 | |
326 // Update the number of users. | |
327 void UpdateNumberOfUsers(); | |
328 | |
329 // A wrapper around C++ delete operator. Deletes |user|, and when |user| | |
330 // equals to active_user_, active_user_ is reset to NULL. | |
331 void DeleteUser(user_manager::User* user); | |
332 | |
333 // Interface to the signed settings store. | |
334 CrosSettings* cros_settings_; | |
335 | |
336 // Interface to device-local account definitions and associated policy. | |
337 policy::DeviceLocalAccountPolicyService* device_local_account_policy_service_; | |
338 | |
339 // Indicates stage of loading user from prefs. | |
340 UserLoadStage user_loading_stage_; | |
341 | |
342 // List of all known users. User instances are owned by |this|. Regular users | |
343 // are removed by |RemoveUserFromList|, public accounts by | |
344 // |UpdateAndCleanUpPublicAccounts|. | |
345 user_manager::UserList users_; | |
346 | |
347 // List of all users that are logged in current session. These point to User | |
348 // instances in |users_|. Only one of them could be marked as active. | |
349 user_manager::UserList logged_in_users_; | |
350 | |
351 // A list of all users that are logged in the current session. In contrast to | |
352 // |logged_in_users|, the order of this list is least recently used so that | |
353 // the active user should always be the first one in the list. | |
354 user_manager::UserList lru_logged_in_users_; | |
355 | |
356 // The list which gets reported when the |lru_logged_in_users_| list is empty. | |
357 user_manager::UserList temp_single_logged_in_users_; | |
358 | |
359 // The logged-in user that is currently active in current session. | |
360 // NULL until a user has logged in, then points to one | |
361 // of the User instances in |users_|, the |guest_user_| instance or an | |
362 // ephemeral user instance. | |
363 user_manager::User* active_user_; | |
364 | |
365 // The primary user of the current session. It is recorded for the first | |
366 // signed-in user and does not change thereafter. | |
367 user_manager::User* primary_user_; | |
368 | |
369 // True if SessionStarted() has been called. | |
370 bool session_started_; | |
371 | |
372 // Cached flag of whether currently logged-in user is owner or not. | |
373 // May be accessed on different threads, requires locking. | |
374 bool is_current_user_owner_; | |
375 mutable base::Lock is_current_user_owner_lock_; | |
376 | |
377 // Cached flag of whether the currently logged-in user existed before this | |
378 // login. | |
379 bool is_current_user_new_; | |
380 | |
381 // Cached flag of whether the currently logged-in user is a regular user who | |
382 // logged in as ephemeral. Storage of persistent information is avoided for | |
383 // such users by not adding them to the persistent user list, not downloading | |
384 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults | |
385 // to |false|. | |
386 bool is_current_user_ephemeral_regular_user_; | |
387 | |
388 // Cached flag indicating whether the ephemeral user policy is enabled. | |
389 // Defaults to |false| if the value has not been read from trusted device | |
390 // policy yet. | |
391 bool ephemeral_users_enabled_; | |
392 | |
393 // Cached name of device owner. Defaults to empty string if the value has not | |
394 // been read from trusted device policy yet. | |
395 std::string owner_email_; | |
396 | |
397 content::NotificationRegistrar registrar_; | |
398 | |
399 ObserverList<UserManager::Observer> observer_list_; | |
400 | |
401 // TODO(nkostylev): Merge with session state refactoring CL. | |
402 ObserverList<UserManager::UserSessionStateObserver> | |
403 session_state_observer_list_; | |
404 | |
405 // User avatar managers. | |
406 UserImageManagerMap user_image_managers_; | |
407 | |
408 // Supervised user manager. | |
409 scoped_ptr<SupervisedUserManagerImpl> supervised_user_manager_; | |
410 | |
411 // Session length limiter. | |
412 scoped_ptr<SessionLengthLimiter> session_length_limiter_; | |
413 | |
414 typedef std::map<std::string, UserFlow*> FlowMap; | |
415 | |
416 // Lazy-initialized default flow. | |
417 mutable scoped_ptr<UserFlow> default_flow_; | |
418 | |
419 // Specific flows by user e-mail. Keys should be canonicalized before | |
420 // access. | |
421 FlowMap specific_flows_; | |
422 | |
423 // Time at which this object was created. | |
424 base::TimeTicks manager_creation_time_; | |
425 | |
426 scoped_ptr<CrosSettings::ObserverSubscription> | |
427 local_accounts_subscription_; | |
428 | |
429 scoped_ptr<MultiProfileUserController> multi_profile_user_controller_; | |
430 | |
431 // Observer for the policy that can be used to manage user images. | |
432 scoped_ptr<policy::CloudExternalDataPolicyObserver> avatar_policy_observer_; | |
433 | |
434 // Observer for the policy that can be used to manage wallpapers. | |
435 scoped_ptr<policy::CloudExternalDataPolicyObserver> | |
436 wallpaper_policy_observer_; | |
437 | |
438 // ID of the user just added to the session that needs to be activated | |
439 // as soon as user's profile is loaded. | |
440 std::string pending_user_switch_; | |
441 | |
442 DISALLOW_COPY_AND_ASSIGN(UserManagerImpl); | |
443 }; | |
444 | |
445 } // namespace chromeos | |
446 | |
447 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_USER_MANAGER_IMPL_H_ | |
OLD | NEW |