| 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 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 parsed_window_size->set_width(width); | 49 parsed_window_size->set_width(width); |
| 50 parsed_window_size->set_height(height); | 50 parsed_window_size->set_height(height); |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 return false; | 53 return false; |
| 54 } | 54 } |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 // An application which implements a simple headless browser. | 57 // An application which implements a simple headless browser. |
| 58 class HeadlessShell : public HeadlessWebContents::Observer, | 58 class HeadlessShell : public HeadlessWebContents::Observer, |
| 59 emulation::ExperimentalObserver, | 59 public emulation::ExperimentalObserver, |
| 60 inspector::ExperimentalObserver, | 60 public inspector::ExperimentalObserver, |
| 61 page::Observer { | 61 public page::Observer { |
| 62 public: | 62 public: |
| 63 HeadlessShell() | 63 HeadlessShell() |
| 64 : browser_(nullptr), | 64 : browser_(nullptr), |
| 65 devtools_client_(HeadlessDevToolsClient::Create()), | 65 devtools_client_(HeadlessDevToolsClient::Create()), |
| 66 web_contents_(nullptr), | 66 web_contents_(nullptr), |
| 67 processed_page_ready_(false), | 67 processed_page_ready_(false), |
| 68 browser_context_(nullptr), | 68 browser_context_(nullptr), |
| 69 weak_factory_(this) {} | 69 weak_factory_(this) {} |
| 70 ~HeadlessShell() override {} | 70 ~HeadlessShell() override {} |
| 71 | 71 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 250 } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 251 switches::kScreenshot)) { | 251 switches::kScreenshot)) { |
| 252 CaptureScreenshot(); | 252 CaptureScreenshot(); |
| 253 } else { | 253 } else { |
| 254 Shutdown(); | 254 Shutdown(); |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 | 257 |
| 258 void FetchDom() { | 258 void FetchDom() { |
| 259 devtools_client_->GetRuntime()->Evaluate( | 259 devtools_client_->GetRuntime()->Evaluate( |
| 260 "document.body.innerHTML", | 260 "document.body.outerHTML", |
| 261 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr())); | 261 base::Bind(&HeadlessShell::OnDomFetched, weak_factory_.GetWeakPtr())); |
| 262 } | 262 } |
| 263 | 263 |
| 264 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result) { | 264 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result) { |
| 265 if (result->HasExceptionDetails()) { | 265 if (result->HasExceptionDetails()) { |
| 266 LOG(ERROR) << "Failed to evaluate document.body.innerHTML: " | 266 LOG(ERROR) << "Failed to evaluate document.body.outerHTML: " |
| 267 << result->GetExceptionDetails()->GetText(); | 267 << result->GetExceptionDetails()->GetText(); |
| 268 } else { | 268 } else { |
| 269 std::string dom; | 269 std::string dom; |
| 270 if (result->GetResult()->GetValue()->GetAsString(&dom)) { | 270 if (result->GetResult()->GetValue()->GetAsString(&dom)) { |
| 271 printf("%s\n", dom.c_str()); | 271 printf("%s\n", dom.c_str()); |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 Shutdown(); | 274 Shutdown(); |
| 275 } | 275 } |
| 276 | 276 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 builder.SetOverrideWebPreferencesCallback(base::Bind([]( | 516 builder.SetOverrideWebPreferencesCallback(base::Bind([]( |
| 517 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); | 517 WebPreferences* preferences) { preferences->hide_scrollbars = true; })); |
| 518 } | 518 } |
| 519 | 519 |
| 520 return HeadlessBrowserMain( | 520 return HeadlessBrowserMain( |
| 521 builder.Build(), | 521 builder.Build(), |
| 522 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); | 522 base::Bind(&HeadlessShell::OnStart, base::Unretained(&shell))); |
| 523 } | 523 } |
| 524 | 524 |
| 525 } // namespace headless | 525 } // namespace headless |
| OLD | NEW |