[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 1 | // Copyright 2014 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/print_settings_conversion.h" |
| 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <cmath> |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 11 | #include <memory> |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 12 | #include <string> |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 13 | #include <utility> |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 14 | |
Daniel Hosseinian | fc4d825 | 2021-09-03 21:44:16 | [diff] [blame^] | 15 | #include "base/containers/contains.h" |
| 16 | #include "base/containers/fixed_flat_set.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
Daniel Hosseinian | fc4d825 | 2021-09-03 21:44:16 | [diff] [blame^] | 18 | #include "base/strings/string_piece.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
| 20 | #include "base/time/time.h" |
| 21 | #include "base/values.h" |
Wei Li | 02720a4 | 2017-10-25 22:06:48 | [diff] [blame] | 22 | #include "build/build_config.h" |
Yuta Hijikata | 2f66389 | 2020-10-06 10:34:22 | [diff] [blame] | 23 | #include "build/chromeos_buildflags.h" |
Julie Jeongeun Kim | e454f260 | 2020-04-30 05:20:07 | [diff] [blame] | 24 | #include "printing/mojom/print.mojom.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 25 | #include "printing/print_job_constants.h" |
| 26 | #include "printing/print_settings.h" |
| 27 | #include "printing/units.h" |
| 28 | |
| 29 | namespace printing { |
| 30 | |
| 31 | namespace { |
| 32 | |
Daniel Hosseinian | 3553e27 | 2021-04-24 00:51:18 | [diff] [blame] | 33 | // Note: If this code crashes, then the caller has passed in invalid `settings`. |
Lei Zhang | c8553b4 | 2019-08-07 19:13:46 | [diff] [blame] | 34 | // Fix the caller, instead of trying to avoid the crash here. |
| 35 | PageMargins GetCustomMarginsFromJobSettings(const base::Value& settings) { |
| 36 | PageMargins margins_in_points; |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 37 | const base::Value* custom_margins = settings.FindKey(kSettingMarginsCustom); |
Lei Zhang | c8553b4 | 2019-08-07 19:13:46 | [diff] [blame] | 38 | margins_in_points.top = custom_margins->FindIntKey(kSettingMarginTop).value(); |
| 39 | margins_in_points.bottom = |
| 40 | custom_margins->FindIntKey(kSettingMarginBottom).value(); |
| 41 | margins_in_points.left = |
| 42 | custom_margins->FindIntKey(kSettingMarginLeft).value(); |
| 43 | margins_in_points.right = |
| 44 | custom_margins->FindIntKey(kSettingMarginRight).value(); |
| 45 | return margins_in_points; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void SetMarginsToJobSettings(const std::string& json_path, |
| 49 | const PageMargins& margins, |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 50 | base::Value& job_settings) { |
| 51 | base::Value dict(base::Value::Type::DICTIONARY); |
| 52 | dict.SetIntKey(kSettingMarginTop, margins.top); |
| 53 | dict.SetIntKey(kSettingMarginBottom, margins.bottom); |
| 54 | dict.SetIntKey(kSettingMarginLeft, margins.left); |
| 55 | dict.SetIntKey(kSettingMarginRight, margins.right); |
| 56 | job_settings.SetKey(json_path, std::move(dict)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void SetSizeToJobSettings(const std::string& json_path, |
| 60 | const gfx::Size& size, |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 61 | base::Value& job_settings) { |
| 62 | base::Value dict(base::Value::Type::DICTIONARY); |
| 63 | dict.SetIntKey("width", size.width()); |
| 64 | dict.SetIntKey("height", size.height()); |
| 65 | job_settings.SetKey(json_path, std::move(dict)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void SetRectToJobSettings(const std::string& json_path, |
| 69 | const gfx::Rect& rect, |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 70 | base::Value& job_settings) { |
| 71 | base::Value dict(base::Value::Type::DICTIONARY); |
| 72 | dict.SetIntKey("x", rect.x()); |
| 73 | dict.SetIntKey("y", rect.y()); |
| 74 | dict.SetIntKey("width", rect.width()); |
| 75 | dict.SetIntKey("height", rect.height()); |
| 76 | job_settings.SetKey(json_path, std::move(dict)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
Jesse Schettler | a092a99c | 2019-11-06 15:21:27 | [diff] [blame] | 81 | PageRanges GetPageRangesFromJobSettings(const base::Value& job_settings) { |
| 82 | PageRanges page_ranges; |
| 83 | const base::Value* page_range_array = |
| 84 | job_settings.FindListKey(kSettingPageRange); |
| 85 | if (page_range_array) { |
| 86 | for (const base::Value& page_range : page_range_array->GetList()) { |
| 87 | if (!page_range.is_dict()) |
| 88 | continue; |
| 89 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 90 | absl::optional<int> from = page_range.FindIntKey(kSettingPageRangeFrom); |
| 91 | absl::optional<int> to = page_range.FindIntKey(kSettingPageRangeTo); |
Jesse Schettler | a092a99c | 2019-11-06 15:21:27 | [diff] [blame] | 92 | if (!from.has_value() || !to.has_value()) |
| 93 | continue; |
| 94 | |
| 95 | // Page numbers are 1-based in the dictionary. |
| 96 | // Page numbers are 0-based for the printing context. |
Peter Kasting | 2bb51ca | 2021-06-09 06:51:39 | [diff] [blame] | 97 | page_ranges.push_back(PageRange{static_cast<uint32_t>(from.value() - 1), |
| 98 | static_cast<uint32_t>(to.value() - 1)}); |
Jesse Schettler | a092a99c | 2019-11-06 15:21:27 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | return page_ranges; |
| 102 | } |
| 103 | |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 104 | std::unique_ptr<PrintSettings> PrintSettingsFromJobSettings( |
| 105 | const base::Value& job_settings) { |
| 106 | auto settings = std::make_unique<PrintSettings>(); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 107 | absl::optional<bool> display_header_footer = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 108 | job_settings.FindBoolKey(kSettingHeaderFooterEnabled); |
| 109 | if (!display_header_footer.has_value()) |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 110 | return nullptr; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 111 | |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 112 | settings->set_display_header_footer(display_header_footer.value()); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 113 | if (settings->display_header_footer()) { |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 114 | const std::string* title = |
| 115 | job_settings.FindStringKey(kSettingHeaderFooterTitle); |
| 116 | const std::string* url = |
| 117 | job_settings.FindStringKey(kSettingHeaderFooterURL); |
| 118 | if (!title || !url) |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 119 | return nullptr; |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 120 | |
| 121 | settings->set_title(base::UTF8ToUTF16(*title)); |
| 122 | settings->set_url(base::UTF8ToUTF16(*url)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 123 | } |
| 124 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 125 | absl::optional<bool> backgrounds = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 126 | job_settings.FindBoolKey(kSettingShouldPrintBackgrounds); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 127 | absl::optional<bool> selection_only = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 128 | job_settings.FindBoolKey(kSettingShouldPrintSelectionOnly); |
| 129 | if (!backgrounds.has_value() || !selection_only.has_value()) |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 130 | return nullptr; |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 131 | |
| 132 | settings->set_should_print_backgrounds(backgrounds.value()); |
| 133 | settings->set_selection_only(selection_only.value()); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 134 | |
| 135 | PrintSettings::RequestedMedia requested_media; |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 136 | const base::Value* media_size_value = job_settings.FindKeyOfType( |
| 137 | kSettingMediaSize, base::Value::Type::DICTIONARY); |
| 138 | if (media_size_value) { |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 139 | absl::optional<int> width_microns = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 140 | media_size_value->FindIntKey(kSettingMediaSizeWidthMicrons); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 141 | absl::optional<int> height_microns = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 142 | media_size_value->FindIntKey(kSettingMediaSizeHeightMicrons); |
| 143 | if (width_microns.has_value() && height_microns.has_value()) { |
| 144 | requested_media.size_microns = |
| 145 | gfx::Size(width_microns.value(), height_microns.value()); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 146 | } |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 147 | |
| 148 | const std::string* vendor_id = |
| 149 | media_size_value->FindStringKey(kSettingMediaSizeVendorId); |
| 150 | if (vendor_id && !vendor_id->empty()) |
| 151 | requested_media.vendor_id = *vendor_id; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 152 | } |
| 153 | settings->set_requested_media(requested_media); |
| 154 | |
Julie Jeongeun Kim | cb00d1ab | 2020-08-14 07:43:32 | [diff] [blame] | 155 | mojom::MarginType margin_type = static_cast<mojom::MarginType>( |
| 156 | job_settings.FindIntKey(kSettingMarginsType) |
| 157 | .value_or(static_cast<int>(mojom::MarginType::kDefaultMargins))); |
| 158 | if (margin_type != mojom::MarginType::kDefaultMargins && |
| 159 | margin_type != mojom::MarginType::kNoMargins && |
| 160 | margin_type != mojom::MarginType::kCustomMargins && |
| 161 | margin_type != mojom::MarginType::kPrintableAreaMargins) { |
| 162 | margin_type = mojom::MarginType::kDefaultMargins; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 163 | } |
Julie Jeongeun Kim | cb00d1ab | 2020-08-14 07:43:32 | [diff] [blame] | 164 | settings->set_margin_type(margin_type); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 165 | |
Julie Jeongeun Kim | cb00d1ab | 2020-08-14 07:43:32 | [diff] [blame] | 166 | if (margin_type == mojom::MarginType::kCustomMargins) |
Lei Zhang | c8553b4 | 2019-08-07 19:13:46 | [diff] [blame] | 167 | settings->SetCustomMargins(GetCustomMarginsFromJobSettings(job_settings)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 168 | |
Jesse Schettler | a092a99c | 2019-11-06 15:21:27 | [diff] [blame] | 169 | settings->set_ranges(GetPageRangesFromJobSettings(job_settings)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 170 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 171 | absl::optional<bool> collate = job_settings.FindBoolKey(kSettingCollate); |
| 172 | absl::optional<int> copies = job_settings.FindIntKey(kSettingCopies); |
| 173 | absl::optional<int> color = job_settings.FindIntKey(kSettingColor); |
| 174 | absl::optional<int> duplex_mode = job_settings.FindIntKey(kSettingDuplexMode); |
| 175 | absl::optional<bool> landscape = job_settings.FindBoolKey(kSettingLandscape); |
| 176 | absl::optional<int> scale_factor = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 177 | job_settings.FindIntKey(kSettingScaleFactor); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 178 | absl::optional<bool> rasterize_pdf = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 179 | job_settings.FindBoolKey(kSettingRasterizePdf); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 180 | absl::optional<int> pages_per_sheet = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 181 | job_settings.FindIntKey(kSettingPagesPerSheet); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 182 | |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 183 | if (!collate.has_value() || !copies.has_value() || !color.has_value() || |
Lei Zhang | dfe53f9 | 2019-10-11 05:41:07 | [diff] [blame] | 184 | !duplex_mode.has_value() || !landscape.has_value() || |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 185 | !scale_factor.has_value() || !rasterize_pdf.has_value() || |
| 186 | !pages_per_sheet.has_value()) { |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 187 | return nullptr; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 188 | } |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 189 | |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 190 | absl::optional<int> dpi_horizontal = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 191 | job_settings.FindIntKey(kSettingDpiHorizontal); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 192 | absl::optional<int> dpi_vertical = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 193 | job_settings.FindIntKey(kSettingDpiVertical); |
| 194 | if (!dpi_horizontal.has_value() || !dpi_vertical.has_value()) |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 195 | return nullptr; |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 196 | settings->set_dpi_xy(dpi_horizontal.value(), dpi_vertical.value()); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 197 | |
Alan Screen | dce2e585b | 2021-08-12 21:10:47 | [diff] [blame] | 198 | absl::optional<int> rasterize_pdf_dpi = |
| 199 | job_settings.FindIntKey(kSettingRasterizePdfDpi); |
| 200 | if (rasterize_pdf_dpi.has_value()) |
| 201 | settings->set_rasterize_pdf_dpi(rasterize_pdf_dpi.value()); |
| 202 | |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 203 | settings->set_collate(collate.value()); |
| 204 | settings->set_copies(copies.value()); |
| 205 | settings->SetOrientation(landscape.value()); |
Lei Zhang | dfe53f9 | 2019-10-11 05:41:07 | [diff] [blame] | 206 | settings->set_device_name( |
| 207 | base::UTF8ToUTF16(*job_settings.FindStringKey(kSettingDeviceName))); |
Julie Jeongeun Kim | e454f260 | 2020-04-30 05:20:07 | [diff] [blame] | 208 | settings->set_duplex_mode( |
| 209 | static_cast<mojom::DuplexMode>(duplex_mode.value())); |
Alan Screen | 3fb63bf | 2020-08-19 00:11:23 | [diff] [blame] | 210 | settings->set_color(static_cast<mojom::ColorModel>(color.value())); |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 211 | settings->set_scale_factor(static_cast<double>(scale_factor.value()) / 100.0); |
| 212 | settings->set_rasterize_pdf(rasterize_pdf.value()); |
| 213 | settings->set_pages_per_sheet(pages_per_sheet.value()); |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 214 | absl::optional<bool> is_modifiable = |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 215 | job_settings.FindBoolKey(kSettingPreviewModifiable); |
| 216 | if (is_modifiable.has_value()) { |
| 217 | settings->set_is_modifiable(is_modifiable.value()); |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 218 | #if defined(OS_WIN) |
Vladislav Kuzkokov | 490ab0c | 2019-01-16 11:21:57 | [diff] [blame] | 219 | settings->set_print_text_with_gdi(is_modifiable.value()); |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 220 | #endif |
Wei Li | 02720a4 | 2017-10-25 22:06:48 | [diff] [blame] | 221 | } |
Vladislav Kuzkokov | 48b1500 | 2019-01-30 17:13:35 | [diff] [blame] | 222 | |
Pranav Batra | 32b98bfc | 2021-05-12 00:17:02 | [diff] [blame] | 223 | #if defined(OS_CHROMEOS) || (defined(OS_LINUX) && defined(USE_CUPS)) |
Daniel Hosseinian | c3db7f4c | 2020-06-25 02:09:52 | [diff] [blame] | 224 | const base::Value* advanced_settings = |
| 225 | job_settings.FindDictKey(kSettingAdvancedSettings); |
| 226 | if (advanced_settings) { |
Daniel Hosseinian | fc4d825 | 2021-09-03 21:44:16 | [diff] [blame^] | 227 | for (const auto item : advanced_settings->DictItems()) { |
| 228 | static constexpr auto kNonJobAttributes = |
| 229 | base::MakeFixedFlatSet<base::StringPiece>( |
| 230 | {"printer-info", "printer-make-and-model", "system_driverinfo"}); |
| 231 | if (!base::Contains(kNonJobAttributes, item.first)) |
| 232 | settings->advanced_settings().emplace(item.first, item.second.Clone()); |
| 233 | } |
Daniel Hosseinian | c3db7f4c | 2020-06-25 02:09:52 | [diff] [blame] | 234 | } |
Pranav Batra | 32b98bfc | 2021-05-12 00:17:02 | [diff] [blame] | 235 | #endif // defined(OS_CHROMEOS) || (defined(OS_LINUX) && defined(USE_CUPS)) |
Daniel Hosseinian | c3db7f4c | 2020-06-25 02:09:52 | [diff] [blame] | 236 | |
Pranav Batra | 32b98bfc | 2021-05-12 00:17:02 | [diff] [blame] | 237 | #if defined(OS_CHROMEOS) |
Vladislav Kuzkokov | 48b1500 | 2019-01-30 17:13:35 | [diff] [blame] | 238 | bool send_user_info = |
| 239 | job_settings.FindBoolKey(kSettingSendUserInfo).value_or(false); |
| 240 | settings->set_send_user_info(send_user_info); |
| 241 | if (send_user_info) { |
| 242 | const std::string* username = job_settings.FindStringKey(kSettingUsername); |
| 243 | if (username) |
| 244 | settings->set_username(*username); |
| 245 | } |
Nikita Podguzov | c1bf3f8 | 2019-03-19 15:23:23 | [diff] [blame] | 246 | |
| 247 | const std::string* pin_value = job_settings.FindStringKey(kSettingPinValue); |
| 248 | if (pin_value) |
| 249 | settings->set_pin_value(*pin_value); |
Pranav Batra | 32b98bfc | 2021-05-12 00:17:02 | [diff] [blame] | 250 | #endif // defined(OS_CHROMEOS) |
Vladislav Kuzkokov | 48b1500 | 2019-01-30 17:13:35 | [diff] [blame] | 251 | |
Pranav Batra | 636e204 | 2020-09-03 03:35:05 | [diff] [blame] | 252 | return settings; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 253 | } |
| 254 | |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 255 | base::Value PrintSettingsToJobSettingsDebug(const PrintSettings& settings) { |
| 256 | base::Value job_settings(base::Value::Type::DICTIONARY); |
| 257 | |
| 258 | job_settings.SetBoolKey(kSettingHeaderFooterEnabled, |
| 259 | settings.display_header_footer()); |
| 260 | job_settings.SetStringKey(kSettingHeaderFooterTitle, settings.title()); |
| 261 | job_settings.SetStringKey(kSettingHeaderFooterURL, settings.url()); |
| 262 | job_settings.SetBoolKey(kSettingShouldPrintBackgrounds, |
| 263 | settings.should_print_backgrounds()); |
| 264 | job_settings.SetBoolKey(kSettingShouldPrintSelectionOnly, |
| 265 | settings.selection_only()); |
| 266 | job_settings.SetIntKey(kSettingMarginsType, |
| 267 | static_cast<int>(settings.margin_type())); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 268 | if (!settings.ranges().empty()) { |
Song Fangzhen | 6ff811d | 2021-06-29 08:35:43 | [diff] [blame] | 269 | base::ListValue page_range_array; |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 270 | for (const auto& range : settings.ranges()) { |
| 271 | auto dict = std::make_unique<base::Value>(base::Value::Type::DICTIONARY); |
| 272 | dict->SetIntKey(kSettingPageRangeFrom, range.from + 1); |
| 273 | dict->SetIntKey(kSettingPageRangeTo, range.to + 1); |
Song Fangzhen | 6ff811d | 2021-06-29 08:35:43 | [diff] [blame] | 274 | page_range_array.Append(std::move(dict)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 275 | } |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 276 | job_settings.SetKey(kSettingPageRange, std::move(page_range_array)); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 277 | } |
| 278 | |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 279 | job_settings.SetBoolKey(kSettingCollate, settings.collate()); |
| 280 | job_settings.SetIntKey(kSettingCopies, settings.copies()); |
| 281 | job_settings.SetIntKey(kSettingColor, static_cast<int>(settings.color())); |
| 282 | job_settings.SetIntKey(kSettingDuplexMode, |
| 283 | static_cast<int>(settings.duplex_mode())); |
| 284 | job_settings.SetBoolKey(kSettingLandscape, settings.landscape()); |
| 285 | job_settings.SetStringKey(kSettingDeviceName, settings.device_name()); |
| 286 | job_settings.SetIntKey(kSettingDpiHorizontal, settings.dpi_horizontal()); |
| 287 | job_settings.SetIntKey(kSettingDpiVertical, settings.dpi_vertical()); |
| 288 | job_settings.SetIntKey( |
| 289 | kSettingScaleFactor, |
| 290 | static_cast<int>((settings.scale_factor() * 100.0) + 0.5)); |
| 291 | job_settings.SetBoolKey(kSettingRasterizePdf, settings.rasterize_pdf()); |
| 292 | job_settings.SetIntKey(kSettingPagesPerSheet, settings.pages_per_sheet()); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 293 | |
| 294 | // Following values are not read form JSON by InitSettings, so do not have |
| 295 | // common public constants. So just serialize in "debug" section. |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 296 | base::Value debug(base::Value::Type::DICTIONARY); |
| 297 | debug.SetIntKey("dpi", settings.dpi()); |
| 298 | debug.SetIntKey("deviceUnitsPerInch", settings.device_units_per_inch()); |
| 299 | debug.SetBoolKey("support_alpha_blend", settings.should_print_backgrounds()); |
| 300 | debug.SetStringKey("media_vendor_id", settings.requested_media().vendor_id); |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 301 | SetSizeToJobSettings("media_size", settings.requested_media().size_microns, |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 302 | debug); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 303 | SetMarginsToJobSettings("requested_custom_margins_in_points", |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 304 | settings.requested_custom_margins_in_points(), debug); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 305 | const PageSetup& page_setup = settings.page_setup_device_units(); |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 306 | SetMarginsToJobSettings("effective_margins", page_setup.effective_margins(), |
Alan Screen | 3c25f8e | 2021-08-04 17:41:51 | [diff] [blame] | 307 | debug); |
| 308 | SetSizeToJobSettings("physical_size", page_setup.physical_size(), debug); |
| 309 | SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug); |
| 310 | SetRectToJobSettings("content_area", page_setup.content_area(), debug); |
| 311 | SetRectToJobSettings("printable_area", page_setup.printable_area(), debug); |
| 312 | job_settings.SetKey("debug", std::move(debug)); |
| 313 | |
| 314 | return job_settings; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } // namespace printing |