| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/chromeos/login/oauth2_login_verifier.h" | 13 #include "chrome/browser/chromeos/login/oauth2_login_verifier.h" |
| 14 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" | 14 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" |
| 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 16 #include "google_apis/gaia/gaia_oauth_client.h" |
| 16 #include "google_apis/gaia/oauth2_token_service.h" | 17 #include "google_apis/gaia/oauth2_token_service.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 18 | 19 |
| 19 class GoogleServiceAuthError; | 20 class GoogleServiceAuthError; |
| 20 class Profile; | 21 class Profile; |
| 21 class TokenService; | 22 class ProfileOAuth2TokenService; |
| 22 | 23 |
| 23 namespace chromeos { | 24 namespace chromeos { |
| 24 | 25 |
| 25 // This class is responsible for restoring authenticated web sessions out of | 26 // This class is responsible for restoring authenticated web sessions out of |
| 26 // OAuth2 refresh tokens or pre-authenticated cookie jar. | 27 // OAuth2 refresh tokens or pre-authenticated cookie jar. |
| 27 class OAuth2LoginManager : public BrowserContextKeyedService, | 28 class OAuth2LoginManager : public BrowserContextKeyedService, |
| 29 public gaia::GaiaOAuthClient::Delegate, |
| 28 public OAuth2LoginVerifier::Delegate, | 30 public OAuth2LoginVerifier::Delegate, |
| 29 public OAuth2TokenFetcher::Delegate, | 31 public OAuth2TokenFetcher::Delegate, |
| 30 public OAuth2TokenService::Observer { | 32 public OAuth2TokenService::Observer { |
| 31 public: | 33 public: |
| 32 // Session restore states. | 34 // Session restore states. |
| 33 enum SessionRestoreState { | 35 enum SessionRestoreState { |
| 34 // Session restore is not started. | 36 // Session restore is not started. |
| 35 SESSION_RESTORE_NOT_STARTED, | 37 SESSION_RESTORE_NOT_STARTED, |
| 36 // Session restore is being prepared. | 38 // Session restore is being prepared. |
| 37 SESSION_RESTORE_PREPARING, | 39 SESSION_RESTORE_PREPARING, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, | 120 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, |
| 119 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, | 121 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, |
| 120 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, | 122 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, |
| 121 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, | 123 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, |
| 122 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, | 124 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 // BrowserContextKeyedService implementation. | 127 // BrowserContextKeyedService implementation. |
| 126 virtual void Shutdown() OVERRIDE; | 128 virtual void Shutdown() OVERRIDE; |
| 127 | 129 |
| 130 // gaia::GaiaOAuthClient::Delegate overrides. |
| 131 virtual void OnRefreshTokenResponse(const std::string& access_token, |
| 132 int expires_in_seconds) OVERRIDE; |
| 133 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; |
| 134 virtual void OnOAuthError() OVERRIDE; |
| 135 virtual void OnNetworkError(int response_code) OVERRIDE; |
| 136 |
| 128 // OAuth2LoginVerifier::Delegate overrides. | 137 // OAuth2LoginVerifier::Delegate overrides. |
| 129 virtual void OnOAuthLoginSuccess( | 138 virtual void OnOAuthLoginSuccess( |
| 130 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; | 139 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; |
| 131 virtual void OnOAuthLoginFailure(bool connection_error) OVERRIDE; | 140 virtual void OnOAuthLoginFailure(bool connection_error) OVERRIDE; |
| 132 virtual void OnSessionMergeSuccess() OVERRIDE; | 141 virtual void OnSessionMergeSuccess() OVERRIDE; |
| 133 virtual void OnSessionMergeFailure(bool connection_error) OVERRIDE; | 142 virtual void OnSessionMergeFailure(bool connection_error) OVERRIDE; |
| 134 | 143 |
| 135 // OAuth2TokenFetcher::Delegate overrides. | 144 // OAuth2TokenFetcher::Delegate overrides. |
| 136 virtual void OnOAuth2TokensAvailable( | 145 virtual void OnOAuth2TokensAvailable( |
| 137 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; | 146 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; |
| 138 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; | 147 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; |
| 139 | 148 |
| 140 // OAuth2TokenService::Observer implementation: | 149 // OAuth2TokenService::Observer implementation: |
| 141 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 150 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
| 142 | 151 |
| 143 // Signals delegate that authentication is completed, kicks off token fetching | 152 // Signals delegate that authentication is completed, kicks off token fetching |
| 144 // process in TokenService. | 153 // process. |
| 145 void CompleteAuthentication(); | 154 void CompleteAuthentication(); |
| 146 | 155 |
| 147 // Retrieves TokenService for |user_profile_| and sets up notification | 156 // Retrieves ProfileOAuth2TokenService for |user_profile_|. |
| 148 // observer events. | 157 ProfileOAuth2TokenService* GetTokenService(); |
| 149 TokenService* SetupTokenService(); | |
| 150 | 158 |
| 151 // Records OAuth2 tokens fetched through cookies-to-token exchange into | 159 // Get the account id corresponding to the specified refresh token. |
| 152 // TokenService. | 160 void GetAccountIdOfRefreshToken(const std::string& refresh_token); |
| 153 void StoreOAuth2Tokens( | |
| 154 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); | |
| 155 | |
| 156 // Loads previously stored OAuth2 tokens and kicks off its validation. | |
| 157 void LoadAndVerifyOAuth2Tokens(); | |
| 158 | 161 |
| 159 // Attempts to fetch OAuth2 tokens by using pre-authenticated cookie jar from | 162 // Attempts to fetch OAuth2 tokens by using pre-authenticated cookie jar from |
| 160 // provided |auth_profile|. | 163 // provided |auth_profile|. |
| 161 void FetchOAuth2Tokens(); | 164 void FetchOAuth2Tokens(); |
| 162 | 165 |
| 163 // Reports when all tokens are loaded. | 166 // Reports when all tokens are loaded. |
| 164 void ReportOAuth2TokensLoaded(); | 167 void ReportOAuth2TokensLoaded(); |
| 165 | 168 |
| 166 // Issue GAIA cookie recovery (MergeSession) from |refresh_token_|. | 169 // Issue GAIA cookie recovery (MergeSession) from |refresh_token_|. |
| 167 void RestoreSessionCookies(); | 170 void RestoreSessionCookies(); |
| 168 | 171 |
| 169 // Checks GAIA error and figures out whether the request should be | 172 // Checks GAIA error and figures out whether the request should be |
| 170 // re-attempted. | 173 // re-attempted. |
| 171 bool RetryOnError(const GoogleServiceAuthError& error); | 174 bool RetryOnError(const GoogleServiceAuthError& error); |
| 172 | 175 |
| 173 // On successfuly OAuthLogin, starts token service token fetching process. | |
| 174 void StartTokenService( | |
| 175 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); | |
| 176 | |
| 177 // Changes |state_|, if needed fires observers (OnSessionRestoreStateChanged). | 176 // Changes |state_|, if needed fires observers (OnSessionRestoreStateChanged). |
| 178 void SetSessionRestoreState(SessionRestoreState state); | 177 void SetSessionRestoreState(SessionRestoreState state); |
| 179 | 178 |
| 180 // Testing helper. | 179 // Testing helper. |
| 181 void SetSessionRestoreStartForTesting(const base::Time& time); | 180 void SetSessionRestoreStartForTesting(const base::Time& time); |
| 182 | 181 |
| 183 // Keeps the track if we have already reported OAuth2 token being loaded | 182 // Keeps the track if we have already reported OAuth2 token being loaded |
| 184 // by TokenService. | 183 // by OAuth2TokenService. |
| 185 Profile* user_profile_; | 184 Profile* user_profile_; |
| 186 scoped_refptr<net::URLRequestContextGetter> auth_request_context_; | 185 scoped_refptr<net::URLRequestContextGetter> auth_request_context_; |
| 187 SessionRestoreStrategy restore_strategy_; | 186 SessionRestoreStrategy restore_strategy_; |
| 188 SessionRestoreState state_; | 187 SessionRestoreState state_; |
| 189 | 188 |
| 190 bool loading_reported_; | 189 bool loading_reported_; |
| 191 | 190 |
| 192 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; | 191 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; |
| 193 scoped_ptr<OAuth2LoginVerifier> login_verifier_; | 192 scoped_ptr<OAuth2LoginVerifier> login_verifier_; |
| 193 scoped_ptr<gaia::GaiaOAuthClient> account_id_fetcher_; |
| 194 | 194 |
| 195 // OAuth2 refresh token. | 195 // OAuth2 refresh token. |
| 196 std::string refresh_token_; | 196 std::string refresh_token_; |
| 197 | 197 |
| 198 // Authorization code for fetching OAuth2 tokens. | 198 // Authorization code for fetching OAuth2 tokens. |
| 199 std::string auth_code_; | 199 std::string auth_code_; |
| 200 | 200 |
| 201 // Session restore start time. | 201 // Session restore start time. |
| 202 base::Time session_restore_start_; | 202 base::Time session_restore_start_; |
| 203 | 203 |
| 204 // List of observers to notify when token availability changes. | 204 // List of observers to notify when token availability changes. |
| 205 // Makes sure list is empty on destruction. | 205 // Makes sure list is empty on destruction. |
| 206 // TODO(zelidrag|gspencer): Figure out how to get rid of ProfileHelper so we | 206 // TODO(zelidrag|gspencer): Figure out how to get rid of ProfileHelper so we |
| 207 // can change the line below to ObserverList<Observer, true>. | 207 // can change the line below to ObserverList<Observer, true>. |
| 208 ObserverList<Observer, false> observer_list_; | 208 ObserverList<Observer, false> observer_list_; |
| 209 | 209 |
| 210 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); | 210 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); |
| 211 }; | 211 }; |
| 212 | 212 |
| 213 } // namespace chromeos | 213 } // namespace chromeos |
| 214 | 214 |
| 215 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ | 215 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ |
| OLD | NEW |