fix: blank page when printing pdf (#43327)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2024-08-15 10:30:22 -04:00 committed by GitHub
parent bd70c3a740
commit a959476dc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 92 additions and 17 deletions

View file

@ -10,12 +10,20 @@
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/browser_process.h"
#include "components/pdf/browser/pdf_frame_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "electron/buildflags/buildflags.h"
#include "pdf/pdf_features.h"
#include "printing/backend/print_backend.h" // nogncheck
#include "printing/units.h"
#include "shell/common/thread_restrictions.h"
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#endif
#if BUILDFLAG(IS_MAC)
#include <ApplicationServices/ApplicationServices.h>
#endif
@ -68,6 +76,49 @@ bool IsDeviceNameValid(const std::u16string& device_name) {
#endif
}
// Duplicated from chrome/browser/printing/print_view_manager_common.cc
content::RenderFrameHost* GetFullPagePlugin(content::WebContents* contents) {
content::RenderFrameHost* full_page_plugin = nullptr;
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
contents->ForEachRenderFrameHostWithAction(
[&full_page_plugin](content::RenderFrameHost* rfh) {
auto* guest_view =
extensions::MimeHandlerViewGuest::FromRenderFrameHost(rfh);
if (guest_view && guest_view->is_full_page_plugin()) {
DCHECK_EQ(guest_view->GetGuestMainFrame(), rfh);
full_page_plugin = rfh;
return content::RenderFrameHost::FrameIterationAction::kStop;
}
return content::RenderFrameHost::FrameIterationAction::kContinue;
});
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
return full_page_plugin;
}
// Pick the right RenderFrameHost based on the WebContents.
// Modified from chrome/browser/printing/print_view_manager_common.cc
content::RenderFrameHost* GetRenderFrameHostToUse(
content::WebContents* contents) {
#if BUILDFLAG(ENABLE_PDF_VIEWER)
// Pick the plugin frame host if `contents` is a PDF viewer guest. If using
// OOPIF PDF viewer, pick the PDF extension frame host.
content::RenderFrameHost* full_page_pdf_embedder_host =
chrome_pdf::features::IsOopifPdfEnabled()
? pdf_frame_util::FindFullPagePdfExtensionHost(contents)
: GetFullPagePlugin(contents);
content::RenderFrameHost* pdf_rfh = pdf_frame_util::FindPdfChildFrame(
full_page_pdf_embedder_host ? full_page_pdf_embedder_host
: contents->GetPrimaryMainFrame());
if (pdf_rfh) {
return pdf_rfh;
}
#endif // BUILDFLAG(ENABLE_PDF)
auto* focused_frame = contents->GetFocusedFrame();
return (focused_frame && focused_frame->HasSelection())
? focused_frame
: contents->GetPrimaryMainFrame();
}
std::pair<std::string, std::u16string> GetDeviceNameToUse(
const std::u16string& device_name) {
#if BUILDFLAG(IS_WIN)