1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PRINTING_PRINT_SETTINGS_H_
6 #define PRINTING_PRINT_SETTINGS_H_
13 #include "base/component_export.h"
14 #include "build/build_config.h"
15 #include "printing/buildflags/buildflags.h"
16 #include "printing/mojom/print.mojom.h"
17 #include "printing/page_range.h"
18 #include "printing/page_setup.h"
19 #include "printing/print_job_constants.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/geometry/size.h"
24 #if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
25 #include "base/values.h"
28 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
31 #include "base/values.h"
32 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
34 #if BUILDFLAG(IS_CHROMEOS)
35 #include "chromeos/crosapi/mojom/local_printer.mojom.h"
36 #endif // BUILDFLAG(IS_CHROMEOS)
40 #if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
43 inline constexpr char kMacSystemPrintDialogDataDestinationType[] =
45 inline constexpr char kMacSystemPrintDialogDataDestinationFormat[] =
47 inline constexpr char kMacSystemPrintDialogDataDestinationLocation[] =
48 "destination_location";
49 inline constexpr char kMacSystemPrintDialogDataPageFormat[] = "page_format";
50 inline constexpr char kMacSystemPrintDialogDataPrintSettings[] =
52 #endif // BUILDFLAG(IS_MAC)
54 #if BUILDFLAG(IS_LINUX)
55 inline constexpr char kLinuxSystemPrintDialogDataPrinter[] = "printer_name";
56 inline constexpr char kLinuxSystemPrintDialogDataPrintSettings[] =
58 inline constexpr char kLinuxSystemPrintDialogDataPageSetup[] = "page_setup";
59 #endif // BUILDFLAG(IS_LINUX)
61 #endif // BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
63 // Convert from `color_mode` into a `color_model`. An invalid `color_mode`
64 // will give a result of `mojom::ColorModel::kUnknownColorModel`.
65 COMPONENT_EXPORT(PRINTING)
66 mojom::ColorModel ColorModeToColorModel(int color_mode);
68 // Returns true if `color_model` is color and false if it is B&W. Callers
69 // are not supposed to pass in `mojom::ColorModel::kUnknownColorModel`, but
70 // if they do then the result will be absl::nullopt.
71 COMPONENT_EXPORT(PRINTING)
72 absl::optional<bool> IsColorModelSelected(mojom::ColorModel color_model);
74 #if BUILDFLAG(USE_CUPS)
75 // Get the color model setting name and value for the `color_model`.
76 COMPONENT_EXPORT(PRINTING)
77 void GetColorModelForModel(mojom::ColorModel color_model,
78 std::string* color_setting_name,
79 std::string* color_value);
80 #endif // BUILDFLAG(USE_CUPS)
82 #if BUILDFLAG(USE_CUPS_IPP)
83 // Convert from `color_model` to a print-color-mode value from PWG 5100.13.
84 COMPONENT_EXPORT(PRINTING)
85 std::string GetIppColorModelForModel(mojom::ColorModel color_model);
86 #endif // BUILDFLAG(USE_CUPS_IPP)
88 class COMPONENT_EXPORT(PRINTING) PrintSettings {
90 // Media properties requested by the user. Default instance represents
91 // default media selection.
92 struct RequestedMedia {
93 bool operator==(const RequestedMedia& other) const;
94 bool IsDefault() const {
95 return size_microns.IsEmpty() && vendor_id.empty();
98 // Size of the media, in microns.
99 gfx::Size size_microns;
100 // Platform specific id to map it back to the particular media.
101 std::string vendor_id;
104 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
105 using AdvancedSettings = std::map<std::string, base::Value>;
106 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
109 PrintSettings(const PrintSettings&);
110 PrintSettings& operator=(const PrintSettings&);
113 bool operator==(const PrintSettings& other) const;
115 // Reinitialize the settings to the default values.
118 void SetCustomMargins(const PageMargins& requested_margins_in_points);
119 const PageMargins& requested_custom_margins_in_points() const {
120 return requested_custom_margins_in_points_;
122 void set_margin_type(mojom::MarginType margin_type) {
123 margin_type_ = margin_type;
125 mojom::MarginType margin_type() const { return margin_type_; }
127 // Updates the orientation and flip the page if needed.
128 void SetOrientation(bool landscape);
129 bool landscape() const { return landscape_; }
131 // Updates user requested media.
132 void set_requested_media(const RequestedMedia& media) {
133 requested_media_ = media;
135 // Media properties requested by the user. Translated into device media by the
136 // platform specific layers.
137 const RequestedMedia& requested_media() const { return requested_media_; }
139 // Set printer printable area in in device units.
140 // Some platforms already provide flipped area. Set `landscape_needs_flip`
141 // to false on those platforms to avoid double flipping.
142 // This method assumes correct DPI is already set.
143 void SetPrinterPrintableArea(const gfx::Size& physical_size_device_units,
144 const gfx::Rect& printable_area_device_units,
145 bool landscape_needs_flip);
146 #if BUILDFLAG(IS_WIN)
147 // Update the printer printable area for the current media using the
148 // provided area in microns.
149 void UpdatePrinterPrintableArea(const gfx::Rect& printable_area_um);
151 const PageSetup& page_setup_device_units() const {
152 return page_setup_device_units_;
154 // `set_page_setup_device_units()` intended to be used only for mojom
156 void set_page_setup_device_units(const PageSetup& page_setup_device_units) {
157 page_setup_device_units_ = page_setup_device_units;
160 void set_device_name(const std::u16string& device_name) {
161 device_name_ = device_name;
163 const std::u16string& device_name() const { return device_name_; }
165 void set_borderless(bool borderless) { borderless_ = borderless; }
166 bool borderless() const { return borderless_; }
168 void set_media_type(const std::string& media_type) {
169 media_type_ = media_type;
171 const std::string& media_type() const { return media_type_; }
173 void set_dpi(int dpi) { dpi_ = gfx::Size(dpi, dpi); }
174 void set_dpi_xy(int dpi_horizontal, int dpi_vertical) {
175 dpi_ = gfx::Size(dpi_horizontal, dpi_vertical);
178 int dpi() const { return std::max(dpi_.width(), dpi_.height()); }
179 int dpi_horizontal() const { return dpi_.width(); }
180 int dpi_vertical() const { return dpi_.height(); }
181 const gfx::Size& dpi_size() const { return dpi_; }
183 void set_scale_factor(double scale_factor) { scale_factor_ = scale_factor; }
184 double scale_factor() const { return scale_factor_; }
186 void set_rasterize_pdf(bool rasterize_pdf) { rasterize_pdf_ = rasterize_pdf; }
187 bool rasterize_pdf() const { return rasterize_pdf_; }
189 void set_rasterize_pdf_dpi(int32_t dpi) { rasterize_pdf_dpi_ = dpi; }
190 int32_t rasterize_pdf_dpi() const { return rasterize_pdf_dpi_; }
192 int device_units_per_inch() const {
193 #if BUILDFLAG(IS_MAC)
194 return kMacDeviceUnitsPerInch;
195 #else // BUILDFLAG(IS_MAC)
197 #endif // BUILDFLAG(IS_MAC)
200 const gfx::Size& device_units_per_inch_size() const {
201 #if BUILDFLAG(IS_MAC)
202 static constexpr gfx::Size kSize{kMacDeviceUnitsPerInch,
203 kMacDeviceUnitsPerInch};
205 #else // BUILDFLAG(IS_MAC)
207 #endif // BUILDFLAG(IS_MAC)
210 void set_ranges(const PageRanges& ranges) { ranges_ = ranges; }
211 const PageRanges& ranges() const { return ranges_; }
213 void set_selection_only(bool selection_only) {
214 selection_only_ = selection_only;
216 bool selection_only() const { return selection_only_; }
218 void set_should_print_backgrounds(bool should_print_backgrounds) {
219 should_print_backgrounds_ = should_print_backgrounds;
221 bool should_print_backgrounds() const { return should_print_backgrounds_; }
223 void set_display_header_footer(bool display_header_footer) {
224 display_header_footer_ = display_header_footer;
226 bool display_header_footer() const { return display_header_footer_; }
228 void set_title(const std::u16string& title) { title_ = title; }
229 const std::u16string& title() const { return title_; }
231 void set_url(const std::u16string& url) { url_ = url; }
232 const std::u16string& url() const { return url_; }
234 void set_collate(bool collate) { collate_ = collate; }
235 bool collate() const { return collate_; }
237 void set_color(mojom::ColorModel color) { color_ = color; }
238 mojom::ColorModel color() const { return color_; }
240 void set_copies(int copies) { copies_ = copies; }
241 int copies() const { return copies_; }
243 void set_duplex_mode(mojom::DuplexMode duplex_mode) {
244 duplex_mode_ = duplex_mode;
246 mojom::DuplexMode duplex_mode() const { return duplex_mode_; }
248 #if BUILDFLAG(IS_WIN)
249 void set_printer_language_type(mojom::PrinterLanguageType type) {
250 printer_language_type_ = type;
252 mojom::PrinterLanguageType printer_language_type() const {
253 return printer_language_type_;
255 bool printer_language_is_textonly() const {
256 return printer_language_type_ == mojom::PrinterLanguageType::kTextOnly;
258 bool printer_language_is_xps() const {
259 return printer_language_type_ == mojom::PrinterLanguageType::kXps;
261 bool printer_language_is_ps2() const {
262 return printer_language_type_ ==
263 mojom::PrinterLanguageType::kPostscriptLevel2;
265 bool printer_language_is_ps3() const {
266 return printer_language_type_ ==
267 mojom::PrinterLanguageType::kPostscriptLevel3;
271 void set_is_modifiable(bool is_modifiable) { is_modifiable_ = is_modifiable; }
272 bool is_modifiable() const { return is_modifiable_; }
274 int pages_per_sheet() const { return pages_per_sheet_; }
275 void set_pages_per_sheet(int pages_per_sheet) {
276 pages_per_sheet_ = pages_per_sheet;
279 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
280 AdvancedSettings& advanced_settings() { return advanced_settings_; }
281 const AdvancedSettings& advanced_settings() const {
282 return advanced_settings_;
284 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
286 #if BUILDFLAG(IS_CHROMEOS)
287 void set_send_user_info(bool send_user_info) {
288 send_user_info_ = send_user_info;
290 bool send_user_info() const { return send_user_info_; }
292 void set_username(const std::string& username) { username_ = username; }
293 const std::string& username() const { return username_; }
295 void set_oauth_token(const std::string& oauth_token) {
296 oauth_token_ = oauth_token;
298 const std::string& oauth_token() const { return oauth_token_; }
300 void set_pin_value(const std::string& pin_value) { pin_value_ = pin_value; }
301 const std::string& pin_value() const { return pin_value_; }
303 void set_client_infos(std::vector<mojom::IppClientInfo> client_infos) {
304 client_infos_ = std::move(client_infos);
306 const std::vector<mojom::IppClientInfo>& client_infos() const {
307 return client_infos_;
310 void set_printer_manually_selected(bool printer_manually_selected) {
311 printer_manually_selected_ = printer_manually_selected;
313 bool printer_manually_selected() const { return printer_manually_selected_; }
315 void set_printer_status_reason(
316 crosapi::mojom::StatusReason::Reason printer_status_reason) {
317 printer_status_reason_ = printer_status_reason;
319 absl::optional<crosapi::mojom::StatusReason::Reason> printer_status_reason()
321 return printer_status_reason_;
323 #endif // BUILDFLAG(IS_CHROMEOS)
325 #if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
326 void set_system_print_dialog_data(base::Value::Dict data) {
327 system_print_dialog_data_ = std::move(data);
329 const base::Value::Dict& system_print_dialog_data() const {
330 return system_print_dialog_data_;
333 // Cookie generator. It is used to initialize `PrintedDocument` with its
334 // associated `PrintSettings`, to be sure that each generated `PrintedPage`
335 // is correctly associated with its corresponding `PrintedDocument`.
336 static int NewCookie();
338 // Creates an invalid cookie for use in situations where the cookie needs to
339 // be marked as invalid.
340 static int NewInvalidCookie();
343 #if BUILDFLAG(IS_MAC)
344 static constexpr int kMacDeviceUnitsPerInch = 72;
347 // Multi-page printing. Each `PageRange` describes a from-to page combination.
348 // This permits printing selected pages only.
351 // Indicates if the user only wants to print the current selection.
352 bool selection_only_;
354 // Indicates what kind of margins should be applied to the printable area.
355 mojom::MarginType margin_type_;
357 // Strings to be printed as headers and footers if requested by the user.
358 std::u16string title_;
361 // True if the user wants headers and footers to be displayed.
362 bool display_header_footer_;
364 // True if the user wants to print CSS backgrounds.
365 bool should_print_backgrounds_;
367 // True if the user wants to print with collate.
370 // Color model type for the printer to use.
371 mojom::ColorModel color_;
373 // Number of copies user wants to print.
376 // Duplex type user wants to use.
377 mojom::DuplexMode duplex_mode_;
379 // Printer device name as opened by the OS.
380 std::u16string device_name_;
382 #if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
383 // Platform-specific print settings captured from a system print dialog.
384 // The settings are captured in the browser process for transmission to
385 // the Print Backend service for OOP printing.
386 base::Value::Dict system_print_dialog_data_;
389 // Media requested by the user.
390 RequestedMedia requested_media_;
392 // Page setup in device units.
393 PageSetup page_setup_device_units_;
395 // Whether the user has requested borderless (zero margin) printing.
398 // Media type requested by the user.
399 std::string media_type_;
401 // Printer's device effective dots per inch in both axes. The two values will
402 // generally be identical. However, on Windows, there are a few rare printers
403 // that support resolutions with different DPI in different dimensions.
407 double scale_factor_;
409 // True if PDF should be printed as a raster PDF
412 // The DPI which overrides the calculated value normally used when
413 // rasterizing a PDF. A non-positive value would be an invalid choice of a
414 // DPI and indicates no override.
415 int32_t rasterize_pdf_dpi_;
417 // Is the orientation landscape or portrait.
420 #if BUILDFLAG(IS_WIN)
421 mojom::PrinterLanguageType printer_language_type_;
426 // If margin type is custom, this is what was requested.
427 PageMargins requested_custom_margins_in_points_;
429 // Number of pages per sheet.
430 int pages_per_sheet_;
432 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
433 // Advanced settings.
434 AdvancedSettings advanced_settings_;
435 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
437 #if BUILDFLAG(IS_CHROMEOS)
438 // Whether to send user info.
439 bool send_user_info_;
441 // Username if it's required by the printer.
442 std::string username_;
444 // OAuth access token if it's required by the printer.
445 std::string oauth_token_;
447 // PIN code entered by the user.
448 std::string pin_value_;
450 // Value of the 'client-info' that will be sent to the printer.
451 // Should only be set for printers that support 'client-info'.
452 std::vector<mojom::IppClientInfo> client_infos_;
454 // True if the user selects to print to a different printer than the original
455 // destination shown when Print Preview opens.
456 bool printer_manually_selected_;
458 // The printer status reason shown for the selected printer at the time print
459 // is requested. Only local CrOS printers set printer statuses.
460 absl::optional<crosapi::mojom::StatusReason::Reason> printer_status_reason_;
461 #endif // BUILDFLAG(IS_CHROMEOS)
464 } // namespace printing
466 #endif // PRINTING_PRINT_SETTINGS_H_