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
|
// Copyright 2020 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_ANDROID_PAYMENT_APP_SERVICE_BRIDGE_H_
#define COMPONENTS_PAYMENTS_CONTENT_ANDROID_PAYMENT_APP_SERVICE_BRIDGE_H_
#include <map>
#include <memory>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "components/payments/content/payment_app_factory.h"
#include "content/public/browser/global_routing_id.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
class RenderFrameHost;
class WebContents;
} // namespace content
namespace payments {
class PaymentAppService;
// A bridge that holds parameters needed by PaymentAppService and redirects
// callbacks from PaymentAppFactory to callbacks set by the caller.
class PaymentAppServiceBridge : public PaymentAppFactory::Delegate {
public:
using CanMakePaymentCalculatedCallback = base::OnceCallback<void(bool)>;
using PaymentAppCreatedCallback =
base::RepeatingCallback<void(std::unique_ptr<PaymentApp>)>;
using PaymentAppCreationErrorCallback =
base::RepeatingCallback<void(const std::string&,
AppCreationFailureReason)>;
// Creates a new PaymentAppServiceBridge. This object is self-deleting; its
// memory is freed after CreatePaymentApps() is invoked and
// OnDoneCreatingPaymentApps() is called `number_of_pending_factories_` times.
static PaymentAppServiceBridge* Create(
std::unique_ptr<PaymentAppService> payment_app_service,
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
base::WeakPtr<PaymentRequestSpec> spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool is_off_the_record,
base::WeakPtr<CSPChecker> csp_checker,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
PaymentAppCreatedCallback payment_app_created_callback,
PaymentAppCreationErrorCallback payment_app_creation_error_callback,
base::OnceClosure done_creating_payment_apps_callback,
base::RepeatingClosure set_can_make_payment_even_without_apps_callback,
base::RepeatingClosure set_opt_out_offered_callback);
~PaymentAppServiceBridge() override;
// Not copiable or movable.
PaymentAppServiceBridge(const PaymentAppServiceBridge&) = delete;
PaymentAppServiceBridge& operator=(const PaymentAppServiceBridge&) = delete;
void CreatePaymentApps();
base::WeakPtr<PaymentAppServiceBridge> GetWeakPtrForTest();
// PaymentAppFactory::Delegate
content::WebContents* GetWebContents() override;
const GURL& GetTopOrigin() override;
const GURL& GetFrameOrigin() override;
const url::Origin& GetFrameSecurityOrigin() override;
content::RenderFrameHost* GetInitiatorRenderFrameHost() const override;
content::GlobalRenderFrameHostId GetInitiatorRenderFrameHostId()
const override;
const std::vector<mojom::PaymentMethodDataPtr>& GetMethodData()
const override;
std::unique_ptr<webauthn::InternalAuthenticator> CreateInternalAuthenticator()
const override;
scoped_refptr<PaymentManifestWebDataService>
GetPaymentManifestWebDataService() const override;
bool IsOffTheRecord() const override;
base::WeakPtr<ContentPaymentRequestDelegate> GetPaymentRequestDelegate()
const override;
void ShowProcessingSpinner() override;
base::WeakPtr<PaymentRequestSpec> GetSpec() const override;
void GetTwaPackageName(GetTwaPackageNameCallback callback) override;
void OnPaymentAppCreated(std::unique_ptr<PaymentApp> app) override;
void OnPaymentAppCreationError(
const std::string& error_message,
AppCreationFailureReason error_reason) override;
void OnDoneCreatingPaymentApps() override;
void SetCanMakePaymentEvenWithoutApps() override;
base::WeakPtr<CSPChecker> GetCSPChecker() override;
void SetOptOutOffered() override;
std::optional<base::UnguessableToken> GetChromeOSTWAInstanceId()
const override;
private:
// Prevents direct instantiation. Callers should use Create() instead.
PaymentAppServiceBridge(
std::unique_ptr<PaymentAppService> payment_app_service,
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
base::WeakPtr<PaymentRequestSpec> spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool is_off_the_record,
base::WeakPtr<CSPChecker> csp_checker,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
PaymentAppCreatedCallback payment_app_created_callback,
PaymentAppCreationErrorCallback payment_app_creation_error_callback,
base::OnceClosure done_creating_payment_apps_callback,
base::RepeatingClosure set_can_make_payment_even_without_apps_callback,
base::RepeatingClosure set_opt_out_offered_callback);
const std::unique_ptr<PaymentAppService> payment_app_service_;
size_t number_of_pending_factories_;
content::GlobalRenderFrameHostId frame_routing_id_;
const GURL top_origin_;
const GURL frame_origin_;
const url::Origin frame_security_origin_;
base::WeakPtr<PaymentRequestSpec> spec_;
const std::string twa_package_name_;
scoped_refptr<PaymentManifestWebDataService>
payment_manifest_web_data_service_;
bool is_off_the_record_;
base::WeakPtr<CSPChecker> csp_checker_;
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback_;
PaymentAppCreatedCallback payment_app_created_callback_;
PaymentAppCreationErrorCallback payment_app_creation_error_callback_;
base::OnceClosure done_creating_payment_apps_callback_;
base::RepeatingClosure set_can_make_payment_even_without_apps_callback_;
base::RepeatingClosure set_opt_out_offered_callback_;
base::WeakPtrFactory<PaymentAppServiceBridge> weak_ptr_factory_{this};
};
} // namespace payments
#endif // COMPONENTS_PAYMENTS_CONTENT_ANDROID_PAYMENT_APP_SERVICE_BRIDGE_H_
|