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"
27 class PrefRegistrySimple;
31 namespace user_manager {
35 } // namespace user_manager
39 class EasyUnlockKeyManager;
41 class UserSessionManagerDelegate {
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;
49 #if defined(ENABLE_RLZ)
50 // Called after post-profile RLZ initialization.
51 virtual void OnRlzInitialized();
54 virtual ~UserSessionManagerDelegate();
57 class UserSessionStateObserver {
59 // Called when UserManager finishes restoring user sessions after crash.
60 virtual void PendingUserSessionsRestoreFinished();
63 virtual ~UserSessionStateObserver();
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 {
80 // Context of StartSession calls.
82 // Starting primary user session, through login UI.
85 // Starting secondary user session, through multi-profiles login UI.
86 SECONDARY_USER_SESSION,
88 // Starting primary user session after browser crash.
89 PRIMARY_USER_SESSION_AFTER_CRASH,
91 // Starting secondary user session after browser crash.
92 SECONDARY_USER_SESSION_AFTER_CRASH,
95 // Returns UserSessionManager instance.
96 static UserSessionManager* GetInstance();
98 // Called when user is logged in to override base::DIR_HOME path.
99 static void OverrideHomedir();
101 // Registers session related preferences.
102 static void RegisterPrefs(PrefRegistrySimple* registry);
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);
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);
118 // Perform additional actions once system wide notification
119 // "UserLoggedIn" has been sent.
120 void PerformPostUserLoggedInActions();
122 // Restores authentication session after crash.
123 void RestoreAuthenticationSession(Profile* profile);
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();
132 // Returns true iff browser has been restarted after crash and
133 // UserSessionManager finished restoring user sessions.
134 bool UserSessionsRestored() const;
136 // Returns true iff browser has been restarted after crash and
137 // user sessions restoration is in progress.
138 bool UserSessionsRestoreInProgress() const;
141 void InitRlz(Profile* profile);
143 // Get the NSS cert database for the user represented with |profile|
144 // and start certificate loader with it.
145 void InitializeCerts(Profile* profile);
147 // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
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;
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);
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);
170 // Changes browser locale (selects best suitable locale from different
171 // user settings). Returns true if callback will be called.
172 bool RespectLocalePreference(
174 const user_manager::User* user,
175 scoped_ptr<locale_util::SwitchLanguageCallback> callback) const;
177 // Returns true if Easy unlock keys needs to be updated.
178 bool NeedsToUpdateEasyUnlockKeys() const;
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);
184 void AddSessionStateObserver(chromeos::UserSessionStateObserver* observer);
185 void RemoveSessionStateObserver(chromeos::UserSessionStateObserver* observer);
187 virtual void ActiveUserChanged(
188 const user_manager::User* active_user) override;
190 // Returns default IME state for user session.
191 scoped_refptr<input_method::InputMethodManager::State> GetDefaultIMEState(
194 // Note this could return NULL if not enabled.
195 EasyUnlockKeyManager* GetEasyUnlockKeyManager();
197 // Update Easy unlock cryptohome keys for given user context.
198 void UpdateEasyUnlockKeys(const UserContext& user_context);
201 friend struct DefaultSingletonTraits<UserSessionManager>;
203 typedef std::set<std::string> SigninSessionRestoreStateSet;
205 UserSessionManager();
206 virtual ~UserSessionManager();
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;
214 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
215 virtual void OnConnectionTypeChanged(
216 net::NetworkChangeNotifier::ConnectionType type) override;
218 // UserSessionManagerDelegate overrides:
219 // Used when restoring user sessions after crash.
220 virtual void OnProfilePrepared(Profile* profile,
221 bool browser_launched) override;
223 void CreateUserSession(const UserContext& user_context,
224 bool has_auth_cookies);
225 void PreStartSession();
226 void StartCrosSession();
227 void NotifyUserLoggedIn();
228 void PrepareProfile();
230 // Callback for asynchronous profile creation.
231 void OnProfileCreated(const UserContext& user_context,
232 bool is_incognito_profile,
234 Profile::CreateStatus status);
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);
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);
249 // Callback to resume profile creation after transferring auth data from
250 // the authentication profile.
251 void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
253 // Finalized profile preparation.
254 void FinalizePrepareProfile(Profile* profile);
256 // Starts out-of-box flow with the specified screen.
257 void ActivateWizard(const std::string& screen_name);
259 // Adds first-time login URLs.
260 void InitializeStartUrls() const;
262 // Perform session initialization and either move to additional login flows
263 // such as TOS (public sessions), priority pref sync UI (new users) or
265 // Returns true if browser has been launched or false otherwise.
266 bool InitializeUserSession(Profile* profile);
268 // Initializes member variables needed for session restore process via
269 // OAuthLoginManager.
270 void InitSessionRestoreStrategy();
272 // Restores GAIA auth cookies for the created user profile from OAuth2 token.
273 void RestoreAuthSessionImpl(Profile* profile,
274 bool restore_from_auth_cookies);
276 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
277 void InitRlzImpl(Profile* profile, bool disabled);
279 // Starts loading CRL set.
280 void InitializeCRLSetFetcher(const user_manager::User* user);
282 // Callback to process RetrieveActiveSessions() request results.
283 void OnRestoreActiveSessions(
284 const SessionManagerClient::ActiveSessionsMap& sessions,
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();
293 // Notifies observers that user pending sessions restore has finished.
294 void NotifyPendingUserSessionsRestoreFinished();
296 // Callback invoked when Easy unlock key operations are finished.
297 void OnEasyUnlockKeyOpsFinished(const std::string& user_id,
300 UserSessionManagerDelegate* delegate_;
302 // Authentication/user context.
303 UserContext user_context_;
304 scoped_refptr<Authenticator> authenticator_;
305 StartSessionType start_session_type_;
307 // True if the authentication context's cookie jar contains authentication
308 // cookies from the authentication extension login flow.
309 bool has_auth_cookies_;
311 // Active user session restoration related members.
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_;
317 // True if user sessions restoration after crash is in progress.
318 bool user_sessions_restore_in_progress_;
320 // User sessions that have to be restored after browser crash.
321 // [user_id] > [user_id_hash]
322 SessionManagerClient::ActiveSessionsMap pending_user_sessions_;
324 ObserverList<chromeos::UserSessionStateObserver> session_state_observer_list_;
326 // OAuth2 session related members.
328 // True if we should restart chrome right after session restore.
329 bool exit_after_session_restore_;
331 // Sesion restore strategy.
332 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
334 // OAuth2 refresh token for session restore.
335 std::string oauth2_refresh_token_;
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_;
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_;
346 // Per-user-session Input Methods states.
347 std::map<Profile*, scoped_refptr<input_method::InputMethodManager::State>,
348 ProfileCompare> default_ime_states_;
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_;
355 DISALLOW_COPY_AND_ASSIGN(UserSessionManager);
358 } // namespace chromeos
360 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_USER_SESSION_MANAGER_H_