Ladybird: Add singleton process logic for Qt chrome

This only adds the new tab behavior, as handling multiple windows in
the same process needs some extra help.
This commit is contained in:
Andrew Kaster 2024-04-26 15:23:20 -06:00 committed by Tim Flynn
parent bc976fe7e1
commit 2bb0f65309
Notes: sideshowbarker 2024-07-17 07:43:05 +09:00
2 changed files with 40 additions and 15 deletions

View file

@ -35,6 +35,7 @@ public:
WebContentView& view() const { return m_current_tab->view(); }
int tab_index(Tab*);
Tab& create_new_tab(Web::HTML::ActivateTab activate_tab);
QAction& go_back_action()
{
@ -117,7 +118,6 @@ private:
virtual void wheelEvent(QWheelEvent*) override;
virtual void closeEvent(QCloseEvent*) override;
Tab& create_new_tab(Web::HTML::ActivateTab activate_tab);
Tab& create_new_tab(Web::HTML::ActivateTab, Tab& parent, Web::HTML::WebViewHints, Optional<u64> page_index);
void initialize_tab(Tab*);

View file

@ -17,6 +17,7 @@
#include <LibCore/System.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibMain/Main.h>
#include <LibWebView/ChromeProcess.h>
#include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h>
#include <LibWebView/ProcessManager.h>
@ -58,6 +59,21 @@ static ErrorOr<void> handle_attached_debugger()
return {};
}
static Vector<URL::URL> sanitize_urls(Vector<ByteString> const& raw_urls)
{
Vector<URL::URL> sanitized_urls;
for (auto const& raw_url : raw_urls) {
if (auto url = WebView::sanitize_url(raw_url); url.has_value())
sanitized_urls.append(url.release_value());
}
if (sanitized_urls.is_empty()) {
auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
sanitized_urls.append(ak_string_from_qstring(new_tab_page));
}
return sanitized_urls;
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
@ -76,7 +92,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
Vector<StringView> raw_urls;
Vector<ByteString> raw_urls;
StringView webdriver_content_ipc_path;
Vector<ByteString> certificates;
bool enable_callgrind_profiling = false;
@ -103,6 +119,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
args_parser.parse(arguments);
WebView::ChromeProcess chrome_process;
auto new_window = false;
if (TRY(chrome_process.connect(raw_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
outln("Opening in existing process");
return 0;
}
chrome_process.on_new_window = [](auto const& urls) {
dbgln("asked to open new window with urls: {}", urls);
};
WebView::ProcessManager::initialize();
#if defined(AK_OS_MACOS)
@ -124,18 +151,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
Vector<URL::URL> initial_urls;
for (auto const& raw_url : raw_urls) {
if (auto url = WebView::sanitize_url(raw_url); url.has_value())
initial_urls.append(url.release_value());
}
if (initial_urls.is_empty()) {
auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
initial_urls.append(ak_string_from_qstring(new_tab_page));
}
// NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
@ -156,9 +171,19 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
.expose_internals_object = expose_internals_object ? Ladybird::ExposeInternalsObject::Yes : Ladybird::ExposeInternalsObject::No,
};
Ladybird::BrowserWindow window(initial_urls, cookie_jar, web_content_options, webdriver_content_ipc_path);
Ladybird::BrowserWindow window(sanitize_urls(raw_urls), cookie_jar, web_content_options, webdriver_content_ipc_path);
window.setWindowTitle("Ladybird");
chrome_process.on_new_tab = [&](auto const& raw_urls) {
auto urls = sanitize_urls(raw_urls);
for (size_t i = 0; i < urls.size(); ++i) {
window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
}
window.show();
window.activateWindow();
window.raise();
};
app.on_open_file = [&](auto file_url) {
window.view().load(file_url);
};