diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-03-12 12:38:15 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-13 07:21:00 +0000 |
commit | 11fe34ce0fb827f3bfe9c61c31b3dbe99a894233 (patch) | |
tree | 8662c62c9701ed1955d58be045f3c2e6a7a4c060 | |
parent | add15a5f04984592121839a27fcb59992ec1b737 (diff) | |
download | serenity-11fe34ce0fb827f3bfe9c61c31b3dbe99a894233.zip |
headless-browser: Re-implement headless-browser using an OOPWV
headless-browser currently uses its own PageClient to load web pages
in-process. Due to this, it also needs to set up a whole bunch of other
objects needed to run LibWeb, e.g. image decoders, request servers, etc.
This changes headless-browser to instead implement a WebView to launch
WebContent out-of-process. This implementation is almost entirely empty,
but can be filled in as-needed. For example, we may want to print
JavaScript console messages.
-rw-r--r-- | Ladybird/CMakeLists.txt | 9 | ||||
-rw-r--r-- | Ladybird/WebDriver/main.cpp | 6 | ||||
-rw-r--r-- | Meta/Lagom/CMakeLists.txt | 5 | ||||
-rw-r--r-- | Userland/Utilities/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Userland/Utilities/headless-browser.cpp | 855 |
5 files changed, 148 insertions, 729 deletions
diff --git a/Ladybird/CMakeLists.txt b/Ladybird/CMakeLists.txt index 1b06ecf5b8..14fcccf22d 100644 --- a/Ladybird/CMakeLists.txt +++ b/Ladybird/CMakeLists.txt @@ -103,6 +103,15 @@ target_include_directories(ladybird PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(ladybird PRIVATE ${SERENITY_SOURCE_DIR}/Userland/Applications/) target_include_directories(ladybird PRIVATE ${SERENITY_SOURCE_DIR}/Userland/Services/) +qt_add_executable(headless-browser + ${SERENITY_SOURCE_DIR}/Userland/Utilities/headless-browser.cpp + ${SERENITY_SOURCE_DIR}/Userland/Services/WebContent/WebDriverConnection.cpp + HelperProcess.cpp + Utilities.cpp) + +target_include_directories(headless-browser PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(headless-browser PRIVATE Qt::Core LibWeb LibWebView LibWebSocket LibCrypto LibGemini LibHTTP LibJS LibGfx LibMain LibTLS LibIPC LibJS) + set_target_properties(ladybird PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER org.serenityos.ladybird MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} diff --git a/Ladybird/WebDriver/main.cpp b/Ladybird/WebDriver/main.cpp index 7c668af984..0367fa0ad4 100644 --- a/Ladybird/WebDriver/main.cpp +++ b/Ladybird/WebDriver/main.cpp @@ -65,17 +65,11 @@ static ErrorOr<pid_t> launch_browser(DeprecatedString const& socket_path) static ErrorOr<pid_t> launch_headless_browser(DeprecatedString const& socket_path) { auto resources = DeprecatedString::formatted("{}/res", s_serenity_resource_root); - auto error_page = DeprecatedString::formatted("{}/res/html/error.html", s_serenity_resource_root); - auto certs = DeprecatedString::formatted("{}/etc/ca_certs.ini", s_serenity_resource_root); char const* argv[] = { "headless-browser", "--resources", resources.characters(), - "--error-page", - error_page.characters(), - "--certs", - certs.characters(), "--webdriver-ipc-path", socket_path.characters(), "about:blank", diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index f683d435c5..a2d69e6030 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -498,11 +498,6 @@ if (BUILD_LAGOM) add_executable(gml-format ../../Userland/Utilities/gml-format.cpp) target_link_libraries(gml-format LibCore LibGUI LibMain) - if (ENABLE_LAGOM_LIBWEB) - add_executable(headless-browser ../../Userland/Utilities/headless-browser.cpp ../../Userland/Services/WebContent/WebDriverConnection.cpp) - target_link_libraries(headless-browser LibWeb LibWebSocket LibCrypto LibGemini LibHTTP LibJS LibGfx LibMain LibTLS LibIPC LibJS) - endif() - if (ENABLE_LAGOM_LADYBIRD) add_serenity_subdirectory(Ladybird) endif() diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 530a5811ac..621e9640e5 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -93,7 +93,7 @@ target_link_libraries(gml-format PRIVATE LibGUI) target_link_libraries(grep PRIVATE LibRegex) target_link_libraries(gunzip PRIVATE LibCompress) target_link_libraries(gzip PRIVATE LibCompress) -target_link_libraries(headless-browser PRIVATE LibCrypto LibGemini LibGfx LibHTTP LibTLS LibWeb LibWebSocket LibIPC LibJS) +target_link_libraries(headless-browser PRIVATE LibCrypto LibGemini LibGfx LibHTTP LibTLS LibWeb LibWebView LibWebSocket LibIPC LibJS) target_link_libraries(icc PRIVATE LibGfx LibVideo) target_link_libraries(image PRIVATE LibGfx) target_link_libraries(image2bin PRIVATE LibGfx) diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index 465a5cd9e3..dc3f18d7a4 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -1,782 +1,203 @@ /* * Copyright (c) 2022, Dexβͺ <dexes.ttp@gmail.com> + * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ -#include <AK/Assertions.h> -#include <AK/ByteBuffer.h> -#include <AK/Format.h> -#include <AK/HashTable.h> +#include <AK/Platform.h> + +#if !defined(AK_OS_SERENITY) +# define AK_DONT_REPLACE_STD +#endif + +#include <AK/Badge.h> +#include <AK/DeprecatedString.h> #include <AK/LexicalPath.h> #include <AK/NonnullOwnPtr.h> -#include <AK/StringBuilder.h> -#include <AK/Types.h> +#include <AK/URL.h> +#include <AK/Vector.h> #include <LibCore/ArgsParser.h> -#include <LibCore/ConfigFile.h> #include <LibCore/DeprecatedFile.h> #include <LibCore/EventLoop.h> -#include <LibCore/System.h> -#include <LibCore/SystemServerTakeover.h> #include <LibCore/Timer.h> -#include <LibGemini/GeminiRequest.h> -#include <LibGemini/GeminiResponse.h> -#include <LibGemini/Job.h> #include <LibGfx/Bitmap.h> #include <LibGfx/Font/FontDatabase.h> -#include <LibGfx/ImageDecoder.h> #include <LibGfx/PNGWriter.h> +#include <LibGfx/Point.h> #include <LibGfx/Rect.h> -#include <LibHTTP/HttpRequest.h> -#include <LibHTTP/HttpResponse.h> -#include <LibHTTP/HttpsJob.h> -#include <LibHTTP/Job.h> -#include <LibMain/Main.h> +#include <LibGfx/ShareableBitmap.h> +#include <LibGfx/Size.h> +#include <LibGfx/StandardCursor.h> +#include <LibGfx/SystemTheme.h> +#include <LibWeb/Cookie/Cookie.h> #include <LibWeb/Cookie/ParsedCookie.h> -#include <LibWeb/DOM/Document.h> -#include <LibWeb/HTML/BrowsingContext.h> -#include <LibWeb/Layout/Viewport.h> -#include <LibWeb/Loader/ResourceLoader.h> -#include <LibWeb/Page/Page.h> -#include <LibWeb/Painting/PaintableBox.h> -#include <LibWeb/Platform/EventLoopPluginSerenity.h> -#include <LibWeb/Platform/FontPluginSerenity.h> -#include <LibWeb/Platform/ImageCodecPlugin.h> -#include <LibWeb/WebSockets/WebSocket.h> -#include <LibWebSocket/ConnectionInfo.h> -#include <LibWebSocket/Message.h> -#include <LibWebSocket/WebSocket.h> -#include <WebContent/WebDriverConnection.h> - -class HeadlessBrowserPageClient final : public Web::PageClient { -public: - static NonnullOwnPtr<HeadlessBrowserPageClient> create() - { - return adopt_own(*new HeadlessBrowserPageClient()); - } - - virtual Web::Page& page() override { return *m_page; } - virtual Web::Page const& page() const override { return *m_page; } - - Web::Layout::Viewport* layout_root() - { - auto* document = page().top_level_browsing_context().active_document(); - if (!document) - return nullptr; - return document->layout_node(); - } - - void load(AK::URL const& url) - { - page().load(url); - } - - virtual void paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap& target) override - { - Gfx::Painter painter(target); - - if (auto* document = page().top_level_browsing_context().active_document()) - document->update_layout(); - - painter.fill_rect({ {}, content_rect.size().to_type<int>() }, palette().base()); - - auto* layout_root = this->layout_root(); - if (!layout_root) { - return; - } - - Web::PaintContext context(painter, palette(), device_pixels_per_css_pixel()); - context.set_should_show_line_box_borders(false); - context.set_device_viewport_rect(content_rect); - context.set_has_focus(true); - layout_root->paint_all_phases(context); - } - - void setup_palette(Core::AnonymousBuffer theme_buffer) - { - m_palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer); - } - - void set_viewport_rect(Gfx::IntRect viewport_rect) - { - page().top_level_browsing_context().set_viewport_rect(page().device_to_css_rect(viewport_rect.to_type<Web::DevicePixels>())); - } - - void set_screen_rect(Web::DevicePixelRect screen_rect) - { - m_screen_rect = screen_rect; - } - - ErrorOr<void> connect_to_webdriver(StringView webdriver_ipc_path) - { - VERIFY(!m_webdriver); - m_webdriver = TRY(WebContent::WebDriverConnection::connect(*this, webdriver_ipc_path)); - return {}; - } - - // ^Web::PageClient - virtual bool is_connection_open() const override - { - if (m_webdriver) - return m_webdriver->is_open(); - return true; - } - - virtual Gfx::Palette palette() const override - { - return Gfx::Palette(*m_palette_impl); - } - - virtual Web::DevicePixelRect screen_rect() const override - { - return m_screen_rect; - } - - virtual float device_pixels_per_css_pixel() const override - { - return 1.0f; - } - - virtual Web::CSS::PreferredColorScheme preferred_color_scheme() const override - { - return m_preferred_color_scheme; - } - - virtual void page_did_change_title(DeprecatedString const&) override - { - } - - virtual void page_did_start_loading(AK::URL const&, bool) override - { - } - - virtual void page_did_finish_loading(AK::URL const&) override - { - } - - virtual void page_did_change_selection() override - { - } - - virtual void page_did_request_cursor_change(Gfx::StandardCursor) override - { - } - - virtual void page_did_request_context_menu(Web::CSSPixelPoint) override - { - } - - virtual void page_did_request_link_context_menu(Web::CSSPixelPoint, AK::URL const&, DeprecatedString const&, unsigned) override - { - } - - virtual void page_did_request_image_context_menu(Web::CSSPixelPoint, AK::URL const&, DeprecatedString const&, unsigned, Gfx::Bitmap const*) override - { - } - - virtual void page_did_click_link(AK::URL const&, DeprecatedString const&, unsigned) override - { - } - - virtual void page_did_middle_click_link(AK::URL const&, DeprecatedString const&, unsigned) override - { - } - - virtual void page_did_enter_tooltip_area(Web::CSSPixelPoint, DeprecatedString const&) override - { - } - - virtual void page_did_leave_tooltip_area() override - { - } - - virtual void page_did_hover_link(AK::URL const&) override - { - } - - virtual void page_did_unhover_link() override - { - } - - virtual void page_did_invalidate(Web::CSSPixelRect const&) override - { - } - - virtual void page_did_change_favicon(Gfx::Bitmap const&) override - { - } - - virtual void page_did_layout() override - { - } - - virtual void page_did_request_scroll_into_view(Web::CSSPixelRect const&) override - { - } - - virtual void page_did_request_alert(DeprecatedString const&) override - { - } - - virtual void page_did_request_confirm(DeprecatedString const&) override - { - } - - virtual void page_did_request_prompt(DeprecatedString const&, DeprecatedString const&) override - { - } - - virtual DeprecatedString page_did_request_cookie(AK::URL const&, Web::Cookie::Source) override - { - return DeprecatedString::empty(); - } - - virtual void page_did_set_cookie(AK::URL const&, Web::Cookie::ParsedCookie const&, Web::Cookie::Source) override - { - } - - void request_file(Web::FileRequest request) override - { - auto file = Core::System::open(request.path(), O_RDONLY); - request.on_file_request_finish(move(file)); - } - -private: - HeadlessBrowserPageClient() - : m_page(make<Web::Page>(*this)) - { - } - - NonnullOwnPtr<Web::Page> m_page; - - RefPtr<Gfx::PaletteImpl> m_palette_impl; - Web::DevicePixelRect m_screen_rect { 0, 0, 800, 600 }; - Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto }; +#include <LibWeb/Loader/FrameLoader.h> +#include <LibWebView/ViewImplementation.h> +#include <LibWebView/WebContentClient.h> - RefPtr<WebContent::WebDriverConnection> m_webdriver; -}; +#if !defined(AK_OS_SERENITY) +# include <Ladybird/HelperProcess.h> +# include <QCoreApplication> +#endif -class ImageCodecPluginHeadless : public Web::Platform::ImageCodecPlugin { +class HeadlessWebContentView final : public WebView::ViewImplementation { public: - ImageCodecPluginHeadless() = default; - virtual ~ImageCodecPluginHeadless() override = default; - - virtual Optional<Web::Platform::DecodedImage> decode_image(ReadonlyBytes data) override - { - auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(data); - - if (!decoder) - return Web::Platform::DecodedImage { false, 0, Vector<Web::Platform::Frame> {} }; - - if (!decoder->frame_count()) - return Web::Platform::DecodedImage { false, 0, Vector<Web::Platform::Frame> {} }; - - Vector<Web::Platform::Frame> frames; - for (size_t i = 0; i < decoder->frame_count(); ++i) { - auto frame_or_error = decoder->frame(i); - if (frame_or_error.is_error()) { - frames.append({ {}, 0 }); - } else { - auto frame = frame_or_error.release_value(); - frames.append({ move(frame.image), static_cast<size_t>(frame.duration) }); - } - } - - return Web::Platform::DecodedImage { - decoder->is_animated(), - static_cast<u32>(decoder->loop_count()), - frames, - }; - } -}; - -static HashTable<RefPtr<Web::ResourceLoaderConnectorRequest>> s_all_requests; - -class HeadlessRequestServer : public Web::ResourceLoaderConnector { -public: - class HTTPHeadlessRequest - : public Web::ResourceLoaderConnectorRequest - , public Weakable<HTTPHeadlessRequest> { - public: - static ErrorOr<NonnullRefPtr<HTTPHeadlessRequest>> create(DeprecatedString const& method, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const& request_headers, ReadonlyBytes request_body, Core::ProxyData const&) - { - auto stream_backing_buffer = TRY(ByteBuffer::create_uninitialized(1 * MiB)); - auto underlying_socket = TRY(Core::TCPSocket::connect(url.host(), url.port().value_or(80))); - TRY(underlying_socket->set_blocking(false)); - auto socket = TRY(Core::BufferedSocket<Core::TCPSocket>::create(move(underlying_socket))); - - HTTP::HttpRequest request; - if (method.equals_ignoring_ascii_case("head"sv)) - request.set_method(HTTP::HttpRequest::HEAD); - else if (method.equals_ignoring_ascii_case("get"sv)) - request.set_method(HTTP::HttpRequest::GET); - else if (method.equals_ignoring_ascii_case("post"sv)) - request.set_method(HTTP::HttpRequest::POST); - else - request.set_method(HTTP::HttpRequest::Invalid); - request.set_url(move(url)); - request.set_headers(request_headers); - request.set_body(TRY(ByteBuffer::copy(request_body))); - - return adopt_ref(*new HTTPHeadlessRequest(move(request), move(socket), move(stream_backing_buffer))); - } - - virtual ~HTTPHeadlessRequest() override - { - } - - virtual void set_should_buffer_all_input(bool) override - { - } - - virtual bool stop() override - { - return false; - } - - virtual void stream_into(Stream&) override - { - } - - private: - HTTPHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) - : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) - , m_socket(move(socket)) - , m_job(HTTP::Job::construct(move(request), *m_output_stream)) - { - m_job->on_headers_received = [weak_this = make_weak_ptr()](auto& response_headers, auto response_code) { - if (auto strong_this = weak_this.strong_ref()) { - strong_this->m_response_code = response_code; - for (auto& header : response_headers) { - strong_this->m_response_headers.set(header.key, header.value); - } - } - }; - m_job->on_finish = [weak_this = make_weak_ptr()](bool success) { - Core::deferred_invoke([weak_this, success] { - if (auto strong_this = weak_this.strong_ref()) { - ReadonlyBytes response_bytes { strong_this->m_output_stream->bytes().data(), strong_this->m_output_stream->offset() }; - auto response_buffer = ByteBuffer::copy(response_bytes).release_value_but_fixme_should_propagate_errors(); - strong_this->on_buffered_request_finish(success, strong_this->m_output_stream->offset(), strong_this->m_response_headers, strong_this->m_response_code, response_buffer); - } - }); - }; - m_job->start(*m_socket); - } - - Optional<u32> m_response_code; - ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr<FixedMemoryStream> m_output_stream; - NonnullOwnPtr<Core::BufferedSocketBase> m_socket; - NonnullRefPtr<HTTP::Job> m_job; - HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> m_response_headers; - }; - - class HTTPSHeadlessRequest - : public Web::ResourceLoaderConnectorRequest - , public Weakable<HTTPSHeadlessRequest> { - public: - static ErrorOr<NonnullRefPtr<HTTPSHeadlessRequest>> create(DeprecatedString const& method, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const& request_headers, ReadonlyBytes request_body, Core::ProxyData const&) - { - auto stream_backing_buffer = TRY(ByteBuffer::create_uninitialized(1 * MiB)); - auto underlying_socket = TRY(TLS::TLSv12::connect(url.host(), url.port().value_or(443))); - TRY(underlying_socket->set_blocking(false)); - auto socket = TRY(Core::BufferedSocket<TLS::TLSv12>::create(move(underlying_socket))); - - HTTP::HttpRequest request; - if (method.equals_ignoring_ascii_case("head"sv)) - request.set_method(HTTP::HttpRequest::HEAD); - else if (method.equals_ignoring_ascii_case("get"sv)) - request.set_method(HTTP::HttpRequest::GET); - else if (method.equals_ignoring_ascii_case("post"sv)) - request.set_method(HTTP::HttpRequest::POST); - else - request.set_method(HTTP::HttpRequest::Invalid); - request.set_url(move(url)); - request.set_headers(request_headers); - request.set_body(TRY(ByteBuffer::copy(request_body))); - - return adopt_ref(*new HTTPSHeadlessRequest(move(request), move(socket), move(stream_backing_buffer))); - } - - virtual ~HTTPSHeadlessRequest() override - { - } - - virtual void set_should_buffer_all_input(bool) override - { - } - - virtual bool stop() override - { - return false; - } - - virtual void stream_into(Stream&) override - { - } - - private: - HTTPSHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) - : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) - , m_socket(move(socket)) - , m_job(HTTP::HttpsJob::construct(move(request), *m_output_stream)) - { - m_job->on_headers_received = [weak_this = make_weak_ptr()](auto& response_headers, auto response_code) { - if (auto strong_this = weak_this.strong_ref()) { - strong_this->m_response_code = response_code; - for (auto& header : response_headers) { - strong_this->m_response_headers.set(header.key, header.value); - } - } - }; - m_job->on_finish = [weak_this = make_weak_ptr()](bool success) { - Core::deferred_invoke([weak_this, success] { - if (auto strong_this = weak_this.strong_ref()) { - ReadonlyBytes response_bytes { strong_this->m_output_stream->bytes().data(), strong_this->m_output_stream->offset() }; - auto response_buffer = ByteBuffer::copy(response_bytes).release_value_but_fixme_should_propagate_errors(); - strong_this->on_buffered_request_finish(success, strong_this->m_output_stream->offset(), strong_this->m_response_headers, strong_this->m_response_code, response_buffer); - } - }); - }; - m_job->start(*m_socket); - } - - Optional<u32> m_response_code; - ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr<FixedMemoryStream> m_output_stream; - NonnullOwnPtr<Core::BufferedSocketBase> m_socket; - NonnullRefPtr<HTTP::HttpsJob> m_job; - HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> m_response_headers; - }; - - class GeminiHeadlessRequest - : public Web::ResourceLoaderConnectorRequest - , public Weakable<GeminiHeadlessRequest> { - public: - static ErrorOr<NonnullRefPtr<GeminiHeadlessRequest>> create(DeprecatedString const&, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const&, ReadonlyBytes, Core::ProxyData const&) - { - auto stream_backing_buffer = TRY(ByteBuffer::create_uninitialized(1 * MiB)); - auto underlying_socket = TRY(Core::TCPSocket::connect(url.host(), url.port().value_or(80))); - TRY(underlying_socket->set_blocking(false)); - auto socket = TRY(Core::BufferedSocket<Core::TCPSocket>::create(move(underlying_socket))); - - Gemini::GeminiRequest request; - request.set_url(url); - - return adopt_ref(*new GeminiHeadlessRequest(move(request), move(socket), move(stream_backing_buffer))); - } - - virtual ~GeminiHeadlessRequest() override - { - } - - virtual void set_should_buffer_all_input(bool) override - { - } - - virtual bool stop() override - { - return false; - } - - virtual void stream_into(Stream&) override - { - } - - private: - GeminiHeadlessRequest(Gemini::GeminiRequest&& request, NonnullOwnPtr<Core::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) - : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) - , m_socket(move(socket)) - , m_job(Gemini::Job::construct(move(request), *m_output_stream)) - { - m_job->on_headers_received = [weak_this = make_weak_ptr()](auto& response_headers, auto response_code) { - if (auto strong_this = weak_this.strong_ref()) { - strong_this->m_response_code = response_code; - for (auto& header : response_headers) { - strong_this->m_response_headers.set(header.key, header.value); - } - } - }; - m_job->on_finish = [weak_this = make_weak_ptr()](bool success) { - Core::deferred_invoke([weak_this, success] { - if (auto strong_this = weak_this.strong_ref()) { - ReadonlyBytes response_bytes { strong_this->m_output_stream->bytes().data(), strong_this->m_output_stream->offset() }; - auto response_buffer = ByteBuffer::copy(response_bytes).release_value_but_fixme_should_propagate_errors(); - strong_this->on_buffered_request_finish(success, strong_this->m_output_stream->offset(), strong_this->m_response_headers, strong_this->m_response_code, response_buffer); - } - }); - }; - m_job->start(*m_socket); - } - - Optional<u32> m_response_code; - ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr<FixedMemoryStream> m_output_stream; - NonnullOwnPtr<Core::BufferedSocketBase> m_socket; - NonnullRefPtr<Gemini::Job> m_job; - HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> m_response_headers; - }; - - static NonnullRefPtr<HeadlessRequestServer> create() - { - return adopt_ref(*new HeadlessRequestServer()); - } - - virtual ~HeadlessRequestServer() override { } - - virtual void prefetch_dns(AK::URL const&) override { } - virtual void preconnect(AK::URL const&) override { } - - virtual RefPtr<Web::ResourceLoaderConnectorRequest> start_request(DeprecatedString const& method, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const& request_headers, ReadonlyBytes request_body, Core::ProxyData const& proxy) override + static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path) { - RefPtr<Web::ResourceLoaderConnectorRequest> request; - if (url.scheme().equals_ignoring_ascii_case("http"sv)) { - auto request_or_error = HTTPHeadlessRequest::create(method, url, request_headers, request_body, proxy); - if (request_or_error.is_error()) - return {}; - request = request_or_error.release_value(); - } - if (url.scheme().equals_ignoring_ascii_case("https"sv)) { - auto request_or_error = HTTPSHeadlessRequest::create(method, url, request_headers, request_body, proxy); - if (request_or_error.is_error()) - return {}; - request = request_or_error.release_value(); - } - if (url.scheme().equals_ignoring_ascii_case("gemini"sv)) { - auto request_or_error = GeminiHeadlessRequest::create(method, url, request_headers, request_body, proxy); - if (request_or_error.is_error()) - return {}; - request = request_or_error.release_value(); - } - if (request) - s_all_requests.set(request); - return request; - } - -private: - HeadlessRequestServer() { } -}; + auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView())); -class HeadlessWebSocketClientManager : public Web::WebSockets::WebSocketClientManager { -public: - class HeadlessWebSocket - : public Web::WebSockets::WebSocketClientSocket - , public Weakable<HeadlessWebSocket> { - public: - static NonnullRefPtr<HeadlessWebSocket> create(NonnullRefPtr<WebSocket::WebSocket> underlying_socket) - { - return adopt_ref(*new HeadlessWebSocket(move(underlying_socket))); - } - - virtual ~HeadlessWebSocket() override - { - } - - virtual Web::WebSockets::WebSocket::ReadyState ready_state() override - { - switch (m_websocket->ready_state()) { - case WebSocket::ReadyState::Connecting: - return Web::WebSockets::WebSocket::ReadyState::Connecting; - case WebSocket::ReadyState::Open: - return Web::WebSockets::WebSocket::ReadyState::Open; - case WebSocket::ReadyState::Closing: - return Web::WebSockets::WebSocket::ReadyState::Closing; - case WebSocket::ReadyState::Closed: - return Web::WebSockets::WebSocket::ReadyState::Closed; - } - VERIFY_NOT_REACHED(); - } - - virtual DeprecatedString subprotocol_in_use() override - { - return m_websocket->subprotocol_in_use(); - } - - virtual void send(ByteBuffer binary_or_text_message, bool is_text) override - { - m_websocket->send(WebSocket::Message(binary_or_text_message, is_text)); - } - - virtual void send(StringView message) override - { - m_websocket->send(WebSocket::Message(message)); - } +#if defined(AK_OS_SERENITY) + view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view)); - virtual void close(u16 code, DeprecatedString reason) override - { - m_websocket->close(code, reason); - } + if (!web_driver_ipc_path.is_empty()) + view->client().async_connect_to_webdriver(web_driver_ipc_path); +#else + auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv)); + view->m_client_state.client = TRY(view->launch_web_content_process(candidate_web_content_paths, web_driver_ipc_path)); +#endif - private: - HeadlessWebSocket(NonnullRefPtr<WebSocket::WebSocket> underlying_socket) - : m_websocket(move(underlying_socket)) - { - m_websocket->on_open = [weak_this = make_weak_ptr()] { - if (auto strong_this = weak_this.strong_ref()) - if (strong_this->on_open) - strong_this->on_open(); - }; - m_websocket->on_message = [weak_this = make_weak_ptr()](auto message) { - if (auto strong_this = weak_this.strong_ref()) { - if (strong_this->on_message) { - strong_this->on_message(Web::WebSockets::WebSocketClientSocket::Message { - .data = move(message.data()), - .is_text = message.is_text(), - }); - } - } - }; - m_websocket->on_error = [weak_this = make_weak_ptr()](auto error) { - if (auto strong_this = weak_this.strong_ref()) { - if (strong_this->on_error) { - switch (error) { - case WebSocket::WebSocket::Error::CouldNotEstablishConnection: - strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::CouldNotEstablishConnection); - return; - case WebSocket::WebSocket::Error::ConnectionUpgradeFailed: - strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::ConnectionUpgradeFailed); - return; - case WebSocket::WebSocket::Error::ServerClosedSocket: - strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::ServerClosedSocket); - return; - } - VERIFY_NOT_REACHED(); - } - } - }; - m_websocket->on_close = [weak_this = make_weak_ptr()](u16 code, DeprecatedString reason, bool was_clean) { - if (auto strong_this = weak_this.strong_ref()) - if (strong_this->on_close) - strong_this->on_close(code, move(reason), was_clean); - }; - } + view->client().async_update_system_theme(move(theme)); + view->client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query()); - NonnullRefPtr<WebSocket::WebSocket> m_websocket; - }; + view->client().async_set_viewport_rect({ { 0, 0 }, window_size }); + view->client().async_set_window_size(window_size); - static NonnullRefPtr<HeadlessWebSocketClientManager> create() - { - return adopt_ref(*new HeadlessWebSocketClientManager()); + return view; } - virtual ~HeadlessWebSocketClientManager() override { } - - virtual RefPtr<Web::WebSockets::WebSocketClientSocket> connect(AK::URL const& url, DeprecatedString const& origin, Vector<DeprecatedString> const& protocols) override + RefPtr<Gfx::Bitmap> take_screenshot() { - WebSocket::ConnectionInfo connection_info(url); - connection_info.set_origin(origin); - connection_info.set_protocols(protocols); - - auto connection = HeadlessWebSocket::create(WebSocket::WebSocket::create(move(connection_info))); - return connection; + return client().take_document_screenshot().bitmap(); } private: - HeadlessWebSocketClientManager() { } + HeadlessWebContentView() = default; + + void notify_server_did_layout(Badge<WebView::WebContentClient>, Gfx::IntSize) override { } + void notify_server_did_paint(Badge<WebView::WebContentClient>, i32) override { } + void notify_server_did_invalidate_content_rect(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override { } + void notify_server_did_change_selection(Badge<WebView::WebContentClient>) override { } + void notify_server_did_request_cursor_change(Badge<WebView::WebContentClient>, Gfx::StandardCursor) override { } + void notify_server_did_change_title(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } + void notify_server_did_request_scroll(Badge<WebView::WebContentClient>, i32, i32) override { } + void notify_server_did_request_scroll_to(Badge<WebView::WebContentClient>, Gfx::IntPoint) override { } + void notify_server_did_request_scroll_into_view(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override { } + void notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override { } + void notify_server_did_leave_tooltip_area(Badge<WebView::WebContentClient>) override { } + void notify_server_did_hover_link(Badge<WebView::WebContentClient>, const URL&) override { } + void notify_server_did_unhover_link(Badge<WebView::WebContentClient>) override { } + void notify_server_did_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { } + void notify_server_did_middle_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { } + void notify_server_did_start_loading(Badge<WebView::WebContentClient>, const URL&, bool) override { } + void notify_server_did_finish_loading(Badge<WebView::WebContentClient>, const URL&) override { } + void notify_server_did_request_navigate_back(Badge<WebView::WebContentClient>) override { } + void notify_server_did_request_navigate_forward(Badge<WebView::WebContentClient>) override { } + void notify_server_did_request_refresh(Badge<WebView::WebContentClient>) override { } + void notify_server_did_request_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint) override { } + void notify_server_did_request_link_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint, const URL&, DeprecatedString const&, unsigned) override { } + void notify_server_did_request_image_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint, const URL&, DeprecatedString const&, unsigned, Gfx::ShareableBitmap const&) override { } + void notify_server_did_request_alert(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } + void notify_server_did_request_confirm(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } + void notify_server_did_request_prompt(Badge<WebView::WebContentClient>, DeprecatedString const&, DeprecatedString const&) override { } + void notify_server_did_request_set_prompt_text(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } + void notify_server_did_request_accept_dialog(Badge<WebView::WebContentClient>) override { } + void notify_server_did_request_dismiss_dialog(Badge<WebView::WebContentClient>) override { } + void notify_server_did_get_source(const URL&, DeprecatedString const&) override { } + void notify_server_did_get_dom_tree(DeprecatedString const&) override { } + void notify_server_did_get_dom_node_properties(i32, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&) override { } + void notify_server_did_get_accessibility_tree(DeprecatedString const&) override { } + void notify_server_did_output_js_console_message(i32) override { } + void notify_server_did_get_js_console_messages(i32, Vector<DeprecatedString> const&, Vector<DeprecatedString> const&) override { } + void notify_server_did_change_favicon(Gfx::Bitmap const&) override { } + Vector<Web::Cookie::Cookie> notify_server_did_request_all_cookies(Badge<WebView::WebContentClient>, URL const&) override { return {}; } + Optional<Web::Cookie::Cookie> notify_server_did_request_named_cookie(Badge<WebView::WebContentClient>, URL const&, DeprecatedString const&) override { return {}; } + DeprecatedString notify_server_did_request_cookie(Badge<WebView::WebContentClient>, const URL&, Web::Cookie::Source) override { return {}; } + void notify_server_did_set_cookie(Badge<WebView::WebContentClient>, const URL&, Web::Cookie::ParsedCookie const&, Web::Cookie::Source) override { } + void notify_server_did_update_cookie(Badge<WebView::WebContentClient>, Web::Cookie::Cookie const&) override { } + void notify_server_did_close_browsing_context(Badge<WebView::WebContentClient>) override { } + void notify_server_did_update_resource_count(i32) override { } + void notify_server_did_request_restore_window() override { } + Gfx::IntPoint notify_server_did_request_reposition_window(Gfx::IntPoint) override { return {}; } + Gfx::IntSize notify_server_did_request_resize_window(Gfx::IntSize) override { return {}; } + Gfx::IntRect notify_server_did_request_maximize_window() override { return {}; } + Gfx::IntRect notify_server_did_request_minimize_window() override { return {}; } + Gfx::IntRect notify_server_did_request_fullscreen_window() override { return {}; } + void notify_server_did_request_file(Badge<WebView::WebContentClient>, DeprecatedString const&, i32) override { } + void notify_server_did_finish_handling_input_event(bool) override { } + void update_zoom() override { } + void create_client() override { } }; -static void load_page_for_screenshot_and_exit(HeadlessBrowserPageClient& page_client, int take_screenshot_after) +static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, HeadlessWebContentView& view, int screenshot_timeout) { - dbgln("Taking screenshot after {} seconds", take_screenshot_after); - - auto timer = Core::Timer::create_single_shot( - take_screenshot_after * 1000, - [&]() { - // FIXME: Allow passing the output path as argument - DeprecatedString output_file_path = "output.png"; - dbgln("Saving to {}", output_file_path); - - if (Core::DeprecatedFile::exists(output_file_path)) - MUST(Core::DeprecatedFile::remove(output_file_path, Core::DeprecatedFile::RecursionMode::Disallowed)); + // FIXME: Allow passing the output path as an argument. + static constexpr auto output_file_path = "output.png"sv; - auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write)); + if (Core::DeprecatedFile::exists(output_file_path)) + TRY(Core::DeprecatedFile::remove(output_file_path, Core::DeprecatedFile::RecursionMode::Disallowed)); - auto output_rect = page_client.screen_rect(); - auto output_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, output_rect.size().to_type<int>())); + outln("Taking screenshot after {} seconds", screenshot_timeout); - page_client.paint(output_rect, output_bitmap); + auto timer = TRY(Core::Timer::create_single_shot( + screenshot_timeout * 1000, + [&]() { + if (auto screenshot = view.take_screenshot()) { + outln("Saving screenshot to {}", output_file_path); - auto image_buffer = MUST(Gfx::PNGWriter::encode(output_bitmap)); - MUST(output_file->write(image_buffer.bytes())); + auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write)); + auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot)); + MUST(output_file->write(image_buffer.bytes())); + } else { + warnln("No screenshot available"); + } - exit(0); - }).release_value_but_fixme_should_propagate_errors(); + event_loop.quit(0); + })); timer->start(); + return timer; } ErrorOr<int> serenity_main(Main::Arguments arguments) { - int take_screenshot_after = 1; +#if !defined(AK_OS_SERENITY) + QCoreApplication app(arguments.argc, arguments.argv); +#endif + Core::EventLoop event_loop; + + int screenshot_timeout = 1; StringView url; - StringView resources_folder; - StringView error_page_url; - StringView ca_certs_path; - StringView webdriver_ipc_path; + auto resources_folder = "/res"sv; + StringView web_driver_ipc_path; - Core::EventLoop event_loop; Core::ArgsParser args_parser; args_parser.set_general_help("This utility runs the Browser in headless mode."); - args_parser.add_option(take_screenshot_after, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n"); + args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n"); args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path"); - args_parser.add_option(error_page_url, "URL for the error page (defaults to file:///res/html/error.html)", "error-page", 'e', "error-page-url"); - args_parser.add_option(ca_certs_path, "The bundled ca certificates file", "certs", 'c', "ca-certs-path"); - args_parser.add_option(webdriver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path"); + args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path"); args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::Yes); args_parser.parse(arguments); - Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity); - Web::Platform::FontPlugin::install(*new Web::Platform::FontPluginSerenity); - Web::Platform::ImageCodecPlugin::install(*new ImageCodecPluginHeadless); - Web::ResourceLoader::initialize(HeadlessRequestServer::create()); - Web::WebSockets::WebSocketClientManager::initialize(HeadlessWebSocketClientManager::create()); - - if (!resources_folder.is_empty()) { - Web::FrameLoader::set_default_favicon_path(LexicalPath::join(resources_folder, "icons/16x16/app-browser.png"sv).string()); - Gfx::FontDatabase::set_default_fonts_lookup_path(LexicalPath::join(resources_folder, "fonts"sv).string()); - } - if (!ca_certs_path.is_empty()) { - auto config_result = Core::ConfigFile::open(ca_certs_path); - if (config_result.is_error()) { - dbgln("Failed to load CA Certificates: {}", config_result.error()); - } else { - auto config = config_result.release_value(); - DefaultRootCACertificates::the().reload_certificates(config); - } - } - Gfx::FontDatabase::set_default_font_query("Katica 10 400 0"); Gfx::FontDatabase::set_window_title_font_query("Katica 10 700 0"); Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0"); - if (!error_page_url.is_empty()) - Web::FrameLoader::set_error_page_url(error_page_url); - - auto page_client = HeadlessBrowserPageClient::create(); + auto fonts_path = LexicalPath::join(resources_folder, "fonts"sv); + Gfx::FontDatabase::set_default_fonts_lookup_path(fonts_path.string()); - if (!resources_folder.is_empty()) { - auto system_theme = TRY(Gfx::load_system_theme(LexicalPath::join(resources_folder, "themes/Default.ini"sv).string())); - page_client->setup_palette(system_theme); - } else { - auto system_theme = TRY(Gfx::load_system_theme("/res/themes/Default.ini")); - page_client->setup_palette(system_theme); - } + auto theme_path = LexicalPath::join(resources_folder, "themes"sv, "Default.ini"sv); + auto theme = TRY(Gfx::load_system_theme(theme_path.string())); - dbgln("Loading {}", url); - page_client->load(AK::URL(url)); + // FIXME: Allow passing the window size as an argument. + static constexpr Gfx::IntSize window_size { 800, 600 }; - // FIXME: Allow passing these values as arguments - page_client->set_viewport_rect({ 0, 0, 800, 600 }); - page_client->set_screen_rect({ 0, 0, 800, 600 }); + auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path)); + view->load(URL { url }); - if (!webdriver_ipc_path.is_empty()) - TRY(page_client->connect_to_webdriver(webdriver_ipc_path)); - else - load_page_for_screenshot_and_exit(*page_client, take_screenshot_after); + RefPtr<Core::Timer> timer; + if (web_driver_ipc_path.is_empty()) + timer = TRY(load_page_for_screenshot_and_exit(event_loop, *view, screenshot_timeout)); return event_loop.exec(); } |