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/memory/singleton.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/chromeos/base/locale_util.h"
15 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
16 #include "chromeos/dbus/session_manager_client.h"
17 #include "chromeos/login/auth/authenticator.h"
18 #include "chromeos/login/auth/user_context.h"
19 #include "components/user_manager/user.h"
20 #include "net/base/network_change_notifier.h"
22 class PrefRegistrySimple;
28 class UserSessionManagerDelegate {
30 // Called after profile is loaded and prepared for the session.
31 virtual void OnProfilePrepared(Profile* profile) = 0;
33 #if defined(ENABLE_RLZ)
34 // Called after post-profile RLZ initialization.
35 virtual void OnRlzInitialized();
38 virtual ~UserSessionManagerDelegate();
41 class UserSessionStateObserver {
43 // Called when UserManager finishes restoring user sessions after crash.
44 virtual void PendingUserSessionsRestoreFinished();
47 virtual ~UserSessionStateObserver();
50 // UserSessionManager is responsible for starting user session which includes:
51 // load and initialize Profile (including custom Profile preferences),
52 // mark user as logged in and notify observers,
53 // initialize OAuth2 authentication session,
54 // initialize and launch user session based on the user type.
55 // Also supports restoring active user sessions after browser crash:
56 // load profile, restore OAuth authentication session etc.
57 class UserSessionManager
58 : public OAuth2LoginManager::Observer,
59 public net::NetworkChangeNotifier::ConnectionTypeObserver,
60 public base::SupportsWeakPtr<UserSessionManager>,
61 public UserSessionManagerDelegate {
63 // Returns UserSessionManager instance.
64 static UserSessionManager* GetInstance();
66 // Called when user is logged in to override base::DIR_HOME path.
67 static void OverrideHomedir();
69 // Registers session related preferences.
70 static void RegisterPrefs(PrefRegistrySimple* registry);
72 // Start user session given |user_context| and |authenticator| which holds
73 // authentication context (profile).
74 void StartSession(const UserContext& user_context,
75 scoped_refptr<Authenticator> authenticator,
76 bool has_auth_cookies,
77 bool has_active_session,
78 UserSessionManagerDelegate* delegate);
80 // Perform additional actions once system wide notification
81 // "UserLoggedIn" has been sent.
82 void PerformPostUserLoggedInActions();
84 // Restores authentication session after crash.
85 void RestoreAuthenticationSession(Profile* profile);
87 // Usually is called when Chrome is restarted after a crash and there's an
88 // active session. First user (one that is passed with --login-user) Chrome
89 // session has been already restored at this point. This method asks session
90 // manager for all active user sessions, marks them as logged in
91 // and notifies observers.
92 void RestoreActiveSessions();
94 // Returns true iff browser has been restarted after crash and UserManager
95 // finished restoring user sessions.
96 bool UserSessionsRestored() const;
99 void InitRlz(Profile* profile);
101 // Get the NSS cert database for the user represented with |profile|
102 // and start certificate loader with it.
103 void InitializeCerts(Profile* profile);
105 // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
107 OAuth2LoginManager::SessionRestoreStrategy GetSigninSessionRestoreStrategy();
108 bool exit_after_session_restore() { return exit_after_session_restore_; }
109 void set_exit_after_session_restore(bool value) {
110 exit_after_session_restore_ = value;
113 // Invoked when the user is logging in for the first time, or is logging in to
114 // an ephemeral session type, such as guest or a public session.
115 static void SetFirstLoginPrefs(
117 const std::string& public_session_locale,
118 const std::string& public_session_input_method);
120 // Gets/sets Chrome OAuth client id and secret for kiosk app mode. The default
121 // values can be overridden with kiosk auth file.
122 bool GetAppModeChromeClientOAuthInfo(
123 std::string* chrome_client_id,
124 std::string* chrome_client_secret);
125 void SetAppModeChromeClientOAuthInfo(
126 const std::string& chrome_client_id,
127 const std::string& chrome_client_secret);
129 // Changes browser locale (selects best suitable locale from different
130 // user settings). Returns true if callback will be called.
131 // Returns true if callback will be called.
132 bool RespectLocalePreference(
134 const user_manager::User* user,
135 scoped_ptr<locale_util::SwitchLanguageCallback> callback) const;
137 void AddSessionStateObserver(UserSessionStateObserver* observer);
138 void RemoveSessionStateObserver(UserSessionStateObserver* observer);
141 friend struct DefaultSingletonTraits<UserSessionManager>;
143 typedef std::set<std::string> SigninSessionRestoreStateSet;
145 UserSessionManager();
146 virtual ~UserSessionManager();
148 // OAuth2LoginManager::Observer overrides:
149 virtual void OnSessionRestoreStateChanged(
150 Profile* user_profile,
151 OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
152 virtual void OnNewRefreshTokenAvaiable(Profile* user_profile) OVERRIDE;
154 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
155 virtual void OnConnectionTypeChanged(
156 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
158 // UserSessionManagerDelegate overrides:
159 // Used when restoring user sessions after crash.
160 virtual void OnProfilePrepared(Profile* profile) OVERRIDE;
162 void CreateUserSession(const UserContext& user_context,
163 bool has_auth_cookies);
164 void PreStartSession();
165 void StartCrosSession();
166 void NotifyUserLoggedIn();
167 void PrepareProfile();
169 // Callback for asynchronous profile creation.
170 void OnProfileCreated(const UserContext& user_context,
171 bool is_incognito_profile,
173 Profile::CreateStatus status);
175 // Callback for Profile::CREATE_STATUS_CREATED profile state.
176 // Initializes basic preferences for newly created profile. Any other
177 // early profile initialization that needs to happen before
178 // ProfileManager::DoFinalInit() gets called is done here.
179 void InitProfilePreferences(Profile* profile,
180 const UserContext& user_context);
182 // Callback for Profile::CREATE_STATUS_INITIALIZED profile state.
183 // Profile is created, extensions and promo resources are initialized.
184 void UserProfileInitialized(Profile* profile,
185 bool is_incognito_profile,
186 const std::string& user_id);
188 // Callback to resume profile creation after transferring auth data from
189 // the authentication profile.
190 void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
192 // Finalized profile preparation.
193 void FinalizePrepareProfile(Profile* profile);
195 // Initializes member variables needed for session restore process via
196 // OAuthLoginManager.
197 void InitSessionRestoreStrategy();
199 // Restores GAIA auth cookies for the created user profile from OAuth2 token.
200 void RestoreAuthSessionImpl(Profile* profile,
201 bool restore_from_auth_cookies);
203 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
204 void InitRlzImpl(Profile* profile, bool disabled);
206 // Starts loading CRL set.
207 void InitializeCRLSetFetcher(const user_manager::User* user);
209 // Callback to process RetrieveActiveSessions() request results.
210 void OnRestoreActiveSessions(
211 const SessionManagerClient::ActiveSessionsMap& sessions,
214 // Called by OnRestoreActiveSessions() when there're user sessions in
215 // |pending_user_sessions_| that has to be restored one by one.
216 // Also called after first user session from that list is restored and so on.
217 // Process continues till |pending_user_sessions_| map is not empty.
218 void RestorePendingUserSessions();
220 // Notifies observers that user pending sessions restore has finished.
221 void NotifyPendingUserSessionsRestoreFinished();
223 UserSessionManagerDelegate* delegate_;
225 // Authentication/user context.
226 UserContext user_context_;
227 scoped_refptr<Authenticator> authenticator_;
229 // True if the authentication context's cookie jar contains authentication
230 // cookies from the authentication extension login flow.
231 bool has_auth_cookies_;
233 // Active user session restoration related members.
235 // True is user sessions has been restored after crash.
236 // On a normal boot then login into user sessions this will be false.
237 bool user_sessions_restored_;
239 // User sessions that have to be restored after browser crash.
240 // [user_id] > [user_id_hash]
241 SessionManagerClient::ActiveSessionsMap pending_user_sessions_;
243 ObserverList<UserSessionStateObserver> session_state_observer_list_;
245 // OAuth2 session related members.
247 // True if we should restart chrome right after session restore.
248 bool exit_after_session_restore_;
250 // Sesion restore strategy.
251 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
253 // OAuth2 refresh token for session restore.
254 std::string oauth2_refresh_token_;
256 // Set of user_id for those users that we should restore authentication
257 // session when notified about online state change.
258 SigninSessionRestoreStateSet pending_signin_restore_sessions_;
260 // Kiosk mode related members.
261 // Chrome oauth client id and secret - override values for kiosk mode.
262 std::string chrome_client_id_;
263 std::string chrome_client_secret_;
265 DISALLOW_COPY_AND_ASSIGN(UserSessionManager);
268 } // namespace chromeos
270 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_