electron_content_utility_client.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/utility/electron_content_utility_client.h"
  5. #include <utility>
  6. #include "base/command_line.h"
  7. #include "content/public/utility/utility_thread.h"
  8. #include "mojo/public/cpp/bindings/binder_map.h"
  9. #include "mojo/public/cpp/bindings/service_factory.h"
  10. #include "printing/buildflags/buildflags.h"
  11. #include "sandbox/policy/mojom/sandbox.mojom.h"
  12. #include "sandbox/policy/sandbox_type.h"
  13. #include "services/proxy_resolver/proxy_resolver_factory_impl.h"
  14. #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
  15. #include "services/service_manager/public/cpp/service.h"
  16. #include "shell/services/node/node_service.h"
  17. #include "shell/services/node/public/mojom/node_service.mojom.h"
  18. #if BUILDFLAG(IS_WIN)
  19. #include "chrome/services/util_win/public/mojom/util_read_icon.mojom.h"
  20. #include "chrome/services/util_win/public/mojom/util_win.mojom.h"
  21. #include "chrome/services/util_win/util_read_icon.h"
  22. #include "chrome/services/util_win/util_win_impl.h"
  23. #endif // BUILDFLAG(IS_WIN)
  24. #if BUILDFLAG(ENABLE_PRINTING)
  25. #include "components/services/print_compositor/print_compositor_impl.h"
  26. #include "components/services/print_compositor/public/mojom/print_compositor.mojom.h" // nogncheck
  27. #endif // BUILDFLAG(ENABLE_PRINTING)
  28. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  29. #include "chrome/services/printing/print_backend_service_impl.h"
  30. #include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
  31. #endif // BUILDFLAG(ENABLE_OOP_PRINTING)
  32. #if BUILDFLAG(ENABLE_PRINTING) && BUILDFLAG(IS_WIN)
  33. #include "chrome/services/printing/pdf_to_emf_converter_factory.h"
  34. #endif
  35. #if BUILDFLAG(ENABLE_PRINT_PREVIEW) || \
  36. (BUILDFLAG(ENABLE_PRINTING) && BUILDFLAG(IS_WIN))
  37. #include "chrome/services/printing/printing_service.h"
  38. #include "chrome/services/printing/public/mojom/printing_service.mojom.h"
  39. #endif
  40. namespace electron {
  41. namespace {
  42. #if BUILDFLAG(ENABLE_PRINT_PREVIEW) || \
  43. (BUILDFLAG(ENABLE_PRINTING) && BUILDFLAG(IS_WIN))
  44. auto RunPrintingService(
  45. mojo::PendingReceiver<printing::mojom::PrintingService> receiver) {
  46. return std::make_unique<printing::PrintingService>(std::move(receiver));
  47. }
  48. #endif
  49. #if BUILDFLAG(IS_WIN)
  50. auto RunWindowsIconReader(
  51. mojo::PendingReceiver<chrome::mojom::UtilReadIcon> receiver) {
  52. return std::make_unique<UtilReadIcon>(std::move(receiver));
  53. }
  54. auto RunWindowsUtility(mojo::PendingReceiver<chrome::mojom::UtilWin> receiver) {
  55. return std::make_unique<UtilWinImpl>(std::move(receiver));
  56. }
  57. #endif
  58. #if BUILDFLAG(ENABLE_PRINTING)
  59. auto RunPrintCompositor(
  60. mojo::PendingReceiver<printing::mojom::PrintCompositor> receiver) {
  61. return std::make_unique<printing::PrintCompositorImpl>(
  62. std::move(receiver), true /* initialize_environment */,
  63. content::UtilityThread::Get()->GetIOTaskRunner());
  64. }
  65. #endif
  66. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  67. auto RunPrintingSandboxedPrintBackendHost(
  68. mojo::PendingReceiver<printing::mojom::SandboxedPrintBackendHost>
  69. receiver) {
  70. return std::make_unique<printing::SandboxedPrintBackendHostImpl>(
  71. std::move(receiver));
  72. }
  73. auto RunPrintingUnsandboxedPrintBackendHost(
  74. mojo::PendingReceiver<printing::mojom::UnsandboxedPrintBackendHost>
  75. receiver) {
  76. return std::make_unique<printing::UnsandboxedPrintBackendHostImpl>(
  77. std::move(receiver));
  78. }
  79. #endif // BUILDFLAG(ENABLE_OOP_PRINTING)
  80. auto RunProxyResolver(
  81. mojo::PendingReceiver<proxy_resolver::mojom::ProxyResolverFactory>
  82. receiver) {
  83. return std::make_unique<proxy_resolver::ProxyResolverFactoryImpl>(
  84. std::move(receiver));
  85. }
  86. auto RunNodeService(mojo::PendingReceiver<node::mojom::NodeService> receiver) {
  87. return std::make_unique<electron::NodeService>(std::move(receiver));
  88. }
  89. } // namespace
  90. ElectronContentUtilityClient::ElectronContentUtilityClient() = default;
  91. ElectronContentUtilityClient::~ElectronContentUtilityClient() = default;
  92. // The guts of this came from the chromium implementation
  93. // https://source.chromium.org/chromium/chromium/src/+/main:chrome/utility/chrome_content_utility_client.cc
  94. void ElectronContentUtilityClient::ExposeInterfacesToBrowser(
  95. mojo::BinderMap* binders) {
  96. #if BUILDFLAG(IS_WIN)
  97. const auto& cmd_line = *base::CommandLine::ForCurrentProcess();
  98. auto sandbox_type = sandbox::policy::SandboxTypeFromCommandLine(cmd_line);
  99. utility_process_running_elevated_ =
  100. sandbox_type == sandbox::mojom::Sandbox::kNoSandboxAndElevatedPrivileges;
  101. #endif
  102. // If our process runs with elevated privileges, only add elevated Mojo
  103. // interfaces to the BinderMap.
  104. if (!utility_process_running_elevated_) {
  105. #if BUILDFLAG(ENABLE_PRINTING) && BUILDFLAG(IS_WIN)
  106. binders->Add<printing::mojom::PdfToEmfConverterFactory>(
  107. base::BindRepeating(printing::PdfToEmfConverterFactory::Create),
  108. base::SingleThreadTaskRunner::GetCurrentDefault());
  109. #endif
  110. }
  111. }
  112. void ElectronContentUtilityClient::RegisterMainThreadServices(
  113. mojo::ServiceFactory& services) {
  114. #if BUILDFLAG(IS_WIN)
  115. services.Add(RunWindowsIconReader);
  116. services.Add(RunWindowsUtility);
  117. #endif
  118. #if BUILDFLAG(ENABLE_PRINTING)
  119. services.Add(RunPrintCompositor);
  120. #endif
  121. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  122. services.Add(RunPrintingSandboxedPrintBackendHost);
  123. services.Add(RunPrintingUnsandboxedPrintBackendHost);
  124. #endif
  125. #if BUILDFLAG(ENABLE_PRINT_PREVIEW) || \
  126. (BUILDFLAG(ENABLE_PRINTING) && BUILDFLAG(IS_WIN))
  127. services.Add(RunPrintingService);
  128. #endif
  129. services.Add(RunNodeService);
  130. }
  131. void ElectronContentUtilityClient::RegisterIOThreadServices(
  132. mojo::ServiceFactory& services) {
  133. services.Add(RunProxyResolver);
  134. }
  135. } // namespace electron