LibWebView+UI: Handle common WebView client initialization in LibWebView

No need to have every UI manually implement these common steps.
This commit is contained in:
Timothy Flynn 2024-11-13 16:35:17 -05:00 committed by Andreas Kling
parent 44d6601dc5
commit 4e1dab477a
Notes: github-actions[bot] 2024-11-14 10:48:24 +00:00
6 changed files with 47 additions and 97 deletions

View file

@ -10,8 +10,11 @@
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
#include <LibCore/Timer.h> #include <LibCore/Timer.h>
#include <LibGfx/ImageFormats/PNGWriter.h> #include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/Infra/Strings.h> #include <LibWeb/Infra/Strings.h>
#include <LibWebView/Application.h> #include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/UserAgent.h>
#include <LibWebView/ViewImplementation.h> #include <LibWebView/ViewImplementation.h>
#ifdef AK_OS_MACOS #ifdef AK_OS_MACOS
@ -500,6 +503,45 @@ void ViewImplementation::handle_resize()
client().async_set_viewport_size(page_id(), this->viewport_size()); client().async_set_viewport_size(page_id(), this->viewport_size());
} }
void ViewImplementation::initialize_client(CreateNewClient create_new_client)
{
if (create_new_client == CreateNewClient::Yes) {
m_client_state = {};
// FIXME: Fail to open the tab, rather than crashing the whole application if these fail.
auto request_server_socket = connect_new_request_server_client().release_value_but_fixme_should_propagate_errors();
auto image_decoder_socket = connect_new_image_decoder_client().release_value_but_fixme_should_propagate_errors();
m_client_state.client = launch_web_content_process(*this, AK::move(image_decoder_socket), AK::move(request_server_socket)).release_value_but_fixme_should_propagate_errors();
} else {
m_client_state.client->register_view(m_client_state.page_index, *this);
}
m_client_state.client->on_web_content_process_crash = [this] {
Core::deferred_invoke([this] {
handle_web_content_process_crash();
if (on_web_content_crashed)
on_web_content_crashed();
});
};
m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio);
client().async_set_system_visibility_state(m_client_state.page_index, m_system_visibility_state);
if (auto webdriver_content_ipc_path = Application::chrome_options().webdriver_content_ipc_path; webdriver_content_ipc_path.has_value())
client().async_connect_to_webdriver(m_client_state.page_index, *webdriver_content_ipc_path);
if (Application::chrome_options().allow_popups == AllowPopups::Yes)
client().async_debug_request(m_client_state.page_index, "block-pop-ups"sv, "off"sv);
if (auto const& user_agent_preset = Application::web_content_options().user_agent_preset; user_agent_preset.has_value())
client().async_debug_request(m_client_state.page_index, "spoof-user-agent"sv, *user_agents.get(*user_agent_preset));
}
void ViewImplementation::handle_web_content_process_crash(LoadErrorPage load_error_page) void ViewImplementation::handle_web_content_process_crash(LoadErrorPage load_error_page)
{ {
dbgln("\033[31;1mWebContent process crashed!\033[0m Last page loaded: {}", m_url); dbgln("\033[31;1mWebContent process crashed!\033[0m Last page loaded: {}", m_url);

View file

@ -230,6 +230,7 @@ public:
Function<void(size_t, Gfx::IntPoint)> on_inspector_requested_cookie_context_menu; Function<void(size_t, Gfx::IntPoint)> on_inspector_requested_cookie_context_menu;
Function<void(String const&)> on_inspector_executed_console_script; Function<void(String const&)> on_inspector_executed_console_script;
Function<void(String const&)> on_inspector_exported_inspector_html; Function<void(String const&)> on_inspector_exported_inspector_html;
Function<void()> on_web_content_crashed;
virtual Web::DevicePixelSize viewport_size() const = 0; virtual Web::DevicePixelSize viewport_size() const = 0;
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0; virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
@ -253,7 +254,7 @@ protected:
No, No,
Yes, Yes,
}; };
virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) { } virtual void initialize_client(CreateNewClient = CreateNewClient::Yes);
enum class LoadErrorPage { enum class LoadErrorPage {
No, No,

View file

@ -8,10 +8,7 @@
#include <LibGfx/Font/FontDatabase.h> #include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Rect.h> #include <LibGfx/Rect.h>
#include <LibIPC/File.h> #include <LibIPC/File.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWebView/Application.h> #include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/UserAgent.h>
#import <Interface/Palette.h> #import <Interface/Palette.h>
@ -144,45 +141,15 @@ Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position)
void WebViewBridge::initialize_client(CreateNewClient create_new_client) void WebViewBridge::initialize_client(CreateNewClient create_new_client)
{ {
if (create_new_client == CreateNewClient::Yes) { ViewImplementation::initialize_client(create_new_client);
m_client_state = {};
auto request_server_socket = WebView::connect_new_request_server_client().release_value_but_fixme_should_propagate_errors();
auto image_decoder_socket = WebView::connect_new_image_decoder_client().release_value_but_fixme_should_propagate_errors();
m_client_state.client = launch_web_content_process(*this, AK::move(image_decoder_socket), AK::move(request_server_socket)).release_value_but_fixme_should_propagate_errors();
} else {
m_client_state.client->register_view(m_client_state.page_index, *this);
}
m_client_state.client->on_web_content_process_crash = [this] {
Core::deferred_invoke([this] {
handle_web_content_process_crash();
});
};
m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio);
client().async_set_preferred_color_scheme(m_client_state.page_index, m_preferred_color_scheme); client().async_set_preferred_color_scheme(m_client_state.page_index, m_preferred_color_scheme);
set_system_visibility_state(m_system_visibility_state);
update_palette(); update_palette();
if (!m_screen_rects.is_empty()) { if (!m_screen_rects.is_empty()) {
// FIXME: Update the screens again if they ever change. // FIXME: Update the screens again if they ever change.
client().async_update_screen_rects(m_client_state.page_index, m_screen_rects, 0); client().async_update_screen_rects(m_client_state.page_index, m_screen_rects, 0);
} }
if (auto const& webdriver_content_ipc_path = WebView::Application::chrome_options().webdriver_content_ipc_path; webdriver_content_ipc_path.has_value()) {
client().async_connect_to_webdriver(m_client_state.page_index, *webdriver_content_ipc_path);
}
if (auto const& user_agent_preset = WebView::Application::web_content_options().user_agent_preset; user_agent_preset.has_value()) {
auto user_agent = *WebView::user_agents.get(*user_agent_preset);
client().async_debug_request(m_client_state.page_index, "spoof-user-agent"sv, user_agent);
}
} }
void WebViewBridge::initialize_client_as_child(WebViewBridge const& parent, u64 page_index) void WebViewBridge::initialize_client_as_child(WebViewBridge const& parent, u64 page_index)

