From 3e6d790cf07024f6ed0e5246e0c2b091fed65638 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Thu, 2 Feb 2023 03:00:30 -0700 Subject: [PATCH] Ladybird: Abstract spawning helper processes into separate methods This will let us use the same path discovery methods for WebContent, SQLServer, and any other helper processes we need to launch. --- Ladybird/CMakeLists.txt | 1 + Ladybird/HelperProcess.cpp | 35 +++++++++++++++++++++++++++++++++++ Ladybird/HelperProcess.h | 18 ++++++++++++++++++ Ladybird/WebContentView.cpp | 8 ++------ 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 Ladybird/HelperProcess.cpp create mode 100644 Ladybird/HelperProcess.h diff --git a/Ladybird/CMakeLists.txt b/Ladybird/CMakeLists.txt index 43daac54f1..bdff1b7a9d 100644 --- a/Ladybird/CMakeLists.txt +++ b/Ladybird/CMakeLists.txt @@ -82,6 +82,7 @@ set(SOURCES ${BROWSER_SOURCE_DIR}/History.cpp BrowserWindow.cpp ConsoleWidget.cpp + HelperProcess.cpp InspectorWidget.cpp LocationEdit.cpp ModelTranslator.cpp diff --git a/Ladybird/HelperProcess.cpp b/Ladybird/HelperProcess.cpp new file mode 100644 index 0000000000..52a0d0ca68 --- /dev/null +++ b/Ladybird/HelperProcess.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "HelperProcess.h" +#include "Utilities.h" +#include +#include + +ErrorOr spawn_helper_process(StringView process_name, Span arguments, Core::System::SearchInPath search_in_path, Optional> environment) +{ + auto paths = TRY(get_paths_for_helper_process(process_name)); + VERIFY(!paths.is_empty()); + ErrorOr result; + for (auto const& path : paths) { + arguments[0] = path.bytes_as_string_view(); + result = Core::System::exec(path, arguments, search_in_path, environment); + if (!result.is_error()) + break; + } + + return result; +} + +ErrorOr> get_paths_for_helper_process(StringView process_name) +{ + Vector paths; + TRY(paths.try_append(TRY(String::formatted("./{}/{}", process_name, process_name)))); + TRY(paths.try_append(TRY(String::formatted("{}/{}", TRY(ak_string_from_qstring(QCoreApplication::applicationDirPath())), process_name)))); + TRY(paths.try_append(TRY(String::formatted("./{}", process_name)))); + // NOTE: Add platform-specific paths here + return paths; +} diff --git a/Ladybird/HelperProcess.h b/Ladybird/HelperProcess.h new file mode 100644 index 0000000000..18d530a321 --- /dev/null +++ b/Ladybird/HelperProcess.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#define AK_DONT_REPLACE_STD + +#include +#include +#include +#include +#include + +ErrorOr spawn_helper_process(StringView process_name, Span arguments, Core::System::SearchInPath, Optional> environment = {}); +ErrorOr> get_paths_for_helper_process(StringView process_name); diff --git a/Ladybird/WebContentView.cpp b/Ladybird/WebContentView.cpp index 1c2af12c2b..71666bf510 100644 --- a/Ladybird/WebContentView.cpp +++ b/Ladybird/WebContentView.cpp @@ -9,6 +9,7 @@ #include "WebContentView.h" #include "ConsoleWidget.h" +#include "HelperProcess.h" #include "InspectorWidget.h" #include "Utilities.h" #include @@ -586,12 +587,7 @@ void WebContentView::create_client() arguments.append(m_webdriver_content_ipc_path); } - auto result = Core::System::exec("./WebContent/WebContent"sv, arguments, Core::System::SearchInPath::Yes); - if (result.is_error()) { - auto web_content_path = ak_deprecated_string_from_qstring(QCoreApplication::applicationDirPath() + "/WebContent"); - result = Core::System::exec(web_content_path, arguments, Core::System::SearchInPath::Yes); - } - + auto result = spawn_helper_process("WebContent"sv, arguments, Core::System::SearchInPath::Yes); if (result.is_error()) warnln("Could not launch WebContent: {}", result.error()); VERIFY_NOT_REACHED();