File: installable_payment_app_crawler.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (170 lines) | stat: -rw-r--r-- 7,004 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAYMENTS_CONTENT_INSTALLABLE_PAYMENT_APP_CRAWLER_H_
#define COMPONENTS_PAYMENTS_CONTENT_INSTALLABLE_PAYMENT_APP_CRAWLER_H_

#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/payments/content/developer_console_logger.h"
#include "components/payments/content/manifest_verifier.h"
#include "components/payments/content/payment_manifest_web_data_service.h"
#include "components/payments/content/utility/payment_manifest_parser.h"
#include "components/payments/content/web_app_manifest.h"
#include "components/payments/core/payment_manifest_downloader.h"
#include "content/public/browser/global_routing_id.h"
#include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
#include "url/origin.h"

class GURL;

namespace content {
class RenderFrameHost;
}  // namespace content

namespace payments {

struct RefetchedMetadata {
  RefetchedMetadata();
  ~RefetchedMetadata();

  std::string method_name;
  std::unique_ptr<SkBitmap> icon;
  content::SupportedDelegations supported_delegations;
};

// Crawls installable web payment apps. First, fetches and parses the payment
// method manifests to get 'default_applications' manifest urls. Then, fetches
// and parses the web app manifests to get the installable payment apps' info.
class InstallablePaymentAppCrawler {
 public:
  using FinishedCrawlingCallback = base::OnceCallback<void(
      std::map<GURL, std::unique_ptr<WebAppInstallationInfo>>,
      std::map<GURL, std::unique_ptr<RefetchedMetadata>>,
      const std::string& error_message)>;

  enum class CrawlingMode {
    // In this mode the crawler will crawl for finding JIT installable payment
    // apps.
    kJustInTimeInstallation,
    // In this mode the crawler will crawl for refreshing metadata such as the
    // icon and supported delegations for already installed payment apps.
    kInstalledAppMetadataRefresh,
  };

  // |merchant_origin| is the origin of the iframe that created the
  // PaymentRequest object. It is used by security features like
  // 'Sec-Fetch-Site' and 'Cross-Origin-Resource-Policy'.
  // |initiator_render_frame_host| is the iframe for |merchant_origin|.
  //
  // The owner of InstallablePaymentAppCrawler owns |downloader|, |parser| and
  // |cache|. They should live until |finished_using_resources| parameter to
  // Start() method is called.
  InstallablePaymentAppCrawler(
      const url::Origin& merchant_origin,
      content::RenderFrameHost* initiator_render_frame_host,
      PaymentManifestDownloader* downloader,
      PaymentManifestParser* parser,
      PaymentManifestWebDataService* cache);

  InstallablePaymentAppCrawler(const InstallablePaymentAppCrawler&) = delete;
  InstallablePaymentAppCrawler& operator=(const InstallablePaymentAppCrawler&) =
      delete;

  ~InstallablePaymentAppCrawler();

  // Starts the crawling process. All the url based payment methods in
  // |request_method_data| will be crawled. A list of installable payment apps'
  // info will be send back through |callback|. |finished_using_resources| is
  // called after finished using the resources (downloader, parser and cache),
  // then this object is safe to be deleted.
  void Start(
      const std::vector<mojom::PaymentMethodDataPtr>& requested_method_data,
      std::set<GURL> method_manifest_urls_for_metadata_refresh,
      FinishedCrawlingCallback callback,
      base::OnceClosure finished_using_resources);

  void IgnorePortInOriginComparisonForTesting();

  bool IsSameOriginWith(const GURL& a, const GURL& b);

 private:
  void OnPaymentMethodManifestDownloaded(
      const GURL& method_manifest_url,
      const GURL& method_manifest_url_after_redirects,
      const std::string& content,
      const std::string& error_message);
  void OnPaymentMethodManifestParsed(
      const GURL& method_manifest_url,
      const GURL& method_manifest_url_after_redirects,
      const std::string& content,
      const std::vector<GURL>& default_applications,
      const std::vector<url::Origin>& supported_origins);
  void OnPaymentWebAppManifestDownloaded(
      const GURL& method_manifest_url,
      const GURL& web_app_manifest_url,
      const GURL& web_app_manifest_url_after_redirects,
      const std::string& content,
      const std::string& error_message);
  void OnPaymentWebAppInstallationInfo(
      const GURL& method_manifest_url,
      const GURL& web_app_manifest_url,
      std::unique_ptr<WebAppInstallationInfo> app_info,
      std::unique_ptr<std::vector<PaymentManifestParser::WebAppIcon>> icons);
  bool CompleteAndStorePaymentWebAppInfoIfValid(
      const GURL& method_manifest_url,
      const GURL& web_app_manifest_url,
      std::unique_ptr<WebAppInstallationInfo> app_info);
  // Returns true if an icon can get downloaded for the web app with the given
  // manifest.
  bool DownloadAndDecodeWebAppIcon(
      const GURL& method_manifest_url,
      const GURL& web_app_manifest_url,
      std::unique_ptr<std::vector<PaymentManifestParser::WebAppIcon>> icons);
  void OnPaymentWebAppIconDownloadAndDecoded(const GURL& method_manifest_url,
                                             const GURL& web_app_manifest_url,
                                             const SkBitmap& icon);
  void PostTaskToFinishCrawlingPaymentAppsIfReady();
  void FinishCrawlingPaymentAppsIfReady();
  void SetFirstError(const std::string& error_message);

  DeveloperConsoleLogger log_;
  const url::Origin merchant_origin_;
  const content::GlobalRenderFrameHostId initiator_frame_routing_id_;
  raw_ptr<PaymentManifestDownloader> downloader_;
  raw_ptr<PaymentManifestParser> parser_;
  FinishedCrawlingCallback callback_;
  base::OnceClosure finished_using_resources_;

  size_t number_of_payment_method_manifest_to_download_;
  size_t number_of_payment_method_manifest_to_parse_;
  size_t number_of_web_app_manifest_to_download_;
  size_t number_of_web_app_manifest_to_parse_;
  size_t number_of_web_app_icons_to_download_and_decode_;
  std::set<GURL> downloaded_web_app_manifests_;
  std::map<GURL, std::unique_ptr<WebAppInstallationInfo>> installable_apps_;
  std::map<GURL, std::unique_ptr<RefetchedMetadata>> refetched_app_metadata_;
  std::set<GURL> method_manifest_urls_for_metadata_refresh_;

  // The first error message (if any) to be forwarded to the merchant when
  // rejecting the promise returned from PaymentRequest.show().
  std::string first_error_message_;

  bool ignore_port_in_origin_comparison_for_testing_ = false;

  CrawlingMode crawling_mode_ = CrawlingMode::kJustInTimeInstallation;

  base::WeakPtrFactory<InstallablePaymentAppCrawler> weak_ptr_factory_{this};
};

}  // namespace payments.

#endif  // COMPONENTS_PAYMENTS_CONTENT_INSTALLABLE_PAYMENT_APP_CRAWLER_H_