Skip to content

Commit

Permalink
Ladybird+LibWebView: Move options used to launch WebContent to a struct
Browse files Browse the repository at this point in the history
It is currently a bit messy to pass these options along from main() to
where WebContent is actually launched. If a new flag were to be added,
there are a couple dozen files that need to be updated to pass that flag
along. With this change, the flag can just be added to the struct, set
in main(), and handled in launch_web_content_process().
  • Loading branch information
trflynn89 committed Dec 2, 2023
1 parent 8504d8f commit 07e9a8f
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 74 deletions.
3 changes: 3 additions & 0 deletions Ladybird/AppKit/Application/ApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <AK/StringView.h>
#include <AK/URL.h>
#include <AK/Vector.h>
#include <Ladybird/Types.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/HTML/ActivateTab.h>
#include <LibWebView/CookieJar.h>
Expand All @@ -24,6 +25,7 @@
- (nullable instancetype)init:(Vector<URL>)initial_urls
newTabPageURL:(URL)new_tab_page_url
withCookieJar:(WebView::CookieJar)cookie_jar
webContentOptions:(Ladybird::WebContentOptions const&)web_content_options
webdriverContentIPCPath:(StringView)webdriver_content_ipc_path;

- (nonnull TabController*)createNewTab:(Optional<URL> const&)url
Expand All @@ -38,6 +40,7 @@
- (void)removeTab:(nonnull TabController*)controller;

- (WebView::CookieJar&)cookieJar;
- (Ladybird::WebContentOptions const&)webContentOptions;
- (Optional<StringView> const&)webdriverContentIPCPath;
- (Web::CSS::PreferredColorScheme)preferredColorScheme;
- (WebView::SearchEngine const&)searchEngine;
Expand Down
9 changes: 9 additions & 0 deletions Ladybird/AppKit/Application/ApplicationDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @interface ApplicationDelegate ()
// This will always be populated, but we cannot have a non-default constructible instance variable.
Optional<WebView::CookieJar> m_cookie_jar;

Ladybird::WebContentOptions m_web_content_options;
Optional<StringView> m_webdriver_content_ipc_path;

Web::CSS::PreferredColorScheme m_preferred_color_scheme;
Expand Down Expand Up @@ -52,6 +53,7 @@ @implementation ApplicationDelegate
- (instancetype)init:(Vector<URL>)initial_urls
newTabPageURL:(URL)new_tab_page_url
withCookieJar:(WebView::CookieJar)cookie_jar
webContentOptions:(Ladybird::WebContentOptions const&)web_content_options
webdriverContentIPCPath:(StringView)webdriver_content_ipc_path
{
if (self = [super init]) {
Expand All @@ -75,6 +77,8 @@ - (instancetype)init:(Vector<URL>)initial_urls

m_cookie_jar = move(cookie_jar);

m_web_content_options = web_content_options;

if (!webdriver_content_ipc_path.is_empty()) {
m_webdriver_content_ipc_path = webdriver_content_ipc_path;
}
Expand Down Expand Up @@ -122,6 +126,11 @@ - (void)removeTab:(TabController*)controller
return *m_cookie_jar;
}

- (Ladybird::WebContentOptions const&)webContentOptions
{
return m_web_content_options;
}

- (Optional<StringView> const&)webdriverContentIPCPath
{
return m_webdriver_content_ipc_path;
Expand Down
2 changes: 1 addition & 1 deletion Ladybird/AppKit/UI/LadybirdWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ - (instancetype)init:(id<LadybirdWebViewObserver>)observer
// This returns device pixel ratio of the screen the window is opened in
auto device_pixel_ratio = [[NSScreen mainScreen] backingScaleFactor];

m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate webdriverContentIPCPath], [delegate preferredColorScheme]));
m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate webContentOptions], [delegate webdriverContentIPCPath], [delegate preferredColorScheme]));
[self setWebViewCallbacks];

auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds]
Expand Down
13 changes: 7 additions & 6 deletions Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ static T scale_for_device(T size, float device_pixel_ratio)
return size.template to_type<float>().scaled(device_pixel_ratio).template to_type<int>();
}

ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
ErrorOr<NonnullOwnPtr<WebViewBridge>> WebViewBridge::create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, WebContentOptions const& web_content_options, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
{
return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, move(webdriver_content_ipc_path), preferred_color_scheme));
return adopt_nonnull_own_or_enomem(new (nothrow) WebViewBridge(move(screen_rects), device_pixel_ratio, web_content_options, move(webdriver_content_ipc_path), preferred_color_scheme));
}

WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, WebContentOptions const& web_content_options, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme preferred_color_scheme)
: m_screen_rects(move(screen_rects))
, m_web_content_options(web_content_options)
, m_webdriver_content_ipc_path(move(webdriver_content_ipc_path))
, m_preferred_color_scheme(preferred_color_scheme)
{
m_device_pixel_ratio = device_pixel_ratio;

create_client(WebView::EnableCallgrindProfiling::No);
create_client();

on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
auto position = m_viewport_rect.location();
Expand Down Expand Up @@ -184,12 +185,12 @@ Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position)
return scale_for_device(content_position, inverse_device_pixel_ratio());
}

void WebViewBridge::create_client(WebView::EnableCallgrindProfiling enable_callgrind_profiling)
void WebViewBridge::create_client()
{
m_client_state = {};

auto candidate_web_content_paths = MUST(get_paths_for_helper_process("WebContent"sv));
auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, enable_callgrind_profiling, WebView::IsLayoutTestMode::No, Ladybird::UseLagomNetworking::Yes, WebView::EnableGPUPainting::No));
auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options));

m_client_state.client = new_client;
m_client_state.client->on_web_content_process_crash = [this] {
Expand Down
9 changes: 6 additions & 3 deletions Ladybird/AppKit/UI/LadybirdWebViewBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <AK/Vector.h>
#include <Ladybird/Types.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
Expand All @@ -22,7 +23,7 @@ namespace Ladybird {

class WebViewBridge final : public WebView::ViewImplementation {
public:
static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, WebContentOptions const&, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
virtual ~WebViewBridge() override;

float device_pixel_ratio() const { return m_device_pixel_ratio; }
Expand Down Expand Up @@ -59,19 +60,21 @@ class WebViewBridge final : public WebView::ViewImplementation {
Function<void(Gfx::IntPoint)> on_scroll;

private:
WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, WebContentOptions const&, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);

virtual void update_zoom() override;
virtual Gfx::IntRect viewport_rect() const override;
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override;
virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override;

virtual void create_client(WebView::EnableCallgrindProfiling) override;
virtual void create_client() override;

Vector<Gfx::IntRect> m_screen_rects;
Gfx::IntRect m_viewport_rect;

WebContentOptions m_web_content_options;
Optional<StringView> m_webdriver_content_ipc_path;

Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto };
};

Expand Down
6 changes: 6 additions & 0 deletions Ladybird/AppKit/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <BrowserSettings/Defaults.h>
#include <Ladybird/Types.h>
#include <Ladybird/Utilities.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
Expand Down Expand Up @@ -61,9 +62,14 @@
if (initial_urls.is_empty())
initial_urls.append(new_tab_page_url);

Ladybird::WebContentOptions web_content_options {
.use_lagom_networking = Ladybird::UseLagomNetworking::Yes,
};

auto* delegate = [[ApplicationDelegate alloc] init:move(initial_urls)
newTabPageURL:move(new_tab_page_url)
withCookieJar:move(cookie_jar)
webContentOptions:web_content_options
webdriverContentIPCPath:webdriver_content_ipc_path];

[NSApp setDelegate:delegate];
Expand Down
18 changes: 8 additions & 10 deletions Ladybird/HelperProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

#include "HelperProcess.h"

ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(WebView::ViewImplementation& view,
ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
WebView::ViewImplementation& view,
ReadonlySpan<String> candidate_web_content_paths,
WebView::EnableCallgrindProfiling enable_callgrind_profiling,
WebView::IsLayoutTestMode is_layout_test_mode,
Ladybird::UseLagomNetworking use_lagom_networking,
WebView::EnableGPUPainting enable_gpu_painting)
Ladybird::WebContentOptions const& web_content_options)
{
int socket_fds[2] {};
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
Expand Down Expand Up @@ -49,13 +47,13 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(Web
"--webcontent-fd-passing-socket"sv,
webcontent_fd_passing_socket_string
};
if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::No)
if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::No)
arguments.remove(0, callgrind_prefix_length);
if (is_layout_test_mode == WebView::IsLayoutTestMode::Yes)
if (web_content_options.is_layout_test_mode == Ladybird::IsLayoutTestMode::Yes)
arguments.append("--layout-test-mode"sv);
if (use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
if (web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
arguments.append("--use-lagom-networking"sv);
if (enable_gpu_painting == WebView::EnableGPUPainting::Yes)
if (web_content_options.enable_gpu_painting == Ladybird::EnableGPUPainting::Yes)
arguments.append("--use-gpu-painting"sv);

result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
Expand All @@ -77,7 +75,7 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(Web
auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(move(socket), view)));
new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));

