mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 1 | // 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 | |
sebsg | a70a6da | 2017-12-21 22:27:02 | [diff] [blame] | 7 | #include <memory> |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 8 | #include <utility> |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 9 | |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 10 | #include "base/no_destructor.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 11 | #include "chrome/browser/payments/chrome_payment_request_delegate.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 12 | #include "components/payments/content/payment_request_web_contents_manager.h" |
Rouslan Solomakhin | a4a9b8c | 2020-10-15 21:16:24 | [diff] [blame^] | 13 | #include "content/public/browser/render_frame_host.h" |
| 14 | #include "content/public/browser/web_contents.h" |
Rouslan Solomakhin | bc783e09 | 2019-11-25 16:54:44 | [diff] [blame] | 15 | #include "mojo/public/cpp/bindings/message.h" |
| 16 | #include "third_party/blink/public/mojom/feature_policy/feature_policy_feature.mojom-shared.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 17 | |
| 18 | namespace payments { |
| 19 | |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 20 | namespace { |
| 21 | |
Miyoung Shin | 5e74bd0 | 2019-09-30 10:54:53 | [diff] [blame] | 22 | using PaymentRequestFactoryCallback = base::RepeatingCallback<void( |
| 23 | mojo::PendingReceiver<mojom::PaymentRequest> receiver, |
| 24 | content::RenderFrameHost* render_frame_host)>; |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 25 | |
| 26 | PaymentRequestFactoryCallback& GetTestingFactoryCallback() { |
| 27 | static base::NoDestructor<PaymentRequestFactoryCallback> callback; |
| 28 | return *callback; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Miyoung Shin | 5e74bd0 | 2019-09-30 10:54:53 | [diff] [blame] | 33 | void CreatePaymentRequest( |
| 34 | content::RenderFrameHost* render_frame_host, |
| 35 | mojo::PendingReceiver<mojom::PaymentRequest> receiver) { |
Liquan (Max) Gu | c176476e | 2020-09-25 02:47:11 | [diff] [blame] | 36 | 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 Solomakhin | bc783e09 | 2019-11-25 16:54:44 | [diff] [blame] | 45 | if (!render_frame_host->IsFeatureEnabled( |
| 46 | blink::mojom::FeaturePolicyFeature::kPayment)) { |
| 47 | mojo::ReportBadMessage("Feature policy blocks Payment"); |
| 48 | return; |
| 49 | } |
| 50 | |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 51 | if (GetTestingFactoryCallback()) { |
Miyoung Shin | 5e74bd0 | 2019-09-30 10:54:53 | [diff] [blame] | 52 | return GetTestingFactoryCallback().Run(std::move(receiver), |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 53 | render_frame_host); |
| 54 | } |
| 55 | |
Rouslan Solomakhin | a4a9b8c | 2020-10-15 21:16:24 | [diff] [blame^] | 56 | PaymentRequestWebContentsManager::GetOrCreateForWebContents( |
| 57 | content::WebContents::FromRenderFrameHost(render_frame_host)) |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 58 | ->CreatePaymentRequest( |
Rouslan Solomakhin | a4a9b8c | 2020-10-15 21:16:24 | [diff] [blame^] | 59 | render_frame_host, |
| 60 | std::make_unique<ChromePaymentRequestDelegate>(render_frame_host), |
Miyoung Shin | 5e74bd0 | 2019-09-30 10:54:53 | [diff] [blame] | 61 | std::move(receiver), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 62 | /*observer_for_testing=*/nullptr); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 63 | } |
| 64 | |
danakj | 8638bd3 | 2019-06-28 18:45:41 | [diff] [blame] | 65 | void SetPaymentRequestFactoryForTesting( |
| 66 | PaymentRequestFactoryCallback factory_callback) { |
| 67 | GetTestingFactoryCallback() = std::move(factory_callback); |
| 68 | } |
| 69 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 70 | } // namespace payments |