Skip to content

Commit

Permalink
LibWebView+UI: Handle common WebView client initialization in LibWebView
Browse files Browse the repository at this point in the history
No need to have every UI manually implement these common steps.
  • Loading branch information
trflynn89 authored and awesomekling committed Nov 14, 2024
1 parent 44d6601 commit 4e1dab4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 97 deletions.
42 changes: 42 additions & 0 deletions Libraries/LibWebView/ViewImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#include <LibCore/StandardPaths.h>
#include <LibCore/Timer.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/UserAgent.h>
#include <LibWebView/ViewImplementation.h>

#ifdef AK_OS_MACOS
Expand Down Expand Up @@ -500,6 +503,45 @@ void ViewImplementation::handle_resize()
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)
{
dbgln("\033[31;1mWebContent process crashed!\033[0m Last page loaded: {}", m_url);
Expand Down
3 changes: 2 additions & 1 deletion Libraries/LibWebView/ViewImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class ViewImplementation {
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_exported_inspector_html;
Function<void()> on_web_content_crashed;

virtual Web::DevicePixelSize viewport_size() const = 0;
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
Expand All @@ -253,7 +254,7 @@ class ViewImplementation {
No,
Yes,
};
virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) { }
virtual void initialize_client(CreateNewClient = CreateNewClient::Yes);

enum class LoadErrorPage {
No,
Expand Down
35 changes: 1 addition & 34 deletions UI/AppKit/Interface/LadybirdWebViewBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Rect.h>
#include <LibIPC/File.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/UserAgent.h>

#import <Interface/Palette.h>

Expand Down Expand Up @@ -144,45 +141,15 @@ Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position)

void WebViewBridge::initialize_client(CreateNewClient create_new_client)
{
if (create_new_client == CreateNewClient::Yes) {
m_client_state = {};
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 = 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);

set_system_visibility_state(m_system_visibility_state);
update_palette();

if (!m_screen_rects.is_empty()) {
// FIXME: Update the screens again if they ever change.
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)
Expand Down
31 changes: 1 addition & 30 deletions UI/Headless/HeadlessWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWebView/HelperProcess.h>
#include <UI/Headless/Application.h>
#include <UI/Headless/HeadlessWebView.h>

Expand Down Expand Up @@ -153,39 +151,12 @@ NonnullOwnPtr<HeadlessWebView> HeadlessWebView::create_child(HeadlessWebView con

void HeadlessWebView::initialize_client(CreateNewClient create_new_client)
{
if (create_new_client == CreateNewClient::Yes) {
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);
ViewImplementation::initialize_client(create_new_client);

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_window_size(m_client_state.page_index, viewport_size());
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()
Expand Down
3 changes: 0 additions & 3 deletions UI/Headless/HeadlessWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#pragma once

#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/RefPtr.h>
#include <LibCore/Forward.h>
#include <LibCore/Promise.h>
Expand All @@ -31,8 +30,6 @@ class HeadlessWebView final : public WebView::ViewImplementation {
TestPromise& test_promise() { return *m_test_promise; }
void on_test_complete(TestCompletion);

Function<void()> on_web_content_crashed;

private:
HeadlessWebView(Core::AnonymousBuffer theme, Web::DevicePixelSize viewport_size);

Expand Down
30 changes: 1 addition & 29 deletions UI/Qt/WebContentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
#include <LibGfx/Palette.h>
#include <LibGfx/Rect.h>
#include <LibGfx/SystemTheme.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/UIEvents/KeyCode.h>
#include <LibWeb/UIEvents/MouseButton.h>
#include <LibWebView/Application.h>
#include <LibWebView/HelperProcess.h>
#include <LibWebView/WebContentClient.h>
#include <UI/Qt/Application.h>
#include <UI/Qt/StringUtils.h>
Expand Down Expand Up @@ -619,36 +617,10 @@ void WebContentView::update_screen_rects()

void WebContentView::initialize_client(WebView::ViewImplementation::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 = 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);
ViewImplementation::initialize_client(create_new_client);

update_palette();
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)
Expand Down

0 comments on commit 4e1dab4

Please sign in to comment.