2022-10-12 11:14:59 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
|
|
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-10-15 14:08:07 +02:00
|
|
|
|
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
2022-10-19 16:50:16 +02:00
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2022-11-03 13:31:08 -04:00
|
|
|
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Session.h"
|
|
|
|
|
|
#include "Client.h"
|
2023-03-06 01:57:36 +03:00
|
|
|
|
#include <AK/JsonObject.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
#include <LibCore/LocalServer.h>
|
2022-12-15 07:36:17 -05:00
|
|
|
|
#include <LibCore/StandardPaths.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
|
|
2022-11-22 07:38:07 -05:00
|
|
|
|
Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
|
2022-10-12 11:14:59 +01:00
|
|
|
|
: m_client(move(client))
|
2022-11-22 07:38:07 -05:00
|
|
|
|
, m_options(move(options))
|
2022-10-12 11:14:59 +01:00
|
|
|
|
, m_id(session_id)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
|
|
{
|
2022-11-14 11:55:10 -05:00
|
|
|
|
if (auto error = stop(); error.is_error())
|
|
|
|
|
|
warnln("Failed to stop session {}: {}", m_id, error.error());
|
2022-10-20 14:17:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-15 10:40:11 -05:00
|
|
|
|
ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(NonnullRefPtr<ServerPromise> promise)
|
2022-10-12 11:14:59 +01:00
|
|
|
|
{
|
2022-12-15 10:40:11 -05:00
|
|
|
|
dbgln("Listening for WebDriver connection on {}", *m_web_content_socket_path);
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
2022-11-08 10:18:11 -05:00
|
|
|
|
auto server = TRY(Core::LocalServer::try_create());
|
2022-12-15 10:40:11 -05:00
|
|
|
|
server->listen(*m_web_content_socket_path);
|
2022-11-08 10:18:11 -05:00
|
|
|
|
|
2022-11-19 01:09:53 +00:00
|
|
|
|
server->on_accept = [this, promise](auto client_socket) {
|
2022-11-11 14:14:58 -05: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 10:18:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-11 14:14:58 -05:00
|
|
|
|
dbgln("WebDriver is connected to WebContent socket");
|
2023-03-06 01:57:36 +03:00
|
|
|
|
auto web_content_connection = maybe_connection.release_value();
|
|
|
|
|
|
|
|
|
|
|
|
auto handle_name = String::formatted("window-{}"sv, m_next_handle_id).release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
|
m_next_handle_id++;
|
|
|
|
|
|
m_windows.set(handle_name, Session::Window { handle_name, move(web_content_connection) });
|
|
|
|
|
|
|
|
|
|
|
|
if (m_current_window_handle.is_empty())
|
|
|
|
|
|
m_current_window_handle = handle_name;
|
2022-11-08 10:18:11 -05:00
|
|
|
|
|
2022-11-11 14:14:58 -05:00
|
|
|
|
promise->resolve({});
|
2022-11-08 10:18:11 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-19 01:09:53 +00:00
|
|
|
|
server->on_accept_error = [promise](auto error) {
|
2022-11-08 10:18:11 -05:00
|
|
|
|
promise->resolve(move(error));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-15 08:46:01 -05:00
|
|
|
|
ErrorOr<void> Session::start(LaunchBrowserCallbacks const& callbacks)
|
2022-11-08 10:18:11 -05:00
|
|
|
|
{
|
|
|
|
|
|
auto promise = TRY(ServerPromise::try_create());
|
|
|
|
|
|
|
2022-12-15 10:40:11 -05:00
|
|
|
|
m_web_content_socket_path = DeprecatedString::formatted("{}/webdriver/session_{}_{}", TRY(Core::StandardPaths::runtime_directory()), getpid(), m_id);
|
|
|
|
|
|
auto web_content_server = TRY(create_server(promise));
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
2022-12-15 08:46:01 -05:00
|
|
|
|
if (m_options.headless)
|
2022-12-15 10:40:11 -05:00
|
|
|
|
m_browser_pid = TRY(callbacks.launch_headless_browser(*m_web_content_socket_path));
|
2022-12-15 08:46:01 -05:00
|
|
|
|
else
|
2022-12-15 10:40:11 -05:00
|
|
|
|
m_browser_pid = TRY(callbacks.launch_browser(*m_web_content_socket_path));
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
2022-11-09 10:56:12 -05:00
|
|
|
|
// FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
|
2022-11-08 10:18:11 -05:00
|
|
|
|
// errors received while accepting the Browser and WebContent sockets.
|
|
|
|
|
|
TRY(promise->await());
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
|
|
|
|
|
m_started = true;
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-08 14:14:29 -05:00
|
|
|
|
// https://w3c.github.io/webdriver/#dfn-close-the-session
|
|
|
|
|
|
Web::WebDriver::Response Session::stop()
|
2022-10-12 11:14:59 +01:00
|
|
|
|
{
|
2022-11-14 11:55:10 -05:00
|
|
|
|
if (!m_started)
|
|
|
|
|
|
return JsonValue {};
|
|
|
|
|
|
|
2022-11-08 14:14:29 -05: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.
|
2023-03-06 01:57:36 +03:00
|
|
|
|
web_content_connection().close_session();
|
2022-11-08 14:14:29 -05:00
|
|
|
|
|
|
|
|
|
|
// 2. Remove the current session from active sessions.
|
|
|
|
|
|
// NOTE: Handled by WebDriver::Client.
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Perform any implementation-specific cleanup steps.
|
2022-11-11 14:14:58 -05:00
|
|
|
|
if (m_browser_pid.has_value()) {
|
|
|
|
|
|
MUST(Core::System::kill(*m_browser_pid, SIGTERM));
|
|
|
|
|
|
m_browser_pid = {};
|
|
|
|
|
|
}
|
2022-12-15 10:40:11 -05:00
|
|
|
|
if (m_web_content_socket_path.has_value()) {
|
|
|
|
|
|
MUST(Core::System::unlink(*m_web_content_socket_path));
|
|
|
|
|
|
m_web_content_socket_path = {};
|
|
|
|
|
|
}
|
2022-11-11 14:14:58 -05:00
|
|
|
|
|
2022-11-08 14:14:29 -05: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 11:14:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-06 01:57:36 +03:00
|
|
|
|
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
|
|
|
|
|
|
Web::WebDriver::Response Session::close_window()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 3. Close the current top-level browsing context.
|
|
|
|
|
|
TRY(web_content_connection().close_window());
|
|
|
|
|
|
m_windows.remove(m_current_window_handle);
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If there are no more open top-level browsing contexts, then close the session.
|
|
|
|
|
|
if (m_windows.is_empty())
|
|
|
|
|
|
stop();
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Return the result of running the remote end steps for the Get Window Handles command.
|
|
|
|
|
|
return get_window_handles();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 11.3 Switch to Window, https://w3c.github.io/webdriver/#dfn-switch-to-window
|
|
|
|
|
|
Web::WebDriver::Response Session::switch_to_window(StringView handle)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 4. If handle is equal to the associated window handle for some top-level browsing context in the
|
|
|
|
|
|
// current session, let context be the that browsing context, and set the current top-level
|
|
|
|
|
|
// browsing context with context.
|
|
|
|
|
|
// Otherwise, return error with error code no such window.
|
|
|
|
|
|
if (auto it = m_windows.find(handle); it != m_windows.end())
|
|
|
|
|
|
m_current_window_handle = it->key;
|
|
|
|
|
|
else
|
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: 5. Update any implementation-specific state that would result from the user selecting the current
|
|
|
|
|
|
// browsing context for interaction, without altering OS-level focus.
|
|
|
|
|
|
|
|
|
|
|
|
// 6. Return success with data null.
|
|
|
|
|
|
return JsonValue {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
|
|
|
|
|
|
Web::WebDriver::Response Session::get_window_handles() const
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Let handles be a JSON List.
|
|
|
|
|
|
JsonArray handles {};
|
|
|
|
|
|
|
|
|
|
|
|
// 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
|
|
|
|
|
|
for (auto const& window_handle : m_windows.keys()) {
|
|
|
|
|
|
handles.append(JsonValue(window_handle));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return success with data handles.
|
|
|
|
|
|
return JsonValue { move(handles) };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
|
}
|