Chromium Code Reviews| 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 "chrome/browser/chromeos/login/oauth2_login_verifier.h" | 12 #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/oauth2_token_fetcher.h" |
| 13 #include "chrome/browser/chromeos/login/oauth_login_manager.h" | 14 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" |
| 14 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
| 16 | 17 |
| 17 class GoogleServiceAuthError; | 18 class GoogleServiceAuthError; |
| 18 class Profile; | 19 class Profile; |
| 19 class TokenService; | 20 class TokenService; |
| 20 | 21 |
| 21 namespace chromeos { | 22 namespace chromeos { |
| 22 | 23 |
| 23 // OAuth2 specialization of OAuthLoginManager. | 24 // OAuth2 specialization of OAuthLoginManager. |
| 24 class OAuth2LoginManager : public OAuthLoginManager, | 25 class OAuth2LoginManager : public BrowserContextKeyedService, |
|
xiyuan
2013/09/06 01:35:49
nit: Update comments.
zel
2013/09/07 02:03:42
Done.
| |
| 25 public OAuth2LoginVerifier::Delegate, | 26 public OAuth2LoginVerifier::Delegate, |
| 26 public OAuth2TokenFetcher::Delegate, | 27 public OAuth2TokenFetcher::Delegate, |
| 27 public OAuth2TokenService::Observer { | 28 public OAuth2TokenService::Observer { |
| 28 public: | 29 public: |
| 29 explicit OAuth2LoginManager(OAuthLoginManager::Delegate* delegate); | 30 // Session restore states. |
| 31 enum SessionRestoreState { | |
| 32 // Session restore is not started. | |
| 33 SESSION_RESTORE_NOT_STARTED, | |
| 34 // Session restore is in progress. We are currently issuing calls to verify | |
| 35 // stored OAuth tokens and populate cookie jar with GAIA credentials. | |
| 36 SESSION_RESTORE_IN_PROGRESS, | |
| 37 // Session restore is completed. | |
| 38 SESSION_RESTORE_DONE, | |
| 39 }; | |
| 40 | |
| 41 // Session restore strategy. | |
| 42 enum SessionRestoreStrategy { | |
| 43 // Generate OAuth2 refresh token from authentication profile's cookie jar. | |
| 44 // Restore session from generated OAuth2 refresh token. | |
| 45 RESTORE_FROM_COOKIE_JAR, | |
| 46 // Restore session from saved OAuth2 refresh token from TokenServices. | |
| 47 RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN, | |
| 48 // Restore session from OAuth2 refresh token passed via command line. | |
| 49 RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN, | |
| 50 // Restore session from authentication code passed via command line. | |
| 51 RESTORE_FROM_AUTH_CODE, | |
| 52 }; | |
| 53 | |
| 54 class Observer { | |
| 55 public: | |
| 56 virtual ~Observer() {} | |
| 57 | |
| 58 // Raised when merge session process starts. | |
| 59 virtual void OnStartMergeSession(Profile* user_profile) {} | |
| 60 | |
| 61 // Raised when merge session is completed. | |
| 62 virtual void OnCompletedMergeSession(Profile* user_profile) {} | |
| 63 | |
| 64 // Raised when cookie jar authentication is successfully completed. | |
| 65 virtual void OnCompletedAuthentication(Profile* user_profile) {} | |
| 66 }; | |
| 67 | |
| 68 explicit OAuth2LoginManager(Profile* user_profile); | |
| 30 virtual ~OAuth2LoginManager(); | 69 virtual ~OAuth2LoginManager(); |
| 31 | 70 |
| 32 // OAuthLoginManager overrides. | 71 void AddObserver(OAuth2LoginManager::Observer* observer); |
| 33 virtual void RestoreSession( | 72 void RemoveObserver(OAuth2LoginManager::Observer* observer); |
| 34 Profile* user_profile, | 73 |
| 74 // Restores and verifies OAuth tokens either following specified | |
| 75 // |restore_strategy|. For |restore_strategy| with values | |
| 76 // RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN or | |
| 77 // RESTORE_FROM_AUTH_CODE, respectively | |
| 78 // parameters |oauth2_refresh_token| or |auth_code| need to have non-empty | |
| 79 // value. | |
| 80 void RestoreSession( | |
| 35 net::URLRequestContextGetter* auth_request_context, | 81 net::URLRequestContextGetter* auth_request_context, |
| 36 SessionRestoreStrategy restore_strategy, | 82 SessionRestoreStrategy restore_strategy, |
| 37 const std::string& oauth2_refresh_token, | 83 const std::string& oauth2_refresh_token, |
| 38 const std::string& auth_code) OVERRIDE; | 84 const std::string& auth_code) OVERRIDE; |
| 39 virtual void ContinueSessionRestore() OVERRIDE; | 85 |
| 40 virtual void Stop() OVERRIDE; | 86 // Continues session restore after transient network errors. |
| 87 void ContinueSessionRestore(); | |
| 88 | |
| 89 // Stops all background authentication requests. | |
| 90 void Stop(); | |
| 91 | |
| 92 // Returns session restore state. | |
| 93 SessionRestoreState state() { return state_; } | |
| 41 | 94 |
| 42 private: | 95 private: |
| 43 // Session restore outcomes (for UMA). | 96 // Session restore outcomes (for UMA). |
| 44 enum { | 97 enum { |
| 45 SESSION_RESTORE_UNDEFINED = 0, | 98 SESSION_RESTORE_UNDEFINED = 0, |
| 46 SESSION_RESTORE_SUCCESS = 1, | 99 SESSION_RESTORE_SUCCESS = 1, |
| 47 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, | 100 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, |
| 48 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, | 101 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, |
| 49 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, | 102 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, |
| 50 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, | 103 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, |
| 51 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, | 104 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, |
| 52 }; | 105 }; |
| 53 | 106 |
| 107 // BrowserContextKeyedService implementation. | |
| 108 virtual void Shutdown() OVERRIDE; | |
| 109 | |
| 54 // OAuth2LoginVerifier::Delegate overrides. | 110 // OAuth2LoginVerifier::Delegate overrides. |
| 55 virtual void OnOAuthLoginSuccess( | 111 virtual void OnOAuthLoginSuccess( |
| 56 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; | 112 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; |
| 57 virtual void OnOAuthLoginFailure() OVERRIDE; | 113 virtual void OnOAuthLoginFailure() OVERRIDE; |
| 58 virtual void OnSessionMergeSuccess() OVERRIDE; | 114 virtual void OnSessionMergeSuccess() OVERRIDE; |
| 59 virtual void OnSessionMergeFailure() OVERRIDE; | 115 virtual void OnSessionMergeFailure() OVERRIDE; |
| 60 | 116 |
| 61 // OAuth2TokenFetcher::Delegate overrides. | 117 // OAuth2TokenFetcher::Delegate overrides. |
| 62 virtual void OnOAuth2TokensAvailable( | 118 virtual void OnOAuth2TokensAvailable( |
| 63 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; | 119 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; |
| 64 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; | 120 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; |
| 65 | 121 |
| 66 // OAuth2TokenService::Observer implementation: | 122 // OAuth2TokenService::Observer implementation: |
| 67 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 123 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
| 68 | 124 |
| 125 // Signals delegate that authentication is completed, kicks off token fetching | |
| 126 // process in TokenService. | |
| 127 void CompleteAuthentication(); | |
| 128 | |
| 69 // Retrieves TokenService for |user_profile_| and sets up notification | 129 // Retrieves TokenService for |user_profile_| and sets up notification |
| 70 // observer events. | 130 // observer events. |
| 71 TokenService* SetupTokenService(); | 131 TokenService* SetupTokenService(); |
| 72 | 132 |
| 73 // Records OAuth2 tokens fetched through cookies-to-token exchange into | 133 // Records OAuth2 tokens fetched through cookies-to-token exchange into |
| 74 // TokenService. | 134 // TokenService. |
| 75 void StoreOAuth2Tokens( | 135 void StoreOAuth2Tokens( |
| 76 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); | 136 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); |
| 77 | 137 |
| 78 // Loads previously stored OAuth2 tokens and kicks off its validation. | 138 // Loads previously stored OAuth2 tokens and kicks off its validation. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 89 void RestoreSessionCookies(); | 149 void RestoreSessionCookies(); |
| 90 | 150 |
| 91 // Checks GAIA error and figures out whether the request should be | 151 // Checks GAIA error and figures out whether the request should be |
| 92 // re-attempted. | 152 // re-attempted. |
| 93 bool RetryOnError(const GoogleServiceAuthError& error); | 153 bool RetryOnError(const GoogleServiceAuthError& error); |
| 94 | 154 |
| 95 // On successfuly OAuthLogin, starts token service token fetching process. | 155 // On successfuly OAuthLogin, starts token service token fetching process. |
| 96 void StartTokenService( | 156 void StartTokenService( |
| 97 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); | 157 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); |
| 98 | 158 |
| 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 | 159 // Keeps the track if we have already reported OAuth2 token being loaded |
| 103 // by TokenService. | 160 // by TokenService. |
| 161 Profile* user_profile_; | |
| 162 scoped_refptr<net::URLRequestContextGetter> auth_request_context_; | |
| 163 SessionRestoreStrategy restore_strategy_; | |
| 164 SessionRestoreState state_; | |
| 165 | |
| 104 bool loading_reported_; | 166 bool loading_reported_; |
| 167 | |
| 105 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; | 168 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; |
| 106 scoped_ptr<OAuth2LoginVerifier> login_verifier_; | 169 scoped_ptr<OAuth2LoginVerifier> login_verifier_; |
| 170 | |
| 107 // OAuth2 refresh token. | 171 // OAuth2 refresh token. |
| 108 std::string refresh_token_; | 172 std::string refresh_token_; |
| 173 | |
| 109 // Authorization code for fetching OAuth2 tokens. | 174 // Authorization code for fetching OAuth2 tokens. |
| 110 std::string auth_code_; | 175 std::string auth_code_; |
| 111 | 176 |
| 177 // List of observers to notify when token availability changes. | |
| 178 // Makes sure list is empty on destruction. | |
| 179 ObserverList<Observer, true> observer_list_; | |
| 180 | |
| 112 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); | 181 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); |
| 113 }; | 182 }; |
| 114 | 183 |
| 115 } // namespace chromeos | 184 } // namespace chromeos |
| 116 | 185 |
| 117 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ | 186 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ |
| OLD | NEW |