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 "components/data_use_measurement/core/data_use_user_data.h" | |
12 #include "components/link_header_util/link_header_util.h" | |
13 #include "net/base/load_flags.h" | |
14 #include "net/http/http_response_headers.h" | |
15 #include "net/url_request/url_request_context_getter.h" | |
16 | |
17 namespace payments { | |
18 | |
19 PaymentManifestDownloader::PaymentManifestDownloader( | |
20 const scoped_refptr<net::URLRequestContextGetter>& context, | |
21 const std::string& method_name, | |
22 Delegate* delegate) | |
23 : context_(context), | |
24 method_name_(method_name), | |
25 delegate_(delegate), | |
26 is_downloading_http_link_header_(true) {} | |
27 | |
28 PaymentManifestDownloader::~PaymentManifestDownloader() {} | |
29 | |
30 void PaymentManifestDownloader::Download() { | |
31 InitiateDownload(method_name_, net::URLFetcher::HEAD); | |
32 } | |
33 | |
34 void PaymentManifestDownloader::InitiateDownload( | |
35 const std::string& url, | |
36 net::URLFetcher::RequestType request_type) { | |
37 fetcher_ = net::URLFetcher::Create(0 /* id */, GURL(url), request_type, this); | |
palmer
2017/01/27 00:11:47
I'd like to see a single point of validation, whic
please use gerrit instead
2017/02/23 19:57:50
AndroidPaymentAppFactory.java:115
| |
38 data_use_measurement::DataUseUserData::AttachToFetcher( | |
39 fetcher_.get(), data_use_measurement::DataUseUserData::PAYMENTS); | |
40 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
41 net::LOAD_DO_NOT_SAVE_COOKIES); | |
42 fetcher_->SetRequestContext(context_.get()); | |
43 fetcher_->Start(); | |
44 } | |
45 | |
46 void PaymentManifestDownloader::OnURLFetchComplete( | |
47 const net::URLFetcher* source) { | |
48 if (source->GetResponseCode() != 200) { | |
palmer
2017/01/27 00:11:47
Have 3xx redirects already been followed, at this
please use gerrit instead
2017/02/23 19:57:50
I've prohibited 3xx redirects in the latest patch.
| |
49 delegate_->OnManifestDownloadFailure(); | |
50 return; | |
51 } | |
52 | |
53 if (is_downloading_http_link_header_) { | |
54 is_downloading_http_link_header_ = false; | |
55 std::string link_header; | |
56 source->GetResponseHeaders()->GetNormalizedHeader("link", &link_header); | |
57 if (!link_header.empty()) { | |
58 std::string url; | |
59 std::unordered_map<std::string, base::Optional<std::string>> params; | |
60 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { | |
61 if (!link_header_util::ParseLinkHeaderValue(value.first, value.second, | |
62 &url, ¶ms)) { | |
63 break; | |
64 } | |
65 | |
66 auto rel = params.find("rel"); | |
67 if (rel != params.end() && | |
68 rel->second.value_or("") == "payment-method-manifest") { | |
69 if (url.find("https://") != 0) | |
70 url = method_name_ + "/" + url; | |
71 InitiateDownload(url, net::URLFetcher::GET); | |
72 return; | |
73 } | |
74 } | |
75 } | |
76 } else { | |
77 std::string content; | |
78 if (source->GetResponseAsString(&content)) { | |
79 delegate_->OnManifestDownloadSuccess(content); | |
80 return; | |
81 } | |
82 } | |
83 | |
84 delegate_->OnManifestDownloadFailure(); | |
85 } | |
86 | |
87 } // namespace payments | |
OLD | NEW |