LibWebView+UI: Move the database and cookie jar to WebView::Application
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-22.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-14, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-22.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-22.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-22.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run

The main motivator here was noticing that --disable-sql-database did not
work with AppKit. Rather than re-implementing this there, move ownership
of these classes to WebView::Application, so that each UI does not need
to individually worry about it.
This commit is contained in:
Timothy Flynn 2024-09-05 18:19:51 -04:00 committed by Tim Flynn
parent 60f30aad72
commit ed3c450359
Notes: github-actions[bot] 2024-09-05 23:46:39 +00:00
12 changed files with 55 additions and 80 deletions

View file

@ -13,7 +13,6 @@
#include <LibWeb/CSS/PreferredContrast.h>
#include <LibWeb/CSS/PreferredMotion.h>
#include <LibWeb/HTML/ActivateTab.h>
#include <LibWebView/CookieJar.h>
#import <Cocoa/Cocoa.h>
@ -22,7 +21,7 @@
@interface ApplicationDelegate : NSObject <NSApplicationDelegate>
- (nullable instancetype)initWithCookieJar:(NonnullOwnPtr<WebView::CookieJar>)cookie_jar;
- (nullable instancetype)init;
- (nonnull TabController*)createNewTab:(Optional<URL::URL> const&)url
fromTab:(nullable Tab*)tab
@ -38,7 +37,6 @@
- (void)removeTab:(nonnull TabController*)controller;
- (WebView::CookieJar&)cookieJar;
- (Web::CSS::PreferredColorScheme)preferredColorScheme;
- (Web::CSS::PreferredContrast)preferredContrast;
- (Web::CSS::PreferredMotion)preferredMotion;

View file

