2022-10-12 11:14:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
|
2022-10-19 16:50:16 +02:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2024-09-29 09:40:17 -04:00
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
|
2022-10-12 11:14:59 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2023-03-07 10:36:39 -05:00
|
|
|
#include <AK/RefCounted.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
#include <AK/RefPtr.h>
|
2024-11-01 07:42:21 -04:00
|
|
|
#include <AK/ScopeGuard.h>
|
2023-03-06 01:57:36 +03:00
|
|
|
#include <AK/String.h>
|
2024-11-01 07:42:21 -04:00
|
|
|
#include <LibCore/EventLoop.h>
|
2022-11-08 10:18:11 -05:00
|
|
|
#include <LibCore/Promise.h>
|
2022-11-08 10:10:27 -05:00
|
|
|
#include <LibWeb/WebDriver/Error.h>
|
2022-11-08 09:42:36 -05:00
|
|
|
#include <LibWeb/WebDriver/Response.h>
|
2022-11-08 10:18:11 -05:00
|
|
|
#include <WebDriver/WebContentConnection.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
2022-12-15 08:46:01 -05:00
|
|
|
struct LaunchBrowserCallbacks;
|
|
|
|
|
2023-03-07 10:36:39 -05:00
|
|
|
class Session : public RefCounted<Session> {
|
2022-10-12 11:14:59 +01:00
|
|
|
public:
|
2022-11-22 07:38:07 -05:00
|
|
|
Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options);
|
2022-10-12 11:14:59 +01:00
|
|
|
~Session();
|
|
|
|
|
|
|
|
unsigned session_id() const { return m_id; }
|
|
|
|
|
2023-03-06 01:57:36 +03:00
|
|
|
struct Window {
|
|
|
|
String handle;
|
|
|
|
NonnullRefPtr<WebContentConnection> web_content_connection;
|
|
|
|
};
|
|
|
|
|
|
|
|
WebContentConnection& web_content_connection() const
|
2022-11-08 10:42:12 -05:00
|
|
|
{
|
2023-03-07 09:16:01 -05:00
|
|
|
auto current_window = m_windows.get(m_current_window_handle);
|
|
|
|
VERIFY(current_window.has_value());
|
|
|
|
|
|
|
|
return current_window->web_content_connection;
|
2023-03-06 01:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
String const& current_window_handle() const
|
|
|
|
{
|
|
|
|
return m_current_window_handle;
|
2022-11-08 10:42:12 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 14:42:23 +01:00
|
|
|
bool has_window_handle(StringView handle) const { return m_windows.contains(handle); }
|
|
|
|
|
2022-12-15 08:46:01 -05:00
|
|
|
ErrorOr<void> start(LaunchBrowserCallbacks const&);
|
2023-03-06 01:57:36 +03:00
|
|
|
Web::WebDriver::Response close_window();
|
|
|
|
Web::WebDriver::Response switch_to_window(StringView);
|
|
|
|
Web::WebDriver::Response get_window_handles() const;
|
2024-09-27 16:53:29 -04:00
|
|
|
ErrorOr<void, Web::WebDriver::Error> ensure_current_window_handle_is_valid() const;
|
2022-10-12 11:14:59 +01:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
template<typename Action>
|
|
|
|
Web::WebDriver::Response perform_async_action(Action&& action)
|
|
|
|
{
|
|
|
|
Optional<Web::WebDriver::Response> response;
|
|
|
|
auto& connection = web_content_connection();
|
2024-10-28 23:37:11 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
ScopeGuard guard { [&]() { connection.on_driver_execution_complete = nullptr; } };
|
|
|
|
connection.on_driver_execution_complete = [&](auto result) { response = move(result); };
|
2024-10-30 15:26:04 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
TRY(action(connection));
|
2024-09-29 09:40:17 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
Core::EventLoop::current().spin_until([&]() {
|
|
|
|
return response.has_value();
|
|
|
|
});
|
2024-10-25 12:31:07 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
return response.release_value();
|
|
|
|
}
|
2024-10-30 20:29:26 -04:00
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
private:
|
2022-11-08 10:18:11 -05:00
|
|
|
using ServerPromise = Core::Promise<ErrorOr<void>>;
|
2022-12-15 10:40:11 -05:00
|
|
|
ErrorOr<NonnullRefPtr<Core::LocalServer>> create_server(NonnullRefPtr<ServerPromise> promise);
|
2022-11-08 10:18:11 -05:00
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
NonnullRefPtr<Client> m_client;
|
2022-11-22 07:38:07 -05:00
|
|
|
Web::WebDriver::LadybirdOptions m_options;
|
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
bool m_started { false };
|
|
|
|
unsigned m_id { 0 };
|
2022-11-22 07:38:07 -05:00
|
|
|
|
2023-03-06 01:57:36 +03:00
|
|
|
HashMap<String, Window> m_windows;
|
|
|
|
String m_current_window_handle;
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Optional<ByteString> m_web_content_socket_path;
|
2022-11-11 14:14:58 -05:00
|
|
|
Optional<pid_t> m_browser_pid;
|
2023-03-06 01:45:30 +03:00
|
|
|
|
|
|
|
RefPtr<Core::LocalServer> m_web_content_server;
|
2022-10-12 11:14:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|