2022-10-12 10:14:59 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
|
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-10-15 12:08:07 +00:00
|
|
|
|
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
2022-10-19 14:50:16 +00:00
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2022-11-03 17:31:08 +00:00
|
|
|
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
2022-10-12 10:14:59 +00:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Session.h"
|
|
|
|
|
#include "Client.h"
|
|
|
|
|
#include <LibCore/LocalServer.h>
|
|
|
|
|
#include <LibCore/Stream.h>
|
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
|
|
|
|
|
Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
|
|
|
|
|
: m_client(move(client))
|
|
|
|
|
, m_id(session_id)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
|
{
|
2022-11-14 16:55:10 +00:00
|
|
|
|
if (auto error = stop(); error.is_error())
|
|
|
|
|
warnln("Failed to stop session {}: {}", m_id, error.error());
|
2022-10-20 13:17:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 19:14:58 +00:00
|
|
|
|
ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, NonnullRefPtr<ServerPromise> promise)
|
2022-10-12 10:14:59 +00:00
|
|
|
|
{
|
|
|
|
|
dbgln("Listening for WebDriver connection on {}", socket_path);
|
|
|
|
|
|
2022-11-08 15:18:11 +00:00
|
|
|
|
auto server = TRY(Core::LocalServer::try_create());
|
|
|
|
|
server->listen(socket_path);
|
|
|
|
|
|
2022-11-19 01:09:53 +00:00
|
|
|
|
server->on_accept = [this, promise](auto client_socket) {
|
2022-11-11 19:14:58 +00:00
|
|
|
|
auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket), m_client, session_id()));
|
|
|
|
|
if (maybe_connection.is_error()) {
|
|
|
|
|
promise->resolve(maybe_connection.release_error());
|
|
|
|
|
return;
|
2022-11-08 15:18:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 19:14:58 +00:00
|
|
|
|
dbgln("WebDriver is connected to WebContent socket");
|
|
|
|
|
m_web_content_connection = maybe_connection.release_value();
|
2022-11-08 15:18:11 +00:00
|
|
|
|
|
2022-11-11 19:14:58 +00:00
|
|
|
|
promise->resolve({});
|
2022-11-08 15:18:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-11-19 01:09:53 +00:00
|
|
|
|
server->on_accept_error = [promise](auto error) {
|
2022-11-08 15:18:11 +00:00
|
|
|
|
promise->resolve(move(error));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> Session::start()
|
|
|
|
|
{
|
|
|
|
|
auto promise = TRY(ServerPromise::try_create());
|
|
|
|
|
|
2022-11-11 19:14:58 +00:00
|
|
|
|
auto web_content_socket_path = String::formatted("/tmp/webdriver/session_{}_{}", getpid(), m_id);
|
|
|
|
|
auto web_content_server = TRY(create_server(web_content_socket_path, promise));
|
2022-10-12 10:14:59 +00:00
|
|
|
|
|
2022-11-08 15:18:11 +00:00
|
|
|
|
char const* argv[] = {
|
|
|
|
|
"/bin/Browser",
|
|
|
|
|
"--webdriver-content-path",
|
|
|
|
|
web_content_socket_path.characters(),
|
|
|
|
|
nullptr,
|
|
|
|
|
};
|
2022-10-12 10:14:59 +00:00
|
|
|
|
|
2022-11-11 19:14:58 +00:00
|
|
|
|
m_browser_pid = TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
|
2022-10-12 10:14:59 +00:00
|
|
|
|
|
2022-11-09 15:56:12 +00:00
|
|
|
|
// FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
|
2022-11-08 15:18:11 +00:00
|
|
|
|
// errors received while accepting the Browser and WebContent sockets.
|
|
|
|
|
TRY(promise->await());
|
2022-10-12 10:14:59 +00:00
|
|
|
|
|
|
|
|
|
m_started = true;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 19:14:29 +00:00
|
|
|
|
// https://w3c.github.io/webdriver/#dfn-close-the-session
|
|
|
|
|
Web::WebDriver::Response Session::stop()
|
2022-10-12 10:14:59 +00:00
|
|
|
|
{
|
2022-11-14 16:55:10 +00:00
|
|
|
|
if (!m_started)
|
|
|
|
|
return JsonValue {};
|
|
|
|
|
|
2022-11-08 19:14:29 +00:00
|
|
|
|
// 1. Perform the following substeps based on the remote end’s type:
|
|
|
|
|
// NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
|
|
|
|
|
m_web_content_connection->close_session();
|
|
|
|
|
|
|
|
|
|
// 2. Remove the current session from active sessions.
|
|
|
|
|
// NOTE: Handled by WebDriver::Client.
|
|
|
|
|
|
|
|
|
|
// 3. Perform any implementation-specific cleanup steps.
|
2022-11-11 19:14:58 +00:00
|
|
|
|
if (m_browser_pid.has_value()) {
|
|
|
|
|
MUST(Core::System::kill(*m_browser_pid, SIGTERM));
|
|
|
|
|
m_browser_pid = {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 19:14:29 +00:00
|
|
|
|
m_started = false;
|
|
|
|
|
|
|
|
|
|
// 4. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
|
|
|
|
|
return JsonValue {};
|
2022-10-12 10:14:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|