Selaa lähdekoodia

LibWeb: Make Blob a Serializable object

Kenneth Myhra 1 vuosi sitten
vanhempi
commit
394c38729f

+ 36 - 1
Userland/Libraries/LibWeb/FileAPI/Blob.cpp

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
+ * Copyright (c) 2022-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
@@ -15,6 +15,7 @@
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/FileAPI/Blob.h>
 #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
+#include <LibWeb/HTML/StructuredSerialize.h>
 #include <LibWeb/Infra/Strings.h>
 #include <LibWeb/Streams/AbstractOperations.h>
 #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
@@ -148,6 +149,40 @@ void Blob::initialize(JS::Realm& realm)
     set_prototype(&Bindings::ensure_web_prototype<Bindings::BlobPrototype>(realm, "Blob"_fly_string));
 }
 
+WebIDL::ExceptionOr<void> Blob::serialization_steps(HTML::SerializationRecord& record, bool)
+{
+    auto& vm = this->vm();
+
+    TRY(HTML::serialize_string(vm, record, interface_name()));
+
+    //  FIXME: 1. Set serialized.[[SnapshotState]] to value’s snapshot state.
+
+    // NON-STANDARD: FileAPI spec doesn't specify that type should be serialized, although
+    //               to be conformant with other browsers this needs to be serialized.
+    TRY(HTML::serialize_string(vm, record, m_type));
+
+    // 2. Set serialized.[[ByteSequence]] to value’s underlying byte sequence.
+    TRY(HTML::serialize_bytes(vm, record, m_byte_buffer.bytes()));
+
+    return {};
+}
+
+WebIDL::ExceptionOr<void> Blob::deserialization_steps(ReadonlySpan<u32> const& record, size_t& position)
+{
+    auto& vm = this->vm();
+
+    // FIXME: 1. Set value’s snapshot state to serialized.[[SnapshotState]].
+
+    // NON-STANDARD: FileAPI spec doesn't specify that type should be deserialized, although
+    //               to be conformant with other browsers this needs to be deserialized.
+    m_type = TRY(HTML::deserialize_string(vm, record, position));
+
+    // 2. Set value’s underlying byte sequence to serialized.[[ByteSequence]].
+    m_byte_buffer = TRY(HTML::deserialize_bytes(vm, record, position));
+
+    return {};
+}
+
 // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
 JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
 {

+ 10 - 2
Userland/Libraries/LibWeb/FileAPI/Blob.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
+ * Copyright (c) 2022-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -10,6 +10,7 @@
 #include <AK/Vector.h>
 #include <LibWeb/Bindings/BlobPrototype.h>
 #include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Bindings/Serializable.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/WebIDL/ExceptionOr.h>
 
@@ -26,7 +27,9 @@ struct BlobPropertyBag {
 [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
 [[nodiscard]] bool is_basic_latin(StringView view);
 
-class Blob : public Bindings::PlatformObject {
+class Blob
+    : public Bindings::PlatformObject
+    , public Bindings::Serializable {
     WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject);
     JS_DECLARE_ALLOCATOR(Blob);
 
@@ -52,6 +55,11 @@ public:
 
     WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> get_stream();
 
+    virtual StringView interface_name() const override { return "Blob"sv; }
+
+    virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage) override;
+    virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position) override;
+
 protected:
     Blob(JS::Realm&, ByteBuffer, String type);
     Blob(JS::Realm&, ByteBuffer);