View file

@ -6,8 +6,6 @@
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h> #include <LibGfx/ShareableBitmap.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWebView/HelperProcess.h>
#include <UI/Headless/Application.h> #include <UI/Headless/Application.h>
#include <UI/Headless/HeadlessWebView.h> #include <UI/Headless/HeadlessWebView.h>
@ -153,39 +151,12 @@ NonnullOwnPtr<HeadlessWebView> HeadlessWebView::create_child(HeadlessWebView con
void HeadlessWebView::initialize_client(CreateNewClient create_new_client) void HeadlessWebView::initialize_client(CreateNewClient create_new_client)
{ {
if (create_new_client == CreateNewClient::Yes) { ViewImplementation::initialize_client(create_new_client);
auto request_server_socket = WebView::connect_new_request_server_client().release_value_but_fixme_should_propagate_errors();
auto image_decoder_socket = WebView::connect_new_image_decoder_client().release_value_but_fixme_should_propagate_errors();
m_client_state.client = WebView::launch_web_content_process(*this, move(image_decoder_socket), move(request_server_socket)).release_value_but_fixme_should_propagate_errors();
} else {
m_client_state.client->register_view(m_client_state.page_index, *this);
}
m_client_state.client_handle = MUST(Web::Crypto::generate_random_uuid());
client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
client().async_update_system_theme(m_client_state.page_index, m_theme); client().async_update_system_theme(m_client_state.page_index, m_theme);
client().async_set_viewport_size(m_client_state.page_index, viewport_size()); client().async_set_viewport_size(m_client_state.page_index, viewport_size());
client().async_set_window_size(m_client_state.page_index, viewport_size()); client().async_set_window_size(m_client_state.page_index, viewport_size());
client().async_update_screen_rects(m_client_state.page_index, { screen_rect }, 0); client().async_update_screen_rects(m_client_state.page_index, { screen_rect }, 0);
set_system_visibility_state(m_system_visibility_state);
if (Application::chrome_options().allow_popups == WebView::AllowPopups::Yes)
client().async_debug_request(m_client_state.page_index, "block-pop-ups"sv, "off"sv);
if (auto const& web_driver_ipc_path = Application::chrome_options().webdriver_content_ipc_path; web_driver_ipc_path.has_value())
client().async_connect_to_webdriver(m_client_state.page_index, *web_driver_ipc_path);
m_client_state.client->on_web_content_process_crash = [this] {
Core::deferred_invoke([this] {
handle_web_content_process_crash(LoadErrorPage::No);
if (on_web_content_crashed)
on_web_content_crashed();
});
};
} }
void HeadlessWebView::clear_content_filters() void HeadlessWebView::clear_content_filters()

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <AK/Badge.h> #include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/RefPtr.h> #include <AK/RefPtr.h>
#include <LibCore/Forward.h> #include <LibCore/Forward.h>
#include <LibCore/Promise.h> #include <LibCore/Promise.h>
@ -31,8 +30,6 @@ public:
TestPromise& test_promise() { return *m_test_promise; } TestPromise& test_promise() { return *m_test_promise; }
void on_test_complete(TestCompletion); void on_test_complete(TestCompletion);
Function<void()> on_web_content_crashed;
private: private:
HeadlessWebView(Core::AnonymousBuffer theme, Web::DevicePixelSize viewport_size); HeadlessWebView(Core::AnonymousBuffer theme, Web::DevicePixelSize viewport_size);

View file

@ -20,11 +20,9 @@
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
#include <LibGfx/Rect.h> #include <LibGfx/Rect.h>
#include <LibGfx/SystemTheme.h> #include <LibGfx/SystemTheme.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/UIEvents/KeyCode.h> #include <LibWeb/UIEvents/KeyCode.h>
#include <LibWeb/UIEvents/MouseButton.h> #include <LibWeb/UIEvents/MouseButton.h>
#include <LibWebView/Application.h> #include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/WebContentClient.h> #include <LibWebView/WebContentClient.h>
#include <UI/Qt/Application.h> #include <UI/Qt/Application.h>
#include <UI/Qt/StringUtils.h> #include <UI/Qt/StringUtils.h>
@ -619,36 +617,10 @@ void WebContentView::update_screen_rects()
void WebContentView::initialize_client(WebView::ViewImplementation::CreateNewClient create_new_client) void WebContentView::initialize_client(WebView::ViewImplementation::CreateNewClient create_new_client)
{ {
if (create_new_client == CreateNewClient::Yes) { ViewImplementation::initialize_client(create_new_client);
m_client_state = {};
// FIXME: Fail to open the tab, rather than crashing the whole application if these fail.
auto request_server_socket = WebView::connect_new_request_server_client().release_value_but_fixme_should_propagate_errors();
auto image_decoder_socket = WebView::connect_new_image_decoder_client().release_value_but_fixme_should_propagate_errors();
m_client_state.client = launch_web_content_process(*this, AK::move(image_decoder_socket), AK::move(request_server_socket)).release_value_but_fixme_should_propagate_errors();
} else {
m_client_state.client->register_view(m_client_state.page_index, *this);
}
m_client_state.client->on_web_content_process_crash = [this] {
Core::deferred_invoke([this] {
handle_web_content_process_crash();
});
};
m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
client().async_set_window_handle(m_client_state.page_index, m_client_state.client_handle);
client().async_set_device_pixels_per_css_pixel(m_client_state.page_index, m_device_pixel_ratio);
set_system_visibility_state(m_system_visibility_state);
update_palette(); update_palette();
update_screen_rects(); update_screen_rects();
if (auto webdriver_content_ipc_path = WebView::Application::chrome_options().webdriver_content_ipc_path; webdriver_content_ipc_path.has_value())
client().async_connect_to_webdriver(m_client_state.page_index, *webdriver_content_ipc_path);
} }
void WebContentView::update_cursor(Gfx::StandardCursor cursor) void WebContentView::update_cursor(Gfx::StandardCursor cursor)