1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PRINTING_PRINT_SETTINGS_H_
#define PRINTING_PRINT_SETTINGS_H_
#include <algorithm>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/component_export.h"
#include "build/build_config.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "printing/page_range.h"
#include "printing/page_setup.h"
#include "printing/print_job_constants.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
#include "base/values.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <map>
#include "base/values.h"
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/crosapi/mojom/local_printer.mojom.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace printing {
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
#if BUILDFLAG(IS_MAC)
inline constexpr char kMacSystemPrintDialogDataDestinationType[] =
"destination_type";
inline constexpr char kMacSystemPrintDialogDataDestinationFormat[] =
"destination_format";
inline constexpr char kMacSystemPrintDialogDataDestinationLocation[] =
"destination_location";
inline constexpr char kMacSystemPrintDialogDataPageFormat[] = "page_format";
inline constexpr char kMacSystemPrintDialogDataPrintSettings[] =
"print_settings";
#endif // BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_LINUX)
inline constexpr char kLinuxSystemPrintDialogDataPrinter[] = "printer_name";
inline constexpr char kLinuxSystemPrintDialogDataPrintSettings[] =
"print_settings";
inline constexpr char kLinuxSystemPrintDialogDataPageSetup[] = "page_setup";
#endif // BUILDFLAG(IS_LINUX)
#endif // BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
// Convert from `color_mode` into a `color_model`. An invalid `color_mode`
// will give a result of `mojom::ColorModel::kUnknownColorModel`.
COMPONENT_EXPORT(PRINTING_SETTINGS)
mojom::ColorModel ColorModeToColorModel(int color_mode);
// Returns true if `color_model` is color and false if it is B&W. Callers
// are not supposed to pass in `mojom::ColorModel::kUnknownColorModel`, but
// if they do then the result will be std::nullopt.
COMPONENT_EXPORT(PRINTING_SETTINGS)
std::optional<bool> IsColorModelSelected(mojom::ColorModel color_model);
#if BUILDFLAG(USE_CUPS)
// Get the color model setting name and value for the `color_model`.
COMPONENT_EXPORT(PRINTING_SETTINGS)
void GetColorModelForModel(mojom::ColorModel color_model,
std::string* color_setting_name,
std::string* color_value);
#endif // BUILDFLAG(USE_CUPS)
#if BUILDFLAG(USE_CUPS_IPP)
// Convert from `color_model` to a print-color-mode value from PWG 5100.13.
COMPONENT_EXPORT(PRINTING_SETTINGS)
std::string GetIppColorModelForModel(mojom::ColorModel color_model);
#endif // BUILDFLAG(USE_CUPS_IPP)
class COMPONENT_EXPORT(PRINTING_SETTINGS) PrintSettings {
public:
// Media properties requested by the user. Default instance represents
// default media selection.
struct RequestedMedia {
bool operator==(const RequestedMedia& other) const;
bool IsDefault() const {
return size_microns.IsEmpty() && vendor_id.empty();
}
// Size of the media, in microns.
gfx::Size size_microns;
// Platform specific id to map it back to the particular media.
std::string vendor_id;
};
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
using AdvancedSettings = std::map<std::string, base::Value>;
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
PrintSettings();
PrintSettings(const PrintSettings&);
PrintSettings& operator=(const PrintSettings&);
~PrintSettings();
bool operator==(const PrintSettings& other) const;
// Reinitialize the settings to the default values.
void Clear();
void SetCustomMargins(const PageMargins& requested_margins_in_microns);
const PageMargins& requested_custom_margins_in_microns() const {
return requested_custom_margins_in_microns_;
}
void set_margin_type(mojom::MarginType margin_type) {
margin_type_ = margin_type;
}
mojom::MarginType margin_type() const { return margin_type_; }
// Updates the orientation and flip the page if needed.
void SetOrientation(bool landscape);
bool landscape() const { return landscape_; }
// Updates user requested media.
void set_requested_media(const RequestedMedia& media) {
requested_media_ = media;
}
// Media properties requested by the user. Translated into device media by the
// platform specific layers.
const RequestedMedia& requested_media() const { return requested_media_; }
// Set printer printable area in in device units.
// Some platforms already provide flipped area. Set `landscape_needs_flip`
// to false on those platforms to avoid double flipping.
// This method assumes correct DPI is already set.
void SetPrinterPrintableArea(const gfx::Size& physical_size_device_units,
const gfx::Rect& printable_area_device_units,
bool landscape_needs_flip);
#if BUILDFLAG(IS_WIN)
// Update the printer printable area for the current media using the
// provided area in microns.
void UpdatePrinterPrintableArea(const gfx::Rect& printable_area_um);
#endif
const PageSetup& page_setup_device_units() const {
return page_setup_device_units_;
}
// `set_page_setup_device_units()` intended to be used only for mojom
// validation.
void set_page_setup_device_units(const PageSetup& page_setup_device_units) {
page_setup_device_units_ = page_setup_device_units;
}
void set_device_name(const std::u16string& device_name) {
device_name_ = device_name;
}
const std::u16string& device_name() const { return device_name_; }
void set_borderless(bool borderless) { borderless_ = borderless; }
bool borderless() const { return borderless_; }
void set_media_type(const std::string& media_type) {
media_type_ = media_type;
}
const std::string& media_type() const { return media_type_; }
void set_dpi(int dpi) { dpi_ = gfx::Size(dpi, dpi); }
void set_dpi_xy(int dpi_horizontal, int dpi_vertical) {
dpi_ = gfx::Size(dpi_horizontal, dpi_vertical);
}
int dpi() const { return std::max(dpi_.width(), dpi_.height()); }
int dpi_horizontal() const { return dpi_.width(); }
int dpi_vertical() const { return dpi_.height(); }
const gfx::Size& dpi_size() const { return dpi_; }
void set_scale_factor(double scale_factor) { scale_factor_ = scale_factor; }
double scale_factor() const { return scale_factor_; }
void set_rasterize_pdf(bool rasterize_pdf) { rasterize_pdf_ = rasterize_pdf; }
bool rasterize_pdf() const { return rasterize_pdf_; }
void set_rasterize_pdf_dpi(int32_t dpi) { rasterize_pdf_dpi_ = dpi; }
int32_t rasterize_pdf_dpi() const { return rasterize_pdf_dpi_; }
int device_units_per_inch() const {
#if BUILDFLAG(IS_MAC)
return kMacDeviceUnitsPerInch;
#else // BUILDFLAG(IS_MAC)
return dpi();
#endif // BUILDFLAG(IS_MAC)
}
const gfx::Size& device_units_per_inch_size() const {
#if BUILDFLAG(IS_MAC)
static constexpr gfx::Size kSize{kMacDeviceUnitsPerInch,
kMacDeviceUnitsPerInch};
return kSize;
#else // BUILDFLAG(IS_MAC)
return dpi_size();
#endif // BUILDFLAG(IS_MAC)
}
void set_ranges(const PageRanges& ranges) { ranges_ = ranges; }
const PageRanges& ranges() const { return ranges_; }
void set_selection_only(bool selection_only) {
selection_only_ = selection_only;
}
bool selection_only() const { return selection_only_; }
void set_should_print_backgrounds(bool should_print_backgrounds) {
should_print_backgrounds_ = should_print_backgrounds;
}
bool should_print_backgrounds() const { return should_print_backgrounds_; }
void set_display_header_footer(bool display_header_footer) {
display_header_footer_ = display_header_footer;
}
bool display_header_footer() const { return display_header_footer_; }
void set_title(const std::u16string& title) { title_ = title; }
const std::u16string& title() const { return title_; }
void set_url(const std::u16string& url) { url_ = url; }
const std::u16string& url() const { return url_; }
void set_collate(bool collate) { collate_ = collate; }
bool collate() const { return collate_; }
void set_color(mojom::ColorModel color) { color_ = color; }
mojom::ColorModel color() const { return color_; }
void set_copies(int copies) { copies_ = copies; }
int copies() const { return copies_; }
void set_duplex_mode(mojom::DuplexMode duplex_mode) {
duplex_mode_ = duplex_mode;
}
mojom::DuplexMode duplex_mode() const { return duplex_mode_; }
#if BUILDFLAG(IS_WIN)
void set_printer_language_type(mojom::PrinterLanguageType type) {
printer_language_type_ = type;
}
mojom::PrinterLanguageType printer_language_type() const {
return printer_language_type_;
}
bool printer_language_is_textonly() const {
return printer_language_type_ == mojom::PrinterLanguageType::kTextOnly;
}
bool printer_language_is_xps() const {
return printer_language_type_ == mojom::PrinterLanguageType::kXps;
}
bool printer_language_is_ps2() const {
return printer_language_type_ ==
mojom::PrinterLanguageType::kPostscriptLevel2;
}
bool printer_language_is_ps3() const {
return printer_language_type_ ==
mojom::PrinterLanguageType::kPostscriptLevel3;
}
#endif
void set_is_modifiable(bool is_modifiable) { is_modifiable_ = is_modifiable; }
bool is_modifiable() const { return is_modifiable_; }
int pages_per_sheet() const { return pages_per_sheet_; }
void set_pages_per_sheet(int pages_per_sheet) {
pages_per_sheet_ = pages_per_sheet;
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
AdvancedSettings& advanced_settings() { return advanced_settings_; }
const AdvancedSettings& advanced_settings() const {
return advanced_settings_;
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
void set_send_user_info(bool send_user_info) {
send_user_info_ = send_user_info;
}
bool send_user_info() const { return send_user_info_; }
void set_username(const std::string& username) { username_ = username; }
const std::string& username() const { return username_; }
void set_oauth_token(const std::string& oauth_token) {
oauth_token_ = oauth_token;
}
const std::string& oauth_token() const { return oauth_token_; }
void set_pin_value(const std::string& pin_value) { pin_value_ = pin_value; }
const std::string& pin_value() const { return pin_value_; }
void set_client_infos(std::vector<mojom::IppClientInfo> client_infos) {
client_infos_ = std::move(client_infos);
}
const std::vector<mojom::IppClientInfo>& client_infos() const {
return client_infos_;
}
void set_printer_manually_selected(bool printer_manually_selected) {
printer_manually_selected_ = printer_manually_selected;
}
bool printer_manually_selected() const { return printer_manually_selected_; }
void set_printer_status_reason(
crosapi::mojom::StatusReason::Reason printer_status_reason) {
printer_status_reason_ = printer_status_reason;
}
std::optional<crosapi::mojom::StatusReason::Reason> printer_status_reason()
const {
return printer_status_reason_;
}
void set_print_scaling(mojom::PrintScalingType print_scaling) {
print_scaling_ = print_scaling;
}
mojom::PrintScalingType print_scaling() const { return print_scaling_; }
void set_quality(mojom::Quality quality) { quality_ = quality; }
mojom::Quality quality() const { return quality_; }
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
void set_system_print_dialog_data(base::Value::Dict data) {
system_print_dialog_data_ = std::move(data);
}
const base::Value::Dict& system_print_dialog_data() const {
return system_print_dialog_data_;
}
#endif
// Cookie generator. It is used to initialize `PrintedDocument` with its
// associated `PrintSettings`, to be sure that each generated `PrintedPage`
// is correctly associated with its corresponding `PrintedDocument`.
static int NewCookie();
// Creates an invalid cookie for use in situations where the cookie needs to
// be marked as invalid.
static int NewInvalidCookie();
private:
#if BUILDFLAG(IS_MAC)
static constexpr int kMacDeviceUnitsPerInch = 72;
#endif
// Multi-page printing. Each `PageRange` describes a from-to page combination.
// This permits printing selected pages only.
PageRanges ranges_;
// Indicates if the user only wants to print the current selection.
bool selection_only_;
// Indicates what kind of margins should be applied to the printable area.
mojom::MarginType margin_type_;
// Strings to be printed as headers and footers if requested by the user.
std::u16string title_;
std::u16string url_;
// True if the user wants headers and footers to be displayed.
bool display_header_footer_;
// True if the user wants to print CSS backgrounds.
bool should_print_backgrounds_;
// True if the user wants to print with collate.
bool collate_;
// Color model type for the printer to use.
mojom::ColorModel color_;
// Number of copies user wants to print.
int copies_;
// Duplex type user wants to use.
mojom::DuplexMode duplex_mode_;
// Printer device name as opened by the OS.
std::u16string device_name_;
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
// Platform-specific print settings captured from a system print dialog.
// The settings are captured in the browser process for transmission to
// the Print Backend service for OOP printing.
base::Value::Dict system_print_dialog_data_;
#endif
// Media requested by the user.
RequestedMedia requested_media_;
// Page setup in device units.
PageSetup page_setup_device_units_;
// Whether the user has requested borderless (zero margin) printing.
bool borderless_;
// Media type requested by the user.
std::string media_type_;
// Printer's device effective dots per inch in both axes. The two values will
// generally be identical. However, on Windows, there are a few rare printers
// that support resolutions with different DPI in different dimensions.
gfx::Size dpi_;
// Scale factor
double scale_factor_;
// True if PDF should be printed as a raster PDF
bool rasterize_pdf_;
// The DPI which overrides the calculated value normally used when
// rasterizing a PDF. A non-positive value would be an invalid choice of a
// DPI and indicates no override.
int32_t rasterize_pdf_dpi_;
// Is the orientation landscape or portrait.
bool landscape_;
#if BUILDFLAG(IS_WIN)
mojom::PrinterLanguageType printer_language_type_;
#endif
bool is_modifiable_;
// If margin type is custom, this is what was requested.
PageMargins requested_custom_margins_in_microns_;
// Number of pages per sheet.
int pages_per_sheet_;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Advanced settings.
AdvancedSettings advanced_settings_;
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
// Whether to send user info.
bool send_user_info_;
// Username if it's required by the printer.
std::string username_;
// OAuth access token if it's required by the printer.
std::string oauth_token_;
// PIN code entered by the user.
std::string pin_value_;
// Value of the 'client-info' that will be sent to the printer.
// Should only be set for printers that support 'client-info'.
std::vector<mojom::IppClientInfo> client_infos_;
// True if the user selects to print to a different printer than the original
// destination shown when Print Preview opens.
bool printer_manually_selected_ = false;
// The printer status reason shown for the selected printer at the time print
// is requested. Only local CrOS printers set printer statuses.
std::optional<crosapi::mojom::StatusReason::Reason> printer_status_reason_;
// Print scaling type.
mojom::PrintScalingType print_scaling_ =
mojom::PrintScalingType::kUnknownPrintScalingType;
// Print qulity for the printer to use.
mojom::Quality quality_ = mojom::Quality::kUnknownQuality;
#endif // BUILDFLAG(IS_CHROMEOS)
};
} // namespace printing
#endif // PRINTING_PRINT_SETTINGS_H_
|