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 "BrowserConnection.h"
|
|
|
|
|
|
#include "Client.h"
|
2022-11-11 13:58:20 -05:00
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
|
|
#include <AK/JsonParser.h>
|
2022-11-02 09:33:24 -04:00
|
|
|
|
#include <AK/NumericLimits.h>
|
2022-10-19 16:53:06 +02:00
|
|
|
|
#include <AK/Time.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
#include <LibCore/LocalServer.h>
|
|
|
|
|
|
#include <LibCore/Stream.h>
|
|
|
|
|
|
#include <LibCore/System.h>
|
2022-11-02 09:33:24 -04:00
|
|
|
|
#include <LibGfx/Point.h>
|
2022-11-02 10:19:58 -04:00
|
|
|
|
#include <LibGfx/Rect.h>
|
2022-11-02 09:33:24 -04:00
|
|
|
|
#include <LibGfx/Size.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
|
|
|
|
|
|
|
Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
|
|
|
|
|
|
: m_client(move(client))
|
|
|
|
|
|
, m_id(session_id)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_started) {
|
|
|
|
|
|
auto error = stop();
|
|
|
|
|
|
if (error.is_error()) {
|
|
|
|
|
|
warnln("Failed to stop session {}: {}", m_id, error.error());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-08 10:10:27 -05:00
|
|
|
|
ErrorOr<Session::Window*, Web::WebDriver::Error> Session::current_window()
|
2022-10-20 14:17:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto window = m_windows.get(m_current_window_handle);
|
|
|
|
|
|
if (!window.has_value())
|
2022-11-08 10:10:27 -05:00
|
|
|
|
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
|
2022-10-20 14:17:23 +01:00
|
|
|
|
return window.release_value();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-08 10:10:27 -05:00
|
|
|
|
ErrorOr<void, Web::WebDriver::Error> Session::check_for_open_top_level_browsing_context_or_return_error()
|
2022-10-20 14:17:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
(void)TRY(current_window());
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-08 10:18:11 -05:00
|
|
|
|
ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, ServerType type, NonnullRefPtr<ServerPromise> promise)
|
2022-10-12 11:14:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
dbgln("Listening for WebDriver connection on {}", socket_path);
|
|
|
|
|
|
|
2022-11-08 10:18:11 -05:00
|
|
|
|
auto server = TRY(Core::LocalServer::try_create());
|
|
|
|
|
|
server->listen(socket_path);
|
|
|
|
|
|
|
|
|
|
|
|
server->on_accept = [this, type, promise](auto client_socket) mutable {
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
case ServerType::Browser: {
|
|
|
|
|
|
auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) BrowserConnection(move(client_socket), m_client, session_id()));
|
|
|
|
|
|
if (maybe_connection.is_error()) {
|
|
|
|
|
|
promise->resolve(maybe_connection.release_error());
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbgln("WebDriver is connected to Browser socket");
|
|
|
|
|
|
m_browser_connection = maybe_connection.release_value();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case ServerType::WebContent: {
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbgln("WebDriver is connected to WebContent socket");
|
|
|
|
|
|
m_web_content_connection = maybe_connection.release_value();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_browser_connection && m_web_content_connection)
|
|
|
|
|
|
promise->resolve({});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
server->on_accept_error = [promise](auto error) mutable {
|
|
|
|
|
|
promise->resolve(move(error));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> Session::start()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto promise = TRY(ServerPromise::try_create());
|
|
|
|
|
|
|
|
|
|
|
|
auto browser_socket_path = String::formatted("/tmp/webdriver/browser_{}_{}", getpid(), m_id);
|
|
|
|
|
|
auto browser_server = TRY(create_server(browser_socket_path, ServerType::Browser, promise));
|
|
|
|
|
|
|
|
|
|
|
|
auto web_content_socket_path = String::formatted("/tmp/webdriver/content_{}_{}", getpid(), m_id);
|
|
|
|
|
|
auto web_content_server = TRY(create_server(web_content_socket_path, ServerType::WebContent, promise));
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
2022-11-08 10:18:11 -05:00
|
|
|
|
char const* argv[] = {
|
|
|
|
|
|
"/bin/Browser",
|
|
|
|
|
|
"--webdriver-browser-path",
|
|
|
|
|
|
browser_socket_path.characters(),
|
|
|
|
|
|
"--webdriver-content-path",
|
|
|
|
|
|
web_content_socket_path.characters(),
|
|
|
|
|
|
nullptr,
|
|
|
|
|
|
};
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
|
|
|
|
|
TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
m_windows.set("main", make<Session::Window>("main", true));
|
|
|
|
|
|
m_current_window_handle = "main";
|
|
|
|
|
|
|
|
|
|
|
|
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-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.
|
|
|
|
|
|
m_web_content_connection->close_session();
|
|
|
|
|
|
m_web_content_connection = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Remove the current session from active sessions.
|
|
|
|
|
|
// NOTE: Handled by WebDriver::Client.
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Perform any implementation-specific cleanup steps.
|
2022-10-12 11:14:59 +01:00
|
|
|
|
m_browser_connection->async_quit();
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-19 17:46:31 +02:00
|
|
|
|
// 9.1 Get Timeouts, https://w3c.github.io/webdriver/#dfn-get-timeouts
|
|
|
|
|
|
JsonObject Session::get_timeouts()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. Let timeouts be the timeouts object for session’s timeouts configuration
|
|
|
|
|
|
auto timeouts = timeouts_object(m_timeouts_configuration);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Return success with data timeouts.
|
|
|
|
|
|
return timeouts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-19 18:07:30 +02:00
|
|
|
|
// 9.2 Set Timeouts, https://w3c.github.io/webdriver/#dfn-set-timeouts
|
2022-11-08 09:42:36 -05:00
|
|
|
|
Web::WebDriver::Response Session::set_timeouts(JsonValue const& payload)
|
2022-10-19 18:07:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Let timeouts be the result of trying to JSON deserialize as a timeouts configuration the request’s parameters.
|
|
|
|
|
|
auto timeouts = TRY(json_deserialize_as_a_timeouts_configuration(payload));
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Make the session timeouts the new timeouts.
|
|
|
|
|
|
m_timeouts_configuration = move(timeouts);
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return success with data null.
|
|
|
|
|
|
return JsonValue {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-19 19:39:54 +02:00
|
|
|
|
// 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
|
2022-11-08 09:42:36 -05:00
|
|
|
|
Web::WebDriver::Response Session::get_window_handle()
|
2022-10-19 19:39:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
|
2022-10-20 14:17:23 +01:00
|
|
|
|
TRY(check_for_open_top_level_browsing_context_or_return_error());
|
2022-10-19 19:39:54 +02:00
|
|
|
|
|
|
|
|
|
|
// 2. Return success with data being the window handle associated with the current top-level browsing context.
|
2022-11-08 09:42:36 -05:00
|
|
|
|
return JsonValue { m_current_window_handle };
|
2022-10-19 19:39:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-17 16:19:45 +02:00
|
|
|
|
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
|
2022-11-08 10:10:27 -05:00
|
|
|
|
ErrorOr<void, Variant<Web::WebDriver::Error, Error>> Session::close_window()
|
2022-10-15 18:18:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
|
2022-10-20 14:17:23 +01:00
|
|
|
|
TRY(check_for_open_top_level_browsing_context_or_return_error());
|
2022-10-15 18:18:35 +01:00
|
|
|
|
|
2022-10-17 16:19:45 +02:00
|
|
|
|
// 2. Close the current top-level browsing context.
|
|
|
|
|
|
m_windows.remove(m_current_window_handle);
|
2022-10-15 18:18:35 +01:00
|
|
|
|
|
2022-10-17 16:19:45 +02:00
|
|
|
|
// 3. If there are no more open top-level browsing contexts, then close the session.
|
|
|
|
|
|
if (m_windows.is_empty()) {
|
|
|
|
|
|
auto result = stop();
|
|
|
|
|
|
if (result.is_error()) {
|
2022-11-08 10:10:27 -05:00
|
|
|
|
return Variant<Web::WebDriver::Error, Error>(result.release_error());
|
2022-10-17 16:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-15 18:18:35 +01:00
|
|
|
|
|
2022-10-17 16:19:45 +02:00
|
|
|
|
return {};
|
2022-10-15 18:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-19 21:41:09 +02:00
|
|
|
|
// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
|
2022-11-08 09:42:36 -05:00
|
|
|
|
Web::WebDriver::Response Session::get_window_handles() const
|
2022-10-19 21:41:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Let handles be a JSON List.
|
|
|
|
|
|
auto handles = JsonArray {};
|
|
|
|
|
|
|
|
|
|
|
|
// 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(window_handle);
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return success with data handles.
|
2022-11-08 09:42:36 -05:00
|
|
|
|
return JsonValue { handles };
|
2022-10-19 21:41:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
|
}
|