Chromium Code Reviews| Index: remoting/host/gaia_oauth_client.cc |
| diff --git a/remoting/host/gaia_oauth_client.cc b/remoting/host/gaia_oauth_client.cc |
| index db7d449faef1b1d741520f8e8b53a41ec961886c..eecdb5a063ee8c5b3f5812b72675a71b142645e2 100644 |
| --- a/remoting/host/gaia_oauth_client.cc |
| +++ b/remoting/host/gaia_oauth_client.cc |
| @@ -5,6 +5,7 @@ |
| #include "remoting/host/gaia_oauth_client.h" |
| #include "base/bind.h" |
| +#include "base/compiler_specific.h" |
| #include "base/json/json_reader.h" |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| @@ -12,9 +13,10 @@ |
| #include "googleurl/src/gurl.h" |
| #include "net/base/escape.h" |
| #include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| #include "net/url_request/url_request_context_getter.h" |
| #include "net/url_request/url_request_status.h" |
| -#include "remoting/host/url_fetcher.h" |
| namespace { |
| @@ -44,18 +46,21 @@ OAuthProviderInfo OAuthProviderInfo::GetDefault() { |
| } |
| class GaiaOAuthClient::Core |
| - : public base::RefCountedThreadSafe<GaiaOAuthClient::Core> { |
| + : public base::RefCountedThreadSafe<GaiaOAuthClient::Core>, |
| + public net::URLFetcherDelegate { |
|
Wez
2012/06/22 20:14:39
Do we actually need this implementation to be spli
Jamie
2012/06/22 22:42:02
This implementation is based on one used by cloud
Wez
2012/06/22 23:58:35
SGTM
|
| public: |
| Core(const OAuthProviderInfo& info, |
| net::URLRequestContextGetter* request_context_getter) |
| : provider_info_(info), |
| request_context_getter_(request_context_getter), |
| - delegate_(NULL) { |
| + delegate_(NULL), |
| + request_is_oauth_refresh_(false) { |
| } |
| void RefreshToken(const OAuthClientInfo& oauth_client_info, |
| const std::string& refresh_token, |
| GaiaOAuthClient::Delegate* delegate); |
| + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
|
Wez
2012/06/22 20:14:39
nit: Prefix this with a blank line and comment "ne
Jamie
2012/06/22 22:42:02
Done.
|
| private: |
| friend class base::RefCountedThreadSafe<Core>; |
| @@ -73,7 +78,8 @@ class GaiaOAuthClient::Core |
| scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| GaiaOAuthClient::Delegate* delegate_; |
| - scoped_ptr<UrlFetcher> request_; |
| + scoped_ptr<net::URLFetcher> request_; |
| + bool request_is_oauth_refresh_; |
|
Wez
2012/06/22 20:14:39
Looking at the rest of the class, I think it'd be
Jamie
2012/06/22 22:42:02
That's what I had at first, but we check that the
Wez
2012/06/22 23:58:35
I take your point, but you should really use a "st
Jamie
2012/06/23 00:20:23
Done.
|
| std::string access_token_; |
| int expires_in_seconds_; |
| @@ -97,12 +103,27 @@ void GaiaOAuthClient::Core::RefreshToken( |
| "&client_secret=" + |
| net::EscapeUrlEncodedData(oauth_client_info.client_secret, true) + |
| "&grant_type=refresh_token"; |
| - request_.reset(new UrlFetcher(GURL(provider_info_.access_token_url), |
| - UrlFetcher::POST)); |
| + request_.reset(net::URLFetcher::Create( |
| + GURL(provider_info_.access_token_url), net::URLFetcher::POST, this)); |
| request_->SetRequestContext(request_context_getter_); |
| request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
| - request_->Start( |
| - base::Bind(&GaiaOAuthClient::Core::OnAuthTokenFetchComplete, this)); |
| + request_is_oauth_refresh_ = true; |
| + request_->Start(); |
| +} |
| + |
| +void GaiaOAuthClient::Core::OnURLFetchComplete( |
| + const net::URLFetcher* source) { |
| + std::string response_string; |
| + source->GetResponseAsString(&response_string); |
| + if (request_is_oauth_refresh_) { |
| + OnAuthTokenFetchComplete(source->GetStatus(), |
| + source->GetResponseCode(), |
| + response_string); |
| + } else { |
| + OnUserInfoFetchComplete(source->GetStatus(), |
| + source->GetResponseCode(), |
| + response_string); |
| + } |
| } |
| void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( |
| @@ -146,18 +167,19 @@ void GaiaOAuthClient::Core::OnAuthTokenFetchComplete( |
| } |
| void GaiaOAuthClient::Core::FetchUserInfoAndInvokeCallback() { |
| - request_.reset(new UrlFetcher( |
| - GURL(provider_info_.user_info_url), UrlFetcher::GET)); |
| + request_.reset(net::URLFetcher::Create( |
| + GURL(provider_info_.user_info_url), net::URLFetcher::GET, this)); |
| request_->SetRequestContext(request_context_getter_); |
| - request_->SetHeader("Authorization", "Bearer " + access_token_); |
| - request_->Start( |
| - base::Bind(&GaiaOAuthClient::Core::OnUserInfoFetchComplete, this)); |
| + request_->AddExtraRequestHeader("Authorization: Bearer " + access_token_); |
|
Wez
2012/06/22 20:14:39
Do we need to sanitize access_token_, or will AddE
Jamie
2012/06/22 22:42:02
It DCHECKs that it doesn't contain CRLF. Something
|
| + request_is_oauth_refresh_ = false; |
| + request_->Start(); |
| } |
| void GaiaOAuthClient::Core::OnUserInfoFetchComplete( |
| const net::URLRequestStatus& status, |
| int response_code, |
| const std::string& response) { |
| + request_.reset(); |
| std::string email; |
| if (response_code == net::HTTP_OK) { |
| scoped_ptr<Value> message_value(base::JSONReader::Read(response)); |