if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::Yes) {
if (web_content_options.enable_callgrind_profiling == Ladybird::EnableCallgrindProfiling::Yes) {
dbgln();
dbgln("\033[1;45mLaunched WebContent process under callgrind!\033[0m");
dbgln("\033[100mRun `\033[4mcallgrind_control -i on\033[24m` to start instrumentation and `\033[4mcallgrind_control -i off\033[24m` stop it again.\033[0m");
Expand Down
8 changes: 3 additions & 5 deletions Ladybird/HelperProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebContentClient.h>

ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(WebView::ViewImplementation& view,
ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
WebView::ViewImplementation& view,
ReadonlySpan<String> candidate_web_content_paths,
WebView::EnableCallgrindProfiling,
WebView::IsLayoutTestMode,
Ladybird::UseLagomNetworking,
WebView::EnableGPUPainting);
Ladybird::WebContentOptions const&);

ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<String> candidate_image_decoder_paths);
ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<String> candidate_request_server_paths, StringView serenity_resource_root);
Expand Down
8 changes: 3 additions & 5 deletions Ladybird/Qt/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ static QIcon const& app_icon()
return icon;
}

BrowserWindow::BrowserWindow(Vector<URL> const& initial_urls, WebView::CookieJar& cookie_jar, StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling, UseLagomNetworking use_lagom_networking, WebView::EnableGPUPainting use_gpu_painting)
BrowserWindow::BrowserWindow(Vector<URL> const& initial_urls, WebView::CookieJar& cookie_jar, WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path)
: m_cookie_jar(cookie_jar)
, m_web_content_options(web_content_options)
, m_webdriver_content_ipc_path(webdriver_content_ipc_path)
, m_enable_callgrind_profiling(enable_callgrind_profiling)
, m_use_lagom_networking(use_lagom_networking)
, m_use_gpu_painting(use_gpu_painting)
{
setWindowIcon(app_icon());
m_tabs_container = new QTabWidget(this);
Expand Down Expand Up @@ -464,7 +462,7 @@ Tab& BrowserWindow::new_tab(StringView html, Web::HTML::ActivateTab activate_tab

Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab)
{
auto tab = make<Tab>(this, m_webdriver_content_ipc_path, m_enable_callgrind_profiling, m_use_lagom_networking, m_use_gpu_painting);
auto tab = make<Tab>(this, m_web_content_options, m_webdriver_content_ipc_path);
auto tab_ptr = tab.ptr();
m_tabs.append(std::move(tab));

Expand Down
8 changes: 4 additions & 4 deletions Ladybird/Qt/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include "Tab.h"
#include <Ladybird/Types.h>
#include <LibCore/Forward.h>
#include <LibWeb/HTML/ActivateTab.h>
#include <LibWebView/Forward.h>
Expand All @@ -24,8 +25,9 @@ class WebContentView;

class BrowserWindow : public QMainWindow {
Q_OBJECT

public:
explicit BrowserWindow(Vector<URL> const& initial_urls, WebView::CookieJar&, StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling, UseLagomNetworking, WebView::EnableGPUPainting);
BrowserWindow(Vector<URL> const& initial_urls, WebView::CookieJar&, WebContentOptions const&, StringView webdriver_content_ipc_path);

WebContentView& view() const { return m_current_tab->view(); }

Expand Down Expand Up @@ -118,10 +120,8 @@ public slots:

WebView::CookieJar& m_cookie_jar;

WebContentOptions m_web_content_options;
StringView m_webdriver_content_ipc_path;
WebView::EnableCallgrindProfiling m_enable_callgrind_profiling;
UseLagomNetworking m_use_lagom_networking;
WebView::EnableGPUPainting m_use_gpu_painting;
};

}
2 changes: 1 addition & 1 deletion Ladybird/Qt/ConsoleWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ConsoleWidget::ConsoleWidget(WebContentView& content_view)
{
setLayout(new QVBoxLayout);

m_output_view = new WebContentView({}, WebView::EnableCallgrindProfiling::No, UseLagomNetworking::No, WebView::EnableGPUPainting::No);
m_output_view = new WebContentView({}, {});
if (is_using_dark_system_theme(*this))
m_output_view->update_palette(WebContentView::PaletteMode::Dark);

Expand Down
2 changes: 1 addition & 1 deletion Ladybird/Qt/InspectorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern bool is_using_dark_system_theme(QWidget&);

InspectorWidget::InspectorWidget(WebContentView& content_view)
{
m_inspector_view = make<WebContentView>(StringView {}, WebView::EnableCallgrindProfiling::No, UseLagomNetworking::No, WebView::EnableGPUPainting::No);
m_inspector_view = make<WebContentView>(WebContentOptions {}, StringView {});

if (is_using_dark_system_theme(*this))
m_inspector_view->update_palette(WebContentView::PaletteMode::Dark);
Expand Down
4 changes: 2 additions & 2 deletions Ladybird/Qt/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ static QIcon create_tvg_icon_with_theme_colors(QString name, QPalette const& pal
return QIcon(icon_engine);
}

Tab::Tab(BrowserWindow* window, StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling, UseLagomNetworking use_lagom_networking, WebView::EnableGPUPainting enable_gpu_painting)
Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path)
: QWidget(window)
, m_window(window)
{
m_layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);
m_layout->setSpacing(0);
m_layout->setContentsMargins(0, 0, 0, 0);

m_view = new WebContentView(webdriver_content_ipc_path, enable_callgrind_profiling, use_lagom_networking, enable_gpu_painting);
m_view = new WebContentView(web_content_options, webdriver_content_ipc_path);
m_toolbar = new QToolBar(this);
m_location_edit = new LocationEdit(this);

Expand Down
3 changes: 2 additions & 1 deletion Ladybird/Qt/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class InspectorWidget;

class Tab final : public QWidget {
Q_OBJECT

public:
Tab(BrowserWindow* window, StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling, UseLagomNetworking, WebView::EnableGPUPainting);
Tab(BrowserWindow* window, WebContentOptions const&, StringView webdriver_content_ipc_path);
virtual ~Tab() override;

WebContentView& view() { return *m_view; }
Expand Down
11 changes: 5 additions & 6 deletions Ladybird/Qt/WebContentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ namespace Ladybird {

bool is_using_dark_system_theme(QWidget&);

WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling, UseLagomNetworking use_lagom_networking, WebView::EnableGPUPainting enable_gpu_painting)
: m_use_lagom_networking(use_lagom_networking)
, m_use_gpu_painting(enable_gpu_painting)
WebContentView::WebContentView(WebContentOptions const& web_content_options, StringView webdriver_content_ipc_path)
: m_web_content_options(web_content_options)
, m_webdriver_content_ipc_path(webdriver_content_ipc_path)
{
setMouseTracking(true);
Expand All @@ -76,7 +75,7 @@ WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::E
update_viewport_rect();
});

create_client(enable_callgrind_profiling);
create_client();

on_did_layout = [this](auto content_size) {
verticalScrollBar()->setMinimum(0);
Expand Down Expand Up @@ -600,12 +599,12 @@ void WebContentView::update_palette(PaletteMode mode)
client().async_update_system_theme(make_system_theme_from_qt_palette(*this, mode));
}

void WebContentView::create_client(WebView::EnableCallgrindProfiling enable_callgrind_profiling)
void WebContentView::create_client()
{
m_client_state = {};

auto candidate_web_content_paths = get_paths_for_helper_process("WebContent"sv).release_value_but_fixme_should_propagate_errors();
auto new_client = launch_web_content_process(*this, candidate_web_content_paths, enable_callgrind_profiling, WebView::IsLayoutTestMode::No, m_use_lagom_networking, m_use_gpu_painting).release_value_but_fixme_should_propagate_errors();
auto new_client = launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options).release_value_but_fixme_should_propagate_errors();

m_client_state.client = new_client;
m_client_state.client->on_web_content_process_crash = [this] {
Expand Down
Loading

0 comments on commit 07e9a8f

Please sign in to comment.