Chromium Code Reviews| Index: components/payments/payment_manifest_downloader.cc |
| diff --git a/components/payments/payment_manifest_downloader.cc b/components/payments/payment_manifest_downloader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6847bcbbb396211afc871065c9c7189efdab6ac8 |
| --- /dev/null |
| +++ b/components/payments/payment_manifest_downloader.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/payments/payment_manifest_downloader.h" |
| + |
| +#include <unordered_map> |
|
xunjieli
2017/02/24 18:18:30
nit: not used.
please use gerrit instead
2017/03/03 03:11:17
Used on line 83:
std::unordered_map<std::string, b
|
| +#include <utility> |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "components/data_use_measurement/core/data_use_user_data.h" |
| +#include "components/link_header_util/link_header_util.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "url/url_constants.h" |
| + |
| +namespace payments { |
| +namespace { |
| + |
| +bool IsValidManifestUrl(const GURL& url) { |
| + return url.is_valid() && url.SchemeIs(url::kHttpsScheme); |
| +} |
| + |
| +} // namespace |
| + |
| +PaymentManifestDownloader::PaymentManifestDownloader( |
| + const scoped_refptr<net::URLRequestContextGetter>& context, |
| + const GURL& method_name, |
| + Delegate* delegate) |
| + : context_(context), |
| + method_name_(method_name), |
| + delegate_(delegate), |
| + is_downloading_http_link_header_(true) { |
| + DCHECK(IsValidManifestUrl(method_name_)); |
| +} |
| + |
| +PaymentManifestDownloader::~PaymentManifestDownloader() {} |
| + |
| +void PaymentManifestDownloader::Download() { |
| + InitiateDownload(method_name_, net::URLFetcher::HEAD); |
| +} |
| + |
| +void PaymentManifestDownloader::InitiateDownload( |
| + const GURL& url, |
| + net::URLFetcher::RequestType request_type) { |
| + if (!IsValidManifestUrl(url)) { |
| + delegate_->OnManifestDownloadFailure(); |
| + return; |
| + } |
| + |
| + fetcher_ = net::URLFetcher::Create(0 /* id */, url, request_type, this); |
| + data_use_measurement::DataUseUserData::AttachToFetcher( |
| + fetcher_.get(), data_use_measurement::DataUseUserData::PAYMENTS); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + fetcher_->SetStopOnRedirect(true); |
| + fetcher_->SetRequestContext(context_.get()); |
| + fetcher_->Start(); |
| +} |
| + |
| +void PaymentManifestDownloader::OnURLFetchComplete( |
| + const net::URLFetcher* source) { |
| + if (source->GetResponseCode() != 200) { |
|
xunjieli
2017/02/24 18:18:30
nit: net::HTTP_OK
please use gerrit instead
2017/03/03 03:11:17
Done.
|
| + delegate_->OnManifestDownloadFailure(); |
| + return; |
| + } |
| + |
| + if (is_downloading_http_link_header_) { |
| + is_downloading_http_link_header_ = false; |
| + |
| + net::HttpResponseHeaders* headers = source->GetResponseHeaders(); |
| + if (headers == nullptr) { |
| + delegate_->OnManifestDownloadFailure(); |
| + return; |
| + } |
| + |
| + std::string link_header; |
| + headers->GetNormalizedHeader("link", &link_header); |
| + if (!link_header.empty()) { |
| + std::string manifest_url; |
| + std::unordered_map<std::string, base::Optional<std::string>> params; |
| + for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { |
| + if (!link_header_util::ParseLinkHeaderValue(value.first, value.second, |
| + &manifest_url, ¶ms)) { |
| + break; |
|
Marijn Kruisselbrink
2017/02/23 20:55:22
Should this be continue rather than break? For exa
please use gerrit instead
2017/03/03 03:11:17
Done.
|
| + } |
| + |
| + auto rel = params.find("rel"); |
| + if (rel != params.end() && |
| + rel->second.value_or("") == "payment-method-manifest") { |
|
Marijn Kruisselbrink
2017/02/23 20:55:21
This doesn't seem entirely spec compliant. https:/
please use gerrit instead
2017/03/03 03:11:17
Done.
|
| + InitiateDownload(method_name_.Resolve(manifest_url), |
| + net::URLFetcher::GET); |
| + return; |
| + } |
| + } |
| + } |
| + } else { |
| + std::string content; |
| + if (source->GetResponseAsString(&content) && !content.empty()) { |
| + delegate_->OnManifestDownloadSuccess(content); |
| + return; |
| + } |
| + } |
| + |
| + delegate_->OnManifestDownloadFailure(); |
| +} |
| + |
| +} // namespace payments |