Serenity Operating System
1/*
2 * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibJS/Runtime/Realm.h>
8#include <LibWeb/Bindings/Intrinsics.h>
9#include <LibWeb/Bindings/LegacyPlatformObject.h>
10#include <LibWeb/FileAPI/FileList.h>
11
12namespace Web::FileAPI {
13
14WebIDL::ExceptionOr<JS::NonnullGCPtr<FileList>> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
15{
16 return MUST_OR_THROW_OOM(realm.heap().allocate<FileList>(realm, realm, move(files)));
17}
18
19FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
20 : Bindings::LegacyPlatformObject(realm)
21 , m_files(move(files))
22{
23}
24
25FileList::~FileList() = default;
26
27JS::ThrowCompletionOr<void> FileList::initialize(JS::Realm& realm)
28{
29 MUST_OR_THROW_OOM(Base::initialize(realm));
30 set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"));
31
32 return {};
33}
34
35// https://w3c.github.io/FileAPI/#dfn-item
36bool FileList::is_supported_property_index(u32 index) const
37{
38 // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
39 // If there are no such File objects, then there are no supported property indices.
40 if (m_files.is_empty())
41 return false;
42
43 return m_files.size() < index;
44}
45
46WebIDL::ExceptionOr<JS::Value> FileList::item_value(size_t index) const
47{
48 if (index >= m_files.size())
49 return JS::js_undefined();
50
51 return m_files[index].ptr();
52}
53
54void FileList::visit_edges(Cell::Visitor& visitor)
55{
56 Base::visit_edges(visitor);
57 for (auto file : m_files)
58 visitor.visit(file);
59}
60
61}