From 96fe03b200b24e884edd7d336763b5aa7246e14c Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 21:56:16 -0500 Subject: [PATCH] test: add -pdf-ready-to-print event to WebContents for testing (#43651) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- BUILD.gn | 1 + .../browser/api/electron_api_web_contents.cc | 4 +++ shell/browser/api/electron_api_web_contents.h | 2 ++ .../electron_pdf_document_helper_client.cc | 28 +++++++++++++------ .../electron_pdf_document_helper_client.h | 6 ++-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index bbd5e3f0949b..8afb3b064e27 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -744,6 +744,7 @@ source_set("electron_lib") { "//components/pdf/common:util", "//components/pdf/renderer", "//pdf", + "//pdf:content_restriction", ] sources += [ "shell/browser/electron_pdf_document_helper_client.cc", diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index ed4d02105bcd..bc7c1b61ce33 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3739,6 +3739,10 @@ void WebContents::OnInputEvent(const blink::WebInputEvent& event) { Emit("input-event", event); } +void WebContents::PDFReadyToPrint() { + Emit("-pdf-ready-to-print"); +} + void WebContents::RunJavaScriptDialog(content::WebContents* web_contents, content::RenderFrameHost* rfh, content::JavaScriptDialogType dialog_type, diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 2a44587134ca..4ee19ca9ce55 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -478,6 +478,8 @@ class WebContents : public ExclusiveAccessContext, void SetBackgroundColor(std::optional color); + void PDFReadyToPrint(); + SkRegion* draggable_region() { return force_non_draggable_ ? nullptr : draggable_region_.get(); } diff --git a/shell/browser/electron_pdf_document_helper_client.cc b/shell/browser/electron_pdf_document_helper_client.cc index 6250a32ed63e..6902f4c22f55 100644 --- a/shell/browser/electron_pdf_document_helper_client.cc +++ b/shell/browser/electron_pdf_document_helper_client.cc @@ -5,17 +5,29 @@ #include "shell/browser/electron_pdf_document_helper_client.h" #include "content/public/browser/web_contents.h" +#include "pdf/content_restriction.h" +#include "shell/browser/api/electron_api_web_contents.h" ElectronPDFDocumentHelperClient::ElectronPDFDocumentHelperClient() = default; ElectronPDFDocumentHelperClient::~ElectronPDFDocumentHelperClient() = default; void ElectronPDFDocumentHelperClient::UpdateContentRestrictions( content::RenderFrameHost* render_frame_host, - int content_restrictions) {} -void ElectronPDFDocumentHelperClient::OnPDFHasUnsupportedFeature( - content::WebContents* contents) {} -void ElectronPDFDocumentHelperClient::OnSaveURL( - content::WebContents* contents) {} -void ElectronPDFDocumentHelperClient::SetPluginCanSave( - content::RenderFrameHost* render_frame_host, - bool can_save) {} + int content_restrictions) { + // UpdateContentRestrictions potentially gets called twice from + // pdf/pdf_view_web_plugin.cc. The first time it is potentially called is + // when loading starts and it is called with a restriction on printing. The + // second time it is called is when loading is finished and if printing is + // allowed there won't be a printing restriction passed, so we can use this + // second call to notify that the pdf document is ready to print. + if (!(content_restrictions & chrome_pdf::kContentRestrictionPrint)) { + content::WebContents* web_contents = + content::WebContents::FromRenderFrameHost(render_frame_host); + electron::api::WebContents* api_web_contents = + electron::api::WebContents::From( + web_contents->GetOutermostWebContents()); + if (api_web_contents) { + api_web_contents->PDFReadyToPrint(); + } + } +} diff --git a/shell/browser/electron_pdf_document_helper_client.h b/shell/browser/electron_pdf_document_helper_client.h index bcc24a077487..55929e961a3c 100644 --- a/shell/browser/electron_pdf_document_helper_client.h +++ b/shell/browser/electron_pdf_document_helper_client.h @@ -19,10 +19,10 @@ class ElectronPDFDocumentHelperClient : public pdf::PDFDocumentHelperClient { // pdf::PDFDocumentHelperClient void UpdateContentRestrictions(content::RenderFrameHost* render_frame_host, int content_restrictions) override; - void OnPDFHasUnsupportedFeature(content::WebContents* contents) override; - void OnSaveURL(content::WebContents* contents) override; + void OnPDFHasUnsupportedFeature(content::WebContents* contents) override {} + void OnSaveURL(content::WebContents* contents) override {} void SetPluginCanSave(content::RenderFrameHost* render_frame_host, - bool can_save) override; + bool can_save) override {} }; #endif // ELECTRON_SHELL_BROWSER_ELECTRON_PDF_DOCUMENT_HELPER_CLIENT_H_