2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2023-03-05 20:22:38 +00:00
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-30 01:41:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-07-04 04:44:34 +00:00
|
|
|
#include <AK/Concepts.h>
|
2022-09-13 15:42:39 +00:00
|
|
|
#include <AK/HashMap.h>
|
2021-07-04 04:44:34 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2022-11-09 16:05:04 +00:00
|
|
|
#include <AK/Variant.h>
|
2022-02-01 19:49:29 +00:00
|
|
|
#include <LibCore/SharedCircularQueue.h>
|
2023-01-02 03:51:39 +00:00
|
|
|
#include <LibIPC/Concepts.h>
|
2024-04-16 19:46:30 +00:00
|
|
|
#include <LibIPC/File.h>
|
2020-05-03 20:15:27 +00:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-06 13:54:09 +00:00
|
|
|
#include <LibIPC/Message.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/Forward.h>
|
2019-12-30 01:41:45 +00:00
|
|
|
|
2020-02-05 18:57:18 +00:00
|
|
|
namespace IPC {
|
|
|
|
|
2020-05-12 16:05:43 +00:00
|
|
|
template<typename T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, T const&)
|
2020-05-12 16:05:43 +00:00
|
|
|
{
|
2020-10-03 12:56:09 +00:00
|
|
|
static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-12 16:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 18:57:18 +00:00
|
|
|
class Encoder {
|
2019-12-30 01:41:45 +00:00
|
|
|
public:
|
2020-02-05 18:57:18 +00:00
|
|
|
explicit Encoder(MessageBuffer& buffer)
|
2019-12-30 01:41:45 +00:00
|
|
|
: m_buffer(buffer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:05:43 +00:00
|
|
|
template<typename T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(T const& value);
|
2020-05-12 16:05:43 +00:00
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
ErrorOr<void> extend_capacity(size_t capacity)
|
2022-02-01 19:49:29 +00:00
|
|
|
{
|
2024-01-03 01:27:29 +00:00
|
|
|
TRY(m_buffer.extend_data_capacity(capacity));
|
|
|
|
return {};
|
2022-02-01 19:49:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
ErrorOr<void> append(u8 const* values, size_t count)
|
2021-07-04 04:44:34 +00:00
|
|
|
{
|
2024-01-03 01:27:29 +00:00
|
|
|
TRY(m_buffer.append_data(values, count));
|
2023-01-02 03:51:39 +00:00
|
|
|
return {};
|
2021-07-04 04:44:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
ErrorOr<void> append_file_descriptor(int fd)
|
2020-05-12 16:05:43 +00:00
|
|
|
{
|
2024-01-03 01:27:29 +00:00
|
|
|
TRY(m_buffer.append_file_descriptor(fd));
|
|
|
|
return {};
|
2020-05-12 16:05:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
ErrorOr<void> encode_size(size_t size);
|
|
|
|
|
2019-12-30 01:41:45 +00:00
|
|
|
private:
|
2020-02-05 18:57:18 +00:00
|
|
|
MessageBuffer& m_buffer;
|
2019-12-30 01:41:45 +00:00
|
|
|
};
|
2020-02-05 18:57:18 +00:00
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<Arithmetic T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& value)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2024-01-03 01:24:28 +00:00
|
|
|
TRY(encoder.append(reinterpret_cast<u8 const*>(&value), sizeof(value)));
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Enum T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& value)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
|
|
|
return encoder.encode(to_underlying(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, float const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, double const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-03-05 20:22:38 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<void> encode(Encoder&, String const&);
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, StringView const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, ByteString const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, ByteBuffer const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, JsonValue const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-02-24 18:51:37 +00:00
|
|
|
template<>
|
2024-07-17 05:44:07 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, AK::Duration const&);
|
2023-02-24 18:51:37 +00:00
|
|
|
|
2023-03-13 21:06:22 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<void> encode(Encoder&, UnixDateTime const&);
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2024-03-18 03:22:27 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, URL::URL const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2024-10-05 02:33:34 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<void> encode(Encoder&, URL::Origin const&);
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, File const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, Empty const&);
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2024-03-05 16:12:18 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
ErrorOr<void> encode(Encoder& encoder, Array<T, N> const& array)
|
|
|
|
{
|
|
|
|
TRY(encoder.encode_size(array.size()));
|
|
|
|
|
|
|
|
for (auto const& value : array)
|
|
|
|
TRY(encoder.encode(value));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<Concepts::Vector T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& vector)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
// NOTE: Do not change this encoding without also updating LibC/netdb.cpp.
|
|
|
|
TRY(encoder.encode_size(vector.size()));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
for (auto const& value : vector)
|
|
|
|
TRY(encoder.encode(value));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashMap T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& hashmap)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
TRY(encoder.encode_size(hashmap.size()));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
for (auto it : hashmap) {
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(it.key));
|
|
|
|
TRY(encoder.encode(it.value));
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::SharedSingleProducerCircularQueue T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& queue)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2024-04-16 19:46:30 +00:00
|
|
|
TRY(encoder.encode(TRY(IPC::File::clone_fd(queue.fd()))));
|
|
|
|
return {};
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::Optional T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& optional)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(optional.has_value()));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
if (optional.has_value())
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(optional.value()));
|
|
|
|
|
|
|
|
return {};
|
2023-01-02 03:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::Variant T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, T const& variant)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(variant.index()));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
|
|
|
return variant.visit([&](auto const& value) {
|
|
|
|
return encoder.encode(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This must be last so that it knows about the above specializations.
|
|
|
|
template<typename T>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> Encoder::encode(T const& value)
|
2023-01-02 03:51:39 +00:00
|
|
|
{
|
|
|
|
return IPC::encode(*this, value);
|
|
|
|
}
|
|
|
|
|
2020-02-05 18:57:18 +00:00
|
|
|
}
|