fix: DCHECK on webContents.print() (#34271)

This commit is contained in:
Shelley Vohr 2022-05-19 10:05:07 +02:00 committed by GitHub
parent 73e0bf973d
commit 588005a9d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 71 deletions

View file

@ -2586,7 +2586,7 @@ bool WebContents::IsCurrentlyAudible() {
#if BUILDFLAG(ENABLE_PRINTING)
void WebContents::OnGetDefaultPrinter(
base::Value print_settings,
base::Value::Dict print_settings,
printing::CompletionCallback print_callback,
std::u16string device_name,
bool silent,
@ -2616,7 +2616,7 @@ void WebContents::OnGetDefaultPrinter(
return;
}
print_settings.SetStringKey(printing::kSettingDeviceName, printer_name);
print_settings.Set(printing::kSettingDeviceName, printer_name);
auto* print_view_manager =
PrintViewManagerElectron::FromWebContents(web_contents());
@ -2635,7 +2635,7 @@ void WebContents::OnGetDefaultPrinter(
void WebContents::Print(gin::Arguments* args) {
gin_helper::Dictionary options =
gin::Dictionary::CreateEmpty(args->isolate());
base::Value settings(base::Value::Type::DICTIONARY);
base::Value::Dict settings;
if (args->Length() >= 1 && !args->GetNext(&options)) {
gin_helper::ErrorThrower(args->isolate())
@ -2656,8 +2656,7 @@ void WebContents::Print(gin::Arguments* args) {
bool print_background = false;
options.Get("printBackground", &print_background);
settings.SetBoolKey(printing::kSettingShouldPrintBackgrounds,
print_background);
settings.Set(printing::kSettingShouldPrintBackgrounds, print_background);
// Set custom margin settings
gin_helper::Dictionary margins =
@ -2666,28 +2665,26 @@ void WebContents::Print(gin::Arguments* args) {
printing::mojom::MarginType margin_type =
printing::mojom::MarginType::kDefaultMargins;
margins.Get("marginType", &margin_type);
settings.SetIntKey(printing::kSettingMarginsType,
static_cast<int>(margin_type));
settings.Set(printing::kSettingMarginsType, static_cast<int>(margin_type));
if (margin_type == printing::mojom::MarginType::kCustomMargins) {
base::Value custom_margins(base::Value::Type::DICTIONARY);
base::Value::Dict custom_margins;
int top = 0;
margins.Get("top", &top);
custom_margins.SetIntKey(printing::kSettingMarginTop, top);
custom_margins.Set(printing::kSettingMarginTop, top);
int bottom = 0;
margins.Get("bottom", &bottom);
custom_margins.SetIntKey(printing::kSettingMarginBottom, bottom);
custom_margins.Set(printing::kSettingMarginBottom, bottom);
int left = 0;
margins.Get("left", &left);
custom_margins.SetIntKey(printing::kSettingMarginLeft, left);
custom_margins.Set(printing::kSettingMarginLeft, left);
int right = 0;
margins.Get("right", &right);
custom_margins.SetIntKey(printing::kSettingMarginRight, right);
settings.SetPath(printing::kSettingMarginsCustom,
std::move(custom_margins));
custom_margins.Set(printing::kSettingMarginRight, right);
settings.Set(printing::kSettingMarginsCustom, std::move(custom_margins));
}
} else {
settings.SetIntKey(
settings.Set(
printing::kSettingMarginsType,
static_cast<int>(printing::mojom::MarginType::kDefaultMargins));
}
@ -2697,12 +2694,12 @@ void WebContents::Print(gin::Arguments* args) {
options.Get("color", &print_color);
auto const color_model = print_color ? printing::mojom::ColorModel::kColor
: printing::mojom::ColorModel::kGray;
settings.SetIntKey(printing::kSettingColor, static_cast<int>(color_model));
settings.Set(printing::kSettingColor, static_cast<int>(color_model));
// Is the orientation landscape or portrait.
bool landscape = false;
options.Get("landscape", &landscape);
settings.SetBoolKey(printing::kSettingLandscape, landscape);
settings.Set(printing::kSettingLandscape, landscape);
// We set the default to the system's default printer and only update
// if at the Chromium level if the user overrides.
@ -2717,21 +2714,21 @@ void WebContents::Print(gin::Arguments* args) {
int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
settings.SetIntKey(printing::kSettingScaleFactor, scale_factor);
settings.Set(printing::kSettingScaleFactor, scale_factor);
int pages_per_sheet = 1;
options.Get("pagesPerSheet", &pages_per_sheet);
settings.SetIntKey(printing::kSettingPagesPerSheet, pages_per_sheet);
settings.Set(printing::kSettingPagesPerSheet, pages_per_sheet);
// True if the user wants to print with collate.
bool collate = true;
options.Get("collate", &collate);
settings.SetBoolKey(printing::kSettingCollate, collate);
settings.Set(printing::kSettingCollate, collate);
// The number of individual copies to print
int copies = 1;
options.Get("copies", &copies);
settings.SetIntKey(printing::kSettingCopies, copies);
settings.Set(printing::kSettingCopies, copies);
// Strings to be printed as headers and footers if requested by the user.
std::string header;
@ -2740,53 +2737,52 @@ void WebContents::Print(gin::Arguments* args) {
options.Get("footer", &footer);
if (!(header.empty() && footer.empty())) {
settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, true);
settings.Set(printing::kSettingHeaderFooterEnabled, true);
settings.SetStringKey(printing::kSettingHeaderFooterTitle, header);
settings.SetStringKey(printing::kSettingHeaderFooterURL, footer);
settings.Set(printing::kSettingHeaderFooterTitle, header);
settings.Set(printing::kSettingHeaderFooterURL, footer);
} else {
settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, false);
settings.Set(printing::kSettingHeaderFooterEnabled, false);
}
// We don't want to allow the user to enable these settings
// but we need to set them or a CHECK is hit.
settings.SetIntKey(printing::kSettingPrinterType,
static_cast<int>(printing::mojom::PrinterType::kLocal));
settings.SetBoolKey(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolKey(printing::kSettingRasterizePdf, false);
settings.Set(printing::kSettingPrinterType,
static_cast<int>(printing::mojom::PrinterType::kLocal));
settings.Set(printing::kSettingShouldPrintSelectionOnly, false);
settings.Set(printing::kSettingRasterizePdf, false);
// Set custom page ranges to print
std::vector<gin_helper::Dictionary> page_ranges;
if (options.Get("pageRanges", &page_ranges)) {
base::Value page_range_list(base::Value::Type::LIST);
base::Value::List page_range_list;
for (auto& range : page_ranges) {
int from, to;
if (range.Get("from", &from) && range.Get("to", &to)) {
base::Value range(base::Value::Type::DICTIONARY);
base::Value::Dict range;
// Chromium uses 1-based page ranges, so increment each by 1.
range.SetIntKey(printing::kSettingPageRangeFrom, from + 1);
range.SetIntKey(printing::kSettingPageRangeTo, to + 1);
range.Set(printing::kSettingPageRangeFrom, from + 1);
range.Set(printing::kSettingPageRangeTo, to + 1);
page_range_list.Append(std::move(range));
} else {
continue;
}
}
if (!page_range_list.GetListDeprecated().empty())
settings.SetPath(printing::kSettingPageRange, std::move(page_range_list));
if (!page_range_list.empty())
settings.Set(printing::kSettingPageRange, std::move(page_range_list));
}
// Duplex type user wants to use.
printing::mojom::DuplexMode duplex_mode =
printing::mojom::DuplexMode::kSimplex;
options.Get("duplexMode", &duplex_mode);
settings.SetIntKey(printing::kSettingDuplexMode,
static_cast<int>(duplex_mode));
settings.Set(printing::kSettingDuplexMode, static_cast<int>(duplex_mode));
// We've already done necessary parameter sanitization at the
// JS level, so we can simply pass this through.
base::Value media_size(base::Value::Type::DICTIONARY);
if (options.Get("mediaSize", &media_size))
settings.SetKey(printing::kSettingMediaSize, std::move(media_size));
settings.Set(printing::kSettingMediaSize, std::move(media_size));
// Set custom dots per inch (dpi)
gin_helper::Dictionary dpi_settings;
@ -2794,13 +2790,13 @@ void WebContents::Print(gin::Arguments* args) {
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetIntKey(printing::kSettingDpiHorizontal, horizontal);
settings.Set(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetIntKey(printing::kSettingDpiVertical, vertical);
settings.Set(printing::kSettingDpiVertical, vertical);
} else {
settings.SetIntKey(printing::kSettingDpiHorizontal, dpi);
settings.SetIntKey(printing::kSettingDpiVertical, dpi);
settings.Set(printing::kSettingDpiHorizontal, dpi);
settings.Set(printing::kSettingDpiVertical, dpi);
}
print_task_runner_->PostTaskAndReplyWithResult(