Home | History | Annotate | Download | only in session
      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_SESSION_MANAGER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_SESSION_MANAGER_H_
      7 
      8 #include <string>
      9 
     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"
     17 
     18 class PrefRegistrySimple;
     19 class PrefService;
     20 class Profile;
     21 
     22 namespace chromeos {
     23 
     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> {
     33  public:
     34   class Delegate {
     35    public:
     36     // Called after profile is loaded and prepared for the session.
     37     virtual void OnProfilePrepared(Profile* profile) = 0;
     38 
     39 #if defined(ENABLE_RLZ)
     40     // Called after post-profile RLZ initialization.
     41     virtual void OnRlzInitialized() {}
     42 #endif
     43    protected:
     44     virtual ~Delegate() {}
     45   };
     46 
     47   // Returns SessionManager instance.
     48   static SessionManager* GetInstance();
     49 
     50   // Registers session related preferences.
     51   static void RegisterPrefs(PrefRegistrySimple* registry);
     52 
     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;
     58 
     59   // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
     60   virtual void OnConnectionTypeChanged(
     61       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
     62 
     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,
     67                     bool has_cookies,
     68                     bool has_active_session,
     69                     Delegate* delegate);
     70 
     71   // Restores authentication session after crash.
     72   void RestoreAuthenticationSession(Profile* profile);
     73 
     74   // Initialize RLZ.
     75   void InitRlz(Profile* profile);
     76 
     77   // TODO(nkostylev): Drop these methods once LoginUtilsImpl::AttemptRestart()
     78   // is migrated.
     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;
     83   }
     84 
     85   // Invoked when the user is logging in for the first time, or is logging in as
     86   // a guest user.
     87   static void SetFirstLoginPrefs(PrefService* prefs);
     88 
     89  private:
     90   friend struct DefaultSingletonTraits<SessionManager>;
     91 
     92   typedef std::set<std::string> SessionRestoreStateSet;
     93 
     94   SessionManager();
     95   virtual ~SessionManager();
     96 
     97   void CreateUserSession(const UserContext& user_context,
     98                          bool has_cookies);
     99   void PreStartSession();
    100   void StartCrosSession();
    101   void NotifyUserLoggedIn();
    102   void PrepareProfile();
    103 
    104   // Callback for asynchronous profile creation.
    105   void OnProfileCreated(const std::string& user_id,
    106                         bool is_incognito_profile,
    107                         Profile* profile,
    108                         Profile::CreateStatus status);
    109 
    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);
    116 
    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);
    120 
    121   // Callback to resume profile creation after transferring auth data from
    122   // the authentication profile.
    123   void CompleteProfileCreateAfterAuthTransfer(Profile* profile);
    124 
    125   // Finalized profile preparation.
    126   void FinalizePrepareProfile(Profile* profile);
    127 
    128   // Initializes member variables needed for session restore process via
    129   // OAuthLoginManager.
    130   void InitSessionRestoreStrategy();
    131 
    132   // Restores GAIA auth cookies for the created user profile from OAuth2 token.
    133   void RestoreAuthSessionImpl(Profile* profile,
    134                               bool restore_from_auth_cookies);
    135 
    136   // Initializes RLZ. If |disabled| is true, RLZ pings are disabled.
    137   void InitRlzImpl(Profile* profile, bool disabled);
    138 
    139   Delegate* delegate_;
    140 
    141   // Authentication/user context.
    142   UserContext user_context_;
    143   scoped_refptr<Authenticator> authenticator_;
    144 
    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_;
    148 
    149   // OAuth2 session related members.
    150 
    151   // True if we should restart chrome right after session restore.
    152   bool exit_after_session_restore_;
    153 
    154   // Sesion restore strategy.
    155   OAuth2LoginManager::SessionRestoreStrategy session_restore_strategy_;
    156 
    157   // OAuth2 refresh token for session restore.
    158   std::string oauth2_refresh_token_;
    159 
    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_;
    163 
    164   DISALLOW_COPY_AND_ASSIGN(SessionManager);
    165 };
    166 
    167 }  // namespace chromeos
    168 
    169 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SESSION_SESSION_MANAGER_H_
    170