| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/local_discovery/gcd_base_api_flow.h" | 5 #include "chrome/browser/local_discovery/gcd_base_api_flow.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/local_discovery/gcd_constants.h" | 11 #include "chrome/browser/local_discovery/gcd_constants.h" |
| 12 #include "chrome/common/cloud_print/cloud_print_constants.h" | 12 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 13 #include "components/cloud_devices/common/cloud_devices_urls.h" | 13 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 14 #include "google_apis/gaia/google_service_auth_error.h" | 14 #include "google_apis/gaia/google_service_auth_error.h" |
| 15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 16 #include "net/base/url_util.h" | 16 #include "net/base/url_util.h" |
| 17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 19 | 19 |
| 20 namespace local_discovery { | 20 namespace local_discovery { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s"; | 23 const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s"; |
| 24 } | 24 } |
| 25 | 25 |
| 26 std::string GCDBaseApiFlow::Delegate::GetOAuthScope() { |
| 27 return cloud_devices::kCloudDevicesAuthScope; |
| 28 } |
| 29 |
| 26 net::URLFetcher::RequestType GCDBaseApiFlow::Delegate::GetRequestType() { | 30 net::URLFetcher::RequestType GCDBaseApiFlow::Delegate::GetRequestType() { |
| 27 return net::URLFetcher::GET; | 31 return net::URLFetcher::GET; |
| 28 } | 32 } |
| 29 | 33 |
| 34 std::vector<std::string> GCDBaseApiFlow::Delegate::GetExtraRequestHeaders() { |
| 35 return std::vector<std::string>(); |
| 36 } |
| 37 |
| 30 void GCDBaseApiFlow::Delegate::GetUploadData(std::string* upload_type, | 38 void GCDBaseApiFlow::Delegate::GetUploadData(std::string* upload_type, |
| 31 std::string* upload_data) { | 39 std::string* upload_data) { |
| 32 *upload_type = std::string(); | 40 *upload_type = std::string(); |
| 33 *upload_data = std::string(); | 41 *upload_data = std::string(); |
| 34 } | 42 } |
| 35 | 43 |
| 36 GCDBaseApiFlow::GCDBaseApiFlow(net::URLRequestContextGetter* request_context, | 44 GCDBaseApiFlow::GCDBaseApiFlow(net::URLRequestContextGetter* request_context, |
| 37 OAuth2TokenService* token_service, | 45 OAuth2TokenService* token_service, |
| 38 const std::string& account_id, | 46 const std::string& account_id, |
| 39 const GURL& url, | |
| 40 Delegate* delegate) | 47 Delegate* delegate) |
| 41 : OAuth2TokenService::Consumer("cloud_print"), | 48 : OAuth2TokenService::Consumer("cloud_print"), |
| 42 request_context_(request_context), | 49 request_context_(request_context), |
| 43 token_service_(token_service), | 50 token_service_(token_service), |
| 44 account_id_(account_id), | 51 account_id_(account_id), |
| 45 url_(url), | |
| 46 delegate_(delegate) { | 52 delegate_(delegate) { |
| 47 } | 53 } |
| 48 | 54 |
| 49 GCDBaseApiFlow::~GCDBaseApiFlow() {} | 55 GCDBaseApiFlow::~GCDBaseApiFlow() {} |
| 50 | 56 |
| 51 void GCDBaseApiFlow::Start() { | 57 void GCDBaseApiFlow::Start() { |
| 52 OAuth2TokenService::ScopeSet oauth_scopes; | 58 OAuth2TokenService::ScopeSet oauth_scopes; |
| 53 if (delegate_->GCDIsCloudPrint()) { | 59 oauth_scopes.insert(delegate_->GetOAuthScope()); |
| 54 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope); | |
| 55 } else { | |
| 56 oauth_scopes.insert(cloud_devices::kCloudDevicesAuthScope); | |
| 57 } | |
| 58 oauth_request_ = | 60 oauth_request_ = |
| 59 token_service_->StartRequest(account_id_, oauth_scopes, this); | 61 token_service_->StartRequest(account_id_, oauth_scopes, this); |
| 60 } | 62 } |
| 61 | 63 |
| 62 void GCDBaseApiFlow::OnGetTokenSuccess( | 64 void GCDBaseApiFlow::OnGetTokenSuccess( |
| 63 const OAuth2TokenService::Request* request, | 65 const OAuth2TokenService::Request* request, |
| 64 const std::string& access_token, | 66 const std::string& access_token, |
| 65 const base::Time& expiration_time) { | 67 const base::Time& expiration_time) { |
| 66 CreateRequest(url_); | 68 CreateRequest(delegate_->GetURL()); |
| 67 | 69 |
| 68 std::string authorization_header = | 70 std::string authorization_header = |
| 69 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); | 71 base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str()); |
| 70 | 72 |
| 71 url_fetcher_->AddExtraRequestHeader(authorization_header); | 73 url_fetcher_->AddExtraRequestHeader(authorization_header); |
| 72 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 74 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 73 net::LOAD_DO_NOT_SEND_COOKIES); | 75 net::LOAD_DO_NOT_SEND_COOKIES); |
| 74 url_fetcher_->Start(); | 76 url_fetcher_->Start(); |
| 75 } | 77 } |
| 76 | 78 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 87 | 89 |
| 88 if (request_type != net::URLFetcher::GET) { | 90 if (request_type != net::URLFetcher::GET) { |
| 89 std::string upload_type; | 91 std::string upload_type; |
| 90 std::string upload_data; | 92 std::string upload_data; |
| 91 delegate_->GetUploadData(&upload_type, &upload_data); | 93 delegate_->GetUploadData(&upload_type, &upload_data); |
| 92 url_fetcher_->SetUploadData(upload_type, upload_data); | 94 url_fetcher_->SetUploadData(upload_type, upload_data); |
| 93 } | 95 } |
| 94 | 96 |
| 95 url_fetcher_->SetRequestContext(request_context_.get()); | 97 url_fetcher_->SetRequestContext(request_context_.get()); |
| 96 | 98 |
| 97 if (delegate_->GCDIsCloudPrint()) { | 99 std::vector<std::string> extra_headers = delegate_->GetExtraRequestHeaders(); |
| 98 url_fetcher_->AddExtraRequestHeader( | 100 for (size_t i = 0; i < extra_headers.size(); ++i) |
| 99 cloud_print::kChromeCloudPrintProxyHeader); | 101 url_fetcher_->AddExtraRequestHeader(extra_headers[i]); |
| 100 } | |
| 101 } | 102 } |
| 102 | 103 |
| 103 void GCDBaseApiFlow::OnURLFetchComplete(const net::URLFetcher* source) { | 104 void GCDBaseApiFlow::OnURLFetchComplete(const net::URLFetcher* source) { |
| 104 // TODO(noamsml): Error logging. | 105 // TODO(noamsml): Error logging. |
| 105 | 106 |
| 106 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into | 107 // TODO(noamsml): Extract this and PrivetURLFetcher::OnURLFetchComplete into |
| 107 // one helper method. | 108 // one helper method. |
| 108 std::string response_str; | 109 std::string response_str; |
| 109 | 110 |
| 110 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | 111 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
| (...skipping 12 matching lines...) Expand all Loading... |
| 123 const base::DictionaryValue* dictionary_value = NULL; | 124 const base::DictionaryValue* dictionary_value = NULL; |
| 124 | 125 |
| 125 if (!value || !value->GetAsDictionary(&dictionary_value)) { | 126 if (!value || !value->GetAsDictionary(&dictionary_value)) { |
| 126 delegate_->OnGCDAPIFlowError(this, ERROR_MALFORMED_RESPONSE); | 127 delegate_->OnGCDAPIFlowError(this, ERROR_MALFORMED_RESPONSE); |
| 127 return; | 128 return; |
| 128 } | 129 } |
| 129 | 130 |
| 130 delegate_->OnGCDAPIFlowComplete(this, dictionary_value); | 131 delegate_->OnGCDAPIFlowComplete(this, dictionary_value); |
| 131 } | 132 } |
| 132 | 133 |
| 134 CloudPrintApiFlowDelegate::CloudPrintApiFlowDelegate() {} |
| 135 |
| 136 CloudPrintApiFlowDelegate::~CloudPrintApiFlowDelegate() {} |
| 137 |
| 138 std::string CloudPrintApiFlowDelegate::GetOAuthScope() { |
| 139 return cloud_devices::kCloudPrintAuthScope; |
| 140 } |
| 141 |
| 142 std::vector<std::string> CloudPrintApiFlowDelegate::GetExtraRequestHeaders() { |
| 143 return std::vector<std::string>(1, cloud_print::kChromeCloudPrintProxyHeader); |
| 144 } |
| 145 |
| 133 } // namespace local_discovery | 146 } // namespace local_discovery |
| OLD | NEW |