Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Unified Diff: remoting/host/gaia_oauth_client.cc

Issue 10637008: Remove UrlFetcher from remoting and use the one in net instead. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use enum instead of bool to distinguish fetchers. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | remoting/host/host_port_allocator.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..048ad5146e2f1f2bd72a2d6bfb37f91887250c07 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,21 +46,33 @@ OAuthProviderInfo OAuthProviderInfo::GetDefault() {
}
class GaiaOAuthClient::Core
- : public base::RefCountedThreadSafe<GaiaOAuthClient::Core> {
+ : public base::RefCountedThreadSafe<GaiaOAuthClient::Core>,
+ public net::URLFetcherDelegate {
public:
Core(const OAuthProviderInfo& info,
net::URLRequestContextGetter* request_context_getter)
: provider_info_(info),
request_context_getter_(request_context_getter),
- delegate_(NULL) {
+ delegate_(NULL),
+ url_fetcher_type_(URL_FETCHER_NONE) {
}
void RefreshToken(const OAuthClientInfo& oauth_client_info,
const std::string& refresh_token,
GaiaOAuthClient::Delegate* delegate);
+ // net::URLFetcherDelegate interface
+ virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
+
private:
friend class base::RefCountedThreadSafe<Core>;
+
+ enum URLFetcherType {
+ URL_FETCHER_NONE,
+ URL_FETCHER_REFRESH_TOKEN,
+ URL_FETCHER_GET_USER_INFO
+ };
+
virtual ~Core() {}
void OnAuthTokenFetchComplete(const net::URLRequestStatus& status,
@@ -73,7 +87,8 @@ class GaiaOAuthClient::Core
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
GaiaOAuthClient::Delegate* delegate_;
- scoped_ptr<UrlFetcher> request_;
+ scoped_ptr<net::URLFetcher> request_;
+ URLFetcherType url_fetcher_type_;
std::string access_token_;
int expires_in_seconds_;
@@ -97,12 +112,32 @@ 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));
+ url_fetcher_type_ = URL_FETCHER_REFRESH_TOKEN;
+ request_->Start();
+}
+
+void GaiaOAuthClient::Core::OnURLFetchComplete(
+ const net::URLFetcher* source) {
+ std::string response_string;
+ source->GetResponseAsString(&response_string);
+ switch (url_fetcher_type_) {
+ case URL_FETCHER_REFRESH_TOKEN:
+ OnAuthTokenFetchComplete(source->GetStatus(),
+ source->GetResponseCode(),
+ response_string);
+ break;
+ case URL_FETCHER_GET_USER_INFO:
+ OnUserInfoFetchComplete(source->GetStatus(),
+ source->GetResponseCode(),
+ response_string);
+ break;
+ default:
+ LOG(ERROR) << "Unrecognised URLFetcher type: " << url_fetcher_type_;
+ }
}
void GaiaOAuthClient::Core::OnAuthTokenFetchComplete(
@@ -129,7 +164,14 @@ void GaiaOAuthClient::Core::OnAuthTokenFetchComplete(
message_value->IsType(Value::TYPE_DICTIONARY)) {
scoped_ptr<DictionaryValue> response_dict(
static_cast<DictionaryValue*>(message_value.release()));
- response_dict->GetString(kAccessTokenValue, &access_token_);
+ std::string access_token;
+ response_dict->GetString(kAccessTokenValue, &access_token);
+ if (access_token.find("\r\n") != std::string::npos) {
+ LOG(ERROR) << "Gaia response: access token include CRLF";
+ delegate_->OnOAuthError();
+ return;
+ }
+ access_token_ = access_token;
response_dict->GetInteger(kExpiresInValue, &expires_in_seconds_);
}
VLOG(1) << "Gaia response: acess_token='" << access_token_
@@ -146,18 +188,20 @@ 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_);
+ url_fetcher_type_ = URL_FETCHER_GET_USER_INFO;
+ request_->Start();
}
void GaiaOAuthClient::Core::OnUserInfoFetchComplete(
const net::URLRequestStatus& status,
int response_code,
const std::string& response) {
+ request_.reset();
+ url_fetcher_type_ = URL_FETCHER_NONE;
std::string email;
if (response_code == net::HTTP_OK) {
scoped_ptr<Value> message_value(base::JSONReader::Read(response));
« no previous file with comments | « no previous file | remoting/host/host_port_allocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698