| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "components/payments/content/payment_manifest_downloader.h" | 5 #include "components/payments/content/payment_manifest_downloader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 bool IsValidManifestUrl(const GURL& url) { | 26 bool IsValidManifestUrl(const GURL& url) { |
| 27 return url.is_valid() && url.SchemeIs(url::kHttpsScheme); | 27 return url.is_valid() && url.SchemeIs(url::kHttpsScheme); |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 PaymentManifestDownloader::PaymentManifestDownloader( | 32 PaymentManifestDownloader::PaymentManifestDownloader( |
| 33 const scoped_refptr<net::URLRequestContextGetter>& context, | 33 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 34 const GURL& method_name, | 34 const GURL& url, |
| 35 Delegate* delegate) | 35 Delegate* delegate) |
| 36 : context_(context), | 36 : context_(context), |
| 37 method_name_(method_name), | 37 url_(url), |
| 38 delegate_(delegate), | 38 delegate_(delegate), |
| 39 is_downloading_http_link_header_(true) { | 39 is_downloading_http_link_header_(true) { |
| 40 DCHECK(IsValidManifestUrl(method_name_)); | 40 DCHECK(IsValidManifestUrl(url_)); |
| 41 } | 41 } |
| 42 | 42 |
| 43 PaymentManifestDownloader::~PaymentManifestDownloader() {} | 43 PaymentManifestDownloader::~PaymentManifestDownloader() {} |
| 44 | 44 |
| 45 void PaymentManifestDownloader::Download() { | 45 void PaymentManifestDownloader::DownloadPaymentMethodManifest() { |
| 46 InitiateDownload(method_name_, net::URLFetcher::HEAD); | 46 DCHECK(!fetcher_); |
| 47 is_downloading_http_link_header_ = true; |
| 48 InitiateDownload(url_, net::URLFetcher::HEAD); |
| 49 } |
| 50 |
| 51 void PaymentManifestDownloader::DownloadWebAppManifest() { |
| 52 DCHECK(!fetcher_); |
| 53 is_downloading_http_link_header_ = false; |
| 54 InitiateDownload(url_, net::URLFetcher::GET); |
| 47 } | 55 } |
| 48 | 56 |
| 49 void PaymentManifestDownloader::InitiateDownload( | 57 void PaymentManifestDownloader::InitiateDownload( |
| 50 const GURL& url, | 58 const GURL& url, |
| 51 net::URLFetcher::RequestType request_type) { | 59 net::URLFetcher::RequestType request_type) { |
| 52 if (!IsValidManifestUrl(url)) { | 60 if (!IsValidManifestUrl(url)) { |
| 53 delegate_->OnManifestDownloadFailure(); | 61 delegate_->OnManifestDownloadFailure(); |
| 54 return; | 62 return; |
| 55 } | 63 } |
| 56 | 64 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 76 | 84 |
| 77 net::HttpResponseHeaders* headers = source->GetResponseHeaders(); | 85 net::HttpResponseHeaders* headers = source->GetResponseHeaders(); |
| 78 if (!headers) { | 86 if (!headers) { |
| 79 delegate_->OnManifestDownloadFailure(); | 87 delegate_->OnManifestDownloadFailure(); |
| 80 return; | 88 return; |
| 81 } | 89 } |
| 82 | 90 |
| 83 std::string link_header; | 91 std::string link_header; |
| 84 headers->GetNormalizedHeader("link", &link_header); | 92 headers->GetNormalizedHeader("link", &link_header); |
| 85 if (!link_header.empty()) { | 93 if (!link_header.empty()) { |
| 86 std::string manifest_url; | 94 std::string payment_method_manifest_url; |
| 87 std::unordered_map<std::string, base::Optional<std::string>> params; | 95 std::unordered_map<std::string, base::Optional<std::string>> params; |
| 88 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { | 96 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { |
| 89 if (!link_header_util::ParseLinkHeaderValue(value.first, value.second, | 97 if (!link_header_util::ParseLinkHeaderValue( |
| 90 &manifest_url, ¶ms)) { | 98 value.first, value.second, &payment_method_manifest_url, |
| 99 ¶ms)) { |
| 91 continue; | 100 continue; |
| 92 } | 101 } |
| 93 | 102 |
| 94 auto rel = params.find("rel"); | 103 auto rel = params.find("rel"); |
| 95 if (rel == params.end()) | 104 if (rel == params.end()) |
| 96 continue; | 105 continue; |
| 97 | 106 |
| 98 std::vector<std::string> rel_parts = | 107 std::vector<std::string> rel_parts = |
| 99 base::SplitString(rel->second.value_or(""), HTTP_LWS, | 108 base::SplitString(rel->second.value_or(""), HTTP_LWS, |
| 100 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 109 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 101 if (std::find(rel_parts.begin(), rel_parts.end(), | 110 if (std::find(rel_parts.begin(), rel_parts.end(), |
| 102 "payment-method-manifest") != rel_parts.end()) { | 111 "payment-method-manifest") != rel_parts.end()) { |
| 103 InitiateDownload(method_name_.Resolve(manifest_url), | 112 InitiateDownload(url_.Resolve(payment_method_manifest_url), |
| 104 net::URLFetcher::GET); | 113 net::URLFetcher::GET); |
| 105 return; | 114 return; |
| 106 } | 115 } |
| 107 } | 116 } |
| 108 } | 117 } |
| 109 } else { | 118 } else { |
| 110 std::string content; | 119 std::string content; |
| 111 if (source->GetResponseAsString(&content) && !content.empty()) { | 120 if (source->GetResponseAsString(&content) && !content.empty()) { |
| 112 delegate_->OnManifestDownloadSuccess(content); | 121 delegate_->OnManifestDownloadSuccess(content); |
| 113 return; | 122 return; |
| 114 } | 123 } |
| 115 } | 124 } |
| 116 | 125 |
| 117 delegate_->OnManifestDownloadFailure(); | 126 delegate_->OnManifestDownloadFailure(); |
| 118 } | 127 } |
| 119 | 128 |
| 120 } // namespace payments | 129 } // namespace payments |
| OLD | NEW |