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

Side by Side Diff: headless/app/headless_shell.cc

Issue 2780433002: add print to pdf for headless (Closed)
Patch Set: Created 3 years, 9 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 <memory> 5 #include <memory>
6 #include <sstream> 6 #include <sstream>
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/base_switches.h" 10 #include "base/base_switches.h"
(...skipping 14 matching lines...) Expand all
25 #include "net/base/ip_address.h" 25 #include "net/base/ip_address.h"
26 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
27 #include "ui/gfx/geometry/size.h" 27 #include "ui/gfx/geometry/size.h"
28 28
29 namespace headless { 29 namespace headless {
30 namespace { 30 namespace {
31 // Address where to listen to incoming DevTools connections. 31 // Address where to listen to incoming DevTools connections.
32 const char kDevToolsHttpServerAddress[] = "127.0.0.1"; 32 const char kDevToolsHttpServerAddress[] = "127.0.0.1";
33 // Default file name for screenshot. Can be overriden by "--screenshot" switch. 33 // Default file name for screenshot. Can be overriden by "--screenshot" switch.
34 const char kDefaultScreenshotFileName[] = "screenshot.png"; 34 const char kDefaultScreenshotFileName[] = "screenshot.png";
35 // Default file name for pdf. Can be overriden by "--print-to-pdf" switch.
36 const char kDefaultPDFFileName[] = "output.pdf";
35 37
36 bool ParseWindowSize(std::string window_size, gfx::Size* parsed_window_size) { 38 bool ParseWindowSize(std::string window_size, gfx::Size* parsed_window_size) {
37 int width, height = 0; 39 int width, height = 0;
38 if (sscanf(window_size.c_str(), "%d%*[x,]%d", &width, &height) >= 2 && 40 if (sscanf(window_size.c_str(), "%d%*[x,]%d", &width, &height) >= 2 &&
39 width >= 0 && height >= 0) { 41 width >= 0 && height >= 0) {
40 parsed_window_size->set_width(width); 42 parsed_window_size->set_width(width);
41 parsed_window_size->set_height(height); 43 parsed_window_size->set_height(height);
42 return true; 44 return true;
43 } 45 }
44 return false; 46 return false;
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpDom)) { 250 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpDom)) {
249 FetchDom(); 251 FetchDom();
250 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( 252 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch(
251 switches::kRepl)) { 253 switches::kRepl)) {
252 LOG(INFO) 254 LOG(INFO)
253 << "Type a Javascript expression to evaluate or \"quit\" to exit."; 255 << "Type a Javascript expression to evaluate or \"quit\" to exit.";
254 InputExpression(); 256 InputExpression();
255 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( 257 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch(
256 switches::kScreenshot)) { 258 switches::kScreenshot)) {
257 CaptureScreenshot(); 259 CaptureScreenshot();
260 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch(
261 switches::kPrintToPDF)) {
262 PrintToPDF();
258 } else { 263 } else {
259 Shutdown(); 264 Shutdown();
260 } 265 }
261 } 266 }
262 267
263 void HeadlessShell::FetchDom() { 268 void HeadlessShell::FetchDom() {
264 devtools_client_->GetRuntime()->Evaluate( 269 devtools_client_->GetRuntime()->Evaluate(
265 "document.body.outerHTML", 270 "document.body.outerHTML",
266 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr())); 271 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr()));
267 } 272 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 317
313 void HeadlessShell::CaptureScreenshot() { 318 void HeadlessShell::CaptureScreenshot() {
314 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot( 319 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot(
315 page::CaptureScreenshotParams::Builder().SetFromSurface(true).Build(), 320 page::CaptureScreenshotParams::Builder().SetFromSurface(true).Build(),
316 base::Bind(&HeadlessShell::OnScreenshotCaptured, 321 base::Bind(&HeadlessShell::OnScreenshotCaptured,
317 weak_factory_.GetWeakPtr())); 322 weak_factory_.GetWeakPtr()));
318 } 323 }
319 324
320 void HeadlessShell::OnScreenshotCaptured( 325 void HeadlessShell::OnScreenshotCaptured(
321 std::unique_ptr<page::CaptureScreenshotResult> result) { 326 std::unique_ptr<page::CaptureScreenshotResult> result) {
327 WriteFile(switches::kScreenshot, kDefaultScreenshotFileName,
328 result->GetData());
329 }
330
331 void HeadlessShell::PrintToPDF() {
332 devtools_client_->GetPage()->GetExperimental()->PrintToPDF(
333 page::PrintToPDFParams::Builder().Build(),
334 base::Bind(&HeadlessShell::OnPDFCreated, weak_factory_.GetWeakPtr()));
335 }
336
337 void HeadlessShell::OnPDFCreated(
338 std::unique_ptr<page::PrintToPDFResult> result) {
339 if (!result) {
340 LOG(ERROR) << "Print to PDF failed";
341 Shutdown();
342 return;
343 }
344 WriteFile(switches::kPrintToPDF, kDefaultPDFFileName, result->GetData());
345 }
346
347 void HeadlessShell::WriteFile(const std::string& switch_string,
348 const std::string& default_file_name,
349 const std::string& data) {
322 base::FilePath file_name = 350 base::FilePath file_name =
323 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( 351 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(switch_string);
324 switches::kScreenshot);
325 if (file_name.empty()) { 352 if (file_name.empty()) {
326 file_name = base::FilePath().AppendASCII(kDefaultScreenshotFileName); 353 file_name = base::FilePath().AppendASCII(default_file_name);
327 } 354 }
328 355
329 screenshot_file_proxy_.reset( 356 file_proxy_.reset(new base::FileProxy(browser_->BrowserFileThread().get()));
330 new base::FileProxy(browser_->BrowserFileThread().get())); 357 if (!file_proxy_->CreateOrOpen(
331 if (!screenshot_file_proxy_->CreateOrOpen(
332 file_name, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, 358 file_name, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE,
333 base::Bind(&HeadlessShell::OnScreenshotFileOpened, 359 base::Bind(&HeadlessShell::OnFileOpened, weak_factory_.GetWeakPtr(),
334 weak_factory_.GetWeakPtr(), 360 data, file_name))) {
335 base::Passed(std::move(result)), file_name))) {
336 // Operation could not be started. 361 // Operation could not be started.
337 OnScreenshotFileOpened(nullptr, file_name, base::File::FILE_ERROR_FAILED); 362 OnFileOpened("", file_name, base::File::FILE_ERROR_FAILED);
338 } 363 }
339 } 364 }
340 365
341 void HeadlessShell::OnScreenshotFileOpened( 366 void HeadlessShell::OnFileOpened(const std::string& data,
342 std::unique_ptr<page::CaptureScreenshotResult> result, 367 const base::FilePath file_name,
343 const base::FilePath file_name, 368 base::File::Error error_code) {
344 base::File::Error error_code) { 369 if (!file_proxy_->IsValid()) {
345 if (!screenshot_file_proxy_->IsValid()) { 370 LOG(ERROR) << "Writing to file " << file_name.value()
346 LOG(ERROR) << "Writing screenshot to file " << file_name.value()
347 << " was unsuccessful, could not open file: " 371 << " was unsuccessful, could not open file: "
348 << base::File::ErrorToString(error_code); 372 << base::File::ErrorToString(error_code);
349 return; 373 return;
350 } 374 }
351 375
352 std::string decoded_png; 376 std::string decoded_data;
353 base::Base64Decode(result->GetData(), &decoded_png); 377 base::Base64Decode(data, &decoded_data);
354 scoped_refptr<net::IOBufferWithSize> buf = 378 scoped_refptr<net::IOBufferWithSize> buf =
355 new net::IOBufferWithSize(decoded_png.size()); 379 new net::IOBufferWithSize(decoded_data.size());
356 memcpy(buf->data(), decoded_png.data(), decoded_png.size()); 380 memcpy(buf->data(), decoded_data.data(), decoded_data.size());
357 381
358 if (!screenshot_file_proxy_->Write( 382 if (!file_proxy_->Write(
359 0, buf->data(), buf->size(), 383 0, buf->data(), buf->size(),
360 base::Bind(&HeadlessShell::OnScreenshotFileWritten, 384 base::Bind(&HeadlessShell::OnFileWritten, weak_factory_.GetWeakPtr(),
361 weak_factory_.GetWeakPtr(), file_name, buf->size()))) { 385 file_name, buf->size()))) {
362 // Operation may have completed successfully or failed. 386 // Operation may have completed successfully or failed.
363 OnScreenshotFileWritten(file_name, buf->size(), 387 OnFileWritten(file_name, buf->size(), base::File::FILE_ERROR_FAILED, 0);
364 base::File::FILE_ERROR_FAILED, 0);
365 } 388 }
366 } 389 }
367 390
368 void HeadlessShell::OnScreenshotFileWritten(const base::FilePath file_name, 391 void HeadlessShell::OnFileWritten(const base::FilePath file_name,
369 const int length, 392 const int length,
370 base::File::Error error_code, 393 base::File::Error error_code,
371 int write_result) { 394 int write_result) {
372 if (write_result < length) { 395 if (write_result < length) {
373 // TODO(eseckler): Support recovering from partial writes. 396 // TODO(eseckler): Support recovering from partial writes.
374 LOG(ERROR) << "Writing screenshot to file " << file_name.value() 397 LOG(ERROR) << "Writing to file " << file_name.value()
375 << " was unsuccessful: " << net::ErrorToString(write_result); 398 << " was unsuccessful: " << net::ErrorToString(write_result);
376 } else { 399 } else {
377 LOG(INFO) << "Screenshot written to file " << file_name.value() << "." 400 LOG(INFO) << "Written to file " << file_name.value() << ".";
378 << std::endl;
379 } 401 }
380 if (!screenshot_file_proxy_->Close( 402 if (!file_proxy_->Close(base::Bind(&HeadlessShell::OnFileClosed,
381 base::Bind(&HeadlessShell::OnScreenshotFileClosed, 403 weak_factory_.GetWeakPtr()))) {
382 weak_factory_.GetWeakPtr()))) {
383 // Operation could not be started. 404 // Operation could not be started.
384 OnScreenshotFileClosed(base::File::FILE_ERROR_FAILED); 405 OnFileClosed(base::File::FILE_ERROR_FAILED);
385 } 406 }
386 } 407 }
387 408
388 void HeadlessShell::OnScreenshotFileClosed(base::File::Error error_code) { 409 void HeadlessShell::OnFileClosed(base::File::Error error_code) {
389 Shutdown(); 410 Shutdown();
390 } 411 }
391 412
392 bool HeadlessShell::RemoteDebuggingEnabled() const { 413 bool HeadlessShell::RemoteDebuggingEnabled() const {
393 const base::CommandLine& command_line = 414 const base::CommandLine& command_line =
394 *base::CommandLine::ForCurrentProcess(); 415 *base::CommandLine::ForCurrentProcess();
395 return command_line.HasSwitch(switches::kRemoteDebuggingPort); 416 return command_line.HasSwitch(switches::kRemoteDebuggingPort);
396 } 417 }
397 418
398 bool ValidateCommandLine(const base::CommandLine& command_line) { 419 bool ValidateCommandLine(const base::CommandLine& command_line) {
(...skipping 11 matching lines...) Expand all
410 if (command_line.HasSwitch(switches::kRepl)) { 431 if (command_line.HasSwitch(switches::kRepl)) {
411 LOG(ERROR) << "Evaluate Javascript is disabled " 432 LOG(ERROR) << "Evaluate Javascript is disabled "
412 << "when remote debugging is enabled."; 433 << "when remote debugging is enabled.";
413 return false; 434 return false;
414 } 435 }
415 if (command_line.HasSwitch(switches::kScreenshot)) { 436 if (command_line.HasSwitch(switches::kScreenshot)) {
416 LOG(ERROR) << "Capture screenshot is disabled " 437 LOG(ERROR) << "Capture screenshot is disabled "
417 << "when remote debugging is enabled."; 438 << "when remote debugging is enabled.";
418 return false; 439 return false;
419 } 440 }
441 if (command_line.HasSwitch(switches::kPrintToPDF)) {
442 LOG(ERROR) << "Print to PDF is disabled "
443 << "when remote debugging is enabled.";
444 return false;
445 }
420 if (command_line.HasSwitch(switches::kTimeout)) { 446 if (command_line.HasSwitch(switches::kTimeout)) {
421 LOG(ERROR) << "Navigation timeout is disabled " 447 LOG(ERROR) << "Navigation timeout is disabled "
422 << "when remote debugging is enabled."; 448 << "when remote debugging is enabled.";
423 return false; 449 return false;
424 } 450 }
425 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { 451 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) {
426 LOG(ERROR) << "Virtual time budget is disabled " 452 LOG(ERROR) << "Virtual time budget is disabled "
427 << "when remote debugging is enabled."; 453 << "when remote debugging is enabled.";
428 return false; 454 return false;
429 } 455 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 builder.SetOverrideWebPreferencesCallback(base::Bind([]( 544 builder.SetOverrideWebPreferencesCallback(base::Bind([](
519 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); 545 WebPreferences* preferences) { preferences->hide_scrollbars = true; }));
520 } 546 }
521 547
522 return HeadlessBrowserMain( 548 return HeadlessBrowserMain(
523 builder.Build(), 549 builder.Build(),
524 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); 550 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell)));
525 } 551 }
526 552
527 } // namespace headless 553 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698