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