1 // Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_IMPL_H_
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_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/login_utils.h"
18 #include "chrome/browser/chromeos/login/user.h"
19 #include "chrome/browser/chromeos/login/user_image_manager_impl.h"
20 #include "chrome/browser/chromeos/login/user_manager.h"
21 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #include "chromeos/dbus/session_manager_client.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
30 class ProfileSyncService
;
33 struct DeviceLocalAccount
;
38 class RemoveUserDelegate
;
39 class SessionLengthLimiter
;
40 class UserPolicyStatusManager
;
42 // Implementation of the UserManager.
45 public LoginUtils::Delegate
,
46 public content::NotificationObserver
,
47 public policy::DeviceLocalAccountPolicyService::Observer
{
49 virtual ~UserManagerImpl();
51 // UserManager implementation:
52 virtual void Shutdown() OVERRIDE
;
53 virtual UserImageManager
* GetUserImageManager() OVERRIDE
;
54 virtual const UserList
& GetUsers() const OVERRIDE
;
55 virtual UserList
GetUsersAdmittedForMultiProfile() const OVERRIDE
;
56 virtual const UserList
& GetLoggedInUsers() const OVERRIDE
;
57 virtual const UserList
& GetLRULoggedInUsers() OVERRIDE
;
58 virtual const std::string
& GetOwnerEmail() OVERRIDE
;
59 virtual void UserLoggedIn(const std::string
& email
,
60 const std::string
& username_hash
,
61 bool browser_restart
) OVERRIDE
;
62 virtual void SwitchActiveUser(const std::string
& email
) OVERRIDE
;
63 virtual void RestoreActiveSessions() OVERRIDE
;
64 virtual void SessionStarted() OVERRIDE
;
65 virtual void RemoveUser(const std::string
& email
,
66 RemoveUserDelegate
* delegate
) OVERRIDE
;
67 virtual void RemoveUserFromList(const std::string
& email
) OVERRIDE
;
68 virtual bool IsKnownUser(const std::string
& email
) const OVERRIDE
;
69 virtual const User
* FindUser(const std::string
& email
) const OVERRIDE
;
70 virtual const User
* FindLocallyManagedUser(
71 const string16
& display_name
) const OVERRIDE
;
72 virtual const User
* GetLoggedInUser() const OVERRIDE
;
73 virtual User
* GetLoggedInUser() OVERRIDE
;
74 virtual const User
* GetActiveUser() const OVERRIDE
;
75 virtual User
* GetActiveUser() OVERRIDE
;
76 virtual void SaveUserOAuthStatus(
77 const std::string
& username
,
78 User::OAuthTokenStatus oauth_token_status
) OVERRIDE
;
79 virtual void SaveUserDisplayName(const std::string
& username
,
80 const string16
& display_name
) OVERRIDE
;
81 virtual string16
GetUserDisplayName(
82 const std::string
& username
) const OVERRIDE
;
83 virtual void SaveUserDisplayEmail(const std::string
& username
,
84 const std::string
& display_email
) OVERRIDE
;
85 virtual std::string
GetUserDisplayEmail(
86 const std::string
& username
) const OVERRIDE
;
87 virtual std::string
GetManagedUserSyncId(
88 const std::string
& managed_user_id
) const OVERRIDE
;
89 virtual string16
GetManagerDisplayNameForManagedUser(
90 const std::string
& managed_user_id
) const OVERRIDE
;
91 virtual std::string
GetManagerUserIdForManagedUser(
92 const std::string
& managed_user_id
) const OVERRIDE
;
93 virtual std::string
GetManagerDisplayEmailForManagedUser(
94 const std::string
& managed_user_id
) const OVERRIDE
;
95 virtual bool IsCurrentUserOwner() const OVERRIDE
;
96 virtual bool IsCurrentUserNew() const OVERRIDE
;
97 virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const OVERRIDE
;
98 virtual bool CanCurrentUserLock() const OVERRIDE
;
99 virtual bool IsUserLoggedIn() const OVERRIDE
;
100 virtual bool IsLoggedInAsRegularUser() const OVERRIDE
;
101 virtual bool IsLoggedInAsDemoUser() const OVERRIDE
;
102 virtual bool IsLoggedInAsPublicAccount() const OVERRIDE
;
103 virtual bool IsLoggedInAsGuest() const OVERRIDE
;
104 virtual bool IsLoggedInAsLocallyManagedUser() const OVERRIDE
;
105 virtual bool IsLoggedInAsKioskApp() const OVERRIDE
;
106 virtual bool IsLoggedInAsStub() const OVERRIDE
;
107 virtual bool IsSessionStarted() const OVERRIDE
;
108 virtual bool UserSessionsRestored() const OVERRIDE
;
109 virtual bool HasBrowserRestarted() const OVERRIDE
;
110 virtual bool IsUserNonCryptohomeDataEphemeral(
111 const std::string
& email
) const OVERRIDE
;
112 virtual void AddObserver(UserManager::Observer
* obs
) OVERRIDE
;
113 virtual void RemoveObserver(UserManager::Observer
* obs
) OVERRIDE
;
114 virtual void AddSessionStateObserver(
115 UserManager::UserSessionStateObserver
* obs
) OVERRIDE
;
116 virtual void RemoveSessionStateObserver(
117 UserManager::UserSessionStateObserver
* obs
) OVERRIDE
;
118 virtual void NotifyLocalStateChanged() OVERRIDE
;
119 virtual const User
* CreateLocallyManagedUserRecord(
120 const std::string
& manager_id
,
121 const std::string
& local_user_id
,
122 const std::string
& sync_user_id
,
123 const string16
& display_name
) OVERRIDE
;
124 virtual std::string
GenerateUniqueLocallyManagedUserId() OVERRIDE
;
125 virtual void StartLocallyManagedUserCreationTransaction(
126 const string16
& display_name
) OVERRIDE
;
127 virtual void SetLocallyManagedUserCreationTransactionUserId(
128 const std::string
& email
) OVERRIDE
;
129 virtual void CommitLocallyManagedUserCreationTransaction() OVERRIDE
;
131 virtual UserFlow
* GetCurrentUserFlow() const OVERRIDE
;
132 virtual UserFlow
* GetUserFlow(const std::string
& email
) const OVERRIDE
;
133 virtual void SetUserFlow(const std::string
& email
, UserFlow
* flow
) OVERRIDE
;
134 virtual void ResetUserFlow(const std::string
& email
) OVERRIDE
;
135 virtual bool GetAppModeChromeClientOAuthInfo(
136 std::string
* chrome_client_id
,
137 std::string
* chrome_client_secret
) OVERRIDE
;
138 virtual void SetAppModeChromeClientOAuthInfo(
139 const std::string
& chrome_client_id
,
140 const std::string
& chrome_client_secret
) OVERRIDE
;
141 virtual bool AreLocallyManagedUsersAllowed() const OVERRIDE
;
142 virtual base::FilePath
GetUserProfileDir(
143 const std::string
& email
) const OVERRIDE
;
145 // content::NotificationObserver implementation.
146 virtual void Observe(int type
,
147 const content::NotificationSource
& source
,
148 const content::NotificationDetails
& details
) OVERRIDE
;
150 // policy::DeviceLocalAccountPolicyService::Observer implementation.
151 virtual void OnPolicyUpdated(const std::string
& user_id
) OVERRIDE
;
152 virtual void OnDeviceLocalAccountsChanged() OVERRIDE
;
155 friend class UserManager
;
156 friend class WallpaperManager
;
157 friend class UserManagerTest
;
158 friend class WallpaperManagerTest
;
162 // LoginUtils::Delegate implementation:
163 // Used when restoring user sessions after crash.
164 virtual void OnProfilePrepared(Profile
* profile
) OVERRIDE
;
166 // Loads |users_| from Local State if the list has not been loaded yet.
167 // Subsequent calls have no effect. Must be called on the UI thread.
168 void EnsureUsersLoaded();
170 // Retrieves trusted device policies and removes users from the persistent
171 // list if ephemeral users are enabled. Schedules a callback to itself if
172 // trusted device policies are not yet available.
173 void RetrieveTrustedDevicePolicies();
175 // Returns true if trusted device policies have successfully been retrieved
176 // and ephemeral users are enabled.
177 bool AreEphemeralUsersEnabled() const;
179 // Returns a list of users who have logged into this device previously.
180 // Same as GetUsers but used if you need to modify User from that list.
181 UserList
& GetUsersAndModify();
183 // Returns the user with the given email address if found in the persistent
184 // list or currently logged in as ephemeral. Returns |NULL| otherwise.
185 // Same as FindUser but returns non-const pointer to User object.
186 User
* FindUserAndModify(const std::string
& email
);
188 // Returns the user with the given email address if found in the persistent
189 // list. Returns |NULL| otherwise.
190 const User
* FindUserInList(const std::string
& email
) const;
192 // Same as FindUserInList but returns non-const pointer to User object.
193 User
* FindUserInListAndModify(const std::string
& email
);
195 // Indicates that a user just logged in as guest.
196 void GuestUserLoggedIn();
198 // Indicates that a regular user just logged in.
199 void RegularUserLoggedIn(const std::string
& email
);
201 // Indicates that a regular user just logged in as ephemeral.
202 void RegularUserLoggedInAsEphemeral(const std::string
& email
);
204 // Indicates that a locally managed user just logged in.
205 void LocallyManagedUserLoggedIn(const std::string
& username
);
207 // Indicates that a user just logged into a public session.
208 void PublicAccountUserLoggedIn(User
* user
);
210 // Indicates that a kiosk app robot just logged in.
211 void KioskAppLoggedIn(const std::string
& app_id
);
213 // Indicates that a user just logged into a retail mode session.
214 void RetailModeUserLoggedIn();
216 // Notifies that user has logged in.
217 // Sends NOTIFICATION_LOGIN_USER_CHANGED notification.
218 void NotifyOnLogin();
220 // Reads user's oauth token status from local state preferences.
221 User::OAuthTokenStatus
LoadUserOAuthStatus(const std::string
& username
) const;
223 void SetCurrentUserIsOwner(bool is_current_user_owner
);
225 // Updates current user ownership on UI thread.
226 void UpdateOwnership();
228 // Removes data stored or cached outside the user's cryptohome (wallpaper,
229 // avatar, OAuth token status, display name, display email).
230 void RemoveNonCryptohomeData(const std::string
& email
);
232 // Removes a regular or locally managed user from the user list.
233 // Returns the user if found or NULL otherwise.
234 // Also removes the user from the persistent user list.
235 User
* RemoveRegularOrLocallyManagedUserFromList(const std::string
& username
);
237 // If data for a public account is marked as pending removal and the user is
238 // no longer logged into that account, removes the data.
239 void CleanUpPublicAccountNonCryptohomeDataPendingRemoval();
241 // Removes data belonging to public accounts that are no longer found on the
242 // user list. If the user is currently logged into one of these accounts, the
243 // data for that account is not removed immediately but marked as pending
244 // removal after logout.
245 void CleanUpPublicAccountNonCryptohomeData(
246 const std::vector
<std::string
>& old_public_accounts
);
248 // Replaces the list of public accounts with those found in
249 // |device_local_accounts|. Ensures that data belonging to accounts no longer
250 // on the list is removed. Returns |true| if the list has changed.
251 // Public accounts are defined by policy. This method is called whenever an
252 // updated list of public accounts is received from policy.
253 bool UpdateAndCleanUpPublicAccounts(
254 const std::vector
<policy::DeviceLocalAccount
>& device_local_accounts
);
256 // Updates the display name for public account |username| from policy settings
257 // associated with that username.
258 void UpdatePublicAccountDisplayName(const std::string
& username
);
260 // Notifies the UI about a change to the user list.
261 void NotifyUserListChanged();
263 // Notifies observers that merge session state had changed.
264 void NotifyMergeSessionStateChanged();
266 // Notifies observers that active user has changed.
267 void NotifyActiveUserChanged(const User
* active_user
);
269 // Notifies observers that active user_id hash has changed.
270 void NotifyActiveUserHashChanged(const std::string
& hash
);
272 // Notifies observers that user pending sessions restore has finished.
273 void NotifyPendingUserSessionsRestoreFinished();
275 // Returns true if there is non-committed user creation transaction.
276 bool HasFailedLocallyManagedUserCreationTransaction();
278 // Attempts to clean up data that could be left from failed user creation.
279 void RollbackLocallyManagedUserCreationTransaction();
281 // Lazily creates default user flow.
282 UserFlow
* GetDefaultUserFlow() const;
284 // Update the global LoginState.
285 void UpdateLoginState();
287 // Insert |user| at the front of the LRU user list..
288 void SetLRUUser(User
* user
);
290 // Callback to process RetrieveActiveSessions() request results.
291 void OnRestoreActiveSessions(
292 const SessionManagerClient::ActiveSessionsMap
& sessions
,
295 // Called by OnRestoreActiveSessions() when there're user sessions in
296 // |pending_user_sessions_| that has to be restored one by one.
297 // Also called after first user session from that list is restored and so on.
298 // Process continues till |pending_user_sessions_| map is not empty.
299 void RestorePendingUserSessions();
301 // Sends metrics in response to a regular user logging in.
302 void SendRegularUserLoginMetrics(const std::string
& email
);
304 // Interface to the signed settings store.
305 CrosSettings
* cros_settings_
;
307 // Interface to device-local account definitions and associated policy.
308 policy::DeviceLocalAccountPolicyService
* device_local_account_policy_service_
;
310 // True if users have been loaded from prefs already.
313 // List of all known users. User instances are owned by |this|. Regular users
314 // are removed by |RemoveUserFromList|, public accounts by
315 // |UpdateAndCleanUpPublicAccounts|.
318 // List of all users that are logged in current session. These point to User
319 // instances in |users_|. Only one of them could be marked as active.
320 UserList logged_in_users_
;
322 // A list of all users that are logged in the current session. In contrast to
323 // |logged_in_users|, the order of this list is least recently used so that
324 // the active user should always be the first one in the list.
325 UserList lru_logged_in_users_
;
327 // The list which gets reported when the |lru_logged_in_users_| list is empty.
328 UserList temp_single_logged_in_users_
;
330 // The logged-in user that is currently active in current session.
331 // NULL until a user has logged in, then points to one
332 // of the User instances in |users_|, the |guest_user_| instance or an
333 // ephemeral user instance.
336 // True if SessionStarted() has been called.
337 bool session_started_
;
339 // True is user sessions has been restored after crash.
340 // On a normal boot then login into user sessions this will be false.
341 bool user_sessions_restored_
;
343 // Cached flag of whether currently logged-in user is owner or not.
344 // May be accessed on different threads, requires locking.
345 bool is_current_user_owner_
;
346 mutable base::Lock is_current_user_owner_lock_
;
348 // Cached flag of whether the currently logged-in user existed before this
350 bool is_current_user_new_
;
352 // Cached flag of whether the currently logged-in user is a regular user who
353 // logged in as ephemeral. Storage of persistent information is avoided for
354 // such users by not adding them to the persistent user list, not downloading
355 // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
357 bool is_current_user_ephemeral_regular_user_
;
359 // Cached flag indicating whether the ephemeral user policy is enabled.
360 // Defaults to |false| if the value has not been read from trusted device
362 bool ephemeral_users_enabled_
;
364 // Cached name of device owner. Defaults to empty string if the value has not
365 // been read from trusted device policy yet.
366 std::string owner_email_
;
368 // Chrome oauth client id and secret - override values for kiosk mode.
369 std::string chrome_client_id_
;
370 std::string chrome_client_secret_
;
372 content::NotificationRegistrar registrar_
;
374 ObserverList
<UserManager::Observer
> observer_list_
;
376 // TODO(nkostylev): Merge with session state refactoring CL.
377 ObserverList
<UserManager::UserSessionStateObserver
>
378 session_state_observer_list_
;
380 // User avatar manager.
381 scoped_ptr
<UserImageManagerImpl
> user_image_manager_
;
383 // Session length limiter.
384 scoped_ptr
<SessionLengthLimiter
> session_length_limiter_
;
386 typedef std::map
<std::string
, UserFlow
*> FlowMap
;
388 // Lazy-initialized default flow.
389 mutable scoped_ptr
<UserFlow
> default_flow_
;
391 // Specific flows by user e-mail. Keys should be canonicalized before
393 FlowMap specific_flows_
;
395 // User sessions that have to be restored after browser crash.
396 // [user_id] > [user_id_hash]
397 SessionManagerClient::ActiveSessionsMap pending_user_sessions_
;
399 // Time at which this object was created.
400 base::TimeTicks manager_creation_time_
;
402 scoped_ptr
<UserPolicyStatusManager
> user_policy_status_manager_
;
404 DISALLOW_COPY_AND_ASSIGN(UserManagerImpl
);
407 } // namespace chromeos
409 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_IMPL_H_