Session.cpp 3.6 KB

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