LibWeb: Add {,de}serialization steps for FileList

This commit is contained in:
Kenneth Myhra 2024-03-18 21:58:25 +01:00 committed by Andreas Kling
commit c92f556aa5
Notes: sideshowbarker 2024-07-16 20:51:53 +09:00
5 changed files with 91 additions and 1 deletions

View file

@ -0,0 +1,5 @@
Select files...4 files selected. input1:
file1: text/plain: Contents for file1
file2: text/plain: Contents for file2
file3: text/plain: Contents for file3
file4: text/plain: Contents for file4

View file

@ -0,0 +1,32 @@
<input id="input1" type="file" multiple />
<script src="../include.js"></script>
<script>
const runTest = async id => {
let input = document.getElementById(id);
return new Promise(resolve => {
input.addEventListener("input", async () => {
println(`${id}:`);
let files = structuredClone(input.files);
for (let file of input.files) {
const text = await file.text();
println(`${file.name}: ${file.type}: ${text}`);
}
resolve();
});
internals.dispatchUserActivatedEvent(input, new Event("mousedown"));
input.showPicker();
});
}
asyncTest(async done => {
await runTest("input1");
done();
});
</script>

View file

@ -18,6 +18,11 @@ JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::Nonnull
return realm.heap().allocate<FileList>(realm, realm, move(files));
}
JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm)
{
return realm.heap().allocate<FileList>(realm, realm);
}
FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
: Bindings::PlatformObject(realm)
, m_files(move(files))
@ -25,6 +30,12 @@ FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
}
FileList::FileList(JS::Realm& realm)
: Bindings::PlatformObject(realm)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
}
FileList::~FileList() = default;
void FileList::initialize(JS::Realm& realm)
@ -59,4 +70,34 @@ void FileList::visit_edges(Cell::Visitor& visitor)
visitor.visit(file);
}
WebIDL::ExceptionOr<void> FileList::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory)
{
auto& vm = this->vm();
// 1. Set serialized.[[Files]] to an empty list.
// 2. For each file in value, append the sub-serialization of file to serialized.[[Files]].
HTML::serialize_primitive_type(serialized, m_files.size());
for (auto& file : m_files)
serialized.extend(TRY(HTML::structured_serialize_internal(vm, file, for_storage, memory)));
return {};
}
WebIDL::ExceptionOr<void> FileList::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
{
auto& vm = this->vm();
auto& realm = *vm.current_realm();
// 1. For each file of serialized.[[Files]], add the sub-deserialization of file to value.
auto size = HTML::deserialize_primitive_type<size_t>(serialized, position);
for (size_t i = 0; i < size; ++i) {
auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
if (deserialized_record.value.has_value() && is<File>(deserialized_record.value.value().as_object()))
m_files.append(dynamic_cast<File&>(deserialized_record.value.release_value().as_object()));
position = deserialized_record.position;
}
return {};
}
}

View file

@ -15,12 +15,16 @@
namespace Web::FileAPI {
class FileList : public Bindings::PlatformObject {
class FileList
: public Bindings::PlatformObject
, public Bindings::Serializable {
WEB_PLATFORM_OBJECT(FileList, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(FileList);
public:
[[nodiscard]] static JS::NonnullGCPtr<FileList> create(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
[[nodiscard]] static JS::NonnullGCPtr<FileList> create(JS::Realm&);
virtual ~FileList() override;
// https://w3c.github.io/FileAPI/#dfn-length
@ -41,8 +45,13 @@ public:
virtual bool is_supported_property_index(u32 index) const override;
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
virtual StringView interface_name() const override { return "FileList"sv; }
virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory&) override;
virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory&) override;
private:
FileList(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
explicit FileList(JS::Realm&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;

View file

@ -36,6 +36,7 @@
#include <LibWeb/Crypto/CryptoKey.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/FileAPI/FileList.h>
#include <LibWeb/Geometry/DOMMatrix.h>
#include <LibWeb/Geometry/DOMMatrixReadOnly.h>
#include <LibWeb/Geometry/DOMPoint.h>
@ -974,6 +975,8 @@ private:
return FileAPI::Blob::create(realm);
if (interface_name == "File"sv)
return FileAPI::File::create(realm);
if (interface_name == "FileList"sv)
return FileAPI::FileList::create(realm);
if (interface_name == "DOMMatrixReadOnly"sv)
return Geometry::DOMMatrixReadOnly::create(realm);
if (interface_name == "DOMMatrix"sv)