Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / session / user_session_manager.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 CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/chromeos/base/locale_util.h"
17 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
18 #include "chromeos/dbus/session_manager_client.h"
19 #include "chromeos/ime/input_method_manager.h"
20 #include "chromeos/login/auth/authenticator.h"
21 #include "chromeos/login/auth/user_context.h"
22 #include "components/user_manager/user.h"
23 #include "components/user_manager/user_manager.h"
24 #include "net/base/network_change_notifier.h"
25
26 class PrefRegistrySimple;
27 class PrefService;
28 class Profile;
29
30 namespace user_manager {
31
32 class User;
33
34 }  // namespace user_manager
35
36 namespace chromeos {
37
38 class EasyUnlockKeyManager;
39
40 class UserSessionManagerDelegate {
41  public:
42   // Called after profile is loaded and prepared for the session.
43   virtual void OnProfilePrepared(Profile* profile) = 0;
44
45 #if defined(ENABLE_RLZ)
46   // Called after post-profile RLZ initialization.
47   virtual void OnRlzInitialized();
48 #endif
49  protected:
50   virtual ~UserSessionManagerDelegate();
51 };
52
53 class UserSessionStateObserver {
54  public:
55   // Called when UserManager finishes restoring user sessions after crash.
56   virtual void PendingUserSessionsRestoreFinished();
57
58  protected:
59   virtual ~UserSessionStateObserver();
60 };
61
62 // UserSessionManager is responsible for starting user session which includes:
63 // load and initialize Profile (including custom Profile preferences),
64 // mark user as logged in and notify observers,
65 // initialize OAuth2 authentication session,
66 // initialize and launch user session based on the user type.
67 // Also supports restoring active user sessions after browser crash:
68 // load profile, restore OAuth authentication session etc.
69 class UserSessionManager
70     : public OAuth2LoginManager::Observer,
71       public net::NetworkChangeNotifier::ConnectionTypeObserver,
72       public base::SupportsWeakPtr<UserSessionManager>,
73       public UserSessionManagerDelegate,
74       public user_manager::UserManager::UserSessionStateObserver {
75  public:
76   // Returns UserSessionManager instance.
77   static UserSessionManager* GetInstance();
78
79   // Called when user is logged in to override base::DIR_HOME path.
80   static void OverrideHomedir();
81
82   // Registers session related preferences.
83   static void RegisterPrefs(PrefRegistrySimple* registry);
84
85   // Start user session given |user_context| and |authenticator| which holds
86   // authentication context (profile).
87   void StartSession(const UserContext& user_context,
88                     scoped_refptr<Authenticator> authenticator,
89                     bool has_auth_cookies,
90                     bool has_active_session,
91                     UserSessionManagerDelegate* delegate);
92
93   // Perform additional actions once system wide notification
94   // "UserLoggedIn" has been sent.
95   void PerformPostUserLoggedInActions();
96
97   // Restores authentication session after crash.
98   void RestoreAuthenticationSession(Profile* profile);
99
100   // Usually is called when Chrome is restarted after a crash and there's an
101   // active session. First user (one that is passed with --login-user) Chrome
102   // session has been already restored at this point. This method asks session
103   // manager for all active user sessions, marks them as logged in
104   // and notifies observers.
105   void RestoreActiveSessions();
106
107   // Returns true iff browser has been restarted after crash and
108   // UserSessionManager finished restoring user sessions.
109   bool UserSessionsRestored() const;
110
111   // Returns true iff browser has been restarted after crash and
112   // user sessions restoration is in progress.
113   bool UserSessionsRestoreInProgress() const;
114
115   // Initialize RLZ.
116   void InitRlz(Profile* profile);
117
118   // Get the NSS cert database for the user represented with |profile|
119   // and start certificate loader with it.
120   void InitializeCerts(Profile* profile);
121
122   // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
123   // is migrated.
124   OAuth2LoginManager::SessionRestoreStrategy GetSigninSessionRestoreStrategy();
125   bool exit_after_session_restore() { return exit_after_session_restore_; }
126   void set_exit_after_session_restore(bool value) {
127     exit_after_session_restore_ = value;
128   }
129
130   // Invoked when the user is logging in for the first time, or is logging in to
131   // an ephemeral session type, such as guest or a public session.
132   void SetFirstLoginPrefs(Profile* profile,
133                           const std::string& public_session_locale,
134                           const std::string& public_session_input_method);
135
136   // Gets/sets Chrome OAuth client id and secret for kiosk app mode. The default
137   // values can be overridden with kiosk auth file.
138   bool GetAppModeChromeClientOAuthInfo(
139       std::string* chrome_client_id,
140       std::string* chrome_client_secret);
141   void SetAppModeChromeClientOAuthInfo(
142       const std::string& chrome_client_id,
143       const std::string& chrome_client_secret);
144
145   // Changes browser locale (selects best suitable locale from different
146   // user settings). Returns true if callback will be called.
147   bool RespectLocalePreference(
148       Profile* profile,
149       const user_manager::User* user,
150       scoped_ptr<locale_util::SwitchLanguageCallback> callback) const;
151
152   // Returns true if Easy unlock keys needs to be updated.
153   bool NeedsToUpdateEasyUnlockKeys() const;
154
155   // Returns true if there are pending Easy unlock key operations and
156   // |callback| will be invoked when it is done.
157   bool CheckEasyUnlockKeyOps(const base::Closure& callback);
158
159   void AddSessionStateObserver(chromeos::UserSessionStateObserver* observer);
160   void RemoveSessionStateObserver(chromeos::UserSessionStateObserver* observer);
161
162   virtual void ActiveUserChanged(
163       const user_manager::User* active_user) OVERRIDE;
164
165   // Returns default IME state for user session.
166   scoped_refptr<input_method::InputMethodManager::State> GetDefaultIMEState(
167       Profile* profile);
168
169   // Note this could return NULL if not enabled.
170   EasyUnlockKeyManager* GetEasyUnlockKeyManager();
171
172  private:
173   friend struct DefaultSingletonTraits<UserSessionManager>;
174
175   typedef std::set<std::string> SigninSessionRestoreStateSet;
176
177   UserSessionManager();
178   virtual ~UserSessionManager();
179
180   // OAuth2LoginManager::Observer overrides:
181   virtual void OnSessionRestoreStateChanged(
182       Profile* user_profile,
183       OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
184   virtual void OnNewRefreshTokenAvaiable(Profile* user_profile) OVERRIDE;
185
186   // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
187   virtual void OnConnectionTypeChanged(
188       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
189
190   // UserSessionManagerDelegate overrides:
191   // Used when restoring user sessions after crash.
192   virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
193
194   void CreateUserSession(const UserContext& user_context,
195                          bool has_auth_cookies);
196   void PreStartSession();
197   void StartCrosSession();
198   void NotifyUserLoggedIn();
199   void PrepareProfile();
200
201   // Callback for asynchronous profile creation.
202   void OnProfileCreated(const UserContext& user_context,
203                         bool is_incognito_profile,
204                         Profile* profile,
205                         Profile::CreateStatus status);
206
207   // Callback for Profile::CREATE_STATUS_CREATED profile state.
208   // Initializes basic preferences for newly created profile. Any other
209   // early profile initialization that needs to happen before
210   // ProfileManager::DoFinalInit() gets called is done here.
211   void InitProfilePreferences(Profile* profile,
212                               const UserContext& user_context);
213
214   // Callback for Profile::CREATE_STATUS_INITIALIZED profile state.
215   // Profile is created, extensions and promo resources are initialized.
216   void UserProfileInitialized(Profile* profile,
217                               bool is_incognito_profile,
218                               const std::string& user_id);
219
220   // Callback to resume profile creation after transferring auth data from
221   // the authentication profile.
222   void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
223
224   // Finalized profile preparation.
225   void FinalizePrepareProfile(Profile* profile);
226
227   // Initializes member variables needed for session restore process via
228   // OAuthLoginManager.
229   void InitSessionRestoreStrategy();
230
231   // Restores GAIA auth cookies for the created user profile from OAuth2 token.
232   void RestoreAuthSessionImpl(Profile* profile,
233                               bool restore_from_auth_cookies);
234
235   // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
236   void InitRlzImpl(Profile* profile, bool disabled);
237
238   // Starts loading CRL set.
239   void InitializeCRLSetFetcher(const user_manager::User* user);
240
241   // Callback to process RetrieveActiveSessions() request results.
242   void OnRestoreActiveSessions(
243       const SessionManagerClient::ActiveSessionsMap& sessions,
244       bool success);
245
246   // Called by OnRestoreActiveSessions() when there're user sessions in
247   // |pending_user_sessions_| that has to be restored one by one.
248   // Also called after first user session from that list is restored and so on.
249   // Process continues till |pending_user_sessions_| map is not empty.
250   void RestorePendingUserSessions();
251
252   // Notifies observers that user pending sessions restore has finished.
253   void NotifyPendingUserSessionsRestoreFinished();
254
255   // Update Easy unlock cryptohome keys using the pairing data in user prefs.
256   void UpdateEasyUnlockKeys(Profile* user_profile);
257
258   // Callback invoked when Easy unlock key operations are finished.
259   void OnEasyUnlockKeyOpsFinished(bool success);
260
261   UserSessionManagerDelegate* delegate_;
262
263   // Authentication/user context.
264   UserContext user_context_;
265   scoped_refptr<Authenticator> authenticator_;
266
267   // True if the authentication context's cookie jar contains authentication
268   // cookies from the authentication extension login flow.
269   bool has_auth_cookies_;
270
271   // Active user session restoration related members.
272
273   // True if user sessions has been restored after crash.
274   // On a normal boot then login into user sessions this will be false.
275   bool user_sessions_restored_;
276
277   // True if user sessions restoration after crash is in progress.
278   bool user_sessions_restore_in_progress_;
279
280   // User sessions that have to be restored after browser crash.
281   // [user_id] > [user_id_hash]
282   SessionManagerClient::ActiveSessionsMap pending_user_sessions_;
283
284   ObserverList<chromeos::UserSessionStateObserver> session_state_observer_list_;
285
286   // OAuth2 session related members.
287
288   // True if we should restart chrome right after session restore.
289   bool exit_after_session_restore_;
290
291   // Sesion restore strategy.
292   OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
293
294   // OAuth2 refresh token for session restore.
295   std::string oauth2_refresh_token_;
296
297   // Set of user_id for those users that we should restore authentication
298   // session when notified about online state change.
299   SigninSessionRestoreStateSet pending_signin_restore_sessions_;
300
301   // Kiosk mode related members.
302   // Chrome oauth client id and secret - override values for kiosk mode.
303   std::string chrome_client_id_;
304   std::string chrome_client_secret_;
305
306   // Per-user-session Input Methods states.
307   std::map<Profile*, scoped_refptr<input_method::InputMethodManager::State> >
308       default_ime_states_;
309
310   // Manages Easy unlock cryptohome keys.
311   scoped_ptr<EasyUnlockKeyManager> easy_unlock_key_manager_;
312   bool running_easy_unlock_key_ops_;
313   base::Closure easy_unlock_key_ops_finished_callback_;
314
315   DISALLOW_COPY_AND_ASSIGN(UserSessionManager);
316 };
317
318 }  // namespace chromeos
319
320 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_