From de34351ba84df47dc1063f124fe1fcb697cb381b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 7 Feb 2025 11:09:28 -0500 Subject: [PATCH] LibWeb+WebDriver: Convert WebDriver session flags to an enumeration Rather than a list of strings, this will be easier to deal with as a bitwise enumeration. --- Libraries/LibWeb/WebDriver/Capabilities.cpp | 6 +++--- Libraries/LibWeb/WebDriver/Capabilities.h | 9 ++++++++- Services/WebDriver/Client.cpp | 2 +- Services/WebDriver/Session.cpp | 10 +++++----- Services/WebDriver/Session.h | 6 +++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Libraries/LibWeb/WebDriver/Capabilities.cpp b/Libraries/LibWeb/WebDriver/Capabilities.cpp index b9f494299d6..1442b990197 100644 --- a/Libraries/LibWeb/WebDriver/Capabilities.cpp +++ b/Libraries/LibWeb/WebDriver/Capabilities.cpp @@ -263,7 +263,7 @@ static bool matches_platform_name(StringView requested_platform_name, StringView } // https://w3c.github.io/webdriver/#dfn-matching-capabilities -static JsonValue match_capabilities(JsonObject const& capabilities, ReadonlySpan flags) +static JsonValue match_capabilities(JsonObject const& capabilities, SessionFlags flags) { static auto browser_name = StringView { BROWSER_NAME, strlen(BROWSER_NAME) }.to_lowercase_string(); static auto platform_name = StringView { OS_STRING, strlen(OS_STRING) }.to_lowercase_string(); @@ -294,7 +294,7 @@ static JsonValue match_capabilities(JsonObject const& capabilities, ReadonlySpan matched_capabilities.set("userAgent"sv, Web::default_user_agent); // 2. If flags contains "http", add the following entries to matched capabilities: - if (flags.contains_slow("http"sv)) { + if (has_flag(flags, SessionFlags::Http)) { // "strictFileInteractability" // Boolean initially set to false, indicating that interactabilty checks will be applied to . matched_capabilities.set("strictFileInteractability"sv, false); @@ -377,7 +377,7 @@ static JsonValue match_capabilities(JsonObject const& capabilities, ReadonlySpan } // https://w3c.github.io/webdriver/#dfn-capabilities-processing -Response process_capabilities(JsonValue const& parameters, ReadonlySpan flags) +Response process_capabilities(JsonValue const& parameters, SessionFlags flags) { if (!parameters.is_object()) return Error::from_code(ErrorCode::InvalidArgument, "Session parameters is not an object"sv); diff --git a/Libraries/LibWeb/WebDriver/Capabilities.h b/Libraries/LibWeb/WebDriver/Capabilities.h index 412f7e241e0..e8e4b5d8f5b 100644 --- a/Libraries/LibWeb/WebDriver/Capabilities.h +++ b/Libraries/LibWeb/WebDriver/Capabilities.h @@ -6,12 +6,19 @@ #pragma once +#include #include #include #include namespace Web::WebDriver { +enum class SessionFlags { + Default = 0x0, + Http = 0x1, +}; +AK_ENUM_BITWISE_OPERATORS(SessionFlags); + // https://w3c.github.io/webdriver/#dfn-page-load-strategy enum class PageLoadStrategy { None, @@ -42,6 +49,6 @@ struct LadybirdOptions { bool headless { false }; }; -Response process_capabilities(JsonValue const& parameters, ReadonlySpan flags); +Response process_capabilities(JsonValue const& parameters, SessionFlags flags); } diff --git a/Services/WebDriver/Client.cpp b/Services/WebDriver/Client.cpp index 678ed325295..5cad543c3e9 100644 --- a/Services/WebDriver/Client.cpp +++ b/Services/WebDriver/Client.cpp @@ -54,7 +54,7 @@ Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonVal // commands may be forwarded to this associated session on subsequent commands. // 3. Let flags be a set containing "http". - static constexpr Array flags { "http"sv }; + static constexpr auto flags = Web::WebDriver::SessionFlags::Http; // 4. Let capabilities be the result of trying to process capabilities with parameters and flags. auto capabilities = TRY(Web::WebDriver::process_capabilities(payload, flags)); diff --git a/Services/WebDriver/Session.cpp b/Services/WebDriver/Session.cpp index 9aec7ac512d..02f471c7e61 100644 --- a/Services/WebDriver/Session.cpp +++ b/Services/WebDriver/Session.cpp @@ -25,13 +25,13 @@ namespace WebDriver { static HashMap> s_sessions; // https://w3c.github.io/webdriver/#dfn-create-a-session -ErrorOr> Session::create(NonnullRefPtr client, JsonObject& capabilities, ReadonlySpan flags) +ErrorOr> Session::create(NonnullRefPtr client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags) { // 1. Let session id be the result of generating a UUID. auto session_id = MUST(Web::Crypto::generate_random_uuid()); // 2. Let session be a new session with session ID session id, and HTTP flag flags contains "http". - auto session = adopt_ref(*new Session(client, capabilities, move(session_id), flags.contains_slow("http"sv))); + auto session = adopt_ref(*new Session(client, capabilities, move(session_id), flags)); TRY(session->start(client->launch_browser_callbacks())); // FIXME: 3. Let proxy be the result of getting property "proxy" from capabilities and run the substeps of the first matching statement: @@ -59,7 +59,7 @@ ErrorOr> Session::create(NonnullRefPtr client, Js capabilities.set("unhandledPromptBehavior"sv, move(serialized_user_prompt_handler)); // 9. If flags contains "http": - if (flags.contains_slow("http"sv)) { + if (has_flag(flags, Web::WebDriver::SessionFlags::Http)) { // 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". @@ -106,11 +106,11 @@ ErrorOr> Session::create(NonnullRefPtr client, Js return session; } -Session::Session(NonnullRefPtr client, JsonObject const& capabilities, String session_id, bool http) +Session::Session(NonnullRefPtr client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags) : m_client(move(client)) , m_options(capabilities) , m_session_id(move(session_id)) - , m_http(http) + , m_session_flags(flags) { } diff --git a/Services/WebDriver/Session.h b/Services/WebDriver/Session.h index c18aa25104b..a204312b91f 100644 --- a/Services/WebDriver/Session.h +++ b/Services/WebDriver/Session.h @@ -29,7 +29,7 @@ struct LaunchBrowserCallbacks; class Session : public RefCounted { public: - static ErrorOr> create(NonnullRefPtr client, JsonObject& capabilities, ReadonlySpan flags); + static ErrorOr> create(NonnullRefPtr client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags); ~Session(); enum class AllowInvalidWindowHandle { @@ -83,7 +83,7 @@ public: } private: - Session(NonnullRefPtr client, JsonObject const& capabilities, String session_id, bool http); + Session(NonnullRefPtr client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags); ErrorOr start(LaunchBrowserCallbacks const&); @@ -94,7 +94,7 @@ private: Web::WebDriver::LadybirdOptions m_options; String m_session_id; - bool m_http { false }; + Web::WebDriver::SessionFlags m_session_flags { Web::WebDriver::SessionFlags::Default }; HashMap m_windows; String m_current_window_handle;