diff --git a/shell/browser/extensions/electron_extensions_api_client.cc b/shell/browser/extensions/electron_extensions_api_client.cc index b6873f523cc1..9a74b5a6add0 100644 --- a/shell/browser/extensions/electron_extensions_api_client.cc +++ b/shell/browser/extensions/electron_extensions_api_client.cc @@ -14,6 +14,12 @@ #include "shell/browser/extensions/electron_extension_web_contents_observer.h" #include "shell/browser/extensions/electron_messaging_delegate.h" +#if BUILDFLAG(ENABLE_PRINTING) +#include "chrome/browser/printing/print_view_manager_basic.h" +#include "components/printing/browser/print_manager_utils.h" +#include "shell/browser/printing/print_preview_message_handler.h" +#endif + #if BUILDFLAG(ENABLE_PDF_VIEWER) #include "components/pdf/browser/pdf_web_contents_helper.h" // nogncheck #include "shell/browser/electron_pdf_web_contents_helper_client.h" @@ -52,13 +58,18 @@ MessagingDelegate* ElectronExtensionsAPIClient::GetMessagingDelegate() { void ElectronExtensionsAPIClient::AttachWebContentsHelpers( content::WebContents* web_contents) const { - extensions::ElectronExtensionWebContentsObserver::CreateForWebContents( - web_contents); +#if BUILDFLAG(ENABLE_PRINTING) + electron::PrintPreviewMessageHandler::CreateForWebContents(web_contents); + printing::PrintViewManagerBasic::CreateForWebContents(web_contents); +#endif #if BUILDFLAG(ENABLE_PDF_VIEWER) pdf::PDFWebContentsHelper::CreateForWebContentsWithClient( web_contents, std::make_unique()); #endif + + extensions::ElectronExtensionWebContentsObserver::CreateForWebContents( + web_contents); } ManagementAPIDelegate* diff --git a/shell/renderer/printing/print_render_frame_helper_delegate.cc b/shell/renderer/printing/print_render_frame_helper_delegate.cc index 1ee85036c6be..c554d4d723a4 100644 --- a/shell/renderer/printing/print_render_frame_helper_delegate.cc +++ b/shell/renderer/printing/print_render_frame_helper_delegate.cc @@ -5,9 +5,14 @@ #include "shell/renderer/printing/print_render_frame_helper_delegate.h" #include "content/public/renderer/render_frame.h" +#include "extensions/buildflags/buildflags.h" #include "third_party/blink/public/web/web_element.h" #include "third_party/blink/public/web/web_local_frame.h" +#if BUILDFLAG(ENABLE_EXTENSIONS) +#include "extensions/common/constants.h" +#endif // BUILDFLAG(ENABLE_EXTENSIONS) + namespace electron { PrintRenderFrameHelperDelegate::PrintRenderFrameHelperDelegate() = default; @@ -17,6 +22,25 @@ PrintRenderFrameHelperDelegate::~PrintRenderFrameHelperDelegate() = default; // Return the PDF object element if |frame| is the out of process PDF extension. blink::WebElement PrintRenderFrameHelperDelegate::GetPdfElement( blink::WebLocalFrame* frame) { +#if BUILDFLAG(ENABLE_EXTENSIONS) + GURL url = frame->GetDocument().Url(); + bool inside_pdf_extension = + url.SchemeIs(extensions::kExtensionScheme) && + url.host_piece() == extension_misc::kPdfExtensionId; + if (inside_pdf_extension) { + // with id="plugin" is created in + // chrome/browser/resources/pdf/pdf_viewer_base.js. + auto viewer_element = frame->GetDocument().GetElementById("viewer"); + if (!viewer_element.IsNull() && !viewer_element.ShadowRoot().IsNull()) { + auto plugin_element = + viewer_element.ShadowRoot().QuerySelector("#plugin"); + if (!plugin_element.IsNull()) { + return plugin_element; + } + } + NOTREACHED(); + } +#endif // BUILDFLAG(ENABLE_EXTENSIONS) return blink::WebElement(); } diff --git a/shell/renderer/renderer_client_base.cc b/shell/renderer/renderer_client_base.cc index 43b66b640546..abb8d8778e76 100644 --- a/shell/renderer/renderer_client_base.cc +++ b/shell/renderer/renderer_client_base.cc @@ -54,7 +54,6 @@ #endif #if BUILDFLAG(ENABLE_PDF_VIEWER) -#include "chrome/renderer/pepper/chrome_pdf_print_client.h" // nogncheck #include "shell/common/electron_constants.h" #endif // BUILDFLAG(ENABLE_PDF_VIEWER) @@ -173,7 +172,8 @@ void RendererClientBase::RenderThreadStarted() { #if BUILDFLAG(ENABLE_PDF_VIEWER) // Enables printing from Chrome PDF viewer. - pdf::PepperPDFHost::SetPrintClient(new ChromePDFPrintClient()); + pdf_print_client_.reset(new ChromePDFPrintClient()); + pdf::PepperPDFHost::SetPrintClient(pdf_print_client_.get()); #endif #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) diff --git a/shell/renderer/renderer_client_base.h b/shell/renderer/renderer_client_base.h index 4a0d5d3309ea..433626dfa108 100644 --- a/shell/renderer/renderer_client_base.h +++ b/shell/renderer/renderer_client_base.h @@ -11,6 +11,7 @@ #include "content/public/renderer/content_renderer_client.h" #include "electron/buildflags/buildflags.h" +#include "printing/buildflags/buildflags.h" #include "third_party/blink/public/web/web_local_frame.h" // In SHARED_INTERMEDIATE_DIR. #include "widevine_cdm_version.h" // NOLINT(build/include_directory) @@ -19,6 +20,10 @@ #include "chrome/renderer/media/chrome_key_systems_provider.h" // nogncheck #endif +#if BUILDFLAG(ENABLE_PDF_VIEWER) +#include "chrome/renderer/pepper/chrome_pdf_print_client.h" // nogncheck +#endif // BUILDFLAG(ENABLE_PDF_VIEWER) + #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) #include "services/service_manager/public/cpp/binder_registry.h" #include "services/service_manager/public/cpp/local_interface_provider.h" @@ -146,6 +151,9 @@ class RendererClientBase : public content::ContentRendererClient #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) std::unique_ptr spellcheck_; #endif +#if BUILDFLAG(ENABLE_PDF_VIEWER) + std::unique_ptr pdf_print_client_; +#endif }; } // namespace electron