From 6c2af8b421395cfc3a20e045218d7a1c739f08d5 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 8 Nov 2019 10:01:50 -0800 Subject: [PATCH] fix: refactor printing for mojom (#21026) --- patches/chromium/printing.patch | 88 ++++++++++++---------- shell/browser/api/atom_api_web_contents.cc | 74 +++++++++--------- 2 files changed, 86 insertions(+), 76 deletions(-) diff --git a/patches/chromium/printing.patch b/patches/chromium/printing.patch index 2fd4b5983170..0c22743fea91 100644 --- a/patches/chromium/printing.patch +++ b/patches/chromium/printing.patch @@ -63,7 +63,7 @@ index ab32d5475a0e269d32f6ab71f7dc82aa9e02035a..9ada750416f02d23c2f9faae0c41e4a7 } diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc -index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb07588684d1ea05a 100644 +index 81cb954f02363c829947dde830f340a761c80a77..98043d6ec0cb2c55493ef7446a6de0c4eeed184e 100644 --- a/chrome/browser/printing/print_view_manager_base.cc +++ b/chrome/browser/printing/print_view_manager_base.cc @@ -27,10 +27,7 @@ @@ -117,28 +117,35 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 } PrintViewManagerBase::~PrintViewManagerBase() { -@@ -123,7 +126,8 @@ PrintViewManagerBase::~PrintViewManagerBase() { +@@ -123,7 +126,10 @@ PrintViewManagerBase::~PrintViewManagerBase() { DisconnectFromCurrentPrintJob(); } -bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) { +bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh, -+ CompletionCallback callback) { ++ bool silent, ++ base::Value settings, ++ CompletionCallback callback) { DisconnectFromCurrentPrintJob(); // Don't print / print preview interstitials or crashed tabs. -@@ -131,7 +135,9 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) { +@@ -131,7 +136,14 @@ bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) { return false; SetPrintingRFH(rfh); - GetPrintRenderFrame(rfh)->PrintRequestedPages(); + callback_ = std::move(callback); -+ // FIXME: pass 'silent' through from the caller..? -+ GetPrintRenderFrame(rfh)->PrintRequestedPages(false /* silent */); ++ ++ if (!callback_.is_null()) { ++ registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, ++ content::NotificationService::AllSources()); ++ } ++ ++ GetPrintRenderFrame(rfh)->PrintRequestedPages(silent, std::move(settings)); return true; } -@@ -246,9 +252,9 @@ void PrintViewManagerBase::StartLocalPrintJob( +@@ -246,9 +258,9 @@ void PrintViewManagerBase::StartLocalPrintJob( void PrintViewManagerBase::UpdatePrintingEnabled() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); // The Unretained() is safe because ForEachFrame() is synchronous. @@ -151,7 +158,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 } void PrintViewManagerBase::NavigationStopped() { -@@ -351,7 +357,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) { +@@ -351,7 +363,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) { PrintManager::OnPrintingFailed(cookie); #if BUILDFLAG(ENABLE_PRINT_PREVIEW) @@ -160,7 +167,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 #endif ReleasePrinterQuery(); -@@ -449,9 +455,13 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent( +@@ -449,9 +461,13 @@ void PrintViewManagerBase::OnNotifyPrintJobEvent( content::NotificationService::NoDetails()); break; } @@ -176,7 +183,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 NOTREACHED(); break; } -@@ -546,8 +556,6 @@ bool PrintViewManagerBase::CreateNewPrintJob( +@@ -546,8 +562,6 @@ bool PrintViewManagerBase::CreateNewPrintJob( DCHECK(!quit_inner_loop_); DCHECK(query); @@ -185,7 +192,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 // We can't print if there is no renderer. if (!web_contents()->GetRenderViewHost() || -@@ -562,8 +570,6 @@ bool PrintViewManagerBase::CreateNewPrintJob( +@@ -562,8 +576,6 @@ bool PrintViewManagerBase::CreateNewPrintJob( print_job_->SetSource(PrintJob::Source::PRINT_PREVIEW, /*source_id=*/""); #endif // defined(OS_CHROMEOS) @@ -194,7 +201,7 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 printing_succeeded_ = false; return true; } -@@ -612,14 +618,22 @@ void PrintViewManagerBase::ReleasePrintJob() { +@@ -612,14 +624,24 @@ void PrintViewManagerBase::ReleasePrintJob() { content::RenderFrameHost* rfh = printing_rfh_; printing_rfh_ = nullptr; @@ -204,6 +211,8 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 + cb_str = printing_cancelled_ ? "cancelled" : "failed"; + std::move(callback_).Run(printing_succeeded_, cb_str); + } ++ ++ TerminatePrintJob(true); + if (!print_job_) return; @@ -219,18 +228,8 @@ index 81cb954f02363c829947dde830f340a761c80a77..2a26bb6d5367337fa13da07cb0758868 // Don't close the worker thread. print_job_ = nullptr; } -@@ -694,6 +708,9 @@ bool PrintViewManagerBase::PrintNowInternal( - // Don't print / print preview interstitials or crashed tabs. - if (IsInterstitialOrCrashed()) - return false; -+ -+ registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, -+ content::NotificationService::AllSources()); - return rfh->Send(message.release()); - } - diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h -index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a9e5342ff 100644 +index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..b99b5d0a4fce3b15484495948462ee2aa2e321c3 100644 --- a/chrome/browser/printing/print_view_manager_base.h +++ b/chrome/browser/printing/print_view_manager_base.h @@ -33,6 +33,8 @@ class PrintJob; @@ -242,17 +241,19 @@ index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a // Base class for managing the print commands for a WebContents. class PrintViewManagerBase : public content::NotificationObserver, public PrintManager { -@@ -42,7 +44,8 @@ class PrintViewManagerBase : public content::NotificationObserver, +@@ -42,7 +44,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, ++ base::Value settings, + CompletionCallback callback); #if BUILDFLAG(ENABLE_PRINT_PREVIEW) // Prints the document in |print_data| with settings specified in -@@ -206,9 +209,15 @@ class PrintViewManagerBase : public content::NotificationObserver, +@@ -206,9 +210,15 @@ class PrintViewManagerBase : public content::NotificationObserver, // The current RFH that is printing with a system printing dialog. content::RenderFrameHost* printing_rfh_; @@ -269,7 +270,7 @@ index af49d3e2f8abaf7dc4d82dc3f9beccdf4fbd9f18..26eecede8e698046c4b304a06b49946a // This means we are _blocking_ until all the necessary pages have been // rendered or the print settings are being loaded. diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc -index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2eb48262ce 100644 +index 40762a36024bc48dfe5259520161dc203197bfd0..8f2d0b808df1b68699bb946436b219315f1ea6d3 100644 --- a/chrome/browser/printing/printing_message_filter.cc +++ b/chrome/browser/printing/printing_message_filter.cc @@ -22,6 +22,7 @@ @@ -335,7 +336,7 @@ index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2e std::unique_ptr printer_query = queue_->PopPrinterQuery(document_cookie); if (!printer_query) { -@@ -258,7 +266,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( +@@ -258,7 +267,9 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( std::unique_ptr printer_query, IPC::Message* reply_msg) { PrintMsg_PrintPages_Params params; @@ -346,7 +347,7 @@ index 40762a36024bc48dfe5259520161dc203197bfd0..e38aa442df858ce362645230f7642b2e params.Reset(); } else { RenderParamsFromPrintSettings(printer_query->settings(), ¶ms.params); -@@ -296,7 +306,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( +@@ -296,7 +307,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( #if BUILDFLAG(ENABLE_PRINT_PREVIEW) void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids, bool* cancel) { @@ -381,20 +382,29 @@ index 9fbea6d0a2dbe55b1d600fbc217dee5aa8ae8cd5..de9bd267e408c02fd4da7d903523c0e6 // content::BrowserMessageFilter: bool OnMessageReceived(const IPC::Message& message) override; diff --git a/components/printing/common/print.mojom b/components/printing/common/print.mojom -index d5ff612320fb3f324b58c91b0c05e0eac689beef..9fafde963ecce596c9514384d2aa11a7540e0d87 100644 +index d5ff612320fb3f324b58c91b0c05e0eac689beef..254810ca174c456b6bf6966954e4e188c2460f15 100644 --- a/components/printing/common/print.mojom +++ b/components/printing/common/print.mojom -@@ -16,7 +16,7 @@ interface PrintRenderer { +@@ -4,6 +4,8 @@ + + module printing.mojom; + ++import "mojo/public/mojom/base/values.mojom"; ++ + // Interface implemented by a class that desires to render print documents for + // Chrome print preview. + interface PrintRenderer { +@@ -16,7 +18,7 @@ interface PrintRenderer { interface PrintRenderFrame { // Tells the RenderFrame to switch the CSS to print media type, render every // requested page, and then switch back the CSS to display media type. - PrintRequestedPages(); -+ PrintRequestedPages(bool silent); ++ PrintRequestedPages(bool silent, mojo_base.mojom.DictionaryValue settings); // Tells the RenderFrame to switch the CSS to print media type, render every // requested page using the print preview document's frame/node, and then diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc -index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f7546550644a1d0 100644 +index 95bf92929983110de4489ee18f52783ddf178bcf..43c6b504f60a44daedd66f7dcf7d8d0b2cf9c6e9 100644 --- a/components/printing/renderer/print_render_frame_helper.cc +++ b/components/printing/renderer/print_render_frame_helper.cc @@ -38,6 +38,7 @@ @@ -420,7 +430,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655 } -void PrintRenderFrameHelper::PrintRequestedPages() { -+void PrintRenderFrameHelper::PrintRequestedPages(bool silent) { ++void PrintRenderFrameHelper::PrintRequestedPages(bool silent, base::Value settings) { ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr()); if (ipc_nesting_level_ > 1) return; @@ -429,7 +439,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655 // that instead. auto plugin = delegate_->GetPdfElement(frame); - Print(frame, plugin, PrintRequestType::kRegular); -+ Print(frame, plugin, PrintRequestType::kRegular, silent, base::DictionaryValue()); ++ Print(frame, plugin, PrintRequestType::kRegular, silent, std::move(settings)); if (!render_frame_gone_) frame->DispatchAfterPrintEvent(); // WARNING: |this| may be gone at this point. Do not do any more work here and @@ -469,7 +479,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655 - PrintRequestType print_request_type) { + PrintRequestType print_request_type, + bool silent, -+ const base::DictionaryValue& settings) { ++ base::Value settings) { // If still not finished with earlier print request simply ignore. if (prep_frame_view_) return; @@ -478,7 +488,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655 int expected_page_count = 0; - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { -+ if (!CalculateNumberOfPages(frame, node, &expected_page_count, settings)) { ++ if (!CalculateNumberOfPages(frame, node, &expected_page_count, base::Value::AsDictionaryValue(settings))) { DidFinishPrinting(FAIL_PRINT_INIT); return; // Failed to init print page settings. } @@ -543,7 +553,7 @@ index 95bf92929983110de4489ee18f52783ddf178bcf..c82a3765fa2e0a45ed379e924f754655 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); return false; diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h -index dcb0da469d5aab19bc568deb07d3619f8defa76e..9d5049cc42a8a13055fadc94e02c9feca7fe8955 100644 +index dcb0da469d5aab19bc568deb07d3619f8defa76e..6ade4fcb0d9e758e69016f1b032d7d37e1a635ab 100644 --- a/components/printing/renderer/print_render_frame_helper.h +++ b/components/printing/renderer/print_render_frame_helper.h @@ -212,7 +212,7 @@ class PrintRenderFrameHelper @@ -551,7 +561,7 @@ index dcb0da469d5aab19bc568deb07d3619f8defa76e..9d5049cc42a8a13055fadc94e02c9fec // printing::mojom::PrintRenderFrame: - void PrintRequestedPages() override; -+ void PrintRequestedPages(bool silent) override; ++ void PrintRequestedPages(bool silent, base::Value settings) override; void PrintForSystemDialog() override; #if BUILDFLAG(ENABLE_PRINT_PREVIEW) void InitiatePrintPreview( @@ -562,7 +572,7 @@ index dcb0da469d5aab19bc568deb07d3619f8defa76e..9d5049cc42a8a13055fadc94e02c9fec - PrintRequestType print_request_type); + PrintRequestType print_request_type, + bool silent, -+ const base::DictionaryValue& settings); ++ base::Value settings); // Notification when printing is done - signal tear-down/free resources. void DidFinishPrinting(PrintingResult result); diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index ec4a3983eef6..b4e8faec4a78 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -1692,7 +1692,7 @@ bool WebContents::IsCurrentlyAudible() { void WebContents::Print(gin_helper::Arguments* args) { gin_helper::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate()); - base::DictionaryValue settings; + base::Value settings(base::Value::Type::DICTIONARY); if (args->Length() >= 1 && !args->GetNext(&options)) { args->ThrowError("webContents.print(): Invalid print settings specified."); @@ -1712,7 +1712,7 @@ void WebContents::Print(gin_helper::Arguments* args) { bool print_background = false; options.Get("printBackground", &print_background); - settings.SetBoolean(printing::kSettingShouldPrintBackgrounds, + settings.SetBoolKey(printing::kSettingShouldPrintBackgrounds, print_background); // Set custom margin settings @@ -1721,62 +1721,62 @@ void WebContents::Print(gin_helper::Arguments* args) { if (options.Get("margins", &margins)) { printing::MarginType margin_type = printing::DEFAULT_MARGINS; margins.Get("marginType", &margin_type); - settings.SetInteger(printing::kSettingMarginsType, margin_type); + settings.SetIntKey(printing::kSettingMarginsType, margin_type); if (margin_type == printing::CUSTOM_MARGINS) { int top = 0; margins.Get("top", &top); - settings.SetInteger(printing::kSettingMarginTop, top); + settings.SetIntKey(printing::kSettingMarginTop, top); int bottom = 0; margins.Get("bottom", &bottom); - settings.SetInteger(printing::kSettingMarginBottom, bottom); + settings.SetIntKey(printing::kSettingMarginBottom, bottom); int left = 0; margins.Get("left", &left); - settings.SetInteger(printing::kSettingMarginLeft, left); + settings.SetIntKey(printing::kSettingMarginLeft, left); int right = 0; margins.Get("right", &right); - settings.SetInteger(printing::kSettingMarginRight, right); + settings.SetIntKey(printing::kSettingMarginRight, right); } } else { - settings.SetInteger(printing::kSettingMarginsType, - printing::DEFAULT_MARGINS); + settings.SetIntKey(printing::kSettingMarginsType, + printing::DEFAULT_MARGINS); } // Set whether to print color or greyscale bool print_color = true; options.Get("color", &print_color); int color_setting = print_color ? printing::COLOR : printing::GRAY; - settings.SetInteger(printing::kSettingColor, color_setting); + settings.SetIntKey(printing::kSettingColor, color_setting); // Is the orientation landscape or portrait. bool landscape = false; options.Get("landscape", &landscape); - settings.SetBoolean(printing::kSettingLandscape, landscape); + settings.SetBoolKey(printing::kSettingLandscape, landscape); // We set the default to empty string here and only update // if at the Chromium level if it's non-empty // Printer device name as opened by the OS. base::string16 device_name; options.Get("deviceName", &device_name); - settings.SetString(printing::kSettingDeviceName, device_name); + settings.SetStringKey(printing::kSettingDeviceName, device_name); int scale_factor = 100; options.Get("scaleFactor", &scale_factor); - settings.SetInteger(printing::kSettingScaleFactor, scale_factor); + settings.SetIntKey(printing::kSettingScaleFactor, scale_factor); int pages_per_sheet = 1; options.Get("pagesPerSheet", &pages_per_sheet); - settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet); + settings.SetIntKey(printing::kSettingPagesPerSheet, pages_per_sheet); // True if the user wants to print with collate. bool collate = true; options.Get("collate", &collate); - settings.SetBoolean(printing::kSettingCollate, collate); + settings.SetBoolKey(printing::kSettingCollate, collate); // The number of individual copies to print int copies = 1; options.Get("copies", &copies); - settings.SetInteger(printing::kSettingCopies, copies); + settings.SetIntKey(printing::kSettingCopies, copies); // Strings to be printed as headers and footers if requested by the user. std::string header; @@ -1785,44 +1785,43 @@ void WebContents::Print(gin_helper::Arguments* args) { options.Get("footer", &footer); if (!(header.empty() && footer.empty())) { - settings.SetBoolean(printing::kSettingHeaderFooterEnabled, true); + settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, true); - settings.SetString(printing::kSettingHeaderFooterTitle, header); - settings.SetString(printing::kSettingHeaderFooterURL, footer); + settings.SetStringKey(printing::kSettingHeaderFooterTitle, header); + settings.SetStringKey(printing::kSettingHeaderFooterURL, footer); } else { - settings.SetBoolean(printing::kSettingHeaderFooterEnabled, false); + settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, false); } // We don't want to allow the user to enable these settings // but we need to set them or a CHECK is hit. - settings.SetInteger(printing::kSettingPrinterType, printing::kLocalPrinter); - settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false); - settings.SetBoolean(printing::kSettingRasterizePdf, false); + settings.SetIntKey(printing::kSettingPrinterType, printing::kLocalPrinter); + settings.SetBoolKey(printing::kSettingShouldPrintSelectionOnly, false); + settings.SetBoolKey(printing::kSettingRasterizePdf, false); // Set custom page ranges to print std::vector page_ranges; if (options.Get("pageRanges", &page_ranges)) { - std::unique_ptr page_range_list(new base::ListValue()); + base::Value page_range_list(base::Value::Type::LIST); for (auto& range : page_ranges) { int from, to; if (range.Get("from", &from) && range.Get("to", &to)) { - std::unique_ptr range( - new base::DictionaryValue()); - range->SetInteger(printing::kSettingPageRangeFrom, from); - range->SetInteger(printing::kSettingPageRangeTo, to); - page_range_list->Append(std::move(range)); + base::Value range(base::Value::Type::DICTIONARY); + range.SetIntKey(printing::kSettingPageRangeFrom, from); + range.SetIntKey(printing::kSettingPageRangeTo, to); + page_range_list.Append(std::move(range)); } else { continue; } } - if (page_range_list->GetSize() > 0) - settings.SetList(printing::kSettingPageRange, std::move(page_range_list)); + if (page_range_list.GetList().size() > 0) + settings.SetPath(printing::kSettingPageRange, std::move(page_range_list)); } // Duplex type user wants to use. printing::DuplexMode duplex_mode; options.Get("duplexMode", &duplex_mode); - settings.SetInteger(printing::kSettingDuplexMode, duplex_mode); + settings.SetIntKey(printing::kSettingDuplexMode, duplex_mode); // Set custom dots per inch (dpi) gin_helper::Dictionary dpi_settings; @@ -1830,13 +1829,13 @@ void WebContents::Print(gin_helper::Arguments* args) { if (options.Get("dpi", &dpi_settings)) { int horizontal = 72; dpi_settings.Get("horizontal", &horizontal); - settings.SetInteger(printing::kSettingDpiHorizontal, horizontal); + settings.SetIntKey(printing::kSettingDpiHorizontal, horizontal); int vertical = 72; dpi_settings.Get("vertical", &vertical); - settings.SetInteger(printing::kSettingDpiVertical, vertical); + settings.SetIntKey(printing::kSettingDpiVertical, vertical); } else { - settings.SetInteger(printing::kSettingDpiHorizontal, dpi); - settings.SetInteger(printing::kSettingDpiVertical, dpi); + settings.SetIntKey(printing::kSettingDpiHorizontal, dpi); + settings.SetIntKey(printing::kSettingDpiVertical, dpi); } auto* print_view_manager = @@ -1845,7 +1844,8 @@ void WebContents::Print(gin_helper::Arguments* args) { auto* rfh = focused_frame && focused_frame->HasSelection() ? focused_frame : web_contents()->GetMainFrame(); - print_view_manager->PrintNow(rfh, std::move(callback)); + print_view_manager->PrintNow(rfh, silent, std::move(settings), + std::move(callback)); } std::vector WebContents::GetPrinterList() {