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 "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/strings/string16.h"
13 #include "components/user_manager/user.h"
14 #include "components/user_manager/user_manager_export.h"
15 #include "components/user_manager/user_type.h"
16 
17 class AccountId;
18 class PrefService;
19 
20 namespace user_manager {
21 
22 class ScopedUserManager;
23 class RemoveUserDelegate;
24 
25 // A list pref of the the regular users known on this device, arranged in LRU
26 // order, stored in local state.
27 USER_MANAGER_EXPORT extern const char kRegularUsersPref[];
28 
29 // Interface for UserManagerBase - that provides base implementation for
30 // Chrome OS user management. Typical features:
31 // * Get list of all know users (who have logged into this Chrome OS device)
32 // * Keep track for logged in/LRU users, active user in multi-user session.
33 // * Find/modify users, store user meta-data such as display name/email.
34 class USER_MANAGER_EXPORT UserManager {
35  public:
36   // Interface that observers of UserManager must implement in order
37   // to receive notification when local state preferences is changed
38   class Observer {
39    public:
40     // Called when the local state preferences is changed.
41     virtual void LocalStateChanged(UserManager* user_manager);
42 
43     // Called when the image of the given user is changed.
44     virtual void OnUserImageChanged(const User& user);
45 
46     // Called when the profile image download for the given user fails or
47     // user has the default profile image or no porfile image at all.
48     virtual void OnUserProfileImageUpdateFailed(const User& user);
49 
50     // Called when the profile image for the given user is downloaded.
51     // |profile_image| contains the downloaded profile image.
52     virtual void OnUserProfileImageUpdated(const User& user,
53                                            const gfx::ImageSkia& profile_image);
54 
55     // Called when any of the device cros settings which are responsible for
56     // user sign in are changed.
57     virtual void OnUsersSignInConstraintsChanged();
58 
59    protected:
60     virtual ~Observer();
61   };
62 
63   // TODO(xiyuan): Refactor and move this observer out of UserManager.
64   // Observer interface that defines methods used to notify on user session /
65   // active user state changes. Default implementation is empty.
66   class UserSessionStateObserver {
67    public:
68     // Called when active user has changed.
69     virtual void ActiveUserChanged(User* active_user);
70 
71     // Called when another user got added to the existing session.
72     virtual void UserAddedToSession(const User* added_user);
73 
74     // Called right before notifying on user change so that those who rely
75     // on account_id hash would be accessing up-to-date value.
76     virtual void ActiveUserHashChanged(const std::string& hash);
77 
78    protected:
79     virtual ~UserSessionStateObserver();
80   };
81 
82   // Data retrieved from user account.
83   class UserAccountData {
84    public:
85     UserAccountData(const base::string16& display_name,
86                     const base::string16& given_name,
87                     const std::string& locale);
88     ~UserAccountData();
display_name()89     const base::string16& display_name() const { return display_name_; }
given_name()90     const base::string16& given_name() const { return given_name_; }
locale()91     const std::string& locale() const { return locale_; }
92 
93    private:
94     const base::string16 display_name_;
95     const base::string16 given_name_;
96     const std::string locale_;
97 
98     DISALLOW_COPY_AND_ASSIGN(UserAccountData);
99   };
100 
101   // Initializes UserManager instance to this. Normally should be called right
102   // after creation so that user_manager::UserManager::Get() doesn't fail.
103   // Tests could call this method if they are replacing existing UserManager
104   // instance with their own test instance.
105   virtual void Initialize();
106 
107   // Checks whether the UserManager instance has been created already.
108   // This method is not thread-safe and must be called from the main UI thread.
109   static bool IsInitialized();
110 
111   // Shuts down the UserManager. After this method has been called, the
112   // singleton has unregistered itself as an observer but remains available so
113   // that other classes can access it during their shutdown. This method is not
114   // thread-safe and must be called from the main UI thread.
115   virtual void Shutdown() = 0;
116 
117   // Sets UserManager instance to NULL. Always call Shutdown() first.
118   // This method is not thread-safe and must be called from the main UI thread.
119   void Destroy();
120 
121   // Returns UserManager instance or will crash if it is |NULL| (has either not
122   // been created yet or is already destroyed). This method is not thread-safe
123   // and must be called from the main UI thread.
124   static UserManager* Get();
125 
126   virtual ~UserManager();
127 
128   // Returns a list of users who have logged into this device previously. This
129   // is sorted by last login date with the most recent user at the beginning.
130   virtual const UserList& GetUsers() const = 0;
131 
132   // Returns list of users allowed for logging in into multi-profile session.
133   // Users that have a policy that prevents them from being added to the
134   // multi-profile session will still be part of this list as long as they
135   // are regular users (i.e. not a public session/supervised etc.).
136   // Returns an empty list in case when primary user is not a regular one or
137   // has a policy that prohibits it to be part of multi-profile session.
138   virtual UserList GetUsersAllowedForMultiProfile() const = 0;
139 
140   // Returns a list of users who are currently logged in.
141   virtual const UserList& GetLoggedInUsers() const = 0;
142 
143   // Returns a list of users who are currently logged in in the LRU order -
144   // so the active user is the first one in the list. If there is no user logged
145   // in, the current user will be returned.
146   virtual const UserList& GetLRULoggedInUsers() const = 0;
147 
148   // Returns a list of users who can unlock the device.
149   // This list is based on policy and whether user is able to do unlock.
150   // Policy:
151   // * If user has primary-only policy then it is the only user in unlock users.
152   // * Otherwise all users with unrestricted policy are added to this list.
153   // All users that are unable to perform unlock are excluded from this list.
154   virtual UserList GetUnlockUsers() const = 0;
155 
156   // Returns account Id of the owner user. Returns an empty Id if there is
157   // no owner for the device.
158   virtual const AccountId& GetOwnerAccountId() const = 0;
159 
160   // Indicates that a user with the given |account_id| has just logged in. The
161   // persistent list is updated accordingly if the user is not ephemeral.
162   // |browser_restart| is true when reloading Chrome after crash to distinguish
163   // from normal sign in flow.
164   // |username_hash| is used to identify homedir mount point.
165   virtual void UserLoggedIn(const AccountId& account_id,
166                             const std::string& username_hash,
167                             bool browser_restart,
168                             bool is_child) = 0;
169 
170   // Switches to active user identified by |account_id|. User has to be logged
171   // in.
172   virtual void SwitchActiveUser(const AccountId& account_id) = 0;
173 
174   // Switches to the last active user (called after crash happens and session
175   // restore has completed).
176   virtual void SwitchToLastActiveUser() = 0;
177 
178   // Invoked by session manager to inform session start.
179   virtual void OnSessionStarted() = 0;
180 
181   // Removes the user from the device. Note, it will verify that the given user
182   // isn't the owner, so calling this method for the owner will take no effect.
183   // Note, |delegate| can be NULL.
184   virtual void RemoveUser(const AccountId& account_id,
185                           RemoveUserDelegate* delegate) = 0;
186 
187   // Removes the user from the persistent list only. Also removes the user's
188   // picture.
189   virtual void RemoveUserFromList(const AccountId& account_id) = 0;
190 
191   // Returns true if a user with the given account id is found in the persistent
192   // list or currently logged in as ephemeral.
193   virtual bool IsKnownUser(const AccountId& account_id) const = 0;
194 
195   // Returns the user with the given account id if found in the persistent
196   // list or currently logged in as ephemeral. Returns |NULL| otherwise.
197   virtual const User* FindUser(const AccountId& account_id) const = 0;
198 
199   // Returns the user with the given account id if found in the persistent
200   // list or currently logged in as ephemeral. Returns |NULL| otherwise.
201   // Same as FindUser but returns non-const pointer to User object.
202   virtual User* FindUserAndModify(const AccountId& account_id) = 0;
203 
204   // Returns the logged-in user that is currently active within this session.
205   // There could be multiple users logged in at the the same but for now
206   // we support only one of them being active.
207   virtual const User* GetActiveUser() const = 0;
208   virtual User* GetActiveUser() = 0;
209 
210   // Returns the primary user of the current session. It is recorded for the
211   // first signed-in user and does not change thereafter.
212   virtual const User* GetPrimaryUser() const = 0;
213 
214   // Saves user's oauth token status in local state preferences.
215   virtual void SaveUserOAuthStatus(
216       const AccountId& account_id,
217       User::OAuthTokenStatus oauth_token_status) = 0;
218 
219   // Saves a flag indicating whether online authentication against GAIA should
220   // be enforced during the user's next sign-in.
221   virtual void SaveForceOnlineSignin(const AccountId& account_id,
222                                      bool force_online_signin) = 0;
223 
224   // Saves user's displayed name in local state preferences.
225   // Ignored If there is no such user.
226   virtual void SaveUserDisplayName(const AccountId& account_id,
227                                    const base::string16& display_name) = 0;
228 
229   // Updates data upon User Account download.
230   virtual void UpdateUserAccountData(const AccountId& account_id,
231                                      const UserAccountData& account_data) = 0;
232 
233   // Returns the display name for user |account_id| if it is known (was
234   // previously set by a |SaveUserDisplayName| call).
235   // Otherwise, returns an empty string.
236   virtual base::string16 GetUserDisplayName(
237       const AccountId& account_id) const = 0;
238 
239   // Saves user's displayed (non-canonical) email in local state preferences.
240   // Ignored If there is no such user.
241   virtual void SaveUserDisplayEmail(const AccountId& account_id,
242                                     const std::string& display_email) = 0;
243 
244   // Saves user's type for |user| into local state preferences.
245   virtual void SaveUserType(const User* user) = 0;
246 
247   // Returns true if current user is an owner.
248   virtual bool IsCurrentUserOwner() const = 0;
249 
250   // Returns true if current user is not existing one (hasn't signed in before).
251   virtual bool IsCurrentUserNew() const = 0;
252 
253   // Returns true if data stored or cached for the current user outside that
254   // user's cryptohome (wallpaper, avatar, OAuth token status, display name,
255   // display email) is ephemeral.
256   virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const = 0;
257 
258   // Returns true if data stored or cached for the current user inside that
259   // user's cryptohome is ephemeral.
260   virtual bool IsCurrentUserCryptohomeDataEphemeral() const = 0;
261 
262   // Returns true if the current user's session can be locked (i.e. the user has
263   // a password with which to unlock the session).
264   virtual bool CanCurrentUserLock() const = 0;
265 
266   // Returns true if at least one user has signed in.
267   virtual bool IsUserLoggedIn() const = 0;
268 
269   // Returns true if we're logged in as a user with gaia account.
270   virtual bool IsLoggedInAsUserWithGaiaAccount() const = 0;
271 
272   // Returns true if we're logged in as a child user.
273   virtual bool IsLoggedInAsChildUser() const = 0;
274 
275   // Returns true if we're logged in as a public account.
276   virtual bool IsLoggedInAsPublicAccount() const = 0;
277 
278   // Returns true if we're logged in as a Guest.
279   virtual bool IsLoggedInAsGuest() const = 0;
280 
281   // Returns true if we're logged in as a kiosk app.
282   virtual bool IsLoggedInAsKioskApp() const = 0;
283 
284   // Returns true if we're logged in as an ARC kiosk app.
285   virtual bool IsLoggedInAsArcKioskApp() const = 0;
286 
287   // Returns true if we're logged in as a Web kiosk app.
288   virtual bool IsLoggedInAsWebKioskApp() const = 0;
289 
290   // Returns true if we're logged in as chrome, ARC or Web kiosk app.
291   virtual bool IsLoggedInAsAnyKioskApp() const = 0;
292 
293   // Returns true if we're logged in as the stub user used for testing on Linux.
294   virtual bool IsLoggedInAsStub() const = 0;
295 
296   // Returns true if data stored or cached for the user with the given
297   // |account_id|
298   // address outside that user's cryptohome (wallpaper, avatar, OAuth token
299   // status, display name, display email) is to be treated as ephemeral.
300   virtual bool IsUserNonCryptohomeDataEphemeral(
301       const AccountId& account_id) const = 0;
302 
303   virtual bool IsUserCryptohomeDataEphemeral(
304       const AccountId& account_id) const = 0;
305 
306   virtual void AddObserver(Observer* obs) = 0;
307   virtual void RemoveObserver(Observer* obs) = 0;
308 
309   virtual void AddSessionStateObserver(UserSessionStateObserver* obs) = 0;
310   virtual void RemoveSessionStateObserver(UserSessionStateObserver* obs) = 0;
311 
312   virtual void NotifyLocalStateChanged() = 0;
313   virtual void NotifyUserImageChanged(const User& user) = 0;
314   virtual void NotifyUserProfileImageUpdateFailed(const User& user) = 0;
315   virtual void NotifyUserProfileImageUpdated(
316       const User& user,
317       const gfx::ImageSkia& profile_image) = 0;
318   virtual void NotifyUsersSignInConstraintsChanged() = 0;
319 
320   // Returns true if guest user is allowed.
321   virtual bool IsGuestSessionAllowed() const = 0;
322 
323   // Returns true if the |user|, which has a GAIA account is allowed according
324   // to device settings and policies.
325   // Accept only users who has gaia account.
326   virtual bool IsGaiaUserAllowed(const User& user) const = 0;
327 
328   // Returns true if |user| is allowed depending on device policies.
329   // Accepted user types: USER_TYPE_REGULAR, USER_TYPE_GUEST,
330   // USER_TYPE_SUPERVISED, USER_TYPE_CHILD.
331   virtual bool IsUserAllowed(const User& user) const = 0;
332 
333   // Returns "Local State" PrefService instance.
334   virtual PrefService* GetLocalState() const = 0;
335 
336   // Checks for platform-specific known users matching given |user_email| and
337   // |gaia_id|. If data matches a known account, fills |out_account_id| with
338   // account id and returns true.
339   virtual bool GetPlatformKnownUserId(const std::string& user_email,
340                                       const std::string& gaia_id,
341                                       AccountId* out_account_id) const = 0;
342 
343   // Returns account id of the Guest user.
344   virtual const AccountId& GetGuestAccountId() const = 0;
345 
346   // Returns true if this is first exec after boot.
347   virtual bool IsFirstExecAfterBoot() const = 0;
348 
349   // Actually removes cryptohome.
350   virtual void AsyncRemoveCryptohome(const AccountId& account_id) const = 0;
351 
352   // Returns true if |account_id| is Guest user.
353   virtual bool IsGuestAccountId(const AccountId& account_id) const = 0;
354 
355   // Returns true if |account_id| is Stub user.
356   virtual bool IsStubAccountId(const AccountId& account_id) const = 0;
357 
358   // Returns true if |account_id| is supervised.
359   // TODO(crbug.com/866790): Check it is not used anymore and remove it.
360   virtual bool IsSupervisedAccountId(const AccountId& account_id) const = 0;
361 
362   virtual bool IsDeviceLocalAccountMarkedForRemoval(
363       const AccountId& account_id) const = 0;
364 
365   // Returns true when the browser has crashed and restarted during the current
366   // user's session.
367   virtual bool HasBrowserRestarted() const = 0;
368 
369   // Returns image from resources bundle.
370   virtual const gfx::ImageSkia& GetResourceImagekiaNamed(int id) const = 0;
371 
372   // Returns string from resources bundle.
373   virtual base::string16 GetResourceStringUTF16(int string_id) const = 0;
374 
375   // Schedules CheckAndResolveLocale using given task runner and
376   // |on_resolved_callback| as reply callback.
377   virtual void ScheduleResolveLocale(
378       const std::string& locale,
379       base::OnceClosure on_resolved_callback,
380       std::string* out_resolved_locale) const = 0;
381 
382   // Returns true if |image_index| is a valid default user image index.
383   virtual bool IsValidDefaultUserImageId(int image_index) const = 0;
384 
385   UserType CalculateUserType(const AccountId& account_id,
386                              const User* user,
387                              bool browser_restart,
388                              bool is_child) const;
389 
390  protected:
391   // Sets UserManager instance.
392   static void SetInstance(UserManager* user_manager);
393 
394   // Pointer to the existing UserManager instance (if any).
395   // Usually is set by calling Initialize(), reset by calling Destroy().
396   // Not owned since specific implementation of UserManager should decide on its
397   // own appropriate owner. For src/chrome implementation such place is
398   // g_browser_process->platform_part().
399   static UserManager* instance;
400 
401  private:
402   friend class ScopedUserManager;
403 
404   // Same as Get() but doesn't won't crash is current instance is NULL.
405   static UserManager* GetForTesting();
406 
407   // Sets UserManager instance to the given |user_manager|.
408   // Returns the previous value of the instance.
409   static UserManager* SetForTesting(UserManager* user_manager);
410 };
411 
412 }  // namespace user_manager
413 
414 #endif  // COMPONENTS_USER_MANAGER_USER_MANAGER_H_
415