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> ParsePrintSettings( | |
Lei Zhang
2017/04/20 08:35:58
Now that you have written a parser, you probably w
jzfeng
2017/04/27 06:56:06
Done. Added headless_printing_unittest.cc to do al
| |
79 int command_id, | |
80 const base::DictionaryValue* params, | |
81 printing::HeadlessPrintSettings* settings) { | |
82 params->GetInteger("dpi", &settings->dpi); | |
Eric Seckler
2017/04/20 09:15:28
nit: add a comment that we can safely ignore retur
jzfeng
2017/04/27 06:56:06
Done.
| |
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) | |
Lei Zhang
2017/04/20 08:35:58
In print preview, the valid range is [10, 200]. Ho
jzfeng
2017/04/27 06:56:06
Done.
| |
88 return CreateInvalidParamResponse(command_id, "scale"); | |
89 | |
90 std::string paper_type; | |
91 if (params->GetString("paperType", &paper_type)) { | |
92 if (paper_type == "letter") | |
Lei Zhang
2017/04/20 08:35:58
Maybe use the same names as https://developers.goo
jzfeng
2017/04/27 06:56:05
Done.
| |
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)) | |
Lei Zhang
2017/04/20 08:35:58
Can you try setting this to 20 inches and see what
jzfeng
2017/04/27 06:56:06
I tested it, this type of error has been handled b
| |
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 { | |
Lei Zhang
2017/04/20 08:35:58
Did you leave out PRINTABLE_AREA_MARGINS on purpos
jzfeng
2017/04/27 06:56:06
Yeah. Now I further restrict the type to be DEFAUL
| |
124 return CreateInvalidParamResponse(command_id, "marginType"); | |
125 } | |
126 } | |
127 | |
128 std::string page_ranges; | |
129 if (params->GetString("pageRanges", &page_ranges)) { | |
Lei Zhang
2017/04/20 09:15:05
If you want to get fancy, see pageRangeTextToPageR
jzfeng
2017/04/27 06:56:06
Re-implement the logic in headless_print_manager.c
| |
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); | |
Lei Zhang
2017/04/20 08:35:58
StringToInt() comes with a 16 line comment about i
jzfeng
2017/04/27 06:56:06
Added unit test for it.
| |
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."; | |
Lei Zhang
2017/04/20 08:35:58
Which settings don't work? Is it just headers+foot
jzfeng
2017/04/27 06:56:06
headers+footers and scale don't work.
In PrintWe
Lei Zhang
2017/04/27 22:17:46
I would suggest experimenting with making these fe
Eric Seckler
2017/04/28 08:13:06
If this works, would we be able to avoid adding th
jzfeng
2017/05/02 07:50:55
In PrintWebViewHelper,
1) I removed some of the BU
| |
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 = ParsePrintSettings(command_id, params, &settings); | |
Lei Zhang
2017/04/20 08:35:58
You may want to write out the type instead of usin
Lei Zhang
2017/04/20 08:53:12
See https://google.github.io/styleguide/cppguide.h
jzfeng
2017/04/27 06:56:06
Done.
| |
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 |