Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / user_manager / user_manager_base.h
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_BASE_H_
6 #define COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/synchronization/lock.h"
16 #include "base/time/time.h"
17 #include "components/user_manager/user.h"
18 #include "components/user_manager/user_manager.h"
19 #include "components/user_manager/user_manager_export.h"
20
21 class PrefService;
22 class PrefRegistrySimple;
23
24 namespace base {
25 class ListValue;
26 class TaskRunner;
27 }
28
29 namespace user_manager {
30
31 class RemoveUserDelegate;
32
33 // Base implementation of the UserManager interface.
34 class USER_MANAGER_EXPORT UserManagerBase : public UserManager {
35  public:
36   UserManagerBase(scoped_refptr<base::TaskRunner> task_runner,
37                   scoped_refptr<base::TaskRunner> blocking_task_runner);
38   virtual ~UserManagerBase();
39
40   // Registers UserManagerBase preferences.
41   static void RegisterPrefs(PrefRegistrySimple* registry);
42
43   // UserManager implementation:
44   virtual void Shutdown() OVERRIDE;
45   virtual const UserList& GetUsers() const OVERRIDE;
46   virtual const UserList& GetLoggedInUsers() const OVERRIDE;
47   virtual const UserList& GetLRULoggedInUsers() const OVERRIDE;
48   virtual const std::string& GetOwnerEmail() const OVERRIDE;
49   virtual void UserLoggedIn(const std::string& user_id,
50                             const std::string& user_id_hash,
51                             bool browser_restart) OVERRIDE;
52   virtual void SwitchActiveUser(const std::string& user_id) OVERRIDE;
53   virtual void SessionStarted() OVERRIDE;
54   virtual void RemoveUser(const std::string& user_id,
55                           RemoveUserDelegate* delegate) OVERRIDE;
56   virtual void RemoveUserFromList(const std::string& user_id) OVERRIDE;
57   virtual bool IsKnownUser(const std::string& user_id) const OVERRIDE;
58   virtual const User* FindUser(const std::string& user_id) const OVERRIDE;
59   virtual User* FindUserAndModify(const std::string& user_id) OVERRIDE;
60   virtual const User* GetLoggedInUser() const OVERRIDE;
61   virtual User* GetLoggedInUser() OVERRIDE;
62   virtual const User* GetActiveUser() const OVERRIDE;
63   virtual User* GetActiveUser() OVERRIDE;
64   virtual const User* GetPrimaryUser() const OVERRIDE;
65   virtual void SaveUserOAuthStatus(
66       const std::string& user_id,
67       User::OAuthTokenStatus oauth_token_status) OVERRIDE;
68   virtual void SaveForceOnlineSignin(const std::string& user_id,
69                                      bool force_online_signin) OVERRIDE;
70   virtual void SaveUserDisplayName(const std::string& user_id,
71                                    const base::string16& display_name) OVERRIDE;
72   virtual base::string16 GetUserDisplayName(
73       const std::string& user_id) const OVERRIDE;
74   virtual void SaveUserDisplayEmail(const std::string& user_id,
75                                     const std::string& display_email) OVERRIDE;
76   virtual std::string GetUserDisplayEmail(
77       const std::string& user_id) const OVERRIDE;
78   virtual void UpdateUserAccountData(
79       const std::string& user_id,
80       const UserAccountData& account_data) OVERRIDE;
81   virtual bool IsCurrentUserOwner() const OVERRIDE;
82   virtual bool IsCurrentUserNew() const OVERRIDE;
83   virtual bool IsCurrentUserNonCryptohomeDataEphemeral() const OVERRIDE;
84   virtual bool CanCurrentUserLock() const OVERRIDE;
85   virtual bool IsUserLoggedIn() const OVERRIDE;
86   virtual bool IsLoggedInAsRegularUser() const OVERRIDE;
87   virtual bool IsLoggedInAsDemoUser() const OVERRIDE;
88   virtual bool IsLoggedInAsPublicAccount() const OVERRIDE;
89   virtual bool IsLoggedInAsGuest() const OVERRIDE;
90   virtual bool IsLoggedInAsSupervisedUser() const OVERRIDE;
91   virtual bool IsLoggedInAsKioskApp() const OVERRIDE;
92   virtual bool IsLoggedInAsStub() const OVERRIDE;
93   virtual bool IsSessionStarted() const OVERRIDE;
94   virtual bool IsUserNonCryptohomeDataEphemeral(
95       const std::string& user_id) const OVERRIDE;
96   virtual void AddObserver(UserManager::Observer* obs) OVERRIDE;
97   virtual void RemoveObserver(UserManager::Observer* obs) OVERRIDE;
98   virtual void AddSessionStateObserver(
99       UserManager::UserSessionStateObserver* obs) OVERRIDE;
100   virtual void RemoveSessionStateObserver(
101       UserManager::UserSessionStateObserver* obs) OVERRIDE;
102   virtual void NotifyLocalStateChanged() OVERRIDE;
103
104   // Helper function that copies users from |users_list| to |users_vector| and
105   // |users_set|. Duplicates and users already present in |existing_users| are
106   // skipped.
107   static void ParseUserList(const base::ListValue& users_list,
108                             const std::set<std::string>& existing_users,
109                             std::vector<std::string>* users_vector,
110                             std::set<std::string>* users_set);
111
112  protected:
113   UserManagerBase();
114
115   // Adds |user| to users list, and adds it to front of LRU list. It is assumed
116   // that there is no user with same id.
117   virtual void AddUserRecord(User* user);
118
119   // Returns true if trusted device policies have successfully been retrieved
120   // and ephemeral users are enabled.
121   virtual bool AreEphemeralUsersEnabled() const = 0;
122
123   // Returns true if user may be removed.
124   virtual bool CanUserBeRemoved(const User* user) const;
125
126   // A wrapper around C++ delete operator. Deletes |user|, and when |user|
127   // equals to active_user_, active_user_ is reset to NULL.
128   virtual void DeleteUser(User* user);
129
130   // Returns the locale used by the application.
131   virtual const std::string& GetApplicationLocale() const = 0;
132
133   // Returns "Local State" PrefService instance.
134   virtual PrefService* GetLocalState() const = 0;
135
136   // Loads |users_| from Local State if the list has not been loaded yet.
137   // Subsequent calls have no effect. Must be called on the UI thread.
138   void EnsureUsersLoaded();
139
140   // Handle OAuth token |status| change for |user_id|.
141   virtual void HandleUserOAuthTokenStatusChange(
142       const std::string& user_id,
143       User::OAuthTokenStatus status) const = 0;
144
145   // Returns true if device is enterprise managed.
146   virtual bool IsEnterpriseManaged() const = 0;
147
148   // Helper function that copies users from |users_list| to |users_vector| and
149   // |users_set|. Duplicates and users already present in |existing_users| are
150   // skipped.
151   // Loads public accounts from the Local state and fills in
152   // |public_sessions_set|.
153   virtual void LoadPublicAccounts(
154       std::set<std::string>* public_sessions_set) = 0;
155
156   // Notifies that user has logged in.
157   virtual void NotifyOnLogin();
158
159   // Notifies observers that another user was added to the session.
160   // If |user_switch_pending| is true this means that user has not been fully
161   // initialized yet like waiting for profile to be loaded.
162   virtual void NotifyUserAddedToSession(const User* added_user,
163                                         bool user_switch_pending);
164
165   // Performs any additional actions before user list is loaded.
166   virtual void PerformPreUserListLoadingActions() = 0;
167
168   // Performs any additional actions after user list is loaded.
169   virtual void PerformPostUserListLoadingActions() = 0;
170
171   // Performs any additional actions after UserLoggedIn() execution has been
172   // completed.
173   // |browser_restart| is true when reloading Chrome after crash to distinguish
174   // from normal sign in flow.
175   virtual void PerformPostUserLoggedInActions(bool browser_restart) = 0;
176
177   // Implementation for RemoveUser method. It is synchronous. It is called from
178   // RemoveUserInternal after owner check.
179   virtual void RemoveNonOwnerUserInternal(const std::string& user_email,
180                                           RemoveUserDelegate* delegate);
181
182   // Removes a regular or supervised user from the user list.
183   // Returns the user if found or NULL otherwise.
184   // Also removes the user from the persistent user list.
185   User* RemoveRegularOrSupervisedUserFromList(const std::string& user_id);
186
187   // Implementation for RemoveUser method. This is an asynchronous part of the
188   // method, that verifies that owner will not get deleted, and calls
189   // |RemoveNonOwnerUserInternal|.
190   virtual void RemoveUserInternal(const std::string& user_email,
191                                   RemoveUserDelegate* delegate);
192
193   // Removes data stored or cached outside the user's cryptohome (wallpaper,
194   // avatar, OAuth token status, display name, display email).
195   virtual void RemoveNonCryptohomeData(const std::string& user_id);
196
197   // Check for a particular user type.
198
199   // Returns true if |user_id| represents demo app.
200   virtual bool IsDemoApp(const std::string& user_id) const = 0;
201
202   // Returns true if |user_id| represents kiosk app.
203   virtual bool IsKioskApp(const std::string& user_id) const = 0;
204
205   // Returns true if |user_id| represents public account that has been marked
206   // for deletion.
207   virtual bool IsPublicAccountMarkedForRemoval(
208       const std::string& user_id) const = 0;
209
210   // These methods are called when corresponding user type has signed in.
211
212   // Indicates that the demo account has just logged in.
213   virtual void DemoAccountLoggedIn() = 0;
214
215   // Indicates that a user just logged in as guest.
216   virtual void GuestUserLoggedIn();
217
218   // Indicates that a kiosk app robot just logged in.
219   virtual void KioskAppLoggedIn(const std::string& app_id) = 0;
220
221   // Indicates that a user just logged into a public session.
222   virtual void PublicAccountUserLoggedIn(User* user) = 0;
223
224   // Indicates that a regular user just logged in.
225   virtual void RegularUserLoggedIn(const std::string& user_id);
226
227   // Indicates that a regular user just logged in as ephemeral.
228   virtual void RegularUserLoggedInAsEphemeral(const std::string& user_id);
229
230   // Indicates that a user just logged into a retail mode session.
231   virtual void RetailModeUserLoggedIn() = 0;
232
233   // Indicates that a supervised user just logged in.
234   virtual void SupervisedUserLoggedIn(const std::string& user_id) = 0;
235
236   // Getters/setters for private members.
237
238   virtual void SetCurrentUserIsOwner(bool is_current_user_owner);
239
240   virtual bool GetEphemeralUsersEnabled() const;
241   virtual void SetEphemeralUsersEnabled(bool enabled);
242
243   virtual void SetIsCurrentUserNew(bool is_new);
244
245   virtual void SetOwnerEmail(std::string owner_user_id);
246
247   virtual const std::string& GetPendingUserSwitchID() const;
248   virtual void SetPendingUserSwitchID(std::string user_id);
249
250   // The logged-in user that is currently active in current session.
251   // NULL until a user has logged in, then points to one
252   // of the User instances in |users_|, the |guest_user_| instance or an
253   // ephemeral user instance.
254   User* active_user_;
255
256   // The primary user of the current session. It is recorded for the first
257   // signed-in user and does not change thereafter.
258   User* primary_user_;
259
260   // List of all known users. User instances are owned by |this|. Regular users
261   // are removed by |RemoveUserFromList|, public accounts by
262   // |UpdateAndCleanUpPublicAccounts|.
263   UserList users_;
264
265  private:
266   // Stages of loading user list from preferences. Some methods can have
267   // different behavior depending on stage.
268   enum UserLoadStage { STAGE_NOT_LOADED = 0, STAGE_LOADING, STAGE_LOADED };
269
270   // Returns a list of users who have logged into this device previously.
271   // Same as GetUsers but used if you need to modify User from that list.
272   UserList& GetUsersAndModify();
273
274   // Returns the user with the given email address if found in the persistent
275   // list. Returns |NULL| otherwise.
276   const User* FindUserInList(const std::string& user_id) const;
277
278   // Returns |true| if user with the given id is found in the persistent list.
279   // Returns |false| otherwise. Does not trigger user loading.
280   const bool UserExistsInList(const std::string& user_id) const;
281
282   // Same as FindUserInList but returns non-const pointer to User object.
283   User* FindUserInListAndModify(const std::string& user_id);
284
285   // Reads user's oauth token status from local state preferences.
286   User::OAuthTokenStatus LoadUserOAuthStatus(const std::string& user_id) const;
287
288   // Read a flag indicating whether online authentication against GAIA should
289   // be enforced during the user's next sign-in from local state preferences.
290   bool LoadForceOnlineSignin(const std::string& user_id) const;
291
292   // Notifies observers that merge session state had changed.
293   void NotifyMergeSessionStateChanged();
294
295   // Notifies observers that active user has changed.
296   void NotifyActiveUserChanged(const User* active_user);
297
298   // Notifies observers that active user_id hash has changed.
299   void NotifyActiveUserHashChanged(const std::string& hash);
300
301   // Update the global LoginState.
302   void UpdateLoginState();
303
304   // Insert |user| at the front of the LRU user list.
305   void SetLRUUser(User* user);
306
307   // Sends metrics in response to a regular user logging in.
308   void SendRegularUserLoginMetrics(const std::string& user_id);
309
310   // Sets account locale for user with id |user_id|.
311   virtual void UpdateUserAccountLocale(const std::string& user_id,
312                                        const std::string& locale);
313
314   // Runs on SequencedWorkerPool thread. Passes resolved locale to
315   // |on_resolve_callback| on UI thread.
316   void ResolveLocale(
317       const std::string& raw_locale,
318       base::Callback<void(const std::string&)> on_resolve_callback);
319
320   // Updates user account after locale was resolved.
321   void DoUpdateAccountLocale(const std::string& user_id,
322                              const std::string& resolved_locale);
323
324   // Indicates stage of loading user from prefs.
325   UserLoadStage user_loading_stage_;
326
327   // List of all users that are logged in current session. These point to User
328   // instances in |users_|. Only one of them could be marked as active.
329   UserList logged_in_users_;
330
331   // A list of all users that are logged in the current session. In contrast to
332   // |logged_in_users|, the order of this list is least recently used so that
333   // the active user should always be the first one in the list.
334   UserList lru_logged_in_users_;
335
336   // True if SessionStarted() has been called.
337   bool session_started_;
338
339   // Cached flag of whether currently logged-in user is owner or not.
340   // May be accessed on different threads, requires locking.
341   bool is_current_user_owner_;
342   mutable base::Lock is_current_user_owner_lock_;
343
344   // Cached flag of whether the currently logged-in user existed before this
345   // login.
346   bool is_current_user_new_;
347
348   // Cached flag of whether the currently logged-in user is a regular user who
349   // logged in as ephemeral. Storage of persistent information is avoided for
350   // such users by not adding them to the persistent user list, not downloading
351   // their custom avatars and mounting their cryptohomes using tmpfs. Defaults
352   // to |false|.
353   bool is_current_user_ephemeral_regular_user_;
354
355   // Cached flag indicating whether the ephemeral user policy is enabled.
356   // Defaults to |false| if the value has not been read from trusted device
357   // policy yet.
358   bool ephemeral_users_enabled_;
359
360   // Cached name of device owner. Defaults to empty string if the value has not
361   // been read from trusted device policy yet.
362   std::string owner_email_;
363
364   ObserverList<UserManager::Observer> observer_list_;
365
366   // TODO(nkostylev): Merge with session state refactoring CL.
367   ObserverList<UserManager::UserSessionStateObserver>
368       session_state_observer_list_;
369
370   // Time at which this object was created.
371   base::TimeTicks manager_creation_time_;
372
373   // ID of the user just added to the session that needs to be activated
374   // as soon as user's profile is loaded.
375   std::string pending_user_switch_;
376
377   scoped_refptr<base::TaskRunner> task_runner_;
378   scoped_refptr<base::TaskRunner> blocking_task_runner_;
379
380   base::WeakPtrFactory<UserManagerBase> weak_factory_;
381
382   DISALLOW_COPY_AND_ASSIGN(UserManagerBase);
383 };
384
385 }  // namespace user_manager
386
387 #endif  // COMPONENTS_USER_MANAGER_USER_MANAGER_BASE_H_