From 95e591b61b4866f22f949e3e715e83a1cbdb3392 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 5 Nov 2022 00:09:41 -0400 Subject: [PATCH] LibWebView+WebContent: Add an IPC to take a full document screenshot --- .../Libraries/LibWebView/OutOfProcessWebView.cpp | 5 +++++ .../Libraries/LibWebView/OutOfProcessWebView.h | 1 + .../Services/WebContent/ConnectionFromClient.cpp | 14 ++++++++++++++ .../Services/WebContent/ConnectionFromClient.h | 1 + Userland/Services/WebContent/WebContentServer.ipc | 1 + 5 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index c5a9a6a670..d04df78f99 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -622,6 +622,11 @@ Gfx::ShareableBitmap OutOfProcessWebView::take_element_screenshot(i32 element_id return client().take_element_screenshot(element_id); } +Gfx::ShareableBitmap OutOfProcessWebView::take_document_screenshot() +{ + return client().take_document_screenshot(); +} + Messages::WebContentServer::WebdriverExecuteScriptResponse OutOfProcessWebView::webdriver_execute_script(String const& body, Vector const& json_arguments, Optional const& timeout, bool async) { return client().webdriver_execute_script(body, json_arguments, timeout, async); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 2cc526d32c..abf43cc71e 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -86,6 +86,7 @@ public: Gfx::ShareableBitmap take_screenshot() const; Gfx::ShareableBitmap take_element_screenshot(i32 element_id); + Gfx::ShareableBitmap take_document_screenshot(); Messages::WebContentServer::WebdriverExecuteScriptResponse webdriver_execute_script(String const& body, Vector const& json_arguments, Optional const& timeout, bool async); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index b97ffbf7ff..e4f76e2c92 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -708,6 +708,20 @@ Messages::WebContentServer::TakeElementScreenshotResponse ConnectionFromClient:: return { bitmap->to_shareable_bitmap() }; } +Messages::WebContentServer::TakeDocumentScreenshotResponse ConnectionFromClient::take_document_screenshot() +{ + auto* document = page().top_level_browsing_context().active_document(); + if (!document || !document->document_element()) + return { {} }; + + auto rect = calculate_absolute_rect_of_element(page(), *document->document_element()); + + auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, rect.size()).release_value_but_fixme_should_propagate_errors(); + m_page_host->paint(rect, *bitmap); + + return { bitmap->to_shareable_bitmap() }; +} + Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text() { return page().focused_context().selected_text(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index 8c404e5a0b..51cb3c4126 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -96,6 +96,7 @@ private: virtual Messages::WebContentServer::GetElementRectResponse get_element_rect(i32 element_id) override; virtual Messages::WebContentServer::IsElementEnabledResponse is_element_enabled(i32 element_id) override; virtual Messages::WebContentServer::TakeElementScreenshotResponse take_element_screenshot(i32 element_id) override; + virtual Messages::WebContentServer::TakeDocumentScreenshotResponse take_document_screenshot() override; virtual Messages::WebContentServer::GetLocalStorageEntriesResponse get_local_storage_entries() override; virtual Messages::WebContentServer::GetSessionStorageEntriesResponse get_session_storage_entries() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 6b45b1002f..ce182d721a 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -53,6 +53,7 @@ endpoint WebContentServer get_element_rect(i32 element_id) => (Gfx::IntRect rect) is_element_enabled(i32 element_id) => (bool enabled) take_element_screenshot(i32 element_id) => (Gfx::ShareableBitmap data) + take_document_screenshot() => (Gfx::ShareableBitmap data) webdriver_execute_script(String body, Vector json_arguments, Optional timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)