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_SESSION_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_SESSION_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/singleton.h"
12 #include "base/memory/weak_ptr.h"
13 #include "chrome/browser/chromeos/login/auth/authenticator.h"
14 #include "chrome/browser/chromeos/login/auth/user_context.h"
15 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
16 #include "net/base/network_change_notifier.h"
18 class PrefRegistrySimple;
24 // SessionManager is responsible for starting user session which includes:
25 // load and initialize Profile (including custom Profile preferences),
26 // mark user as logged in and notify observers,
27 // initialize OAuth2 authentication session,
28 // initialize and launch user session based on the user type.
29 class SessionManager :
30 public OAuth2LoginManager::Observer,
31 public net::NetworkChangeNotifier::ConnectionTypeObserver,
32 public base::SupportsWeakPtr<SessionManager> {
36 // Called after profile is loaded and prepared for the session.
37 virtual void OnProfilePrepared(Profile* profile) = 0;
39 #if defined(ENABLE_RLZ)
40 // Called after post-profile RLZ initialization.
41 virtual void OnRlzInitialized() {}
44 virtual ~Delegate() {}
47 // Returns SessionManager instance.
48 static SessionManager* GetInstance();
50 // Registers session related preferences.
51 static void RegisterPrefs(PrefRegistrySimple* registry);
53 // OAuth2LoginManager::Observer overrides:
54 virtual void OnSessionRestoreStateChanged(
55 Profile* user_profile,
56 OAuth2LoginManager::SessionRestoreState state) OVERRIDE;
57 virtual void OnNewRefreshTokenAvaiable(Profile* user_profile) OVERRIDE;
59 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
60 virtual void OnConnectionTypeChanged(
61 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
63 // Start user session given |user_context| and |authenticator| which holds
64 // authentication context (profile).
65 void StartSession(const UserContext& user_context,
66 scoped_refptr<Authenticator> authenticator,
68 bool has_active_session,
71 // Restores authentication session after crash.
72 void RestoreAuthenticationSession(Profile* profile);
75 void InitRlz(Profile* profile);
77 // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
79 OAuth2LoginManager::SessionRestoreStrategy GetSigninSessionRestoreStrategy();
80 bool exit_after_session_restore() { return exit_after_session_restore_; }
81 void set_exit_after_session_restore(bool value) {
82 exit_after_session_restore_ = value;
85 // Invoked when the user is logging in for the first time, or is logging in as
87 static void SetFirstLoginPrefs(PrefService* prefs);
90 friend struct DefaultSingletonTraits<SessionManager>;
92 typedef std::set<std::string> SessionRestoreStateSet;
95 virtual ~SessionManager();
97 void CreateUserSession(const UserContext& user_context,
99 void PreStartSession();
100 void StartCrosSession();
101 void NotifyUserLoggedIn();
102 void PrepareProfile();
104 // Callback for asynchronous profile creation.
105 void OnProfileCreated(const std::string& user_id,
106 bool is_incognito_profile,
108 Profile::CreateStatus status);
110 // Callback for Profile::CREATE_STATUS_CREATED profile state.
111 // Initializes basic preferences for newly created profile. Any other
112 // early profile initialization that needs to happen before
113 // ProfileManager::DoFinalInit() gets called is done here.
114 void InitProfilePreferences(Profile* profile,
115 const std::string& email);
117 // Callback for Profile::CREATE_STATUS_INITIALIZED profile state.
118 // Profile is created, extensions and promo resources are initialized.
119 void UserProfileInitialized(Profile* profile, bool is_incognito_profile);
121 // Callback to resume profile creation after transferring auth data from
122 // the authentication profile.
123 void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
125 // Finalized profile preparation.
126 void FinalizePrepareProfile(Profile* profile);
128 // Initializes member variables needed for session restore process via
129 // OAuthLoginManager.
130 void InitSessionRestoreStrategy();
132 // Restores GAIA auth cookies for the created user profile from OAuth2 token.
133 void RestoreAuthSessionImpl(Profile* profile,
134 bool restore_from_auth_cookies);
136 // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
137 void InitRlzImpl(Profile* profile, bool disabled);
141 // Authentication/user context.
142 UserContext user_context_;
143 scoped_refptr<Authenticator> authenticator_;
145 // True if the authentication context cookie jar should contain
146 // authentication cookies from the authentication extension log in flow.
147 bool has_web_auth_cookies_;
149 // OAuth2 session related members.
151 // True if we should restart chrome right after session restore.
152 bool exit_after_session_restore_;
154 // Sesion restore strategy.
155 OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
157 // OAuth2 refresh token for session restore.
158 std::string oauth2_refresh_token_;
160 // Set of user_id for those users that we should restore authentication
161 // session when notified about online state change.
162 SessionRestoreStateSet pending_restore_sessions_;
164 DISALLOW_COPY_AND_ASSIGN(SessionManager);
167 } // namespace chromeos
169 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_SESSION_MANAGER_H_