Skip to content

Commit 044be7c

Browse files
authored
fix: avoid crash in window.print() when prefilling native print dialog (#50843)
fix: avoid crash in window.print() when prefilling native print dialog When UpdatePrinterSettings() fails (e.g. the printer rejects the requested resolution), OnError() nullifies print_info_ via ReleaseContext(). The return value was not checked, so AskUserForSettings() passed nil to [NSPrintPanel runModalWithPrintInfo:], crashing in PJCSessionHasApplicationSetPrinter with a null PMPrintSession. Check the return value and fall back to UseDefaultSettings() on failure so the dialog opens with defaults instead of crashing.
1 parent 7245c6a commit 044be7c

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

patches/chromium/printing.patch

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ index 2a477e820d9f0126a05f86cd44f02c2189275bad..a2e9442ff9f5acf8e301f457b1806251
620620

621621
#if BUILDFLAG(IS_CHROMEOS)
622622
diff --git a/chrome/browser/printing/printer_query_oop.cc b/chrome/browser/printing/printer_query_oop.cc
623-
index dc2a15ab4d784b0b6c85b84a30c3c08a17ed8e3d..e197026e8a7f132c1bf90a0f5f1eabb4f5f064ee 100644
623+
index dc2a15ab4d784b0b6c85b84a30c3c08a17ed8e3d..5ca7920c8525c3c72fd96b14709fb35a9cc28daf 100644
624624
--- a/chrome/browser/printing/printer_query_oop.cc
625625
+++ b/chrome/browser/printing/printer_query_oop.cc
626626
@@ -126,7 +126,7 @@ void PrinterQueryOop::OnDidAskUserForSettings(
@@ -632,7 +632,7 @@ index dc2a15ab4d784b0b6c85b84a30c3c08a17ed8e3d..e197026e8a7f132c1bf90a0f5f1eabb4
632632
// Want the same PrintBackend service as the query so that we use the same
633633
// device context.
634634
print_document_client_id_ =
635-
@@ -189,6 +189,21 @@ void PrinterQueryOop::GetSettingsWithUI(uint32_t document_page_count,
635+
@@ -189,6 +189,28 @@ void PrinterQueryOop::GetSettingsWithUI(uint32_t document_page_count,
636636
// browser process.
637637
// - Other platforms don't have a system print UI or do not use OOP
638638
// printing, so this does not matter.
@@ -643,12 +643,19 @@ index dc2a15ab4d784b0b6c85b84a30c3c08a17ed8e3d..e197026e8a7f132c1bf90a0f5f1eabb4
643643
+ // remote service context, not the local one used by the native dialog.
644644
+ if (settings().dpi()) {
645645
+ printing_context()->SetPrintSettings(settings());
646-
+ printing_context()->UpdatePrinterSettings(PrintingContext::PrinterSettings{
646+
+ if (printing_context()->UpdatePrinterSettings(
647+
+ PrintingContext::PrinterSettings{
647648
+#if BUILDFLAG(IS_MAC)
648-
+ .external_preview = false,
649+
+ .external_preview = false,
649650
+#endif
650-
+ .show_system_dialog = false,
651-
+ });
651+
+ .show_system_dialog = false,
652+
+ }) != mojom::ResultCode::kSuccess) {
653+
+ // Prefilling failed (e.g. the printer does not support the requested
654+
+ // resolution). Reinitialize with defaults so that AskUserForSettings
655+
+ // does not crash due to a null print_info_. The dialog will simply
656+
+ // open without prefilled values.
657+
+ printing_context()->UseDefaultSettings();
658+
+ }
652659
+ }
653660
+
654661
PrinterQuery::GetSettingsWithUI(

0 commit comments

Comments
 (0)