@ -5,6 +5,7 @@
*/
#include <LibWebView/Application.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/SearchEngine.h>
#import <Application/ApplicationDelegate.h>
@ -30,9 +31,6 @@
@interface ApplicationDelegate () <TaskManagerDelegate>
{
// This will always be populated, but we cannot have a non-default constructible instance variable.
OwnPtr<WebView::CookieJar> m_cookie_jar;
Web::CSS::PreferredColorScheme m_preferred_color_scheme;
Web::CSS::PreferredContrast m_preferred_contrast;
Web::CSS::PreferredMotion m_preferred_motion;
@ -61,7 +59,7 @@
@implementation ApplicationDelegate
- (instancetype)initWithCookieJar:(NonnullOwnPtr<WebView::CookieJar>)cookie_jar
- (instancetype)init
{
if (self = [super init]) {
[NSApp setMainMenu:[[NSMenu alloc] init]];
@ -79,8 +77,6 @@
self.managed_tabs = [[NSMutableArray alloc] init];
m_cookie_jar = move(cookie_jar);
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Auto;
m_preferred_contrast = Web::CSS::PreferredContrast::Auto;
m_preferred_motion = Web::CSS::PreferredMotion::Auto;
@ -138,11 +134,6 @@
}
}
- (WebView::CookieJar&)cookieJar
{
return *m_cookie_jar;
}
- (Web::CSS::PreferredColorScheme)preferredColorScheme
{
return m_preferred_color_scheme;
@ -333,7 +324,7 @@
- (void)dumpCookies:(id)sender
{
m_cookie_jar->dump_cookies();
WebView::Application::cookie_jar().dump_cookies();
}
- (NSMenuItem*)createApplicationMenu

View file

@ -10,6 +10,8 @@
#include <LibGfx/ShareableBitmap.h>
#include <LibURL/URL.h>
#include <LibWeb/HTML/SelectedFile.h>
#include <LibWebView/Application.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/SourceHighlighter.h>
#include <LibWebView/URL.h>
@ -973,28 +975,23 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
};
m_web_view_bridge->on_get_all_cookies = [](auto const& url) {
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
return [delegate cookieJar].get_all_cookies(url);
return WebView::Application::cookie_jar().get_all_cookies(url);
};
m_web_view_bridge->on_get_named_cookie = [](auto const& url, auto const& name) {
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
return [delegate cookieJar].get_named_cookie(url, name);
return WebView::Application::cookie_jar().get_named_cookie(url, name);
};
m_web_view_bridge->on_get_cookie = [](auto const& url, auto source) {
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
return [delegate cookieJar].get_cookie(url, source);
return WebView::Application::cookie_jar().get_cookie(url, source);
};
m_web_view_bridge->on_set_cookie = [](auto const& url, auto const& cookie, auto source) {
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
[delegate cookieJar].set_cookie(url, cookie, source);
WebView::Application::cookie_jar().set_cookie(url, cookie, source);
};
m_web_view_bridge->on_update_cookie = [](auto const& cookie) {
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
[delegate cookieJar].update_cookie(cookie);
WebView::Application::cookie_jar().update_cookie(cookie);
};
m_web_view_bridge->on_restore_window = [weak_self]() {

View file

@ -12,8 +12,6 @@
#include <LibMain/Main.h>
#include <LibWebView/Application.h>
#include <LibWebView/ChromeProcess.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h>
#include <LibWebView/URL.h>
#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebContentClient.h>
@ -84,15 +82,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
view->did_allocate_iosurface_backing_stores(message.front_backing_store_id, move(message.front_backing_store_port), message.back_backing_store_id, move(message.back_backing_store_port));
};
auto database = TRY(WebView::Database::create());
auto cookie_jar = TRY(WebView::CookieJar::create(*database));
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
TRY([application launchRequestServer]);
TRY([application launchImageDecoder]);
auto* delegate = [[ApplicationDelegate alloc] initWithCookieJar:move(cookie_jar)];
auto* delegate = [[ApplicationDelegate alloc] init];
[NSApp setDelegate:delegate];
return WebView::Application::the().execute();

View file

@ -110,9 +110,9 @@ void Application::close_task_manager_window()
}
}
BrowserWindow& Application::new_window(Vector<URL::URL> const& initial_urls, WebView::CookieJar& cookie_jar, BrowserWindow::IsPopupWindow is_popup_window, Tab* parent_tab, Optional<u64> page_index)
BrowserWindow& Application::new_window(Vector<URL::URL> const& initial_urls, BrowserWindow::IsPopupWindow is_popup_window, Tab* parent_tab, Optional<u64> page_index)
{
auto* window = new BrowserWindow(initial_urls, cookie_jar, is_popup_window, parent_tab, move(page_index));
auto* window = new BrowserWindow(initial_urls, is_popup_window, parent_tab, move(page_index));
set_active_window(*window);
window->show();
if (initial_urls.is_empty()) {

View file

@ -34,7 +34,7 @@ public:
NonnullRefPtr<ImageDecoderClient::Client> image_decoder_client() const { return *m_image_decoder_client; }
ErrorOr<void> initialize_image_decoder();
BrowserWindow& new_window(Vector<URL::URL> const& initial_urls, WebView::CookieJar&, BrowserWindow::IsPopupWindow is_popup_window = BrowserWindow::IsPopupWindow::No, Tab* parent_tab = nullptr, Optional<u64> page_index = {});
BrowserWindow& new_window(Vector<URL::URL> const& initial_urls, BrowserWindow::IsPopupWindow is_popup_window = BrowserWindow::IsPopupWindow::No, Tab* parent_tab = nullptr, Optional<u64> page_index = {});
void show_task_manager_window();
void close_task_manager_window();

View file

@ -72,10 +72,9 @@ public:
}
};
BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::CookieJar& cookie_jar, IsPopupWindow is_popup_window, Tab* parent_tab, Optional<u64> page_index)
BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window, Tab* parent_tab, Optional<u64> page_index)
: m_tabs_container(new TabWidget(this))
, m_new_tab_button_toolbar(new QToolBar("New Tab", m_tabs_container))
, m_cookie_jar(cookie_jar)
, m_is_popup_window(is_popup_window)
{
setWindowIcon(app_icon());
@ -421,8 +420,8 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::Cook
auto* dump_cookies_action = new QAction("Dump C&ookies", this);
dump_cookies_action->setIcon(load_icon_from_uri("resource://icons/browser/cookie.png"sv));
debug_menu->addAction(dump_cookies_action);
QObject::connect(dump_cookies_action, &QAction::triggered, this, [this] {
m_cookie_jar.dump_cookies();
QObject::connect(dump_cookies_action, &QAction::triggered, this, [] {
WebView::Application::cookie_jar().dump_cookies();
});
auto* dump_local_storage_action = new QAction("Dump Loc&al Storage", this);
@ -605,8 +604,8 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::Cook
tab.set_url_is_hidden(true);
tab.focus_location_editor();
});
QObject::connect(m_new_window_action, &QAction::triggered, this, [this] {
(void)static_cast<Ladybird::Application*>(QApplication::instance())->new_window({}, m_cookie_jar);
QObject::connect(m_new_window_action, &QAction::triggered, this, [] {
(void)static_cast<Ladybird::Application*>(QApplication::instance())->new_window({});
});
QObject::connect(open_file_action, &QAction::triggered, this, &BrowserWindow::open_file);
QObject::connect(settings_action, &QAction::triggered, this, [this] {
@ -778,7 +777,7 @@ void BrowserWindow::initialize_tab(Tab* tab)
tab->view().on_new_web_view = [this, tab](auto activate_tab, Web::HTML::WebViewHints hints, Optional<u64> page_index) {
if (hints.popup) {
auto& window = static_cast<Ladybird::Application*>(QApplication::instance())->new_window({}, m_cookie_jar, IsPopupWindow::Yes, tab, AK::move(page_index));
auto& window = static_cast<Ladybird::Application*>(QApplication::instance())->new_window({}, IsPopupWindow::Yes, tab, AK::move(page_index));
window.set_window_rect(hints.screen_x, hints.screen_y, hints.width, hints.height);
return window.current_tab()->view().handle();
}
@ -807,24 +806,24 @@ void BrowserWindow::initialize_tab(Tab* tab)
(void)modifiers;
};
tab->view().on_get_all_cookies = [this](auto const& url) {
return m_cookie_jar.get_all_cookies(url);
tab->view().on_get_all_cookies = [](auto const& url) {
return WebView::Application::cookie_jar().get_all_cookies(url);
};
tab->view().on_get_named_cookie = [this](auto const& url, auto const& name) {
return m_cookie_jar.get_named_cookie(url, name);
tab->view().on_get_named_cookie = [](auto const& url, auto const& name) {
return WebView::Application::cookie_jar().get_named_cookie(url, name);
};
tab->view().on_get_cookie = [this](auto& url, auto source) {
return m_cookie_jar.get_cookie(url, source);
tab->view().on_get_cookie = [](auto& url, auto source) {
return WebView::Application::cookie_jar().get_cookie(url, source);
};
tab->view().on_set_cookie = [this](auto& url, auto& cookie, auto source) {
m_cookie_jar.set_cookie(url, cookie, source);
tab->view().on_set_cookie = [](auto& url, auto& cookie, auto source) {
WebView::Application::cookie_jar().set_cookie(url, cookie, source);
};
tab->view().on_update_cookie = [this](auto const& cookie) {
m_cookie_jar.update_cookie(cookie);
tab->view().on_update_cookie = [](auto const& cookie) {
WebView::Application::cookie_jar().update_cookie(cookie);
};
m_tabs_container->setTabIcon(m_tabs_container->indexOf(tab), tab->favicon());

View file

@ -35,7 +35,7 @@ public:
Yes,
};
BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::CookieJar&, IsPopupWindow is_popup_window = IsPopupWindow::No, Tab* parent_tab = nullptr, Optional<u64> page_index = {});
BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window = IsPopupWindow::No, Tab* parent_tab = nullptr, Optional<u64> page_index = {});
WebContentView& view() const { return m_current_tab->view(); }
@ -214,8 +214,6 @@ private:
SettingsDialog* m_settings_dialog { nullptr };
WebView::CookieJar& m_cookie_jar;
IsPopupWindow m_is_popup_window { IsPopupWindow::No };
};

View file

@ -19,8 +19,6 @@
#include <LibMain/Main.h>
#include <LibWebView/Application.h>
#include <LibWebView/ChromeProcess.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h>
#include <LibWebView/ProcessManager.h>
#include <LibWebView/URL.h>
@ -115,12 +113,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
copy_default_config_files(Ladybird::Settings::the()->directory());
RefPtr<WebView::Database> database;
if (app->chrome_options().disable_sql_database == WebView::DisableSQLDatabase::No)
database = TRY(WebView::Database::create());
auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
if (app->web_content_options().use_lagom_networking == WebView::UseLagomNetworking::Yes) {
auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
@ -131,10 +123,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(app->initialize_image_decoder());
chrome_process.on_new_window = [&](auto const& urls) {
app->new_window(urls, *cookie_jar);
app->new_window(urls);
};
auto& window = app->new_window(app->chrome_options().urls, *cookie_jar);
auto& window = app->new_window(app->chrome_options().urls);
window.setWindowTitle("Ladybird");
if (Ladybird::Settings::the()->is_maximized()) {

View file

@ -12,6 +12,8 @@
#include <LibFileSystem/FileSystem.h>
#include <LibImageDecoderClient/Client.h>
#include <LibWebView/Application.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h>
#include <LibWebView/URL.h>
#include <LibWebView/UserAgent.h>
#include <LibWebView/WebContentClient.h>
@ -138,6 +140,13 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_
};
create_platform_options(m_chrome_options, m_web_content_options);
if (m_chrome_options.disable_sql_database == DisableSQLDatabase::No) {
m_database = Database::create().release_value_but_fixme_should_propagate_errors();
m_cookie_jar = CookieJar::create(*m_database).release_value_but_fixme_should_propagate_errors();
} else {
m_cookie_jar = CookieJar::create();
}
}
int Application::execute()

View file

@ -34,6 +34,8 @@ public:
static ChromeOptions const& chrome_options() { return the().m_chrome_options; }
static WebContentOptions const& web_content_options() { return the().m_web_content_options; }
static CookieJar& cookie_jar() { return *the().m_cookie_jar; }
Core::EventLoop& event_loop() { return m_event_loop; }
void add_child_process(Process&&);
@ -77,6 +79,9 @@ private:
ChromeOptions m_chrome_options;
WebContentOptions m_web_content_options;
RefPtr<Database> m_database;
OwnPtr<CookieJar> m_cookie_jar;
OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
Core::EventLoop m_event_loop;

View file

@ -52,7 +52,6 @@
#include <LibWeb/Worker/WebWorkerClient.h>
#include <LibWebView/Application.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h>
#include <LibWebView/URL.h>
#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebContentClient.h>
@ -74,10 +73,7 @@ public:
auto image_decoder_paths = TRY(get_paths_for_helper_process("ImageDecoder"sv));
image_decoder_client = TRY(launch_image_decoder_process(image_decoder_paths));
auto database = TRY(WebView::Database::create());
auto cookie_jar = TRY(WebView::CookieJar::create(*database));
auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView(move(database), move(cookie_jar), image_decoder_client, request_client)));
auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView(image_decoder_client, request_client)));
auto request_server_socket = TRY(connect_new_request_server_client(*request_client));
auto image_decoder_socket = TRY(connect_new_image_decoder_client(*image_decoder_client));
@ -145,18 +141,16 @@ public:
}
private:
HeadlessWebContentView(NonnullRefPtr<WebView::Database> database, NonnullOwnPtr<WebView::CookieJar> cookie_jar, RefPtr<ImageDecoderClient::Client> image_decoder_client, RefPtr<Requests::RequestClient> request_client)
: m_database(move(database))
, m_cookie_jar(move(cookie_jar))
, m_request_client(move(request_client))
HeadlessWebContentView(RefPtr<ImageDecoderClient::Client> image_decoder_client, RefPtr<Requests::RequestClient> request_client)
: m_request_client(move(request_client))
, m_image_decoder_client(move(image_decoder_client))
{
on_get_cookie = [this](auto const& url, auto source) {
return m_cookie_jar->get_cookie(url, source);
on_get_cookie = [](auto const& url, auto source) {
return WebView::Application::cookie_jar().get_cookie(url, source);
};
on_set_cookie = [this](auto const& url, auto const& cookie, auto source) {
m_cookie_jar->set_cookie(url, cookie, source);
on_set_cookie = [](auto const& url, auto const& cookie, auto source) {
WebView::Application::cookie_jar().set_cookie(url, cookie, source);
};
on_request_worker_agent = [this]() {
@ -172,12 +166,9 @@ private:
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override { return widget_position; }
virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override { return content_position; }
private:
Gfx::IntSize m_viewport_size;
RefPtr<Core::Promise<RefPtr<Gfx::Bitmap>>> m_pending_screenshot;
NonnullRefPtr<WebView::Database> m_database;
NonnullOwnPtr<WebView::CookieJar> m_cookie_jar;
RefPtr<Requests::RequestClient> m_request_client;
RefPtr<ImageDecoderClient::Client> m_image_decoder_client;
};