FileList.cpp 3.3 KB

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