2022-10-12 07:14:59 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
|
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-10-15 09:08:07 -03:00
|
|
|
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
|
2022-10-19 11:50:16 -03:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2025-02-05 13:36:19 -03:00
|
|
|
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
|
2022-10-12 07:14:59 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
#include <AK/HashMap.h>
|
2023-03-05 19:57:36 -03:00
|
|
|
#include <AK/JsonObject.h>
|
2026-06-13 11:06:49 -03:00
|
|
|
#include <AK/NumericLimits.h>
|
2026-03-22 15:59:08 -03:00
|
|
|
#if !defined(AK_OS_MACOS)
|
|
|
|
|
# include <LibCore/LocalServer.h>
|
|
|
|
|
# include <LibCore/Socket.h>
|
|
|
|
|
# include <LibCore/StandardPaths.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <LibIPC/TransportBootstrapMach.h>
|
|
|
|
|
# include <LibWebView/Utilities.h>
|
|
|
|
|
#endif
|
2022-10-12 07:14:59 -03:00
|
|
|
#include <LibCore/System.h>
|
2026-06-13 11:06:49 -03:00
|
|
|
#include <LibCore/Timer.h>
|
2026-03-19 15:42:05 -03:00
|
|
|
#include <LibIPC/Transport.h>
|
2025-02-05 18:05:25 -03:00
|
|
|
#include <LibWeb/Crypto/Crypto.h>
|
2025-02-07 15:52:41 -03:00
|
|
|
#include <LibWeb/WebDriver/Proxy.h>
|
2024-11-04 10:37:16 -03:00
|
|
|
#include <LibWeb/WebDriver/TimeoutsConfiguration.h>
|
2025-02-05 13:36:19 -03:00
|
|
|
#include <LibWeb/WebDriver/UserPrompt.h>
|
2025-06-06 16:42:12 -03:00
|
|
|
#include <WebDriver/Session.h>
|
2022-10-12 07:14:59 -03:00
|
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
static HashMap<String, NonnullRefPtr<Session>> s_sessions;
|
2025-02-07 11:16:20 -03:00
|
|
|
static HashMap<String, NonnullRefPtr<Session>> s_http_sessions;
|
2025-02-07 11:04:45 -03:00
|
|
|
|
2025-02-05 18:05:25 -03:00
|
|
|
// https://w3c.github.io/webdriver/#dfn-create-a-session
|
2025-02-07 13:09:28 -03:00
|
|
|
ErrorOr<NonnullRefPtr<Session>> Session::create(NonnullRefPtr<Client> client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags)
|
2025-02-05 18:05:25 -03:00
|
|
|
{
|
|
|
|
|
// 1. Let session id be the result of generating a UUID.
|
2026-03-24 09:33:21 -03:00
|
|
|
auto session_id = Web::Crypto::generate_random_uuid();
|
2025-02-05 18:05:25 -03:00
|
|
|
|
|
|
|
|
// 2. Let session be a new session with session ID session id, and HTTP flag flags contains "http".
|
2025-02-07 13:09:28 -03:00
|
|
|
auto session = adopt_ref(*new Session(client, capabilities, move(session_id), flags));
|
2025-06-06 16:42:12 -03:00
|
|
|
TRY(session->start(client->launch_browser_callback()));
|
2025-02-05 18:05:25 -03:00
|
|
|
|
2025-02-07 15:52:41 -03:00
|
|
|
// 3. Let proxy be the result of getting property "proxy" from capabilities and run the substeps of the first matching statement:
|
|
|
|
|
// -> proxy is a proxy configuration object
|
|
|
|
|
if (auto proxy = capabilities.get_object("proxy"sv); proxy.has_value()) {
|
|
|
|
|
// Take implementation-defined steps to set the user agent proxy using the extracted proxy configuration. If the
|
|
|
|
|
// defined proxy cannot be configured return error with error code session not created. Otherwise set the has
|
|
|
|
|
// proxy configuration flag to true.
|
|
|
|
|
return Error::from_string_literal("Proxy configuration is not yet supported");
|
|
|
|
|
}
|
|
|
|
|
// -> Otherwise
|
|
|
|
|
else {
|
|
|
|
|
// Set a property of capabilities with name "proxy" and a value that is a new JSON Object.
|
2025-02-17 14:18:27 -03:00
|
|
|
capabilities.set("proxy"sv, JsonObject {});
|
2025-02-07 15:52:41 -03:00
|
|
|
}
|
2025-02-05 18:05:25 -03:00
|
|
|
|
|
|
|
|
// FIXME: 4. If capabilites has a property named "acceptInsecureCerts", set the endpoint node's accept insecure TLS flag
|
|
|
|
|
// to the result of getting a property named "acceptInsecureCerts" from capabilities.
|
|
|
|
|
|
|
|
|
|
// 5. Let user prompt handler capability be the result of getting property "unhandledPromptBehavior" from capabilities.
|
|
|
|
|
auto user_prompt_handler_capability = capabilities.get_object("unhandledPromptBehavior"sv);
|
|
|
|
|
|
|
|
|
|
// 6. If user prompt handler capability is not undefined, update the user prompt handler with user prompt handler capability.
|
|
|
|
|
if (user_prompt_handler_capability.has_value())
|
|
|
|
|
Web::WebDriver::update_the_user_prompt_handler(*user_prompt_handler_capability);
|
|
|
|
|
|
|
|
|
|
session->web_content_connection().async_set_user_prompt_handler(Web::WebDriver::user_prompt_handler());
|
|
|
|
|
|
|
|
|
|
// 7. Let serialized user prompt handler be serialize the user prompt handler.
|
|
|
|
|
auto serialized_user_prompt_handler = Web::WebDriver::serialize_the_user_prompt_handler();
|
|
|
|
|
|
|
|
|
|
// 8. Set a property on capabilities with the name "unhandledPromptBehavior", and the value serialized user prompt handler.
|
|
|
|
|
capabilities.set("unhandledPromptBehavior"sv, move(serialized_user_prompt_handler));
|
|
|
|
|
|
|
|
|
|
// 9. If flags contains "http":
|
2025-02-07 13:09:28 -03:00
|
|
|
if (has_flag(flags, Web::WebDriver::SessionFlags::Http)) {
|
2025-02-05 18:05:25 -03:00
|
|
|
// 1. Let strategy be the result of getting property "pageLoadStrategy" from capabilities. If strategy is a
|
|
|
|
|
// string, set the session's page loading strategy to strategy. Otherwise, set the page loading strategy to
|
|
|
|
|
// normal and set a property of capabilities with name "pageLoadStrategy" and value "normal".
|
2025-02-17 15:58:21 -03:00
|
|
|
if (auto strategy = capabilities.get_string("pageLoadStrategy"sv); strategy.has_value()) {
|
2025-02-05 18:05:25 -03:00
|
|
|
session->m_page_load_strategy = Web::WebDriver::page_load_strategy_from_string(*strategy);
|
|
|
|
|
session->web_content_connection().async_set_page_load_strategy(session->m_page_load_strategy);
|
|
|
|
|
} else {
|
|
|
|
|
capabilities.set("pageLoadStrategy"sv, "normal"sv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Let strictFileInteractability be the result of getting property "strictFileInteractability" from .
|
|
|
|
|
// capabilities. If strictFileInteractability is a boolean, set session's strict file interactability to
|
|
|
|
|
// strictFileInteractability.
|
|
|
|
|
if (auto strict_file_interactiblity = capabilities.get_bool("strictFileInteractability"sv); strict_file_interactiblity.has_value()) {
|
|
|
|
|
session->m_strict_file_interactiblity = *strict_file_interactiblity;
|
|
|
|
|
session->web_content_connection().async_set_strict_file_interactability(session->m_strict_file_interactiblity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Let timeouts be the result of getting a property "timeouts" from capabilities. If timeouts is not
|
|
|
|
|
// undefined, set session's session timeouts to timeouts.
|
|
|
|
|
if (auto timeouts = capabilities.get_object("timeouts"sv); timeouts.has_value()) {
|
|
|
|
|
MUST(session->set_timeouts(*timeouts));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Set a property on capabilities with name "timeouts" and value serialize the timeouts configuration with
|
|
|
|
|
// session's session timeouts.
|
|
|
|
|
capabilities.set("timeouts"sv, session->m_timeouts_configuration.value_or_lazy_evaluated([]() {
|
|
|
|
|
return Web::WebDriver::timeouts_object({});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: 10. Process any extension capabilities in capabilities in an implementation-defined manner.
|
|
|
|
|
|
|
|
|
|
// FIXME: 11. Run any WebDriver new session algorithm defined in external specifications, with arguments session, capabilities, and flags.
|
|
|
|
|
|
|
|
|
|
// 12. Append session to active sessions.
|
2025-02-07 11:04:45 -03:00
|
|
|
s_sessions.set(session->session_id(), session);
|
|
|
|
|
|
2025-02-07 11:16:20 -03:00
|
|
|
// 13. If flags contains "http", append session to active HTTP sessions.
|
|
|
|
|
if (has_flag(flags, Web::WebDriver::SessionFlags::Http))
|
|
|
|
|
s_http_sessions.set(session->session_id(), session);
|
2025-02-05 18:05:25 -03:00
|
|
|
|
|
|
|
|
// 14. Set the webdriver-active flag to true.
|
|
|
|
|
session->web_content_connection().async_set_is_webdriver_active(true);
|
|
|
|
|
|
|
|
|
|
return session;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 13:09:28 -03:00
|
|
|
Session::Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags)
|
2022-10-12 07:14:59 -03:00
|
|
|
: m_client(move(client))
|
2025-02-05 18:05:25 -03:00
|
|
|
, m_options(capabilities)
|
2025-02-07 11:04:45 -03:00
|
|
|
, m_session_id(move(session_id))
|
2025-02-07 13:09:28 -03:00
|
|
|
, m_session_flags(flags)
|
2026-06-04 14:56:43 -03:00
|
|
|
, m_event_loop(Core::EventLoop::current())
|
2022-10-12 07:14:59 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
Session::~Session() = default;
|
|
|
|
|
|
2025-02-07 11:16:20 -03:00
|
|
|
ErrorOr<NonnullRefPtr<Session>, Web::WebDriver::Error> Session::find_session(StringView session_id, Web::WebDriver::SessionFlags session_flags, AllowInvalidWindowHandle allow_invalid_window_handle)
|
2022-10-12 07:14:59 -03:00
|
|
|
{
|
2025-04-15 19:04:06 -03:00
|
|
|
auto& sessions = has_flag(session_flags, Web::WebDriver::SessionFlags::Http) ? s_http_sessions : s_sessions;
|
2025-02-07 11:16:20 -03:00
|
|
|
|
|
|
|
|
if (auto session = sessions.get(session_id); session.has_value()) {
|
2025-02-07 11:04:45 -03:00
|
|
|
if (allow_invalid_window_handle == AllowInvalidWindowHandle::No)
|
2026-06-13 11:06:49 -03:00
|
|
|
TRY(session.value()->wait_for_current_window_to_have_web_content_connection());
|
2023-03-19 07:36:32 -03:00
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
return *session.release_value();
|
2023-03-19 07:52:14 -03:00
|
|
|
}
|
2023-03-19 07:36:32 -03:00
|
|
|
|
2025-02-17 15:58:21 -03:00
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id"sv);
|
2025-02-07 11:04:45 -03:00
|
|
|
}
|
|
|
|
|
|
2025-02-07 11:16:20 -03:00
|
|
|
size_t Session::session_count(Web::WebDriver::SessionFlags session_flags)
|
|
|
|
|
{
|
|
|
|
|
if (has_flag(session_flags, Web::WebDriver::SessionFlags::Http))
|
|
|
|
|
return s_http_sessions.size();
|
|
|
|
|
return s_sessions.size();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
// https://w3c.github.io/webdriver/#dfn-close-the-session
|
|
|
|
|
void Session::close()
|
|
|
|
|
{
|
2025-02-07 11:16:20 -03:00
|
|
|
// 1. If session's HTTP flag is set, remove session from active HTTP sessions.
|
|
|
|
|
if (has_flag(session_flags(), Web::WebDriver::SessionFlags::Http))
|
|
|
|
|
s_http_sessions.remove(m_session_id);
|
2023-03-19 07:36:32 -03:00
|
|
|
|
2025-02-07 11:04:45 -03:00
|
|
|
// 2. Remove session from active sessions.
|
|
|
|
|
s_sessions.remove(m_session_id);
|
|
|
|
|
|
|
|
|
|
// 3. Perform the following substeps based on the remote end's type:
|
|
|
|
|
// -> Remote end is an endpoint node
|
|
|
|
|
// 1. If the list of active sessions is empty:
|
|
|
|
|
if (s_sessions.is_empty()) {
|
|
|
|
|
// 1. Set the webdriver-active flag to false
|
|
|
|
|
// NOTE: This is handled by the WebContent process.
|
|
|
|
|
|
|
|
|
|
// 2. Set the user prompt handler to null.
|
|
|
|
|
Web::WebDriver::set_user_prompt_handler({});
|
|
|
|
|
|
|
|
|
|
// FIXME: 3. Unset the accept insecure TLS flag.
|
2025-02-07 15:52:41 -03:00
|
|
|
|
|
|
|
|
// 4. Reset the has proxy configuration flag to its default value.
|
|
|
|
|
Web::WebDriver::reset_has_proxy_configuration();
|
2025-02-07 11:04:45 -03:00
|
|
|
|
|
|
|
|
// 5. Optionally, close all top-level browsing contexts, without prompting to unload.
|
2026-06-13 11:06:49 -03:00
|
|
|
for (auto& it : m_windows) {
|
|
|
|
|
if (!it.value.web_content_connection)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
it.value.web_content_connection->on_close = nullptr;
|
|
|
|
|
it.value.web_content_connection->on_driver_execution_complete = nullptr;
|
|
|
|
|
it.value.web_content_connection->on_did_set_window_handle = nullptr;
|
|
|
|
|
it.value.web_content_connection->on_did_start_window_replacement = nullptr;
|
|
|
|
|
it.value.web_content_connection->on_did_close_window = nullptr;
|
2025-02-07 11:04:45 -03:00
|
|
|
it.value.web_content_connection->close_session();
|
2026-06-13 11:06:49 -03:00
|
|
|
}
|
2025-02-07 11:04:45 -03:00
|
|
|
}
|
|
|
|
|
// -> Remote end is an intermediary node
|
|
|
|
|
// 1. Close the associated session. If this causes an error to occur, complete the remainder of this algorithm
|
|
|
|
|
// before returning the error.
|
|
|
|
|
|
|
|
|
|
// 4. Perform any implementation-specific cleanup steps.
|
2026-02-11 10:46:07 -03:00
|
|
|
for (auto& [_, connection] : m_pending_connections) {
|
|
|
|
|
connection->on_close = nullptr;
|
2026-06-13 11:06:49 -03:00
|
|
|
connection->on_driver_execution_complete = nullptr;
|
2026-02-11 10:46:07 -03:00
|
|
|
connection->on_did_set_window_handle = nullptr;
|
2026-06-13 11:06:49 -03:00
|
|
|
connection->on_did_start_window_replacement = nullptr;
|
|
|
|
|
connection->on_did_close_window = nullptr;
|
2026-02-11 10:46:07 -03:00
|
|
|
}
|
|
|
|
|
m_pending_connections.clear();
|
|
|
|
|
|
2024-11-17 12:11:13 -03:00
|
|
|
if (m_browser_process.has_value())
|
|
|
|
|
MUST(Core::System::kill(m_browser_process->pid(), SIGTERM));
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
m_web_content_mach_port_server = nullptr;
|
|
|
|
|
#else
|
|
|
|
|
if (!m_web_content_endpoint.is_empty())
|
|
|
|
|
MUST(Core::System::unlink(m_web_content_endpoint));
|
|
|
|
|
#endif
|
|
|
|
|
m_web_content_endpoint = {};
|
2025-02-07 11:04:45 -03:00
|
|
|
|
|
|
|
|
// 5. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
|
2022-10-20 10:17:23 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
ErrorOr<void> Session::accept_web_content_transport(NonnullOwnPtr<IPC::Transport> transport, NonnullRefPtr<ServerPromise> promise)
|
|
|
|
|
{
|
|
|
|
|
auto web_content_connection = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(transport))));
|
|
|
|
|
dbgln("WebDriver is connected to WebContent");
|
|
|
|
|
|
|
|
|
|
auto connection_id = m_next_pending_connection_id++;
|
|
|
|
|
// Publish the connection before the initial did_set_window_handle message can race in.
|
|
|
|
|
m_pending_connections.set(connection_id, web_content_connection);
|
|
|
|
|
|
|
|
|
|
web_content_connection->on_close = [this, promise, connection_id]() {
|
|
|
|
|
if (m_pending_connections.remove(connection_id)) {
|
|
|
|
|
dbgln_if(WEBDRIVER_DEBUG, "Pending connection {} closed before sending its handle", connection_id);
|
|
|
|
|
promise->reject(Error::from_string_literal("Window was closed before sending its handle"));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-13 11:06:49 -03:00
|
|
|
web_content_connection->on_did_set_window_handle = [this, promise, connection_id, connection = web_content_connection.ptr()](String window_handle) {
|
2026-03-22 15:59:08 -03:00
|
|
|
auto maybe_pending_connection = m_pending_connections.take(connection_id);
|
2026-06-13 11:06:49 -03:00
|
|
|
if (!maybe_pending_connection.has_value()) {
|
|
|
|
|
did_update_window_handle(move(window_handle), *connection);
|
2026-03-22 15:59:08 -03:00
|
|
|
return;
|
2026-06-13 11:06:49 -03:00
|
|
|
}
|
2026-03-22 15:59:08 -03:00
|
|
|
|
|
|
|
|
auto pending_connection = maybe_pending_connection.value();
|
|
|
|
|
|
|
|
|
|
dbgln_if(WEBDRIVER_DEBUG, "Window {} registered with WebDriver.", window_handle);
|
|
|
|
|
|
2026-06-13 11:06:49 -03:00
|
|
|
pending_connection->on_close = [this, connection]() {
|
|
|
|
|
dbgln_if(WEBDRIVER_DEBUG, "WebContent connection closed remotely.");
|
|
|
|
|
web_content_connection_closed(*connection);
|
|
|
|
|
};
|
|
|
|
|
pending_connection->on_did_start_window_replacement = [this, connection](String replaced_window_handle) {
|
|
|
|
|
did_start_window_replacement(replaced_window_handle, *connection);
|
|
|
|
|
};
|
|
|
|
|
pending_connection->on_did_close_window = [this, connection](String closed_window_handle) {
|
|
|
|
|
did_close_window(closed_window_handle, *connection);
|
2026-03-22 15:59:08 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pending_connection->async_set_page_load_strategy(m_page_load_strategy);
|
|
|
|
|
pending_connection->async_set_strict_file_interactability(m_strict_file_interactiblity);
|
|
|
|
|
pending_connection->async_set_user_prompt_handler(Web::WebDriver::user_prompt_handler());
|
|
|
|
|
if (m_timeouts_configuration.has_value())
|
|
|
|
|
pending_connection->async_set_timeouts(*m_timeouts_configuration);
|
|
|
|
|
|
2026-06-13 11:06:49 -03:00
|
|
|
if (auto window = m_windows.find(window_handle); window != m_windows.end()) {
|
|
|
|
|
window->value.web_content_connection = move(pending_connection);
|
|
|
|
|
window->value.is_awaiting_replacement = false;
|
|
|
|
|
} else {
|
|
|
|
|
m_windows.set(window_handle, Session::Window { window_handle, move(pending_connection) });
|
|
|
|
|
}
|
2026-03-22 15:59:08 -03:00
|
|
|
|
|
|
|
|
if (m_current_window_handle.is_empty())
|
|
|
|
|
m_current_window_handle = window_handle;
|
|
|
|
|
|
|
|
|
|
promise->resolve({});
|
|
|
|
|
};
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-13 11:06:49 -03:00
|
|
|
void Session::web_content_connection_closed(WebContentConnection const& connection)
|
|
|
|
|
{
|
|
|
|
|
Optional<String> closed_window_handle;
|
|
|
|
|
for (auto& window : m_windows) {
|
|
|
|
|
if (window.value.web_content_connection.ptr() != &connection)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (window.value.is_awaiting_replacement) {
|
|
|
|
|
window.value.web_content_connection = nullptr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closed_window_handle = window.key;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (closed_window_handle.has_value())
|
|
|
|
|
remove_window(*closed_window_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::did_update_window_handle(String window_handle, WebContentConnection const& connection)
|
|
|
|
|
{
|
|
|
|
|
Optional<String> previous_window_handle;
|
|
|
|
|
for (auto const& window : m_windows) {
|
|
|
|
|
if (window.value.web_content_connection.ptr() == &connection) {
|
|
|
|
|
previous_window_handle = window.key;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!previous_window_handle.has_value() || *previous_window_handle == window_handle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto maybe_window = m_windows.take(*previous_window_handle);
|
|
|
|
|
if (!maybe_window.has_value())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto window = maybe_window.release_value();
|
|
|
|
|
window.handle = window_handle;
|
|
|
|
|
window.is_awaiting_replacement = false;
|
|
|
|
|
|
|
|
|
|
if (auto existing_window = m_windows.find(window_handle); existing_window != m_windows.end()) {
|
|
|
|
|
existing_window->value.web_content_connection = move(window.web_content_connection);
|
|
|
|
|
existing_window->value.is_awaiting_replacement = false;
|
|
|
|
|
} else {
|
|
|
|
|
m_windows.set(window_handle, move(window));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_current_window_handle == *previous_window_handle)
|
|
|
|
|
m_current_window_handle = move(window_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::did_start_window_replacement(String const& window_handle, WebContentConnection const& connection)
|
|
|
|
|
{
|
|
|
|
|
auto window = m_windows.find(window_handle);
|
|
|
|
|
if (window == m_windows.end() || window->value.web_content_connection.ptr() != &connection)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
window->value.is_awaiting_replacement = true;
|
|
|
|
|
window->value.web_content_connection = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::mark_current_window_as_awaiting_replacement(WebContentConnection const& connection)
|
|
|
|
|
{
|
|
|
|
|
auto window = m_windows.find(m_current_window_handle);
|
|
|
|
|
if (window == m_windows.end() || window->value.web_content_connection.ptr() != &connection)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
window->value.is_awaiting_replacement = true;
|
|
|
|
|
window->value.web_content_connection = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::did_close_window(String const& window_handle, WebContentConnection const& connection)
|
|
|
|
|
{
|
|
|
|
|
auto window = m_windows.find(window_handle);
|
|
|
|
|
if (window == m_windows.end() || window->value.web_content_connection.ptr() != &connection)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
remove_window(window_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::remove_window(StringView window_handle)
|
|
|
|
|
{
|
|
|
|
|
m_windows.remove(window_handle);
|
|
|
|
|
|
|
|
|
|
if (m_current_window_handle == window_handle)
|
|
|
|
|
m_current_window_handle = "NoSuchWindowPleaseSelectANewOne"_string;
|
|
|
|
|
|
|
|
|
|
if (m_windows.is_empty())
|
|
|
|
|
close();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
ErrorOr<void> Session::create_server(NonnullRefPtr<ServerPromise> promise)
|
2022-10-12 07:14:59 -03:00
|
|
|
{
|
2025-08-24 01:03:37 -03:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
|
|
|
|
static_assert(IsSame<IPC::Transport, IPC::TransportSocketWindows>, "Need to handle other IPC transports here");
|
2026-03-19 15:42:05 -03:00
|
|
|
#elif defined(AK_OS_MACOS)
|
|
|
|
|
static_assert(IsSame<IPC::Transport, IPC::TransportMachPort>, "Need to handle other IPC transports here");
|
2025-08-24 01:03:37 -03:00
|
|
|
#else
|
2024-10-22 18:47:33 -03:00
|
|
|
static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
|
2025-08-24 01:03:37 -03:00
|
|
|
#endif
|
2024-10-22 18:47:33 -03:00
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
dbgln("Listening for WebDriver connection on {}", m_web_content_endpoint);
|
|
|
|
|
|
|
|
|
|
#if defined(AK_OS_MACOS)
|
2026-03-24 03:28:28 -03:00
|
|
|
m_web_content_mach_port_server = make<IPC::MachBootstrapListener>(m_web_content_endpoint);
|
2026-03-22 15:59:08 -03:00
|
|
|
if (!m_web_content_mach_port_server->is_initialized())
|
|
|
|
|
return Error::from_string_literal("Failed to initialize Mach port server for WebDriver");
|
|
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
m_web_content_mach_port_server->on_bootstrap_request = [this, promise](auto request) {
|
|
|
|
|
auto result = m_transport_bootstrap_server.handle_bootstrap_request(request.pid, move(request.reply_port));
|
|
|
|
|
if (result.is_error()) {
|
2026-06-04 14:56:43 -03:00
|
|
|
m_event_loop.deferred_invoke([promise, error = result.release_error()]() mutable {
|
2026-03-22 15:59:08 -03:00
|
|
|
promise->resolve(move(error));
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 01:42:42 -03:00
|
|
|
result.release_value().visit(
|
|
|
|
|
[](IPC::TransportBootstrapMachServer::ChildTransportHandled) {
|
2026-03-22 15:59:08 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
},
|
|
|
|
|
[this, promise](IPC::TransportBootstrapMachServer::OnDemandTransport& transport) {
|
2026-06-04 14:56:43 -03:00
|
|
|
m_event_loop.deferred_invoke([this, promise, transport = move(transport.ports)]() mutable {
|
2026-03-22 15:59:08 -03:00
|
|
|
if (auto result = accept_web_content_transport(make<IPC::Transport>(move(transport.receive_right), move(transport.send_right)), promise); result.is_error())
|
|
|
|
|
promise->resolve(result.release_error());
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-10-12 07:14:59 -03:00
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
return {};
|
|
|
|
|
#else
|
|
|
|
|
(void)Core::System::unlink(m_web_content_endpoint);
|
2023-03-23 11:33:29 -03:00
|
|
|
|
2025-08-11 07:29:33 -03:00
|
|
|
auto server = Core::LocalServer::construct();
|
2026-03-22 15:59:08 -03:00
|
|
|
server->listen(m_web_content_endpoint);
|
2022-11-08 12:18:11 -03:00
|
|
|
|
2022-11-18 22:09:53 -03:00
|
|
|
server->on_accept = [this, promise](auto client_socket) {
|
2026-03-19 15:42:05 -03:00
|
|
|
auto maybe_transport = IPC::Transport::from_socket(move(client_socket));
|
|
|
|
|
if (maybe_transport.is_error()) {
|
|
|
|
|
promise->resolve(maybe_transport.release_error());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-22 15:59:08 -03:00
|
|
|
if (auto result = accept_web_content_transport(maybe_transport.release_value(), promise); result.is_error())
|
|
|
|
|
promise->resolve(result.release_error());
|
2022-11-08 12:18:11 -03:00
|
|
|
};
|
|
|
|
|
|
2022-11-18 22:09:53 -03:00
|
|
|
server->on_accept_error = [promise](auto error) {
|
2023-07-07 19:00:27 -03:00
|
|
|
promise->resolve(move(error));
|
2022-11-08 12:18:11 -03:00
|
|
|
};
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
m_web_content_server = server;
|
|
|
|
|
return {};
|
|
|
|
|
#endif
|
2022-11-08 12:18:11 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:42:12 -03:00
|
|
|
ErrorOr<void> Session::start(LaunchBrowserCallback const& launch_browser_callback)
|
2022-11-08 12:18:11 -03:00
|
|
|
{
|
2025-08-11 05:58:49 -03:00
|
|
|
auto promise = ServerPromise::construct();
|
2022-11-08 12:18:11 -03:00
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
m_web_content_endpoint = ByteString::formatted("{}.{}", WebView::mach_server_name_for_process("WebDriver"sv, Core::System::getpid()), m_session_id);
|
|
|
|
|
#else
|
|
|
|
|
m_web_content_endpoint = ByteString::formatted("{}/webdriver/session_{}_{}", TRY(Core::StandardPaths::runtime_directory()), Core::System::getpid(), m_session_id);
|
|
|
|
|
#endif
|
|
|
|
|
TRY(create_server(promise));
|
2022-10-12 07:14:59 -03:00
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
m_browser_process = TRY(launch_browser_callback(m_web_content_endpoint, m_options.headless));
|
2022-10-12 07:14:59 -03:00
|
|
|
|
2022-11-09 12:56:12 -03:00
|
|
|
// FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
|
2022-11-08 12:18:11 -03:00
|
|
|
// errors received while accepting the Browser and WebContent sockets.
|
2022-12-29 10:53:05 -03:00
|
|
|
TRY(TRY(promise->await()));
|
2022-10-12 07:14:59 -03:00
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 10:37:16 -03:00
|
|
|
Web::WebDriver::Response Session::set_timeouts(JsonValue payload)
|
|
|
|
|
{
|
|
|
|
|
m_timeouts_configuration = TRY(web_content_connection().set_timeouts(move(payload)));
|
|
|
|
|
return JsonValue {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 19:57:36 -03:00
|
|
|
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
|
|
|
|
|
Web::WebDriver::Response Session::close_window()
|
|
|
|
|
{
|
2024-11-05 09:47:51 -03:00
|
|
|
// 3. Close the current top-level browsing context.
|
|
|
|
|
TRY(perform_async_action([&](auto& connection) {
|
|
|
|
|
return connection.close_window();
|
|
|
|
|
}));
|
|
|
|
|
|
2026-06-13 11:06:49 -03:00
|
|
|
// 4. If there are no more open top-level browsing contexts, then close the session.
|
|
|
|
|
auto closed_window_handle = m_current_window_handle;
|
|
|
|
|
remove_window(closed_window_handle);
|
2023-03-05 19:57:36 -03:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
2025-02-07 11:16:20 -03:00
|
|
|
// 4. If handle is equal to the associated window handle for some top-level browsing context, let context be the that
|
|
|
|
|
// browsing context, and set the current top-level browsing context with session and context.
|
2023-03-05 19:57:36 -03:00
|
|
|
// Otherwise, return error with error code no such window.
|
2026-06-13 11:06:49 -03:00
|
|
|
if (auto it = m_windows.find(handle); it != m_windows.end()) {
|
|
|
|
|
if (!it->value.web_content_connection)
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnknownError, "Window is waiting for a replacement WebContent process"sv);
|
|
|
|
|
|
2023-03-05 19:57:36 -03:00
|
|
|
m_current_window_handle = it->key;
|
2026-06-13 11:06:49 -03:00
|
|
|
} else {
|
2025-02-17 15:58:21 -03:00
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
|
2026-06-13 11:06:49 -03:00
|
|
|
}
|
2023-03-05 19:57:36 -03:00
|
|
|
|
2023-03-20 20:53:15 -03:00
|
|
|
// 5. Update any implementation-specific state that would result from the user selecting the current
|
|
|
|
|
// browsing context for interaction, without altering OS-level focus.
|
2024-09-14 14:02:44 -03:00
|
|
|
TRY(web_content_connection().switch_to_window(m_current_window_handle));
|
2023-03-05 19:57:36 -03:00
|
|
|
|
|
|
|
|
// 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()) {
|
2023-04-17 02:38:27 -03:00
|
|
|
handles.must_append(JsonValue(window_handle));
|
2023-03-05 19:57:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Return success with data handles.
|
|
|
|
|
return JsonValue { move(handles) };
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 17:53:29 -03:00
|
|
|
ErrorOr<void, Web::WebDriver::Error> Session::ensure_current_window_handle_is_valid() const
|
|
|
|
|
{
|
2026-06-13 11:06:49 -03:00
|
|
|
auto current_window = m_windows.get(m_current_window_handle);
|
|
|
|
|
if (!current_window.has_value())
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
|
|
|
|
|
|
|
|
|
|
if (!current_window->web_content_connection)
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnknownError, "Window is waiting for a replacement WebContent process"sv);
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<bool, Web::WebDriver::Error> Session::wait_for_current_window_to_have_web_content_connection()
|
|
|
|
|
{
|
|
|
|
|
m_event_loop.pump(Core::EventLoop::WaitMode::PollForEvents);
|
|
|
|
|
|
|
|
|
|
auto current_window = m_windows.get(m_current_window_handle);
|
|
|
|
|
if (!current_window.has_value())
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
|
|
|
|
|
|
|
|
|
|
if (current_window->web_content_connection)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Optional<u64> page_load_timeout = Web::WebDriver::TimeoutsConfiguration {}.page_load_timeout;
|
|
|
|
|
if (m_timeouts_configuration.has_value() && m_timeouts_configuration->is_object()) {
|
|
|
|
|
if (auto value = m_timeouts_configuration->as_object().get("pageLoad"sv); value.has_value()) {
|
|
|
|
|
if (value->is_null())
|
|
|
|
|
page_load_timeout = {};
|
|
|
|
|
else
|
|
|
|
|
page_load_timeout = value->get_integer<u64>().value_or(*page_load_timeout);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool timed_out = false;
|
|
|
|
|
RefPtr<Core::Timer> timer;
|
|
|
|
|
if (page_load_timeout.has_value()) {
|
|
|
|
|
auto timer_interval = *page_load_timeout > NumericLimits<int>::max() ? NumericLimits<int>::max() : static_cast<int>(*page_load_timeout);
|
|
|
|
|
timer = Core::Timer::create_single_shot(timer_interval, [&timed_out] {
|
|
|
|
|
timed_out = true;
|
|
|
|
|
});
|
|
|
|
|
timer->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::EventLoop::current().spin_until([this, &timed_out] {
|
|
|
|
|
auto current_window = m_windows.get(m_current_window_handle);
|
|
|
|
|
return !current_window.has_value() || current_window->web_content_connection || timed_out;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (timer)
|
|
|
|
|
timer->stop();
|
|
|
|
|
|
|
|
|
|
if (timed_out)
|
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::Timeout, "Timed out waiting for replacement WebContent process"sv);
|
|
|
|
|
|
|
|
|
|
TRY(ensure_current_window_handle_is_valid());
|
|
|
|
|
return true;
|
2024-09-27 17:53:29 -03:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 07:14:59 -03:00
|
|
|
}
|