Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / printing_context.cc
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.h"
6
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "printing/buildflags/buildflags.h"
10 #include "printing/page_setup.h"
11 #include "printing/page_size_margins.h"
12 #include "printing/print_job_constants.h"
13 #include "printing/print_settings_conversion.h"
14 #include "printing/units.h"
15
16 namespace printing {
17
18 namespace {
19 const float kCloudPrintMarginInch = 0.25;
20 }
21
22 PrintingContext::PrintingContext(Delegate* delegate)
23     : delegate_(delegate),
24       in_print_job_(false),
25       abort_printing_(false) {
26   DCHECK(delegate_);
27 }
28
29 PrintingContext::~PrintingContext() = default;
30
31 void PrintingContext::set_margin_type(MarginType type) {
32   DCHECK(type != CUSTOM_MARGINS);
33   settings_.set_margin_type(type);
34 }
35
36 void PrintingContext::set_is_modifiable(bool is_modifiable) {
37   settings_.set_is_modifiable(is_modifiable);
38 #if defined(OS_WIN)
39   settings_.set_print_text_with_gdi(is_modifiable);
40 #endif
41 }
42
43 void PrintingContext::ResetSettings() {
44   ReleaseContext();
45
46   settings_.Clear();
47
48   in_print_job_ = false;
49   abort_printing_ = false;
50 }
51
52 PrintingContext::Result PrintingContext::OnError() {
53   Result result = abort_printing_ ? CANCEL : FAILED;
54   ResetSettings();
55   return result;
56 }
57
58 PrintingContext::Result PrintingContext::UsePdfSettings() {
59   base::Value pdf_settings(base::Value::Type::DICTIONARY);
60   pdf_settings.SetKey(kSettingHeaderFooterEnabled, base::Value(false));
61   pdf_settings.SetKey(kSettingShouldPrintBackgrounds, base::Value(false));
62   pdf_settings.SetKey(kSettingShouldPrintSelectionOnly, base::Value(false));
63   pdf_settings.SetKey(kSettingMarginsType, base::Value(printing::NO_MARGINS));
64   pdf_settings.SetKey(kSettingCollate, base::Value(true));
65   pdf_settings.SetKey(kSettingCopies, base::Value(1));
66   pdf_settings.SetKey(kSettingColor, base::Value(printing::COLOR));
67   pdf_settings.SetKey(kSettingDpiHorizontal, base::Value(kPointsPerInch));
68   pdf_settings.SetKey(kSettingDpiVertical, base::Value(kPointsPerInch));
69   pdf_settings.SetKey(kSettingDuplexMode, base::Value(printing::SIMPLEX));
70   pdf_settings.SetKey(kSettingLandscape, base::Value(false));
71   pdf_settings.SetKey(kSettingDeviceName, base::Value(""));
72   pdf_settings.SetKey(kSettingPrintToPDF, base::Value(true));
73   pdf_settings.SetKey(kSettingCloudPrintDialog, base::Value(false));
74   pdf_settings.SetKey(kSettingPrintWithPrivet, base::Value(false));
75   pdf_settings.SetKey(kSettingPrintWithExtension, base::Value(false));
76   pdf_settings.SetKey(kSettingScaleFactor, base::Value(100));
77   pdf_settings.SetKey(kSettingRasterizePdf, base::Value(false));
78   pdf_settings.SetKey(kSettingPagesPerSheet, base::Value(1));
79   return UpdatePrintSettings(std::move(pdf_settings));
80 }
81
82 PrintingContext::Result PrintingContext::UpdatePrintSettings(
83     base::Value job_settings) {
84   ResetSettings();
85
86   if (!PrintSettingsFromJobSettings(job_settings, &settings_)) {
87     NOTREACHED();
88     return OnError();
89   }
90
91   base::Optional<bool> print_to_pdf_opt =
92       job_settings.FindBoolKey(kSettingPrintToPDF);
93   base::Optional<bool> is_cloud_dialog_opt =
94       job_settings.FindBoolKey(kSettingCloudPrintDialog);
95   base::Optional<bool> print_with_privet_opt =
96       job_settings.FindBoolKey(kSettingPrintWithPrivet);
97   base::Optional<bool> print_with_extension_opt =
98       job_settings.FindBoolKey(kSettingPrintWithExtension);
99
100   if (!print_to_pdf_opt || !is_cloud_dialog_opt || !print_with_privet_opt ||
101       !print_with_extension_opt) {
102     NOTREACHED();
103     return OnError();
104   }
105
106   bool print_to_pdf = print_to_pdf_opt.value();
107   bool is_cloud_dialog = is_cloud_dialog_opt.value();
108   bool print_with_privet = print_with_privet_opt.value();
109   bool print_with_extension = print_with_extension_opt.value();
110
111   bool print_to_cloud = job_settings.FindKey(kSettingCloudPrintId) != nullptr;
112   bool open_in_external_preview =
113       job_settings.FindKey(kSettingOpenPDFInPreview) != nullptr;
114
115   if (!open_in_external_preview &&
116       (print_to_pdf || print_to_cloud || is_cloud_dialog || print_with_privet ||
117        print_with_extension)) {
118     settings_.set_dpi(kDefaultPdfDpi);
119     gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
120     if (!settings_.requested_media().size_microns.IsEmpty()) {
121       float device_microns_per_device_unit =
122           static_cast<float>(kMicronsPerInch) /
123           settings_.device_units_per_inch();
124       paper_size = gfx::Size(settings_.requested_media().size_microns.width() /
125                                  device_microns_per_device_unit,
126                              settings_.requested_media().size_microns.height() /
127                                  device_microns_per_device_unit);
128     }
129     gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
130     if (print_to_cloud || print_with_privet) {
131       paper_rect.Inset(
132           kCloudPrintMarginInch * settings_.device_units_per_inch(),
133           kCloudPrintMarginInch * settings_.device_units_per_inch());
134     }
135     settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
136     return OK;
137   }
138
139   return UpdatePrinterSettings(
140       open_in_external_preview,
141       job_settings.FindBoolKey(kSettingShowSystemDialog).value_or(false),
142       job_settings.FindIntKey(kSettingPreviewPageCount).value_or(0));
143 }
144
145 #if defined(OS_CHROMEOS)
146 PrintingContext::Result PrintingContext::UpdatePrintSettingsFromPOD(
147     std::unique_ptr<PrintSettings> job_settings) {
148   ResetSettings();
149   settings_ = *job_settings;
150
151   return UpdatePrinterSettings(false /* external_preview */,
152                                false /* show_system_dialog */,
153                                0 /* page_count is only used on Android */);
154 }
155 #endif
156
157 }  // namespace printing