2022-10-04 00:10:39 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
2024-04-26 21:09:58 -03:00
|
|
|
#include <LibWeb/Bindings/FileListPrototype.h>
|
2022-10-04 00:10:39 -03:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-01-09 20:05:03 -03:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2022-10-04 00:10:39 -03:00
|
|
|
#include <LibWeb/FileAPI/FileList.h>
|
2024-11-22 07:51:40 -03:00
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
2022-10-04 00:10:39 -03:00
|
|
|
|
|
|
|
|
namespace Web::FileAPI {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(FileList);
|
2023-12-23 11:15:27 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<FileList> FileList::create(JS::Realm& realm)
|
2024-03-18 17:58:25 -03:00
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
return realm.create<FileList>(realm);
|
2024-03-18 17:58:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileList::FileList(JS::Realm& realm)
|
|
|
|
|
: Bindings::PlatformObject(realm)
|
|
|
|
|
{
|
|
|
|
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 00:10:39 -03:00
|
|
|
FileList::~FileList() = default;
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void FileList::initialize(JS::Realm& realm)
|
2023-01-10 08:56:59 -03:00
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(FileList);
|
2025-04-20 11:22:57 -03:00
|
|
|
Base::initialize(realm);
|
2023-01-10 08:56:59 -03:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 03:15:51 -03:00
|
|
|
Optional<JS::Value> FileList::item_value(size_t index) const
|
2022-10-04 00:10:39 -03:00
|
|
|
{
|
|
|
|
|
if (index >= m_files.size())
|
2024-07-25 03:15:51 -03:00
|
|
|
return {};
|
2022-10-04 00:10:39 -03:00
|
|
|
|
|
|
|
|
return m_files[index].ptr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileList::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2024-04-15 08:58:21 -03:00
|
|
|
visitor.visit(m_files);
|
2022-10-04 00:10:39 -03:00
|
|
|
}
|
|
|
|
|
|
2025-07-17 10:51:04 -03:00
|
|
|
WebIDL::ExceptionOr<void> FileList::serialization_steps(HTML::TransferDataEncoder& serialized, bool for_storage, HTML::SerializationMemory& memory)
|
2024-03-18 17:58:25 -03:00
|
|
|
{
|
|
|
|
|
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]].
|
2025-07-17 10:51:04 -03:00
|
|
|
serialized.encode(m_files.size());
|
|
|
|
|
|
|
|
|
|
for (auto file : m_files)
|
|
|
|
|
serialized.append(TRY(HTML::structured_serialize_internal(vm, file, for_storage, memory)));
|
2024-03-18 17:58:25 -03:00
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 10:51:04 -03:00
|
|
|
WebIDL::ExceptionOr<void> FileList::deserialization_steps(HTML::TransferDataDecoder& serialized, HTML::DeserializationMemory& memory)
|
2024-03-18 17:58:25 -03:00
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
2025-07-17 10:51:04 -03:00
|
|
|
auto& realm = this->realm();
|
2024-03-18 17:58:25 -03:00
|
|
|
|
|
|
|
|
// 1. For each file of serialized.[[Files]], add the sub-deserialization of file to value.
|
2025-07-17 10:51:04 -03:00
|
|
|
auto size = serialized.decode<size_t>();
|
|
|
|
|
|
2024-03-18 17:58:25 -03:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2025-07-17 10:51:04 -03:00
|
|
|
auto deserialized = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory));
|
|
|
|
|
|
|
|
|
|
if (auto* file = as_if<File>(deserialized.as_object()))
|
|
|
|
|
m_files.append(*file);
|
2024-03-18 17:58:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 00:10:39 -03:00
|
|
|
}
|