2020-02-15 11:04:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2023-03-05 20:22:38 +00:00
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
2020-02-15 11:04:35 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 11:04:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-04 04:44:34 +00:00
|
|
|
#include <AK/Concepts.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-02-15 11:04:35 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-05-12 16:05:43 +00:00
|
|
|
#include <AK/NumericLimits.h>
|
2020-10-03 12:56:09 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2023-03-05 20:22:38 +00:00
|
|
|
#include <AK/String.h>
|
2022-02-01 19:49:29 +00:00
|
|
|
#include <AK/Try.h>
|
2022-11-09 16:05:04 +00:00
|
|
|
#include <AK/TypeList.h>
|
|
|
|
#include <AK/Variant.h>
|
2022-02-01 19:49:29 +00:00
|
|
|
#include <LibCore/SharedCircularQueue.h>
|
2023-02-09 02:11:50 +00:00
|
|
|
#include <LibCore/Socket.h>
|
2022-12-23 01:40:33 +00:00
|
|
|
#include <LibIPC/Concepts.h>
|
2022-02-01 19:49:29 +00:00
|
|
|
#include <LibIPC/File.h>
|
2020-05-03 20:15:27 +00:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-15 11:04:35 +00:00
|
|
|
#include <LibIPC/Message.h>
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
template<typename T>
|
2022-12-23 01:40:33 +00:00
|
|
|
inline ErrorOr<T> decode(Decoder&)
|
2020-02-15 11:04:35 +00:00
|
|
|
{
|
2020-10-03 12:56:09 +00:00
|
|
|
static_assert(DependentFalse<T>, "Base IPC::decoder() instantiated");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-15 11:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Decoder {
|
|
|
|
public:
|
2023-02-10 00:00:18 +00:00
|
|
|
Decoder(Stream& stream, Core::LocalSocket& socket)
|
2020-02-15 11:04:35 +00:00
|
|
|
: m_stream(stream)
|
2022-01-14 13:12:49 +00:00
|
|
|
, m_socket(socket)
|
2020-02-15 11:04:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<typename T>
|
|
|
|
ErrorOr<T> decode();
|
2020-02-15 11:04:35 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<typename T>
|
|
|
|
ErrorOr<void> decode_into(T& value)
|
2022-04-02 21:43:22 +00:00
|
|
|
{
|
2023-01-14 21:04:55 +00:00
|
|
|
value = TRY(m_stream.read_value<T>());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> decode_into(Bytes bytes)
|
|
|
|
{
|
2023-03-01 14:27:35 +00:00
|
|
|
TRY(m_stream.read_until_filled(bytes));
|
2022-04-02 21:43:22 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
ErrorOr<size_t> decode_size();
|
|
|
|
|
2023-03-05 20:22:38 +00:00
|
|
|
Stream& stream() { return m_stream; }
|
2023-02-08 22:05:44 +00:00
|
|
|
Core::LocalSocket& socket() { return m_socket; }
|
2021-07-04 04:44:34 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
private:
|
2023-02-10 00:00:18 +00:00
|
|
|
Stream& m_stream;
|
2023-02-08 22:05:44 +00:00
|
|
|
Core::LocalSocket& m_socket;
|
2022-12-23 01:40:33 +00:00
|
|
|
};
|
2020-02-15 11:04:35 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<Arithmetic T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
T value { 0 };
|
|
|
|
TRY(decoder.decode_into(value));
|
|
|
|
return value;
|
|
|
|
}
|
2020-05-12 16:05:43 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<Enum T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto value = TRY(decoder.decode<UnderlyingType<T>>());
|
|
|
|
return static_cast<T>(value);
|
|
|
|
}
|
2022-02-01 19:49:29 +00:00
|
|
|
|
2023-03-05 20:22:38 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<String> decode(Decoder&);
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<DeprecatedString> decode(Decoder&);
|
2022-02-01 19:49:29 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<ByteBuffer> decode(Decoder&);
|
2022-11-09 16:05:04 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<JsonValue> decode(Decoder&);
|
|
|
|
|
2023-02-24 18:51:37 +00:00
|
|
|
template<>
|
2023-03-13 15:30:34 +00:00
|
|
|
ErrorOr<Duration> decode(Decoder&);
|
2023-02-24 18:51:37 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<URL> decode(Decoder&);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<File> decode(Decoder&);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<Empty> decode(Decoder&);
|
|
|
|
|
|
|
|
template<Concepts::Vector T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
T vector;
|
2023-01-04 12:08:29 +00:00
|
|
|
|
|
|
|
auto size = TRY(decoder.decode_size());
|
2022-12-23 01:40:33 +00:00
|
|
|
TRY(vector.try_ensure_capacity(size));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
auto value = TRY(decoder.decode<typename T::ValueType>());
|
|
|
|
vector.template unchecked_append(move(value));
|
2022-11-09 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashMap T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
T hashmap;
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
auto size = TRY(decoder.decode_size());
|
|
|
|
TRY(hashmap.try_ensure_capacity(size));
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
auto key = TRY(decoder.decode<typename T::KeyType>());
|
|
|
|
auto value = TRY(decoder.decode<typename T::ValueType>());
|
|
|
|
TRY(hashmap.try_set(move(key), move(value)));
|
2020-05-12 17:02:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
return hashmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::SharedSingleProducerCircularQueue T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto anon_file = TRY(decoder.decode<IPC::File>());
|
2023-01-28 20:12:17 +00:00
|
|
|
return T::create(anon_file.take_fd());
|
2022-12-23 01:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::Optional T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
if (auto has_value = TRY(decoder.decode<bool>()); !has_value)
|
|
|
|
return T {};
|
|
|
|
return T { TRY(decoder.decode<typename T::ValueType>()) };
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
template<Concepts::Variant T, size_t Index = 0>
|
|
|
|
ErrorOr<T> decode_variant(Decoder& decoder, size_t index)
|
|
|
|
{
|
|
|
|
using ElementList = TypeList<T>;
|
|
|
|
|
|
|
|
if constexpr (Index < ElementList::size) {
|
|
|
|
if (index == Index) {
|
|
|
|
using ElementType = typename ElementList::template Type<Index>;
|
|
|
|
return T { TRY(decoder.decode<ElementType>()) };
|
2022-11-09 16:05:04 +00:00
|
|
|
}
|
2022-12-23 01:40:33 +00:00
|
|
|
|
|
|
|
return decode_variant<T, Index + 1>(decoder, index);
|
|
|
|
} else {
|
|
|
|
VERIFY_NOT_REACHED();
|
2022-11-09 16:05:04 +00:00
|
|
|
}
|
2022-12-23 01:40:33 +00:00
|
|
|
}
|
2022-11-09 16:05:04 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::Variant T>
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto index = TRY(decoder.decode<typename T::IndexType>());
|
|
|
|
return Detail::decode_variant<T>(decoder, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This must be last so that it knows about the above specializations.
|
|
|
|
template<typename T>
|
|
|
|
ErrorOr<T> Decoder::decode()
|
|
|
|
{
|
|
|
|
return IPC::decode<T>(*this);
|
|
|
|
}
|
2020-02-15 11:04:35 +00:00
|
|
|
|
|
|
|
}
|