Procházet zdrojové kódy

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.
Andrew Kaster před 1 rokem
rodič
revize
2bb0f65309
2 změnil soubory, kde provedl 40 přidání a 15 odebrání
  1. 1 1
      Ladybird/Qt/BrowserWindow.h
  2. 39 14
      Ladybird/Qt/main.cpp

+ 1 - 1
Ladybird/Qt/BrowserWindow.h

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

+ 39 - 14
Ladybird/Qt/main.cpp

@@ -17,6 +17,7 @@
 #include <LibCore/System.h>
 #include <LibCore/System.h>
 #include <LibGfx/Font/FontDatabase.h>
 #include <LibGfx/Font/FontDatabase.h>
 #include <LibMain/Main.h>
 #include <LibMain/Main.h>
+#include <LibWebView/ChromeProcess.h>
 #include <LibWebView/CookieJar.h>
 #include <LibWebView/CookieJar.h>
 #include <LibWebView/Database.h>
 #include <LibWebView/Database.h>
 #include <LibWebView/ProcessManager.h>
 #include <LibWebView/ProcessManager.h>
@@ -58,6 +59,21 @@ static ErrorOr<void> handle_attached_debugger()
     return {};
     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)
 ErrorOr<int> serenity_main(Main::Arguments arguments)
 {
 {
     AK::set_rich_debug_enabled(true);
     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_default_font_query("Katica 10 400 0");
     Gfx::FontDatabase::set_fixed_width_font_query("Csilla 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;
     StringView webdriver_content_ipc_path;
     Vector<ByteString> certificates;
     Vector<ByteString> certificates;
     bool enable_callgrind_profiling = false;
     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.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
     args_parser.parse(arguments);
     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();
     WebView::ProcessManager::initialize();
 
 
 #if defined(AK_OS_MACOS)
 #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();
     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
     // 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
     // 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));
     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,
         .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");
     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) {
     app.on_open_file = [&](auto file_url) {
         window.view().load(file_url);
         window.view().load(file_url);
     };
     };