Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(565)

Side by Side Diff: headless/lib/browser/headless_devtools_manager_delegate.cc

Issue 2829973002: add customized printing setting for headless (Closed)
Patch Set: add skia as public_deps instead Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/devtools_agent_host.h" 11 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/devtools_frontend_host.h" 12 #include "content/public/browser/devtools_frontend_host.h"
13 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
14 #include "headless/grit/headless_lib_resources.h" 14 #include "headless/grit/headless_lib_resources.h"
15 #include "headless/lib/browser/headless_browser_context_impl.h" 15 #include "headless/lib/browser/headless_browser_context_impl.h"
16 #include "headless/lib/browser/headless_browser_impl.h" 16 #include "headless/lib/browser/headless_browser_impl.h"
17 #include "headless/lib/browser/headless_web_contents_impl.h" 17 #include "headless/lib/browser/headless_web_contents_impl.h"
18 #include "headless/public/devtools/domains/target.h" 18 #include "headless/public/devtools/domains/target.h"
19 #include "printing/features/features.h" 19 #include "printing/units.h"
20 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
21 21
22 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
23 #include "headless/lib/browser/headless_print_manager.h"
24 #endif
25
26 namespace headless { 22 namespace headless {
27 23
28 namespace { 24 namespace {
29 const char kIdParam[] = "id"; 25 const char kIdParam[] = "id";
30 const char kResultParam[] = "result"; 26 const char kResultParam[] = "result";
31 const char kErrorParam[] = "error"; 27 const char kErrorParam[] = "error";
32 const char kErrorCodeParam[] = "code"; 28 const char kErrorCodeParam[] = "code";
33 const char kErrorMessageParam[] = "message"; 29 const char kErrorMessageParam[] = "message";
34 30
31 // The max and min value should match the ones in scaling_settings.html.
32 // Update both files at the same time.
33 const double kScaleMaxVal = 200;
34 const double kScaleMinVal = 10;
35
35 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object 36 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
36 enum Error { 37 enum Error {
37 kErrorInvalidParam = -32602, 38 kErrorInvalidParam = -32602,
38 kErrorServerError = -32000 39 kErrorServerError = -32000
39 }; 40 };
40 41
41 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse( 42 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse(
42 int command_id, 43 int command_id,
43 std::unique_ptr<base::Value> result) { 44 std::unique_ptr<base::Value> result) {
44 if (!result) 45 if (!result)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 response = CreateErrorResponse( 88 response = CreateErrorResponse(
88 command_id, kErrorServerError, 89 command_id, kErrorServerError,
89 printing::HeadlessPrintManager::PrintResultToString(print_result)); 90 printing::HeadlessPrintManager::PrintResultToString(print_result));
90 } 91 }
91 callback.Run(std::move(response)); 92 callback.Run(std::move(response));
92 } 93 }
93 #endif 94 #endif
94 95
95 } // namespace 96 } // namespace
96 97
98 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
99 std::unique_ptr<base::DictionaryValue> ParsePrintSettings(
100 int command_id,
101 const base::DictionaryValue* params,
102 printing::HeadlessPrintSettings* settings) {
103 // We can safely ignore the return values of the following Get methods since
104 // the defaults are already set in |settings|.
105 params->GetBoolean("landscape", &settings->landscape);
106 params->GetBoolean("displayHeaderFooter", &settings->display_header_footer);
107 params->GetBoolean("printBackground", &settings->should_print_backgrounds);
108 params->GetDouble("scale", &settings->scale);
109 if (settings->scale > kScaleMaxVal / 100 ||
110 settings->scale < kScaleMinVal / 100)
111 return CreateInvalidParamResponse(command_id, "scale");
112 params->GetString("pageRanges", &settings->page_ranges);
113
114 double paper_width_in_inch = printing::kLetterWidthInch;
115 double paper_height_in_inch = printing::kLetterHeightInch;
116 params->GetDouble("paperWidth", &paper_width_in_inch);
117 params->GetDouble("paperHeight", &paper_height_in_inch);
118 if (paper_width_in_inch <= 0)
119 return CreateInvalidParamResponse(command_id, "paperWidth");
120 if (paper_height_in_inch <= 0)
121 return CreateInvalidParamResponse(command_id, "paperHeight");
122 settings->paper_size_in_points =
123 gfx::Size(paper_width_in_inch * printing::kPointsPerInch,
124 paper_height_in_inch * printing::kPointsPerInch);
125
126 // Set default margin to 1.0cm = ~2/5 of an inch.
127 double default_margin_in_inch = 1000.0 / printing::kHundrethsMMPerInch;
128 double margin_top_in_inch = default_margin_in_inch;
129 double margin_bottom_in_inch = default_margin_in_inch;
130 double margin_left_in_inch = default_margin_in_inch;
131 double margin_right_in_inch = default_margin_in_inch;
132 params->GetDouble("marginTop", &margin_top_in_inch);
133 params->GetDouble("marginBottom", &margin_bottom_in_inch);
134 params->GetDouble("marginLeft", &margin_left_in_inch);
135 params->GetDouble("marginRight", &margin_right_in_inch);
136 if (margin_top_in_inch < 0)
137 return CreateInvalidParamResponse(command_id, "marginTop");
138 if (margin_bottom_in_inch < 0)
139 return CreateInvalidParamResponse(command_id, "marginBottom");
140 if (margin_left_in_inch < 0)
141 return CreateInvalidParamResponse(command_id, "marginLeft");
142 if (margin_right_in_inch < 0)
143 return CreateInvalidParamResponse(command_id, "marginRight");
144 settings->margins_in_points.top =
145 margin_top_in_inch * printing::kPointsPerInch;
146 settings->margins_in_points.bottom =
147 margin_bottom_in_inch * printing::kPointsPerInch;
148 settings->margins_in_points.left =
149 margin_left_in_inch * printing::kPointsPerInch;
150 settings->margins_in_points.right =
151 margin_right_in_inch * printing::kPointsPerInch;
152
153 return nullptr;
154 }
155 #endif
156
97 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( 157 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate(
98 base::WeakPtr<HeadlessBrowserImpl> browser) 158 base::WeakPtr<HeadlessBrowserImpl> browser)
99 : browser_(std::move(browser)) { 159 : browser_(std::move(browser)) {
100 command_map_["Target.createTarget"] = base::Bind( 160 command_map_["Target.createTarget"] = base::Bind(
101 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this)); 161 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this));
102 command_map_["Target.closeTarget"] = base::Bind( 162 command_map_["Target.closeTarget"] = base::Bind(
103 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this)); 163 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this));
104 command_map_["Target.createBrowserContext"] = 164 command_map_["Target.createBrowserContext"] =
105 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext, 165 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext,
106 base::Unretained(this)); 166 base::Unretained(this));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( 242 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource(
183 const std::string& path) { 243 const std::string& path) {
184 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); 244 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
185 } 245 }
186 246
187 void HeadlessDevToolsManagerDelegate::PrintToPDF( 247 void HeadlessDevToolsManagerDelegate::PrintToPDF(
188 content::DevToolsAgentHost* agent_host, 248 content::DevToolsAgentHost* agent_host,
189 int command_id, 249 int command_id,
190 const base::DictionaryValue* params, 250 const base::DictionaryValue* params,
191 const CommandCallback& callback) { 251 const CommandCallback& callback) {
252 DCHECK(callback);
253
192 #if BUILDFLAG(ENABLE_BASIC_PRINTING) 254 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
193 content::WebContents* web_contents = agent_host->GetWebContents(); 255 content::WebContents* web_contents = agent_host->GetWebContents();
194 content::RenderFrameHost* rfh = web_contents->GetMainFrame(); 256 content::RenderFrameHost* rfh = web_contents->GetMainFrame();
195 257
258 printing::HeadlessPrintSettings settings;
259 std::unique_ptr<base::DictionaryValue> response =
260 ParsePrintSettings(command_id, params, &settings);
261 if (response) {
262 callback.Run(std::move(response));
263 return;
264 }
196 printing::HeadlessPrintManager::FromWebContents(web_contents) 265 printing::HeadlessPrintManager::FromWebContents(web_contents)
197 ->GetPDFContents(rfh, base::Bind(&PDFCreated, callback, command_id)); 266 ->GetPDFContents(rfh, settings,
267 base::Bind(&PDFCreated, callback, command_id));
198 #else 268 #else
199 DCHECK(callback);
200 callback.Run(CreateErrorResponse(command_id, kErrorServerError, 269 callback.Run(CreateErrorResponse(command_id, kErrorServerError,
201 "Printing is not enabled")); 270 "Printing is not enabled"));
202 #endif 271 #endif
203 } 272 }
204 273
205 std::unique_ptr<base::DictionaryValue> 274 std::unique_ptr<base::DictionaryValue>
206 HeadlessDevToolsManagerDelegate::CreateTarget( 275 HeadlessDevToolsManagerDelegate::CreateTarget(
207 int command_id, 276 int command_id,
208 const base::DictionaryValue* params) { 277 const base::DictionaryValue* params) {
209 std::string url; 278 std::string url;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 370
302 std::unique_ptr<base::Value> result( 371 std::unique_ptr<base::Value> result(
303 target::DisposeBrowserContextResult::Builder() 372 target::DisposeBrowserContextResult::Builder()
304 .SetSuccess(success) 373 .SetSuccess(success)
305 .Build() 374 .Build()
306 ->Serialize()); 375 ->Serialize());
307 return CreateSuccessResponse(command_id, std::move(result)); 376 return CreateSuccessResponse(command_id, std::move(result));
308 } 377 }
309 378
310 } // namespace headless 379 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698