diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-08 09:42:36 -0500 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-11-08 19:58:34 -0500 |
commit | 8ae10ba0fd863894ffea885fdea0fab67fb20ba3 (patch) | |
tree | 9df5c3f0f5bbe9d9876d9cf13293c91fefef3be7 /Userland/Libraries/LibWeb | |
parent | 0246abec8001cf22312f6453f6080ddf24a11f16 (diff) | |
download | serenity-8ae10ba0fd863894ffea885fdea0fab67fb20ba3.zip |
LibWeb+WebDriver: Add an IPC-transferable Web::WebDriver::Response class
This is essentially an ErrorOr<JsonValue, Web::WebDriver::Error> class.
Unfortunately, that ErrorOr would not be default-constructible, which is
required for the generated IPC classes. So this is a thin wrapper around
a Variant<JsonValue, Web::WebDriver::Error> to emulate ErrorOr.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebDriver/Response.cpp | 76 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebDriver/Response.h | 52 |
3 files changed, 129 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index da87a6c0db..5ed7e06d14 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -440,6 +440,7 @@ set(SOURCES WebAssembly/WebAssemblyTablePrototype.cpp WebDriver/Error.cpp WebDriver/ExecuteScript.cpp + WebDriver/Response.cpp WebGL/WebGLContextAttributes.cpp WebGL/WebGLContextEvent.cpp WebGL/WebGLRenderingContext.cpp diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.cpp b/Userland/Libraries/LibWeb/WebDriver/Response.cpp new file mode 100644 index 0000000000..c8102d35a2 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/Response.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibIPC/Decoder.h> +#include <LibIPC/Encoder.h> +#include <LibWeb/WebDriver/Response.h> + +enum class ResponseType : u8 { + Success, + Error, +}; + +namespace Web::WebDriver { + +Response::Response(JsonValue&& value) + : m_value_or_error(move(value)) +{ +} + +Response::Response(Error&& error) + : m_value_or_error(move(error)) +{ +} + +} + +bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) +{ + response.visit( + [](Empty) { VERIFY_NOT_REACHED(); }, + [&](JsonValue const& value) { + encoder << ResponseType::Success; + encoder << value; + }, + [&](Web::WebDriver::Error const& error) { + encoder << ResponseType::Error; + encoder << error.http_status; + encoder << error.error; + encoder << error.message; + encoder << error.data; + }); + + return true; +} + +ErrorOr<void> IPC::decode(Decoder& decoder, Web::WebDriver::Response& response) +{ + ResponseType type {}; + TRY(decoder.decode(type)); + + switch (type) { + case ResponseType::Success: { + JsonValue value; + TRY(decoder.decode(value)); + + response = move(value); + break; + } + + case ResponseType::Error: { + Web::WebDriver::Error error {}; + TRY(decoder.decode(error.http_status)); + TRY(decoder.decode(error.error)); + TRY(decoder.decode(error.message)); + TRY(decoder.decode(error.data)); + + response = move(error); + break; + } + } + + return {}; +} diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.h b/Userland/Libraries/LibWeb/WebDriver/Response.h new file mode 100644 index 0000000000..7d7e5c4da6 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/Response.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/JsonValue.h> +#include <AK/Variant.h> +#include <LibIPC/Forward.h> +#include <LibWeb/WebDriver/Error.h> + +namespace Web::WebDriver { + +// FIXME: Ideally, this could be `using Response = ErrorOr<JsonValue, Error>`, but that won't be +// default-constructible, which is a requirement for the generated IPC. +struct Response { + Response() = default; + Response(JsonValue&&); + Response(Error&&); + + JsonValue& value() { return m_value_or_error.template get<JsonValue>(); } + JsonValue const& value() const { return m_value_or_error.template get<JsonValue>(); } + + Error& error() { return m_value_or_error.template get<Error>(); } + Error const& error() const { return m_value_or_error.template get<Error>(); } + + bool is_error() const { return m_value_or_error.template has<Error>(); } + + JsonValue release_value() { return move(value()); } + Error release_error() { return move(error()); } + + template<typename... Visitors> + decltype(auto) visit(Visitors&&... visitors) const + { + return m_value_or_error.visit(forward<Visitors>(visitors)...); + } + +private: + // Note: Empty is only a possible state until the Response has been decoded by IPC. + Variant<Empty, JsonValue, Error> m_value_or_error; +}; + +} + +namespace IPC { + +bool encode(Encoder&, Web::WebDriver::Response const&); +ErrorOr<void> decode(Decoder&, Web::WebDriver::Response&); + +} |