Chromium Code Reviews| 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 WriteFile(switches::kScreenshot, kDefaultScreenshotFileName, | |
| 329 result->GetData()); | |
| 330 } | |
| 331 | |
| 332 void HeadlessShell::PrintToPDF() { | |
| 333 devtools_client_->GetPage()->GetExperimental()->PrintToPDF( | |
| 334 page::PrintToPDFParams::Builder().Build(), | |
| 335 base::Bind(&HeadlessShell::OnPDFCreated, weak_factory_.GetWeakPtr())); | |
| 336 } | |
| 337 | |
| 338 void HeadlessShell::OnPDFCreated( | |
| 339 std::unique_ptr<page::PrintToPDFResult> result) { | |
| 340 if (!result) { | |
| 
 
Eric Seckler
2017/03/29 11:21:19
For consistency, could you add the same check to O
 
jzfeng
2017/03/30 03:04:56
Done.
 
 | |
| 341 LOG(ERROR) << "Print to PDF failed"; | |
| 342 Shutdown(); | |
| 343 return; | |
| 344 } | |
| 345 WriteFile(switches::kPrintToPDF, kDefaultPDFFileName, result->GetData()); | |
| 346 } | |
| 347 | |
| 348 void HeadlessShell::WriteFile(const std::string& switch_string, | |
| 
 
Eric Seckler
2017/03/29 11:21:19
nit: file_path_switch
 
jzfeng
2017/03/30 03:04:56
Done.
 
 | |
| 349 const std::string& default_file_name, | |
| 350 const std::string& data) { | |
| 
 
Eric Seckler
2017/03/29 11:21:19
nit: base64_data
 
jzfeng
2017/03/30 03:04:56
Done.
 
 | |
| 322 base::FilePath file_name = | 351 base::FilePath file_name = | 
| 323 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 352 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(switch_string); | 
| 324 switches::kScreenshot); | |
| 325 if (file_name.empty()) { | 353 if (file_name.empty()) { | 
| 326 file_name = base::FilePath().AppendASCII(kDefaultScreenshotFileName); | 354 file_name = base::FilePath().AppendASCII(default_file_name); | 
| 327 } | 355 } | 
| 328 | 356 | 
| 329 screenshot_file_proxy_.reset( | 357 file_proxy_.reset(new base::FileProxy(browser_->BrowserFileThread().get())); | 
| 330 new base::FileProxy(browser_->BrowserFileThread().get())); | 358 if (!file_proxy_->CreateOrOpen( | 
| 331 if (!screenshot_file_proxy_->CreateOrOpen( | |
| 332 file_name, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, | 359 file_name, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, | 
| 333 base::Bind(&HeadlessShell::OnScreenshotFileOpened, | 360 base::Bind(&HeadlessShell::OnFileOpened, weak_factory_.GetWeakPtr(), | 
| 334 weak_factory_.GetWeakPtr(), | 361 data, file_name))) { | 
| 335 base::Passed(std::move(result)), file_name))) { | |
| 336 // Operation could not be started. | 362 // Operation could not be started. | 
| 337 OnScreenshotFileOpened(nullptr, file_name, base::File::FILE_ERROR_FAILED); | 363 OnFileOpened("", file_name, base::File::FILE_ERROR_FAILED); | 
| 
 
Lei Zhang
2017/03/29 05:30:15
Pass in a std::string() instead of "". There's a f
 
jzfeng
2017/03/30 03:04:56
Done.
 
 | |
| 338 } | 364 } | 
| 339 } | 365 } | 
| 340 | 366 | 
| 341 void HeadlessShell::OnScreenshotFileOpened( | 367 void HeadlessShell::OnFileOpened(const std::string& data, | 
| 342 std::unique_ptr<page::CaptureScreenshotResult> result, | 368 const base::FilePath file_name, | 
| 343 const base::FilePath file_name, | 369 base::File::Error error_code) { | 
| 344 base::File::Error error_code) { | 370 if (!file_proxy_->IsValid()) { | 
| 345 if (!screenshot_file_proxy_->IsValid()) { | 371 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: " | 372 << " was unsuccessful, could not open file: " | 
| 348 << base::File::ErrorToString(error_code); | 373 << base::File::ErrorToString(error_code); | 
| 349 return; | 374 return; | 
| 350 } | 375 } | 
| 351 | 376 | 
| 352 std::string decoded_png; | 377 std::string decoded_data; | 
| 353 base::Base64Decode(result->GetData(), &decoded_png); | 378 base::Base64Decode(data, &decoded_data); | 
| 
 
Lei Zhang
2017/03/29 05:30:15
base::Base64Decode() can fail, BTW.
 
jzfeng
2017/03/30 03:04:56
Added error handling logic.
 
 | |
| 354 scoped_refptr<net::IOBufferWithSize> buf = | 379 scoped_refptr<net::IOBufferWithSize> buf = | 
| 355 new net::IOBufferWithSize(decoded_png.size()); | 380 new net::IOBufferWithSize(decoded_data.size()); | 
| 356 memcpy(buf->data(), decoded_png.data(), decoded_png.size()); | 381 memcpy(buf->data(), decoded_data.data(), decoded_data.size()); | 
| 357 | 382 | 
| 358 if (!screenshot_file_proxy_->Write( | 383 if (!file_proxy_->Write( | 
| 359 0, buf->data(), buf->size(), | 384 0, buf->data(), buf->size(), | 
| 360 base::Bind(&HeadlessShell::OnScreenshotFileWritten, | 385 base::Bind(&HeadlessShell::OnFileWritten, weak_factory_.GetWeakPtr(), | 
| 361 weak_factory_.GetWeakPtr(), file_name, buf->size()))) { | 386 file_name, buf->size()))) { | 
| 362 // Operation may have completed successfully or failed. | 387 // Operation may have completed successfully or failed. | 
| 363 OnScreenshotFileWritten(file_name, buf->size(), | 388 OnFileWritten(file_name, buf->size(), base::File::FILE_ERROR_FAILED, 0); | 
| 364 base::File::FILE_ERROR_FAILED, 0); | |
| 365 } | 389 } | 
| 366 } | 390 } | 
| 367 | 391 | 
| 368 void HeadlessShell::OnScreenshotFileWritten(const base::FilePath file_name, | 392 void HeadlessShell::OnFileWritten(const base::FilePath file_name, | 
| 369 const int length, | 393 const int length, | 
| 370 base::File::Error error_code, | 394 base::File::Error error_code, | 
| 371 int write_result) { | 395 int write_result) { | 
| 372 if (write_result < length) { | 396 if (write_result < length) { | 
| 373 // TODO(eseckler): Support recovering from partial writes. | 397 // TODO(eseckler): Support recovering from partial writes. | 
| 374 LOG(ERROR) << "Writing screenshot to file " << file_name.value() | 398 LOG(ERROR) << "Writing to file " << file_name.value() | 
| 375 << " was unsuccessful: " << net::ErrorToString(write_result); | 399 << " was unsuccessful: " << net::ErrorToString(write_result); | 
| 
 
Eric Seckler
2017/03/29 11:21:19
ups, I think we should be using File::ErrorToStrin
 
jzfeng
2017/03/30 03:04:56
Done.
 
 | |
| 376 } else { | 400 } else { | 
| 377 LOG(INFO) << "Screenshot written to file " << file_name.value() << "." | 401 LOG(INFO) << "Written to file " << file_name.value() << "."; | 
| 378 << std::endl; | |
| 379 } | 402 } | 
| 380 if (!screenshot_file_proxy_->Close( | 403 if (!file_proxy_->Close(base::Bind(&HeadlessShell::OnFileClosed, | 
| 381 base::Bind(&HeadlessShell::OnScreenshotFileClosed, | 404 weak_factory_.GetWeakPtr()))) { | 
| 382 weak_factory_.GetWeakPtr()))) { | |
| 383 // Operation could not be started. | 405 // Operation could not be started. | 
| 384 OnScreenshotFileClosed(base::File::FILE_ERROR_FAILED); | 406 OnFileClosed(base::File::FILE_ERROR_FAILED); | 
| 385 } | 407 } | 
| 386 } | 408 } | 
| 387 | 409 | 
| 388 void HeadlessShell::OnScreenshotFileClosed(base::File::Error error_code) { | 410 void HeadlessShell::OnFileClosed(base::File::Error error_code) { | 
| 389 Shutdown(); | 411 Shutdown(); | 
| 390 } | 412 } | 
| 391 | 413 | 
| 392 bool HeadlessShell::RemoteDebuggingEnabled() const { | 414 bool HeadlessShell::RemoteDebuggingEnabled() const { | 
| 393 const base::CommandLine& command_line = | 415 const base::CommandLine& command_line = | 
| 394 *base::CommandLine::ForCurrentProcess(); | 416 *base::CommandLine::ForCurrentProcess(); | 
| 395 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 417 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 
| 396 } | 418 } | 
| 397 | 419 | 
| 398 bool ValidateCommandLine(const base::CommandLine& command_line) { | 420 bool ValidateCommandLine(const base::CommandLine& command_line) { | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 410 if (command_line.HasSwitch(switches::kRepl)) { | 432 if (command_line.HasSwitch(switches::kRepl)) { | 
| 411 LOG(ERROR) << "Evaluate Javascript is disabled " | 433 LOG(ERROR) << "Evaluate Javascript is disabled " | 
| 412 << "when remote debugging is enabled."; | 434 << "when remote debugging is enabled."; | 
| 413 return false; | 435 return false; | 
| 414 } | 436 } | 
| 415 if (command_line.HasSwitch(switches::kScreenshot)) { | 437 if (command_line.HasSwitch(switches::kScreenshot)) { | 
| 416 LOG(ERROR) << "Capture screenshot is disabled " | 438 LOG(ERROR) << "Capture screenshot is disabled " | 
| 417 << "when remote debugging is enabled."; | 439 << "when remote debugging is enabled."; | 
| 418 return false; | 440 return false; | 
| 419 } | 441 } | 
| 442 if (command_line.HasSwitch(switches::kPrintToPDF)) { | |
| 443 LOG(ERROR) << "Print to PDF is disabled " | |
| 444 << "when remote debugging is enabled."; | |
| 445 return false; | |
| 446 } | |
| 420 if (command_line.HasSwitch(switches::kTimeout)) { | 447 if (command_line.HasSwitch(switches::kTimeout)) { | 
| 421 LOG(ERROR) << "Navigation timeout is disabled " | 448 LOG(ERROR) << "Navigation timeout is disabled " | 
| 422 << "when remote debugging is enabled."; | 449 << "when remote debugging is enabled."; | 
| 423 return false; | 450 return false; | 
| 424 } | 451 } | 
| 425 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { | 452 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { | 
| 426 LOG(ERROR) << "Virtual time budget is disabled " | 453 LOG(ERROR) << "Virtual time budget is disabled " | 
| 427 << "when remote debugging is enabled."; | 454 << "when remote debugging is enabled."; | 
| 428 return false; | 455 return false; | 
| 429 } | 456 } | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 545 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 
| 519 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 546 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 
| 520 } | 547 } | 
| 521 | 548 | 
| 522 return HeadlessBrowserMain( | 549 return HeadlessBrowserMain( | 
| 523 builder.Build(), | 550 builder.Build(), | 
| 524 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 551 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 
| 525 } | 552 } | 
| 526 | 553 | 
| 527 } // namespace headless | 554 } // namespace headless | 
| OLD | NEW |