OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/payments/payment_manifest_downloader.h" |
| 6 |
| 7 #include <unordered_map> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/logging.h" |
| 12 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 13 #include "components/link_header_util/link_header_util.h" |
| 14 #include "net/base/load_flags.h" |
| 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "url/url_constants.h" |
| 18 |
| 19 namespace payments { |
| 20 namespace { |
| 21 |
| 22 bool IsValidManifestUrl(const GURL& url) { |
| 23 return url.is_valid() && url.SchemeIs(url::kHttpsScheme); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 PaymentManifestDownloader::PaymentManifestDownloader( |
| 29 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 30 const GURL& method_name, |
| 31 Delegate* delegate) |
| 32 : context_(context), |
| 33 method_name_(method_name), |
| 34 delegate_(delegate), |
| 35 is_downloading_http_link_header_(true) { |
| 36 DCHECK(IsValidManifestUrl(method_name_)); |
| 37 } |
| 38 |
| 39 PaymentManifestDownloader::~PaymentManifestDownloader() {} |
| 40 |
| 41 void PaymentManifestDownloader::Download() { |
| 42 InitiateDownload(method_name_, net::URLFetcher::HEAD); |
| 43 } |
| 44 |
| 45 void PaymentManifestDownloader::InitiateDownload( |
| 46 const GURL& url, |
| 47 net::URLFetcher::RequestType request_type) { |
| 48 if (!IsValidManifestUrl(url)) { |
| 49 delegate_->OnManifestDownloadFailure(); |
| 50 return; |
| 51 } |
| 52 |
| 53 fetcher_ = net::URLFetcher::Create(0 /* id */, url, request_type, this); |
| 54 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 55 fetcher_.get(), data_use_measurement::DataUseUserData::PAYMENTS); |
| 56 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 57 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 58 fetcher_->SetStopOnRedirect(true); |
| 59 fetcher_->SetRequestContext(context_.get()); |
| 60 fetcher_->Start(); |
| 61 } |
| 62 |
| 63 void PaymentManifestDownloader::OnURLFetchComplete( |
| 64 const net::URLFetcher* source) { |
| 65 if (source->GetResponseCode() != 200) { |
| 66 delegate_->OnManifestDownloadFailure(); |
| 67 return; |
| 68 } |
| 69 |
| 70 if (is_downloading_http_link_header_) { |
| 71 is_downloading_http_link_header_ = false; |
| 72 |
| 73 net::HttpResponseHeaders* headers = source->GetResponseHeaders(); |
| 74 if (headers == nullptr) { |
| 75 delegate_->OnManifestDownloadFailure(); |
| 76 return; |
| 77 } |
| 78 |
| 79 std::string link_header; |
| 80 headers->GetNormalizedHeader("link", &link_header); |
| 81 if (!link_header.empty()) { |
| 82 std::string manifest_url; |
| 83 std::unordered_map<std::string, base::Optional<std::string>> params; |
| 84 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { |
| 85 if (!link_header_util::ParseLinkHeaderValue(value.first, value.second, |
| 86 &manifest_url, ¶ms)) { |
| 87 break; |
| 88 } |
| 89 |
| 90 auto rel = params.find("rel"); |
| 91 if (rel != params.end() && |
| 92 rel->second.value_or("") == "payment-method-manifest") { |
| 93 InitiateDownload(method_name_.Resolve(manifest_url), |
| 94 net::URLFetcher::GET); |
| 95 return; |
| 96 } |
| 97 } |
| 98 } |
| 99 } else { |
| 100 std::string content; |
| 101 if (source->GetResponseAsString(&content) && !content.empty()) { |
| 102 delegate_->OnManifestDownloadSuccess(content); |
| 103 return; |
| 104 } |
| 105 } |
| 106 |
| 107 delegate_->OnManifestDownloadFailure(); |
| 108 } |
| 109 |
| 110 } // namespace payments |
OLD | NEW |