| 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" |
| 12 #include "base/time/time.h" |
| 11 #include "chrome/browser/chromeos/login/oauth2_login_verifier.h" | 13 #include "chrome/browser/chromeos/login/oauth2_login_verifier.h" |
| 12 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" | 14 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" |
| 13 #include "chrome/browser/chromeos/login/oauth_login_manager.h" | 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 14 #include "google_apis/gaia/oauth2_token_service.h" | 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 16 | 18 |
| 17 class GoogleServiceAuthError; | 19 class GoogleServiceAuthError; |
| 18 class Profile; | 20 class Profile; |
| 19 class TokenService; | 21 class TokenService; |
| 20 | 22 |
| 21 namespace chromeos { | 23 namespace chromeos { |
| 22 | 24 |
| 23 // OAuth2 specialization of OAuthLoginManager. | 25 // This class is responsible for restoring authenticated web sessions out of |
| 24 class OAuth2LoginManager : public OAuthLoginManager, | 26 // OAuth2 refresh tokens or pre-authenticated cookie jar. |
| 27 class OAuth2LoginManager : public BrowserContextKeyedService, |
| 25 public OAuth2LoginVerifier::Delegate, | 28 public OAuth2LoginVerifier::Delegate, |
| 26 public OAuth2TokenFetcher::Delegate, | 29 public OAuth2TokenFetcher::Delegate, |
| 27 public OAuth2TokenService::Observer { | 30 public OAuth2TokenService::Observer { |
| 28 public: | 31 public: |
| 29 explicit OAuth2LoginManager(OAuthLoginManager::Delegate* delegate); | 32 // Session restore states. |
| 33 enum SessionRestoreState { |
| 34 // Session restore is not started. |
| 35 SESSION_RESTORE_NOT_STARTED, |
| 36 // Session restore is being prepared. |
| 37 SESSION_RESTORE_PREPARING, |
| 38 // Session restore is in progress. We are currently issuing calls to verify |
| 39 // stored OAuth tokens and populate cookie jar with GAIA credentials. |
| 40 SESSION_RESTORE_IN_PROGRESS, |
| 41 // Session restore is completed. |
| 42 SESSION_RESTORE_DONE, |
| 43 // Session restore failed. |
| 44 SESSION_RESTORE_FAILED, |
| 45 }; |
| 46 |
| 47 // Session restore strategy. |
| 48 enum SessionRestoreStrategy { |
| 49 // Generate OAuth2 refresh token from authentication profile's cookie jar. |
| 50 // Restore session from generated OAuth2 refresh token. |
| 51 RESTORE_FROM_COOKIE_JAR, |
| 52 // Restore session from saved OAuth2 refresh token from TokenServices. |
| 53 RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN, |
| 54 // Restore session from OAuth2 refresh token passed via command line. |
| 55 RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN, |
| 56 // Restore session from authentication code passed via command line. |
| 57 RESTORE_FROM_AUTH_CODE, |
| 58 }; |
| 59 |
| 60 class Observer { |
| 61 public: |
| 62 virtual ~Observer() {} |
| 63 |
| 64 // Raised when merge session state changes. |
| 65 virtual void OnSessionRestoreStateChanged(Profile* user_profile, |
| 66 SessionRestoreState state) {} |
| 67 // Raised when session's GAIA credentials (SID+LSID) are available to |
| 68 // other signed in services. |
| 69 virtual void OnSessionAuthenticated(Profile* user_profile) {} |
| 70 }; |
| 71 |
| 72 explicit OAuth2LoginManager(Profile* user_profile); |
| 30 virtual ~OAuth2LoginManager(); | 73 virtual ~OAuth2LoginManager(); |
| 31 | 74 |
| 32 // OAuthLoginManager overrides. | 75 void AddObserver(OAuth2LoginManager::Observer* observer); |
| 33 virtual void RestoreSession( | 76 void RemoveObserver(OAuth2LoginManager::Observer* observer); |
| 34 Profile* user_profile, | 77 |
| 78 // Restores and verifies OAuth tokens either following specified |
| 79 // |restore_strategy|. For |restore_strategy| with values |
| 80 // RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN or |
| 81 // RESTORE_FROM_AUTH_CODE, respectively |
| 82 // parameters |oauth2_refresh_token| or |auth_code| need to have non-empty |
| 83 // value. |
| 84 void RestoreSession( |
| 35 net::URLRequestContextGetter* auth_request_context, | 85 net::URLRequestContextGetter* auth_request_context, |
| 36 SessionRestoreStrategy restore_strategy, | 86 SessionRestoreStrategy restore_strategy, |
| 37 const std::string& oauth2_refresh_token, | 87 const std::string& oauth2_refresh_token, |
| 38 const std::string& auth_code) OVERRIDE; | 88 const std::string& auth_code); |
| 39 virtual void ContinueSessionRestore() OVERRIDE; | 89 |
| 40 virtual void Stop() OVERRIDE; | 90 // Continues session restore after transient network errors. |
| 91 void ContinueSessionRestore(); |
| 92 |
| 93 // Stops all background authentication requests. |
| 94 void Stop(); |
| 95 |
| 96 // Returns session restore state. |
| 97 SessionRestoreState state() { return state_; } |
| 98 |
| 99 const base::Time& session_restore_start() { return session_restore_start_; } |
| 100 |
| 101 // Returns true if the tab loading should block until session restore |
| 102 // finishes. |
| 103 bool ShouldBlockTabLoading(); |
| 41 | 104 |
| 42 private: | 105 private: |
| 106 friend class MergeSessionLoadPageTest; |
| 107 |
| 43 // Session restore outcomes (for UMA). | 108 // Session restore outcomes (for UMA). |
| 44 enum { | 109 enum { |
| 45 SESSION_RESTORE_UNDEFINED = 0, | 110 SESSION_RESTORE_UNDEFINED = 0, |
| 46 SESSION_RESTORE_SUCCESS = 1, | 111 SESSION_RESTORE_SUCCESS = 1, |
| 47 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, | 112 SESSION_RESTORE_TOKEN_FETCH_FAILED = 2, |
| 48 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, | 113 SESSION_RESTORE_NO_REFRESH_TOKEN_FAILED = 3, |
| 49 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, | 114 SESSION_RESTORE_OAUTHLOGIN_FAILED = 4, |
| 50 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, | 115 SESSION_RESTORE_MERGE_SESSION_FAILED = 5, |
| 51 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, | 116 SESSION_RESTORE_COUNT = SESSION_RESTORE_MERGE_SESSION_FAILED, |
| 52 }; | 117 }; |
| 53 | 118 |
| 119 // BrowserContextKeyedService implementation. |
| 120 virtual void Shutdown() OVERRIDE; |
| 121 |
| 54 // OAuth2LoginVerifier::Delegate overrides. | 122 // OAuth2LoginVerifier::Delegate overrides. |
| 55 virtual void OnOAuthLoginSuccess( | 123 virtual void OnOAuthLoginSuccess( |
| 56 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; | 124 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) OVERRIDE; |
| 57 virtual void OnOAuthLoginFailure() OVERRIDE; | 125 virtual void OnOAuthLoginFailure() OVERRIDE; |
| 58 virtual void OnSessionMergeSuccess() OVERRIDE; | 126 virtual void OnSessionMergeSuccess() OVERRIDE; |
| 59 virtual void OnSessionMergeFailure() OVERRIDE; | 127 virtual void OnSessionMergeFailure() OVERRIDE; |
| 60 | 128 |
| 61 // OAuth2TokenFetcher::Delegate overrides. | 129 // OAuth2TokenFetcher::Delegate overrides. |
| 62 virtual void OnOAuth2TokensAvailable( | 130 virtual void OnOAuth2TokensAvailable( |
| 63 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; | 131 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE; |
| 64 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; | 132 virtual void OnOAuth2TokensFetchFailed() OVERRIDE; |
| 65 | 133 |
| 66 // OAuth2TokenService::Observer implementation: | 134 // OAuth2TokenService::Observer implementation: |
| 67 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 135 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
| 68 | 136 |
| 137 // Signals delegate that authentication is completed, kicks off token fetching |
| 138 // process in TokenService. |
| 139 void CompleteAuthentication(); |
| 140 |
| 69 // Retrieves TokenService for |user_profile_| and sets up notification | 141 // Retrieves TokenService for |user_profile_| and sets up notification |
| 70 // observer events. | 142 // observer events. |
| 71 TokenService* SetupTokenService(); | 143 TokenService* SetupTokenService(); |
| 72 | 144 |
| 73 // Records OAuth2 tokens fetched through cookies-to-token exchange into | 145 // Records OAuth2 tokens fetched through cookies-to-token exchange into |
| 74 // TokenService. | 146 // TokenService. |
| 75 void StoreOAuth2Tokens( | 147 void StoreOAuth2Tokens( |
| 76 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); | 148 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens); |
| 77 | 149 |
| 78 // Loads previously stored OAuth2 tokens and kicks off its validation. | 150 // Loads previously stored OAuth2 tokens and kicks off its validation. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 89 void RestoreSessionCookies(); | 161 void RestoreSessionCookies(); |
| 90 | 162 |
| 91 // Checks GAIA error and figures out whether the request should be | 163 // Checks GAIA error and figures out whether the request should be |
| 92 // re-attempted. | 164 // re-attempted. |
| 93 bool RetryOnError(const GoogleServiceAuthError& error); | 165 bool RetryOnError(const GoogleServiceAuthError& error); |
| 94 | 166 |
| 95 // On successfuly OAuthLogin, starts token service token fetching process. | 167 // On successfuly OAuthLogin, starts token service token fetching process. |
| 96 void StartTokenService( | 168 void StartTokenService( |
| 97 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); | 169 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials); |
| 98 | 170 |
| 99 // Stops listening for a new login refresh token. | 171 // Changes |state_|, if needed fires observers (OnSessionRestoreStateChanged). |
| 100 void StopObservingRefreshToken(); | 172 void SetSessionRestoreState(SessionRestoreState state); |
| 173 |
| 174 // Testing helper. |
| 175 void SetSessionRestoreStartForTesting(const base::Time& time); |
| 101 | 176 |
| 102 // Keeps the track if we have already reported OAuth2 token being loaded | 177 // Keeps the track if we have already reported OAuth2 token being loaded |
| 103 // by TokenService. | 178 // by TokenService. |
| 179 Profile* user_profile_; |
| 180 scoped_refptr<net::URLRequestContextGetter> auth_request_context_; |
| 181 SessionRestoreStrategy restore_strategy_; |
| 182 SessionRestoreState state_; |
| 183 |
| 104 bool loading_reported_; | 184 bool loading_reported_; |
| 185 |
| 105 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; | 186 scoped_ptr<OAuth2TokenFetcher> oauth2_token_fetcher_; |
| 106 scoped_ptr<OAuth2LoginVerifier> login_verifier_; | 187 scoped_ptr<OAuth2LoginVerifier> login_verifier_; |
| 188 |
| 107 // OAuth2 refresh token. | 189 // OAuth2 refresh token. |
| 108 std::string refresh_token_; | 190 std::string refresh_token_; |
| 191 |
| 109 // Authorization code for fetching OAuth2 tokens. | 192 // Authorization code for fetching OAuth2 tokens. |
| 110 std::string auth_code_; | 193 std::string auth_code_; |
| 111 | 194 |
| 195 // Session restore start time. |
| 196 base::Time session_restore_start_; |
| 197 |
| 198 // List of observers to notify when token availability changes. |
| 199 // Makes sure list is empty on destruction. |
| 200 // TODO(zelidrag|gspencer): Figure out how to get rid of ProfileHelper so we |
| 201 // can change the line below to ObserverList<Observer, true>. |
| 202 ObserverList<Observer, false> observer_list_; |
| 203 |
| 112 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); | 204 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManager); |
| 113 }; | 205 }; |
| 114 | 206 |
| 115 } // namespace chromeos | 207 } // namespace chromeos |
| 116 | 208 |
| 117 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ | 209 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_LOGIN_MANAGER_H_ |
| OLD | NEW |