1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 14:47:34 +00:00

LibCore: Use Core::Process::spawn to start WebDriver processes

This commit is contained in:
MacDue 2023-03-24 21:58:22 +00:00 committed by Linus Groh
parent cd7459d608
commit 95b6e57bfb

View file

@ -12,6 +12,7 @@
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/Directory.h> #include <LibCore/Directory.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibCore/TCPServer.h> #include <LibCore/TCPServer.h>
@ -19,31 +20,16 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <WebDriver/Client.h> #include <WebDriver/Client.h>
#if defined(AK_OS_MACOS)
# include <crt_externs.h>
#endif
extern DeprecatedString s_serenity_resource_root; extern DeprecatedString s_serenity_resource_root;
static char** environment() static ErrorOr<pid_t> launch_process(StringView application, ReadonlySpan<char const*> arguments)
{
#if defined(AK_OS_MACOS)
return *_NSGetEnviron();
#else
extern char** environ;
return environ;
#endif
}
static ErrorOr<pid_t> launch_process(StringView application, char const* argv[])
{ {
auto paths = TRY(get_paths_for_helper_process(application)); auto paths = TRY(get_paths_for_helper_process(application));
ErrorOr<pid_t> result = -1; ErrorOr<pid_t> result = -1;
for (auto const& path : paths) { for (auto const& path : paths) {
auto path_view = path.bytes_as_string_view(); auto path_view = path.bytes_as_string_view();
argv[0] = path_view.characters_without_null_termination(); result = Core::Process::spawn(path_view, arguments, {}, Core::Process::KeepAsChild::Yes);
result = Core::System::posix_spawn(path_view, nullptr, nullptr, const_cast<char**>(argv), environment());
if (!result.is_error()) if (!result.is_error())
break; break;
} }
@ -52,31 +38,24 @@ static ErrorOr<pid_t> launch_process(StringView application, char const* argv[])
static ErrorOr<pid_t> launch_browser(DeprecatedString const& socket_path) static ErrorOr<pid_t> launch_browser(DeprecatedString const& socket_path)
{ {
char const* argv[] = { return launch_process("ladybird"sv,
"ladybird", Array {
"--webdriver-content-path", "--webdriver-content-path",
socket_path.characters(), socket_path.characters(),
nullptr, });
};
return launch_process("ladybird"sv, argv);
} }
static ErrorOr<pid_t> launch_headless_browser(DeprecatedString const& socket_path) static ErrorOr<pid_t> launch_headless_browser(DeprecatedString const& socket_path)
{ {
auto resources = DeprecatedString::formatted("{}/res", s_serenity_resource_root); auto resources = DeprecatedString::formatted("{}/res", s_serenity_resource_root);
return launch_process("headless-browser"sv,
char const* argv[] = { Array {
"headless-browser", "--resources",
"--resources", resources.characters(),
resources.characters(), "--webdriver-ipc-path",
"--webdriver-ipc-path", socket_path.characters(),
socket_path.characters(), "about:blank",
"about:blank", });
nullptr,
};
return launch_process("headless-browser"sv, argv);
} }
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)