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.
This commit is contained in:
Timothy Flynn 2025-02-07 11:09:28 -05:00 committed by Tim Flynn
commit de34351ba8
Notes: github-actions[bot] 2025-02-10 16:35:17 +00:00
5 changed files with 20 additions and 13 deletions

View file

@ -263,7 +263,7 @@ static bool matches_platform_name(StringView requested_platform_name, StringView
} }
// https://w3c.github.io/webdriver/#dfn-matching-capabilities // https://w3c.github.io/webdriver/#dfn-matching-capabilities
static JsonValue match_capabilities(JsonObject const& capabilities, ReadonlySpan<StringView> 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 browser_name = StringView { BROWSER_NAME, strlen(BROWSER_NAME) }.to_lowercase_string();
static auto platform_name = StringView { OS_STRING, strlen(OS_STRING) }.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); matched_capabilities.set("userAgent"sv, Web::default_user_agent);
// 2. If flags contains "http", add the following entries to matched capabilities: // 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" // "strictFileInteractability"
// Boolean initially set to false, indicating that interactabilty checks will be applied to <input type=file>. // Boolean initially set to false, indicating that interactabilty checks will be applied to <input type=file>.
matched_capabilities.set("strictFileInteractability"sv, false); 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 // https://w3c.github.io/webdriver/#dfn-capabilities-processing
Response process_capabilities(JsonValue const& parameters, ReadonlySpan<StringView> flags) Response process_capabilities(JsonValue const& parameters, SessionFlags flags)
{ {
if (!parameters.is_object()) if (!parameters.is_object())
return Error::from_code(ErrorCode::InvalidArgument, "Session parameters is not an object"sv); return Error::from_code(ErrorCode::InvalidArgument, "Session parameters is not an object"sv);

View file

@ -6,12 +6,19 @@
#pragma once #pragma once
#include <AK/EnumBits.h>
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibWeb/WebDriver/Response.h> #include <LibWeb/WebDriver/Response.h>
namespace Web::WebDriver { namespace Web::WebDriver {
enum class SessionFlags {
Default = 0x0,
Http = 0x1,
};
AK_ENUM_BITWISE_OPERATORS(SessionFlags);
// https://w3c.github.io/webdriver/#dfn-page-load-strategy // https://w3c.github.io/webdriver/#dfn-page-load-strategy
enum class PageLoadStrategy { enum class PageLoadStrategy {
None, None,
@ -42,6 +49,6 @@ struct LadybirdOptions {
bool headless { false }; bool headless { false };
}; };
Response process_capabilities(JsonValue const& parameters, ReadonlySpan<StringView> flags); Response process_capabilities(JsonValue const& parameters, SessionFlags flags);
} }

View file

@ -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. // commands may be forwarded to this associated session on subsequent commands.
// 3. Let flags be a set containing "http". // 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. // 4. Let capabilities be the result of trying to process capabilities with parameters and flags.
auto capabilities = TRY(Web::WebDriver::process_capabilities(payload, flags)); auto capabilities = TRY(Web::WebDriver::process_capabilities(payload, flags));

View file

@ -25,13 +25,13 @@ namespace WebDriver {
static HashMap<String, NonnullRefPtr<Session>> s_sessions; static HashMap<String, NonnullRefPtr<Session>> s_sessions;
// https://w3c.github.io/webdriver/#dfn-create-a-session // https://w3c.github.io/webdriver/#dfn-create-a-session
ErrorOr<NonnullRefPtr<Session>> Session::create(NonnullRefPtr<Client> client, JsonObject& capabilities, ReadonlySpan<StringView> flags) ErrorOr<NonnullRefPtr<Session>> Session::create(NonnullRefPtr<Client> client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags)
{ {
// 1. Let session id be the result of generating a UUID. // 1. Let session id be the result of generating a UUID.
auto session_id = MUST(Web::Crypto::generate_random_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". // 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())); 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: // 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<NonnullRefPtr<Session>> Session::create(NonnullRefPtr<Client> client, Js
capabilities.set("unhandledPromptBehavior"sv, move(serialized_user_prompt_handler)); capabilities.set("unhandledPromptBehavior"sv, move(serialized_user_prompt_handler));
// 9. If flags contains "http": // 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 // 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 // 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". // normal and set a property of capabilities with name "pageLoadStrategy" and value "normal".
@ -106,11 +106,11 @@ ErrorOr<NonnullRefPtr<Session>> Session::create(NonnullRefPtr<Client> client, Js
return session; return session;
} }
Session::Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, bool http) Session::Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags)
: m_client(move(client)) : m_client(move(client))
, m_options(capabilities) , m_options(capabilities)
, m_session_id(move(session_id)) , m_session_id(move(session_id))
, m_http(http) , m_session_flags(flags)
{ {
} }

View file

@ -29,7 +29,7 @@ struct LaunchBrowserCallbacks;
class Session : public RefCounted<Session> { class Session : public RefCounted<Session> {
public: public:
static ErrorOr<NonnullRefPtr<Session>> create(NonnullRefPtr<Client> client, JsonObject& capabilities, ReadonlySpan<StringView> flags); static ErrorOr<NonnullRefPtr<Session>> create(NonnullRefPtr<Client> client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags);
~Session(); ~Session();
enum class AllowInvalidWindowHandle { enum class AllowInvalidWindowHandle {
@ -83,7 +83,7 @@ public:
} }
private: private:
Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, bool http); Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags);
ErrorOr<void> start(LaunchBrowserCallbacks const&); ErrorOr<void> start(LaunchBrowserCallbacks const&);
@ -94,7 +94,7 @@ private:
Web::WebDriver::LadybirdOptions m_options; Web::WebDriver::LadybirdOptions m_options;
String m_session_id; String m_session_id;
bool m_http { false }; Web::WebDriver::SessionFlags m_session_flags { Web::WebDriver::SessionFlags::Default };
HashMap<String, Window> m_windows; HashMap<String, Window> m_windows;
String m_current_window_handle; String m_current_window_handle;