FileSystemFileEntry.cpp (3247B)
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 "FileSystemFileEntry.h" 8 9 #include "CallbackRunnables.h" 10 #include "mozilla/dom/File.h" 11 #include "mozilla/dom/FileSystemFileEntryBinding.h" 12 #include "mozilla/dom/FileSystemUtils.h" 13 #include "mozilla/dom/MultipartBlobImpl.h" 14 15 namespace mozilla::dom { 16 17 namespace { 18 19 class FileCallbackRunnable final : public Runnable { 20 public: 21 FileCallbackRunnable(FileCallback* aCallback, File* aFile) 22 : Runnable("FileCallbackRunnable"), mCallback(aCallback), mFile(aFile) { 23 MOZ_ASSERT(aCallback); 24 MOZ_ASSERT(aFile); 25 } 26 27 // MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See 28 // bug 1535398. 29 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override { 30 // Here we clone the File object. 31 32 RefPtr<File> file = File::Create(mFile->GetParentObject(), mFile->Impl()); 33 mCallback->Call(*file); 34 return NS_OK; 35 } 36 37 private: 38 const RefPtr<FileCallback> mCallback; 39 RefPtr<File> mFile; 40 }; 41 42 } // anonymous namespace 43 44 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemFileEntry, FileSystemEntry, mFile) 45 46 NS_IMPL_ADDREF_INHERITED(FileSystemFileEntry, FileSystemEntry) 47 NS_IMPL_RELEASE_INHERITED(FileSystemFileEntry, FileSystemEntry) 48 49 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemFileEntry) 50 NS_INTERFACE_MAP_END_INHERITING(FileSystemEntry) 51 52 FileSystemFileEntry::FileSystemFileEntry(nsIGlobalObject* aGlobal, File* aFile, 53 FileSystemDirectoryEntry* aParentEntry, 54 FileSystem* aFileSystem) 55 : FileSystemEntry(aGlobal, aParentEntry, aFileSystem), mFile(aFile) { 56 MOZ_ASSERT(aGlobal); 57 MOZ_ASSERT(mFile); 58 } 59 60 FileSystemFileEntry::~FileSystemFileEntry() = default; 61 62 JSObject* FileSystemFileEntry::WrapObject(JSContext* aCx, 63 JS::Handle<JSObject*> aGivenProto) { 64 return FileSystemFileEntry_Binding::Wrap(aCx, this, aGivenProto); 65 } 66 67 void FileSystemFileEntry::GetName(nsAString& aName, ErrorResult& aRv) const { 68 mFile->GetName(aName); 69 } 70 71 void FileSystemFileEntry::GetFullPath(nsAString& aPath, 72 ErrorResult& aRv) const { 73 mFile->Impl()->GetDOMPath(aPath); 74 if (aPath.IsEmpty()) { 75 // We're under the root directory. webkitRelativePath 76 // (implemented as GetPath) is for cases when file is selected because its 77 // ancestor directory is selected. But that is not the case here, so need to 78 // manually prepend '/'. 79 nsAutoString name; 80 mFile->GetName(name); 81 aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); 82 aPath.Append(name); 83 } 84 } 85 86 void FileSystemFileEntry::GetFile( 87 FileCallback& aSuccessCallback, 88 const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const { 89 RefPtr<FileCallbackRunnable> runnable = 90 new FileCallbackRunnable(&aSuccessCallback, mFile); 91 92 FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget()); 93 } 94 95 } // namespace mozilla::dom