| Index: headless/lib/browser/headless_devtools_manager_delegate.cc
|
| diff --git a/headless/lib/browser/headless_devtools_manager_delegate.cc b/headless/lib/browser/headless_devtools_manager_delegate.cc
|
| index 040b8207c9e89b1fae5ff68455756533ce756c14..7483270f26a2e9a9882cd24e0d7c5ebe7100c031 100644
|
| --- a/headless/lib/browser/headless_devtools_manager_delegate.cc
|
| +++ b/headless/lib/browser/headless_devtools_manager_delegate.cc
|
| @@ -16,13 +16,8 @@
|
| #include "headless/lib/browser/headless_browser_impl.h"
|
| #include "headless/lib/browser/headless_web_contents_impl.h"
|
| #include "headless/public/devtools/domains/target.h"
|
| -#include "printing/features/features.h"
|
| #include "ui/base/resource/resource_bundle.h"
|
|
|
| -#if BUILDFLAG(ENABLE_BASIC_PRINTING)
|
| -#include "headless/lib/browser/headless_print_manager.h"
|
| -#endif
|
| -
|
| namespace headless {
|
|
|
| namespace {
|
| @@ -32,6 +27,11 @@ const char kErrorParam[] = "error";
|
| const char kErrorCodeParam[] = "code";
|
| const char kErrorMessageParam[] = "message";
|
|
|
| +// The max and min value should match the ones in scaling_settings.html.
|
| +// Update both files at the same time.
|
| +const double kScaleMaxVal = 200;
|
| +const double kScaleMinVal = 10;
|
| +
|
| // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
|
| enum Error {
|
| kErrorInvalidParam = -32602,
|
| @@ -94,6 +94,74 @@ void PDFCreated(
|
|
|
| } // namespace
|
|
|
| +#if BUILDFLAG(ENABLE_BASIC_PRINTING)
|
| +std::unique_ptr<base::DictionaryValue> ParsePrintSettings(
|
| + int command_id,
|
| + const base::DictionaryValue* params,
|
| + printing::HeadlessPrintSettings* settings) {
|
| + const double kMMToInch = 1 / 25.4;
|
| + // we can safely ignore the return values of the following Get methods since
|
| + // the defaults are already set in |settings|.
|
| + params->GetBoolean("landscape", &settings->landscape);
|
| + params->GetBoolean("displayHeaderFooter", &settings->display_header_footer);
|
| + params->GetBoolean("printBackgrounds", &settings->should_print_backgrounds);
|
| + params->GetDouble("scale", &settings->scale);
|
| + if (settings->scale > kScaleMaxVal / 100 ||
|
| + settings->scale < kScaleMinVal / 100)
|
| + return CreateInvalidParamResponse(command_id, "scale");
|
| + params->GetString("pageRanges", &settings->page_ranges);
|
| +
|
| + std::string paper_type = "NA_LETTER";
|
| + params->GetString("paperType", &paper_type);
|
| + if (paper_type == "NA_LETTER") {
|
| + settings->paper_type = printing::HeadlessPrintSettings::NA_LETTER;
|
| + } else if (paper_type == "NA_LEGAL") {
|
| + settings->paper_type = printing::HeadlessPrintSettings::NA_LEGAL;
|
| + } else if (paper_type == "ISO_A4") {
|
| + settings->paper_type = printing::HeadlessPrintSettings::ISO_A4;
|
| + } else if (paper_type == "ISO_A3") {
|
| + settings->paper_type = printing::HeadlessPrintSettings::ISO_A3;
|
| + } else if (paper_type == "CUSTOM") {
|
| + settings->paper_type = printing::HeadlessPrintSettings::CUSTOM;
|
| + if (!params->GetDouble("paperWidth", &settings->paper_width_in_inch))
|
| + return CreateInvalidParamResponse(command_id, "paperWidth");
|
| + if (!params->GetDouble("paperHeight", &settings->paper_height_in_inch))
|
| + return CreateInvalidParamResponse(command_id, "paperHeight");
|
| + } else {
|
| + return CreateInvalidParamResponse(command_id, "paperType");
|
| + }
|
| +
|
| + bool custom_margin = false;
|
| + if (params->GetDouble("marginTop", &settings->margin_top_in_inch))
|
| + custom_margin = true;
|
| + if (params->GetDouble("marginBottom", &settings->margin_bottom_in_inch))
|
| + custom_margin = true;
|
| + if (params->GetDouble("marginLeft", &settings->margin_left_in_inch))
|
| + custom_margin = true;
|
| + if (params->GetDouble("marginRight", &settings->margin_right_in_inch))
|
| + custom_margin = true;
|
| + if (custom_margin) {
|
| + settings->margin_type = printing::CUSTOM_MARGINS;
|
| + } else {
|
| + settings->margin_type = printing::DEFAULT_MARGINS;
|
| + }
|
| +
|
| + std::string unit = "INCH";
|
| + if (params->GetString("unit", &unit) && unit != "MM" && unit != "INCH")
|
| + return CreateInvalidParamResponse(command_id, "unit");
|
| + if (unit == "MM") {
|
| + settings->paper_width_in_inch *= kMMToInch;
|
| + settings->paper_height_in_inch *= kMMToInch;
|
| + settings->margin_top_in_inch *= kMMToInch;
|
| + settings->margin_bottom_in_inch *= kMMToInch;
|
| + settings->margin_left_in_inch *= kMMToInch;
|
| + settings->margin_right_in_inch *= kMMToInch;
|
| + }
|
| +
|
| + return nullptr;
|
| +}
|
| +#endif
|
| +
|
| HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate(
|
| base::WeakPtr<HeadlessBrowserImpl> browser)
|
| : browser_(std::move(browser)) {
|
| @@ -189,14 +257,27 @@ void HeadlessDevToolsManagerDelegate::PrintToPDF(
|
| int command_id,
|
| const base::DictionaryValue* params,
|
| const CommandCallback& callback) {
|
| + DCHECK(callback);
|
| +
|
| #if BUILDFLAG(ENABLE_BASIC_PRINTING)
|
| +#if !BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
| + LOG(WARNING)
|
| + << "Print preview is not enabled. Some print settings may not work.";
|
| +#endif
|
| content::WebContents* web_contents = agent_host->GetWebContents();
|
| content::RenderFrameHost* rfh = web_contents->GetMainFrame();
|
|
|
| + printing::HeadlessPrintSettings settings;
|
| + std::unique_ptr<base::DictionaryValue> response =
|
| + ParsePrintSettings(command_id, params, &settings);
|
| + if (response) {
|
| + callback.Run(std::move(response));
|
| + return;
|
| + }
|
| printing::HeadlessPrintManager::FromWebContents(web_contents)
|
| - ->GetPDFContents(rfh, base::Bind(&PDFCreated, callback, command_id));
|
| + ->GetPDFContents(rfh, settings,
|
| + base::Bind(&PDFCreated, callback, command_id));
|
| #else
|
| - DCHECK(callback);
|
| callback.Run(CreateErrorResponse(command_id, kErrorServerError,
|
| "Printing is not enabled"));
|
| #endif
|
|
|