Session.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/JsonParser.h>
  14. #include <AK/NumericLimits.h>
  15. #include <AK/Time.h>
  16. #include <LibCore/LocalServer.h>
  17. #include <LibCore/Stream.h>
  18. #include <LibCore/System.h>
  19. #include <LibGfx/Point.h>
  20. #include <LibGfx/Rect.h>
  21. #include <LibGfx/Size.h>
  22. #include <unistd.h>
  23. namespace WebDriver {
  24. Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
  25. : m_client(move(client))
  26. , m_id(session_id)
  27. {
  28. }
  29. Session::~Session()
  30. {
  31. if (m_started) {
  32. auto error = stop();
  33. if (error.is_error()) {
  34. warnln("Failed to stop session {}: {}", m_id, error.error());
  35. }
  36. }
  37. }
  38. ErrorOr<Session::Window*, Web::WebDriver::Error> Session::current_window()
  39. {
  40. auto window = m_windows.get(m_current_window_handle);
  41. if (!window.has_value())
  42. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
  43. return window.release_value();
  44. }
  45. ErrorOr<void, Web::WebDriver::Error> Session::check_for_open_top_level_browsing_context_or_return_error()
  46. {
  47. (void)TRY(current_window());
  48. return {};
  49. }
  50. ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, NonnullRefPtr<ServerPromise> promise)
  51. {
  52. dbgln("Listening for WebDriver connection on {}", socket_path);
  53. auto server = TRY(Core::LocalServer::try_create());
  54. server->listen(socket_path);
  55. server->on_accept = [this, promise](auto client_socket) mutable {
  56. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket), m_client, session_id()));
  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. m_web_content_connection = maybe_connection.release_value();
  63. promise->resolve({});
  64. };
  65. server->on_accept_error = [promise](auto error) mutable {
  66. promise->resolve(move(error));
  67. };
  68. return server;
  69. }
  70. ErrorOr<void> Session::start()
  71. {
  72. auto promise = TRY(ServerPromise::try_create());
  73. auto web_content_socket_path = String::formatted("/tmp/webdriver/session_{}_{}", getpid(), m_id);
  74. auto web_content_server = TRY(create_server(web_content_socket_path, promise));
  75. char const* argv[] = {
  76. "/bin/Browser",
  77. "--webdriver-content-path",
  78. web_content_socket_path.characters(),
  79. nullptr,
  80. };
  81. m_browser_pid = TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
  82. // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
  83. // errors received while accepting the Browser and WebContent sockets.
  84. TRY(promise->await());
  85. m_started = true;
  86. m_windows.set("main", make<Session::Window>("main", true));
  87. m_current_window_handle = "main";
  88. return {};
  89. }
  90. // https://w3c.github.io/webdriver/#dfn-close-the-session
  91. Web::WebDriver::Response Session::stop()
  92. {
  93. // 1. Perform the following substeps based on the remote end’s type:
  94. // NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
  95. m_web_content_connection->close_session();
  96. m_web_content_connection = nullptr;
  97. // 2. Remove the current session from active sessions.
  98. // NOTE: Handled by WebDriver::Client.
  99. // 3. Perform any implementation-specific cleanup steps.
  100. if (m_browser_pid.has_value()) {
  101. MUST(Core::System::kill(*m_browser_pid, SIGTERM));
  102. m_browser_pid = {};
  103. }
  104. m_started = false;
  105. // 4. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
  106. return JsonValue {};
  107. }
  108. // 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
  109. Web::WebDriver::Response Session::get_window_handle()
  110. {
  111. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  112. TRY(check_for_open_top_level_browsing_context_or_return_error());
  113. // 2. Return success with data being the window handle associated with the current top-level browsing context.
  114. return JsonValue { m_current_window_handle };
  115. }
  116. // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
  117. ErrorOr<void, Variant<Web::WebDriver::Error, Error>> Session::close_window()
  118. {
  119. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  120. TRY(check_for_open_top_level_browsing_context_or_return_error());
  121. // 2. Close the current top-level browsing context.
  122. m_windows.remove(m_current_window_handle);
  123. // 3. If there are no more open top-level browsing contexts, then close the session.
  124. if (m_windows.is_empty()) {
  125. auto result = stop();
  126. if (result.is_error()) {
  127. return Variant<Web::WebDriver::Error, Error>(result.release_error());
  128. }
  129. }
  130. return {};
  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. auto handles = JsonArray {};
  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.append(window_handle);
  140. // 3. Return success with data handles.
  141. return JsonValue { handles };
  142. }
  143. }