2024-06-30 04:24:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
|
|
|
#include <LibCore/Process.h>
|
|
|
|
#include <LibIPC/Connection.h>
|
2024-10-22 21:47:33 +00:00
|
|
|
#include <LibIPC/Transport.h>
|
2024-06-30 04:24:01 +00:00
|
|
|
#include <LibWebView/ProcessType.h>
|
|
|
|
|
|
|
|
namespace WebView {
|
|
|
|
|
|
|
|
class Process {
|
|
|
|
AK_MAKE_NONCOPYABLE(Process);
|
|
|
|
AK_MAKE_DEFAULT_MOVABLE(Process);
|
|
|
|
|
|
|
|
public:
|
|
|
|
Process(ProcessType type, RefPtr<IPC::ConnectionBase> connection, Core::Process process);
|
|
|
|
~Process();
|
|
|
|
|
2024-10-22 19:27:15 +00:00
|
|
|
template<typename ClientType>
|
|
|
|
struct ProcessAndClient;
|
|
|
|
|
|
|
|
template<typename ClientType, typename... ClientArguments>
|
|
|
|
static ErrorOr<ProcessAndClient<ClientType>> spawn(ProcessType type, Core::ProcessSpawnOptions const& options, ClientArguments&&... client_arguments);
|
|
|
|
|
2024-06-30 04:24:01 +00:00
|
|
|
ProcessType type() const { return m_type; }
|
|
|
|
Optional<String> const& title() const { return m_title; }
|
|
|
|
void set_title(Optional<String> title) { m_title = move(title); }
|
|
|
|
|
|
|
|
template<typename ConnectionFromClient>
|
|
|
|
Optional<ConnectionFromClient&> client()
|
|
|
|
{
|
|
|
|
if (auto strong_connection = m_connection.strong_ref())
|
|
|
|
return verify_cast<ConnectionFromClient>(*strong_connection);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid() const { return m_process.pid(); }
|
|
|
|
|
2024-10-22 19:27:15 +00:00
|
|
|
struct ProcessPaths {
|
|
|
|
ByteString socket_path;
|
|
|
|
ByteString pid_path;
|
|
|
|
};
|
|
|
|
static ErrorOr<ProcessPaths> paths_for_process(StringView process_name);
|
|
|
|
static ErrorOr<Optional<pid_t>> get_process_pid(StringView process_name, StringView pid_path);
|
|
|
|
static ErrorOr<int> create_ipc_socket(ByteString const& socket_path);
|
|
|
|
|
2024-06-30 04:24:01 +00:00
|
|
|
private:
|
2024-10-22 21:47:33 +00:00
|
|
|
struct ProcessAndIPCTransport {
|
2024-10-22 19:27:15 +00:00
|
|
|
Core::Process process;
|
2024-10-22 21:47:33 +00:00
|
|
|
IPC::Transport transport;
|
2024-10-22 19:27:15 +00:00
|
|
|
};
|
2024-10-22 21:47:33 +00:00
|
|
|
static ErrorOr<ProcessAndIPCTransport> spawn_and_connect_to_process(Core::ProcessSpawnOptions const& options);
|
2024-10-22 19:27:15 +00:00
|
|
|
|
2024-06-30 04:24:01 +00:00
|
|
|
Core::Process m_process;
|
|
|
|
ProcessType m_type;
|
|
|
|
Optional<String> m_title;
|
|
|
|
WeakPtr<IPC::ConnectionBase> m_connection;
|
|
|
|
};
|
|
|
|
|
2024-10-22 19:27:15 +00:00
|
|
|
template<typename ClientType>
|
|
|
|
struct Process::ProcessAndClient {
|
|
|
|
Process process;
|
|
|
|
NonnullRefPtr<ClientType> client;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ClientType, typename... ClientArguments>
|
|
|
|
ErrorOr<Process::ProcessAndClient<ClientType>> Process::spawn(ProcessType type, Core::ProcessSpawnOptions const& options, ClientArguments&&... client_arguments)
|
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
auto [core_process, transport] = TRY(spawn_and_connect_to_process(options));
|
|
|
|
auto client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ClientType { move(transport), forward<ClientArguments>(client_arguments)... }));
|
2024-10-22 19:27:15 +00:00
|
|
|
|
2024-10-27 03:38:30 +00:00
|
|
|
return ProcessAndClient<ClientType> { Process { type, client, move(core_process) }, client };
|
2024-10-22 19:27:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 04:24:01 +00:00
|
|
|
}
|