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