From 05f41382bb527fbf2bb47340ad065b36f2e7409f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 15 Nov 2022 11:24:59 -0500 Subject: [PATCH] Userland: Properly define IPC::encode and IPC::decode specializations In order to avoid the base encode/decode methods from being used (and failing a static assertion), we must be sure to declare/define the custom type implementations as template specializations. After this, LibIPC is no longer sensitive to include order. --- Userland/Libraries/LibCore/AnonymousBuffer.h | 3 +++ Userland/Libraries/LibCore/DateTime.h | 3 +++ Userland/Libraries/LibCore/Proxy.h | 5 +++++ Userland/Libraries/LibDNS/Answer.cpp | 2 ++ Userland/Libraries/LibDNS/Answer.h | 3 +++ Userland/Libraries/LibGfx/Color.cpp | 2 ++ Userland/Libraries/LibGfx/Color.h | 3 +++ Userland/Libraries/LibGfx/Point.cpp | 2 ++ Userland/Libraries/LibGfx/Point.h | 3 +++ Userland/Libraries/LibGfx/Rect.cpp | 2 ++ Userland/Libraries/LibGfx/Rect.h | 3 +++ Userland/Libraries/LibGfx/ShareableBitmap.cpp | 2 ++ Userland/Libraries/LibGfx/ShareableBitmap.h | 3 +++ Userland/Libraries/LibGfx/Size.cpp | 2 ++ Userland/Libraries/LibGfx/Size.h | 3 +++ Userland/Libraries/LibIPC/Decoder.cpp | 3 +++ Userland/Libraries/LibIPC/Encoder.cpp | 3 +++ Userland/Libraries/LibWeb/Cookie/Cookie.cpp | 2 ++ Userland/Libraries/LibWeb/Cookie/Cookie.h | 3 +++ Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp | 2 ++ Userland/Libraries/LibWeb/Cookie/ParsedCookie.h | 3 +++ Userland/Libraries/LibWeb/WebDriver/Response.cpp | 2 ++ Userland/Libraries/LibWeb/WebDriver/Response.h | 3 +++ Userland/Services/WindowServer/ScreenLayout.h | 7 +++++++ Userland/Services/WindowServer/ScreenLayout.ipp | 8 ++++++-- 25 files changed, 75 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/AnonymousBuffer.h b/Userland/Libraries/LibCore/AnonymousBuffer.h index a0fd5bc76de..8e9f0b3d8e9 100644 --- a/Userland/Libraries/LibCore/AnonymousBuffer.h +++ b/Userland/Libraries/LibCore/AnonymousBuffer.h @@ -74,7 +74,10 @@ private: namespace IPC { +template<> bool encode(Encoder&, Core::AnonymousBuffer const&); + +template<> ErrorOr decode(Decoder&, Core::AnonymousBuffer&); } diff --git a/Userland/Libraries/LibCore/DateTime.h b/Userland/Libraries/LibCore/DateTime.h index 1e8cb3af6d4..1e38b94ba64 100644 --- a/Userland/Libraries/LibCore/DateTime.h +++ b/Userland/Libraries/LibCore/DateTime.h @@ -54,7 +54,10 @@ private: namespace IPC { +template<> bool encode(Encoder&, Core::DateTime const&); + +template<> ErrorOr decode(Decoder&, Core::DateTime&); } diff --git a/Userland/Libraries/LibCore/Proxy.h b/Userland/Libraries/LibCore/Proxy.h index 6b56a0c3df9..66e1d432b06 100644 --- a/Userland/Libraries/LibCore/Proxy.h +++ b/Userland/Libraries/LibCore/Proxy.h @@ -52,6 +52,11 @@ struct ProxyData { } namespace IPC { + +template<> bool encode(Encoder&, Core::ProxyData const&); + +template<> ErrorOr decode(Decoder&, Core::ProxyData&); + } diff --git a/Userland/Libraries/LibDNS/Answer.cpp b/Userland/Libraries/LibDNS/Answer.cpp index e494d3bd8cf..d68971343e8 100644 --- a/Userland/Libraries/LibDNS/Answer.cpp +++ b/Userland/Libraries/LibDNS/Answer.cpp @@ -100,12 +100,14 @@ ErrorOr AK::Formatter::format(AK::FormatBuilder& builder namespace IPC { +template<> bool 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; } +template<> ErrorOr decode(Decoder& decoder, DNS::Answer& answer) { String name; diff --git a/Userland/Libraries/LibDNS/Answer.h b/Userland/Libraries/LibDNS/Answer.h index a3d0ea7063c..a8592ed749c 100644 --- a/Userland/Libraries/LibDNS/Answer.h +++ b/Userland/Libraries/LibDNS/Answer.h @@ -94,7 +94,10 @@ struct AK::Formatter : StandardFormatter { namespace IPC { +template<> bool encode(Encoder&, DNS::Answer const&); + +template<> ErrorOr decode(Decoder&, DNS::Answer&); } diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index 45e2b98d6f3..6ab2ebe9425 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -366,12 +366,14 @@ Vector Color::tints(u32 steps, float max) const } +template<> bool IPC::encode(Encoder& encoder, Color const& color) { encoder << color.value(); return true; } +template<> ErrorOr IPC::decode(Decoder& decoder, Color& color) { u32 rgba; diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index eddd73b9266..c63834358d7 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -568,7 +568,10 @@ struct Formatter : public Formatter { namespace IPC { +template<> bool encode(Encoder&, Gfx::Color const&); + +template<> ErrorOr decode(Decoder&, Gfx::Color&); } diff --git a/Userland/Libraries/LibGfx/Point.cpp b/Userland/Libraries/LibGfx/Point.cpp index 901826f98ef..dddbae8206b 100644 --- a/Userland/Libraries/LibGfx/Point.cpp +++ b/Userland/Libraries/LibGfx/Point.cpp @@ -51,12 +51,14 @@ String FloatPoint::to_string() const namespace IPC { +template<> bool encode(Encoder& encoder, Gfx::IntPoint const& point) { encoder << point.x() << point.y(); return true; } +template<> ErrorOr decode(Decoder& decoder, Gfx::IntPoint& point) { int x = 0; diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 5e4b39feec2..9ef99607fcd 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -290,7 +290,10 @@ struct Formatter> : Formatter { namespace IPC { +template<> bool encode(Encoder&, Gfx::IntPoint const&); + +template<> ErrorOr decode(Decoder&, Gfx::IntPoint&); } diff --git a/Userland/Libraries/LibGfx/Rect.cpp b/Userland/Libraries/LibGfx/Rect.cpp index c278834ce05..eea30394162 100644 --- a/Userland/Libraries/LibGfx/Rect.cpp +++ b/Userland/Libraries/LibGfx/Rect.cpp @@ -30,12 +30,14 @@ String FloatRect::to_string() const namespace IPC { +template<> bool encode(Encoder& encoder, Gfx::IntRect const& rect) { encoder << rect.location() << rect.size(); return true; } +template<> ErrorOr decode(Decoder& decoder, Gfx::IntRect& rect) { Gfx::IntPoint point; diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index a583da49e2c..493fe2fa037 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -1031,7 +1031,10 @@ struct Formatter> : Formatter { namespace IPC { +template<> bool encode(Encoder&, Gfx::IntRect const&); + +template<> ErrorOr decode(Decoder&, Gfx::IntRect&); } diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.cpp b/Userland/Libraries/LibGfx/ShareableBitmap.cpp index 4aeedda267d..3edffe06963 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.cpp +++ b/Userland/Libraries/LibGfx/ShareableBitmap.cpp @@ -22,6 +22,7 @@ ShareableBitmap::ShareableBitmap(NonnullRefPtr bitmap, Tag) namespace IPC { +template<> bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap) { encoder << shareable_bitmap.is_valid(); @@ -39,6 +40,7 @@ bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap) return true; } +template<> ErrorOr decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap) { bool valid = false; diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.h b/Userland/Libraries/LibGfx/ShareableBitmap.h index 04e59855828..26c074a2970 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.h +++ b/Userland/Libraries/LibGfx/ShareableBitmap.h @@ -34,7 +34,10 @@ private: namespace IPC { +template<> bool encode(Encoder&, Gfx::ShareableBitmap const&); + +template<> ErrorOr decode(Decoder&, Gfx::ShareableBitmap&); } diff --git a/Userland/Libraries/LibGfx/Size.cpp b/Userland/Libraries/LibGfx/Size.cpp index dc122d13bc5..b5c70fe8897 100644 --- a/Userland/Libraries/LibGfx/Size.cpp +++ b/Userland/Libraries/LibGfx/Size.cpp @@ -27,12 +27,14 @@ String FloatSize::to_string() const namespace IPC { +template<> bool encode(Encoder& encoder, Gfx::IntSize const& size) { encoder << size.width() << size.height(); return true; } +template<> ErrorOr decode(Decoder& decoder, Gfx::IntSize& size) { int width = 0; diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index 8054b5c71da..e66c2476564 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -188,7 +188,10 @@ struct Formatter> : Formatter { namespace IPC { +template<> bool encode(Encoder&, Gfx::IntSize const&); + +template<> ErrorOr decode(Decoder&, Gfx::IntSize&); } diff --git a/Userland/Libraries/LibIPC/Decoder.cpp b/Userland/Libraries/LibIPC/Decoder.cpp index 8e2f25b4e6a..2cb9daac03f 100644 --- a/Userland/Libraries/LibIPC/Decoder.cpp +++ b/Userland/Libraries/LibIPC/Decoder.cpp @@ -170,6 +170,7 @@ ErrorOr Decoder::decode([[maybe_unused]] File& file) return {}; } +template<> ErrorOr decode(Decoder& decoder, Core::AnonymousBuffer& buffer) { bool valid; @@ -187,6 +188,7 @@ ErrorOr decode(Decoder& decoder, Core::AnonymousBuffer& buffer) return {}; } +template<> ErrorOr decode(Decoder& decoder, Core::DateTime& datetime) { i64 timestamp; @@ -195,6 +197,7 @@ ErrorOr decode(Decoder& decoder, Core::DateTime& datetime) return {}; } +template<> ErrorOr decode(Decoder& decoder, Core::ProxyData& data) { UnderlyingType type; diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index 132cc8adaac..a8e86f16bfd 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -196,6 +196,7 @@ Encoder& Encoder::operator<<(File const& file) return *this; } +template<> bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) { encoder << buffer.is_valid(); @@ -206,12 +207,14 @@ bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) return true; } +template<> bool encode(Encoder& encoder, Core::DateTime const& datetime) { encoder << static_cast(datetime.timestamp()); return true; } +template<> bool encode(Encoder& encoder, Core::ProxyData const& proxy) { encoder << to_underlying(proxy.type); diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp index d47d6d92bdd..2a43385e648 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp @@ -38,6 +38,7 @@ SameSite same_site_from_string(StringView same_site_mode) } +template<> bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie) { encoder << cookie.name; @@ -56,6 +57,7 @@ bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie) return true; } +template<> ErrorOr IPC::decode(Decoder& decoder, Web::Cookie::Cookie& cookie) { TRY(decoder.decode(cookie.name)); diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.h b/Userland/Libraries/LibWeb/Cookie/Cookie.h index 2192cc5d69d..8c946864943 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.h +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.h @@ -46,7 +46,10 @@ SameSite same_site_from_string(StringView same_site_mode); namespace IPC { +template<> bool encode(Encoder&, Web::Cookie::Cookie const&); + +template<> ErrorOr decode(Decoder&, Web::Cookie::Cookie&); } diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp index bcf44ee5e07..3dd0b2d0fde 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp @@ -347,6 +347,7 @@ Optional parse_date_time(StringView date_string) } +template<> bool IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie) { encoder << cookie.name; @@ -362,6 +363,7 @@ bool IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie) return true; } +template<> ErrorOr IPC::decode(Decoder& decoder, Web::Cookie::ParsedCookie& cookie) { TRY(decoder.decode(cookie.name)); diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h index 58df4efb733..1fc16512194 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h @@ -32,7 +32,10 @@ Optional parse_cookie(String const& cookie_string); namespace IPC { +template<> bool encode(Encoder&, Web::Cookie::ParsedCookie const&); + +template<> ErrorOr decode(Decoder&, Web::Cookie::ParsedCookie&); } diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.cpp b/Userland/Libraries/LibWeb/WebDriver/Response.cpp index c8102d35a29..a44e17f5784 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Response.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Response.cpp @@ -27,6 +27,7 @@ Response::Response(Error&& error) } +template<> bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) { response.visit( @@ -46,6 +47,7 @@ bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) return true; } +template<> ErrorOr IPC::decode(Decoder& decoder, Web::WebDriver::Response& response) { ResponseType type {}; diff --git a/Userland/Libraries/LibWeb/WebDriver/Response.h b/Userland/Libraries/LibWeb/WebDriver/Response.h index 7d7e5c4da6a..1a6663746ed 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Response.h +++ b/Userland/Libraries/LibWeb/WebDriver/Response.h @@ -46,7 +46,10 @@ private: namespace IPC { +template<> bool encode(Encoder&, Web::WebDriver::Response const&); + +template<> ErrorOr decode(Decoder&, Web::WebDriver::Response&); } diff --git a/Userland/Services/WindowServer/ScreenLayout.h b/Userland/Services/WindowServer/ScreenLayout.h index c4d7bd92b21..132794d78df 100644 --- a/Userland/Services/WindowServer/ScreenLayout.h +++ b/Userland/Services/WindowServer/ScreenLayout.h @@ -73,9 +73,16 @@ public: namespace IPC { +template<> bool encode(Encoder&, WindowServer::ScreenLayout::Screen const&); + +template<> ErrorOr decode(Decoder&, WindowServer::ScreenLayout::Screen&); + +template<> bool encode(Encoder&, WindowServer::ScreenLayout const&); + +template<> ErrorOr decode(Decoder&, WindowServer::ScreenLayout&); } diff --git a/Userland/Services/WindowServer/ScreenLayout.ipp b/Userland/Services/WindowServer/ScreenLayout.ipp index 65d950d3c2b..476c630d17b 100644 --- a/Userland/Services/WindowServer/ScreenLayout.ipp +++ b/Userland/Services/WindowServer/ScreenLayout.ipp @@ -394,12 +394,14 @@ bool ScreenLayout::try_auto_add_display_connector(String const& device_path) namespace IPC { -bool encode(Encoder& encoder, const WindowServer::ScreenLayout::Screen& screen) +template<> +bool encode(Encoder& encoder, WindowServer::ScreenLayout::Screen const& screen) { encoder << screen.mode << screen.device << screen.location << screen.resolution << screen.scale_factor; return true; } +template<> ErrorOr decode(Decoder& decoder, WindowServer::ScreenLayout::Screen& screen) { WindowServer::ScreenLayout::Screen::Mode mode; @@ -416,12 +418,14 @@ ErrorOr decode(Decoder& decoder, WindowServer::ScreenLayout::Screen& scree return {}; } -bool encode(Encoder& encoder, const WindowServer::ScreenLayout& screen_layout) +template<> +bool encode(Encoder& encoder, WindowServer::ScreenLayout const& screen_layout) { encoder << screen_layout.screens << screen_layout.main_screen_index; return true; } +template<> ErrorOr decode(Decoder& decoder, WindowServer::ScreenLayout& screen_layout) { Vector screens;