Session.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <LibCore/LocalServer.h>
  13. #include <LibCore/Stream.h>
  14. #include <LibCore/System.h>
  15. #include <unistd.h>
  16. namespace WebDriver {
  17. Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
  18. : m_client(move(client))
  19. , m_id(session_id)
  20. {
  21. }
  22. Session::~Session()
  23. {
  24. if (auto error = stop(); error.is_error())
  25. warnln("Failed to stop session {}: {}", m_id, error.error());
  26. }
  27. ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, NonnullRefPtr<ServerPromise> promise)
  28. {
  29. dbgln("Listening for WebDriver connection on {}", socket_path);
  30. auto server = TRY(Core::LocalServer::try_create());
  31. server->listen(socket_path);
  32. server->on_accept = [this, promise](auto client_socket) {
  33. auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket), m_client, session_id()));
  34. if (maybe_connection.is_error()) {
  35. promise->resolve(maybe_connection.release_error());
  36. return;
  37. }
  38. dbgln("WebDriver is connected to WebContent socket");
  39. m_web_content_connection = maybe_connection.release_value();
  40. promise->resolve({});
  41. };
  42. server->on_accept_error = [promise](auto error) {
  43. promise->resolve(move(error));
  44. };
  45. return server;
  46. }
  47. ErrorOr<void> Session::start()
  48. {
  49. auto promise = TRY(ServerPromise::try_create());
  50. auto web_content_socket_path = String::formatted("/tmp/webdriver/session_{}_{}", getpid(), m_id);
  51. auto web_content_server = TRY(create_server(web_content_socket_path, promise));
  52. char const* argv[] = {
  53. "/bin/Browser",
  54. "--webdriver-content-path",
  55. web_content_socket_path.characters(),
  56. nullptr,
  57. };
  58. m_browser_pid = TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
  59. // FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
  60. // errors received while accepting the Browser and WebContent sockets.
  61. TRY(promise->await());
  62. m_started = true;
  63. return {};
  64. }
  65. // https://w3c.github.io/webdriver/#dfn-close-the-session
  66. Web::WebDriver::Response Session::stop()
  67. {
  68. if (!m_started)
  69. return JsonValue {};
  70. // 1. Perform the following substeps based on the remote end’s type:
  71. // NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
  72. m_web_content_connection->close_session();
  73. // 2. Remove the current session from active sessions.
  74. // NOTE: Handled by WebDriver::Client.
  75. // 3. Perform any implementation-specific cleanup steps.
  76. if (m_browser_pid.has_value()) {
  77. MUST(Core::System::kill(*m_browser_pid, SIGTERM));
  78. m_browser_pid = {};
  79. }
  80. m_started = false;
  81. // 4. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
  82. return JsonValue {};
  83. }
  84. }