blob: 5b53d1791a872414f64aad3bc042acf4af9e48dd [file] [log] [blame]
mathpf709499d2017-01-09 20:48:361// Copyright 2016 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 "chrome/browser/payments/payment_request_factory.h"
6
sebsga70a6da2017-12-21 22:27:027#include <memory>
rouslan690997682017-05-09 18:07:398#include <utility>
mathpf709499d2017-01-09 20:48:369
danakj8638bd32019-06-28 18:45:4110#include "base/no_destructor.h"
mathpf709499d2017-01-09 20:48:3611#include "chrome/browser/payments/chrome_payment_request_delegate.h"
rouslan908248c2017-02-27 21:30:2412#include "components/payments/content/payment_request_web_contents_manager.h"
Rouslan Solomakhina4a9b8c2020-10-15 21:16:2413#include "content/public/browser/render_frame_host.h"
14#include "content/public/browser/web_contents.h"
Rouslan Solomakhinbc783e092019-11-25 16:54:4415#include "mojo/public/cpp/bindings/message.h"
16#include "third_party/blink/public/mojom/feature_policy/feature_policy_feature.mojom-shared.h"
mathpf709499d2017-01-09 20:48:3617
18namespace payments {
19
danakj8638bd32019-06-28 18:45:4120namespace {
21
Miyoung Shin5e74bd02019-09-30 10:54:5322using PaymentRequestFactoryCallback = base::RepeatingCallback<void(
23 mojo::PendingReceiver<mojom::PaymentRequest> receiver,
24 content::RenderFrameHost* render_frame_host)>;
danakj8638bd32019-06-28 18:45:4125
26PaymentRequestFactoryCallback& GetTestingFactoryCallback() {
27 static base::NoDestructor<PaymentRequestFactoryCallback> callback;
28 return *callback;
29}
30
31} // namespace
32
Miyoung Shin5e74bd02019-09-30 10:54:5333void CreatePaymentRequest(
34 content::RenderFrameHost* render_frame_host,
35 mojo::PendingReceiver<mojom::PaymentRequest> receiver) {
Liquan (Max) Guc176476e2020-09-25 02:47:1136 if (!render_frame_host->IsCurrent()) {
37 // This happens when the page has navigated away, which would cause the
38 // blink PaymentRequest to be released shortly, or when the iframe is being
39 // removed from the page, which is not a use case that we support.
40 // Abandoning the `receiver` will close the mojo connection, so blink
41 // PaymentRequest will receive a connection error and will clean up itself.
42 return;
43 }
44
Rouslan Solomakhinbc783e092019-11-25 16:54:4445 if (!render_frame_host->IsFeatureEnabled(
46 blink::mojom::FeaturePolicyFeature::kPayment)) {
47 mojo::ReportBadMessage("Feature policy blocks Payment");
48 return;
49 }
50
danakj8638bd32019-06-28 18:45:4151 if (GetTestingFactoryCallback()) {
Miyoung Shin5e74bd02019-09-30 10:54:5352 return GetTestingFactoryCallback().Run(std::move(receiver),
danakj8638bd32019-06-28 18:45:4153 render_frame_host);
54 }
55
Rouslan Solomakhina4a9b8c2020-10-15 21:16:2456 PaymentRequestWebContentsManager::GetOrCreateForWebContents(
57 content::WebContents::FromRenderFrameHost(render_frame_host))
mathp300fa542017-03-27 19:29:3758 ->CreatePaymentRequest(
Rouslan Solomakhina4a9b8c2020-10-15 21:16:2459 render_frame_host,
60 std::make_unique<ChromePaymentRequestDelegate>(render_frame_host),
Miyoung Shin5e74bd02019-09-30 10:54:5361 std::move(receiver),
mathp300fa542017-03-27 19:29:3762 /*observer_for_testing=*/nullptr);
mathpf709499d2017-01-09 20:48:3663}
64
danakj8638bd32019-06-28 18:45:4165void SetPaymentRequestFactoryForTesting(
66 PaymentRequestFactoryCallback factory_callback) {
67 GetTestingFactoryCallback() = std::move(factory_callback);
68}
69
mathpf709499d2017-01-09 20:48:3670} // namespace payments