FileSystemRootDirectoryReader.cpp (3144B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "FileSystemRootDirectoryReader.h" 8 9 #include "CallbackRunnables.h" 10 #include "mozilla/dom/FileSystemDirectoryReaderBinding.h" 11 #include "mozilla/dom/FileSystemUtils.h" 12 #include "nsIGlobalObject.h" 13 14 namespace mozilla::dom { 15 16 namespace { 17 18 class EntriesCallbackRunnable final : public Runnable { 19 public: 20 EntriesCallbackRunnable(FileSystemEntriesCallback* aCallback, 21 const Sequence<RefPtr<FileSystemEntry>>& aEntries) 22 : Runnable("EntriesCallbackRunnable"), 23 mCallback(aCallback), 24 mEntries(aEntries) { 25 MOZ_ASSERT(aCallback); 26 } 27 28 // MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See 29 // bug 1535398. 30 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override { 31 Sequence<OwningNonNull<FileSystemEntry>> entries; 32 for (uint32_t i = 0; i < mEntries.Length(); ++i) { 33 if (!entries.AppendElement(mEntries[i].forget(), fallible)) { 34 return NS_ERROR_OUT_OF_MEMORY; 35 } 36 } 37 38 mCallback->Call(entries); 39 return NS_OK; 40 } 41 42 private: 43 const RefPtr<FileSystemEntriesCallback> mCallback; 44 Sequence<RefPtr<FileSystemEntry>> mEntries; 45 }; 46 47 } // anonymous namespace 48 49 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryReader, 50 FileSystemDirectoryReader, mEntries) 51 52 NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryReader, 53 FileSystemDirectoryReader) 54 NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryReader, 55 FileSystemDirectoryReader) 56 57 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemRootDirectoryReader) 58 NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryReader) 59 60 FileSystemRootDirectoryReader::FileSystemRootDirectoryReader( 61 FileSystemDirectoryEntry* aParentEntry, FileSystem* aFileSystem, 62 const Sequence<RefPtr<FileSystemEntry>>& aEntries) 63 : FileSystemDirectoryReader(aParentEntry, aFileSystem, nullptr), 64 mEntries(aEntries), 65 mAlreadyRead(false) { 66 MOZ_ASSERT(aParentEntry); 67 MOZ_ASSERT(aFileSystem); 68 } 69 70 FileSystemRootDirectoryReader::~FileSystemRootDirectoryReader() = default; 71 72 void FileSystemRootDirectoryReader::ReadEntries( 73 FileSystemEntriesCallback& aSuccessCallback, 74 const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback, 75 ErrorResult& aRv) { 76 if (mAlreadyRead) { 77 RefPtr<EmptyEntriesCallbackRunnable> runnable = 78 new EmptyEntriesCallbackRunnable(&aSuccessCallback); 79 80 aRv = 81 FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget()); 82 return; 83 } 84 85 // This object can be used only once. 86 mAlreadyRead = true; 87 88 RefPtr<EntriesCallbackRunnable> runnable = 89 new EntriesCallbackRunnable(&aSuccessCallback, mEntries); 90 91 aRv = FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget()); 92 } 93 94 } // namespace mozilla::dom