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