| 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 <iostream> | |
| 6 #include <memory> | 5 #include <memory> |
| 6 #include <sstream> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 236 |
| 237 void OnPageReady() { | 237 void OnPageReady() { |
| 238 if (processed_page_ready_) | 238 if (processed_page_ready_) |
| 239 return; | 239 return; |
| 240 processed_page_ready_ = true; | 240 processed_page_ready_ = true; |
| 241 | 241 |
| 242 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpDom)) { | 242 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpDom)) { |
| 243 FetchDom(); | 243 FetchDom(); |
| 244 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 244 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 245 switches::kRepl)) { | 245 switches::kRepl)) { |
| 246 std::cout | 246 LOG(INFO) |
| 247 << "Type a Javascript expression to evaluate or \"quit\" to exit." | 247 << "Type a Javascript expression to evaluate or \"quit\" to exit."; |
| 248 << std::endl; | |
| 249 InputExpression(); | 248 InputExpression(); |
| 250 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 249 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 251 switches::kScreenshot)) { | 250 switches::kScreenshot)) { |
| 252 CaptureScreenshot(); | 251 CaptureScreenshot(); |
| 253 } else { | 252 } else { |
| 254 Shutdown(); | 253 Shutdown(); |
| 255 } | 254 } |
| 256 } | 255 } |
| 257 | 256 |
| 258 void FetchDom() { | 257 void FetchDom() { |
| 259 devtools_client_->GetRuntime()->Evaluate( | 258 devtools_client_->GetRuntime()->Evaluate( |
| 260 "document.body.innerHTML", | 259 "document.body.innerHTML", |
| 261 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr())); | 260 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr())); |
| 262 } | 261 } |
| 263 | 262 |
| 264 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result) { | 263 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result) { |
| 265 if (result->HasExceptionDetails()) { | 264 if (result->HasExceptionDetails()) { |
| 266 LOG(ERROR) << "Failed to evaluate document.body.innerHTML: " | 265 LOG(ERROR) << "Failed to evaluate document.body.innerHTML: " |
| 267 << result->GetExceptionDetails()->GetText(); | 266 << result->GetExceptionDetails()->GetText(); |
| 268 } else { | 267 } else { |
| 269 std::string dom; | 268 std::string dom; |
| 270 if (result->GetResult()->GetValue()->GetAsString(&dom)) { | 269 if (result->GetResult()->GetValue()->GetAsString(&dom)) { |
| 271 std::cout << dom << std::endl; | 270 printf("%s\n", dom.c_str()); |
| 272 } | 271 } |
| 273 } | 272 } |
| 274 Shutdown(); | 273 Shutdown(); |
| 275 } | 274 } |
| 276 | 275 |
| 277 void InputExpression() { | 276 void InputExpression() { |
| 278 // Note that a real system should read user input asynchronously, because | 277 // Note that a real system should read user input asynchronously, because |
| 279 // otherwise all other browser activity is suspended (e.g., page loading). | 278 // otherwise all other browser activity is suspended (e.g., page loading). |
| 280 std::string expression; | 279 printf(">>> "); |
| 281 std::cout << ">>> "; | 280 std::stringstream expression; |
| 282 std::getline(std::cin, expression); | 281 while (true) { |
| 283 if (std::cin.bad() || std::cin.eof() || expression == "quit") { | 282 char c = fgetc(stdin); |
| 283 if (c == EOF || c == '\n') { |
| 284 break; |
| 285 } |
| 286 expression << c; |
| 287 } |
| 288 if (expression.str() == "quit") { |
| 284 Shutdown(); | 289 Shutdown(); |
| 285 return; | 290 return; |
| 286 } | 291 } |
| 287 devtools_client_->GetRuntime()->Evaluate( | 292 devtools_client_->GetRuntime()->Evaluate( |
| 288 expression, base::Bind(&HeadlessShell::OnExpressionResult, | 293 expression.str(), base::Bind(&HeadlessShell::OnExpressionResult, |
| 289 weak_factory_.GetWeakPtr())); | 294 weak_factory_.GetWeakPtr())); |
| 290 } | 295 } |
| 291 | 296 |
| 292 void OnExpressionResult(std::unique_ptr<runtime::EvaluateResult> result) { | 297 void OnExpressionResult(std::unique_ptr<runtime::EvaluateResult> result) { |
| 293 std::unique_ptr<base::Value> value = result->Serialize(); | 298 std::unique_ptr<base::Value> value = result->Serialize(); |
| 294 std::string result_json; | 299 std::string result_json; |
| 295 base::JSONWriter::Write(*value, &result_json); | 300 base::JSONWriter::Write(*value, &result_json); |
| 296 std::cout << result_json << std::endl; | 301 printf("%s\n", result_json.c_str()); |
| 297 InputExpression(); | 302 InputExpression(); |
| 298 } | 303 } |
| 299 | 304 |
| 300 void CaptureScreenshot() { | 305 void CaptureScreenshot() { |
| 301 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot( | 306 devtools_client_->GetPage()->GetExperimental()->CaptureScreenshot( |
| 302 page::CaptureScreenshotParams::Builder().Build(), | 307 page::CaptureScreenshotParams::Builder().Build(), |
| 303 base::Bind(&HeadlessShell::OnScreenshotCaptured, | 308 base::Bind(&HeadlessShell::OnScreenshotCaptured, |
| 304 weak_factory_.GetWeakPtr())); | 309 weak_factory_.GetWeakPtr())); |
| 305 } | 310 } |
| 306 | 311 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 359 } |
| 355 | 360 |
| 356 void OnScreenshotFileWritten(const base::FilePath file_name, | 361 void OnScreenshotFileWritten(const base::FilePath file_name, |
| 357 const int length, | 362 const int length, |
| 358 const int write_result) { | 363 const int write_result) { |
| 359 if (write_result < length) { | 364 if (write_result < length) { |
| 360 // TODO(eseckler): Support recovering from partial writes. | 365 // TODO(eseckler): Support recovering from partial writes. |
| 361 LOG(ERROR) << "Writing screenshot to file " << file_name.value() | 366 LOG(ERROR) << "Writing screenshot to file " << file_name.value() |
| 362 << " was unsuccessful: " << net::ErrorToString(write_result); | 367 << " was unsuccessful: " << net::ErrorToString(write_result); |
| 363 } else { | 368 } else { |
| 364 std::cout << "Screenshot written to file " << file_name.value() << "." | 369 LOG(INFO) << "Screenshot written to file " << file_name.value() << "." |
| 365 << std::endl; | 370 << std::endl; |
| 366 } | 371 } |
| 367 int close_result = screenshot_file_stream_->Close(base::Bind( | 372 int close_result = screenshot_file_stream_->Close(base::Bind( |
| 368 &HeadlessShell::OnScreenshotFileClosed, weak_factory_.GetWeakPtr())); | 373 &HeadlessShell::OnScreenshotFileClosed, weak_factory_.GetWeakPtr())); |
| 369 if (close_result != net::ERR_IO_PENDING) { | 374 if (close_result != net::ERR_IO_PENDING) { |
| 370 // Operation could not be started. | 375 // Operation could not be started. |
| 371 OnScreenshotFileClosed(close_result); | 376 OnScreenshotFileClosed(close_result); |
| 372 } | 377 } |
| 373 } | 378 } |
| 374 | 379 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 } | 470 } |
| 466 builder.SetWindowSize(parsed_window_size); | 471 builder.SetWindowSize(parsed_window_size); |
| 467 } | 472 } |
| 468 | 473 |
| 469 return HeadlessBrowserMain( | 474 return HeadlessBrowserMain( |
| 470 builder.Build(), | 475 builder.Build(), |
| 471 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 476 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 472 } | 477 } |
| 473 | 478 |
| 474 } // namespace headless | 479 } // namespace headless |
| OLD | NEW |