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_CHROME_USER_MANAGER_IMPL_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_CHROME_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/memory/weak_ptr.h"
17#include "base/observer_list.h"
18#include "base/synchronization/lock.h"
19#include "base/time/time.h"
20#include "chrome/browser/chromeos/login/user_flow.h"
21#include "chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.h"
22#include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
23#include "chrome/browser/chromeos/login/users/multi_profile_user_controller_delegate.h"
24#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
25#include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
26#include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
27#include "chrome/browser/chromeos/settings/cros_settings.h"
28#include "chrome/browser/chromeos/settings/device_settings_service.h"
29#include "components/user_manager/user.h"
30#include "content/public/browser/notification_observer.h"
31#include "content/public/browser/notification_registrar.h"
32
33class PrefRegistrySimple;
34class PrefService;
35class ProfileSyncService;
36
37namespace policy {
38struct DeviceLocalAccount;
39}
40
41namespace user_manager {
42class RemoveUserDelegate;
43}
44
45namespace chromeos {
46
47class MultiProfileUserController;
48class SupervisedUserManagerImpl;
49class SessionLengthLimiter;
50
51// Chrome specific implementation of the UserManager.
52class ChromeUserManagerImpl
53    : public ChromeUserManager,
54      public content::NotificationObserver,
55      public policy::CloudExternalDataPolicyObserver::Delegate,
56      public policy::DeviceLocalAccountPolicyService::Observer,
57      public MultiProfileUserControllerDelegate {
58 public:
59  virtual ~ChromeUserManagerImpl();
60
61  // Creates ChromeUserManagerImpl instance.
62  static scoped_ptr<ChromeUserManager> CreateChromeUserManager();
63
64  // Registers user manager preferences.
65  static void RegisterPrefs(PrefRegistrySimple* registry);
66
67  // ChromeUserManager implementation:
68  virtual MultiProfileUserController* GetMultiProfileUserController() OVERRIDE;
69  virtual UserImageManager* GetUserImageManager(
70      const std::string& user_id) OVERRIDE;
71  virtual SupervisedUserManager* GetSupervisedUserManager() OVERRIDE;
72  virtual UserFlow* GetCurrentUserFlow() const OVERRIDE;
73  virtual UserFlow* GetUserFlow(const std::string& user_id) const OVERRIDE;
74  virtual void SetUserFlow(const std::string& user_id, UserFlow* flow) OVERRIDE;
75  virtual void ResetUserFlow(const std::string& user_id) OVERRIDE;
76
77  // UserManager implementation:
78  virtual void Shutdown() OVERRIDE;
79  virtual user_manager::UserList GetUsersAdmittedForMultiProfile()
80      const OVERRIDE;
81  virtual user_manager::UserList GetUnlockUsers() const OVERRIDE;
82  virtual void SessionStarted() OVERRIDE;
83  virtual void SaveUserOAuthStatus(
84      const std::string& user_id,
85      user_manager::User::OAuthTokenStatus oauth_token_status) OVERRIDE;
86  virtual void SaveUserDisplayName(const std::string& user_id,
87                                   const base::string16& display_name) OVERRIDE;
88  virtual bool CanCurrentUserLock() const OVERRIDE;
89  virtual bool IsUserNonCryptohomeDataEphemeral(
90      const std::string& user_id) const OVERRIDE;
91  virtual bool AreSupervisedUsersAllowed() const OVERRIDE;
92
93  // content::NotificationObserver implementation.
94  virtual void Observe(int type,
95                       const content::NotificationSource& source,
96                       const content::NotificationDetails& details) OVERRIDE;
97
98  // policy::CloudExternalDataPolicyObserver::Delegate:
99  virtual void OnExternalDataSet(const std::string& policy,
100                                 const std::string& user_id) OVERRIDE;
101  virtual void OnExternalDataCleared(const std::string& policy,
102                                     const std::string& user_id) OVERRIDE;
103  virtual void OnExternalDataFetched(const std::string& policy,
104                                     const std::string& user_id,
105                                     scoped_ptr<std::string> data) OVERRIDE;
106
107  // policy::DeviceLocalAccountPolicyService::Observer implementation.
108  virtual void OnPolicyUpdated(const std::string& user_id) OVERRIDE;
109  virtual void OnDeviceLocalAccountsChanged() OVERRIDE;
110
111  void StopPolicyObserverForTesting();
112
113 protected:
114  // UserManagerBase implementation:
115  virtual bool AreEphemeralUsersEnabled() const OVERRIDE;
116  virtual const std::string& GetApplicationLocale() const OVERRIDE;
117  virtual PrefService* GetLocalState() const OVERRIDE;
118  virtual void HandleUserOAuthTokenStatusChange(
119      const std::string& user_id,
120      user_manager::User::OAuthTokenStatus status) const OVERRIDE;
121  virtual bool IsEnterpriseManaged() const OVERRIDE;
122  virtual void LoadPublicAccounts(std::set<std::string>* users_set) OVERRIDE;
123  virtual void NotifyOnLogin() OVERRIDE;
124  virtual void NotifyUserAddedToSession(const user_manager::User* added_user,
125                                        bool user_switch_pending) OVERRIDE;
126  virtual void PerformPreUserListLoadingActions() OVERRIDE;
127  virtual void PerformPostUserListLoadingActions() OVERRIDE;
128  virtual void PerformPostUserLoggedInActions(bool browser_restart) OVERRIDE;
129  virtual void RemoveNonCryptohomeData(const std::string& user_id) OVERRIDE;
130  virtual void RemoveUserInternal(
131      const std::string& user_email,
132      user_manager::RemoveUserDelegate* delegate) OVERRIDE;
133  virtual bool IsDemoApp(const std::string& user_id) const OVERRIDE;
134  virtual bool IsKioskApp(const std::string& user_id) const OVERRIDE;
135  virtual bool IsPublicAccountMarkedForRemoval(
136      const std::string& user_id) const OVERRIDE;
137  virtual void DemoAccountLoggedIn() OVERRIDE;
138  virtual void GuestUserLoggedIn() OVERRIDE;
139  virtual void KioskAppLoggedIn(const std::string& app_id) OVERRIDE;
140  virtual void PublicAccountUserLoggedIn(user_manager::User* user) OVERRIDE;
141  virtual void RegularUserLoggedIn(const std::string& user_id) OVERRIDE;
142  virtual void RegularUserLoggedInAsEphemeral(
143      const std::string& user_id) OVERRIDE;
144  virtual void RetailModeUserLoggedIn() OVERRIDE;
145  virtual void SupervisedUserLoggedIn(const std::string& user_id) OVERRIDE;
146
147 private:
148  friend class SupervisedUserManagerImpl;
149  friend class UserManagerTest;
150  friend class WallpaperManager;
151  friend class WallpaperManagerTest;
152
153  typedef base::hash_map<std::string, linked_ptr<UserImageManager> >
154      UserImageManagerMap;
155
156  ChromeUserManagerImpl();
157
158  // Retrieves trusted device policies and removes users from the persistent
159  // list if ephemeral users are enabled. Schedules a callback to itself if
160  // trusted device policies are not yet available.
161  void RetrieveTrustedDevicePolicies();
162
163  // Updates current user ownership on UI thread.
164  void UpdateOwnership();
165
166  // If data for a public account is marked as pending removal and the user is
167  // no longer logged into that account, removes the data.
168  void CleanUpPublicAccountNonCryptohomeDataPendingRemoval();
169
170  // Removes data belonging to public accounts that are no longer found on the
171  // user list. If the user is currently logged into one of these accounts, the
172  // data for that account is not removed immediately but marked as pending
173  // removal after logout.
174  void CleanUpPublicAccountNonCryptohomeData(
175      const std::vector<std::string>& old_public_accounts);
176
177  // Replaces the list of public accounts with those found in
178  // |device_local_accounts|. Ensures that data belonging to accounts no longer
179  // on the list is removed. Returns |true| if the list has changed.
180  // Public accounts are defined by policy. This method is called whenever an
181  // updated list of public accounts is received from policy.
182  bool UpdateAndCleanUpPublicAccounts(
183      const std::vector<policy::DeviceLocalAccount>& device_local_accounts);
184
185  // Updates the display name for public account |username| from policy settings
186  // associated with that username.
187  void UpdatePublicAccountDisplayName(const std::string& user_id);
188
189  // Notifies the UI about a change to the user list.
190  void NotifyUserListChanged();
191
192  // Lazily creates default user flow.
193  UserFlow* GetDefaultUserFlow() const;
194
195  // MultiProfileUserControllerDelegate implementation:
196  virtual void OnUserNotAllowed(const std::string& user_email) OVERRIDE;
197
198  // Update the number of users.
199  void UpdateNumberOfUsers();
200
201  // Interface to the signed settings store.
202  CrosSettings* cros_settings_;
203
204  // Interface to device-local account definitions and associated policy.
205  policy::DeviceLocalAccountPolicyService* device_local_account_policy_service_;
206
207  content::NotificationRegistrar registrar_;
208
209  // User avatar managers.
210  UserImageManagerMap user_image_managers_;
211
212  // Supervised user manager.
213  scoped_ptr<SupervisedUserManagerImpl> supervised_user_manager_;
214
215  // Session length limiter.
216  scoped_ptr<SessionLengthLimiter> session_length_limiter_;
217
218  typedef std::map<std::string, UserFlow*> FlowMap;
219
220  // Lazy-initialized default flow.
221  mutable scoped_ptr<UserFlow> default_flow_;
222
223  // Specific flows by user e-mail. Keys should be canonicalized before
224  // access.
225  FlowMap specific_flows_;
226
227  scoped_ptr<CrosSettings::ObserverSubscription> local_accounts_subscription_;
228
229  scoped_ptr<MultiProfileUserController> multi_profile_user_controller_;
230
231  // Observer for the policy that can be used to manage user images.
232  scoped_ptr<policy::CloudExternalDataPolicyObserver> avatar_policy_observer_;
233
234  // Observer for the policy that can be used to manage wallpapers.
235  scoped_ptr<policy::CloudExternalDataPolicyObserver>
236      wallpaper_policy_observer_;
237
238  base::WeakPtrFactory<ChromeUserManagerImpl> weak_factory_;
239
240  DISALLOW_COPY_AND_ASSIGN(ChromeUserManagerImpl);
241};
242
243}  // namespace chromeos
244
245#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_CHROME_USER_MANAGER_IMPL_H_
246