ChromeProcess.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteString.h>
  7. #include <LibCore/Process.h>
  8. #include <LibCore/StandardPaths.h>
  9. #include <LibIPC/ConnectionToServer.h>
  10. #include <LibWebView/ChromeProcess.h>
  11. namespace WebView {
  12. static HashMap<int, RefPtr<UIProcessConnectionFromClient>> s_connections;
  13. class UIProcessClient final
  14. : public IPC::ConnectionToServer<UIProcessClientEndpoint, UIProcessServerEndpoint> {
  15. C_OBJECT(UIProcessClient);
  16. private:
  17. UIProcessClient(NonnullOwnPtr<Core::LocalSocket>);
  18. };
  19. ErrorOr<ChromeProcess::ProcessDisposition> ChromeProcess::connect(Vector<ByteString> const& raw_urls, bool new_window)
  20. {
  21. static constexpr auto process_name = "Ladybird"sv;
  22. auto [socket_path, pid_path] = TRY(Core::IPCProcess::paths_for_process(process_name));
  23. if (auto pid = TRY(Core::IPCProcess::get_process_pid(process_name, pid_path)); pid.has_value()) {
  24. TRY(connect_as_client(socket_path, raw_urls, new_window));
  25. return ProcessDisposition::ExitProcess;
  26. }
  27. TRY(connect_as_server(socket_path));
  28. m_pid_path = pid_path;
  29. m_pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write));
  30. TRY(m_pid_file->write_until_depleted(ByteString::number(::getpid())));
  31. return ProcessDisposition::ContinueMainProcess;
  32. }
  33. ErrorOr<void> ChromeProcess::connect_as_client(ByteString const& socket_path, Vector<ByteString> const& raw_urls, bool new_window)
  34. {
  35. auto socket = TRY(Core::LocalSocket::connect(socket_path));
  36. auto client = UIProcessClient::construct(move(socket));
  37. if (new_window) {
  38. if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewWindow>(raw_urls))
  39. dbgln("Failed to send CreateNewWindow message to UIProcess");
  40. } else {
  41. if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewTab>(raw_urls))
  42. dbgln("Failed to send CreateNewTab message to UIProcess");
  43. }
  44. return {};
  45. }
  46. ErrorOr<void> ChromeProcess::connect_as_server(ByteString const& socket_path)
  47. {
  48. auto socket_fd = TRY(Core::IPCProcess::create_ipc_socket(socket_path));
  49. m_socket_path = socket_path;
  50. auto local_server = TRY(Core::LocalServer::try_create());
  51. TRY(local_server->take_over_fd(socket_fd));
  52. m_server_connection = TRY(IPC::MultiServer<UIProcessConnectionFromClient>::try_create(move(local_server)));
  53. m_server_connection->on_new_client = [this](auto& client) {
  54. client.on_new_tab = [this](auto raw_urls) {
  55. if (this->on_new_tab)
  56. this->on_new_tab(raw_urls);
  57. };
  58. client.on_new_window = [this](auto raw_urls) {
  59. if (this->on_new_window)
  60. this->on_new_window(raw_urls);
  61. };
  62. };
  63. return {};
  64. }
  65. ChromeProcess::~ChromeProcess()
  66. {
  67. if (m_pid_file) {
  68. MUST(m_pid_file->truncate(0));
  69. MUST(Core::System::unlink(m_pid_path));
  70. }
  71. if (!m_socket_path.is_empty())
  72. MUST(Core::System::unlink(m_socket_path));
  73. }
  74. UIProcessClient::UIProcessClient(NonnullOwnPtr<Core::LocalSocket> socket)
  75. : IPC::ConnectionToServer<UIProcessClientEndpoint, UIProcessServerEndpoint>(*this, move(socket))
  76. {
  77. }
  78. UIProcessConnectionFromClient::UIProcessConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket, int client_id)
  79. : IPC::ConnectionFromClient<UIProcessClientEndpoint, UIProcessServerEndpoint>(*this, move(socket), client_id)
  80. {
  81. s_connections.set(client_id, *this);
  82. }
  83. void UIProcessConnectionFromClient::die()
  84. {
  85. s_connections.remove(client_id());
  86. }
  87. void UIProcessConnectionFromClient::create_new_tab(Vector<ByteString> const& urls)
  88. {
  89. if (on_new_tab)
  90. on_new_tab(urls);
  91. }
  92. void UIProcessConnectionFromClient::create_new_window(Vector<ByteString> const& urls)
  93. {
  94. if (on_new_window)
  95. on_new_window(urls);
  96. }
  97. }