WebDriver: Actually create a UUID for session_id

This commit is contained in:
Feng Yu 2025-01-03 11:07:56 -08:00 committed by Tim Flynn
parent ed83bb75e9
commit 57f776fcb7
Notes: github-actions[bot] 2025-01-03 21:16:49 +00:00
4 changed files with 13 additions and 19 deletions

View file

@ -13,13 +13,13 @@
#include <AK/JsonValue.h> #include <AK/JsonValue.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/Timer.h> #include <LibCore/Timer.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/WebDriver/Capabilities.h> #include <LibWeb/WebDriver/Capabilities.h>
#include <WebDriver/Client.h> #include <WebDriver/Client.h>
namespace WebDriver { namespace WebDriver {
Atomic<unsigned> Client::s_next_session_id; HashMap<String, NonnullRefPtr<Session>> Client::s_sessions;
HashMap<unsigned, NonnullRefPtr<Session>> Client::s_sessions;
ErrorOr<NonnullRefPtr<Client>> Client::try_create(NonnullOwnPtr<Core::BufferedTCPSocket> socket, LaunchBrowserCallbacks callbacks, Core::EventReceiver* parent) ErrorOr<NonnullRefPtr<Client>> Client::try_create(NonnullOwnPtr<Core::BufferedTCPSocket> socket, LaunchBrowserCallbacks callbacks, Core::EventReceiver* parent)
{ {
@ -40,11 +40,7 @@ Client::~Client() = default;
ErrorOr<NonnullRefPtr<Session>, Web::WebDriver::Error> Client::find_session_with_id(StringView session_id, AllowInvalidWindowHandle allow_invalid_window_handle) ErrorOr<NonnullRefPtr<Session>, Web::WebDriver::Error> Client::find_session_with_id(StringView session_id, AllowInvalidWindowHandle allow_invalid_window_handle)
{ {
auto session_id_or_error = session_id.to_number<unsigned>(); if (auto session = s_sessions.get(session_id); session.has_value()) {
if (!session_id_or_error.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id");
if (auto session = s_sessions.get(*session_id_or_error); session.has_value()) {
if (allow_invalid_window_handle == AllowInvalidWindowHandle::No) if (allow_invalid_window_handle == AllowInvalidWindowHandle::No)
TRY(session.value()->ensure_current_window_handle_is_valid()); TRY(session.value()->ensure_current_window_handle_is_valid());
@ -54,7 +50,7 @@ ErrorOr<NonnullRefPtr<Session>, Web::WebDriver::Error> Client::find_session_with
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id"); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSessionId, "Invalid session id");
} }
void Client::close_session(unsigned session_id) void Client::close_session(String const& session_id)
{ {
if (s_sessions.remove(session_id)) if (s_sessions.remove(session_id))
dbgln_if(WEBDRIVER_DEBUG, "Shut down session {}", session_id); dbgln_if(WEBDRIVER_DEBUG, "Shut down session {}", session_id);
@ -89,8 +85,7 @@ Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonVal
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::SessionNotCreated, "Could not match capabilities"sv); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::SessionNotCreated, "Could not match capabilities"sv);
// 6. Let session id be the result of generating a UUID. // 6. Let session id be the result of generating a UUID.
// FIXME: Actually create a UUID. auto session_id = MUST(Web::Crypto::generate_random_uuid());
auto session_id = Client::s_next_session_id++;
// 7. Let session be a new session with the session ID of session id. // 7. Let session be a new session with the session ID of session id.
Web::WebDriver::LadybirdOptions options { capabilities.as_object() }; Web::WebDriver::LadybirdOptions options { capabilities.as_object() };
@ -118,7 +113,7 @@ Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonVal
JsonObject body; JsonObject body;
// "sessionId" // "sessionId"
// session id // session id
body.set("sessionId", ByteString::number(session_id)); body.set("sessionId", JsonValue { session_id });
// "capabilities" // "capabilities"
// capabilities // capabilities
body.set("capabilities", move(capabilities)); body.set("capabilities", move(capabilities));

View file

@ -30,7 +30,7 @@ public:
static ErrorOr<NonnullRefPtr<Client>> try_create(NonnullOwnPtr<Core::BufferedTCPSocket>, LaunchBrowserCallbacks, Core::EventReceiver* parent); static ErrorOr<NonnullRefPtr<Client>> try_create(NonnullOwnPtr<Core::BufferedTCPSocket>, LaunchBrowserCallbacks, Core::EventReceiver* parent);
virtual ~Client() override; virtual ~Client() override;
void close_session(unsigned session_id); void close_session(String const& session_id);
private: private:
Client(NonnullOwnPtr<Core::BufferedTCPSocket>, LaunchBrowserCallbacks, Core::EventReceiver* parent); Client(NonnullOwnPtr<Core::BufferedTCPSocket>, LaunchBrowserCallbacks, Core::EventReceiver* parent);
@ -104,8 +104,7 @@ private:
virtual Web::WebDriver::Response take_element_screenshot(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response take_element_screenshot(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response print_page(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response print_page(Web::WebDriver::Parameters parameters, JsonValue payload) override;
static HashMap<unsigned, NonnullRefPtr<Session>> s_sessions; static HashMap<String, NonnullRefPtr<Session>> s_sessions;
static Atomic<unsigned> s_next_session_id;
LaunchBrowserCallbacks m_callbacks; LaunchBrowserCallbacks m_callbacks;
}; };

View file

@ -19,10 +19,10 @@
namespace WebDriver { namespace WebDriver {
Session::Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options) Session::Session(String session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options)
: m_client(move(client)) : m_client(move(client))
, m_options(move(options)) , m_options(move(options))
, m_id(session_id) , m_id(move(session_id))
{ {
} }

View file

@ -29,12 +29,12 @@ struct LaunchBrowserCallbacks;
class Session : public RefCounted<Session> { class Session : public RefCounted<Session> {
public: public:
Session(unsigned session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options); Session(String session_id, NonnullRefPtr<Client> client, Web::WebDriver::LadybirdOptions options);
~Session(); ~Session();
void initialize_from_capabilities(JsonObject&); void initialize_from_capabilities(JsonObject&);
unsigned session_id() const { return m_id; } String session_id() const { return m_id; }
struct Window { struct Window {
String handle; String handle;
@ -90,7 +90,7 @@ private:
Web::WebDriver::LadybirdOptions m_options; Web::WebDriver::LadybirdOptions m_options;
bool m_started { false }; bool m_started { false };
unsigned m_id { 0 }; String m_id;
HashMap<String, Window> m_windows; HashMap<String, Window> m_windows;
String m_current_window_handle; String m_current_window_handle;