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