2022-04-05 02:24:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Daniel Ehrenberg <dan@littledan.dev>
|
|
|
|
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
|
2024-02-23 18:30:50 +01:00
|
|
|
* Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2022-04-05 02:24:10 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Result.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
2023-12-07 15:52:08 -07:00
|
|
|
#include <LibIPC/Forward.h>
|
2022-04-05 02:24:10 +02:00
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
|
|
|
|
|
|
|
// Structured serialize is an entirely different format from IPC because:
|
|
|
|
// - It contains representation of type information
|
|
|
|
// - It may contain circularities
|
|
|
|
// - It is restricted to JS values
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
|
|
using SerializationRecord = Vector<u32>;
|
2023-11-11 20:14:46 +02:00
|
|
|
using SerializationMemory = HashMap<JS::Handle<JS::Value>, u32>;
|
|
|
|
using DeserializationMemory = JS::MarkedVector<JS::Value>;
|
2022-04-05 02:24:10 +02:00
|
|
|
|
2023-12-07 15:52:08 -07:00
|
|
|
struct TransferDataHolder {
|
|
|
|
Vector<u8> data;
|
|
|
|
Vector<IPC::File> fds;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SerializedTransferRecord {
|
|
|
|
SerializationRecord serialized;
|
|
|
|
Vector<TransferDataHolder> transfer_data_holders;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DeserializedTransferRecord {
|
|
|
|
JS::Value deserialized;
|
2023-12-12 13:30:16 -07:00
|
|
|
Vector<JS::Handle<JS::Object>> transferred_values;
|
2023-12-07 15:52:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class TransferType : u8 {
|
|
|
|
MessagePort,
|
|
|
|
};
|
|
|
|
|
2022-04-05 02:24:10 +02:00
|
|
|
WebIDL::ExceptionOr<SerializationRecord> structured_serialize(JS::VM& vm, JS::Value);
|
|
|
|
WebIDL::ExceptionOr<SerializationRecord> structured_serialize_for_storage(JS::VM& vm, JS::Value);
|
2023-09-12 16:41:13 -06:00
|
|
|
WebIDL::ExceptionOr<SerializationRecord> structured_serialize_internal(JS::VM& vm, JS::Value, bool for_storage, SerializationMemory&);
|
2022-04-05 02:24:10 +02:00
|
|
|
|
2023-11-11 20:14:46 +02:00
|
|
|
WebIDL::ExceptionOr<JS::Value> structured_deserialize(JS::VM& vm, SerializationRecord const& serialized, JS::Realm& target_realm, Optional<DeserializationMemory>);
|
2022-04-05 02:24:10 +02:00
|
|
|
|
2024-03-02 16:45:12 +01:00
|
|
|
void serialize_boolean_primitive(SerializationRecord& serialized, JS::Value& value);
|
|
|
|
void serialize_number_primitive(SerializationRecord& serialized, JS::Value& value);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_big_int_primitive(JS::VM& vm, SerializationRecord& serialized, JS::Value& value);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_string_primitive(JS::VM& vm, SerializationRecord& serialized, JS::Value& value);
|
|
|
|
void serialize_boolean_object(SerializationRecord& serialized, JS::Value& value);
|
|
|
|
void serialize_number_object(SerializationRecord& serialized, JS::Value& value);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_big_int_object(JS::VM& vm, SerializationRecord& serialized, JS::Value& value);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_string_object(JS::VM& vm, SerializationRecord& serialized, JS::Value& value);
|
|
|
|
void serialize_date_object(SerializationRecord& serialized, JS::Value& value);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_reg_exp_object(JS::VM& vm, SerializationRecord& serialized, JS::Value& value);
|
|
|
|
|
2024-02-23 18:30:50 +01:00
|
|
|
WebIDL::ExceptionOr<void> serialize_bytes(JS::VM& vm, Vector<u32>& vector, ReadonlyBytes bytes);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_string(JS::VM& vm, Vector<u32>& vector, DeprecatedFlyString const& string);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_string(JS::VM& vm, Vector<u32>& vector, String const& string);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_string(JS::VM& vm, Vector<u32>& vector, JS::PrimitiveString const& primitive_string);
|
|
|
|
WebIDL::ExceptionOr<void> serialize_array_buffer(JS::VM& vm, Vector<u32>& vector, JS::ArrayBuffer const& array_buffer, bool for_storage);
|
|
|
|
template<OneOf<JS::TypedArrayBase, JS::DataView> ViewType>
|
|
|
|
WebIDL::ExceptionOr<void> serialize_viewed_array_buffer(JS::VM& vm, Vector<u32>& vector, ViewType const& view, bool for_storage, SerializationMemory& memory);
|
|
|
|
|
2024-02-22 18:28:34 +01:00
|
|
|
WebIDL::ExceptionOr<ByteBuffer> deserialize_bytes(JS::VM& vm, ReadonlySpan<u32> vector, size_t& position);
|
|
|
|
WebIDL::ExceptionOr<String> deserialize_string(JS::VM& vm, ReadonlySpan<u32> vector, size_t& position);
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::PrimitiveString>> deserialize_string_primitive(JS::VM& vm, ReadonlySpan<u32> vector, size_t& position);
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::BigInt>> deserialize_big_int_primitive(JS::VM& vm, ReadonlySpan<u32> vector, size_t& position);
|
|
|
|
|
2023-12-12 13:30:16 -07:00
|
|
|
WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer(JS::VM& vm, JS::Value value, Vector<JS::Handle<JS::Object>> const& transfer_list);
|
2023-12-18 17:24:31 -07:00
|
|
|
WebIDL::ExceptionOr<DeserializedTransferRecord> structured_deserialize_with_transfer(JS::VM& vm, SerializedTransferRecord&);
|
2022-04-05 02:24:10 +02:00
|
|
|
|
|
|
|
}
|
2023-12-19 15:28:56 -07:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<void> encode(Encoder&, ::Web::HTML::SerializedTransferRecord const&);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<void> encode(Encoder&, ::Web::HTML::TransferDataHolder const&);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<::Web::HTML::SerializedTransferRecord> decode(Decoder&);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ErrorOr<::Web::HTML::TransferDataHolder> decode(Decoder&);
|
|
|
|
|
|
|
|
}
|