Chromium Code Reviews| Index: chrome/browser/local_discovery/cloud_print_base_api_flow.cc |
| diff --git a/chrome/browser/local_discovery/privet_confirm_api_flow.cc b/chrome/browser/local_discovery/cloud_print_base_api_flow.cc |
| similarity index 59% |
| copy from chrome/browser/local_discovery/privet_confirm_api_flow.cc |
| copy to chrome/browser/local_discovery/cloud_print_base_api_flow.cc |
| index 1917e4a7b28267be269f13fa78e3a333bf158cad..886216e4e9d121335ca9a223ca1b6b3bce50ecb7 100644 |
| --- a/chrome/browser/local_discovery/privet_confirm_api_flow.cc |
| +++ b/chrome/browser/local_discovery/cloud_print_base_api_flow.cc |
| @@ -5,7 +5,7 @@ |
| #include "base/json/json_reader.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/values.h" |
| -#include "chrome/browser/local_discovery/privet_confirm_api_flow.h" |
| +#include "chrome/browser/local_discovery/cloud_print_base_api_flow.h" |
| #include "chrome/common/cloud_print/cloud_print_constants.h" |
| #include "google_apis/gaia/google_service_auth_error.h" |
| #include "net/base/load_flags.h" |
| @@ -17,47 +17,66 @@ namespace local_discovery { |
| namespace { |
| const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s"; |
| const char kCookieURLFormat[] = "%s&xsrf=%s&user=%d"; |
| +const char kCookieURLFormatNoXSRF[] = "%s&user=%d"; |
| } |
| -PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow( |
| +CloudPrintBaseApiFlow::CloudPrintBaseApiFlow( |
| net::URLRequestContextGetter* request_context, |
| OAuth2TokenService* token_service, |
| const GURL& automated_claim_url, |
| - const ResponseCallback& callback) |
| + Delegate* delegate) |
| : request_context_(request_context), |
| token_service_(token_service), |
| - automated_claim_url_(automated_claim_url), |
| - callback_(callback) { |
| + url_(automated_claim_url), |
| + delegate_(delegate) { |
| } |
| -PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow( |
| +CloudPrintBaseApiFlow::CloudPrintBaseApiFlow( |
| net::URLRequestContextGetter* request_context, |
| int user_index, |
| const std::string& xsrf_token, |
| const GURL& automated_claim_url, |
| - const ResponseCallback& callback) |
| + Delegate* delegate) |
| : request_context_(request_context), |
| token_service_(NULL), |
| user_index_(user_index), |
| xsrf_token_(xsrf_token), |
| - automated_claim_url_(automated_claim_url), |
| - callback_(callback) { |
| + url_(automated_claim_url), |
| + delegate_(delegate) { |
| } |
| -PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() { |
| +CloudPrintBaseApiFlow::CloudPrintBaseApiFlow( |
| + net::URLRequestContextGetter* request_context, |
| + int user_index, |
| + const GURL& automated_claim_url, |
| + Delegate* delegate) |
| + : request_context_(request_context), |
| + token_service_(NULL), |
| + user_index_(user_index), |
| + url_(automated_claim_url), |
| + delegate_(delegate) { |
| +} |
| + |
| +CloudPrintBaseApiFlow::~CloudPrintBaseApiFlow() { |
| } |
| -void PrivetConfirmApiCallFlow::Start() { |
| +void CloudPrintBaseApiFlow::Start() { |
| if (UseOAuth2()) { |
| OAuth2TokenService::ScopeSet oauth_scopes; |
| oauth_scopes.insert(cloud_print::kCloudPrintAuth); |
| oauth_request_ = token_service_->StartRequest(oauth_scopes, this); |
| } else { |
| - GURL cookie_url( |
| - base::StringPrintf(kCookieURLFormat, |
| - automated_claim_url_.spec().c_str(), |
| - xsrf_token_.c_str(), |
| - user_index_)); |
| + GURL cookie_url; |
| + if (xsrf_token_.empty()) { |
| + cookie_url = GURL(base::StringPrintf(kCookieURLFormatNoXSRF, |
| + url_.spec().c_str(), |
|
Vitaly Buka (NO REVIEWS)
2013/09/05 01:08:33
net::AppendQueryParameter
Noam Samuel
2013/09/05 23:22:27
Done.
|
| + user_index_)); |
| + } else { |
| + cookie_url = GURL(base::StringPrintf(kCookieURLFormat, |
| + url_.spec().c_str(), |
| + xsrf_token_.c_str(), |
| + user_index_)); |
| + } |
| CreateRequest(cookie_url); |
| @@ -67,11 +86,11 @@ void PrivetConfirmApiCallFlow::Start() { |
| } |
| } |
| -void PrivetConfirmApiCallFlow::OnGetTokenSuccess( |
| +void CloudPrintBaseApiFlow::OnGetTokenSuccess( |
| const OAuth2TokenService::Request* request, |
| const std::string& access_token, |
| const base::Time& expiration_time) { |
| - CreateRequest(automated_claim_url_); |
| + CreateRequest(url_); |
| std::string authorization_header = |
| base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); |
| @@ -82,13 +101,13 @@ void PrivetConfirmApiCallFlow::OnGetTokenSuccess( |
| url_fetcher_->Start(); |
| } |
| -void PrivetConfirmApiCallFlow::OnGetTokenFailure( |
| +void CloudPrintBaseApiFlow::OnGetTokenFailure( |
| const OAuth2TokenService::Request* request, |
| const GoogleServiceAuthError& error) { |
| - callback_.Run(ERROR_TOKEN); |
| + delegate_->OnCloudPrintAPIFlowError(this, ERROR_TOKEN); |
| } |
| -void PrivetConfirmApiCallFlow::CreateRequest(const GURL& url) { |
| +void CloudPrintBaseApiFlow::CreateRequest(const GURL& url) { |
| url_fetcher_.reset(net::URLFetcher::Create(url, |
| net::URLFetcher::GET, |
| this)); |
| @@ -99,7 +118,7 @@ void PrivetConfirmApiCallFlow::CreateRequest(const GURL& url) { |
| cloud_print::kChromeCloudPrintProxyHeader); |
| } |
| -void PrivetConfirmApiCallFlow::OnURLFetchComplete( |
| +void CloudPrintBaseApiFlow::OnURLFetchComplete( |
| const net::URLFetcher* source) { |
| // TODO(noamsml): Error logging. |
| @@ -109,31 +128,25 @@ void PrivetConfirmApiCallFlow::OnURLFetchComplete( |
| if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
| !source->GetResponseAsString(&response_str)) { |
| - callback_.Run(ERROR_NETWORK); |
| + delegate_->OnCloudPrintAPIFlowError(this, ERROR_NETWORK); |
| return; |
| } |
| if (source->GetResponseCode() != net::HTTP_OK) { |
| - callback_.Run(ERROR_HTTP_CODE); |
| + delegate_->OnCloudPrintAPIFlowError(this, ERROR_HTTP_CODE); |
| return; |
| } |
| base::JSONReader reader; |
| scoped_ptr<const base::Value> value(reader.Read(response_str)); |
| - const base::DictionaryValue* dictionary_value; |
| - bool success = false; |
| + const base::DictionaryValue* dictionary_value = NULL; |
| - if (!value.get() || !value->GetAsDictionary(&dictionary_value) |
| - || !dictionary_value->GetBoolean(cloud_print::kSuccessValue, &success)) { |
| - callback_.Run(ERROR_MALFORMED_RESPONSE); |
| + if (!value || !value->GetAsDictionary(&dictionary_value)) { |
| + delegate_->OnCloudPrintAPIFlowError(this, ERROR_MALFORMED_RESPONSE); |
| return; |
| } |
| - if (success) { |
| - callback_.Run(SUCCESS); |
| - } else { |
| - callback_.Run(ERROR_FROM_SERVER); |
| - } |
| + delegate_->OnCloudPrintAPIFlowComplete(this, dictionary_value); |
| } |
| } // namespace local_discovery |