1 // Copyright (c) 2013 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_OAUTH2_LOGIN_MANAGER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ 7 8 #include <string> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "chrome/browser/chromeos/login/oauth2_login_verifier.h" 12 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" 13 #include "chrome/browser/chromeos/login/oauth_login_manager.h" 14 #include "chrome/browser/signin/oauth2_token_service.h" 15 #include "net/url_request/url_request_context_getter.h" 16 17 class GoogleServiceAuthError; 18 class Profile; 19 class TokenService; 20 21 namespace chromeos { 22 23 // OAuth2 specialization of OAuthLoginManager. 24 class OAuth2LoginManager : public OAuthLoginManager, 25 public OAuth2LoginVerifier::Delegate, 26 public OAuth2TokenFetcher::Delegate, 27 public OAuth2TokenService::Observer { 28 public: 29 explicit OAuth2LoginManager(OAuthLoginManager::Delegate* delegate); 30 virtual ~OAuth2LoginManager(); 31 32 // OAuthLoginManager overrides. 33 virtual void RestoreSession( 34 Profile* user_profile, 35 net::URLRequestContextGetter* auth_request_context, 36 SessionRestoreStrategy restore_strategy, 37 const std::string& oauth2_refresh_token, 38 const std::string& auth_code) OVERRIDE; 39 virtual void ContinueSessionRestore() OVERRIDE; 40 virtual void Stop() OVERRIDE; 41 42 private: 43 // Session restore outcomes (for UMA). 44 enum { 45 SESSION_RESTORE_UNDEFINED = 0, 46 SESSION_RESTORE_SUCCESS = 1, 47 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, 48 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, 49 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, 50 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, 51 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, 52 }; 53 54 // OAuth2LoginVerifier::Delegate overrides. 55 virtual void OnOAuthLoginSuccess( 56 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; 57 virtual void OnOAuthLoginFailure() OVERRIDE; 58 virtual void OnSessionMergeSuccess() OVERRIDE; 59 virtual void OnSessionMergeFailure() OVERRIDE; 60 61 // OAuth2TokenFetcher::Delegate overrides. 62 virtual void OnOAuth2TokensAvailable( 63 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; 64 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; 65 66 // OAuth2TokenService::Observer implementation: 67 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; 68 69 // Retrieves TokenService for |user_profile_| and sets up notification 70 // observer events. 71 TokenService* SetupTokenService(); 72 73 // Records OAuth2 tokens fetched through cookies-to-token exchange into 74 // TokenService. 75 void StoreOAuth2Tokens( 76 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); 77 78 // Loads previously stored OAuth2 tokens and kicks off its validation. 79 void LoadAndVerifyOAuth2Tokens(); 80 81 // Attempts to fetch OAuth2 tokens by using pre-authenticated cookie jar from 82 // provided |auth_profile|. 83 void FetchOAuth2Tokens(); 84 85 // Reports when all tokens are loaded. 86 void ReportOAuth2TokensLoaded(); 87 88 // Issue GAIA cookie recovery (MergeSession) from |refresh_token_|. 89 void RestoreSessionCookies(); 90 91 // Checks GAIA error and figures out whether the request should be 92 // re-attempted. 93 bool RetryOnError(const GoogleServiceAuthError& error); 94 95 // On successfuly OAuthLogin, starts token service token fetching process. 96 void StartTokenService( 97 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); 98 99 // Stops listening for a new login refresh token. 100 void StopObservingRefreshToken(); 101 102 // Keeps the track if we have already reported OAuth2 token being loaded 103 // by TokenService. 104 bool loading_reported_; 105 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; 106 scoped_ptr<OAuth2LoginVerifier> login_verifier_; 107 // OAuth2 refresh token. 108 std::string refresh_token_; 109 // Authorization code for fetching OAuth2 tokens. 110 std::string auth_code_; 111 112 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); 113 }; 114 115 } // namespace chromeos 116 117 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ 118