[email protected] | 37a401bf | 2011-03-31 16:12:36 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 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.h" |
| 6 | |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 37a401bf | 2011-03-31 16:12:36 | [diff] [blame] | 8 | #include "base/values.h" |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 9 | #include "printing/features/features.h" |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 10 | #include "printing/page_setup.h" |
[email protected] | 61c1046 | 2011-11-15 07:56:18 | [diff] [blame] | 11 | #include "printing/page_size_margins.h" |
[email protected] | ae98e57 | 2014-05-14 23:52:55 | [diff] [blame] | 12 | #include "printing/print_job_constants.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 13 | #include "printing/print_settings_conversion.h" |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 14 | #include "printing/units.h" |
[email protected] | 37a401bf | 2011-03-31 16:12:36 | [diff] [blame] | 15 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 16 | namespace printing { |
| 17 | |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 18 | namespace { |
| 19 | const float kCloudPrintMarginInch = 0.25; |
| 20 | } |
| 21 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 22 | PrintingContext::PrintingContext(Delegate* delegate) |
| 23 | : delegate_(delegate), |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 24 | in_print_job_(false), |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 25 | abort_printing_(false) { |
| 26 | CHECK(delegate_); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | PrintingContext::~PrintingContext() { |
| 30 | } |
| 31 | |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 32 | void PrintingContext::set_margin_type(MarginType type) { |
| 33 | DCHECK(type != CUSTOM_MARGINS); |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 34 | settings_.set_margin_type(type); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 35 | } |
| 36 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 37 | void PrintingContext::set_is_modifiable(bool is_modifiable) { |
Wei Li | 02720a4 | 2017-10-25 22:06:48 | [diff] [blame] | 38 | settings_.set_is_modifiable(is_modifiable); |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 39 | #if defined(OS_WIN) |
| 40 | settings_.set_print_text_with_gdi(is_modifiable); |
| 41 | #endif |
| 42 | } |
| 43 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 44 | void PrintingContext::ResetSettings() { |
| 45 | ReleaseContext(); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 46 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 47 | settings_.Clear(); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 48 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 49 | in_print_job_ = false; |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 50 | abort_printing_ = false; |
| 51 | } |
| 52 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 53 | PrintingContext::Result PrintingContext::OnError() { |
[email protected] | 5934bae | 2014-08-20 04:12:54 | [diff] [blame] | 54 | Result result = abort_printing_ ? CANCEL : FAILED; |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 55 | ResetSettings(); |
[email protected] | 5934bae | 2014-08-20 04:12:54 | [diff] [blame] | 56 | return result; |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 59 | PrintingContext::Result PrintingContext::UsePdfSettings() { |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 60 | std::unique_ptr<base::DictionaryValue> pdf_settings( |
| 61 | new base::DictionaryValue); |
[email protected] | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 62 | pdf_settings->SetBoolean(kSettingHeaderFooterEnabled, false); |
| 63 | pdf_settings->SetBoolean(kSettingShouldPrintBackgrounds, false); |
| 64 | pdf_settings->SetBoolean(kSettingShouldPrintSelectionOnly, false); |
| 65 | pdf_settings->SetInteger(kSettingMarginsType, printing::NO_MARGINS); |
| 66 | pdf_settings->SetBoolean(kSettingCollate, true); |
| 67 | pdf_settings->SetInteger(kSettingCopies, 1); |
| 68 | pdf_settings->SetInteger(kSettingColor, printing::COLOR); |
rbpotter | 7993a7f | 2017-05-01 17:51:02 | [diff] [blame] | 69 | pdf_settings->SetInteger(kSettingDpiHorizontal, kPointsPerInch); |
| 70 | pdf_settings->SetInteger(kSettingDpiVertical, kPointsPerInch); |
[email protected] | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 71 | pdf_settings->SetInteger(kSettingDuplexMode, printing::SIMPLEX); |
| 72 | pdf_settings->SetBoolean(kSettingLandscape, false); |
| 73 | pdf_settings->SetString(kSettingDeviceName, ""); |
| 74 | pdf_settings->SetBoolean(kSettingPrintToPDF, true); |
| 75 | pdf_settings->SetBoolean(kSettingCloudPrintDialog, false); |
| 76 | pdf_settings->SetBoolean(kSettingPrintWithPrivet, false); |
tbarzic | 32b8f25 | 2015-03-03 07:55:50 | [diff] [blame] | 77 | pdf_settings->SetBoolean(kSettingPrintWithExtension, false); |
rbpotter | 769ffdf | 2016-10-26 00:53:57 | [diff] [blame] | 78 | pdf_settings->SetInteger(kSettingScaleFactor, 100); |
rbpotter | 0fab35602 | 2016-12-28 22:00:23 | [diff] [blame] | 79 | pdf_settings->SetBoolean(kSettingRasterizePdf, false); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 80 | return UpdatePrintSettings(*pdf_settings); |
[email protected] | 2dafb8b | 2014-05-29 16:32:48 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 83 | PrintingContext::Result PrintingContext::UpdatePrintSettings( |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 84 | const base::DictionaryValue& job_settings) { |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 85 | ResetSettings(); |
| 86 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 87 | if (!PrintSettingsFromJobSettings(job_settings, &settings_)) { |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 88 | NOTREACHED(); |
| 89 | return OnError(); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 92 | bool print_to_pdf = false; |
| 93 | bool is_cloud_dialog = false; |
[email protected] | b63d9af | 2013-11-20 08:52:35 | [diff] [blame] | 94 | bool print_with_privet = false; |
tbarzic | 32b8f25 | 2015-03-03 07:55:50 | [diff] [blame] | 95 | bool print_with_extension = false; |
[email protected] | 19d1c2d | 2013-01-14 00:59:46 | [diff] [blame] | 96 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 97 | if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || |
[email protected] | b63d9af | 2013-11-20 08:52:35 | [diff] [blame] | 98 | !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) || |
tbarzic | 32b8f25 | 2015-03-03 07:55:50 | [diff] [blame] | 99 | !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet) || |
| 100 | !job_settings.GetBoolean(kSettingPrintWithExtension, |
| 101 | &print_with_extension)) { |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 102 | NOTREACHED(); |
| 103 | return OnError(); |
| 104 | } |
| 105 | |
| 106 | bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); |
| 107 | bool open_in_external_preview = |
| 108 | job_settings.HasKey(kSettingOpenPDFInPreview); |
| 109 | |
tbarzic | 32b8f25 | 2015-03-03 07:55:50 | [diff] [blame] | 110 | if (!open_in_external_preview && |
| 111 | (print_to_pdf || print_to_cloud || is_cloud_dialog || print_with_privet || |
| 112 | print_with_extension)) { |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 113 | settings_.set_dpi(kDefaultPdfDpi); |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 114 | gfx::Size paper_size(GetPdfPaperSizeDeviceUnits()); |
[email protected] | 72ddef9 | 2014-06-12 08:08:06 | [diff] [blame] | 115 | if (!settings_.requested_media().size_microns.IsEmpty()) { |
| 116 | float deviceMicronsPerDeviceUnit = |
| 117 | (kHundrethsMMPerInch * 10.0f) / settings_.device_units_per_inch(); |
| 118 | paper_size = gfx::Size(settings_.requested_media().size_microns.width() / |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 119 | deviceMicronsPerDeviceUnit, |
[email protected] | 72ddef9 | 2014-06-12 08:08:06 | [diff] [blame] | 120 | settings_.requested_media().size_microns.height() / |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 121 | deviceMicronsPerDeviceUnit); |
[email protected] | ae98e57 | 2014-05-14 23:52:55 | [diff] [blame] | 122 | } |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 123 | gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height()); |
[email protected] | 6da2cdb | 2013-11-20 13:13:53 | [diff] [blame] | 124 | if (print_to_cloud || print_with_privet) { |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 125 | paper_rect.Inset( |
| 126 | kCloudPrintMarginInch * settings_.device_units_per_inch(), |
| 127 | kCloudPrintMarginInch * settings_.device_units_per_inch()); |
| 128 | } |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 129 | settings_.SetPrinterPrintableArea(paper_size, paper_rect, true); |
| 130 | return OK; |
| 131 | } |
| 132 | |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 133 | bool show_system_dialog = false; |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 134 | #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 135 | job_settings.GetBoolean(kSettingShowSystemDialog, &show_system_dialog); |
thestig | 9b14c59 | 2016-01-14 19:30:03 | [diff] [blame] | 136 | #endif |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 137 | |
vitalybuka | 95fa3c9 | 2015-05-05 03:03:32 | [diff] [blame] | 138 | int page_count = 0; |
| 139 | job_settings.GetInteger(kSettingPreviewPageCount, &page_count); |
| 140 | |
| 141 | return UpdatePrinterSettings(open_in_external_preview, show_system_dialog, |
| 142 | page_count); |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 145 | } // namespace printing |