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 COMPONENTS_USER_MANAGER_USER_MANAGER_H_
6#define COMPONENTS_USER_MANAGER_USER_MANAGER_H_
7
8#include <string>
9
10#include "components/user_manager/user.h"
11#include "components/user_manager/user_manager_export.h"
12
13namespace chromeos {
14class ScopedUserManagerEnabler;
15}
16
17namespace user_manager {
18
19class RemoveUserDelegate;
20
21// Interface for UserManagerBase - that provides base implementation for
22// Chrome OS user management. Typical features:
23// * Get list of all know users (who have logged into this Chrome OS device)
24// * Keep track for logged in/LRU users, active user in multi-user session.
25// * Find/modify users, store user meta-data such as display name/email.
26class USER_MANAGER_EXPORT UserManager {
27 public:
28  // Interface that observers of UserManager must implement in order
29  // to receive notification when local state preferences is changed
30  class Observer {
31   public:
32    // Called when the local state preferences is changed.
33    virtual void LocalStateChanged(UserManager* user_manager);
34
35   protected:
36    virtual ~Observer();
37  };
38
39  // TODO(nkostylev): Refactor and move this observer out of UserManager.
40  // Observer interface that defines methods used to notify on user session /
41  // active user state changes. Default implementation is empty.
42  class UserSessionStateObserver {
43   public:
44    // Called when active user has changed.
45    virtual void ActiveUserChanged(const User* active_user);
46
47    // Called when another user got added to the existing session.
48    virtual void UserAddedToSession(const User* added_user);
49
50    // Called right before notifying on user change so that those who rely
51    // on user_id hash would be accessing up-to-date value.
52    virtual void ActiveUserHashChanged(const std::string& hash);
53
54   protected:
55    virtual ~UserSessionStateObserver();
56  };
57
58  // Data retrieved from user account.
59  class UserAccountData {
60   public:
61    UserAccountData(const base::string16& display_name,
62                    const base::string16& given_name,
63                    const std::string& locale);
64    ~UserAccountData();
65    const base::string16& display_name() const { return display_name_; }
66    const base::string16& given_name() const { return given_name_; }
67    const std::string& locale() const { return locale_; }
68
69   private:
70    const base::string16 display_name_;
71    const base::string16 given_name_;
72    const std::string locale_;
73
74    DISALLOW_COPY_AND_ASSIGN(UserAccountData);
75  };
76
77  // Initializes UserManager instance to this. Normally should be called right
78  // after creation so that user_manager::UserManager::Get() doesn't fail.
79  // Tests could call this method if they are replacing existing UserManager
80  // instance with their own test instance.
81  void Initialize();
82
83  // Checks whether the UserManager instance has been created already.
84  // This method is not thread-safe and must be called from the main UI thread.
85  static bool IsInitialized();
86
87  // Shuts down the UserManager. After this method has been called, the
88  // singleton has unregistered itself as an observer but remains available so
89  // that other classes can access it during their shutdown. This method is not
90  // thread-safe and must be called from the main UI thread.
91  virtual void Shutdown() = 0;
92
93  // Sets UserManager instance to NULL. Always call Shutdown() first.
94  // This method is not thread-safe and must be called from the main UI thread.
95  void Destroy();
96
97  // Returns UserManager instance or will crash if it is |NULL| (has either not
98  // been created yet or is already destroyed). This method is not thread-safe
99  // and must be called from the main UI thread.
100  static UserManager* Get();
101
102  virtual ~UserManager();
103
104  // Returns a list of users who have logged into this device previously. This
105  // is sorted by last login date with the most recent user at the beginning.
106  virtual const UserList& GetUsers() const = 0;
107
108  // Returns list of users admitted for logging in into multi-profile session.
109  // Users that have a policy that prevents them from being added to the
110  // multi-profile session will still be part of this list as long as they
111  // are regular users (i.e. not a public session/supervised etc.).
112  // Returns an empty list in case when primary user is not a regular one or
113  // has a policy that prohibids it to be part of multi-profile session.
114  virtual UserList GetUsersAdmittedForMultiProfile() const = 0;
115
116  // Returns a list of users who are currently logged in.
117  virtual const UserList& GetLoggedInUsers() const = 0;
118
119  // Returns a list of users who are currently logged in in the LRU order -
120  // so the active user is the first one in the list. If there is no user logged
121  // in, the current user will be returned.
122  virtual const UserList& GetLRULoggedInUsers() const = 0;
123
124  // Returns a list of users who can unlock the device.
125  // This list is based on policy and whether user is able to do unlock.
126  // Policy:
127  // * If user has primary-only policy then it is the only user in unlock users.
128  // * Otherwise all users with unrestricted policy are added to this list.
129  // All users that are unable to perform unlock are excluded from this list.
130  virtual UserList GetUnlockUsers() const = 0;
131
132  // Returns the email of the owner user. Returns an empty string if there is
133  // no owner for the device.
134  virtual const std::string& GetOwnerEmail() const = 0;
135
136  // Indicates that a user with the given |user_id| has just logged in. The
137  // persistent list is updated accordingly if the user is not ephemeral.
138  // |browser_restart| is true when reloading Chrome after crash to distinguish
139  // from normal sign in flow.
140  // |username_hash| is used to identify homedir mount point.
141  virtual void UserLoggedIn(const std::string& user_id,
142                            const std::string& username_hash,
143                            bool browser_restart) = 0;
144
145  // Switches to active user identified by |user_id|. User has to be logged in.
146  virtual void SwitchActiveUser(const std::string& user_id) = 0;
147
148  // Switches to the last active user (called after crash happens and session
149  // restore has completed).
150  virtual void SwitchToLastActiveUser() = 0;
151
152  // Called when browser session is started i.e. after
153  // browser_creator.LaunchBrowser(...) was called after user sign in.
154  // When user is at the image screen IsUserLoggedIn() will return true
155  // but IsSessionStarted() will return false. During the kiosk splash screen,
156  // we perform additional initialization after the user is logged in but
157  // before the session has been started.
158  // Fires NOTIFICATION_SESSION_STARTED.
159  virtual void SessionStarted() = 0;
160
161  // Removes the user from the device. Note, it will verify that the given user
162  // isn't the owner, so calling this method for the owner will take no effect.
163  // Note, |delegate| can be NULL.
164  virtual void RemoveUser(const std::string& user_id,
165                          RemoveUserDelegate* delegate) = 0;
166
167  // Removes the user from the persistent list only. Also removes the user's
168  // picture.
169  virtual void RemoveUserFromList(const std::string& user_id) = 0;
170
171  // Returns true if a user with the given user id is found in the persistent
172  // list or currently logged in as ephemeral.
173  virtual bool IsKnownUser(const std::string& user_id) const = 0;
174
175  // Returns the user with the given user id if found in the persistent
176  // list or currently logged in as ephemeral. Returns |NULL| otherwise.
177  virtual const User* FindUser(const std::string& user_id) const = 0;
178
179  // Returns the user with the given user id if found in the persistent
180  // list or currently logged in as ephemeral. Returns |NULL| otherwise.
181  // Same as FindUser but returns non-const pointer to User object.
182  virtual User* FindUserAndModify(const std::string& user_id) = 0;
183
184  // Returns the logged-in user.
185  // TODO(nkostylev): Deprecate this call, move clients to GetActiveUser().
186  // http://crbug.com/230852
187  virtual const User* GetLoggedInUser() const = 0;
188  virtual User* GetLoggedInUser() = 0;
189
190  // Returns the logged-in user that is currently active within this session.
191  // There could be multiple users logged in at the the same but for now
192  // we support only one of them being active.
193  virtual const User* GetActiveUser() const = 0;
194  virtual User* GetActiveUser() = 0;
195
196  // Returns the primary user of the current session. It is recorded for the
197  // first signed-in user and does not change thereafter.
198  virtual const User* GetPrimaryUser() const = 0;
199
200  // Saves user's oauth token status in local state preferences.
201  virtual void SaveUserOAuthStatus(
202      const std::string& user_id,
203      User::OAuthTokenStatus oauth_token_status) = 0;
204
205  // Saves a flag indicating whether online authentication against GAIA should
206  // be enforced during the user's next sign-in.
207  virtual void SaveForceOnlineSignin(const std::string& user_id,
208                                     bool force_online_signin) = 0;
209
210  // Saves user's displayed name in local state preferences.
211  // Ignored If there is no such user.
212  virtual void SaveUserDisplayName(const std::string& user_id,
213                                   const base::string16& display_name) = 0;
214
215  // Updates data upon User Account download.
216  virtual void UpdateUserAccountData(const std::string& user_id,
217                                     const UserAccountData& account_data) = 0;
218
219  // Returns the display name for user |user_id| if it is known (was
220  // previously set by a |SaveUserDisplayName| call).
221  // Otherwise, returns an empty string.
222  virtual base::string16 GetUserDisplayName(
223      const std::string& user_id) const = 0;
224
225  // Saves user's displayed (non-canonical) email in local state preferences.
226  // Ignored If there is no such user.
227  virtual void SaveUserDisplayEmail(const std::string& user_id,
228                                    const std::string& display_email) = 0;
229
230  // Returns the display email for user |user_id| if it is known (was
231  // previously set by a |SaveUserDisplayEmail| call).
232  // Otherwise, returns |user_id| itself.
233  virtual std::string GetUserDisplayEmail(const std::string& user_id) const = 0;
234
235  // Returns true if current user is an owner.
236  virtual bool IsCurrentUserOwner() const = 0;
237
238  // Returns true if current user is not existing one (hasn't signed in before).
239  virtual bool IsCurrentUserNew() const = 0;
240
241  // Returns true if data stored or cached for the current user outside that
242  // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
243  // display email) is ephemeral.
244  virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
245
246  // Returns true if the current user's session can be locked (i.e. the user has
247  // a password with which to unlock the session).
248  virtual bool CanCurrentUserLock() const = 0;
249
250  // Returns true if at least one user has signed in.
251  virtual bool IsUserLoggedIn() const = 0;
252
253  // Returns true if we're logged in as a regular user.
254  virtual bool IsLoggedInAsRegularUser() const = 0;
255
256  // Returns true if we're logged in as a demo user.
257  virtual bool IsLoggedInAsDemoUser() const = 0;
258
259  // Returns true if we're logged in as a public account.
260  virtual bool IsLoggedInAsPublicAccount() const = 0;
261
262  // Returns true if we're logged in as a Guest.
263  virtual bool IsLoggedInAsGuest() const = 0;
264
265  // Returns true if we're logged in as a supervised user.
266  virtual bool IsLoggedInAsSupervisedUser() const = 0;
267
268  // Returns true if we're logged in as a kiosk app.
269  virtual bool IsLoggedInAsKioskApp() const = 0;
270
271  // Returns true if we're logged in as the stub user used for testing on Linux.
272  virtual bool IsLoggedInAsStub() const = 0;
273
274  // Returns true if we're logged in and browser has been started i.e.
275  // browser_creator.LaunchBrowser(...) was called after sign in
276  // or restart after crash.
277  virtual bool IsSessionStarted() const = 0;
278
279  // Returns true if data stored or cached for the user with the given user id
280  // address outside that user's cryptohome (wallpaper, avatar, OAuth token
281  // status, display name, display email) is to be treated as ephemeral.
282  virtual bool IsUserNonCryptohomeDataEphemeral(
283      const std::string& user_id) const = 0;
284
285  virtual void AddObserver(Observer* obs) = 0;
286  virtual void RemoveObserver(Observer* obs) = 0;
287
288  virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
289  virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
290
291  virtual void NotifyLocalStateChanged() = 0;
292
293  // Returns true if supervised users allowed.
294  virtual bool AreSupervisedUsersAllowed() const = 0;
295
296  // Force update login state.
297  virtual void ForceUpdateState() {}
298
299 protected:
300  // Sets UserManager instance.
301  static void SetInstance(UserManager* user_manager);
302
303  // Pointer to the existing UserManager instance (if any).
304  // Usually is set by calling Initialize(), reset by calling Destroy().
305  // Not owned since specific implementation of UserManager should decide on its
306  // own appropriate owner. For src/chrome implementation such place is
307  // g_browser_process->platform_part().
308  static UserManager* instance;
309
310 private:
311  friend class chromeos::ScopedUserManagerEnabler;
312
313  // Same as Get() but doesn't won't crash is current instance is NULL.
314  static UserManager* GetForTesting();
315
316  // Sets UserManager instance to the given |user_manager|.
317  // Returns the previous value of the instance.
318  static UserManager* SetForTesting(UserManager* user_manager);
319};
320
321}  // namespace user_manager
322
323#endif  // COMPONENTS_USER_MANAGER_USER_MANAGER_H_
324