FileList.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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)
  14. {
  15. return realm.heap().allocate<FileList>(realm, realm);
  16. }
  17. FileList::FileList(JS::Realm& realm)
  18. : Bindings::PlatformObject(realm)
  19. {
  20. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  21. }
  22. FileList::~FileList() = default;
  23. void FileList::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(FileList);
  27. }
  28. Optional<JS::Value> FileList::item_value(size_t index) const
  29. {
  30. if (index >= m_files.size())
  31. return {};
  32. return m_files[index].ptr();
  33. }
  34. void FileList::visit_edges(Cell::Visitor& visitor)
  35. {
  36. Base::visit_edges(visitor);
  37. visitor.visit(m_files);
  38. }
  39. WebIDL::ExceptionOr<void> FileList::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory)
  40. {
  41. auto& vm = this->vm();
  42. // 1. Set serialized.[[Files]] to an empty list.
  43. // 2. For each file in value, append the sub-serialization of file to serialized.[[Files]].
  44. HTML::serialize_primitive_type(serialized, m_files.size());
  45. for (auto& file : m_files)
  46. serialized.extend(TRY(HTML::structured_serialize_internal(vm, file, for_storage, memory)));
  47. return {};
  48. }
  49. WebIDL::ExceptionOr<void> FileList::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
  50. {
  51. auto& vm = this->vm();
  52. auto& realm = *vm.current_realm();
  53. // 1. For each file of serialized.[[Files]], add the sub-deserialization of file to value.
  54. auto size = HTML::deserialize_primitive_type<size_t>(serialized, position);
  55. for (size_t i = 0; i < size; ++i) {
  56. auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
  57. if (deserialized_record.value.has_value() && is<File>(deserialized_record.value.value().as_object()))
  58. m_files.append(dynamic_cast<File&>(deserialized_record.value.release_value().as_object()));
  59. position = deserialized_record.position;
  60. }
  61. return {};
  62. }
  63. }