From 25c75c5fc4fc7d7fbdebdf20ca56ab3162eab570 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Wed, 12 Oct 2022 17:53:06 -0400 Subject: [PATCH] Use new PrintViewManagerBase::PrintToPdf method (fixes issue #3377) --- BUILD.gn | 4 +- CHROMIUM_UPDATE.txt | 2 - include/cef_api_hash.h | 8 +- include/internal/cef_types.h | 97 +++-- include/internal/cef_types_wrappers.h | 35 +- .../browser/alloy/alloy_browser_host_impl.cc | 24 -- .../browser/alloy/alloy_browser_host_impl.h | 4 - .../alloy/alloy_content_browser_client.cc | 4 +- .../alloy/browser_platform_delegate_alloy.cc | 56 +-- .../alloy/browser_platform_delegate_alloy.h | 4 - libcef/browser/browser_host_base.cc | 32 ++ libcef/browser/browser_host_base.h | 4 + libcef/browser/browser_platform_delegate.cc | 11 - libcef/browser/browser_platform_delegate.h | 4 - .../browser_platform_delegate_chrome.cc | 7 + .../chrome/browser_platform_delegate_chrome.h | 1 + .../chrome/chrome_browser_host_impl.cc | 25 -- .../browser/chrome/chrome_browser_host_impl.h | 4 - .../extensions/extensions_api_client.cc | 4 +- libcef/browser/printing/print_dialog_linux.cc | 18 +- libcef/browser/printing/print_dialog_linux.h | 3 - libcef/browser/printing/print_util.cc | 143 +++++++ libcef/browser/printing/print_util.h | 28 ++ libcef/browser/printing/print_view_manager.cc | 373 ------------------ libcef/browser/printing/print_view_manager.h | 107 ----- patch/patch.cfg | 5 - patch/patches/print_preview_123.patch | 14 - patch/patches/printing_pdf_3047.patch | 37 -- tests/cefclient/browser/test_runner.cc | 19 +- 29 files changed, 324 insertions(+), 753 deletions(-) create mode 100644 libcef/browser/printing/print_util.cc create mode 100644 libcef/browser/printing/print_util.h delete mode 100644 libcef/browser/printing/print_view_manager.cc delete mode 100644 libcef/browser/printing/print_view_manager.h delete mode 100644 patch/patches/printing_pdf_3047.patch diff --git a/BUILD.gn b/BUILD.gn index ae03d34d6..92e2b2375 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -715,8 +715,8 @@ source_set("libcef_static") { "libcef/browser/prefs/renderer_prefs.h", "libcef/browser/print_settings_impl.cc", "libcef/browser/print_settings_impl.h", - "libcef/browser/printing/print_view_manager.cc", - "libcef/browser/printing/print_view_manager.h", + "libcef/browser/printing/print_util.cc", + "libcef/browser/printing/print_util.h", "libcef/browser/process_util_impl.cc", "libcef/browser/request_context_handler_map.cc", "libcef/browser/request_context_handler_map.h", diff --git a/CHROMIUM_UPDATE.txt b/CHROMIUM_UPDATE.txt index 9db928e4e..5bac2ff8a 100644 --- a/CHROMIUM_UPDATE.txt +++ b/CHROMIUM_UPDATE.txt @@ -33,8 +33,6 @@ 'chrome/browser/extensions/chrome_extension_web_contents_observer.*', 'chrome/browser/extensions/component_loader.*', 'chrome/browser/extensions/extension_service.*', - 'chrome/browser/printing/print_view_manager*', - 'chrome/browser/printing/printing_message_filter*', 'chrome/browser/profiles/profile.h', 'chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.*', 'chrome/common/extensions/api/*_features.json', diff --git a/include/cef_api_hash.h b/include/cef_api_hash.h index 521d38acc..1ccf8b812 100644 --- a/include/cef_api_hash.h +++ b/include/cef_api_hash.h @@ -42,13 +42,13 @@ // way that may cause binary incompatibility with other builds. The universal // hash value will change if any platform is affected whereas the platform hash // values will change only if that particular platform is affected. -#define CEF_API_HASH_UNIVERSAL "befd42a0b7773d9e98205d8024d90af580a6d4fa" +#define CEF_API_HASH_UNIVERSAL "057e58f3e7e2fcd056a78d2354f3de44f31c00a5" #if defined(OS_WIN) -#define CEF_API_HASH_PLATFORM "c9d32d56f34afe8c33b115ece110c54a8854b2c2" +#define CEF_API_HASH_PLATFORM "ceffda27fda02aa102a6241b65cd3fdeb531d2c1" #elif defined(OS_MAC) -#define CEF_API_HASH_PLATFORM "11ee1bfabb19306adf55aaf71de537514557245c" +#define CEF_API_HASH_PLATFORM "18faa4c5663b9665a4b64347bfeb26eaa8d0acfd" #elif defined(OS_LINUX) -#define CEF_API_HASH_PLATFORM "c093ef6f613de44faf3d23abf6b668054d9c749d" +#define CEF_API_HASH_PLATFORM "8e087e42c70649ae19534a1680090d8f130cbc56" #endif #ifdef __cplusplus diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index 776461fe7..009539c0a 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -2480,7 +2480,7 @@ typedef enum { /// typedef enum { /// - /// Default margins. + /// Default margins of 1cm (~0.4 inches). /// PDF_PRINT_MARGIN_DEFAULT, @@ -2489,11 +2489,6 @@ typedef enum { /// PDF_PRINT_MARGIN_NONE, - /// - /// Minimum margins. - /// - PDF_PRINT_MARGIN_MINIMUM, - /// /// Custom margins using the |margin_*| values from cef_pdf_print_settings_t. /// @@ -2501,43 +2496,41 @@ typedef enum { } cef_pdf_print_margin_type_t; /// -/// Structure representing PDF print settings. +/// Structure representing PDF print settings. These values match the parameters +/// supported by the DevTools Page.printToPDF function. See +/// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF /// typedef struct _cef_pdf_print_settings_t { /// - /// Page title to display in the header. Only used if |header_footer_enabled| - /// is set to true (1). + /// Set to true (1) for landscape mode or false (0) for portrait mode. /// - cef_string_t header_footer_title; + int landscape; /// - /// URL to display in the footer. Only used if |header_footer_enabled| is set - /// to true (1). + /// Set to true (1) to print background graphics. /// - cef_string_t header_footer_url; + int print_background; /// - /// Output page size in microns. If either of these values is less than or - /// equal to zero then the default paper size (A4) will be used. - /// - int page_width; - int page_height; - - /// - /// The percentage to scale the PDF by before printing (e.g. 50 is 50%). - /// If this value is less than or equal to zero the default value of 100 + /// The percentage to scale the PDF by before printing (e.g. .5 is 50%). + /// If this value is less than or equal to zero the default value of 1.0 /// will be used. /// - int scale_factor; + double scale; /// - /// Margins in points. Only used if |margin_type| is set to - /// PDF_PRINT_MARGIN_CUSTOM. + /// Output paper size in inches. If either of these values is less than or + /// equal to zero then the default paper size (letter, 8.5 x 11 inches) will + /// be used. /// - int margin_top; - int margin_right; - int margin_bottom; - int margin_left; + double paper_width; + double paper_height; + + /// + /// Set to true (1) to prefer page size as defined by css. Defaults to false + /// (0), in which case the content will be scaled to fit the paper size. + /// + int prefer_css_page_size; /// /// Margin type. @@ -2545,27 +2538,53 @@ typedef struct _cef_pdf_print_settings_t { cef_pdf_print_margin_type_t margin_type; /// - /// Set to true (1) to print headers and footers or false (0) to not print - /// headers and footers. + /// Margins in inches. Only used if |margin_type| is set to + /// PDF_PRINT_MARGIN_CUSTOM. /// - int header_footer_enabled; + double margin_top; + double margin_right; + double margin_bottom; + double margin_left; /// - /// Set to true (1) to print the selection only or false (0) to print all. + /// Paper ranges to print, one based, e.g., '1-5, 8, 11-13'. Pages are printed + /// in the document order, not in the order specified, and no more than once. + /// Defaults to empty string, which implies the entire document is printed. + /// The page numbers are quietly capped to actual page count of the document, + /// and ranges beyond the end of the document are ignored. If this results in + /// no pages to print, an error is reported. It is an error to specify a range + /// with start greater than end. /// - int selection_only; + cef_string_t page_ranges; /// - /// Set to true (1) for landscape mode or false (0) for portrait mode. + /// Set to true (1) to display the header and/or footer. Modify + /// |header_template| and/or |footer_template| to customize the display. /// - int landscape; + int display_header_footer; /// - /// Set to true (1) to print background graphics or false (0) to not print - /// background graphics. + /// HTML template for the print header. Only displayed if + /// |display_header_footer| is true (1). Should be valid HTML markup with + /// the following classes used to inject printing values into them: /// - int backgrounds_enabled; + /// - date: formatted print date + /// - title: document title + /// - url: document location + /// - pageNumber: current page number + /// - totalPages: total pages in the document + /// + /// For example, "" would generate a span containing + /// the title. + /// + cef_string_t header_template; + /// + /// HTML template for the print footer. Only displayed if + /// |display_header_footer| is true (1). Uses the same format as + /// |header_template|. + /// + cef_string_t footer_template; } cef_pdf_print_settings_t; /// diff --git a/include/internal/cef_types_wrappers.h b/include/internal/cef_types_wrappers.h index 40e7799b9..33574ef7c 100644 --- a/include/internal/cef_types_wrappers.h +++ b/include/internal/cef_types_wrappers.h @@ -644,34 +644,35 @@ struct CefPdfPrintSettingsTraits { static inline void init(struct_type* s) {} static inline void clear(struct_type* s) { - cef_string_clear(&s->header_footer_title); - cef_string_clear(&s->header_footer_url); + cef_string_clear(&s->page_ranges); + cef_string_clear(&s->header_template); + cef_string_clear(&s->footer_template); } static inline void set(const struct_type* src, struct_type* target, bool copy) { - cef_string_set(src->header_footer_title.str, - src->header_footer_title.length, - &target->header_footer_title, copy); - cef_string_set(src->header_footer_url.str, src->header_footer_url.length, - &target->header_footer_url, copy); - - target->page_width = src->page_width; - target->page_height = src->page_height; - - target->scale_factor = src->scale_factor; + target->landscape = src->landscape; + target->print_background = src->print_background; + target->scale = src->scale; + target->paper_width = src->paper_width; + target->paper_height = src->paper_height; + target->prefer_css_page_size = src->prefer_css_page_size; + target->margin_type = src->margin_type; target->margin_top = src->margin_top; target->margin_right = src->margin_right; target->margin_bottom = src->margin_bottom; target->margin_left = src->margin_left; - target->margin_type = src->margin_type; - target->header_footer_enabled = src->header_footer_enabled; - target->selection_only = src->selection_only; - target->landscape = src->landscape; - target->backgrounds_enabled = src->backgrounds_enabled; + cef_string_set(src->page_ranges.str, src->page_ranges.length, + &target->page_ranges, copy); + + target->display_header_footer = src->display_header_footer; + cef_string_set(src->header_template.str, src->header_template.length, + &target->header_template, copy); + cef_string_set(src->footer_template.str, src->footer_template.length, + &target->footer_template, copy); } }; diff --git a/libcef/browser/alloy/alloy_browser_host_impl.cc b/libcef/browser/alloy/alloy_browser_host_impl.cc index 7783c2072..2eaec5b07 100644 --- a/libcef/browser/alloy/alloy_browser_host_impl.cc +++ b/libcef/browser/alloy/alloy_browser_host_impl.cc @@ -349,30 +349,6 @@ void AlloyBrowserHostImpl::SetZoomLevel(double zoomLevel) { } } -void AlloyBrowserHostImpl::Print() { - if (!CEF_CURRENTLY_ON_UIT()) { - CEF_POST_TASK(CEF_UIT, base::BindOnce(&AlloyBrowserHostImpl::Print, this)); - return; - } - - if (platform_delegate_) - platform_delegate_->Print(); -} - -void AlloyBrowserHostImpl::PrintToPDF(const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) { - if (!CEF_CURRENTLY_ON_UIT()) { - CEF_POST_TASK(CEF_UIT, base::BindOnce(&AlloyBrowserHostImpl::PrintToPDF, - this, path, settings, callback)); - return; - } - - if (platform_delegate_) { - platform_delegate_->PrintToPDF(path, settings, callback); - } -} - void AlloyBrowserHostImpl::Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/alloy/alloy_browser_host_impl.h b/libcef/browser/alloy/alloy_browser_host_impl.h index bddc2d008..f7241996f 100644 --- a/libcef/browser/alloy/alloy_browser_host_impl.h +++ b/libcef/browser/alloy/alloy_browser_host_impl.h @@ -82,10 +82,6 @@ class AlloyBrowserHostImpl : public CefBrowserHostBase, CefWindowHandle GetOpenerWindowHandle() override; double GetZoomLevel() override; void SetZoomLevel(double zoomLevel) override; - void Print() override; - void PrintToPDF(const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) override; void Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/alloy/alloy_content_browser_client.cc b/libcef/browser/alloy/alloy_content_browser_client.cc index 3d01ae27b..d5326f55e 100644 --- a/libcef/browser/alloy/alloy_content_browser_client.cc +++ b/libcef/browser/alloy/alloy_content_browser_client.cc @@ -32,7 +32,6 @@ #include "libcef/browser/net_service/proxy_url_loader_factory.h" #include "libcef/browser/net_service/resource_request_handler_wrapper.h" #include "libcef/browser/prefs/renderer_prefs.h" -#include "libcef/browser/printing/print_view_manager.h" #include "libcef/browser/speech_recognition_manager_delegate.h" #include "libcef/browser/thread_util.h" #include "libcef/browser/x509_certificate_impl.h" @@ -66,6 +65,7 @@ #include "chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.h" #include "chrome/browser/plugins/plugin_utils.h" #include "chrome/browser/predictors/network_hints_handler_impl.h" +#include "chrome/browser/printing/print_view_manager.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/renderer_updater.h" #include "chrome/browser/profiles/renderer_updater_factory.h" @@ -946,7 +946,7 @@ void AlloyContentBrowserClient:: [](content::RenderFrameHost* render_frame_host, mojo::PendingAssociatedReceiver receiver) { - printing::CefPrintViewManager::BindPrintManagerHost( + printing::PrintViewManager::BindPrintManagerHost( std::move(receiver), render_frame_host); }, &render_frame_host)); diff --git a/libcef/browser/alloy/browser_platform_delegate_alloy.cc b/libcef/browser/alloy/browser_platform_delegate_alloy.cc index 47f2f008f..33ed00291 100644 --- a/libcef/browser/alloy/browser_platform_delegate_alloy.cc +++ b/libcef/browser/alloy/browser_platform_delegate_alloy.cc @@ -11,14 +11,12 @@ #include "libcef/browser/extensions/extension_system.h" #include "libcef/browser/extensions/extension_view_host.h" #include "libcef/browser/extensions/extension_web_contents_observer.h" -#include "libcef/browser/printing/print_view_manager.h" #include "libcef/common/extensions/extensions_util.h" #include "libcef/common/net/url_util.h" #include "libcef/features/runtime_checks.h" #include "base/logging.h" #include "chrome/browser/printing/print_view_manager.h" -#include "chrome/browser/printing/print_view_manager_common.h" #include "chrome/browser/ui/prefs/prefs_tab_helper.h" #include "components/find_in_page/find_tab_helper.h" #include "components/find_in_page/find_types.h" @@ -30,18 +28,8 @@ #include "content/public/browser/render_view_host.h" #include "extensions/browser/process_manager.h" #include "pdf/pdf_features.h" -#include "printing/mojom/print.mojom.h" #include "third_party/blink/public/mojom/frame/find_in_page.mojom.h" -namespace { - -printing::CefPrintViewManager* GetPrintViewManager( - content::WebContents* web_contents) { - return printing::CefPrintViewManager::FromWebContents(web_contents); -} - -} // namespace - CefBrowserPlatformDelegateAlloy::CefBrowserPlatformDelegateAlloy() : weak_ptr_factory_(this) {} @@ -181,7 +169,7 @@ void CefBrowserPlatformDelegateAlloy::BrowserCreated( permissions::PermissionRequestManager::CreateForWebContents(web_contents_); PrefsTabHelper::CreateForWebContents(web_contents_); - printing::CefPrintViewManager::CreateForWebContents(web_contents_); + printing::PrintViewManager::CreateForWebContents(web_contents_); if (extensions::ExtensionsEnabled()) { // Used by the tabs extension API. @@ -357,48 +345,6 @@ bool CefBrowserPlatformDelegateAlloy::IsPrintPreviewSupported() const { return cef_browser_context->IsPrintPreviewSupported(); } -void CefBrowserPlatformDelegateAlloy::Print() { - REQUIRE_ALLOY_RUNTIME(); - - auto contents_to_use = printing::GetWebContentsToUse(web_contents_); - if (!contents_to_use) - return; - - auto rfh_to_use = printing::GetRenderFrameHostToUse(contents_to_use); - if (!rfh_to_use) - return; - - if (IsPrintPreviewSupported()) { - GetPrintViewManager(contents_to_use)->PrintPreviewNow(rfh_to_use, false); - } else { - GetPrintViewManager(contents_to_use)->PrintNow(rfh_to_use); - } -} - -void CefBrowserPlatformDelegateAlloy::PrintToPDF( - const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) { - REQUIRE_ALLOY_RUNTIME(); - - auto contents_to_use = printing::GetWebContentsToUse(web_contents_); - if (!contents_to_use) - return; - - auto rfh_to_use = printing::GetRenderFrameHostToUse(contents_to_use); - if (!rfh_to_use) - return; - - printing::CefPrintViewManager::PdfPrintCallback pdf_callback; - if (callback.get()) { - pdf_callback = base::BindOnce(&CefPdfPrintCallback::OnPdfPrintFinished, - callback.get(), path); - } - GetPrintViewManager(contents_to_use) - ->PrintToPDF(rfh_to_use, base::FilePath(path), settings, - std::move(pdf_callback)); -} - void CefBrowserPlatformDelegateAlloy::Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/alloy/browser_platform_delegate_alloy.h b/libcef/browser/alloy/browser_platform_delegate_alloy.h index 6286fe900..dbd9e5c0e 100644 --- a/libcef/browser/alloy/browser_platform_delegate_alloy.h +++ b/libcef/browser/alloy/browser_platform_delegate_alloy.h @@ -56,10 +56,6 @@ class CefBrowserPlatformDelegateAlloy : public CefBrowserPlatformDelegate { const CefSize& max_size) override; void SetAccessibilityState(cef_state_t accessibility_state) override; bool IsPrintPreviewSupported() const override; - void Print() override; - void PrintToPDF(const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) override; void Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/browser_host_base.cc b/libcef/browser/browser_host_base.cc index e0a2c1b60..70b06a4f8 100644 --- a/libcef/browser/browser_host_base.cc +++ b/libcef/browser/browser_host_base.cc @@ -11,6 +11,7 @@ #include "libcef/browser/context.h" #include "libcef/browser/image_impl.h" #include "libcef/browser/navigation_entry_impl.h" +#include "libcef/browser/printing/print_util.h" #include "libcef/browser/thread_util.h" #include "libcef/common/frame_util.h" #include "libcef/common/net/url_util.h" @@ -348,6 +349,37 @@ void CefBrowserHostBase::DownloadImage( max_image_size, callback)); } +void CefBrowserHostBase::Print() { + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefBrowserHostBase::Print, this)); + return; + } + + auto web_contents = GetWebContents(); + if (!web_contents) + return; + + const bool print_preview_disabled = + !platform_delegate_ || !platform_delegate_->IsPrintPreviewSupported(); + print_util::Print(web_contents, print_preview_disabled); +} + +void CefBrowserHostBase::PrintToPDF(const CefString& path, + const CefPdfPrintSettings& settings, + CefRefPtr callback) { + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefBrowserHostBase::PrintToPDF, this, + path, settings, callback)); + return; + } + + auto web_contents = GetWebContents(); + if (!web_contents) + return; + + print_util::PrintToPDF(web_contents, path, settings, callback); +} + bool CefBrowserHostBase::SendDevToolsMessage(const void* message, size_t message_size) { if (!message || message_size == 0) diff --git a/libcef/browser/browser_host_base.h b/libcef/browser/browser_host_base.h index fd5de196e..a14b27454 100644 --- a/libcef/browser/browser_host_base.h +++ b/libcef/browser/browser_host_base.h @@ -181,6 +181,10 @@ class CefBrowserHostBase : public CefBrowserHost, uint32 max_image_size, bool bypass_cache, CefRefPtr callback) override; + void Print() override; + void PrintToPDF(const CefString& path, + const CefPdfPrintSettings& settings, + CefRefPtr callback) override; void ReplaceMisspelling(const CefString& word) override; void AddWordToDictionary(const CefString& word) override; void SendKeyEvent(const CefKeyEvent& event) override; diff --git a/libcef/browser/browser_platform_delegate.cc b/libcef/browser/browser_platform_delegate.cc index 46b4cbb23..59c7c476f 100644 --- a/libcef/browser/browser_platform_delegate.cc +++ b/libcef/browser/browser_platform_delegate.cc @@ -390,17 +390,6 @@ bool CefBrowserPlatformDelegate::IsPrintPreviewSupported() const { return true; } -void CefBrowserPlatformDelegate::Print() { - NOTIMPLEMENTED(); -} - -void CefBrowserPlatformDelegate::PrintToPDF( - const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) { - NOTIMPLEMENTED(); -} - void CefBrowserPlatformDelegate::Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/browser_platform_delegate.h b/libcef/browser/browser_platform_delegate.h index 3518faa93..cd257f641 100644 --- a/libcef/browser/browser_platform_delegate.h +++ b/libcef/browser/browser_platform_delegate.h @@ -363,10 +363,6 @@ class CefBrowserPlatformDelegate { const CefSize& max_size); virtual void SetAccessibilityState(cef_state_t accessibility_state); virtual bool IsPrintPreviewSupported() const; - virtual void Print(); - virtual void PrintToPDF(const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback); virtual void Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/chrome/browser_platform_delegate_chrome.cc b/libcef/browser/chrome/browser_platform_delegate_chrome.cc index 83820643d..0c9ccc808 100644 --- a/libcef/browser/chrome/browser_platform_delegate_chrome.cc +++ b/libcef/browser/chrome/browser_platform_delegate_chrome.cc @@ -6,9 +6,11 @@ #include "libcef/browser/views/view_util.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h" +#include "chrome/common/pref_names.h" #include "ui/display/display.h" #include "ui/display/screen.h" #include "ui/gfx/geometry/point.h" @@ -117,6 +119,11 @@ CefEventHandle CefBrowserPlatformDelegateChrome::GetEventHandle( return native_delegate_->GetEventHandle(event); } +bool CefBrowserPlatformDelegateChrome::IsPrintPreviewSupported() const { + return chrome_browser_ && !chrome_browser_->profile()->GetPrefs()->GetBoolean( + prefs::kPrintPreviewDisabled); +} + CefWindowHandle CefBrowserPlatformDelegateChrome::GetParentWindowHandle() const { return GetHostWindowHandle(); diff --git a/libcef/browser/chrome/browser_platform_delegate_chrome.h b/libcef/browser/chrome/browser_platform_delegate_chrome.h index 238f30c52..4f13aced8 100644 --- a/libcef/browser/chrome/browser_platform_delegate_chrome.h +++ b/libcef/browser/chrome/browser_platform_delegate_chrome.h @@ -42,6 +42,7 @@ class CefBrowserPlatformDelegateChrome void ViewText(const std::string& text) override; CefEventHandle GetEventHandle( const content::NativeWebKeyboardEvent& event) const override; + bool IsPrintPreviewSupported() const override; // CefBrowserPlatformDelegateNative::WindowlessHandler methods: CefWindowHandle GetParentWindowHandle() const override; diff --git a/libcef/browser/chrome/chrome_browser_host_impl.cc b/libcef/browser/chrome/chrome_browser_host_impl.cc index 673166237..cb6c362dc 100644 --- a/libcef/browser/chrome/chrome_browser_host_impl.cc +++ b/libcef/browser/chrome/chrome_browser_host_impl.cc @@ -13,7 +13,6 @@ #include "base/logging.h" #include "base/notreached.h" -#include "chrome/browser/printing/print_view_manager_common.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_commands.h" @@ -185,30 +184,6 @@ void ChromeBrowserHostImpl::SetZoomLevel(double zoomLevel) { NOTIMPLEMENTED(); } -void ChromeBrowserHostImpl::Print() { - if (!CEF_CURRENTLY_ON_UIT()) { - CEF_POST_TASK(CEF_UIT, base::BindOnce(&ChromeBrowserHostImpl::Print, this)); - return; - } - - if (browser_) { - // Like chrome::Print() but specifying the WebContents. - printing::StartPrint(GetWebContents(), - /*print_renderer=*/mojo::NullAssociatedRemote(), - browser_->profile()->GetPrefs()->GetBoolean( - prefs::kPrintPreviewDisabled), - /*has_selection=*/false); - } -} - -void ChromeBrowserHostImpl::PrintToPDF( - const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) { - NOTIMPLEMENTED(); - callback->OnPdfPrintFinished(CefString(), false); -} - void ChromeBrowserHostImpl::Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/chrome/chrome_browser_host_impl.h b/libcef/browser/chrome/chrome_browser_host_impl.h index 167cbc7a1..effb5205f 100644 --- a/libcef/browser/chrome/chrome_browser_host_impl.h +++ b/libcef/browser/chrome/chrome_browser_host_impl.h @@ -66,10 +66,6 @@ class ChromeBrowserHostImpl : public CefBrowserHostBase { CefWindowHandle GetOpenerWindowHandle() override; double GetZoomLevel() override; void SetZoomLevel(double zoomLevel) override; - void Print() override; - void PrintToPDF(const CefString& path, - const CefPdfPrintSettings& settings, - CefRefPtr callback) override; void Find(const CefString& searchText, bool forward, bool matchCase, diff --git a/libcef/browser/extensions/extensions_api_client.cc b/libcef/browser/extensions/extensions_api_client.cc index 99f4ffc5f..c4b9a4b67 100644 --- a/libcef/browser/extensions/extensions_api_client.cc +++ b/libcef/browser/extensions/extensions_api_client.cc @@ -11,9 +11,9 @@ #include "libcef/browser/extensions/api/storage/sync_value_store_cache.h" #include "libcef/browser/extensions/extension_web_contents_observer.h" #include "libcef/browser/extensions/mime_handler_view_guest_delegate.h" -#include "libcef/browser/printing/print_view_manager.h" #include "base/memory/ptr_util.h" +#include "chrome/browser/printing/print_view_manager.h" #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h" #include "chrome/browser/ui/prefs/prefs_tab_helper.h" #include "components/pdf/browser/pdf_web_contents_helper.h" @@ -51,7 +51,7 @@ CefExtensionsAPIClient::CreateMimeHandlerViewGuestDelegate( void CefExtensionsAPIClient::AttachWebContentsHelpers( content::WebContents* web_contents) const { PrefsTabHelper::CreateForWebContents(web_contents); - printing::CefPrintViewManager::CreateForWebContents(web_contents); + printing::PrintViewManager::CreateForWebContents(web_contents); // Used by the PDF extension. pdf::PDFWebContentsHelper::CreateForWebContentsWithClient( diff --git a/libcef/browser/printing/print_dialog_linux.cc b/libcef/browser/printing/print_dialog_linux.cc index 0b03daf26..ff535de42 100644 --- a/libcef/browser/printing/print_dialog_linux.cc +++ b/libcef/browser/printing/print_dialog_linux.cc @@ -34,6 +34,10 @@ namespace { CefRefPtr GetBrowserForContext( printing::PrintingContextLinux* context) { + // The print preview dialog won't have a valid child ID. + if (!frame_util::IsValidChildId(context->render_process_id())) + return nullptr; + return extensions::GetOwnerBrowserForGlobalId( frame_util::MakeGlobalId(context->render_process_id(), context->render_frame_id()), @@ -192,27 +196,19 @@ void CefPrintingContextLinuxDelegate::SetDefaultDelegate( default_delegate_ = delegate; } -// static -void CefPrintDialogLinux::OnPrintStart(CefRefPtr browser) { - CEF_REQUIRE_UIT(); - DCHECK(browser); - if (browser && browser->GetClient()) { - if (auto handler = browser->GetClient()->GetPrintHandler()) { - handler->OnPrintStart(browser.get()); - } - } -} - CefPrintDialogLinux::CefPrintDialogLinux(PrintingContextLinux* context, CefRefPtr browser, CefRefPtr handler) : context_(context), browser_(browser), handler_(handler) { + CEF_REQUIRE_UIT(); DCHECK(context_); DCHECK(browser_); DCHECK(handler_); // Paired with the ReleaseDialog() call. AddRef(); + + handler->OnPrintStart(browser_.get()); } CefPrintDialogLinux::~CefPrintDialogLinux() { diff --git a/libcef/browser/printing/print_dialog_linux.h b/libcef/browser/printing/print_dialog_linux.h index da73bb5b2..054bfc218 100644 --- a/libcef/browser/printing/print_dialog_linux.h +++ b/libcef/browser/printing/print_dialog_linux.h @@ -52,9 +52,6 @@ class CefPrintDialogLinux : public printing::PrintDialogLinuxInterface, CefPrintDialogLinux(const CefPrintDialogLinux&) = delete; CefPrintDialogLinux& operator=(const CefPrintDialogLinux&) = delete; - // Notify the client when printing has started. - static void OnPrintStart(CefRefPtr browser); - // PrintDialogLinuxInterface implementation. void UseDefaultSettings() override; void UpdateSettings( diff --git a/libcef/browser/printing/print_util.cc b/libcef/browser/printing/print_util.cc new file mode 100644 index 000000000..9054e982d --- /dev/null +++ b/libcef/browser/printing/print_util.cc @@ -0,0 +1,143 @@ +// Copyright (c) 2022 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that +// can be found in the LICENSE file. + +#include "libcef/browser/printing/print_util.h" + +#include "libcef/browser/thread_util.h" + +#include "base/files/file_util.h" +#include "chrome/browser/printing/print_view_manager.h" +#include "chrome/browser/printing/print_view_manager_common.h" +#include "components/printing/browser/print_to_pdf/pdf_print_utils.h" + +namespace print_util { + +namespace { + +// Write the PDF file to disk. +void SavePdfFile(const CefString& path, + CefRefPtr callback, + scoped_refptr data) { + CEF_REQUIRE_BLOCKING(); + DCHECK_GT(data->size(), 0U); + + const bool ok = + base::WriteFile(path, reinterpret_cast(data->data()), + data->size()) == static_cast(data->size()); + + if (callback) { + CEF_POST_TASK(CEF_UIT, + base::BindOnce(&CefPdfPrintCallback::OnPdfPrintFinished, + callback, path, ok)); + } +} + +void OnPDFCreated(const CefString& path, + CefRefPtr callback, + print_to_pdf::PdfPrintResult print_result, + scoped_refptr data) { + if (print_result != print_to_pdf::PdfPrintResult::kPrintSuccess) { + LOG(ERROR) << "PrintToPDF failed with error: " + << print_to_pdf::PdfPrintResultToString(print_result); + callback->OnPdfPrintFinished(CefString(), false); + return; + } + + // Save the PDF file to disk and then execute the callback. + CEF_POST_USER_VISIBLE_TASK( + base::BindOnce(&SavePdfFile, path, callback, std::move(data))); +} + +} // namespace + +void Print(content::WebContents* web_contents, bool print_preview_disabled) { + // Like chrome::Print() but specifying the WebContents. + printing::StartPrint(web_contents, + /*print_renderer=*/mojo::NullAssociatedRemote(), + print_preview_disabled, + /*has_selection=*/false); +} + +// Implementation based on PageHandler::PrintToPDF. +void PrintToPDF(content::WebContents* web_contents, + const CefString& path, + const CefPdfPrintSettings& settings, + CefRefPtr callback) { + const bool display_header_footer = !!settings.display_header_footer; + + // Defaults to no header/footer. + absl::optional header_template, footer_template; + if (display_header_footer) { + if (settings.header_template.length > 0) + header_template = CefString(&settings.header_template); + if (settings.footer_template.length > 0) + footer_template = CefString(&settings.footer_template); + } + + // Defaults to 1.0. + absl::optional scale; + if (settings.scale > 0) { + scale = settings.scale; + } + + // Defaults to letter size. + absl::optional paper_width, paper_height; + if (settings.paper_width > 0 && settings.paper_height > 0) { + paper_width = settings.paper_width; + paper_height = settings.paper_height; + } + + // Defaults to kDefaultMarginInInches. + absl::optional margin_top, margin_bottom, margin_left, margin_right; + if (settings.margin_type == PDF_PRINT_MARGIN_NONE) { + margin_top = 0; + margin_bottom = 0; + margin_left = 0; + margin_right = 0; + } else if (settings.margin_type == PDF_PRINT_MARGIN_CUSTOM) { + if (settings.margin_top >= 0) + margin_top = settings.margin_top; + if (settings.margin_bottom >= 0) + margin_bottom = settings.margin_bottom; + if (settings.margin_left >= 0) + margin_left = settings.margin_left; + if (settings.margin_right >= 0) + margin_right = settings.margin_right; + } + + absl::variant + print_pages_params = print_to_pdf::GetPrintPagesParams( + web_contents->GetPrimaryMainFrame()->GetLastCommittedURL(), + !!settings.landscape, display_header_footer, + !!settings.print_background, scale, paper_width, paper_height, + margin_top, margin_bottom, margin_left, margin_right, + CefString(&settings.header_template), + CefString(&settings.footer_template), + !!settings.prefer_css_page_size); + + if (absl::holds_alternative(print_pages_params)) { + LOG(ERROR) << "PrintToPDF failed with error: " + << absl::get(print_pages_params); + callback->OnPdfPrintFinished(CefString(), false); + return; + } + + DCHECK(absl::holds_alternative( + print_pages_params)); + + if (auto* print_manager = + printing::PrintViewManager::FromWebContents(web_contents)) { + print_manager->PrintToPdf( + web_contents->GetPrimaryMainFrame(), CefString(&settings.page_ranges), + std::move(absl::get( + print_pages_params)), + base::BindOnce(&OnPDFCreated, path, callback)); + return; + } + + LOG(ERROR) << "PrintToPDF was not handled."; + callback->OnPdfPrintFinished(CefString(), false); +} + +} // namespace print_util diff --git a/libcef/browser/printing/print_util.h b/libcef/browser/printing/print_util.h new file mode 100644 index 000000000..fe12ec545 --- /dev/null +++ b/libcef/browser/printing/print_util.h @@ -0,0 +1,28 @@ +// Copyright (c) 2022 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that +// can be found in the LICENSE file. + +#ifndef CEF_LIBCEF_BROWSER_PRINTING_PRINT_UTIL_H_ +#define CEF_LIBCEF_BROWSER_PRINTING_PRINT_UTIL_H_ +#pragma once + +#include "include/cef_browser.h" + +namespace content { +class WebContents; +} + +namespace print_util { + +// Called from CefBrowserHostBase::Print. +void Print(content::WebContents* web_contents, bool print_preview_disabled); + +// Called from CefBrowserHostBase::PrintToPDF. +void PrintToPDF(content::WebContents* web_contents, + const CefString& path, + const CefPdfPrintSettings& settings, + CefRefPtr callback); + +} // namespace print_util + +#endif // CEF_LIBCEF_BROWSER_PRINTING_PRINT_UTIL_H_ diff --git a/libcef/browser/printing/print_view_manager.cc b/libcef/browser/printing/print_view_manager.cc deleted file mode 100644 index 487d0a100..000000000 --- a/libcef/browser/printing/print_view_manager.cc +++ /dev/null @@ -1,373 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "libcef/browser/printing/print_view_manager.h" - -#include "include/internal/cef_types_wrappers.h" -#include "libcef/browser/browser_info.h" -#include "libcef/browser/browser_info_manager.h" -#include "libcef/browser/download_manager_delegate.h" -#include "libcef/browser/extensions/extension_web_contents_observer.h" -#include "libcef/browser/thread_util.h" - -#include -#include - -#include "base/bind.h" -#include "base/lazy_instance.h" -#include "base/memory/ptr_util.h" -#include "base/memory/ref_counted_memory.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/printing/print_job_manager.h" -#include "chrome/browser/printing/print_preview_dialog_controller.h" -#include "chrome/browser/printing/printer_query.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" -#include "components/printing/common/print.mojom.h" -#include "content/browser/download/download_manager_impl.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/browser_task_traits.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/download_manager.h" -#include "content/public/browser/render_frame_host.h" -#include "content/public/browser/render_process_host.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/web_contents.h" -#include "content/public/browser/web_contents_observer.h" -#include "mojo/public/cpp/bindings/associated_remote.h" -#include "printing/metafile_skia.h" -#include "printing/units.h" -#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" - -#if BUILDFLAG(IS_LINUX) -#include "libcef/browser/printing/print_dialog_linux.h" -#endif - -using content::BrowserThread; - -namespace printing { - -namespace { - -const int PREVIEW_UIID = 12345678; - -// Convert CefPdfPrintSettings into base::Value::Dict. -void FillInDictionaryFromPdfPrintSettings( - const CefPdfPrintSettings& pdf_settings, - int request_id, - base::Value::Dict& print_settings) { - // Fixed settings. - print_settings.Set(kSettingPrinterType, - static_cast(mojom::PrinterType::kPdf)); - print_settings.Set(kSettingColor, static_cast(mojom::ColorModel::kGray)); - print_settings.Set(kSettingDuplexMode, - static_cast(mojom::DuplexMode::kSimplex)); - print_settings.Set(kSettingCopies, 1); - print_settings.Set(kSettingCollate, false); - print_settings.Set(kSettingDeviceName, ""); - print_settings.Set(kSettingRasterizePdf, false); - print_settings.Set(kSettingPreviewModifiable, false); - print_settings.Set(kSettingDpiHorizontal, printing::kDefaultPdfDpi); - print_settings.Set(kSettingDpiVertical, printing::kDefaultPdfDpi); - print_settings.Set(kSettingPagesPerSheet, 1); - - // User defined settings. - print_settings.Set(kSettingLandscape, !!pdf_settings.landscape); - print_settings.Set(kSettingShouldPrintSelectionOnly, - !!pdf_settings.selection_only); - print_settings.Set(kSettingShouldPrintBackgrounds, - !!pdf_settings.backgrounds_enabled); - print_settings.Set(kSettingHeaderFooterEnabled, - !!pdf_settings.header_footer_enabled); - print_settings.Set(kSettingScaleFactor, pdf_settings.scale_factor > 0 - ? pdf_settings.scale_factor - : 100); - - if (pdf_settings.header_footer_enabled) { - print_settings.Set( - kSettingHeaderFooterTitle, - CefString(&pdf_settings.header_footer_title).ToString16()); - print_settings.Set(kSettingHeaderFooterURL, - CefString(&pdf_settings.header_footer_url).ToString16()); - } - - if (pdf_settings.page_width > 0 && pdf_settings.page_height > 0) { - base::Value::Dict dict; - dict.Set(kSettingMediaSizeWidthMicrons, pdf_settings.page_width); - dict.Set(kSettingMediaSizeHeightMicrons, pdf_settings.page_height); - print_settings.Set(kSettingMediaSize, std::move(dict)); - } - - auto margin_type = printing::mojom::MarginType::kDefaultMargins; - switch (pdf_settings.margin_type) { - case PDF_PRINT_MARGIN_NONE: - margin_type = printing::mojom::MarginType::kNoMargins; - break; - case PDF_PRINT_MARGIN_MINIMUM: - margin_type = printing::mojom::MarginType::kPrintableAreaMargins; - break; - case PDF_PRINT_MARGIN_CUSTOM: - margin_type = printing::mojom::MarginType::kCustomMargins; - break; - default: - break; - } - - print_settings.Set(kSettingMarginsType, static_cast(margin_type)); - if (margin_type == printing::mojom::MarginType::kCustomMargins) { - base::Value::Dict dict; - dict.Set(kSettingMarginTop, pdf_settings.margin_top); - dict.Set(kSettingMarginRight, pdf_settings.margin_right); - dict.Set(kSettingMarginBottom, pdf_settings.margin_bottom); - dict.Set(kSettingMarginLeft, pdf_settings.margin_left); - print_settings.Set(kSettingMarginsCustom, std::move(dict)); - } - - // Service settings. - print_settings.Set(kPreviewUIID, PREVIEW_UIID); - print_settings.Set(kPreviewRequestID, request_id); - print_settings.Set(kIsFirstRequest, request_id != 0); -} - -void StopWorker(int document_cookie) { - if (document_cookie <= 0) - return; - scoped_refptr queue = - g_browser_process->print_job_manager()->queue(); - std::unique_ptr printer_query = - queue->PopPrinterQuery(document_cookie); - if (printer_query.get()) { - printer_query->StopWorker(); - } -} - -// Write the PDF file to disk. -void SavePdfFile(scoped_refptr data, - const base::FilePath& path, - CefPrintViewManager::PdfPrintCallback callback) { - CEF_REQUIRE_BLOCKING(); - DCHECK_GT(data->size(), 0U); - - MetafileSkia metafile; - metafile.InitFromData(*data); - - base::File file(path, - base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); - bool ok = file.IsValid() && metafile.SaveTo(&file); - - if (!callback.is_null()) { - content::GetUIThreadTaskRunner({})->PostTask( - FROM_HERE, base::BindOnce(std::move(callback), ok)); - } -} - -} // namespace - -struct CefPrintViewManager::PdfPrintState { - content::RenderFrameHost* printing_rfh_ = nullptr; - base::FilePath output_path_; - base::Value::Dict settings_; - PdfPrintCallback callback_; -}; - -CefPrintViewManager::CefPrintViewManager(content::WebContents* web_contents) - : PrintViewManager(web_contents) {} - -CefPrintViewManager::~CefPrintViewManager() { - TerminatePdfPrintJob(); -} - -// static -void CefPrintViewManager::BindPrintManagerHost( - mojo::PendingAssociatedReceiver receiver, - content::RenderFrameHost* rfh) { - auto* web_contents = content::WebContents::FromRenderFrameHost(rfh); - if (!web_contents) - return; - auto* print_manager = CefPrintViewManager::FromWebContents(web_contents); - if (!print_manager) - return; - print_manager->BindReceiver(std::move(receiver), rfh); -} - -bool CefPrintViewManager::PrintToPDF(content::RenderFrameHost* rfh, - const base::FilePath& path, - const CefPdfPrintSettings& settings, - PdfPrintCallback callback) { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - - // Don't start print again while printing is currently in progress. - if (pdf_print_state_) - return false; - - // Don't print crashed tabs. - if (!web_contents() || web_contents()->IsCrashed() || - !rfh->IsRenderFrameLive()) { - return false; - } - - pdf_print_state_.reset(new PdfPrintState); - pdf_print_state_->printing_rfh_ = rfh; - pdf_print_state_->output_path_ = path; - pdf_print_state_->callback_ = std::move(callback); - - FillInDictionaryFromPdfPrintSettings(settings, ++next_pdf_request_id_, - pdf_print_state_->settings_); - - auto& print_render_frame = GetPrintRenderFrame(rfh); - if (!pdf_print_receiver_.is_bound()) { - print_render_frame->SetPrintPreviewUI( - pdf_print_receiver_.BindNewEndpointAndPassRemote()); - } - - print_render_frame->InitiatePrintPreview({}, !!settings.selection_only); - - return true; -} - -void CefPrintViewManager::GetDefaultPrintSettings( - GetDefaultPrintSettingsCallback callback) { -#if BUILDFLAG(IS_LINUX) - // Send notification to the client. - auto browser = CefBrowserHostBase::GetBrowserForContents(web_contents()); - if (browser) { - CefPrintDialogLinux::OnPrintStart(browser); - } -#endif - PrintViewManager::GetDefaultPrintSettings(std::move(callback)); -} - -void CefPrintViewManager::DidShowPrintDialog() { - if (pdf_print_state_) - return; - PrintViewManager::DidShowPrintDialog(); -} - -void CefPrintViewManager::RequestPrintPreview( - mojom::RequestPrintPreviewParamsPtr params) { - if (!pdf_print_state_) { - PrintViewManager::RequestPrintPreview(std::move(params)); - return; - } - - GetPrintRenderFrame(pdf_print_state_->printing_rfh_) - ->PrintPreview(pdf_print_state_->settings_.Clone()); -} - -void CefPrintViewManager::CheckForCancel(int32_t preview_ui_id, - int32_t request_id, - CheckForCancelCallback callback) { - if (!pdf_print_state_) { - return PrintViewManager::CheckForCancel(preview_ui_id, request_id, - std::move(callback)); - } - - std::move(callback).Run(/*cancel=*/false); -} - -void CefPrintViewManager::MetafileReadyForPrinting( - mojom::DidPreviewDocumentParamsPtr params, - int32_t request_id) { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - StopWorker(params->document_cookie); - - if (!pdf_print_state_) - return; - - GetPrintRenderFrame(pdf_print_state_->printing_rfh_) - ->OnPrintPreviewDialogClosed(); - - auto shared_buf = base::RefCountedSharedMemoryMapping::CreateFromWholeRegion( - params->content->metafile_data_region); - if (!shared_buf) { - TerminatePdfPrintJob(); - return; - } - - const base::FilePath output_path = pdf_print_state_->output_path_; - PdfPrintCallback print_callback = std::move(pdf_print_state_->callback_); - - // Reset state information. - pdf_print_state_.reset(); - pdf_print_receiver_.reset(); - - // Save the PDF file to disk and then execute the callback. - CEF_POST_USER_VISIBLE_TASK(base::BindOnce( - &SavePdfFile, shared_buf, output_path, std::move(print_callback))); -} - -void CefPrintViewManager::PrintPreviewFailed(int32_t document_cookie, - int32_t request_id) { - TerminatePdfPrintJob(); -} - -void CefPrintViewManager::PrintPreviewCancelled(int32_t document_cookie, - int32_t request_id) { - // Should never be canceled by CheckForCancel(). - NOTREACHED(); -} - -void CefPrintViewManager::RenderFrameDeleted( - content::RenderFrameHost* render_frame_host) { - if (pdf_print_state_ && - render_frame_host == pdf_print_state_->printing_rfh_) { - TerminatePdfPrintJob(); - } - PrintViewManager::RenderFrameDeleted(render_frame_host); -} - -void CefPrintViewManager::NavigationStopped() { - TerminatePdfPrintJob(); - PrintViewManager::NavigationStopped(); -} - -void CefPrintViewManager::PrimaryMainFrameRenderProcessGone( - base::TerminationStatus status) { - TerminatePdfPrintJob(); - PrintViewManager::PrimaryMainFrameRenderProcessGone(status); -} - -// static -void CefPrintViewManager::CreateForWebContents(content::WebContents* contents) { - DCHECK(contents); - if (!FromWebContents(contents)) { - contents->SetUserData(PrintViewManager::UserDataKey(), - base::WrapUnique(new CefPrintViewManager(contents))); - } -} - -// static -CefPrintViewManager* CefPrintViewManager::FromWebContents( - content::WebContents* contents) { - DCHECK(contents); - return static_cast( - contents->GetUserData(PrintViewManager::UserDataKey())); -} - -// static -const CefPrintViewManager* CefPrintViewManager::FromWebContents( - const content::WebContents* contents) { - DCHECK(contents); - return static_cast( - contents->GetUserData(PrintViewManager::UserDataKey())); -} - -void CefPrintViewManager::TerminatePdfPrintJob() { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - if (!pdf_print_state_) - return; - - if (!pdf_print_state_->callback_.is_null()) { - // Execute the callback. - content::GetUIThreadTaskRunner({})->PostTask( - FROM_HERE, - base::BindOnce(std::move(pdf_print_state_->callback_), false)); - } - - // Reset state information. - pdf_print_state_.reset(); - pdf_print_receiver_.reset(); -} - -} // namespace printing diff --git a/libcef/browser/printing/print_view_manager.h b/libcef/browser/printing/print_view_manager.h deleted file mode 100644 index c49b392cd..000000000 --- a/libcef/browser/printing/print_view_manager.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ -#define CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ - -#include "include/internal/cef_types_wrappers.h" - -#include "base/callback_forward.h" -#include "chrome/browser/printing/print_view_manager.h" -#include "components/printing/common/print.mojom-forward.h" -#include "content/public/browser/web_contents_observer.h" -#include "content/public/browser/web_contents_user_data.h" -#include "printing/mojom/print.mojom.h" - -namespace content { -class RenderFrameHost; -class RenderProcessHost; -class WebContentsObserver; -} // namespace content - -class CefBrowserInfo; - -namespace printing { - -// CEF handler for print commands. -class CefPrintViewManager : public PrintViewManager, - public mojom::PrintPreviewUI { - public: - CefPrintViewManager(const CefPrintViewManager&) = delete; - CefPrintViewManager& operator=(const CefPrintViewManager&) = delete; - - ~CefPrintViewManager() override; - - static void BindPrintManagerHost( - mojo::PendingAssociatedReceiver receiver, - content::RenderFrameHost* rfh); - - // Callback executed on PDF printing completion. - using PdfPrintCallback = base::OnceCallback; - - // Print the current document to a PDF file. Execute |callback| on completion. - bool PrintToPDF(content::RenderFrameHost* rfh, - const base::FilePath& path, - const CefPdfPrintSettings& settings, - PdfPrintCallback callback); - - // mojom::PrintManagerHost methods: - void GetDefaultPrintSettings( - GetDefaultPrintSettingsCallback callback) override; - void DidShowPrintDialog() override; - void RequestPrintPreview(mojom::RequestPrintPreviewParamsPtr params) override; - void CheckForCancel(int32_t preview_ui_id, - int32_t request_id, - CheckForCancelCallback callback) override; - - // printing::mojo::PrintPreviewUI methods: - void SetOptionsFromDocument(const mojom::OptionsFromDocumentParamsPtr params, - int32_t request_id) override {} - void DidPrepareDocumentForPreview(int32_t document_cookie, - int32_t request_id) override {} - void DidPreviewPage(mojom::DidPreviewPageParamsPtr params, - int32_t request_id) override {} - void MetafileReadyForPrinting(mojom::DidPreviewDocumentParamsPtr params, - int32_t request_id) override; - void PrintPreviewFailed(int32_t document_cookie, int32_t request_id) override; - void PrintPreviewCancelled(int32_t document_cookie, - int32_t request_id) override; - void PrinterSettingsInvalid(int32_t document_cookie, - int32_t request_id) override {} - void DidGetDefaultPageLayout(mojom::PageSizeMarginsPtr page_layout_in_points, - const gfx::Rect& printable_area_in_points, - bool has_custom_page_size_style, - int32_t request_id) override {} - void DidStartPreview(mojom::DidStartPreviewParamsPtr params, - int32_t request_id) override {} - - // content::WebContentsObserver methods: - void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; - void NavigationStopped() override; - void PrimaryMainFrameRenderProcessGone( - base::TerminationStatus status) override; - - // Inline versions of the content::WebContentsUserData methods to avoid - // ambiguous warnings due to the PrintViewManager base class also extending - // WebContentsUserData. - static void CreateForWebContents(content::WebContents* contents); - static CefPrintViewManager* FromWebContents(content::WebContents* contents); - static const CefPrintViewManager* FromWebContents( - const content::WebContents* contents); - - private: - explicit CefPrintViewManager(content::WebContents* web_contents); - - void TerminatePdfPrintJob(); - - // Used for printing to PDF. Only accessed on the browser process UI thread. - int next_pdf_request_id_ = content::RenderFrameHost::kNoFrameTreeNodeId; - struct PdfPrintState; - std::unique_ptr pdf_print_state_; - mojo::AssociatedReceiver pdf_print_receiver_{this}; -}; - -} // namespace printing - -#endif // CEF_LIBCEF_BROWSER_PRINTING_PRINT_VIEW_MANAGER_H_ \ No newline at end of file diff --git a/patch/patch.cfg b/patch/patch.cfg index c97822fa5..86a3847de 100644 --- a/patch/patch.cfg +++ b/patch/patch.cfg @@ -370,11 +370,6 @@ patches = [ # https://bitbucket.org/chromiumembedded/cef/issues/2196 'name': 'printing_context_2196', }, - { - # Expose the printing::GetRenderFrameHostToUse() method. - # https://bitbucket.org/chromiumembedded/cef/issues/3057 - 'name': 'printing_pdf_3047', - }, { # Windows: Remove llvmlibthin as the combine_libs.py can't handle those. # https://bitbucket.org/chromiumembedded/cef/issues/2470 diff --git a/patch/patches/print_preview_123.patch b/patch/patches/print_preview_123.patch index e68c3937d..0306fa19c 100644 --- a/patch/patches/print_preview_123.patch +++ b/patch/patches/print_preview_123.patch @@ -65,20 +65,6 @@ index cbb6c5925c2ae..f4faa42ca0b04 100644 } auto* printer_query_ptr = printer_query.get(); printer_query_ptr->SetSettings( -diff --git chrome/browser/printing/print_view_manager_base.h chrome/browser/printing/print_view_manager_base.h -index ffbdbe363d956..9c3a5b9a9814f 100644 ---- chrome/browser/printing/print_view_manager_base.h -+++ chrome/browser/printing/print_view_manager_base.h -@@ -204,9 +204,6 @@ class PrintViewManagerBase : public PrintManager, public PrintJob::Observer { - // Manages the low-level talk to the printer. - scoped_refptr print_job_; - -- private: -- friend class TestPrintViewManager; -- - // content::WebContentsObserver implementation. - void RenderFrameHostStateChanged( - content::RenderFrameHost* render_frame_host, diff --git chrome/browser/resources/print_preview/ui/destination_dialog.html chrome/browser/resources/print_preview/ui/destination_dialog.html index 5d1658999d5bb..d1b7b7288c946 100644 --- chrome/browser/resources/print_preview/ui/destination_dialog.html diff --git a/patch/patches/printing_pdf_3047.patch b/patch/patches/printing_pdf_3047.patch deleted file mode 100644 index 18c723a3d..000000000 --- a/patch/patches/printing_pdf_3047.patch +++ /dev/null @@ -1,37 +0,0 @@ -diff --git chrome/browser/printing/print_view_manager_common.cc chrome/browser/printing/print_view_manager_common.cc -index 06ebb78a547e1..8f470db9167f4 100644 ---- chrome/browser/printing/print_view_manager_common.cc -+++ chrome/browser/printing/print_view_manager_common.cc -@@ -44,6 +44,8 @@ bool StoreFullPagePlugin(content::WebContents** result, - } - #endif // BUILDFLAG(ENABLE_EXTENSIONS) - -+} // namespace -+ - // Pick the right RenderFrameHost based on the WebContents. - content::RenderFrameHost* GetRenderFrameHostToUse( - content::WebContents* contents) { -@@ -57,8 +59,6 @@ content::RenderFrameHost* GetRenderFrameHostToUse( - return GetFrameToPrint(contents); - } - --} // namespace -- - void StartPrint( - content::WebContents* contents, - mojo::PendingAssociatedRemote print_renderer, -diff --git chrome/browser/printing/print_view_manager_common.h chrome/browser/printing/print_view_manager_common.h -index c8ba345acce57..158baf9a5eda6 100644 ---- chrome/browser/printing/print_view_manager_common.h -+++ chrome/browser/printing/print_view_manager_common.h -@@ -36,6 +36,10 @@ content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents); - // guest's WebContents instead. - content::WebContents* GetWebContentsToUse(content::WebContents* contents); - -+// Pick the right RenderFrameHost based on the WebContents. -+content::RenderFrameHost* GetRenderFrameHostToUse( -+ content::WebContents* contents); -+ - } // namespace printing - - #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_COMMON_H_ diff --git a/tests/cefclient/browser/test_runner.cc b/tests/cefclient/browser/test_runner.cc index 1693ede2c..5833976ef 100644 --- a/tests/cefclient/browser/test_runner.cc +++ b/tests/cefclient/browser/test_runner.cc @@ -358,10 +358,21 @@ void PrintToPDF(CefRefPtr browser) { if (!file_paths.empty()) { CefPdfPrintSettings settings; - // Show the URL in the footer. - settings.header_footer_enabled = true; - CefString(&settings.header_footer_url) = - browser_->GetMainFrame()->GetURL(); + // Display a header and footer. + settings.display_header_footer = true; + CefString(&settings.header_template) = + "
" + "
" + "
" + "
"; + CefString(&settings.footer_template) = + "
" + "
" + "
" + "
" + "/" + "
" + "
"; // Print to the selected PDF file. browser_->GetHost()->PrintToPDF(file_paths[0], settings, this);