From 3c1c2994f1a999d8b6767caffb49b8669cda2936 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 4 Nov 2022 17:08:02 -0400 Subject: [PATCH] LibWebView+WebContent: Add an IPC to take an element screenshot --- .../LibWebView/OutOfProcessWebView.cpp | 5 +++++ .../Libraries/LibWebView/OutOfProcessWebView.h | 1 + .../WebContent/ConnectionFromClient.cpp | 17 +++++++++++++++++ .../Services/WebContent/ConnectionFromClient.h | 1 + .../Services/WebContent/WebContentServer.ipc | 1 + 5 files changed, 25 insertions(+) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index cb088c4f6d7..c5a9a6a6704 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -617,6 +617,11 @@ Gfx::ShareableBitmap OutOfProcessWebView::take_screenshot() const return {}; } +Gfx::ShareableBitmap OutOfProcessWebView::take_element_screenshot(i32 element_id) +{ + return client().take_element_screenshot(element_id); +} + 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 bc03b5b3469..2cc526d32c5 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -85,6 +85,7 @@ public: void set_window_size(Gfx::IntSize const&); Gfx::ShareableBitmap take_screenshot() const; + Gfx::ShareableBitmap take_element_screenshot(i32 element_id); 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 ef08c17743f..b97ffbf7ff7 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -691,6 +691,23 @@ Messages::WebContentServer::IsElementEnabledResponse ConnectionFromClient::is_el return { enabled }; } +Messages::WebContentServer::TakeElementScreenshotResponse ConnectionFromClient::take_element_screenshot(i32 element_id) +{ + auto element = find_element_by_id(element_id); + if (!element.has_value()) + return { {} }; + + auto viewport_rect = page().top_level_browsing_context().viewport_rect(); + + auto rect = calculate_absolute_rect_of_element(page(), *element); + rect.intersect(viewport_rect); + + 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 7130f9644dd..8c404e5a0bc 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -95,6 +95,7 @@ private: virtual Messages::WebContentServer::GetElementTagNameResponse get_element_tag_name(i32 element_id) override; 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::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 51c6fbf3bd7..6b45b1002f8 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -52,6 +52,7 @@ endpoint WebContentServer get_element_tag_name(i32 element_id) => (String tag_name) 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) webdriver_execute_script(String body, Vector json_arguments, Optional timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)