Session.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-2024, Tim Flynn <trflynn89@ladybird.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/EventLoop.h>
  15. #include <LibCore/LocalServer.h>
  16. #include <LibCore/StandardPaths.h>
  17. #include <LibCore/System.h>
  18. #include <unistd.h>
  19. namespace WebDriver {
  20. Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
  21. : m_client(move(client))
  22. , m_options(move(options))
  23. , m_id(session_id)
  24. {
  25. }
  26. // https://w3c.github.io/webdriver/#dfn-close-the-session
  27. Session::~Session()
  28. {
  29. if (!m_started)
  30. return;
  31. // 1. Perform the following substeps based on the remote end’s type:
  32. // NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
  33. for (auto& it : m_windows) {
  34. it.value.web_content_connection->close_session();
  35. }
  36. // 2. Remove the current session from active sessions.
  37. // NOTE: We are in a session destruction which means it is already removed
  38. // from active sessions
  39. // 3. Perform any implementation-specific cleanup steps.
  40. if (m_browser_pid.has_value()) {
  41. MUST(Core::System::kill(*m_browser_pid, SIGTERM));
  42. m_browser_pid = {};
  43. }
  44. if (m_web_content_socket_path.has_value()) {
  45. MUST(Core::System::unlink(*m_web_content_socket_path));
  46. m_web_content_socket_path = {};
  47. }
  48. }
  49. ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(NonnullRefPtr<ServerPromise> promise)
  50. {
  51. dbgln("Listening for WebDriver connection on {}", *m_web_content_socket_path);
  52. (void)Core::System::unlink(*m_web_content_socket_path);
  53. auto server = TRY(Core::LocalServer::try_create());
  54. server->listen(*m_web_content_socket_path);
  55. server->on_accept = [this, promise](auto client_socket) {
  56. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket)));
  57. if (maybe_connection.is_error()) {
  58. promise->resolve(maybe_connection.release_error());
  59. return;
  60. }
  61. dbgln("WebDriver is connected to WebContent socket");
  62. auto web_content_connection = maybe_connection.release_value();
  63. auto maybe_window_handle = web_content_connection->get_window_handle();
  64. if (maybe_window_handle.is_error()) {
  65. promise->reject(Error::from_string_literal("Window was closed immediately"));
  66. return;
  67. }
  68. auto window_handle = MUST(String::from_byte_string(maybe_window_handle.value().as_string()));
  69. web_content_connection->on_close = [this, window_handle]() {
  70. dbgln_if(WEBDRIVER_DEBUG, "Window {} was closed remotely.", window_handle);
  71. m_windows.remove(window_handle);
  72. if (m_windows.is_empty())
  73. m_client->close_session(session_id());
  74. };
  75. m_windows.set(window_handle, Session::Window { window_handle, move(web_content_connection) });
  76. if (m_current_window_handle.is_empty())
  77. m_current_window_handle = window_handle;
  78. promise->resolve({});
  79. };
  80. server->on_accept_error = [promise](auto error) {
  81. promise->resolve(move(error));
  82. };
  83. return server;
  84. }
  85. ErrorOr<void> Session::start(LaunchBrowserCallbacks const& callbacks)
  86. {
  87. auto promise = TRY(ServerPromise::try_create());
  88. m_web_content_socket_path = ByteString::formatted("{}/webdriver/session_{}_{}", TRY(Core::StandardPaths::runtime_directory()), getpid(), m_id);
  89. m_web_content_server = TRY(create_server(promise));
  90. if (m_options.headless)
  91. m_browser_pid = TRY(callbacks.launch_headless_browser(*m_web_content_socket_path));
  92. else
  93. m_browser_pid = TRY(callbacks.launch_browser(*m_web_content_socket_path));
  94. // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
  95. // errors received while accepting the Browser and WebContent sockets.
  96. TRY(TRY(promise->await()));
  97. m_started = true;
  98. return {};
  99. }
  100. // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
  101. Web::WebDriver::Response Session::close_window()
  102. {
  103. {
  104. // Defer removing the window handle from this session until after we know we are done with its connection.
  105. ScopeGuard guard { [this] { m_windows.remove(m_current_window_handle); m_current_window_handle = "NoSuchWindowPleaseSelectANewOne"_string; } };
  106. // 3. Close the current top-level browsing context.
  107. TRY(web_content_connection().close_window());
  108. // 4. If there are no more open top-level browsing contexts, then close the session.
  109. if (m_windows.size() == 1)
  110. m_client->close_session(session_id());
  111. }
  112. // 5. Return the result of running the remote end steps for the Get Window Handles command.
  113. return get_window_handles();
  114. }
  115. // 11.3 Switch to Window, https://w3c.github.io/webdriver/#dfn-switch-to-window
  116. Web::WebDriver::Response Session::switch_to_window(StringView handle)
  117. {
  118. // 4. If handle is equal to the associated window handle for some top-level browsing context in the
  119. // current session, let context be the that browsing context, and set the current top-level
  120. // browsing context with context.
  121. // Otherwise, return error with error code no such window.
  122. if (auto it = m_windows.find(handle); it != m_windows.end())
  123. m_current_window_handle = it->key;
  124. else
  125. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
  126. // 5. Update any implementation-specific state that would result from the user selecting the current
  127. // browsing context for interaction, without altering OS-level focus.
  128. TRY(web_content_connection().switch_to_window(m_current_window_handle));
  129. // 6. Return success with data null.
  130. return JsonValue {};
  131. }
  132. // 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
  133. Web::WebDriver::Response Session::get_window_handles() const
  134. {
  135. // 1. Let handles be a JSON List.
  136. JsonArray handles {};
  137. // 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
  138. for (auto const& window_handle : m_windows.keys()) {
  139. handles.must_append(JsonValue(window_handle));
  140. }
  141. // 3. Return success with data handles.
  142. return JsonValue { move(handles) };
  143. }
  144. ErrorOr<void, Web::WebDriver::Error> Session::ensure_current_window_handle_is_valid() const
  145. {
  146. if (auto current_window = m_windows.get(m_current_window_handle); current_window.has_value())
  147. return {};
  148. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
  149. }
  150. Web::WebDriver::Response Session::execute_script(JsonValue payload, ScriptMode mode) const
  151. {
  152. ScopeGuard guard { [&]() { web_content_connection().on_script_executed = nullptr; } };
  153. Optional<Web::WebDriver::Response> response;
  154. web_content_connection().on_script_executed = [&](auto result) {
  155. response = move(result);
  156. };
  157. switch (mode) {
  158. case ScriptMode::Sync:
  159. TRY(web_content_connection().execute_script(move(payload)));
  160. break;
  161. case ScriptMode::Async:
  162. TRY(web_content_connection().execute_async_script(move(payload)));
  163. break;
  164. }
  165. Core::EventLoop::current().spin_until([&]() {
  166. return response.has_value();
  167. });
  168. return response.release_value();
  169. }
  170. Web::WebDriver::Response Session::element_click(String element_id) const
  171. {
  172. ScopeGuard guard { [&]() { web_content_connection().on_actions_performed = nullptr; } };
  173. Optional<Web::WebDriver::Response> response;
  174. web_content_connection().on_actions_performed = [&](auto result) {
  175. response = move(result);
  176. };
  177. TRY(web_content_connection().element_click(move(element_id)));
  178. Core::EventLoop::current().spin_until([&]() {
  179. return response.has_value();
  180. });
  181. return response.release_value();
  182. }
  183. Web::WebDriver::Response Session::perform_actions(JsonValue payload) const
  184. {
  185. ScopeGuard guard { [&]() { web_content_connection().on_actions_performed = nullptr; } };
  186. Optional<Web::WebDriver::Response> response;
  187. web_content_connection().on_actions_performed = [&](auto result) {
  188. response = move(result);
  189. };
  190. TRY(web_content_connection().perform_actions(move(payload)));
  191. Core::EventLoop::current().spin_until([&]() {
  192. return response.has_value();
  193. });
  194. return response.release_value();
  195. }
  196. }