From ab99ed5fba8bf954cdd60bbc90dafbe47b29b18f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 1 Jan 2023 23:37:35 -0500 Subject: [PATCH] LibIPC+Everywhere: Change IPC::encode's return type to ErrorOr In doing so, this removes all uses of the Encoder's stream operator, except for where it is currently still used in the generated IPC code. So the stream operator currently discards any errors, which is the existing behavior. A subsequent commit will propagate the errors. --- .../HackStudio/AutoCompleteResponse.h | 64 ++++++----- Userland/Libraries/LibCore/AnonymousBuffer.h | 2 +- Userland/Libraries/LibCore/DateTime.h | 2 +- Userland/Libraries/LibCore/Proxy.h | 2 +- Userland/Libraries/LibDNS/Answer.cpp | 12 ++- Userland/Libraries/LibDNS/Answer.h | 2 +- Userland/Libraries/LibGfx/Color.cpp | 5 +- Userland/Libraries/LibGfx/Color.h | 2 +- Userland/Libraries/LibGfx/Point.cpp | 7 +- Userland/Libraries/LibGfx/Point.h | 2 +- Userland/Libraries/LibGfx/Rect.cpp | 7 +- Userland/Libraries/LibGfx/Rect.h | 2 +- Userland/Libraries/LibGfx/ShareableBitmap.cpp | 20 ++-- Userland/Libraries/LibGfx/ShareableBitmap.h | 2 +- Userland/Libraries/LibGfx/Size.cpp | 7 +- Userland/Libraries/LibGfx/Size.h | 2 +- Userland/Libraries/LibIPC/Encoder.cpp | 102 ++++++++---------- Userland/Libraries/LibIPC/Encoder.h | 80 +++++++------- Userland/Libraries/LibIPC/Forward.h | 4 +- Userland/Libraries/LibSQL/Value.cpp | 27 ++--- Userland/Libraries/LibSQL/Value.h | 2 +- Userland/Libraries/LibWeb/Cookie/Cookie.cpp | 28 ++--- Userland/Libraries/LibWeb/Cookie/Cookie.h | 2 +- .../Libraries/LibWeb/Cookie/ParsedCookie.cpp | 22 ++-- .../Libraries/LibWeb/Cookie/ParsedCookie.h | 2 +- .../Libraries/LibWeb/WebDriver/Response.cpp | 28 ++--- .../Libraries/LibWeb/WebDriver/Response.h | 2 +- Userland/Services/WindowServer/ScreenLayout.h | 4 +- .../Services/WindowServer/ScreenLayout.ipp | 19 ++-- 29 files changed, 224 insertions(+), 238 deletions(-) diff --git a/Userland/DevTools/HackStudio/AutoCompleteResponse.h b/Userland/DevTools/HackStudio/AutoCompleteResponse.h index fb612b294d4..216d040f79d 100644 --- a/Userland/DevTools/HackStudio/AutoCompleteResponse.h +++ b/Userland/DevTools/HackStudio/AutoCompleteResponse.h @@ -16,14 +16,14 @@ namespace IPC { template<> -inline bool encode(Encoder& encoder, CodeComprehension::AutocompleteResultEntry const& response) +inline ErrorOr encode(Encoder& encoder, CodeComprehension::AutocompleteResultEntry const& response) { - encoder << response.completion; - encoder << response.partial_input_length; - encoder << response.language; - encoder << response.display_text; - encoder << response.hide_autocomplete_after_applying; - return true; + TRY(encoder.encode(response.completion)); + TRY(encoder.encode(response.partial_input_length)); + TRY(encoder.encode(response.language)); + TRY(encoder.encode(response.display_text)); + TRY(encoder.encode(response.hide_autocomplete_after_applying)); + return {}; } template<> @@ -39,12 +39,12 @@ inline ErrorOr decode(Decoder& decod } template<> -inline bool encode(Encoder& encoder, CodeComprehension::ProjectLocation const& location) +inline ErrorOr encode(Encoder& encoder, CodeComprehension::ProjectLocation const& location) { - encoder << location.file; - encoder << location.line; - encoder << location.column; - return true; + TRY(encoder.encode(location.file)); + TRY(encoder.encode(location.line)); + TRY(encoder.encode(location.column)); + return {}; } template<> @@ -58,14 +58,13 @@ inline ErrorOr decode(Decoder& decoder) } template<> -inline bool encode(Encoder& encoder, CodeComprehension::Declaration const& declaration) +inline ErrorOr encode(Encoder& encoder, CodeComprehension::Declaration const& declaration) { - encoder << declaration.name; - if (!encode(encoder, declaration.position)) - return false; - encoder << declaration.type; - encoder << declaration.scope; - return true; + TRY(encoder.encode(declaration.name)); + TRY(encoder.encode(declaration.position)); + TRY(encoder.encode(declaration.type)); + TRY(encoder.encode(declaration.scope)); + return {}; } template<> @@ -80,13 +79,13 @@ inline ErrorOr decode(Decoder& decoder) } template<> -inline bool encode(Encoder& encoder, CodeComprehension::TodoEntry const& entry) +inline ErrorOr encode(Encoder& encoder, CodeComprehension::TodoEntry const& entry) { - encoder << entry.content; - encoder << entry.filename; - encoder << entry.line; - encoder << entry.column; - return true; + TRY(encoder.encode(entry.content)); + TRY(encoder.encode(entry.filename)); + TRY(encoder.encode(entry.line)); + TRY(encoder.encode(entry.column)); + return {}; } template<> @@ -101,15 +100,14 @@ inline ErrorOr decode(Decoder& decoder) } template<> -inline bool encode(Encoder& encoder, CodeComprehension::TokenInfo const& location) +inline ErrorOr encode(Encoder& encoder, CodeComprehension::TokenInfo const& location) { - encoder << (u32)location.type; - static_assert(sizeof(location.type) == sizeof(u32)); - encoder << location.start_line; - encoder << location.start_column; - encoder << location.end_line; - encoder << location.end_column; - return true; + TRY(encoder.encode(location.type)); + TRY(encoder.encode(location.start_line)); + TRY(encoder.encode(location.start_column)); + TRY(encoder.encode(location.end_line)); + TRY(encoder.encode(location.end_column)); + return {}; } template<> diff --git a/Userland/Libraries/LibCore/AnonymousBuffer.h b/Userland/Libraries/LibCore/AnonymousBuffer.h index e4784281f22..6475e9026ae 100644 --- a/Userland/Libraries/LibCore/AnonymousBuffer.h +++ b/Userland/Libraries/LibCore/AnonymousBuffer.h @@ -75,7 +75,7 @@ private: namespace IPC { template<> -bool encode(Encoder&, Core::AnonymousBuffer const&); +ErrorOr encode(Encoder&, Core::AnonymousBuffer const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibCore/DateTime.h b/Userland/Libraries/LibCore/DateTime.h index 3c8879034f1..78a93655516 100644 --- a/Userland/Libraries/LibCore/DateTime.h +++ b/Userland/Libraries/LibCore/DateTime.h @@ -69,7 +69,7 @@ struct Formatter : StandardFormatter { namespace IPC { template<> -bool encode(Encoder&, Core::DateTime const&); +ErrorOr encode(Encoder&, Core::DateTime const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibCore/Proxy.h b/Userland/Libraries/LibCore/Proxy.h index 780114c3fb7..a427569dd13 100644 --- a/Userland/Libraries/LibCore/Proxy.h +++ b/Userland/Libraries/LibCore/Proxy.h @@ -54,7 +54,7 @@ struct ProxyData { namespace IPC { template<> -bool encode(Encoder&, Core::ProxyData const&); +ErrorOr encode(Encoder&, Core::ProxyData const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibDNS/Answer.cpp b/Userland/Libraries/LibDNS/Answer.cpp index c8112755de7..62d8d199506 100644 --- a/Userland/Libraries/LibDNS/Answer.cpp +++ b/Userland/Libraries/LibDNS/Answer.cpp @@ -101,10 +101,16 @@ ErrorOr AK::Formatter::format(AK::FormatBuilder& builder namespace IPC { template<> -bool encode(Encoder& encoder, DNS::Answer const& answer) +ErrorOr encode(Encoder& encoder, DNS::Answer const& answer) { - encoder << answer.name().as_string() << (u16)answer.type() << (u16)answer.class_code() << answer.ttl() << answer.record_data() << answer.mdns_cache_flush(); - return true; + TRY(encoder.encode(answer.name().as_string())); + TRY(encoder.encode(static_cast(answer.type()))); + TRY(encoder.encode(static_cast(answer.class_code()))); + TRY(encoder.encode(answer.ttl())); + TRY(encoder.encode(answer.record_data())); + TRY(encoder.encode(answer.mdns_cache_flush())); + + return {}; } template<> diff --git a/Userland/Libraries/LibDNS/Answer.h b/Userland/Libraries/LibDNS/Answer.h index d666562fb0a..9a4c073e958 100644 --- a/Userland/Libraries/LibDNS/Answer.h +++ b/Userland/Libraries/LibDNS/Answer.h @@ -95,7 +95,7 @@ struct AK::Formatter : StandardFormatter { namespace IPC { template<> -bool encode(Encoder&, DNS::Answer const&); +ErrorOr encode(Encoder&, DNS::Answer const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index 76d169c4958..b2c7256f59c 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -366,10 +366,9 @@ Vector Color::tints(u32 steps, float max) const } template<> -bool IPC::encode(Encoder& encoder, Color const& color) +ErrorOr IPC::encode(Encoder& encoder, Color const& color) { - encoder << color.value(); - return true; + return encoder.encode(color.value()); } template<> diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 9fe5b7e5abd..5f9af4b9edb 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -574,7 +574,7 @@ struct Formatter : public Formatter { namespace IPC { template<> -bool encode(Encoder&, Gfx::Color const&); +ErrorOr encode(Encoder&, Gfx::Color const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibGfx/Point.cpp b/Userland/Libraries/LibGfx/Point.cpp index 8dd484ca199..1b35b502031 100644 --- a/Userland/Libraries/LibGfx/Point.cpp +++ b/Userland/Libraries/LibGfx/Point.cpp @@ -52,10 +52,11 @@ DeprecatedString FloatPoint::to_deprecated_string() const namespace IPC { template<> -bool encode(Encoder& encoder, Gfx::IntPoint const& point) +ErrorOr encode(Encoder& encoder, Gfx::IntPoint const& point) { - encoder << point.x() << point.y(); - return true; + TRY(encoder.encode(point.x())); + TRY(encoder.encode(point.y())); + return {}; } template<> diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 00ee992bca4..67c3ce89b8f 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -291,7 +291,7 @@ struct Formatter> : Formatter { namespace IPC { template<> -bool encode(Encoder&, Gfx::IntPoint const&); +ErrorOr encode(Encoder&, Gfx::IntPoint const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibGfx/Rect.cpp b/Userland/Libraries/LibGfx/Rect.cpp index 7ab90853d8b..99a475de5f0 100644 --- a/Userland/Libraries/LibGfx/Rect.cpp +++ b/Userland/Libraries/LibGfx/Rect.cpp @@ -30,10 +30,11 @@ DeprecatedString FloatRect::to_deprecated_string() const namespace IPC { template<> -bool encode(Encoder& encoder, Gfx::IntRect const& rect) +ErrorOr encode(Encoder& encoder, Gfx::IntRect const& rect) { - encoder << rect.location() << rect.size(); - return true; + TRY(encoder.encode(rect.location())); + TRY(encoder.encode(rect.size())); + return {}; } template<> diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 43b549c1e6a..0334e8a40a5 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -1039,7 +1039,7 @@ struct Formatter> : Formatter { namespace IPC { template<> -bool encode(Encoder&, Gfx::IntRect const&); +ErrorOr encode(Encoder&, Gfx::IntRect const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.cpp b/Userland/Libraries/LibGfx/ShareableBitmap.cpp index a741c3d7e57..31ba8ca335c 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.cpp +++ b/Userland/Libraries/LibGfx/ShareableBitmap.cpp @@ -23,21 +23,23 @@ ShareableBitmap::ShareableBitmap(NonnullRefPtr bitmap, Tag) namespace IPC { template<> -bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap) +ErrorOr encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap) { - encoder << shareable_bitmap.is_valid(); + TRY(encoder.encode(shareable_bitmap.is_valid())); if (!shareable_bitmap.is_valid()) - return true; + return {}; + auto& bitmap = *shareable_bitmap.bitmap(); - encoder << IPC::File(bitmap.anonymous_buffer().fd()); - encoder << bitmap.size(); - encoder << static_cast(bitmap.scale()); - encoder << static_cast(bitmap.format()); + TRY(encoder.encode(IPC::File(bitmap.anonymous_buffer().fd()))); + TRY(encoder.encode(bitmap.size())); + TRY(encoder.encode(static_cast(bitmap.scale()))); + TRY(encoder.encode(static_cast(bitmap.format()))); if (bitmap.is_indexed()) { auto palette = bitmap.palette_to_vector(); - encoder << palette; + TRY(encoder.encode(palette)); } - return true; + + return {}; } template<> diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.h b/Userland/Libraries/LibGfx/ShareableBitmap.h index 16e8df9c138..9f18dc4a7ba 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.h +++ b/Userland/Libraries/LibGfx/ShareableBitmap.h @@ -36,7 +36,7 @@ private: namespace IPC { template<> -bool encode(Encoder&, Gfx::ShareableBitmap const&); +ErrorOr encode(Encoder&, Gfx::ShareableBitmap const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibGfx/Size.cpp b/Userland/Libraries/LibGfx/Size.cpp index b737e7af15e..af997447d79 100644 --- a/Userland/Libraries/LibGfx/Size.cpp +++ b/Userland/Libraries/LibGfx/Size.cpp @@ -28,10 +28,11 @@ DeprecatedString FloatSize::to_deprecated_string() const namespace IPC { template<> -bool encode(Encoder& encoder, Gfx::IntSize const& size) +ErrorOr encode(Encoder& encoder, Gfx::IntSize const& size) { - encoder << size.width() << size.height(); - return true; + TRY(encoder.encode(size.width())); + TRY(encoder.encode(size.height())); + return {}; } template<> diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index ae55b440420..04e876796b7 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -214,7 +214,7 @@ struct Formatter> : Formatter { namespace IPC { template<> -bool encode(Encoder&, Gfx::IntSize const&); +ErrorOr encode(Encoder&, Gfx::IntSize const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index c66f8578800..25b8921efff 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -21,132 +22,113 @@ namespace IPC { template<> -bool encode(Encoder& encoder, float const& value) +ErrorOr encode(Encoder& encoder, float const& value) { return encoder.encode(bit_cast(value)); } template<> -bool encode(Encoder& encoder, double const& value) +ErrorOr encode(Encoder& encoder, double const& value) { return encoder.encode(bit_cast(value)); } template<> -bool encode(Encoder& encoder, StringView const& value) +ErrorOr encode(Encoder& encoder, StringView const& value) { - auto result = encoder.append(reinterpret_cast(value.characters_without_null_termination()), value.length()); - return !result.is_error(); + TRY(encoder.append(reinterpret_cast(value.characters_without_null_termination()), value.length())); + return {}; } template<> -bool encode(Encoder& encoder, DeprecatedString const& value) +ErrorOr encode(Encoder& encoder, DeprecatedString const& value) { if (value.is_null()) return encoder.encode(-1); - if (!encoder.encode(static_cast(value.length()))) - return false; - return encoder.encode(value.view()); + TRY(encoder.encode(static_cast(value.length()))); + TRY(encoder.encode(value.view())); + return {}; } template<> -bool encode(Encoder& encoder, ByteBuffer const& value) +ErrorOr encode(Encoder& encoder, ByteBuffer const& value) { - if (!encoder.encode(static_cast(value.size()))) - return false; - - auto result = encoder.append(value.data(), value.size()); - return !result.is_error(); + TRY(encoder.encode(static_cast(value.size()))); + TRY(encoder.append(value.data(), value.size())); + return {}; } template<> -bool encode(Encoder& encoder, JsonValue const& value) +ErrorOr encode(Encoder& encoder, JsonValue const& value) { return encoder.encode(value.serialized()); } template<> -bool encode(Encoder& encoder, URL const& value) +ErrorOr encode(Encoder& encoder, URL const& value) { return encoder.encode(value.to_deprecated_string()); } template<> -bool encode(Encoder& encoder, Dictionary const& dictionary) +ErrorOr encode(Encoder& encoder, Dictionary const& dictionary) { - if (!encoder.encode(static_cast(dictionary.size()))) - return false; + TRY(encoder.encode(static_cast(dictionary.size()))); - bool had_error = false; + TRY(dictionary.try_for_each_entry([&](auto const& key, auto const& value) -> ErrorOr { + TRY(encoder.encode(key)); + TRY(encoder.encode(value)); + return {}; + })); - dictionary.for_each_entry([&](auto const& key, auto const& value) { - if (had_error) - return; - if (!encoder.encode(key) || !encoder.encode(value)) - had_error = true; - }); - - return !had_error; + return {}; } template<> -bool encode(Encoder& encoder, File const& file) +ErrorOr encode(Encoder& encoder, File const& file) { int fd = file.fd(); - if (fd != -1) { - auto result = dup(fd); - if (result < 0) { - perror("dup"); - VERIFY_NOT_REACHED(); - } - fd = result; - } + if (fd != -1) + fd = TRY(Core::System::dup(fd)); - if (encoder.append_file_descriptor(fd).is_error()) - return false; - return true; + TRY(encoder.append_file_descriptor(fd)); + return {}; } template<> -bool encode(Encoder&, Empty const&) +ErrorOr encode(Encoder&, Empty const&) { - return true; + return {}; } template<> -bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) +ErrorOr encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) { - if (!encoder.encode(buffer.is_valid())) - return false; + TRY(encoder.encode(buffer.is_valid())); if (buffer.is_valid()) { - if (!encoder.encode(static_cast(buffer.size()))) - return false; - if (!encoder.encode(IPC::File { buffer.fd() })) - return false; + TRY(encoder.encode(static_cast(buffer.size()))); + TRY(encoder.encode(IPC::File { buffer.fd() })); } - return true; + return {}; } template<> -bool encode(Encoder& encoder, Core::DateTime const& datetime) +ErrorOr encode(Encoder& encoder, Core::DateTime const& datetime) { return encoder.encode(static_cast(datetime.timestamp())); } template<> -bool encode(Encoder& encoder, Core::ProxyData const& proxy) +ErrorOr encode(Encoder& encoder, Core::ProxyData const& proxy) { - if (!encoder.encode(proxy.type)) - return false; - if (!encoder.encode(proxy.host_ipv4)) - return false; - if (!encoder.encode(proxy.port)) - return false; - return true; + TRY(encoder.encode(proxy.type)); + TRY(encoder.encode(proxy.host_ipv4)); + TRY(encoder.encode(proxy.port)); + return {}; } } diff --git a/Userland/Libraries/LibIPC/Encoder.h b/Userland/Libraries/LibIPC/Encoder.h index e331f9c9c05..e2cffc858ec 100644 --- a/Userland/Libraries/LibIPC/Encoder.h +++ b/Userland/Libraries/LibIPC/Encoder.h @@ -18,7 +18,7 @@ namespace IPC { template -bool encode(Encoder&, T const&) +ErrorOr encode(Encoder&, T const&) { static_assert(DependentFalse, "Base IPC::encode() was instantiated"); VERIFY_NOT_REACHED(); @@ -34,12 +34,12 @@ public: template Encoder& operator<<(T const& value) { - encode(value); + (void)encode(value); return *this; } template - bool encode(T const& value); + ErrorOr encode(T const& value); ErrorOr extend_capacity(size_t capacity) { @@ -72,10 +72,9 @@ private: }; template -bool encode(Encoder& encoder, T const& value) +ErrorOr encode(Encoder& encoder, T const& value) { - if (encoder.extend_capacity(sizeof(T)).is_error()) - return false; + TRY(encoder.extend_capacity(sizeof(T))); if constexpr (sizeof(T) == 1) { encoder.append(static_cast(value)); @@ -100,97 +99,90 @@ bool encode(Encoder& encoder, T const& value) static_assert(DependentFalse); } - return true; + return {}; } template -bool encode(Encoder& encoder, T const& value) +ErrorOr encode(Encoder& encoder, T const& value) { return encoder.encode(to_underlying(value)); } template<> -bool encode(Encoder&, float const&); +ErrorOr encode(Encoder&, float const&); template<> -bool encode(Encoder&, double const&); +ErrorOr encode(Encoder&, double const&); template<> -bool encode(Encoder&, StringView const&); +ErrorOr encode(Encoder&, StringView const&); template<> -bool encode(Encoder&, DeprecatedString const&); +ErrorOr encode(Encoder&, DeprecatedString const&); template<> -bool encode(Encoder&, ByteBuffer const&); +ErrorOr encode(Encoder&, ByteBuffer const&); template<> -bool encode(Encoder&, JsonValue const&); +ErrorOr encode(Encoder&, JsonValue const&); template<> -bool encode(Encoder&, URL const&); +ErrorOr encode(Encoder&, URL const&); template<> -bool encode(Encoder&, Dictionary const&); +ErrorOr encode(Encoder&, Dictionary const&); template<> -bool encode(Encoder&, File const&); +ErrorOr encode(Encoder&, File const&); template<> -bool encode(Encoder&, Empty const&); +ErrorOr encode(Encoder&, Empty const&); template -bool encode(Encoder& encoder, T const& vector) +ErrorOr encode(Encoder& encoder, T const& vector) { - if (!encoder.encode(static_cast(vector.size()))) - return false; + TRY(encoder.encode(static_cast(vector.size()))); - for (auto const& value : vector) { - if (!encoder.encode(value)) - return false; - } + for (auto const& value : vector) + TRY(encoder.encode(value)); - return true; + return {}; } template -bool encode(Encoder& encoder, T const& hashmap) +ErrorOr encode(Encoder& encoder, T const& hashmap) { - if (!encoder.encode(static_cast(hashmap.size()))) - return false; + TRY(encoder.encode(static_cast(hashmap.size()))); for (auto it : hashmap) { - if (!encoder.encode(it.key)) - return false; - if (!encoder.encode(it.value)) - return false; + TRY(encoder.encode(it.key)); + TRY(encoder.encode(it.value)); } - return true; + return {}; } template -bool encode(Encoder& encoder, T const& queue) +ErrorOr encode(Encoder& encoder, T const& queue) { return encoder.encode(IPC::File { queue.fd() }); } template -bool encode(Encoder& encoder, T const& optional) +ErrorOr encode(Encoder& encoder, T const& optional) { - if (!encoder.encode(optional.has_value())) - return false; + TRY(encoder.encode(optional.has_value())); if (optional.has_value()) - return encoder.encode(optional.value()); - return true; + TRY(encoder.encode(optional.value())); + + return {}; } template -bool encode(Encoder& encoder, T const& variant) +ErrorOr encode(Encoder& encoder, T const& variant) { - if (!encoder.encode(variant.index())) - return false; + TRY(encoder.encode(variant.index())); return variant.visit([&](auto const& value) { return encoder.encode(value); @@ -199,7 +191,7 @@ bool encode(Encoder& encoder, T const& variant) // This must be last so that it knows about the above specializations. template -bool Encoder::encode(T const& value) +ErrorOr Encoder::encode(T const& value) { return IPC::encode(*this, value); } diff --git a/Userland/Libraries/LibIPC/Forward.h b/Userland/Libraries/LibIPC/Forward.h index c16e57a2717..c3b5d4f23a0 100644 --- a/Userland/Libraries/LibIPC/Forward.h +++ b/Userland/Libraries/LibIPC/Forward.h @@ -6,6 +6,8 @@ #pragma once +#include + namespace IPC { class Decoder; @@ -16,7 +18,7 @@ class File; class Stub; template -bool encode(Encoder&, T const&); +ErrorOr encode(Encoder&, T const&); template ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp index ee85e3e9a49..789e14623a4 100644 --- a/Userland/Libraries/LibSQL/Value.cpp +++ b/Userland/Libraries/LibSQL/Value.cpp @@ -814,37 +814,32 @@ ResultOr> Value::infer_tuple_descriptor(Vector -bool IPC::encode(Encoder& encoder, SQL::Value const& value) +ErrorOr IPC::encode(Encoder& encoder, SQL::Value const& value) { auto type_flags = encode_type_flags(value); - encoder << type_flags; + TRY(encoder.encode(type_flags)); if (value.is_null()) - return true; + return {}; switch (value.type()) { case SQL::SQLType::Null: - break; + return {}; case SQL::SQLType::Text: - encoder << value.to_deprecated_string(); - break; + return encoder.encode(value.to_deprecated_string()); case SQL::SQLType::Integer: - SQL::downsize_integer(value, [&](auto integer, auto) { - encoder << integer; + return SQL::downsize_integer(value, [&](auto integer, auto) { + return encoder.encode(integer); }); - break; case SQL::SQLType::Float: - encoder << value.to_double().value(); - break; + return encoder.encode(value.to_double().value()); case SQL::SQLType::Boolean: - encoder << value.to_bool().value(); - break; + return encoder.encode(value.to_bool().value()); case SQL::SQLType::Tuple: - encoder << value.to_vector().value(); - break; + return encoder.encode(value.to_vector().value()); } - return true; + VERIFY_NOT_REACHED(); } template<> diff --git a/Userland/Libraries/LibSQL/Value.h b/Userland/Libraries/LibSQL/Value.h index 475e963dc0a..380bb487a31 100644 --- a/Userland/Libraries/LibSQL/Value.h +++ b/Userland/Libraries/LibSQL/Value.h @@ -191,7 +191,7 @@ struct AK::Formatter : Formatter { namespace IPC { template<> -bool encode(Encoder&, SQL::Value const&); +ErrorOr encode(Encoder&, SQL::Value const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp index 6ddaa79c057..229ca380904 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp @@ -39,22 +39,22 @@ SameSite same_site_from_string(StringView same_site_mode) } template<> -bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie) +ErrorOr IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie) { - encoder << cookie.name; - encoder << cookie.value; - encoder << cookie.domain; - encoder << cookie.path; - encoder << cookie.creation_time; - encoder << cookie.expiry_time; - encoder << cookie.host_only; - encoder << cookie.http_only; - encoder << cookie.last_access_time; - encoder << cookie.persistent; - encoder << cookie.secure; - encoder << cookie.same_site; + TRY(encoder.encode(cookie.name)); + TRY(encoder.encode(cookie.value)); + TRY(encoder.encode(cookie.domain)); + TRY(encoder.encode(cookie.path)); + TRY(encoder.encode(cookie.creation_time)); + TRY(encoder.encode(cookie.expiry_time)); + TRY(encoder.encode(cookie.host_only)); + TRY(encoder.encode(cookie.http_only)); + TRY(encoder.encode(cookie.last_access_time)); + TRY(encoder.encode(cookie.persistent)); + TRY(encoder.encode(cookie.secure)); + TRY(encoder.encode(cookie.same_site)); - return true; + return {}; } template<> diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.h b/Userland/Libraries/LibWeb/Cookie/Cookie.h index 429a90f6d0e..c3b4c9768c6 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.h +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.h @@ -47,7 +47,7 @@ SameSite same_site_from_string(StringView same_site_mode); namespace IPC { template<> -bool encode(Encoder&, Web::Cookie::Cookie const&); +ErrorOr encode(Encoder&, Web::Cookie::Cookie const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp index 69ceeb40508..e04870f1bb5 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp @@ -348,19 +348,19 @@ Optional parse_date_time(StringView date_string) } template<> -bool IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie) +ErrorOr IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie) { - encoder << cookie.name; - encoder << cookie.value; - encoder << cookie.expiry_time_from_expires_attribute; - encoder << cookie.expiry_time_from_max_age_attribute; - encoder << cookie.domain; - encoder << cookie.path; - encoder << cookie.secure_attribute_present; - encoder << cookie.http_only_attribute_present; - encoder << cookie.same_site_attribute; + TRY(encoder.encode(cookie.name)); + TRY(encoder.encode(cookie.value)); + TRY(encoder.encode(cookie.expiry_time_from_expires_attribute)); + TRY(encoder.encode(cookie.expiry_time_from_max_age_attribute)); + TRY(encoder.encode(cookie.domain)); + TRY(encoder.encode(cookie.path)); + TRY(encoder.encode(cookie.secure_attribute_present)); + TRY(encoder.encode(cookie.http_only_attribute_present)); + TRY(encoder.encode(cookie.same_site_attribute)); - return true; + return {}; } template<> diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h index 31b6b5b15fa..07cc36b292e 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h @@ -33,7 +33,7 @@ Optional parse_cookie(DeprecatedString const& cookie_string); namespace IPC { template<> -bool encode(Encoder&, Web::Cookie::ParsedCookie const&); +ErrorOr encode(Encoder&, Web::Cookie::ParsedCookie const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.cpp b/Userland/Libraries/LibWeb/WebDriver/Response.cpp index c2ed16cdb4f..eb54591db97 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Response.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Response.cpp @@ -28,23 +28,23 @@ Response::Response(Error&& error) } template<> -bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) +ErrorOr IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) { - response.visit( - [](Empty) { VERIFY_NOT_REACHED(); }, - [&](JsonValue const& value) { - encoder << ResponseType::Success; - encoder << value; + return response.visit( + [](Empty) -> ErrorOr { VERIFY_NOT_REACHED(); }, + [&](JsonValue const& value) -> ErrorOr { + TRY(encoder.encode(ResponseType::Success)); + TRY(encoder.encode(value)); + return {}; }, - [&](Web::WebDriver::Error const& error) { - encoder << ResponseType::Error; - encoder << error.http_status; - encoder << error.error; - encoder << error.message; - encoder << error.data; + [&](Web::WebDriver::Error const& error) -> ErrorOr { + TRY(encoder.encode(ResponseType::Error)); + TRY(encoder.encode(error.http_status)); + TRY(encoder.encode(error.error)); + TRY(encoder.encode(error.message)); + TRY(encoder.encode(error.data)); + return {}; }); - - return true; } template<> diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.h b/Userland/Libraries/LibWeb/WebDriver/Response.h index dc8b5704418..09c76458f82 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Response.h +++ b/Userland/Libraries/LibWeb/WebDriver/Response.h @@ -47,7 +47,7 @@ private: namespace IPC { template<> -bool encode(Encoder&, Web::WebDriver::Response const&); +ErrorOr encode(Encoder&, Web::WebDriver::Response const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Services/WindowServer/ScreenLayout.h b/Userland/Services/WindowServer/ScreenLayout.h index 6a5c5b0571d..4ff2d5bcf87 100644 --- a/Userland/Services/WindowServer/ScreenLayout.h +++ b/Userland/Services/WindowServer/ScreenLayout.h @@ -74,13 +74,13 @@ public: namespace IPC { template<> -bool encode(Encoder&, WindowServer::ScreenLayout::Screen const&); +ErrorOr encode(Encoder&, WindowServer::ScreenLayout::Screen const&); template<> ErrorOr decode(Decoder&); template<> -bool encode(Encoder&, WindowServer::ScreenLayout const&); +ErrorOr encode(Encoder&, WindowServer::ScreenLayout const&); template<> ErrorOr decode(Decoder&); diff --git a/Userland/Services/WindowServer/ScreenLayout.ipp b/Userland/Services/WindowServer/ScreenLayout.ipp index 77b4b04497d..f9fc1d7573f 100644 --- a/Userland/Services/WindowServer/ScreenLayout.ipp +++ b/Userland/Services/WindowServer/ScreenLayout.ipp @@ -393,10 +393,15 @@ bool ScreenLayout::try_auto_add_display_connector(DeprecatedString const& device namespace IPC { template<> -bool encode(Encoder& encoder, WindowServer::ScreenLayout::Screen const& screen) +ErrorOr encode(Encoder& encoder, WindowServer::ScreenLayout::Screen const& screen) { - encoder << screen.mode << screen.device << screen.location << screen.resolution << screen.scale_factor; - return true; + TRY(encoder.encode(screen.mode)); + TRY(encoder.encode(screen.device)); + TRY(encoder.encode(screen.location)); + TRY(encoder.encode(screen.resolution)); + TRY(encoder.encode(screen.scale_factor)); + + return {}; } template<> @@ -412,10 +417,12 @@ ErrorOr decode(Decoder& decoder) } template<> -bool encode(Encoder& encoder, WindowServer::ScreenLayout const& screen_layout) +ErrorOr encode(Encoder& encoder, WindowServer::ScreenLayout const& screen_layout) { - encoder << screen_layout.screens << screen_layout.main_screen_index; - return true; + TRY(encoder.encode(screen_layout.screens)); + TRY(encoder.encode(screen_layout.main_screen_index)); + + return {}; } template<>