Session.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "BrowserConnection.h"
  12. #include "Client.h"
  13. #include <AK/JsonObject.h>
  14. #include <AK/JsonParser.h>
  15. #include <AK/NumericLimits.h>
  16. #include <AK/Time.h>
  17. #include <LibCore/LocalServer.h>
  18. #include <LibCore/Stream.h>
  19. #include <LibCore/System.h>
  20. #include <LibGfx/Point.h>
  21. #include <LibGfx/Rect.h>
  22. #include <LibGfx/Size.h>
  23. #include <unistd.h>
  24. namespace WebDriver {
  25. Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
  26. : m_client(move(client))
  27. , m_id(session_id)
  28. {
  29. }
  30. Session::~Session()
  31. {
  32. if (m_started) {
  33. auto error = stop();
  34. if (error.is_error()) {
  35. warnln("Failed to stop session {}: {}", m_id, error.error());
  36. }
  37. }
  38. }
  39. ErrorOr<Session::Window*, Web::WebDriver::Error> Session::current_window()
  40. {
  41. auto window = m_windows.get(m_current_window_handle);
  42. if (!window.has_value())
  43. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
  44. return window.release_value();
  45. }
  46. ErrorOr<void, Web::WebDriver::Error> Session::check_for_open_top_level_browsing_context_or_return_error()
  47. {
  48. (void)TRY(current_window());
  49. return {};
  50. }
  51. ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, ServerType type, NonnullRefPtr<ServerPromise> promise)
  52. {
  53. dbgln("Listening for WebDriver connection on {}", socket_path);
  54. auto server = TRY(Core::LocalServer::try_create());
  55. server->listen(socket_path);
  56. server->on_accept = [this, type, promise](auto client_socket) mutable {
  57. switch (type) {
  58. case ServerType::Browser: {
  59. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) BrowserConnection(move(client_socket), m_client, session_id()));
  60. if (maybe_connection.is_error()) {
  61. promise->resolve(maybe_connection.release_error());
  62. return;
  63. }
  64. dbgln("WebDriver is connected to Browser socket");
  65. m_browser_connection = maybe_connection.release_value();
  66. break;
  67. }
  68. case ServerType::WebContent: {
  69. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket), m_client, session_id()));
  70. if (maybe_connection.is_error()) {
  71. promise->resolve(maybe_connection.release_error());
  72. return;
  73. }
  74. dbgln("WebDriver is connected to WebContent socket");
  75. m_web_content_connection = maybe_connection.release_value();
  76. break;
  77. }
  78. }
  79. if (m_browser_connection && m_web_content_connection)
  80. promise->resolve({});
  81. };
  82. server->on_accept_error = [promise](auto error) mutable {
  83. promise->resolve(move(error));
  84. };
  85. return server;
  86. }
  87. ErrorOr<void> Session::start()
  88. {
  89. auto promise = TRY(ServerPromise::try_create());
  90. auto browser_socket_path = String::formatted("/tmp/webdriver/browser_{}_{}", getpid(), m_id);
  91. auto browser_server = TRY(create_server(browser_socket_path, ServerType::Browser, promise));
  92. auto web_content_socket_path = String::formatted("/tmp/webdriver/content_{}_{}", getpid(), m_id);
  93. auto web_content_server = TRY(create_server(web_content_socket_path, ServerType::WebContent, promise));
  94. char const* argv[] = {
  95. "/bin/Browser",
  96. "--webdriver-browser-path",
  97. browser_socket_path.characters(),
  98. "--webdriver-content-path",
  99. web_content_socket_path.characters(),
  100. nullptr,
  101. };
  102. TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
  103. // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
  104. // errors received while accepting the Browser and WebContent sockets.
  105. TRY(promise->await());
  106. m_started = true;
  107. m_windows.set("main", make<Session::Window>("main", true));
  108. m_current_window_handle = "main";
  109. return {};
  110. }
  111. // https://w3c.github.io/webdriver/#dfn-close-the-session
  112. Web::WebDriver::Response Session::stop()
  113. {
  114. // 1. Perform the following substeps based on the remote end’s type:
  115. // NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
  116. m_web_content_connection->close_session();
  117. m_web_content_connection = nullptr;
  118. // 2. Remove the current session from active sessions.
  119. // NOTE: Handled by WebDriver::Client.
  120. // 3. Perform any implementation-specific cleanup steps.
  121. m_browser_connection->async_quit();
  122. m_started = false;
  123. // 4. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
  124. return JsonValue {};
  125. }
  126. // 9.1 Get Timeouts, https://w3c.github.io/webdriver/#dfn-get-timeouts
  127. JsonObject Session::get_timeouts()
  128. {
  129. // 1. Let timeouts be the timeouts object for session’s timeouts configuration
  130. auto timeouts = timeouts_object(m_timeouts_configuration);
  131. // 2. Return success with data timeouts.
  132. return timeouts;
  133. }
  134. // 9.2 Set Timeouts, https://w3c.github.io/webdriver/#dfn-set-timeouts
  135. Web::WebDriver::Response Session::set_timeouts(JsonValue const& payload)
  136. {
  137. // 1. Let timeouts be the result of trying to JSON deserialize as a timeouts configuration the request’s parameters.
  138. auto timeouts = TRY(json_deserialize_as_a_timeouts_configuration(payload));
  139. // 2. Make the session timeouts the new timeouts.
  140. m_timeouts_configuration = move(timeouts);
  141. // 3. Return success with data null.
  142. return JsonValue {};
  143. }
  144. // 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
  145. Web::WebDriver::Response Session::get_window_handle()
  146. {
  147. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  148. TRY(check_for_open_top_level_browsing_context_or_return_error());
  149. // 2. Return success with data being the window handle associated with the current top-level browsing context.
  150. return JsonValue { m_current_window_handle };
  151. }
  152. // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
  153. ErrorOr<void, Variant<Web::WebDriver::Error, Error>> Session::close_window()
  154. {
  155. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  156. TRY(check_for_open_top_level_browsing_context_or_return_error());
  157. // 2. Close the current top-level browsing context.
  158. m_windows.remove(m_current_window_handle);
  159. // 3. If there are no more open top-level browsing contexts, then close the session.
  160. if (m_windows.is_empty()) {
  161. auto result = stop();
  162. if (result.is_error()) {
  163. return Variant<Web::WebDriver::Error, Error>(result.release_error());
  164. }
  165. }
  166. return {};
  167. }
  168. // 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
  169. Web::WebDriver::Response Session::get_window_handles() const
  170. {
  171. // 1. Let handles be a JSON List.
  172. auto handles = JsonArray {};
  173. // 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
  174. for (auto const& window_handle : m_windows.keys())
  175. handles.append(window_handle);
  176. // 3. Return success with data handles.
  177. return JsonValue { handles };
  178. }
  179. }