diff --git a/include/capi/cef_print_handler_capi.h b/include/capi/cef_print_handler_capi.h index fbcb71281..162210600 100644 --- a/include/capi/cef_print_handler_capi.h +++ b/include/capi/cef_print_handler_capi.h @@ -95,6 +95,15 @@ typedef struct _cef_print_handler_t { /// cef_base_t base; + /// + // Called when printing has started for the specified |browser|. This function + // will be called before the other OnPrint*() functions and irrespective of + // how printing was initiated (e.g. cef_browser_host_t::print(), JavaScript + // window.print() or PDF extension print button). + /// + void (CEF_CALLBACK *on_print_start)(struct _cef_print_handler_t* self, + struct _cef_browser_t* browser); + /// // Synchronize |settings| with client state. If |get_defaults| is true (1) // then populate |settings| with the default print settings. Do not keep a diff --git a/include/cef_print_handler.h b/include/cef_print_handler.h index 57ae3781d..cac16244b 100644 --- a/include/cef_print_handler.h +++ b/include/cef_print_handler.h @@ -82,6 +82,15 @@ class CefPrintJobCallback : public virtual CefBase { /*--cef(source=client)--*/ class CefPrintHandler : public virtual CefBase { public: + /// + // Called when printing has started for the specified |browser|. This method + // will be called before the other OnPrint*() methods and irrespective of how + // printing was initiated (e.g. CefBrowserHost::Print(), JavaScript + // window.print() or PDF extension print button). + /// + /*--cef()--*/ + virtual void OnPrintStart(CefRefPtr browser) =0; + /// // Synchronize |settings| with client state. If |get_defaults| is true then // populate |settings| with the default print settings. Do not keep a diff --git a/libcef/browser/content_browser_client.cc b/libcef/browser/content_browser_client.cc index c2377b2fb..398048ca5 100644 --- a/libcef/browser/content_browser_client.cc +++ b/libcef/browser/content_browser_client.cc @@ -985,15 +985,7 @@ void CefContentBrowserClient::OverrideWebkitPrefs( base::CommandLine::ForCurrentProcess(); CefRefPtr browser = - CefBrowserHostImpl::GetBrowserForHost(rvh); - if (!browser.get() && extensions::ExtensionsEnabled()) { - // Retrieve the owner browser, if any. - content::WebContents* owner = extensions::GetOwnerForGuestContents( - content::WebContents::FromRenderViewHost(rvh)); - if (owner) - browser = CefBrowserHostImpl::GetBrowserForContents(owner); - } - + extensions::GetOwnerBrowserForHost(rvh); if (browser.get()) { // Populate WebPreferences based on CefBrowserSettings. BrowserToWebSettings(browser->settings(), *prefs); diff --git a/libcef/browser/extensions/browser_extensions_util.cc b/libcef/browser/extensions/browser_extensions_util.cc index 7c57465fb..ebd02cc79 100644 --- a/libcef/browser/extensions/browser_extensions_util.cc +++ b/libcef/browser/extensions/browser_extensions_util.cc @@ -4,6 +4,8 @@ #include "libcef/browser/extensions/browser_extensions_util.h" +#include "libcef/common/extensions/extensions_util.h" + #include "content/browser/browser_plugin/browser_plugin_embedder.h" #include "content/browser/browser_plugin/browser_plugin_guest.h" #include "content/browser/web_contents/web_contents_impl.h" @@ -55,4 +57,27 @@ content::WebContents* GetOwnerForGuestContents(content::WebContents* guest) { return NULL; } +CefRefPtr GetOwnerBrowserForView(int render_process_id, + int render_routing_id) { + content::RenderViewHost* host = + content::RenderViewHost::FromID(render_process_id, render_routing_id); + if (host) + return GetOwnerBrowserForHost(host); + return NULL; +} + +CefRefPtr GetOwnerBrowserForHost( + content::RenderViewHost* host) { + CefRefPtr browser = + CefBrowserHostImpl::GetBrowserForHost(host); + if (!browser.get() && ExtensionsEnabled()) { + // Retrieve the owner browser, if any. + content::WebContents* owner = GetOwnerForGuestContents( + content::WebContents::FromRenderViewHost(host)); + if (owner) + browser = CefBrowserHostImpl::GetBrowserForContents(owner); + } + return browser; +} + } // namespace extensions diff --git a/libcef/browser/extensions/browser_extensions_util.h b/libcef/browser/extensions/browser_extensions_util.h index c6c894cd1..a2d14d980 100644 --- a/libcef/browser/extensions/browser_extensions_util.h +++ b/libcef/browser/extensions/browser_extensions_util.h @@ -7,7 +7,10 @@ #include +#include "libcef/browser/browser_host_impl.h" + namespace content { +class RenderViewHost; class WebContents; } @@ -24,6 +27,15 @@ void GetAllGuestsForOwnerContents(content::WebContents* owner, // Returns the WebContents that owns the specified |guest|, if any. content::WebContents* GetOwnerForGuestContents(content::WebContents* guest); +// Returns the CefBrowserHostImpl that owns the host identified by the specified +// view routing IDs, if any. +CefRefPtr GetOwnerBrowserForView(int render_process_id, + int render_routing_id); + +// Returns the CefBrowserHostImpl that owns the specified |host|, if any. +CefRefPtr GetOwnerBrowserForHost( + content::RenderViewHost* host); + } // namespace extensions #endif // CEF_LIBCEF_BROWSER_EXTENSIONS_BROWSER_EXTENSIONS_UTIL_H_ diff --git a/libcef/browser/printing/print_dialog_linux.cc b/libcef/browser/printing/print_dialog_linux.cc index 478df1d97..e3b512239 100644 --- a/libcef/browser/printing/print_dialog_linux.cc +++ b/libcef/browser/printing/print_dialog_linux.cc @@ -8,6 +8,8 @@ #include #include +#include "libcef/browser/browser_host_impl.h" +#include "libcef/browser/extensions/browser_extensions_util.h" #include "libcef/browser/print_settings_impl.h" #include "libcef/browser/thread_util.h" #include "libcef/common/content_client.h" @@ -134,6 +136,35 @@ gfx::Size CefPrintDialogLinux::GetPdfPaperSize( return size; } +// static +void CefPrintDialogLinux::OnPrintStart(int render_process_id, + int render_routing_id) { + if (!CEF_CURRENTLY_ON(CEF_UIT)) { + CEF_POST_TASK(CEF_UIT, + base::Bind(&CefPrintDialogLinux::OnPrintStart, + render_process_id, render_routing_id)); + return; + } + + CefRefPtr app = CefContentClient::Get()->application(); + if (!app.get()) + return; + + CefRefPtr browser_handler = + app->GetBrowserProcessHandler(); + if (!browser_handler.get()) + return; + + CefRefPtr handler = browser_handler->GetPrintHandler(); + if (!handler.get()) + return; + + CefRefPtr browser = + extensions::GetOwnerBrowserForView(render_process_id, render_routing_id); + if (browser.get()) + handler->OnPrintStart(browser.get()); +} + CefPrintDialogLinux::CefPrintDialogLinux(PrintingContextLinux* context) : context_(context) { } diff --git a/libcef/browser/printing/print_dialog_linux.h b/libcef/browser/printing/print_dialog_linux.h index f6c33aa6c..7f2a8eda9 100644 --- a/libcef/browser/printing/print_dialog_linux.h +++ b/libcef/browser/printing/print_dialog_linux.h @@ -37,6 +37,10 @@ class CefPrintDialogLinux static gfx::Size GetPdfPaperSize( printing::PrintingContextLinux* context); + // Notify the client when printing has started. + static void OnPrintStart(int render_process_id, + int render_routing_id); + // printing::CefPrintDialogLinuxInterface implementation. void UseDefaultSettings() override; bool UpdateSettings(printing::PrintSettings* settings) override; diff --git a/libcef/browser/printing/printing_message_filter.cc b/libcef/browser/printing/printing_message_filter.cc index 677d669fb..f245c4cf3 100644 --- a/libcef/browser/printing/printing_message_filter.cc +++ b/libcef/browser/printing/printing_message_filter.cc @@ -17,6 +17,10 @@ #include "content/public/browser/web_contents.h" #include "content/public/common/child_process_host.h" +#if defined(OS_LINUX) +#include "libcef/browser/printing/print_dialog_linux.h" +#endif + using content::BrowserThread; namespace printing { @@ -72,6 +76,12 @@ void PrintingMessageFilter::OnIsPrintingEnabled(bool* is_enabled) { void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { DCHECK_CURRENTLY_ON(BrowserThread::IO); +#if defined(OS_LINUX) + // Send notification to the client. + CefPrintDialogLinux::OnPrintStart(render_process_id_, + reply_msg->routing_id()); +#endif + scoped_refptr printer_query; printer_query = queue_->PopPrinterQuery(0); if (!printer_query.get()) { diff --git a/libcef_dll/cpptoc/print_handler_cpptoc.cc b/libcef_dll/cpptoc/print_handler_cpptoc.cc index edeba8ceb..b01358754 100644 --- a/libcef_dll/cpptoc/print_handler_cpptoc.cc +++ b/libcef_dll/cpptoc/print_handler_cpptoc.cc @@ -11,6 +11,7 @@ // #include "libcef_dll/cpptoc/print_handler_cpptoc.h" +#include "libcef_dll/ctocpp/browser_ctocpp.h" #include "libcef_dll/ctocpp/print_dialog_callback_ctocpp.h" #include "libcef_dll/ctocpp/print_job_callback_ctocpp.h" #include "libcef_dll/ctocpp/print_settings_ctocpp.h" @@ -20,6 +21,23 @@ namespace { // MEMBER FUNCTIONS - Body may be edited by hand. +void CEF_CALLBACK print_handler_on_print_start( + struct _cef_print_handler_t* self, cef_browser_t* browser) { + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + DCHECK(self); + if (!self) + return; + // Verify param: browser; type: refptr_diff + DCHECK(browser); + if (!browser) + return; + + // Execute + CefPrintHandlerCppToC::Get(self)->OnPrintStart( + CefBrowserCToCpp::Wrap(browser)); +} + void CEF_CALLBACK print_handler_on_print_settings( struct _cef_print_handler_t* self, struct _cef_print_settings_t* settings, int get_defaults) { @@ -126,6 +144,7 @@ cef_size_t CEF_CALLBACK print_handler_get_pdf_paper_size( // CONSTRUCTOR - Do not edit by hand. CefPrintHandlerCppToC::CefPrintHandlerCppToC() { + GetStruct()->on_print_start = print_handler_on_print_start; GetStruct()->on_print_settings = print_handler_on_print_settings; GetStruct()->on_print_dialog = print_handler_on_print_dialog; GetStruct()->on_print_job = print_handler_on_print_job; diff --git a/libcef_dll/ctocpp/print_handler_ctocpp.cc b/libcef_dll/ctocpp/print_handler_ctocpp.cc index 457d690e8..46fbfe651 100644 --- a/libcef_dll/ctocpp/print_handler_ctocpp.cc +++ b/libcef_dll/ctocpp/print_handler_ctocpp.cc @@ -10,6 +10,7 @@ // for more information. // +#include "libcef_dll/cpptoc/browser_cpptoc.h" #include "libcef_dll/cpptoc/print_dialog_callback_cpptoc.h" #include "libcef_dll/cpptoc/print_job_callback_cpptoc.h" #include "libcef_dll/cpptoc/print_settings_cpptoc.h" @@ -18,6 +19,23 @@ // VIRTUAL METHODS - Body may be edited by hand. +void CefPrintHandlerCToCpp::OnPrintStart(CefRefPtr browser) { + cef_print_handler_t* _struct = GetStruct(); + if (CEF_MEMBER_MISSING(_struct, on_print_start)) + return; + + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + // Verify param: browser; type: refptr_diff + DCHECK(browser.get()); + if (!browser.get()) + return; + + // Execute + _struct->on_print_start(_struct, + CefBrowserCppToC::Wrap(browser)); +} + void CefPrintHandlerCToCpp::OnPrintSettings( CefRefPtr settings, bool get_defaults) { cef_print_handler_t* _struct = GetStruct(); diff --git a/libcef_dll/ctocpp/print_handler_ctocpp.h b/libcef_dll/ctocpp/print_handler_ctocpp.h index bac7c2340..89252dd0b 100644 --- a/libcef_dll/ctocpp/print_handler_ctocpp.h +++ b/libcef_dll/ctocpp/print_handler_ctocpp.h @@ -31,6 +31,7 @@ class CefPrintHandlerCToCpp CefPrintHandlerCToCpp(); // CefPrintHandler methods. + void OnPrintStart(CefRefPtr browser) override; void OnPrintSettings(CefRefPtr settings, bool get_defaults) override; bool OnPrintDialog(bool has_selection, diff --git a/tests/cefclient/browser/print_handler_gtk.cc b/tests/cefclient/browser/print_handler_gtk.cc index dc1b389f8..34770308d 100644 --- a/tests/cefclient/browser/print_handler_gtk.cc +++ b/tests/cefclient/browser/print_handler_gtk.cc @@ -282,6 +282,9 @@ ClientPrintHandlerGtk::ClientPrintHandlerGtk() printer_(NULL) { } +void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr browser) { +} + void ClientPrintHandlerGtk::OnPrintSettings( CefRefPtr settings, bool get_defaults) { diff --git a/tests/cefclient/browser/print_handler_gtk.h b/tests/cefclient/browser/print_handler_gtk.h index 76bae4b7b..11618e67d 100644 --- a/tests/cefclient/browser/print_handler_gtk.h +++ b/tests/cefclient/browser/print_handler_gtk.h @@ -19,6 +19,7 @@ class ClientPrintHandlerGtk : public CefPrintHandler { ClientPrintHandlerGtk(); // CefPrintHandler methods. + void OnPrintStart(CefRefPtr browser) OVERRIDE; void OnPrintSettings(CefRefPtr settings, bool get_defaults) OVERRIDE; bool OnPrintDialog(