FileList.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/FileAPI/FileList.h>
  10. namespace Web::FileAPI {
  11. JS_DEFINE_ALLOCATOR(FileList);
  12. JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  13. {
  14. return realm.heap().allocate<FileList>(realm, realm, move(files));
  15. }
  16. JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm)
  17. {
  18. return realm.heap().allocate<FileList>(realm, realm);
  19. }
  20. FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
  21. : Bindings::PlatformObject(realm)
  22. , m_files(move(files))
  23. {
  24. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  25. }
  26. FileList::FileList(JS::Realm& realm)
  27. : Bindings::PlatformObject(realm)
  28. {
  29. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  30. }
  31. FileList::~FileList() = default;
  32. void FileList::initialize(JS::Realm& realm)
  33. {
  34. Base::initialize(realm);
  35. WEB_SET_PROTOTYPE_FOR_INTERFACE(FileList);
  36. }
  37. // https://w3c.github.io/FileAPI/#dfn-item
  38. bool FileList::is_supported_property_index(u32 index) const
  39. {
  40. // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
  41. // If there are no such File objects, then there are no supported property indices.
  42. if (m_files.is_empty())
  43. return false;
  44. return index < m_files.size();
  45. }
  46. WebIDL::ExceptionOr<JS::Value> FileList::item_value(size_t index) const
  47. {
  48. if (index >= m_files.size())
  49. return JS::js_undefined();
  50. return m_files[index].ptr();
  51. }
  52. void FileList::visit_edges(Cell::Visitor& visitor)
  53. {
  54. Base::visit_edges(visitor);
  55. visitor.visit(m_files);
  56. }
  57. WebIDL::ExceptionOr<void> FileList::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory)
  58. {
  59. auto& vm = this->vm();
  60. // 1. Set serialized.[[Files]] to an empty list.
  61. // 2. For each file in value, append the sub-serialization of file to serialized.[[Files]].
  62. HTML::serialize_primitive_type(serialized, m_files.size());
  63. for (auto& file : m_files)
  64. serialized.extend(TRY(HTML::structured_serialize_internal(vm, file, for_storage, memory)));
  65. return {};
  66. }
  67. WebIDL::ExceptionOr<void> FileList::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
  68. {
  69. auto& vm = this->vm();
  70. auto& realm = *vm.current_realm();
  71. // 1. For each file of serialized.[[Files]], add the sub-deserialization of file to value.
  72. auto size = HTML::deserialize_primitive_type<size_t>(serialized, position);
  73. for (size_t i = 0; i < size; ++i) {
  74. auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
  75. if (deserialized_record.value.has_value() && is<File>(deserialized_record.value.value().as_object()))
  76. m_files.append(dynamic_cast<File&>(deserialized_record.value.release_value().as_object()));
  77. position = deserialized_record.position;
  78. }
  79. return {};
  80. }
  81. }