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 return; | |
Eric Seckler
2017/04/03 10:34:34
Should also close the file in this case. Maybe cal
jzfeng
2017/04/04 05:05:19
Done.
| |
386 } | |
387 | |
354 scoped_refptr<net::IOBufferWithSize> buf = | 388 scoped_refptr<net::IOBufferWithSize> buf = |
355 new net::IOBufferWithSize(decoded_png.size()); | 389 new net::IOBufferWithSize(decoded_data.size()); |
356 memcpy(buf->data(), decoded_png.data(), decoded_png.size()); | 390 memcpy(buf->data(), decoded_data.data(), decoded_data.size()); |
357 | 391 |
358 if (!screenshot_file_proxy_->Write( | 392 if (!file_proxy_->Write( |
359 0, buf->data(), buf->size(), | 393 0, buf->data(), buf->size(), |
360 base::Bind(&HeadlessShell::OnScreenshotFileWritten, | 394 base::Bind(&HeadlessShell::OnFileWritten, weak_factory_.GetWeakPtr(), |
361 weak_factory_.GetWeakPtr(), file_name, buf->size()))) { | 395 file_name, buf->size()))) { |
362 // Operation may have completed successfully or failed. | 396 // Operation may have completed successfully or failed. |
363 OnScreenshotFileWritten(file_name, buf->size(), | 397 OnFileWritten(file_name, buf->size(), base::File::FILE_ERROR_FAILED, 0); |
364 base::File::FILE_ERROR_FAILED, 0); | |
365 } | 398 } |
366 } | 399 } |
367 | 400 |
368 void HeadlessShell::OnScreenshotFileWritten(const base::FilePath file_name, | 401 void HeadlessShell::OnFileWritten(const base::FilePath file_name, |
369 const int length, | 402 const int length, |
370 base::File::Error error_code, | 403 base::File::Error error_code, |
371 int write_result) { | 404 int write_result) { |
372 if (write_result < length) { | 405 if (write_result < length) { |
373 // TODO(eseckler): Support recovering from partial writes. | 406 // TODO(eseckler): Support recovering from partial writes. |
374 LOG(ERROR) << "Writing screenshot to file " << file_name.value() | 407 LOG(ERROR) << "Writing to file " << file_name.value() |
375 << " was unsuccessful: " << net::ErrorToString(write_result); | 408 << " was unsuccessful: " |
409 << base::File::ErrorToString(error_code); | |
376 } else { | 410 } else { |
377 LOG(INFO) << "Screenshot written to file " << file_name.value() << "." | 411 LOG(INFO) << "Written to file " << file_name.value() << "."; |
378 << std::endl; | |
379 } | 412 } |
380 if (!screenshot_file_proxy_->Close( | 413 if (!file_proxy_->Close(base::Bind(&HeadlessShell::OnFileClosed, |
381 base::Bind(&HeadlessShell::OnScreenshotFileClosed, | 414 weak_factory_.GetWeakPtr()))) { |
382 weak_factory_.GetWeakPtr()))) { | |
383 // Operation could not be started. | 415 // Operation could not be started. |
384 OnScreenshotFileClosed(base::File::FILE_ERROR_FAILED); | 416 OnFileClosed(base::File::FILE_ERROR_FAILED); |
385 } | 417 } |
386 } | 418 } |
387 | 419 |
388 void HeadlessShell::OnScreenshotFileClosed(base::File::Error error_code) { | 420 void HeadlessShell::OnFileClosed(base::File::Error error_code) { |
389 Shutdown(); | 421 Shutdown(); |
390 } | 422 } |
391 | 423 |
392 bool HeadlessShell::RemoteDebuggingEnabled() const { | 424 bool HeadlessShell::RemoteDebuggingEnabled() const { |
393 const base::CommandLine& command_line = | 425 const base::CommandLine& command_line = |
394 *base::CommandLine::ForCurrentProcess(); | 426 *base::CommandLine::ForCurrentProcess(); |
395 return command_line.HasSwitch(switches::kRemoteDebuggingPort); | 427 return command_line.HasSwitch(switches::kRemoteDebuggingPort); |
396 } | 428 } |
397 | 429 |
398 bool ValidateCommandLine(const base::CommandLine& command_line) { | 430 bool ValidateCommandLine(const base::CommandLine& command_line) { |
(...skipping 11 matching lines...) Expand all Loading... | |
410 if (command_line.HasSwitch(switches::kRepl)) { | 442 if (command_line.HasSwitch(switches::kRepl)) { |
411 LOG(ERROR) << "Evaluate Javascript is disabled " | 443 LOG(ERROR) << "Evaluate Javascript is disabled " |
412 << "when remote debugging is enabled."; | 444 << "when remote debugging is enabled."; |
413 return false; | 445 return false; |
414 } | 446 } |
415 if (command_line.HasSwitch(switches::kScreenshot)) { | 447 if (command_line.HasSwitch(switches::kScreenshot)) { |
416 LOG(ERROR) << "Capture screenshot is disabled " | 448 LOG(ERROR) << "Capture screenshot is disabled " |
417 << "when remote debugging is enabled."; | 449 << "when remote debugging is enabled."; |
418 return false; | 450 return false; |
419 } | 451 } |
452 if (command_line.HasSwitch(switches::kPrintToPDF)) { | |
453 LOG(ERROR) << "Print to PDF is disabled " | |
454 << "when remote debugging is enabled."; | |
455 return false; | |
456 } | |
420 if (command_line.HasSwitch(switches::kTimeout)) { | 457 if (command_line.HasSwitch(switches::kTimeout)) { |
421 LOG(ERROR) << "Navigation timeout is disabled " | 458 LOG(ERROR) << "Navigation timeout is disabled " |
422 << "when remote debugging is enabled."; | 459 << "when remote debugging is enabled."; |
423 return false; | 460 return false; |
424 } | 461 } |
425 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { | 462 if (command_line.HasSwitch(switches::kVirtualTimeBudget)) { |
426 LOG(ERROR) << "Virtual time budget is disabled " | 463 LOG(ERROR) << "Virtual time budget is disabled " |
427 << "when remote debugging is enabled."; | 464 << "when remote debugging is enabled."; |
428 return false; | 465 return false; |
429 } | 466 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 555 builder.SetOverrideWebPreferencesCallback(base::Bind([]( |
519 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 556 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); |
520 } | 557 } |
521 | 558 |
522 return HeadlessBrowserMain( | 559 return HeadlessBrowserMain( |
523 builder.Build(), | 560 builder.Build(), |
524 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 561 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
525 } | 562 } |
526 | 563 |
527 } // namespace headless | 564 } // namespace headless |
OLD | NEW |