OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "headless/lib/browser/headless_devtools_manager_delegate.h" | 5 #include "headless/lib/browser/headless_devtools_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" |
10 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/devtools_agent_host.h" | 13 #include "content/public/browser/devtools_agent_host.h" |
12 #include "content/public/browser/devtools_frontend_host.h" | 14 #include "content/public/browser/devtools_frontend_host.h" |
13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
14 #include "headless/grit/headless_lib_resources.h" | 16 #include "headless/grit/headless_lib_resources.h" |
15 #include "headless/lib/browser/headless_browser_context_impl.h" | 17 #include "headless/lib/browser/headless_browser_context_impl.h" |
16 #include "headless/lib/browser/headless_browser_impl.h" | 18 #include "headless/lib/browser/headless_browser_impl.h" |
17 #include "headless/lib/browser/headless_web_contents_impl.h" | 19 #include "headless/lib/browser/headless_web_contents_impl.h" |
18 #include "headless/public/devtools/domains/target.h" | 20 #include "headless/public/devtools/domains/target.h" |
19 #include "printing/features/features.h" | 21 #include "printing/features/features.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 68 |
67 std::unique_ptr<base::DictionaryValue> CreateInvalidParamResponse( | 69 std::unique_ptr<base::DictionaryValue> CreateInvalidParamResponse( |
68 int command_id, | 70 int command_id, |
69 const std::string& param) { | 71 const std::string& param) { |
70 return CreateErrorResponse( | 72 return CreateErrorResponse( |
71 command_id, kErrorInvalidParam, | 73 command_id, kErrorInvalidParam, |
72 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); | 74 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
73 } | 75 } |
74 | 76 |
75 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 77 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 78 std::unique_ptr<base::DictionaryValue> ParsePrintParams( |
| 79 int command_id, |
| 80 const base::DictionaryValue* params, |
| 81 printing::HeadlessPrintSettings* settings) { |
| 82 params->GetInteger("dpi", &settings->dpi); |
| 83 params->GetBoolean("landscape", &settings->landscape); |
| 84 params->GetBoolean("displayHeaderFooter", &settings->display_header_footer); |
| 85 params->GetBoolean("printBackgrounds", &settings->should_print_backgrounds); |
| 86 params->GetDouble("scale", &settings->scale); |
| 87 if (settings->scale < 0) |
| 88 return CreateInvalidParamResponse(command_id, "scale"); |
| 89 |
| 90 std::string paper_type; |
| 91 if (params->GetString("paperType", &paper_type)) { |
| 92 if (paper_type == "letter") |
| 93 settings->paper_type = printing::HeadlessPrintSettings::LETTER; |
| 94 else if (paper_type == "legal") |
| 95 settings->paper_type = printing::HeadlessPrintSettings::LEGAL; |
| 96 else if (paper_type == "A4") |
| 97 settings->paper_type = printing::HeadlessPrintSettings::A4; |
| 98 else if (paper_type == "A3") |
| 99 settings->paper_type = printing::HeadlessPrintSettings::A3; |
| 100 else |
| 101 return CreateInvalidParamResponse(command_id, "paperType"); |
| 102 } |
| 103 |
| 104 std::string margin_type; |
| 105 if (params->GetString("marginType", &margin_type)) { |
| 106 if (margin_type == "defaultMargin") { |
| 107 settings->margin_type = printing::DEFAULT_MARGINS; |
| 108 } else if (margin_type == "noMargin") { |
| 109 settings->margin_type = printing::NO_MARGINS; |
| 110 } else if (margin_type == "customMargin") { |
| 111 settings->margin_type = printing::CUSTOM_MARGINS; |
| 112 if (!params->GetDouble("marginTop", &settings->margin_top_in_inch)) |
| 113 return CreateInvalidParamResponse(command_id, "marginTop"); |
| 114 if (!params->GetDouble("marginBottom", |
| 115 &settings->margin_bottom_in_inch)) { |
| 116 return CreateInvalidParamResponse(command_id, "marginBottom"); |
| 117 } |
| 118 if (!params->GetDouble("marginLeft", &settings->margin_left_in_inch)) |
| 119 return CreateInvalidParamResponse(command_id, "marginLeft"); |
| 120 if (!params->GetDouble("marginRight", &settings->margin_right_in_inch)) { |
| 121 return CreateInvalidParamResponse(command_id, "marginRight"); |
| 122 } |
| 123 } else { |
| 124 return CreateInvalidParamResponse(command_id, "marginType"); |
| 125 } |
| 126 } |
| 127 |
| 128 std::string page_ranges; |
| 129 if (params->GetString("pageRanges", &page_ranges)) { |
| 130 for (const auto& range_string : |
| 131 base::SplitStringPiece(page_ranges, ",", base::TRIM_WHITESPACE, |
| 132 base::SPLIT_WANT_NONEMPTY)) { |
| 133 auto tokens = base::SplitStringPiece( |
| 134 range_string, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 135 if (tokens.size() > 2 || tokens.empty()) |
| 136 return CreateInvalidParamResponse(command_id, "pageRanges"); |
| 137 printing::PageRange range; |
| 138 base::StringToInt(tokens[0], &range.from); |
| 139 if (tokens.size() == 2) |
| 140 base::StringToInt(tokens[1], &range.to); |
| 141 else |
| 142 range.to = range.from; |
| 143 if (range.from < 1 || range.from > range.to) |
| 144 return CreateInvalidParamResponse(command_id, "pageRanges"); |
| 145 |
| 146 // Page numbers are 1-based in the dictionary. |
| 147 // Page numbers are 0-based for the print settings. |
| 148 range.from--; |
| 149 range.to--; |
| 150 settings->page_ranges.push_back(range); |
| 151 } |
| 152 } |
| 153 |
| 154 return nullptr; |
| 155 } |
| 156 |
76 void PDFCreated( | 157 void PDFCreated( |
77 const content::DevToolsManagerDelegate::CommandCallback& callback, | 158 const content::DevToolsManagerDelegate::CommandCallback& callback, |
78 int command_id, | 159 int command_id, |
79 printing::HeadlessPrintManager::PrintResult print_result, | 160 printing::HeadlessPrintManager::PrintResult print_result, |
80 const std::string& data) { | 161 const std::string& data) { |
81 std::unique_ptr<base::DictionaryValue> response; | 162 std::unique_ptr<base::DictionaryValue> response; |
82 if (print_result == printing::HeadlessPrintManager::PRINT_SUCCESS) { | 163 if (print_result == printing::HeadlessPrintManager::PRINT_SUCCESS) { |
83 response = CreateSuccessResponse( | 164 response = CreateSuccessResponse( |
84 command_id, | 165 command_id, |
85 printing::HeadlessPrintManager::PDFContentsToDictionaryValue(data)); | 166 printing::HeadlessPrintManager::PDFContentsToDictionaryValue(data)); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( | 263 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( |
183 const std::string& path) { | 264 const std::string& path) { |
184 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); | 265 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
185 } | 266 } |
186 | 267 |
187 void HeadlessDevToolsManagerDelegate::PrintToPDF( | 268 void HeadlessDevToolsManagerDelegate::PrintToPDF( |
188 content::DevToolsAgentHost* agent_host, | 269 content::DevToolsAgentHost* agent_host, |
189 int command_id, | 270 int command_id, |
190 const base::DictionaryValue* params, | 271 const base::DictionaryValue* params, |
191 const CommandCallback& callback) { | 272 const CommandCallback& callback) { |
| 273 DCHECK(callback); |
| 274 |
192 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 275 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 276 #if !BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 277 LOG(WARNING) |
| 278 << "Print preview is not enabled. Some print settings may not work"; |
| 279 #endif |
193 content::WebContents* web_contents = agent_host->GetWebContents(); | 280 content::WebContents* web_contents = agent_host->GetWebContents(); |
194 content::RenderFrameHost* rfh = web_contents->GetMainFrame(); | 281 content::RenderFrameHost* rfh = web_contents->GetMainFrame(); |
195 | 282 |
| 283 printing::HeadlessPrintSettings settings; |
| 284 auto response = ParsePrintParams(command_id, params, &settings); |
| 285 if (response) { |
| 286 callback.Run(std::move(response)); |
| 287 return; |
| 288 } |
196 printing::HeadlessPrintManager::FromWebContents(web_contents) | 289 printing::HeadlessPrintManager::FromWebContents(web_contents) |
197 ->GetPDFContents(rfh, base::Bind(&PDFCreated, callback, command_id)); | 290 ->GetPDFContents(rfh, settings, |
| 291 base::Bind(&PDFCreated, callback, command_id)); |
198 #else | 292 #else |
199 DCHECK(callback); | |
200 callback.Run(CreateErrorResponse(command_id, kErrorServerError, | 293 callback.Run(CreateErrorResponse(command_id, kErrorServerError, |
201 "Printing is not enabled")); | 294 "Printing is not enabled")); |
202 #endif | 295 #endif |
203 } | 296 } |
204 | 297 |
205 std::unique_ptr<base::DictionaryValue> | 298 std::unique_ptr<base::DictionaryValue> |
206 HeadlessDevToolsManagerDelegate::CreateTarget( | 299 HeadlessDevToolsManagerDelegate::CreateTarget( |
207 int command_id, | 300 int command_id, |
208 const base::DictionaryValue* params) { | 301 const base::DictionaryValue* params) { |
209 std::string url; | 302 std::string url; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 394 |
302 std::unique_ptr<base::Value> result( | 395 std::unique_ptr<base::Value> result( |
303 target::DisposeBrowserContextResult::Builder() | 396 target::DisposeBrowserContextResult::Builder() |
304 .SetSuccess(success) | 397 .SetSuccess(success) |
305 .Build() | 398 .Build() |
306 ->Serialize()); | 399 ->Serialize()); |
307 return CreateSuccessResponse(command_id, std::move(result)); | 400 return CreateSuccessResponse(command_id, std::move(result)); |
308 } | 401 } |
309 | 402 |
310 } // namespace headless | 403 } // namespace headless |
OLD | NEW |