[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/printing_context_no_system_dialog.h" |
| 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stdint.h> |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 8 | #include <unicode/ulocdata.h> |
| 9 | |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "base/values.h" |
| 14 | #include "printing/metafile.h" |
| 15 | #include "printing/print_job_constants.h" |
| 16 | #include "printing/units.h" |
| 17 | |
| 18 | namespace printing { |
| 19 | |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 20 | #if !defined(USE_CUPS) |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 21 | // static |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 22 | std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame] | 23 | return std::make_unique<PrintingContextNoSystemDialog>(delegate); |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 24 | } |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 25 | #endif // !defined(USE_CUPS) |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 26 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 27 | PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(Delegate* delegate) |
| 28 | : PrintingContext(delegate) { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() { |
| 32 | ReleaseContext(); |
| 33 | } |
| 34 | |
| 35 | void PrintingContextNoSystemDialog::AskUserForSettings( |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 36 | int max_pages, |
| 37 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 38 | bool is_scripted, |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 39 | PrintSettingsCallback callback) { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 40 | // We don't want to bring up a dialog here. Ever. Just signal the callback. |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 41 | std::move(callback).Run(OK); |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() { |
| 45 | DCHECK(!in_print_job_); |
| 46 | |
| 47 | ResetSettings(); |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 48 | settings_.set_dpi(kDefaultPdfDpi); |
| 49 | gfx::Size physical_size = GetPdfPaperSizeDeviceUnits(); |
| 50 | // Assume full page is printable for now. |
| 51 | gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height()); |
| 52 | DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi); |
| 53 | settings_.SetPrinterPrintableArea(physical_size, printable_area, true); |
| 54 | return OK; |
| 55 | } |
| 56 | |
| 57 | gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 58 | int32_t width = 0; |
| 59 | int32_t height = 0; |
| 60 | UErrorCode error = U_ZERO_ERROR; |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 61 | ulocdata_getPaperSize( |
| 62 | delegate_->GetAppLocale().c_str(), &height, &width, &error); |
[email protected] | d1bee42 | 2012-08-07 23:44:31 | [diff] [blame] | 63 | if (error > U_ZERO_ERROR) { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 64 | // If the call failed, assume a paper size of 8.5 x 11 inches. |
| 65 | LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: " |
| 66 | << error; |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 67 | width = static_cast<int>( |
| 68 | kLetterWidthInch * settings_.device_units_per_inch()); |
| 69 | height = static_cast<int>( |
| 70 | kLetterHeightInch * settings_.device_units_per_inch()); |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 71 | } else { |
| 72 | // ulocdata_getPaperSize returns the width and height in mm. |
| 73 | // Convert this to pixels based on the dpi. |
Lei Zhang | d11ab16 | 2018-06-26 04:28:50 | [diff] [blame] | 74 | float multiplier = settings_.device_units_per_inch() / kMicronsPerMil; |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 75 | width *= multiplier; |
| 76 | height *= multiplier; |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 77 | } |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 78 | return gfx::Size(width, height); |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | PrintingContext::Result PrintingContextNoSystemDialog::UpdatePrinterSettings( |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 82 | bool external_preview, |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 83 | bool show_system_dialog, |
| 84 | int page_count) { |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 85 | DCHECK(!show_system_dialog); |
[email protected] | 0c5e302 | 2013-11-01 14:25:01 | [diff] [blame] | 86 | |
| 87 | if (settings_.dpi() == 0) |
| 88 | UseDefaultSettings(); |
| 89 | |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 90 | return OK; |
| 91 | } |
| 92 | |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 93 | PrintingContext::Result PrintingContextNoSystemDialog::NewDocument( |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 94 | const base::string16& document_name) { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 95 | DCHECK(!in_print_job_); |
| 96 | in_print_job_ = true; |
| 97 | |
| 98 | return OK; |
| 99 | } |
| 100 | |
| 101 | PrintingContext::Result PrintingContextNoSystemDialog::NewPage() { |
| 102 | if (abort_printing_) |
| 103 | return CANCEL; |
| 104 | DCHECK(in_print_job_); |
| 105 | |
| 106 | // Intentional No-op. |
| 107 | |
| 108 | return OK; |
| 109 | } |
| 110 | |
| 111 | PrintingContext::Result PrintingContextNoSystemDialog::PageDone() { |
| 112 | if (abort_printing_) |
| 113 | return CANCEL; |
| 114 | DCHECK(in_print_job_); |
| 115 | |
| 116 | // Intentional No-op. |
| 117 | |
| 118 | return OK; |
| 119 | } |
| 120 | |
| 121 | PrintingContext::Result PrintingContextNoSystemDialog::DocumentDone() { |
| 122 | if (abort_printing_) |
| 123 | return CANCEL; |
| 124 | DCHECK(in_print_job_); |
| 125 | |
| 126 | ResetSettings(); |
| 127 | return OK; |
| 128 | } |
| 129 | |
| 130 | void PrintingContextNoSystemDialog::Cancel() { |
| 131 | abort_printing_ = true; |
| 132 | in_print_job_ = false; |
| 133 | } |
| 134 | |
| 135 | void PrintingContextNoSystemDialog::ReleaseContext() { |
| 136 | // Intentional No-op. |
| 137 | } |
| 138 | |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 139 | printing::NativeDrawingContext PrintingContextNoSystemDialog::context() const { |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 140 | // Intentional No-op. |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 141 | return nullptr; |
[email protected] | ea5e81d9 | 2011-11-08 18:45:15 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace printing |
| 145 | |