1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/FileList.h"
11 #include "ErrorList.h"
12 #include "js/RootingAPI.h"
13 #include "mozilla/Assertions.h"
14 #include "mozilla/ErrorResult.h"
15 #include "mozilla/MacroForEach.h"
16 #include "mozilla/RefPtr.h"
17 #include "mozilla/dom/BindingDeclarations.h"
18 #include "mozilla/dom/File.h"
19 #include "mozilla/dom/FileListBinding.h"
20 #include "mozilla/fallible.h"
21 #include "nsCycleCollectionParticipant.h"
22 #include "nsCycleCollectionTraversalCallback.h"
23 #include "nsISupports.h"
25 #include "nsWrapperCache.h"
27 namespace mozilla::dom
{
29 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileList
, mFiles
, mParent
)
31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileList
)
32 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
33 NS_INTERFACE_MAP_ENTRY(nsISupports
)
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(FileList
)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(FileList
)
39 FileList::FileList(nsISupports
* aParent
) : mParent(aParent
) {}
41 FileList::~FileList() = default;
43 JSObject
* FileList::WrapObject(JSContext
* aCx
,
44 JS::Handle
<JSObject
*> aGivenProto
) {
45 return mozilla::dom::FileList_Binding::Wrap(aCx
, this, aGivenProto
);
48 bool FileList::Append(File
* aFile
) {
50 return mFiles
.AppendElement(aFile
, fallible
);
53 bool FileList::Remove(uint32_t aIndex
) {
54 if (aIndex
< mFiles
.Length()) {
55 mFiles
.RemoveElementAt(aIndex
);
62 void FileList::Clear() { return mFiles
.Clear(); }
64 File
* FileList::Item(uint32_t aIndex
) const {
65 if (aIndex
>= mFiles
.Length()) {
69 return mFiles
[aIndex
];
72 File
* FileList::IndexedGetter(uint32_t aIndex
, bool& aFound
) const {
73 aFound
= aIndex
< mFiles
.Length();
77 void FileList::ToSequence(Sequence
<RefPtr
<File
>>& aSequence
,
78 ErrorResult
& aRv
) const {
79 MOZ_ASSERT(aSequence
.IsEmpty());
80 if (mFiles
.IsEmpty()) {
84 if (!aSequence
.SetLength(mFiles
.Length(), mozilla::fallible_t())) {
85 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
89 for (uint32_t i
= 0; i
< mFiles
.Length(); ++i
) {
90 aSequence
[i
] = mFiles
[i
];
94 } // namespace mozilla::dom