| Index: google_apis/gaia/oauth2_access_token_fetcher_impl.cc
|
| diff --git a/google_apis/gaia/oauth2_access_token_fetcher.cc b/google_apis/gaia/oauth2_access_token_fetcher_impl.cc
|
| similarity index 71%
|
| copy from google_apis/gaia/oauth2_access_token_fetcher.cc
|
| copy to google_apis/gaia/oauth2_access_token_fetcher_impl.cc
|
| index ab382056d4bcfda85e5947116bb27a4e0cf41dad..0b3e768f73e6f0a987e2b320c97de83528d3ecb7 100644
|
| --- a/google_apis/gaia/oauth2_access_token_fetcher.cc
|
| +++ b/google_apis/gaia/oauth2_access_token_fetcher_impl.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "google_apis/gaia/oauth2_access_token_fetcher.h"
|
| +#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
|
|
|
| #include <algorithm>
|
| #include <string>
|
| @@ -51,14 +51,14 @@ static const char kErrorKey[] = "error";
|
| // Enumerated constants for logging server responses on 400 errors, matching
|
| // RFC 6749.
|
| enum OAuth2ErrorCodesForHistogram {
|
| - OAUTH2_ACCESS_ERROR_INVALID_REQUEST = 0,
|
| - OAUTH2_ACCESS_ERROR_INVALID_CLIENT,
|
| - OAUTH2_ACCESS_ERROR_INVALID_GRANT,
|
| - OAUTH2_ACCESS_ERROR_UNAUTHORIZED_CLIENT,
|
| - OAUTH2_ACCESS_ERROR_UNSUPPORTED_GRANT_TYPE,
|
| - OAUTH2_ACCESS_ERROR_INVALID_SCOPE,
|
| - OAUTH2_ACCESS_ERROR_UNKNOWN,
|
| - OAUTH2_ACCESS_ERROR_COUNT
|
| + OAUTH2_ACCESS_ERROR_INVALID_REQUEST = 0,
|
| + OAUTH2_ACCESS_ERROR_INVALID_CLIENT,
|
| + OAUTH2_ACCESS_ERROR_INVALID_GRANT,
|
| + OAUTH2_ACCESS_ERROR_UNAUTHORIZED_CLIENT,
|
| + OAUTH2_ACCESS_ERROR_UNSUPPORTED_GRANT_TYPE,
|
| + OAUTH2_ACCESS_ERROR_INVALID_SCOPE,
|
| + OAUTH2_ACCESS_ERROR_UNKNOWN,
|
| + OAUTH2_ACCESS_ERROR_COUNT
|
| };
|
|
|
| OAuth2ErrorCodesForHistogram OAuth2ErrorToHistogramValue(
|
| @@ -96,9 +96,7 @@ static URLFetcher* CreateFetcher(URLRequestContextGetter* getter,
|
| URLFetcherDelegate* delegate) {
|
| bool empty_body = body.empty();
|
| URLFetcher* result = net::URLFetcher::Create(
|
| - 0, url,
|
| - empty_body ? URLFetcher::GET : URLFetcher::POST,
|
| - delegate);
|
| + 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate);
|
|
|
| result->SetRequestContext(getter);
|
| result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
|
| @@ -114,25 +112,36 @@ static URLFetcher* CreateFetcher(URLRequestContextGetter* getter,
|
|
|
| return result;
|
| }
|
| -} // namespace
|
|
|
| -OAuth2AccessTokenFetcher::OAuth2AccessTokenFetcher(
|
| - OAuth2AccessTokenConsumer* consumer,
|
| - URLRequestContextGetter* getter)
|
| - : consumer_(consumer),
|
| - getter_(getter),
|
| - state_(INITIAL) { }
|
| +scoped_ptr<base::DictionaryValue> ParseGetAccessTokenResponse(
|
| + const net::URLFetcher* source) {
|
| + CHECK(source);
|
|
|
| -OAuth2AccessTokenFetcher::~OAuth2AccessTokenFetcher() { }
|
| + std::string data;
|
| + source->GetResponseAsString(&data);
|
| + scoped_ptr<base::Value> value(base::JSONReader::Read(data));
|
| + if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY)
|
| + value.reset();
|
|
|
| -void OAuth2AccessTokenFetcher::CancelRequest() {
|
| - fetcher_.reset();
|
| + return scoped_ptr<base::DictionaryValue>(
|
| + static_cast<base::DictionaryValue*>(value.release()));
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::Start(const std::string& client_id,
|
| - const std::string& client_secret,
|
| - const std::string& refresh_token,
|
| - const std::vector<std::string>& scopes) {
|
| +} // namespace
|
| +
|
| +OAuth2AccessTokenFetcherImpl::OAuth2AccessTokenFetcherImpl(
|
| + OAuth2AccessTokenConsumer* consumer, URLRequestContextGetter* getter)
|
| + : OAuth2AccessTokenFetcher(consumer), getter_(getter), state_(INITIAL) {}
|
| +
|
| +OAuth2AccessTokenFetcherImpl::~OAuth2AccessTokenFetcherImpl() {}
|
| +
|
| +void OAuth2AccessTokenFetcherImpl::CancelRequest() { fetcher_.reset(); }
|
| +
|
| +void OAuth2AccessTokenFetcherImpl::Start(
|
| + const std::string& client_id,
|
| + const std::string& client_secret,
|
| + const std::string& refresh_token,
|
| + const std::vector<std::string>& scopes) {
|
| client_id_ = client_id;
|
| client_secret_ = client_secret;
|
| refresh_token_ = refresh_token;
|
| @@ -140,26 +149,26 @@ void OAuth2AccessTokenFetcher::Start(const std::string& client_id,
|
| StartGetAccessToken();
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::StartGetAccessToken() {
|
| +void OAuth2AccessTokenFetcherImpl::StartGetAccessToken() {
|
| CHECK_EQ(INITIAL, state_);
|
| state_ = GET_ACCESS_TOKEN_STARTED;
|
| - fetcher_.reset(CreateFetcher(
|
| - getter_,
|
| - MakeGetAccessTokenUrl(),
|
| - MakeGetAccessTokenBody(
|
| - client_id_, client_secret_, refresh_token_, scopes_),
|
| - this));
|
| + fetcher_.reset(
|
| + CreateFetcher(getter_,
|
| + MakeGetAccessTokenUrl(),
|
| + MakeGetAccessTokenBody(
|
| + client_id_, client_secret_, refresh_token_, scopes_),
|
| + this));
|
| fetcher_->Start(); // OnURLFetchComplete will be called.
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::EndGetAccessToken(
|
| +void OAuth2AccessTokenFetcherImpl::EndGetAccessToken(
|
| const net::URLFetcher* source) {
|
| CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_);
|
| state_ = GET_ACCESS_TOKEN_DONE;
|
|
|
| URLRequestStatus status = source->GetStatus();
|
| - int histogram_value = status.is_success() ? source->GetResponseCode() :
|
| - status.error();
|
| + int histogram_value =
|
| + status.is_success() ? source->GetResponseCode() : status.error();
|
| UMA_HISTOGRAM_SPARSE_SLOWLY("Gaia.ResponseCodesForOAuth2AccessToken",
|
| histogram_value);
|
| if (!status.is_success()) {
|
| @@ -174,29 +183,30 @@ void OAuth2AccessTokenFetcher::EndGetAccessToken(
|
| case net::HTTP_INTERNAL_SERVER_ERROR:
|
| // HTTP_FORBIDDEN (403) is treated as temporary error, because it may be
|
| // '403 Rate Limit Exeeded.' 500 is always treated as transient.
|
| - OnGetTokenFailure(GoogleServiceAuthError(
|
| - GoogleServiceAuthError::SERVICE_UNAVAILABLE));
|
| + OnGetTokenFailure(
|
| + GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
|
| return;
|
| case net::HTTP_BAD_REQUEST: {
|
| // HTTP_BAD_REQUEST (400) usually contains error as per
|
| // http://tools.ietf.org/html/rfc6749#section-5.2.
|
| std::string gaia_error;
|
| if (!ParseGetAccessTokenFailureResponse(source, &gaia_error)) {
|
| - OnGetTokenFailure(GoogleServiceAuthError(
|
| - GoogleServiceAuthError::SERVICE_ERROR));
|
| + OnGetTokenFailure(
|
| + GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
|
| return;
|
| }
|
|
|
| - OAuth2ErrorCodesForHistogram access_error(OAuth2ErrorToHistogramValue(
|
| - gaia_error));
|
| + OAuth2ErrorCodesForHistogram access_error(
|
| + OAuth2ErrorToHistogramValue(gaia_error));
|
| UMA_HISTOGRAM_ENUMERATION("Gaia.BadRequestTypeForOAuth2AccessToken",
|
| - access_error, OAUTH2_ACCESS_ERROR_COUNT);
|
| -
|
| - OnGetTokenFailure(access_error == OAUTH2_ACCESS_ERROR_INVALID_GRANT ?
|
| - GoogleServiceAuthError(
|
| - GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS) :
|
| - GoogleServiceAuthError(
|
| - GoogleServiceAuthError::SERVICE_ERROR));
|
| + access_error,
|
| + OAUTH2_ACCESS_ERROR_COUNT);
|
| +
|
| + OnGetTokenFailure(
|
| + access_error == OAUTH2_ACCESS_ERROR_INVALID_GRANT
|
| + ? GoogleServiceAuthError(
|
| + GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)
|
| + : GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
|
| return;
|
| }
|
| default:
|
| @@ -210,8 +220,7 @@ void OAuth2AccessTokenFetcher::EndGetAccessToken(
|
| // Parse out the access token and the expiration time.
|
| std::string access_token;
|
| int expires_in;
|
| - if (!ParseGetAccessTokenSuccessResponse(
|
| - source, &access_token, &expires_in)) {
|
| + if (!ParseGetAccessTokenSuccessResponse(source, &access_token, &expires_in)) {
|
| DLOG(WARNING) << "Response doesn't match expected format";
|
| OnGetTokenFailure(
|
| GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
|
| @@ -224,19 +233,19 @@ void OAuth2AccessTokenFetcher::EndGetAccessToken(
|
| base::Time::Now() + base::TimeDelta::FromSeconds(9 * expires_in / 10));
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::OnGetTokenSuccess(
|
| +void OAuth2AccessTokenFetcherImpl::OnGetTokenSuccess(
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| - consumer_->OnGetTokenSuccess(access_token, expiration_time);
|
| + FireOnGetTokenSuccess(access_token, expiration_time);
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::OnGetTokenFailure(
|
| +void OAuth2AccessTokenFetcherImpl::OnGetTokenFailure(
|
| const GoogleServiceAuthError& error) {
|
| state_ = ERROR_STATE;
|
| - consumer_->OnGetTokenFailure(error);
|
| + FireOnGetTokenFailure(error);
|
| }
|
|
|
| -void OAuth2AccessTokenFetcher::OnURLFetchComplete(
|
| +void OAuth2AccessTokenFetcherImpl::OnURLFetchComplete(
|
| const net::URLFetcher* source) {
|
| CHECK(source);
|
| CHECK(state_ == GET_ACCESS_TOKEN_STARTED);
|
| @@ -244,12 +253,12 @@ void OAuth2AccessTokenFetcher::OnURLFetchComplete(
|
| }
|
|
|
| // static
|
| -GURL OAuth2AccessTokenFetcher::MakeGetAccessTokenUrl() {
|
| +GURL OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenUrl() {
|
| return GaiaUrls::GetInstance()->oauth2_token_url();
|
| }
|
|
|
| // static
|
| -std::string OAuth2AccessTokenFetcher::MakeGetAccessTokenBody(
|
| +std::string OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
|
| const std::string& client_id,
|
| const std::string& client_secret,
|
| const std::string& refresh_token,
|
| @@ -260,11 +269,10 @@ std::string OAuth2AccessTokenFetcher::MakeGetAccessTokenBody(
|
| std::string enc_refresh_token =
|
| net::EscapeUrlEncodedData(refresh_token, true);
|
| if (scopes.empty()) {
|
| - return base::StringPrintf(
|
| - kGetAccessTokenBodyFormat,
|
| - enc_client_id.c_str(),
|
| - enc_client_secret.c_str(),
|
| - enc_refresh_token.c_str());
|
| + return base::StringPrintf(kGetAccessTokenBodyFormat,
|
| + enc_client_id.c_str(),
|
| + enc_client_secret.c_str(),
|
| + enc_refresh_token.c_str());
|
| } else {
|
| std::string scopes_string = JoinString(scopes, ' ');
|
| return base::StringPrintf(
|
| @@ -276,42 +284,26 @@ std::string OAuth2AccessTokenFetcher::MakeGetAccessTokenBody(
|
| }
|
| }
|
|
|
| -scoped_ptr<base::DictionaryValue> ParseGetAccessTokenResponse(
|
| - const net::URLFetcher* source) {
|
| - CHECK(source);
|
| -
|
| - std::string data;
|
| - source->GetResponseAsString(&data);
|
| - scoped_ptr<base::Value> value(base::JSONReader::Read(data));
|
| - if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY)
|
| - value.reset();
|
| -
|
| - return scoped_ptr<base::DictionaryValue>(
|
| - static_cast<base::DictionaryValue*>(value.release()));
|
| -}
|
| -
|
| // static
|
| -bool OAuth2AccessTokenFetcher::ParseGetAccessTokenSuccessResponse(
|
| +bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
|
| const net::URLFetcher* source,
|
| std::string* access_token,
|
| int* expires_in) {
|
| CHECK(access_token);
|
| - scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(
|
| - source);
|
| + scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source);
|
| if (value.get() == NULL)
|
| return false;
|
|
|
| return value->GetString(kAccessTokenKey, access_token) &&
|
| - value->GetInteger(kExpiresInKey, expires_in);
|
| + value->GetInteger(kExpiresInKey, expires_in);
|
| }
|
|
|
| // static
|
| -bool OAuth2AccessTokenFetcher::ParseGetAccessTokenFailureResponse(
|
| +bool OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
|
| const net::URLFetcher* source,
|
| std::string* error) {
|
| CHECK(error);
|
| - scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(
|
| - source);
|
| + scoped_ptr<base::DictionaryValue> value = ParseGetAccessTokenResponse(source);
|
| if (value.get() == NULL)
|
| return false;
|
| return value->GetString(kErrorKey, error);
|
|
|