blob: 84f9a9cbc3ca01fbac8ae4dc3c9bb8d891255c36 [file] [log] [blame]
[email protected]37a401bf2011-03-31 16:12:361// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]51e8d9352010-10-06 22:21:172// 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]1c23b4e82011-10-15 22:30:487#include "base/logging.h"
[email protected]37a401bf2011-03-31 16:12:368#include "base/values.h"
Brett Wilson65f951c2016-11-03 22:06:129#include "printing/features/features.h"
[email protected]1c23b4e82011-10-15 22:30:4810#include "printing/page_setup.h"
[email protected]61c10462011-11-15 07:56:1811#include "printing/page_size_margins.h"
[email protected]ae98e572014-05-14 23:52:5512#include "printing/print_job_constants.h"
[email protected]c95198b2014-06-12 16:56:5513#include "printing/print_settings_conversion.h"
[email protected]4c9054b2013-11-04 18:34:2914#include "printing/units.h"
[email protected]37a401bf2011-03-31 16:12:3615
[email protected]51e8d9352010-10-06 22:21:1716namespace printing {
17
[email protected]4c9054b2013-11-04 18:34:2918namespace {
19const float kCloudPrintMarginInch = 0.25;
20}
21
Vitaly Bukabd7c9812014-08-26 08:57:5422PrintingContext::PrintingContext(Delegate* delegate)
23 : delegate_(delegate),
[email protected]51e8d9352010-10-06 22:21:1724 in_print_job_(false),
Vitaly Bukabd7c9812014-08-26 08:57:5425 abort_printing_(false) {
26 CHECK(delegate_);
[email protected]51e8d9352010-10-06 22:21:1727}
28
29PrintingContext::~PrintingContext() {
30}
31
[email protected]1c23b4e82011-10-15 22:30:4832void PrintingContext::set_margin_type(MarginType type) {
33 DCHECK(type != CUSTOM_MARGINS);
[email protected]e5324b52013-10-29 03:16:3734 settings_.set_margin_type(type);
[email protected]1c23b4e82011-10-15 22:30:4835}
36
thestige85e6b62016-08-25 00:00:0637void PrintingContext::set_is_modifiable(bool is_modifiable) {
Wei Li02720a42017-10-25 22:06:4838 settings_.set_is_modifiable(is_modifiable);
thestige85e6b62016-08-25 00:00:0639#if defined(OS_WIN)
40 settings_.set_print_text_with_gdi(is_modifiable);
41#endif
42}
43
[email protected]51e8d9352010-10-06 22:21:1744void PrintingContext::ResetSettings() {
45 ReleaseContext();
[email protected]4993f342010-10-26 17:57:5246
[email protected]51e8d9352010-10-06 22:21:1747 settings_.Clear();
[email protected]4993f342010-10-26 17:57:5248
[email protected]51e8d9352010-10-06 22:21:1749 in_print_job_ = false;
[email protected]51e8d9352010-10-06 22:21:1750 abort_printing_ = false;
51}
52
[email protected]51e8d9352010-10-06 22:21:1753PrintingContext::Result PrintingContext::OnError() {
[email protected]5934bae2014-08-20 04:12:5454 Result result = abort_printing_ ? CANCEL : FAILED;
[email protected]51e8d9352010-10-06 22:21:1755 ResetSettings();
[email protected]5934bae2014-08-20 04:12:5456 return result;
[email protected]51e8d9352010-10-06 22:21:1757}
58
[email protected]2dafb8b2014-05-29 16:32:4859PrintingContext::Result PrintingContext::UsePdfSettings() {
dchengc3df9ba2016-04-07 23:09:3260 std::unique_ptr<base::DictionaryValue> pdf_settings(
61 new base::DictionaryValue);
[email protected]2dafb8b2014-05-29 16:32:4862 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);
rbpotter7993a7f2017-05-01 17:51:0269 pdf_settings->SetInteger(kSettingDpiHorizontal, kPointsPerInch);
70 pdf_settings->SetInteger(kSettingDpiVertical, kPointsPerInch);
[email protected]2dafb8b2014-05-29 16:32:4871 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);
tbarzic32b8f252015-03-03 07:55:5077 pdf_settings->SetBoolean(kSettingPrintWithExtension, false);
rbpotter769ffdf2016-10-26 00:53:5778 pdf_settings->SetInteger(kSettingScaleFactor, 100);
rbpotter0fab356022016-12-28 22:00:2379 pdf_settings->SetBoolean(kSettingRasterizePdf, false);
[email protected]c95198b2014-06-12 16:56:5580 return UpdatePrintSettings(*pdf_settings);
[email protected]2dafb8b2014-05-29 16:32:4881}
82
[email protected]55b23a02011-08-17 23:09:3683PrintingContext::Result PrintingContext::UpdatePrintSettings(
[email protected]c95198b2014-06-12 16:56:5584 const base::DictionaryValue& job_settings) {
[email protected]1c23b4e82011-10-15 22:30:4885 ResetSettings();
86
[email protected]c95198b2014-06-12 16:56:5587 if (!PrintSettingsFromJobSettings(job_settings, &settings_)) {
[email protected]e5324b52013-10-29 03:16:3788 NOTREACHED();
89 return OnError();
[email protected]1c23b4e82011-10-15 22:30:4890 }
91
[email protected]e5324b52013-10-29 03:16:3792 bool print_to_pdf = false;
93 bool is_cloud_dialog = false;
[email protected]b63d9af2013-11-20 08:52:3594 bool print_with_privet = false;
tbarzic32b8f252015-03-03 07:55:5095 bool print_with_extension = false;
[email protected]19d1c2d2013-01-14 00:59:4696
[email protected]e5324b52013-10-29 03:16:3797 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
[email protected]b63d9af2013-11-20 08:52:3598 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) ||
tbarzic32b8f252015-03-03 07:55:5099 !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet) ||
100 !job_settings.GetBoolean(kSettingPrintWithExtension,
101 &print_with_extension)) {
[email protected]e5324b52013-10-29 03:16:37102 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
tbarzic32b8f252015-03-03 07:55:50110 if (!open_in_external_preview &&
111 (print_to_pdf || print_to_cloud || is_cloud_dialog || print_with_privet ||
112 print_with_extension)) {
[email protected]4c9054b2013-11-04 18:34:29113 settings_.set_dpi(kDefaultPdfDpi);
[email protected]4c9054b2013-11-04 18:34:29114 gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
[email protected]72ddef92014-06-12 08:08:06115 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]c95198b2014-06-12 16:56:55119 deviceMicronsPerDeviceUnit,
[email protected]72ddef92014-06-12 08:08:06120 settings_.requested_media().size_microns.height() /
[email protected]c95198b2014-06-12 16:56:55121 deviceMicronsPerDeviceUnit);
[email protected]ae98e572014-05-14 23:52:55122 }
[email protected]4c9054b2013-11-04 18:34:29123 gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
[email protected]6da2cdb2013-11-20 13:13:53124 if (print_to_cloud || print_with_privet) {
[email protected]4c9054b2013-11-04 18:34:29125 paper_rect.Inset(
126 kCloudPrintMarginInch * settings_.device_units_per_inch(),
127 kCloudPrintMarginInch * settings_.device_units_per_inch());
128 }
[email protected]4c9054b2013-11-04 18:34:29129 settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
130 return OK;
131 }
132
vitalybuka92ab8ce2014-08-26 23:41:45133 bool show_system_dialog = false;
Brett Wilson65f951c2016-11-03 22:06:12134#if BUILDFLAG(ENABLE_BASIC_PRINTING)
vitalybuka95fa3c92015-05-05 03:03:32135 job_settings.GetBoolean(kSettingShowSystemDialog, &show_system_dialog);
thestig9b14c592016-01-14 19:30:03136#endif
vitalybuka92ab8ce2014-08-26 23:41:45137
vitalybuka95fa3c92015-05-05 03:03:32138 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]55b23a02011-08-17 23:09:36143}
144
[email protected]51e8d9352010-10-06 22:21:17145} // namespace printing