1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/printing_context_no_system_dialog.h"
8 #include <unicode/ulocdata.h>
13 #include "base/logging.h"
14 #include "base/values.h"
15 #include "printing/buildflags/buildflags.h"
16 #include "printing/metafile.h"
17 #include "printing/print_job_constants.h"
18 #include "printing/units.h"
22 #if !BUILDFLAG(USE_CUPS)
24 std::unique_ptr<PrintingContext> PrintingContext::CreateImpl(
26 bool skip_system_calls) {
27 return std::make_unique<PrintingContextNoSystemDialog>(delegate);
29 #endif // !BUILDFLAG(USE_CUPS)
31 PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(Delegate* delegate)
32 : PrintingContext(delegate) {}
34 PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() {
38 void PrintingContextNoSystemDialog::AskUserForSettings(
42 PrintSettingsCallback callback) {
43 // We don't want to bring up a dialog here. Ever. Just signal the callback.
44 std::move(callback).Run(mojom::ResultCode::kSuccess);
47 mojom::ResultCode PrintingContextNoSystemDialog::UseDefaultSettings() {
48 DCHECK(!in_print_job_);
51 settings_->set_dpi(kDefaultPdfDpi);
52 gfx::Size physical_size = GetPdfPaperSizeDeviceUnits();
53 // Assume full page is printable for now.
54 gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height());
55 DCHECK_EQ(settings_->device_units_per_inch(), kDefaultPdfDpi);
56 settings_->SetPrinterPrintableArea(physical_size, printable_area, true);
57 return mojom::ResultCode::kSuccess;
60 gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() {
63 UErrorCode error = U_ZERO_ERROR;
64 ulocdata_getPaperSize(delegate_->GetAppLocale().c_str(), &height, &width,
66 if (error > U_ZERO_ERROR) {
67 // If the call failed, assume a paper size of 8.5 x 11 inches.
68 LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: "
71 static_cast<int>(kLetterWidthInch * settings_->device_units_per_inch());
72 height = static_cast<int>(kLetterHeightInch *
73 settings_->device_units_per_inch());
75 // ulocdata_getPaperSize returns the width and height in mm.
76 // Convert this to pixels based on the dpi.
77 float multiplier = settings_->device_units_per_inch() / kMicronsPerMil;
81 return gfx::Size(width, height);
84 mojom::ResultCode PrintingContextNoSystemDialog::UpdatePrinterSettings(
85 const PrinterSettings& printer_settings) {
86 DCHECK(!printer_settings.show_system_dialog);
88 if (settings_->dpi() == 0)
91 return mojom::ResultCode::kSuccess;
94 mojom::ResultCode PrintingContextNoSystemDialog::NewDocument(
95 const std::u16string& document_name) {
96 DCHECK(!in_print_job_);
99 return mojom::ResultCode::kSuccess;
102 mojom::ResultCode PrintingContextNoSystemDialog::PrintDocument(
103 const MetafilePlayer& metafile,
104 const PrintSettings& settings,
105 uint32_t num_pages) {
107 return mojom::ResultCode::kCanceled;
108 DCHECK(in_print_job_);
110 // Intentional No-op.
112 return mojom::ResultCode::kSuccess;
115 mojom::ResultCode PrintingContextNoSystemDialog::DocumentDone() {
117 return mojom::ResultCode::kCanceled;
118 DCHECK(in_print_job_);
121 return mojom::ResultCode::kSuccess;
124 void PrintingContextNoSystemDialog::Cancel() {
125 abort_printing_ = true;
126 in_print_job_ = false;
129 void PrintingContextNoSystemDialog::ReleaseContext() {
130 // Intentional No-op.
133 printing::NativeDrawingContext PrintingContextNoSystemDialog::context() const {
134 // Intentional No-op.
138 } // namespace printing