| 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 57%
|
| 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..0b8371d648e39093e80dffa8ed339af3b169f220 100644
|
| --- a/chrome/browser/local_discovery/privet_confirm_api_flow.cc
|
| +++ b/chrome/browser/local_discovery/cloud_print_base_api_flow.cc
|
| @@ -3,12 +3,14 @@
|
| // found in the LICENSE file.
|
|
|
| #include "base/json/json_reader.h"
|
| +#include "base/strings/string_number_conversions.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"
|
| +#include "net/base/url_util.h"
|
| #include "net/http/http_status_code.h"
|
| #include "net/url_request/url_request_status.h"
|
|
|
| @@ -16,48 +18,68 @@ namespace local_discovery {
|
|
|
| namespace {
|
| const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s";
|
| -const char kCookieURLFormat[] = "%s&xsrf=%s&user=%d";
|
| +const char kXSRFURLParameterKey[] = "xsrf";
|
| +const char kUserURLParameterKey[] = "user";
|
| }
|
|
|
| -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) {
|
| + user_index_(kAccountIndexUseOAuth2),
|
| + 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) {
|
| }
|
|
|
| -void PrivetConfirmApiCallFlow::Start() {
|
| +CloudPrintBaseApiFlow::~CloudPrintBaseApiFlow() {
|
| +}
|
| +
|
| +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 = url_;
|
| +
|
| + if (!xsrf_token_.empty()) {
|
| + cookie_url = net::AppendQueryParameter(cookie_url,
|
| + kXSRFURLParameterKey,
|
| + xsrf_token_);
|
| + }
|
| +
|
| + cookie_url = net::AppendQueryParameter(cookie_url,
|
| + kUserURLParameterKey,
|
| + base::IntToString(user_index_));
|
|
|
| CreateRequest(cookie_url);
|
|
|
| @@ -67,11 +89,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 +104,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 +121,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 +131,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
|
|
|