weblayer_browser_interface_binders.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 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. #include "weblayer/browser/weblayer_browser_interface_binders.h"
  5. #include "base/bind.h"
  6. #include "build/build_config.h"
  7. #include "components/no_state_prefetch/browser/no_state_prefetch_contents.h"
  8. #include "components/no_state_prefetch/browser/no_state_prefetch_processor_impl.h"
  9. #include "components/no_state_prefetch/common/prerender_canceler.mojom.h"
  10. #include "components/payments/content/payment_credential_factory.h"
  11. #include "content/public/browser/browser_context.h"
  12. #include "content/public/browser/render_frame_host.h"
  13. #include "content/public/browser/web_contents.h"
  14. #include "content/public/browser/web_ui.h"
  15. #include "content/public/browser/web_ui_controller.h"
  16. #include "third_party/blink/public/mojom/payments/payment_credential.mojom.h"
  17. #include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
  18. #include "third_party/blink/public/mojom/prerender/prerender.mojom.h"
  19. #include "weblayer/browser/no_state_prefetch/no_state_prefetch_processor_impl_delegate_impl.h"
  20. #include "weblayer/browser/no_state_prefetch/no_state_prefetch_utils.h"
  21. #include "weblayer/browser/translate_client_impl.h"
  22. #include "weblayer/browser/webui/weblayer_internals.mojom.h"
  23. #include "weblayer/browser/webui/weblayer_internals_ui.h"
  24. #if BUILDFLAG(IS_ANDROID)
  25. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  26. #include "services/service_manager/public/cpp/interface_provider.h"
  27. #include "third_party/blink/public/mojom/installedapp/installed_app_provider.mojom.h"
  28. #include "third_party/blink/public/mojom/webshare/webshare.mojom.h"
  29. #endif
  30. namespace weblayer {
  31. namespace {
  32. void BindContentTranslateDriver(
  33. content::RenderFrameHost* host,
  34. mojo::PendingReceiver<translate::mojom::ContentTranslateDriver> receiver) {
  35. // Translation does not currently work in subframes.
  36. // TODO(crbug.com/1073370): Transition WebLayer to per-frame translation
  37. // architecture once it's ready.
  38. if (host->GetParent())
  39. return;
  40. auto* contents = content::WebContents::FromRenderFrameHost(host);
  41. if (!contents)
  42. return;
  43. TranslateClientImpl* const translate_client =
  44. TranslateClientImpl::FromWebContents(contents);
  45. translate_client->translate_driver()->AddReceiver(std::move(receiver));
  46. }
  47. void BindPageHandler(
  48. content::RenderFrameHost* host,
  49. mojo::PendingReceiver<weblayer_internals::mojom::PageHandler> receiver) {
  50. auto* contents = content::WebContents::FromRenderFrameHost(host);
  51. if (!contents)
  52. return;
  53. content::WebUI* web_ui = contents->GetWebUI();
  54. // Performs a safe downcast to the concrete WebUIController subclass.
  55. WebLayerInternalsUI* concrete_controller =
  56. web_ui ? web_ui->GetController()->GetAs<WebLayerInternalsUI>() : nullptr;
  57. // This is expected to be called only for main frames and for the right
  58. // WebUI pages matching the same WebUI associated to the RenderFrameHost.
  59. if (host->GetParent() || !concrete_controller)
  60. return;
  61. concrete_controller->BindInterface(std::move(receiver));
  62. }
  63. void BindNoStatePrefetchProcessor(
  64. content::RenderFrameHost* frame_host,
  65. mojo::PendingReceiver<blink::mojom::NoStatePrefetchProcessor> receiver) {
  66. prerender::NoStatePrefetchProcessorImpl::Create(
  67. frame_host, std::move(receiver),
  68. std::make_unique<NoStatePrefetchProcessorImplDelegateImpl>());
  69. }
  70. void BindPrerenderCanceler(
  71. content::RenderFrameHost* frame_host,
  72. mojo::PendingReceiver<prerender::mojom::PrerenderCanceler> receiver) {
  73. auto* web_contents = content::WebContents::FromRenderFrameHost(frame_host);
  74. if (!web_contents)
  75. return;
  76. auto* no_state_prefetch_contents =
  77. NoStatePrefetchContentsFromWebContents(web_contents);
  78. if (!no_state_prefetch_contents)
  79. return;
  80. no_state_prefetch_contents->AddPrerenderCancelerReceiver(std::move(receiver));
  81. }
  82. #if BUILDFLAG(IS_ANDROID)
  83. template <typename Interface>
  84. void ForwardToJavaWebContents(content::RenderFrameHost* frame_host,
  85. mojo::PendingReceiver<Interface> receiver) {
  86. content::WebContents* contents =
  87. content::WebContents::FromRenderFrameHost(frame_host);
  88. if (contents)
  89. contents->GetJavaInterfaces()->GetInterface(std::move(receiver));
  90. }
  91. template <typename Interface>
  92. void ForwardToJavaFrame(content::RenderFrameHost* render_frame_host,
  93. mojo::PendingReceiver<Interface> receiver) {
  94. render_frame_host->GetJavaInterfaces()->GetInterface(std::move(receiver));
  95. }
  96. #endif
  97. } // namespace
  98. void PopulateWebLayerFrameBinders(
  99. content::RenderFrameHost* render_frame_host,
  100. mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
  101. map->Add<weblayer_internals::mojom::PageHandler>(
  102. base::BindRepeating(&BindPageHandler));
  103. map->Add<translate::mojom::ContentTranslateDriver>(
  104. base::BindRepeating(&BindContentTranslateDriver));
  105. map->Add<blink::mojom::NoStatePrefetchProcessor>(
  106. base::BindRepeating(&BindNoStatePrefetchProcessor));
  107. map->Add<prerender::mojom::PrerenderCanceler>(
  108. base::BindRepeating(&BindPrerenderCanceler));
  109. #if BUILDFLAG(IS_ANDROID)
  110. map->Add<blink::mojom::InstalledAppProvider>(base::BindRepeating(
  111. &ForwardToJavaFrame<blink::mojom::InstalledAppProvider>));
  112. map->Add<blink::mojom::ShareService>(base::BindRepeating(
  113. &ForwardToJavaWebContents<blink::mojom::ShareService>));
  114. map->Add<payments::mojom::PaymentRequest>(base::BindRepeating(
  115. &ForwardToJavaFrame<payments::mojom::PaymentRequest>));
  116. map->Add<payments::mojom::PaymentCredential>(
  117. base::BindRepeating(&payments::CreatePaymentCredential));
  118. #endif
  119. }
  120. } // namespace weblayer