fix: refactor printing for mojom (#21026)

This commit is contained in:
Shelley Vohr 2019-11-08 10:01:50 -08:00 committed by John Kleinschmidt
parent fe2ca6e6e7
commit 6c2af8b421
2 changed files with 86 additions and 76 deletions

View file

@ -1692,7 +1692,7 @@ bool WebContents::IsCurrentlyAudible() {
void WebContents::Print(gin_helper::Arguments* args) {
gin_helper::Dictionary options =
gin::Dictionary::CreateEmpty(args->isolate());
base::DictionaryValue settings;
base::Value settings(base::Value::Type::DICTIONARY);
if (args->Length() >= 1 && !args->GetNext(&options)) {
args->ThrowError("webContents.print(): Invalid print settings specified.");
@ -1712,7 +1712,7 @@ void WebContents::Print(gin_helper::Arguments* args) {
bool print_background = false;
options.Get("printBackground", &print_background);
settings.SetBoolean(printing::kSettingShouldPrintBackgrounds,
settings.SetBoolKey(printing::kSettingShouldPrintBackgrounds,
print_background);
// Set custom margin settings
@ -1721,62 +1721,62 @@ void WebContents::Print(gin_helper::Arguments* args) {
if (options.Get("margins", &margins)) {
printing::MarginType margin_type = printing::DEFAULT_MARGINS;
margins.Get("marginType", &margin_type);
settings.SetInteger(printing::kSettingMarginsType, margin_type);
settings.SetIntKey(printing::kSettingMarginsType, margin_type);
if (margin_type == printing::CUSTOM_MARGINS) {
int top = 0;
margins.Get("top", &top);
settings.SetInteger(printing::kSettingMarginTop, top);
settings.SetIntKey(printing::kSettingMarginTop, top);
int bottom = 0;
margins.Get("bottom", &bottom);
settings.SetInteger(printing::kSettingMarginBottom, bottom);
settings.SetIntKey(printing::kSettingMarginBottom, bottom);
int left = 0;
margins.Get("left", &left);
settings.SetInteger(printing::kSettingMarginLeft, left);
settings.SetIntKey(printing::kSettingMarginLeft, left);
int right = 0;
margins.Get("right", &right);
settings.SetInteger(printing::kSettingMarginRight, right);
settings.SetIntKey(printing::kSettingMarginRight, right);
}
} else {
settings.SetInteger(printing::kSettingMarginsType,
printing::DEFAULT_MARGINS);
settings.SetIntKey(printing::kSettingMarginsType,
printing::DEFAULT_MARGINS);
}
// Set whether to print color or greyscale
bool print_color = true;
options.Get("color", &print_color);
int color_setting = print_color ? printing::COLOR : printing::GRAY;
settings.SetInteger(printing::kSettingColor, color_setting);
settings.SetIntKey(printing::kSettingColor, color_setting);
// Is the orientation landscape or portrait.
bool landscape = false;
options.Get("landscape", &landscape);
settings.SetBoolean(printing::kSettingLandscape, landscape);
settings.SetBoolKey(printing::kSettingLandscape, landscape);
// We set the default to empty string here and only update
// if at the Chromium level if it's non-empty
// Printer device name as opened by the OS.
base::string16 device_name;
options.Get("deviceName", &device_name);
settings.SetString(printing::kSettingDeviceName, device_name);
settings.SetStringKey(printing::kSettingDeviceName, device_name);
int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
settings.SetInteger(printing::kSettingScaleFactor, scale_factor);
settings.SetIntKey(printing::kSettingScaleFactor, scale_factor);
int pages_per_sheet = 1;
options.Get("pagesPerSheet", &pages_per_sheet);
settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet);
settings.SetIntKey(printing::kSettingPagesPerSheet, pages_per_sheet);
// True if the user wants to print with collate.
bool collate = true;
options.Get("collate", &collate);
settings.SetBoolean(printing::kSettingCollate, collate);
settings.SetBoolKey(printing::kSettingCollate, collate);
// The number of individual copies to print
int copies = 1;
options.Get("copies", &copies);
settings.SetInteger(printing::kSettingCopies, copies);
settings.SetIntKey(printing::kSettingCopies, copies);
// Strings to be printed as headers and footers if requested by the user.
std::string header;
@ -1785,44 +1785,43 @@ void WebContents::Print(gin_helper::Arguments* args) {
options.Get("footer", &footer);
if (!(header.empty() && footer.empty())) {
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, true);
settings.SetBoolKey(printing::kSettingHeaderFooterEnabled, true);
settings.SetString(printing::kSettingHeaderFooterTitle, header);
settings.SetString(printing::kSettingHeaderFooterURL, footer);
settings.SetStringKey(printing::kSettingHeaderFooterTitle, header);
settings.SetStringKey(printing::kSettingHeaderFooterURL, footer);
} else {
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, false);
settings.SetBoolKey(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.SetInteger(printing::kSettingPrinterType, printing::kLocalPrinter);
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolean(printing::kSettingRasterizePdf, false);
settings.SetIntKey(printing::kSettingPrinterType, printing::kLocalPrinter);
settings.SetBoolKey(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolKey(printing::kSettingRasterizePdf, false);
// Set custom page ranges to print
std::vector<gin_helper::Dictionary> page_ranges;
if (options.Get("pageRanges", &page_ranges)) {
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
base::Value page_range_list(base::Value::Type::LIST);
for (auto& range : page_ranges) {
int from, to;
if (range.Get("from", &from) && range.Get("to", &to)) {
std::unique_ptr<base::DictionaryValue> range(
new base::DictionaryValue());
range->SetInteger(printing::kSettingPageRangeFrom, from);
range->SetInteger(printing::kSettingPageRangeTo, to);
page_range_list->Append(std::move(range));
base::Value range(base::Value::Type::DICTIONARY);
range.SetIntKey(printing::kSettingPageRangeFrom, from);
range.SetIntKey(printing::kSettingPageRangeTo, to);
page_range_list.Append(std::move(range));
} else {
continue;
}
}
if (page_range_list->GetSize() > 0)
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
if (page_range_list.GetList().size() > 0)
settings.SetPath(printing::kSettingPageRange, std::move(page_range_list));
}
// Duplex type user wants to use.
printing::DuplexMode duplex_mode;
options.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);
settings.SetIntKey(printing::kSettingDuplexMode, duplex_mode);
// Set custom dots per inch (dpi)
gin_helper::Dictionary dpi_settings;
@ -1830,13 +1829,13 @@ void WebContents::Print(gin_helper::Arguments* args) {
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
settings.SetIntKey(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetInteger(printing::kSettingDpiVertical, vertical);
settings.SetIntKey(printing::kSettingDpiVertical, vertical);
} else {
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
settings.SetInteger(printing::kSettingDpiVertical, dpi);
settings.SetIntKey(printing::kSettingDpiHorizontal, dpi);
settings.SetIntKey(printing::kSettingDpiVertical, dpi);
}
auto* print_view_manager =
@ -1845,7 +1844,8 @@ void WebContents::Print(gin_helper::Arguments* args) {
auto* rfh = focused_frame && focused_frame->HasSelection()
? focused_frame
: web_contents()->GetMainFrame();
print_view_manager->PrintNow(rfh, std::move(callback));
print_view_manager->PrintNow(rfh, silent, std::move(settings),
std::move(callback));
}
std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {