feat: allow defaulting to printer default page size (#49523)

Co-authored-by: Edvan de Matos <edvan.santos@querodelivery.com>
This commit is contained in:
Shelley Vohr
2026-02-13 13:40:14 +01:00
committed by GitHub
parent 3a5f9e0a33
commit dcdbb0397e
7 changed files with 162 additions and 65 deletions

View File

@@ -191,4 +191,33 @@ scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner() {
#endif
}
std::optional<gfx::Size> GetPrinterDefaultPaperSize(
const std::string& printer_name) {
#if BUILDFLAG(IS_WIN)
// Blocking is needed here because Windows printer drivers are oftentimes
// not thread-safe and have to be accessed on the UI thread.
ScopedAllowBlockingForElectron allow_blocking;
#endif
if (printer_name.empty())
return std::nullopt;
scoped_refptr<printing::PrintBackend> print_backend =
printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
if (!print_backend)
return std::nullopt;
printing::PrinterSemanticCapsAndDefaults caps;
printing::mojom::ResultCode result =
print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, &caps);
if (result != printing::mojom::ResultCode::kSuccess)
return std::nullopt;
if (!caps.default_paper.size_um().IsEmpty())
return caps.default_paper.size_um();
return std::nullopt;
}
} // namespace electron

View File

@@ -44,6 +44,11 @@ std::pair<std::string, std::u16string> GetDeviceNameToUse(
// This function creates a task runner for use with printing tasks.
scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner();
// This function returns the default paper size of the specified printer, if
// available.
std::optional<gfx::Size> GetPrinterDefaultPaperSize(
const std::string& printer_name);
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_PRINTING_PRINTING_UTILS_H_