From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Heilig Benedek Date: Thu, 18 Oct 2018 17:08:18 -0700 Subject: printing.patch Add changeset that was previously applied to sources in chromium_src. The majority of changes originally come from these PRs: * https://github.com/electron/electron/pull/1835 * https://github.com/electron/electron/pull/8596 diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc index 1065e808e621c087bde9320abe019f05292f977c..13715870097b4f47ae164c12deb2bf783049ca6a 100644 --- a/chrome/browser/printing/print_job_worker.cc +++ b/chrome/browser/printing/print_job_worker.cc @@ -20,12 +20,13 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/printing/print_job.h" -#include "chrome/grit/generated_resources.h" +#include "electron/grit/electron_resources.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" #include "printing/print_job_constants.h" +#include "printing/print_settings_conversion.h" #include "printing/printed_document.h" #include "printing/printing_utils.h" #include "ui/base/l10n/l10n_util.h" @@ -174,7 +175,8 @@ void PrintJobWorker::GetSettings(bool ask_user_for_settings, bool has_selection, MarginType margin_type, bool is_scripted, - bool is_modifiable) { + bool is_modifiable, + const base::string16& device_name) { DCHECK(task_runner_->RunsTasksInCurrentSequence()); DCHECK_EQ(page_number_, PageNumber::npos()); @@ -200,6 +202,12 @@ void PrintJobWorker::GetSettings(bool ask_user_for_settings, base::BindOnce(&PrintJobWorker::GetSettingsWithUI, base::Unretained(this), document_page_count, has_selection, is_scripted))); + } else if (!device_name.empty()) { + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + base::BindOnce(&WorkerHoldRefCallback, base::WrapRefCounted(query_), + base::BindOnce(&PrintJobWorker::InitWithDeviceName, + base::Unretained(this), device_name))); } else { BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -316,6 +324,14 @@ void PrintJobWorker::UseDefaultSettings() { GetSettingsDone(result); } +void PrintJobWorker::InitWithDeviceName(const base::string16& device_name) { + const auto& settings = printing_context_->settings(); + std::unique_ptr dic(new base::DictionaryValue); + printing::PrintSettingsToJobSettings(settings, dic.get()); + dic->SetString(kSettingDeviceName, device_name); + UpdatePrintSettings(std::move(dic)); +} + void PrintJobWorker::StartPrinting(PrintedDocument* new_document) { DCHECK(task_runner_->RunsTasksInCurrentSequence()); diff --git a/chrome/browser/printing/print_job_worker.h b/chrome/browser/printing/print_job_worker.h index 182f7563e0c7d6486a5d2843d86f66524c02cb10..eed2230627d6b35486081f628a2ee97895bf16cf 100644 --- a/chrome/browser/printing/print_job_worker.h +++ b/chrome/browser/printing/print_job_worker.h @@ -54,7 +54,8 @@ class PrintJobWorker { bool has_selection, MarginType margin_type, bool is_scripted, - bool is_modifiable); + bool is_modifiable, + const base::string16& device_name); // Set the new print settings from a dictionary value. void SetSettings(std::unique_ptr new_settings); @@ -155,6 +156,9 @@ class PrintJobWorker { // systems. void UseDefaultSettings(); + // set the printer name + void InitWithDeviceName(const base::string16& device_name); + // Printing context delegate. const std::unique_ptr printing_context_delegate_; diff --git a/chrome/browser/printing/print_preview_message_handler.cc b/chrome/browser/printing/print_preview_message_handler.cc index e1613a9b52ad65afc8290498e8d9877ec58a0a10..db62078f2c084bb20681f7ef5ccccbceafd4f0b1 100644 --- a/chrome/browser/printing/print_preview_message_handler.cc +++ b/chrome/browser/printing/print_preview_message_handler.cc @@ -57,7 +57,7 @@ void StopWorker(int document_cookie) { base::BindOnce(&PrinterQuery::StopWorker, printer_query)); } } - +#if 0 scoped_refptr GetDataFromHandle( base::SharedMemoryHandle handle, uint32_t data_size) { @@ -70,6 +70,7 @@ scoped_refptr GetDataFromHandle( return base::MakeRefCounted( std::move(shared_buf), data_size); } +#endif } // namespace @@ -81,7 +82,7 @@ PrintPreviewMessageHandler::PrintPreviewMessageHandler( PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { } - +#if 0 WebContents* PrintPreviewMessageHandler::GetPrintPreviewDialog() { PrintPreviewDialogController* dialog_controller = PrintPreviewDialogController::GetInstance(); @@ -159,6 +160,7 @@ void PrintPreviewMessageHandler::OnDidPreviewPage( GetDataFromHandle(content.metafile_data_handle, content.data_size)); } } +#endif void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( content::RenderFrameHost* render_frame_host, @@ -171,7 +173,8 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( NOTREACHED(); return; } - +} +#if 0 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(ids.ui_id); if (!print_preview_ui) return; @@ -193,12 +196,13 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( GetDataFromHandle(content.metafile_data_handle, content.data_size)); } } - +#endif void PrintPreviewMessageHandler::OnPrintPreviewFailed( int document_cookie, const PrintHostMsg_PreviewIds& ids) { StopWorker(document_cookie); - +} +#if 0 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(ids.ui_id); if (!print_preview_ui) return; @@ -318,15 +322,19 @@ void PrintPreviewMessageHandler::OnCompositePdfDocumentDone( base::RefCountedSharedMemoryMapping::CreateFromWholeRegion(region)); } +#endif + bool PrintPreviewMessageHandler::OnMessageReceived( const IPC::Message& message, content::RenderFrameHost* render_frame_host) { bool handled = true; IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message, render_frame_host) +#if 0 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, OnRequestPrintPreview) IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) +#endif IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, OnMetafileReadyForPrinting) IPC_MESSAGE_UNHANDLED(handled = false) @@ -336,10 +344,13 @@ bool PrintPreviewMessageHandler::OnMessageReceived( handled = true; IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) +#if 0 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, OnDidGetPreviewPageCount) +#endif IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, OnPrintPreviewFailed) +#if 0 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout, OnDidGetDefaultPageLayout) IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, @@ -348,6 +359,7 @@ bool PrintPreviewMessageHandler::OnMessageReceived( OnInvalidPrinterSettings) IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument, OnSetOptionsFromDocument) +#endif IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; diff --git a/chrome/browser/printing/print_preview_message_handler.h b/chrome/browser/printing/print_preview_message_handler.h index 6356792f29e82182c12118825949fe869cc80f9b..5592de3c097fd5d13fb0183fd3627cbb229d57b2 100644 --- a/chrome/browser/printing/print_preview_message_handler.h +++ b/chrome/browser/printing/print_preview_message_handler.h @@ -37,8 +37,7 @@ struct PageSizeMargins; // Manages the print preview handling for a WebContents. class PrintPreviewMessageHandler - : public content::WebContentsObserver, - public content::WebContentsUserData { + : public content::WebContentsObserver { public: ~PrintPreviewMessageHandler() override; @@ -46,10 +45,11 @@ class PrintPreviewMessageHandler bool OnMessageReceived(const IPC::Message& message, content::RenderFrameHost* render_frame_host) override; - private: + protected: explicit PrintPreviewMessageHandler(content::WebContents* web_contents); friend class content::WebContentsUserData; +#if 0 // Gets the print preview dialog associated with the WebContents being // observed. content::WebContents* GetPrintPreviewDialog(); @@ -72,12 +72,14 @@ class PrintPreviewMessageHandler void OnDidPreviewPage(content::RenderFrameHost* render_frame_host, const PrintHostMsg_DidPreviewPage_Params& params, const PrintHostMsg_PreviewIds& ids); - void OnMetafileReadyForPrinting( +#endif + virtual void OnMetafileReadyForPrinting( content::RenderFrameHost* render_frame_host, const PrintHostMsg_DidPreviewDocument_Params& params, const PrintHostMsg_PreviewIds& ids); - void OnPrintPreviewFailed(int document_cookie, - const PrintHostMsg_PreviewIds& ids); + virtual void OnPrintPreviewFailed(int document_cookie, + const PrintHostMsg_PreviewIds& ids); +#if 0 void OnPrintPreviewCancelled(int document_cookie, const PrintHostMsg_PreviewIds& ids); void OnInvalidPrinterSettings(int document_cookie, @@ -104,6 +106,7 @@ class PrintPreviewMessageHandler const PrintHostMsg_PreviewIds& ids, mojom::PdfCompositor::Status status, base::ReadOnlySharedMemoryRegion region); +#endif base::WeakPtrFactory weak_ptr_factory_; diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc index 9b0c15b3c5016707788476295d53ab9d8a80e338..388994072186d1ba7b831f004983d5e087adf891 100644 --- a/chrome/browser/printing/print_view_manager_base.cc +++ b/chrome/browser/printing/print_view_manager_base.cc @@ -30,7 +30,7 @@ #include "chrome/browser/ui/simple_message_box.h" #include "chrome/browser/ui/webui/print_preview/printer_handler.h" #include "chrome/common/pref_names.h" -#include "chrome/grit/generated_resources.h" +#include "electron/grit/electron_resources.h" #include "components/prefs/pref_service.h" #include "components/printing/browser/print_composite_client.h" #include "components/printing/browser/print_manager_utils.h" @@ -67,6 +67,8 @@ using PrintSettingsCallback = base::OnceCallback)>; void ShowWarningMessageBox(const base::string16& message) { + LOG(ERROR) << "Invalid printer settings " << message; +#if 0 // Runs always on the UI thread. static bool is_dialog_shown = false; if (is_dialog_shown) @@ -75,6 +77,7 @@ void ShowWarningMessageBox(const base::string16& message) { base::AutoReset auto_reset(&is_dialog_shown, true); chrome::ShowWarningMessageBox(nullptr, base::string16(), message); +#endif } #if BUILDFLAG(ENABLE_PRINT_PREVIEW) @@ -112,12 +115,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) queue_(g_browser_process->print_job_manager()->queue()), weak_ptr_factory_(this) { DCHECK(queue_); +#if 0 Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); printing_enabled_.Init( prefs::kPrintingEnabled, profile->GetPrefs(), base::Bind(&PrintViewManagerBase::UpdatePrintingEnabled, weak_ptr_factory_.GetWeakPtr())); +#endif } PrintViewManagerBase::~PrintViewManagerBase() { @@ -125,12 +130,16 @@ PrintViewManagerBase::~PrintViewManagerBase() { DisconnectFromCurrentPrintJob(); } -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) { +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh, + bool silent, + bool print_background, + const base::string16& device_name) { DisconnectFromCurrentPrintJob(); SetPrintingRFH(rfh); int32_t id = rfh->GetRoutingID(); - return PrintNowInternal(rfh, std::make_unique(id)); + return PrintNowInternal(rfh, std::make_unique( + id, silent, print_background, device_name)); } #if BUILDFLAG(ENABLE_PRINT_PREVIEW) @@ -249,7 +258,7 @@ void PrintViewManagerBase::UpdatePrintingEnabled() { // The Unretained() is safe because ForEachFrame() is synchronous. web_contents()->ForEachFrame(base::BindRepeating( &PrintViewManagerBase::SendPrintingEnabled, base::Unretained(this), - printing_enabled_.GetValue())); + true)); } void PrintViewManagerBase::NavigationStopped() { @@ -341,8 +350,10 @@ void PrintViewManagerBase::OnDidPrintDocument( void PrintViewManagerBase::OnPrintingFailed(int cookie) { PrintManager::OnPrintingFailed(cookie); +#if 0 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) ShowPrintErrorDialog(); +#endif #endif ReleasePrinterQuery(); @@ -592,6 +603,10 @@ void PrintViewManagerBase::ReleasePrintJob() { content::RenderFrameHost* rfh = printing_rfh_; printing_rfh_ = nullptr; + if (!callback.is_null()) { + callback.Run(printing_succeeded_ && print_job_); + } + if (!print_job_) return; diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h index fe0e0b5b065cdcc4edd04665271db14b1bf935e3..6670fc8abd2dabe601581cf43e51bb59f5ce577f 100644 --- a/chrome/browser/printing/print_view_manager_base.h +++ b/chrome/browser/printing/print_view_manager_base.h @@ -7,6 +7,7 @@ #include +#include "base/callback.h" #include "base/macros.h" #include "base/memory/read_only_shared_memory_region.h" #include "base/memory/scoped_refptr.h" @@ -47,7 +48,10 @@ class PrintViewManagerBase : public content::NotificationObserver, // Prints the current document immediately. Since the rendering is // asynchronous, the actual printing will not be completed on the return of // this function. Returns false if printing is impossible at the moment. - virtual bool PrintNow(content::RenderFrameHost* rfh); + virtual bool PrintNow(content::RenderFrameHost* rfh, + bool silent, + bool print_background, + const base::string16& device_name); #if BUILDFLAG(ENABLE_PRINT_PREVIEW) // Prints the document in |print_data| with settings specified in @@ -72,6 +76,8 @@ class PrintViewManagerBase : public content::NotificationObserver, base::string16 RenderSourceName(); + void SetCallback(const base::Callback& cb) { callback = cb; }; + protected: explicit PrintViewManagerBase(content::WebContents* web_contents); @@ -208,6 +214,8 @@ class PrintViewManagerBase : public content::NotificationObserver, scoped_refptr queue_; + base::Callback callback; + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBase); diff --git a/chrome/browser/printing/print_view_manager_common.cc b/chrome/browser/printing/print_view_manager_common.cc index 3d36b0b972b681c861c4e5273b3371eeabda1bce..3400f4a3470bda3ebf002417837592a12be4d55a 100644 --- a/chrome/browser/printing/print_view_manager_common.cc +++ b/chrome/browser/printing/print_view_manager_common.cc @@ -11,12 +11,12 @@ #include "printing/buildflags/buildflags.h" #include "url/gurl.h" -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 #include "components/guest_view/browser/guest_view_manager.h" #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" #endif // BUILDFLAG(ENABLE_EXTENSIONS) -#if BUILDFLAG(ENABLE_PRINT_PREVIEW) +#if 0 #include "chrome/browser/printing/print_view_manager.h" #else #include "chrome/browser/printing/print_view_manager_basic.h" @@ -25,7 +25,7 @@ namespace printing { namespace { -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a // full page MimeHandlerViewGuest plugin. Otherwise, returns false. bool StoreFullPagePlugin(content::WebContents** result, @@ -43,7 +43,7 @@ bool StoreFullPagePlugin(content::WebContents** result, // If we have a single full-page embedded mime handler view guest, print the // guest's WebContents instead. content::WebContents* GetWebContentsToUse(content::WebContents* contents) { -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 guest_view::GuestViewManager* guest_view_manager = guest_view::GuestViewManager::FromBrowserContext( contents->GetBrowserContext()); @@ -69,7 +69,8 @@ content::RenderFrameHost* GetRenderFrameHostToUse( void StartPrint(content::WebContents* contents, bool print_preview_disabled, bool has_selection) { -#if BUILDFLAG(ENABLE_PRINT_PREVIEW) + +#if 0 using PrintViewManagerImpl = PrintViewManager; #else using PrintViewManagerImpl = PrintViewManagerBasic; @@ -85,18 +86,20 @@ void StartPrint(content::WebContents* contents, GetRenderFrameHostToUse(contents, contents_to_use); if (!rfh_to_use) return; - +#if 0 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) if (!print_preview_disabled) { print_view_manager->PrintPreviewNow(rfh_to_use, has_selection); return; } #endif // ENABLE_PRINT_PREVIEW +#endif - print_view_manager->PrintNow(rfh_to_use); + print_view_manager->PrintNow(rfh_to_use, false, true, base::string16()); } void StartBasicPrint(content::WebContents* contents) { +#if 0 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) content::WebContents* contents_to_use = GetWebContentsToUse(contents); PrintViewManager* print_view_manager = @@ -111,6 +114,7 @@ void StartBasicPrint(content::WebContents* contents) { print_view_manager->BasicPrint(rfh_to_use); #endif // ENABLE_PRINT_PREVIEW +#endif } content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents) { diff --git a/chrome/browser/printing/printer_query.cc b/chrome/browser/printing/printer_query.cc index cc236873136797d90f3f5132726b34e7a3d7a989..159ec3a04502a4462bafbe31e39f58c6104c7e11 100644 --- a/chrome/browser/printing/printer_query.cc +++ b/chrome/browser/printing/printer_query.cc @@ -94,7 +94,31 @@ void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, base::BindOnce(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()), is_print_dialog_box_shown_, expected_page_count, - has_selection, margin_type, is_scripted, is_modifiable)); + has_selection, margin_type, is_scripted, is_modifiable, + base::string16())); +} + +void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings, + int expected_page_count, + bool has_selection, + MarginType margin_type, + bool is_scripted, + bool is_modifiable, + const base::string16& device_name, + base::OnceClosure callback) { + DCHECK(RunsTasksInCurrentSequence()); + DCHECK(!is_print_dialog_box_shown_); + + StartWorker(std::move(callback)); + + is_print_dialog_box_shown_ = false; + worker_->PostTask( + FROM_HERE, + base::BindOnce(&PrintJobWorker::GetSettings, + base::Unretained(worker_.get()), + is_print_dialog_box_shown_, expected_page_count, + has_selection, margin_type, is_scripted, is_modifiable, + device_name)); } void PrinterQuery::SetSettings( diff --git a/chrome/browser/printing/printer_query.h b/chrome/browser/printing/printer_query.h index 64db84f9f8532e2e53c07206f6d1505cc162df58..0e12adfe7957b071a71e377861a2ea0cf1a57b5e 100644 --- a/chrome/browser/printing/printer_query.h +++ b/chrome/browser/printing/printer_query.h @@ -59,6 +59,15 @@ class PrinterQuery : public base::RefCountedThreadSafe { bool is_modifiable, base::OnceClosure callback); + void GetSettings(GetSettingsAskParam ask_user_for_settings, + int expected_page_count, + bool has_selection, + MarginType margin_type, + bool is_scripted, + bool is_modifiable, + const base::string16& device_name, + base::OnceClosure callback); + // Updates the current settings with |new_settings| dictionary values. virtual void SetSettings(std::unique_ptr new_settings, base::OnceClosure callback); diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc index 54866fdcdb64f4ffd2414c8637ffa2f8fb10c024..40d6bd62706c1b47aff9ce32df713a47d203538f 100644 --- a/chrome/browser/printing/printing_message_filter.cc +++ b/chrome/browser/printing/printing_message_filter.cc @@ -94,12 +94,12 @@ PrintViewManager* GetPrintViewManager(int render_process_id, } // namespace -PrintingMessageFilter::PrintingMessageFilter(int render_process_id, - Profile* profile) +PrintingMessageFilter::PrintingMessageFilter(int render_process_id) : BrowserMessageFilter(PrintMsgStart), render_process_id_(render_process_id), queue_(g_browser_process->print_job_manager()->queue()) { DCHECK(queue_.get()); +#if 0 printing_shutdown_notifier_ = PrintingMessageFilterShutdownNotifierFactory::GetInstance() ->Get(profile) @@ -108,6 +108,7 @@ PrintingMessageFilter::PrintingMessageFilter(int render_process_id, is_printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs()); is_printing_enabled_.MoveToThread( BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); +#endif } PrintingMessageFilter::~PrintingMessageFilter() { @@ -116,7 +117,9 @@ PrintingMessageFilter::~PrintingMessageFilter() { void PrintingMessageFilter::ShutdownOnUIThread() { DCHECK_CURRENTLY_ON(BrowserThread::UI); +#if 0 is_printing_enabled_.Destroy(); +#endif printing_shutdown_notifier_.reset(); } @@ -145,6 +148,8 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) { #endif IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings) + IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_InitSettingWithDeviceName, + OnInitSettingWithDeviceName) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings) @@ -188,7 +193,7 @@ void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_frame_id, void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { DCHECK_CURRENTLY_ON(BrowserThread::IO); scoped_refptr printer_query; - if (!is_printing_enabled_.GetValue()) { + if (false) { // Reply with NULL query. OnGetDefaultPrintSettingsReply(printer_query, reply_msg); return; @@ -208,6 +213,26 @@ void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { printer_query, reply_msg)); } +void PrintingMessageFilter::OnInitSettingWithDeviceName( + const base::string16& device_name, + IPC::Message* reply_msg) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + scoped_refptr printer_query; + printer_query = queue_->PopPrinterQuery(0); + if (!printer_query.get()) { + printer_query = + queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id()); + } + + // Loads default settings. This is asynchronous, only the IPC message sender + // will hang until the settings are retrieved. + printer_query->GetSettings( + PrinterQuery::GetSettingsAskParam::DEFAULTS, 0, false, DEFAULT_MARGINS, + true, true, device_name, + base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, this, + printer_query, reply_msg)); +} + void PrintingMessageFilter::OnGetDefaultPrintSettingsReply( scoped_refptr printer_query, IPC::Message* reply_msg) { @@ -301,7 +326,7 @@ void PrintingMessageFilter::OnUpdatePrintSettings( std::unique_ptr new_settings(job_settings.DeepCopy()); scoped_refptr printer_query; - if (!is_printing_enabled_.GetValue()) { + if (false) { // Reply with NULL query. OnUpdatePrintSettingsReply(printer_query, reply_msg); return; @@ -361,10 +386,13 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( } } + #if BUILDFLAG(ENABLE_PRINT_PREVIEW) void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids, bool* cancel) { +#if 0 PrintPreviewUI::GetCurrentPrintPreviewStatus(ids, cancel); +#endif } #endif diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h index a881a853bfb0b46d0e074b7e86121429a5a761a3..46ebeede19557c718bf14b7feb81cf1acf3ca00a 100644 --- a/chrome/browser/printing/printing_message_filter.h +++ b/chrome/browser/printing/printing_message_filter.h @@ -38,7 +38,7 @@ class PrinterQuery; // renderer process on the IPC thread. class PrintingMessageFilter : public content::BrowserMessageFilter { public: - PrintingMessageFilter(int render_process_id, Profile* profile); + PrintingMessageFilter(int render_process_id); // content::BrowserMessageFilter methods. void OverrideThreadForMessage(const IPC::Message& message, @@ -73,6 +73,11 @@ class PrintingMessageFilter : public content::BrowserMessageFilter { // Get the default print setting. void OnGetDefaultPrintSettings(IPC::Message* reply_msg); + + // Set deviceName + void OnInitSettingWithDeviceName(const base::string16& device_name, + IPC::Message* reply_msg); + void OnGetDefaultPrintSettingsReply(scoped_refptr printer_query, IPC::Message* reply_msg); diff --git a/chrome/renderer/printing/chrome_print_render_frame_helper_delegate.cc b/chrome/renderer/printing/chrome_print_render_frame_helper_delegate.cc index 38fd116aa096b00266b6015f3196c3f432d23a9e..62fcfaa00084332ba07ff123e5136df606bb8168 100644 --- a/chrome/renderer/printing/chrome_print_render_frame_helper_delegate.cc +++ b/chrome/renderer/printing/chrome_print_render_frame_helper_delegate.cc @@ -19,7 +19,7 @@ #include "third_party/blink/public/web/web_element.h" #include "third_party/blink/public/web/web_local_frame.h" -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 #include "chrome/common/extensions/extension_constants.h" #include "extensions/common/constants.h" #include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.h" @@ -45,7 +45,7 @@ bool ChromePrintRenderFrameHelperDelegate::CancelPrerender( // Return the PDF object element if |frame| is the out of process PDF extension. blink::WebElement ChromePrintRenderFrameHelperDelegate::GetPdfElement( blink::WebLocalFrame* frame) { -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 GURL url = frame->GetDocument().Url(); bool inside_print_preview = url.GetOrigin() == chrome::kChromeUIPrintURL; bool inside_pdf_extension = @@ -71,7 +71,7 @@ bool ChromePrintRenderFrameHelperDelegate::IsPrintPreviewEnabled() { bool ChromePrintRenderFrameHelperDelegate::OverridePrint( blink::WebLocalFrame* frame) { -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if 0 if (!frame->GetDocument().IsPluginDocument()) return false; diff --git a/components/printing/common/print_messages.h b/components/printing/common/print_messages.h index d29bb6aedecd228e4bc02c84b86cce7151f33746..bb0bd7bfa494f813c30b47e84b78d2e0a951a321 100644 --- a/components/printing/common/print_messages.h +++ b/components/printing/common/print_messages.h @@ -367,7 +367,10 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu) #if BUILDFLAG(ENABLE_PRINTING) // Tells the RenderFrame to switch the CSS to print media type, renders every // requested pages and switch back the CSS to display media type. -IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages) +IPC_MESSAGE_ROUTED3(PrintMsg_PrintPages, + bool /* silent print */, + bool /* print page's background */, + base::string16 /* device name*/) // Like PrintMsg_PrintPages, but using the print preview document's frame/node. IPC_MESSAGE_ROUTED0(PrintMsg_PrintForSystemDialog) @@ -425,6 +428,11 @@ IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPrintFrameContent, IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings, PrintMsg_Print_Params /* default_settings */) +// you can set the printer +IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_InitSettingWithDeviceName, + base::string16, /* device name */ + PrintMsg_Print_Params /* default_settings */) + // The renderer wants to update the current print settings with new // |job_settings|. IPC_SYNC_MESSAGE_ROUTED2_2(PrintHostMsg_UpdatePrintSettings, diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc index c7ea5cf76aa629b8feaf99ff6e289140de1d35bd..1c3177e679ff6a3bbaa10cf74b9d538af7e5a588 100644 --- a/components/printing/renderer/print_render_frame_helper.cc +++ b/components/printing/renderer/print_render_frame_helper.cc @@ -1093,7 +1093,9 @@ void PrintRenderFrameHelper::OnDestruct() { delete this; } -void PrintRenderFrameHelper::OnPrintPages() { +void PrintRenderFrameHelper::OnPrintPages(bool silent, + bool print_background, + const base::string16& device_name) { if (ipc_nesting_level_ > 1) return; @@ -1106,7 +1108,7 @@ void PrintRenderFrameHelper::OnPrintPages() { // If we are printing a PDF extension frame, find the plugin node and print // that instead. auto plugin = delegate_->GetPdfElement(frame); - Print(frame, plugin, false /* is_scripted? */); + Print(frame, plugin, false, silent, print_background, device_name); if (weak_this) frame->DispatchAfterPrintEvent(); // WARNING: |this| may be gone at this point. Do not do any more work here and @@ -1158,6 +1160,8 @@ void PrintRenderFrameHelper::OnPrintPreview( if (ipc_nesting_level_ > 1) return; + blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); + print_preview_context_.InitWithFrame(frame); print_preview_context_.OnPrintPreview(); UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", @@ -1552,7 +1556,10 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) { void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, const blink::WebNode& node, - bool is_scripted) { + bool is_scripted, + bool silent, + bool print_background, + const base::string16& device_name) { // If still not finished with earlier print request simply ignore. if (prep_frame_view_) return; @@ -1560,7 +1567,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, FrameReference frame_ref(frame); int expected_page_count = 0; - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { + if (!CalculateNumberOfPages(frame, node, &expected_page_count, device_name)) { DidFinishPrinting(FAIL_PRINT_INIT); return; // Failed to init print page settings. } @@ -1580,8 +1587,9 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, PrintMsg_PrintPages_Params print_settings; auto self = weak_ptr_factory_.GetWeakPtr(); - GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count, - is_scripted, &print_settings); + if (!silent) + GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count, + is_scripted, &print_settings); // Check if |this| is still valid. if (!self) return; @@ -1591,6 +1599,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, ? blink::kWebPrintScalingOptionSourceSize : scaling_option; SetPrintPagesParams(print_settings); + print_settings.params.should_print_backgrounds = print_background; if (print_settings.params.dpi.IsEmpty() || !print_settings.params.document_cookie) { DidFinishPrinting(OK); // Release resources and fail silently on failure. @@ -1600,7 +1609,6 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, // Render Pages for printing. if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { - LOG(ERROR) << "RenderPagesForPrint failed"; DidFinishPrinting(FAIL_PRINT); } scripting_throttler_.Reset(); @@ -1778,10 +1786,17 @@ std::vector PrintRenderFrameHelper::GetPrintedPages( return printed_pages; } -bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) { +bool PrintRenderFrameHelper::InitPrintSettings( + bool fit_to_paper_size, + const base::string16& device_name) { PrintMsg_PrintPages_Params settings; + if (device_name.empty()) { Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), &settings.params)); + } else { + Send(new PrintHostMsg_InitSettingWithDeviceName(routing_id(), device_name, + &settings.params)); + } // Check if the printer returned any settings, if the settings is empty, we // can safely assume there are no printer drivers configured. So we safely // terminate. @@ -1803,10 +1818,11 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) { bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame, const blink::WebNode& node, - int* number_of_pages) { + int* number_of_pages, + const base::string16& device_name) { DCHECK(frame); bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); - if (!InitPrintSettings(fit_to_paper_size)) { + if (!InitPrintSettings(fit_to_paper_size, device_name)) { notify_browser_of_print_failure_ = false; Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); return false; @@ -1885,11 +1901,13 @@ bool PrintRenderFrameHelper::UpdatePrintSettings( return false; } +#if 0 if (!job_settings->GetInteger(kPreviewUIID, &settings.params.preview_ui_id)) { NOTREACHED(); print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); return false; } +#endif // Validate expected print preview settings. if (!job_settings->GetInteger(kPreviewRequestID, diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h index 6e2d7e1467b7bc179b1a0fc30dd656de612708cb..0e6e8ea770f52e5ddfb9f3e2d5793ad8184e8359 100644 --- a/components/printing/renderer/print_render_frame_helper.h +++ b/components/printing/renderer/print_render_frame_helper.h @@ -186,7 +186,9 @@ class PrintRenderFrameHelper bool OnMessageReceived(const IPC::Message& message) override; // Message handlers --------------------------------------------------------- - void OnPrintPages(); + void OnPrintPages(bool silent, + bool print_background, + const base::string16& device_name); void OnPrintForSystemDialog(); #if BUILDFLAG(ENABLE_PRINT_PREVIEW) void OnInitiatePrintPreview(bool has_selection); @@ -238,7 +240,10 @@ class PrintRenderFrameHelper // WARNING: |this| may be gone after this method returns. void Print(blink::WebLocalFrame* frame, const blink::WebNode& node, - bool is_scripted); + bool is_scripted, + bool silent = false, + bool print_background = false, + const base::string16& device_name = base::string16()); // Notification when printing is done - signal tear-down/free resources. void DidFinishPrinting(PrintingResult result); @@ -247,12 +252,15 @@ class PrintRenderFrameHelper // Initialize print page settings with default settings. // Used only for native printing workflow. - bool InitPrintSettings(bool fit_to_paper_size); + bool InitPrintSettings(bool fit_to_paper_size, + const base::string16& device_name = base::string16()); // Calculate number of pages in source document. - bool CalculateNumberOfPages(blink::WebLocalFrame* frame, - const blink::WebNode& node, - int* number_of_pages); + bool CalculateNumberOfPages( + blink::WebLocalFrame* frame, + const blink::WebNode& node, + int* number_of_pages, + const base::string16& device_name = base::string16()); #if BUILDFLAG(ENABLE_PRINT_PREVIEW) // Set options for print preset from source PDF document. diff --git a/printing/print_settings_conversion.cc b/printing/print_settings_conversion.cc index a4c9f49d65e0f6bccee7d49465cbba5a28785a22..b26eb4186880d869bd7466d1ad7f368aeefb175d 100644 --- a/printing/print_settings_conversion.cc +++ b/printing/print_settings_conversion.cc @@ -42,6 +42,19 @@ void GetCustomMarginsFromJobSettings(const base::DictionaryValue& settings, } } +void SetCustomMarginsToJobSettings(const PageSizeMargins& page_size_margins, + base::DictionaryValue* settings) { + std::unique_ptr custom_margins( + new base::DictionaryValue()); + custom_margins->SetDouble(kSettingMarginTop, page_size_margins.margin_top); + custom_margins->SetDouble(kSettingMarginBottom, + page_size_margins.margin_bottom); + custom_margins->SetDouble(kSettingMarginLeft, page_size_margins.margin_left); + custom_margins->SetDouble(kSettingMarginRight, + page_size_margins.margin_right); + settings->Set(kSettingMarginsCustom, std::move(custom_margins)); +} + void SetMarginsToJobSettings(const std::string& json_path, const PageMargins& margins, base::DictionaryValue* job_settings) { @@ -224,6 +237,72 @@ bool PrintSettingsFromJobSettings(const base::DictionaryValue& job_settings, return true; } +void PrintSettingsToJobSettings(const PrintSettings& settings, + base::DictionaryValue* job_settings) { + // header footer + job_settings->SetBoolean(kSettingHeaderFooterEnabled, + settings.display_header_footer()); + job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); + job_settings->SetString(kSettingHeaderFooterURL, settings.url()); + + // bg + job_settings->SetBoolean(kSettingShouldPrintBackgrounds, + settings.should_print_backgrounds()); + job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, + settings.selection_only()); + + // margin + auto margin_type = settings.margin_type(); + job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); + if (margin_type == CUSTOM_MARGINS) { + const auto& margins_in_points = + settings.requested_custom_margins_in_points(); + + PageSizeMargins page_size_margins; + + page_size_margins.margin_top = margins_in_points.top; + page_size_margins.margin_bottom = margins_in_points.bottom; + page_size_margins.margin_left = margins_in_points.left; + page_size_margins.margin_right = margins_in_points.right; + SetCustomMarginsToJobSettings(page_size_margins, job_settings); + } + job_settings->SetInteger(kSettingPreviewPageCount, 1); + + // range + + if (!settings.ranges().empty()) { + auto page_range_array = std::make_unique(); + job_settings->Set(kSettingPageRange, std::move(page_range_array)); + for (size_t i = 0; i < settings.ranges().size(); ++i) { + std::unique_ptr dict(new base::DictionaryValue); + dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); + dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); + page_range_array->Append(std::move(dict)); + } + } + + job_settings->SetBoolean(kSettingCollate, settings.collate()); + job_settings->SetInteger(kSettingCopies, 1); + job_settings->SetInteger(kSettingColor, settings.color()); + job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); + job_settings->SetBoolean(kSettingLandscape, settings.landscape()); + job_settings->SetString(kSettingDeviceName, settings.device_name()); + job_settings->SetInteger(kSettingScaleFactor, 100); + job_settings->SetBoolean("rasterizePDF", false); + + job_settings->SetInteger("dpi", settings.dpi()); + job_settings->SetInteger("dpiHorizontal", 72); + job_settings->SetInteger("dpiVertical", 72); + + job_settings->SetBoolean(kSettingPrintToPDF, false); + job_settings->SetBoolean(kSettingCloudPrintDialog, false); + job_settings->SetBoolean(kSettingPrintWithPrivet, false); + job_settings->SetBoolean(kSettingPrintWithExtension, false); + + job_settings->SetBoolean(kSettingShowSystemDialog, false); + job_settings->SetInteger(kSettingPreviewPageCount, 1); +} + void PrintSettingsToJobSettingsDebug(const PrintSettings& settings, base::DictionaryValue* job_settings) { job_settings->SetBoolean(kSettingHeaderFooterEnabled, diff --git a/printing/print_settings_conversion.h b/printing/print_settings_conversion.h index 283c0ff81954bb9e777b1e0007a735ec0ad4901e..231873a456442856c43d643e41c1bb22f4203a5c 100644 --- a/printing/print_settings_conversion.h +++ b/printing/print_settings_conversion.h @@ -21,6 +21,10 @@ PRINTING_EXPORT bool PrintSettingsFromJobSettings( const base::DictionaryValue& job_settings, PrintSettings* print_settings); +PRINTING_EXPORT void PrintSettingsToJobSettings( + const PrintSettings& settings, + base::DictionaryValue* job_settings); + // Use for debug only, because output is not completely consistent with format // of |PrintSettingsFromJobSettings| input. void PrintSettingsToJobSettingsDebug(const PrintSettings& settings,