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 #ifndef COMPONENTS_PAYMENTS_CONTENT_ANDROID_PAYMENT_MANIFEST_DOWNLOADER_H_ | |
6 #define COMPONENTS_PAYMENTS_CONTENT_ANDROID_PAYMENT_MANIFEST_DOWNLOADER_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "net/url_request/url_fetcher.h" | |
14 #include "net/url_request/url_fetcher_delegate.h" | |
15 #include "url/gurl.h" | |
16 | |
17 namespace net { | |
18 class URLRequestContextGetter; | |
19 } | |
20 | |
21 namespace payments { | |
22 | |
23 // Downloader of the payment method manifest based on the payment method name | |
24 // that is a URL with HTTPS scheme, e.g., https://bobpay.com. The download | |
25 // happens via two consecutive HTTP requests: | |
26 // | |
27 // 1) HEAD request for the payment method name. The HTTP response header is | |
28 // parsed for Link header that points to the location of the payment method | |
29 // manifest file. Example of a relative location: | |
30 // | |
31 // Link: <data/payment-manifest.json>; rel="payment-method-manifest" | |
32 // | |
33 // (This is relative to the payment method URL.) Example of an absolute | |
34 // location: | |
35 // | |
36 // Link: <https://bobpay.com/data/payment-manifest.json>; | |
37 // rel="payment-method-manifest" | |
38 // | |
39 // The absolute location must use HTTPS scheme. | |
40 // | |
41 // 2) GET request for the payment method manifest file. | |
42 // | |
43 // The downloader does not follow redirects. A download succeeds only if both | |
44 // HTTP response codes are 200. | |
45 class PaymentManifestDownloader : public net::URLFetcherDelegate { | |
46 public: | |
47 // The interface for receiving the result of downloading a manifest. | |
48 class Delegate { | |
49 public: | |
50 // Called when a manifest has been successfully downloaded. | |
51 virtual void OnManifestDownloadSuccess(const std::string& content) = 0; | |
52 | |
53 // Called when failed to download the manifest for any reason: | |
54 // - HTTP response code is not 200. | |
55 // - HTTP response headers are absent. | |
56 // - HTTP response headers does not contain Link headers. | |
57 // - Link header does not contain rel="payment-method-manifest". | |
58 // - Link header does not contain a valid URL. | |
59 // - HTTP GET on the manifest URL returns empty content. | |
60 virtual void OnManifestDownloadFailure() = 0; | |
61 | |
62 protected: | |
63 virtual ~Delegate() {} | |
64 }; | |
65 | |
66 // |delegate| should not be null and must outlive this object. |method_name| | |
67 // should be a valid URL that starts with "https://". | |
68 PaymentManifestDownloader( | |
69 const scoped_refptr<net::URLRequestContextGetter>& context, | |
70 const GURL& method_name, | |
71 Delegate* delegate); | |
72 | |
73 ~PaymentManifestDownloader() override; | |
74 | |
75 void Download(); | |
76 | |
77 private: | |
78 void InitiateDownload(const GURL& url, | |
79 net::URLFetcher::RequestType request_type); | |
80 | |
81 // net::URLFetcherDelegate | |
82 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
83 | |
84 scoped_refptr<net::URLRequestContextGetter> context_; | |
85 const GURL method_name_; | |
86 | |
87 // Non-owned. Never null. Outlives this object. | |
88 Delegate* delegate_; | |
89 | |
90 bool is_downloading_http_link_header_; | |
91 std::unique_ptr<net::URLFetcher> fetcher_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(PaymentManifestDownloader); | |
94 }; | |
95 | |
96 } // namespace payments | |
97 | |
98 #endif // COMPONENTS_PAYMENTS_CONTENT_ANDROID_PAYMENT_MANIFEST_DOWNLOADER_H_ | |
OLD | NEW |