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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_
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"
26 class PrefRegistrySimple;
30 namespace user_manager {
34 } // namespace user_manager
38 class EasyUnlockKeyManager;
40 class UserSessionManagerDelegate {
42 // Called after profile is loaded and prepared for the session.
43 virtual void OnProfilePrepared(Profile* profile) = 0;
45 #if defined(ENABLE_RLZ)
46 // Called after post-profile RLZ initialization.
47 virtual void OnRlzInitialized();
50 virtual ~UserSessionManagerDelegate();
53 class UserSessionStateObserver {
55 // Called when UserManager finishes restoring user sessions after crash.
56 virtual void PendingUserSessionsRestoreFinished();
59 virtual ~UserSessionStateObserver();
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 {
76 // Returns UserSessionManager instance.
77 static UserSessionManager* GetInstance();
79 // Called when user is logged in to override base::DIR_HOME path.
80 static void OverrideHomedir();
82 // Registers session related preferences.
83 static void RegisterPrefs(PrefRegistrySimple* registry);
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);
93 // Perform additional actions once system wide notification
94 // "UserLoggedIn" has been sent.
95 void PerformPostUserLoggedInActions();
97 // Restores authentication session after crash.
98 void RestoreAuthenticationSession(Profile* profile);
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();
107 // Returns true iff browser has been restarted after crash and
108 // UserSessionManager finished restoring user sessions.
109 bool UserSessionsRestored() const;
111 // Returns true iff browser has been restarted after crash and
112 // user sessions restoration is in progress.
113 bool UserSessionsRestoreInProgress() const;
116 void InitRlz(Profile* profile);
118 // Get the NSS cert database for the user represented with |profile|
119 // and start certificate loader with it.
120 void InitializeCerts(Profile* profile);
122 // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
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;
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);
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);
145 // Changes browser locale (selects best suitable locale from different
146 // user settings). Returns true if callback will be called.
147 bool RespectLocalePreference(
149 const user_manager::User* user,
150 scoped_ptr<locale_util::SwitchLanguageCallback> callback) const;
152 // Returns true if Easy unlock keys needs to be updated.
153 bool NeedsToUpdateEasyUnlockKeys() const;
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);
159 void AddSessionStateObserver(chromeos::UserSessionStateObserver* observer);
160 void RemoveSessionStateObserver(chromeos::UserSessionStateObserver* observer);
162 virtual void ActiveUserChanged(
163 const user_manager::User* active_user) OVERRIDE;
165 // Returns default IME state for user session.
166 scoped_refptr<input_method::InputMethodManager::State> GetDefaultIMEState(
169 // Note this could return NULL if not enabled.
170 EasyUnlockKeyManager* GetEasyUnlockKeyManager();
173 friend struct DefaultSingletonTraits<UserSessionManager>;
175 typedef std::set<std::string> SigninSessionRestoreStateSet;
177 UserSessionManager();
178 virtual ~UserSessionManager();
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;
186 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
187 virtual void OnConnectionTypeChanged(
188 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
190 // UserSessionManagerDelegate overrides:
191 // Used when restoring user sessions after crash.
192 virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
194 void CreateUserSession(const UserContext& user_context,
195 bool has_auth_cookies);
196 void PreStartSession();
197 void StartCrosSession();
198 void NotifyUserLoggedIn();
199 void PrepareProfile();
201 // Callback for asynchronous profile creation.
202 void OnProfileCreated(const UserContext& user_context,
203 bool is_incognito_profile,
205 Profile::CreateStatus status);
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);
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);
220 // Callback to resume profile creation after transferring auth data from
221 // the authentication profile.
222 void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
224 // Finalized profile preparation.
225 void FinalizePrepareProfile(Profile* profile);
227 // Initializes member variables needed for session restore process via
228 // OAuthLoginManager.
229 void InitSessionRestoreStrategy();
231 // Restores GAIA auth cookies for the created user profile from OAuth2 token.
232 void RestoreAuthSessionImpl(Profile* profile,
233 bool restore_from_auth_cookies);
235 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
236 void InitRlzImpl(Profile* profile, bool disabled);
238 // Starts loading CRL set.
239 void InitializeCRLSetFetcher(const user_manager::User* user);
241 // Callback to process RetrieveActiveSessions() request results.
242 void OnRestoreActiveSessions(
243 const SessionManagerClient::ActiveSessionsMap& sessions,
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();
252 // Notifies observers that user pending sessions restore has finished.
253 void NotifyPendingUserSessionsRestoreFinished();
255 // Update Easy unlock cryptohome keys using the pairing data in user prefs.
256 void UpdateEasyUnlockKeys(Profile* user_profile);
258 // Callback invoked when Easy unlock key operations are finished.
259 void OnEasyUnlockKeyOpsFinished(bool success);
261 UserSessionManagerDelegate* delegate_;
263 // Authentication/user context.
264 UserContext user_context_;
265 scoped_refptr<Authenticator> authenticator_;
267 // True if the authentication context's cookie jar contains authentication
268 // cookies from the authentication extension login flow.
269 bool has_auth_cookies_;
271 // Active user session restoration related members.
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_;
277 // True if user sessions restoration after crash is in progress.
278 bool user_sessions_restore_in_progress_;
280 // User sessions that have to be restored after browser crash.
281 // [user_id] > [user_id_hash]
282 SessionManagerClient::ActiveSessionsMap pending_user_sessions_;
284 ObserverList<chromeos::UserSessionStateObserver> session_state_observer_list_;
286 // OAuth2 session related members.
288 // True if we should restart chrome right after session restore.
289 bool exit_after_session_restore_;
291 // Sesion restore strategy.
292 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
294 // OAuth2 refresh token for session restore.
295 std::string oauth2_refresh_token_;
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_;
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_;
306 // Per-user-session Input Methods states.
307 std::map<Profile*, scoped_refptr<input_method::InputMethodManager::State> >
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_;
315 DISALLOW_COPY_AND_ASSIGN(UserSessionManager);
318 } // namespace chromeos
320 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_