Session.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  6. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "Session.h"
  11. #include "Client.h"
  12. #include <AK/JsonObject.h>
  13. #include <AK/ScopeGuard.h>
  14. #include <LibCore/LocalServer.h>
  15. #include <LibCore/StandardPaths.h>
  16. #include <LibCore/System.h>
  17. #include <unistd.h>
  18. namespace WebDriver {
  19. Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
  20. : m_client(move(client))
  21. , m_options(move(options))
  22. , m_id(session_id)
  23. {
  24. }
  25. // https://w3c.github.io/webdriver/#dfn-close-the-session
  26. Session::~Session()
  27. {
  28. if (!m_started)
  29. return;
  30. // 1. Perform the following substeps based on the remote end’s type:
  31. // NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
  32. for (auto& it : m_windows) {
  33. it.value.web_content_connection->close_session();
  34. }
  35. // 2. Remove the current session from active sessions.
  36. // NOTE: We are in a session destruction which means it is already removed
  37. // from active sessions
  38. // 3. Perform any implementation-specific cleanup steps.
  39. if (m_browser_pid.has_value()) {
  40. MUST(Core::System::kill(*m_browser_pid, SIGTERM));
  41. m_browser_pid = {};
  42. }
  43. if (m_web_content_socket_path.has_value()) {
  44. MUST(Core::System::unlink(*m_web_content_socket_path));
  45. m_web_content_socket_path = {};
  46. }
  47. }
  48. ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(NonnullRefPtr<ServerPromise> promise)
  49. {
  50. dbgln("Listening for WebDriver connection on {}", *m_web_content_socket_path);
  51. auto server = TRY(Core::LocalServer::try_create());
  52. server->listen(*m_web_content_socket_path);
  53. server->on_accept = [this, promise](auto client_socket) {
  54. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket)));
  55. if (maybe_connection.is_error()) {
  56. // Use of MUST in this function is safe, as our promise callback can never error out.
  57. MUST(promise->resolve(maybe_connection.release_error()));
  58. return;
  59. }
  60. dbgln("WebDriver is connected to WebContent socket");
  61. auto web_content_connection = maybe_connection.release_value();
  62. auto window_handle = web_content_connection->get_window_handle();
  63. web_content_connection->on_close = [this, window_handle]() {
  64. dbgln_if(WEBDRIVER_DEBUG, "Window {} was closed remotely.", window_handle);
  65. m_windows.remove(window_handle);
  66. if (m_windows.is_empty())
  67. m_client->close_session(session_id());
  68. };
  69. m_windows.set(window_handle, Session::Window { window_handle, move(web_content_connection) });
  70. if (m_current_window_handle.is_empty())
  71. m_current_window_handle = window_handle;
  72. MUST(promise->resolve({}));
  73. };
  74. server->on_accept_error = [promise](auto error) {
  75. MUST(promise->resolve(move(error)));
  76. };
  77. return server;
  78. }
  79. ErrorOr<void> Session::start(LaunchBrowserCallbacks const& callbacks)
  80. {
  81. auto promise = TRY(ServerPromise::try_create());
  82. m_web_content_socket_path = DeprecatedString::formatted("{}/webdriver/session_{}_{}", TRY(Core::StandardPaths::runtime_directory()), getpid(), m_id);
  83. m_web_content_server = TRY(create_server(promise));
  84. if (m_options.headless)
  85. m_browser_pid = TRY(callbacks.launch_headless_browser(*m_web_content_socket_path));
  86. else
  87. m_browser_pid = TRY(callbacks.launch_browser(*m_web_content_socket_path));
  88. // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
  89. // errors received while accepting the Browser and WebContent sockets.
  90. TRY(TRY(promise->await()));
  91. m_started = true;
  92. return {};
  93. }
  94. // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
  95. Web::WebDriver::Response Session::close_window()
  96. {
  97. {
  98. // Defer removing the window handle from this session until after we know we are done with its connection.
  99. ScopeGuard guard { [this] { m_windows.remove(m_current_window_handle); } };
  100. // 3. Close the current top-level browsing context.
  101. TRY(web_content_connection().close_window());
  102. // 4. If there are no more open top-level browsing contexts, then close the session.
  103. if (m_windows.size() == 1)
  104. m_client->close_session(session_id());
  105. }
  106. // 5. Return the result of running the remote end steps for the Get Window Handles command.
  107. return get_window_handles();
  108. }
  109. // 11.3 Switch to Window, https://w3c.github.io/webdriver/#dfn-switch-to-window
  110. Web::WebDriver::Response Session::switch_to_window(StringView handle)
  111. {
  112. // 4. If handle is equal to the associated window handle for some top-level browsing context in the
  113. // current session, let context be the that browsing context, and set the current top-level
  114. // browsing context with context.
  115. // Otherwise, return error with error code no such window.
  116. if (auto it = m_windows.find(handle); it != m_windows.end())
  117. m_current_window_handle = it->key;
  118. else
  119. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
  120. // 5. Update any implementation-specific state that would result from the user selecting the current
  121. // browsing context for interaction, without altering OS-level focus.
  122. TRY(web_content_connection().switch_to_window());
  123. // 6. Return success with data null.
  124. return JsonValue {};
  125. }
  126. // 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
  127. Web::WebDriver::Response Session::get_window_handles() const
  128. {
  129. // 1. Let handles be a JSON List.
  130. JsonArray handles {};
  131. // 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
  132. for (auto const& window_handle : m_windows.keys()) {
  133. handles.append(JsonValue(window_handle));
  134. }
  135. // 3. Return success with data handles.
  136. return JsonValue { move(handles) };
  137. }
  138. }