| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef HEADLESS_APP_HEADLESS_SHELL_H_ |
| 6 #define HEADLESS_APP_HEADLESS_SHELL_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 5 #include "base/files/file_proxy.h" | 11 #include "base/files/file_proxy.h" |
| 6 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 7 #include "headless/app/shell_navigation_request.h" | 13 #include "headless/app/shell_navigation_request.h" |
| 8 #include "headless/public/devtools/domains/emulation.h" | 14 #include "headless/public/devtools/domains/emulation.h" |
| 9 #include "headless/public/devtools/domains/inspector.h" | 15 #include "headless/public/devtools/domains/inspector.h" |
| 10 #include "headless/public/devtools/domains/page.h" | 16 #include "headless/public/devtools/domains/page.h" |
| 11 #include "headless/public/devtools/domains/runtime.h" | 17 #include "headless/public/devtools/domains/runtime.h" |
| 12 #include "headless/public/headless_browser.h" | 18 #include "headless/public/headless_browser.h" |
| 13 #include "headless/public/headless_devtools_client.h" | 19 #include "headless/public/headless_devtools_client.h" |
| 14 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
| 15 #include "headless/public/util/deterministic_dispatcher.h" | 21 #include "headless/public/util/deterministic_dispatcher.h" |
| 16 #include "net/base/file_stream.h" | 22 #include "net/base/file_stream.h" |
| 17 | 23 |
| 18 namespace headless { | 24 namespace headless { |
| 19 | 25 |
| 20 // An application which implements a simple headless browser. | 26 // An application which implements a simple headless browser. |
| 21 class HeadlessShell : public HeadlessWebContents::Observer, | 27 class HeadlessShell : public HeadlessWebContents::Observer, |
| 22 public emulation::ExperimentalObserver, | 28 public emulation::ExperimentalObserver, |
| 23 public inspector::ExperimentalObserver, | 29 public inspector::ExperimentalObserver, |
| 24 public page::ExperimentalObserver { | 30 public page::ExperimentalObserver { |
| 25 public: | 31 public: |
| 26 HeadlessShell(); | 32 HeadlessShell(); |
| 27 ~HeadlessShell() override; | 33 ~HeadlessShell() override; |
| 28 | 34 |
| 35 void OnStart(HeadlessBrowser* browser); |
| 36 |
| 37 HeadlessDevToolsClient* devtools_client() const { |
| 38 return devtools_client_.get(); |
| 39 } |
| 40 |
| 41 private: |
| 29 // HeadlessWebContents::Observer implementation: | 42 // HeadlessWebContents::Observer implementation: |
| 30 void DevToolsTargetReady() override; | 43 void DevToolsTargetReady() override; |
| 31 void OnTargetCrashed(const inspector::TargetCrashedParams& params) override; | 44 void OnTargetCrashed(const inspector::TargetCrashedParams& params) override; |
| 32 | 45 |
| 33 // emulation::Observer implementation: | 46 // emulation::Observer implementation: |
| 34 void OnVirtualTimeBudgetExpired( | 47 void OnVirtualTimeBudgetExpired( |
| 35 const emulation::VirtualTimeBudgetExpiredParams& params) override; | 48 const emulation::VirtualTimeBudgetExpiredParams& params) override; |
| 36 | 49 |
| 37 // page::Observer implementation: | 50 // page::Observer implementation: |
| 38 void OnLoadEventFired(const page::LoadEventFiredParams& params) override; | 51 void OnLoadEventFired(const page::LoadEventFiredParams& params) override; |
| 39 void OnNavigationRequested( | 52 void OnNavigationRequested( |
| 40 const headless::page::NavigationRequestedParams& params) override; | 53 const headless::page::NavigationRequestedParams& params) override; |
| 41 | 54 |
| 42 void OnStart(HeadlessBrowser* browser); | |
| 43 void Shutdown(); | 55 void Shutdown(); |
| 44 | 56 |
| 45 void FetchTimeout(); | 57 void FetchTimeout(); |
| 46 | 58 |
| 47 void PollReadyState(); | 59 void PollReadyState(); |
| 48 | 60 |
| 49 void OnReadyState(std::unique_ptr<runtime::EvaluateResult> result); | 61 void OnReadyState(std::unique_ptr<runtime::EvaluateResult> result); |
| 50 | 62 |
| 51 void OnPageReady(); | 63 void OnPageReady(); |
| 52 | 64 |
| 53 void FetchDom(); | 65 void FetchDom(); |
| 54 | 66 |
| 55 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result); | 67 void OnDomFetched(std::unique_ptr<runtime::EvaluateResult> result); |
| 56 | 68 |
| 57 void InputExpression(); | 69 void InputExpression(); |
| 58 | 70 |
| 59 void OnExpressionResult(std::unique_ptr<runtime::EvaluateResult> result); | 71 void OnExpressionResult(std::unique_ptr<runtime::EvaluateResult> result); |
| 60 | 72 |
| 61 void CaptureScreenshot(); | 73 void CaptureScreenshot(); |
| 62 | 74 |
| 63 void OnScreenshotCaptured( | 75 void OnScreenshotCaptured( |
| 64 std::unique_ptr<page::CaptureScreenshotResult> result); | 76 std::unique_ptr<page::CaptureScreenshotResult> result); |
| 65 | 77 |
| 66 void OnScreenshotFileOpened( | 78 void PrintToPDF(); |
| 67 std::unique_ptr<page::CaptureScreenshotResult> result, | |
| 68 const base::FilePath file_name, | |
| 69 base::File::Error error_code); | |
| 70 | 79 |
| 71 void OnScreenshotFileWritten(const base::FilePath file_name, | 80 void OnPDFCreated(std::unique_ptr<page::PrintToPDFResult> result); |
| 72 const int length, | |
| 73 base::File::Error error_code, | |
| 74 int write_result); | |
| 75 | 81 |
| 76 void OnScreenshotFileClosed(base::File::Error error_code); | 82 void WriteFile(const std::string& switch_string, |
| 83 const std::string& default_file_name, |
| 84 const std::string& data); |
| 85 void OnFileOpened(const std::string& data, |
| 86 const base::FilePath file_name, |
| 87 base::File::Error error_code); |
| 88 void OnFileWritten(const base::FilePath file_name, |
| 89 const int length, |
| 90 base::File::Error error_code, |
| 91 int write_result); |
| 92 void OnFileClosed(base::File::Error error_code); |
| 77 | 93 |
| 78 bool RemoteDebuggingEnabled() const; | 94 bool RemoteDebuggingEnabled() const; |
| 79 | 95 |
| 80 HeadlessDevToolsClient* devtools_client() const { | |
| 81 return devtools_client_.get(); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 GURL url_; | 96 GURL url_; |
| 86 HeadlessBrowser* browser_; // Not owned. | 97 HeadlessBrowser* browser_; // Not owned. |
| 87 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; | 98 std::unique_ptr<HeadlessDevToolsClient> devtools_client_; |
| 88 HeadlessWebContents* web_contents_; | 99 HeadlessWebContents* web_contents_; |
| 89 bool processed_page_ready_; | 100 bool processed_page_ready_; |
| 90 std::unique_ptr<base::FileProxy> screenshot_file_proxy_; | 101 std::unique_ptr<base::FileProxy> file_proxy_; |
| 91 HeadlessBrowserContext* browser_context_; | 102 HeadlessBrowserContext* browser_context_; |
| 92 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; | 103 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; |
| 93 base::WeakPtrFactory<HeadlessShell> weak_factory_; | 104 base::WeakPtrFactory<HeadlessShell> weak_factory_; |
| 94 | 105 |
| 95 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); | 106 DISALLOW_COPY_AND_ASSIGN(HeadlessShell); |
| 96 }; | 107 }; |
| 97 | 108 |
| 98 } // namespace headless | 109 } // namespace headless |
| 110 |
| 111 #endif // HEADLESS_APP_HEADLESS_SHELL_H_ |
| OLD | NEW |