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/. */
6
7 #include "mozilla/dom/Directory.h"
8 #include "mozilla/dom/FileList.h"
9 #include "mozilla/dom/FileListBinding.h"
10 #include "mozilla/dom/File.h"
11
12 namespace mozilla {
13 namespace dom {
14
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileList,mFiles,mParent)15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileList, mFiles, mParent)
16
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileList)
18 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
19 NS_INTERFACE_MAP_ENTRY(nsISupports)
20 NS_INTERFACE_MAP_END
21
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(FileList)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(FileList)
24
25 JSObject* FileList::WrapObject(JSContext* aCx,
26 JS::Handle<JSObject*> aGivenProto) {
27 return mozilla::dom::FileList_Binding::Wrap(aCx, this, aGivenProto);
28 }
29
Item(uint32_t aIndex) const30 File* FileList::Item(uint32_t aIndex) const {
31 if (aIndex >= mFiles.Length()) {
32 return nullptr;
33 }
34
35 return mFiles[aIndex];
36 }
37
IndexedGetter(uint32_t aIndex,bool & aFound) const38 File* FileList::IndexedGetter(uint32_t aIndex, bool& aFound) const {
39 aFound = aIndex < mFiles.Length();
40 return Item(aIndex);
41 }
42
ToSequence(Sequence<RefPtr<File>> & aSequence,ErrorResult & aRv) const43 void FileList::ToSequence(Sequence<RefPtr<File>>& aSequence,
44 ErrorResult& aRv) const {
45 MOZ_ASSERT(aSequence.IsEmpty());
46 if (mFiles.IsEmpty()) {
47 return;
48 }
49
50 if (!aSequence.SetLength(mFiles.Length(), mozilla::fallible_t())) {
51 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
52 return;
53 }
54
55 for (uint32_t i = 0; i < mFiles.Length(); ++i) {
56 aSequence[i] = mFiles[i];
57 }
58 }
59
60 } // namespace dom
61 } // namespace mozilla
62