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_SERVICE_WORKER_PAYMENT_APP_FINDER_H_
6 #define COMPONENTS_PAYMENTS_CONTENT_SERVICE_WORKER_PAYMENT_APP_FINDER_H_
7 
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "components/payments/content/web_app_manifest.h"
16 #include "content/public/browser/global_routing_id.h"
17 #include "content/public/browser/installed_payment_apps_finder.h"
18 #include "content/public/browser/payment_app_provider.h"
19 #include "content/public/browser/render_document_host_user_data.h"
20 #include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
21 
22 class GURL;
23 
24 template <class T>
25 class scoped_refptr;
26 
27 namespace content {
28 class RenderFrameHost;
29 }  // namespace content
30 
31 namespace url {
32 class Origin;
33 }  // namespace url
34 
35 namespace payments {
36 
37 class PaymentManifestDownloader;
38 class PaymentManifestWebDataService;
39 
40 // Retrieves service worker payment apps.
41 class ServiceWorkerPaymentAppFinder
42     : public content::RenderDocumentHostUserData<
43           ServiceWorkerPaymentAppFinder> {
44  public:
45   using InstallablePaymentApps =
46       std::map<GURL, std::unique_ptr<WebAppInstallationInfo>>;
47   using GetAllPaymentAppsCallback =
48       base::OnceCallback<void(content::InstalledPaymentAppsFinder::PaymentApps,
49                               InstallablePaymentApps,
50                               const std::string& error_message)>;
51 
52   ~ServiceWorkerPaymentAppFinder() override;
53 
54   // Retrieves all service worker payment apps that can handle payments for
55   // |requested_method_data|, verifies these apps are allowed to handle these
56   // payment methods, and filters them by their capabilities.
57   //
58   // |merchant_origin| should be the origin of the iframe that created the
59   // PaymentRequest object. It is used by security features like
60   // 'Sec-Fetch-Site' and 'Cross-Origin-Resource-Policy'.
61   //
62   // The payment apps will be returned through |callback|. After |callback| has
63   // been invoked, it's safe to show the apps in UI for user to select one of
64   // these apps for payment.
65   //
66   // After |callback| has fired, the factory refreshes its own cache in the
67   // background. Once the cache has been refreshed, the factory invokes the
68   // |finished_writing_cache_callback_for_testing|.
69   //
70   // The method should be called on the UI thread.
71   void GetAllPaymentApps(
72       const url::Origin& merchant_origin,
73       scoped_refptr<PaymentManifestWebDataService> cache,
74       std::vector<mojom::PaymentMethodDataPtr> requested_method_data,
75       bool may_crawl_for_installable_payment_apps,
76       GetAllPaymentAppsCallback callback,
77       base::OnceClosure finished_writing_cache_callback_for_testing);
78 
79   // Removes |apps| that don't match any of the |requested_method_data| based on
80   // the method names and method-specific capabilities.
81   static void RemoveAppsWithoutMatchingMethodData(
82       const std::vector<mojom::PaymentMethodDataPtr>& requested_method_data,
83       content::InstalledPaymentAppsFinder::PaymentApps* apps);
84 
85   // Ignore the given |method|, so that no installed or installable service
86   // workers would ever be looked up in GetAllPaymentApps(). Calling this
87   // multiple times will union the new payment methods with the existing set.
88   void IgnorePaymentMethodForTest(const std::string& method);
89 
90  private:
91   friend class content::RenderDocumentHostUserData<
92       ServiceWorkerPaymentAppFinder>;
93   friend class IframeCspTest;
94   friend class PaymentRequestPaymentAppTest;
95   friend class ServiceWorkerPaymentAppFinderBrowserTest;
96   friend class PaymentRequestPlatformBrowserTestBase;
97   friend class PaymentMethodViewControllerTest;
98   friend class PaymentHandlerIconRefetchTest;
99 
100   explicit ServiceWorkerPaymentAppFinder(content::RenderFrameHost* rfh);
101 
102   // Should be used only in tests.
103   // Should be called before every call to GetAllPaymentApps() (because the test
104   // downloader is moved into the SelfDeletingServiceWorkerPaymentAppFinder).
105   void SetDownloaderAndIgnorePortInOriginComparisonForTesting(
106       std::unique_ptr<PaymentManifestDownloader> downloader);
107 
108   RENDER_DOCUMENT_HOST_USER_DATA_KEY_DECL();
109 
110   // The identifier of the frame that owns this ServiceWorkerPaymentAppFinder.
111   content::GlobalFrameRoutingId frame_routing_id_;
112 
113   std::set<std::string> ignored_methods_;
114   std::unique_ptr<PaymentManifestDownloader> test_downloader_;
115 
116   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerPaymentAppFinder);
117 };
118 
119 }  // namespace payments
120 
121 #endif  // COMPONENTS_PAYMENTS_CONTENT_SERVICE_WORKER_PAYMENT_APP_FINDER_H_
122