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