From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- dom/filesystem/Directory.cpp | 257 ++++++++ dom/filesystem/Directory.h | 131 +++++ dom/filesystem/FileSystemBase.cpp | 169 ++++++ dom/filesystem/FileSystemBase.h | 118 ++++ dom/filesystem/FileSystemRequestParent.cpp | 194 +++++++ dom/filesystem/FileSystemRequestParent.h | 52 ++ dom/filesystem/FileSystemSecurity.cpp | 119 ++++ dom/filesystem/FileSystemSecurity.h | 48 ++ dom/filesystem/FileSystemTaskBase.cpp | 379 ++++++++++++ dom/filesystem/FileSystemTaskBase.h | 279 +++++++++ dom/filesystem/FileSystemUtils.cpp | 75 +++ dom/filesystem/FileSystemUtils.h | 44 ++ dom/filesystem/GetDirectoryListingTask.cpp | 390 +++++++++++++ dom/filesystem/GetDirectoryListingTask.h | 106 ++++ dom/filesystem/GetFileOrDirectoryTask.cpp | 280 +++++++++ dom/filesystem/GetFileOrDirectoryTask.h | 89 +++ dom/filesystem/GetFilesHelper.cpp | 643 +++++++++++++++++++++ dom/filesystem/GetFilesHelper.h | 205 +++++++ dom/filesystem/GetFilesTask.cpp | 263 +++++++++ dom/filesystem/GetFilesTask.h | 95 +++ dom/filesystem/OSFileSystem.cpp | 113 ++++ dom/filesystem/OSFileSystem.h | 132 +++++ dom/filesystem/PFileSystemParams.ipdlh | 50 ++ dom/filesystem/PFileSystemRequest.ipdl | 73 +++ dom/filesystem/compat/CallbackRunnables.cpp | 306 ++++++++++ dom/filesystem/compat/CallbackRunnables.h | 128 ++++ dom/filesystem/compat/FileSystem.cpp | 75 +++ dom/filesystem/compat/FileSystem.h | 73 +++ dom/filesystem/compat/FileSystemDirectoryEntry.cpp | 107 ++++ dom/filesystem/compat/FileSystemDirectoryEntry.h | 84 +++ .../compat/FileSystemDirectoryReader.cpp | 188 ++++++ dom/filesystem/compat/FileSystemDirectoryReader.h | 64 ++ dom/filesystem/compat/FileSystemEntry.cpp | 89 +++ dom/filesystem/compat/FileSystemEntry.h | 89 +++ dom/filesystem/compat/FileSystemFileEntry.cpp | 138 +++++ dom/filesystem/compat/FileSystemFileEntry.h | 57 ++ .../compat/FileSystemRootDirectoryEntry.cpp | 146 +++++ .../compat/FileSystemRootDirectoryEntry.h | 53 ++ .../compat/FileSystemRootDirectoryReader.cpp | 96 +++ .../compat/FileSystemRootDirectoryReader.h | 41 ++ dom/filesystem/compat/moz.build | 30 + dom/filesystem/compat/tests/mochitest.ini | 8 + dom/filesystem/compat/tests/moz.build | 7 + dom/filesystem/compat/tests/script_entries.js | 47 ++ dom/filesystem/compat/tests/test_basic.html | 494 ++++++++++++++++ .../compat/tests/test_formSubmission.html | 267 +++++++++ dom/filesystem/compat/tests/test_no_dnd.html | 85 +++ dom/filesystem/moz.build | 48 ++ dom/filesystem/tests/filesystem_commons.js | 103 ++++ dom/filesystem/tests/mochitest.ini | 10 + dom/filesystem/tests/moz.build | 7 + dom/filesystem/tests/script_fileList.js | 129 +++++ dom/filesystem/tests/test_basic.html | 167 ++++++ dom/filesystem/tests/test_bug1319088.html | 66 +++ dom/filesystem/tests/test_webkitdirectory.html | 191 ++++++ dom/filesystem/tests/test_worker_basic.html | 72 +++ dom/filesystem/tests/worker_basic.js | 41 ++ 57 files changed, 7810 insertions(+) create mode 100644 dom/filesystem/Directory.cpp create mode 100644 dom/filesystem/Directory.h create mode 100644 dom/filesystem/FileSystemBase.cpp create mode 100644 dom/filesystem/FileSystemBase.h create mode 100644 dom/filesystem/FileSystemRequestParent.cpp create mode 100644 dom/filesystem/FileSystemRequestParent.h create mode 100644 dom/filesystem/FileSystemSecurity.cpp create mode 100644 dom/filesystem/FileSystemSecurity.h create mode 100644 dom/filesystem/FileSystemTaskBase.cpp create mode 100644 dom/filesystem/FileSystemTaskBase.h create mode 100644 dom/filesystem/FileSystemUtils.cpp create mode 100644 dom/filesystem/FileSystemUtils.h create mode 100644 dom/filesystem/GetDirectoryListingTask.cpp create mode 100644 dom/filesystem/GetDirectoryListingTask.h create mode 100644 dom/filesystem/GetFileOrDirectoryTask.cpp create mode 100644 dom/filesystem/GetFileOrDirectoryTask.h create mode 100644 dom/filesystem/GetFilesHelper.cpp create mode 100644 dom/filesystem/GetFilesHelper.h create mode 100644 dom/filesystem/GetFilesTask.cpp create mode 100644 dom/filesystem/GetFilesTask.h create mode 100644 dom/filesystem/OSFileSystem.cpp create mode 100644 dom/filesystem/OSFileSystem.h create mode 100644 dom/filesystem/PFileSystemParams.ipdlh create mode 100644 dom/filesystem/PFileSystemRequest.ipdl create mode 100644 dom/filesystem/compat/CallbackRunnables.cpp create mode 100644 dom/filesystem/compat/CallbackRunnables.h create mode 100644 dom/filesystem/compat/FileSystem.cpp create mode 100644 dom/filesystem/compat/FileSystem.h create mode 100644 dom/filesystem/compat/FileSystemDirectoryEntry.cpp create mode 100644 dom/filesystem/compat/FileSystemDirectoryEntry.h create mode 100644 dom/filesystem/compat/FileSystemDirectoryReader.cpp create mode 100644 dom/filesystem/compat/FileSystemDirectoryReader.h create mode 100644 dom/filesystem/compat/FileSystemEntry.cpp create mode 100644 dom/filesystem/compat/FileSystemEntry.h create mode 100644 dom/filesystem/compat/FileSystemFileEntry.cpp create mode 100644 dom/filesystem/compat/FileSystemFileEntry.h create mode 100644 dom/filesystem/compat/FileSystemRootDirectoryEntry.cpp create mode 100644 dom/filesystem/compat/FileSystemRootDirectoryEntry.h create mode 100644 dom/filesystem/compat/FileSystemRootDirectoryReader.cpp create mode 100644 dom/filesystem/compat/FileSystemRootDirectoryReader.h create mode 100644 dom/filesystem/compat/moz.build create mode 100644 dom/filesystem/compat/tests/mochitest.ini create mode 100644 dom/filesystem/compat/tests/moz.build create mode 100644 dom/filesystem/compat/tests/script_entries.js create mode 100644 dom/filesystem/compat/tests/test_basic.html create mode 100644 dom/filesystem/compat/tests/test_formSubmission.html create mode 100644 dom/filesystem/compat/tests/test_no_dnd.html create mode 100644 dom/filesystem/moz.build create mode 100644 dom/filesystem/tests/filesystem_commons.js create mode 100644 dom/filesystem/tests/mochitest.ini create mode 100644 dom/filesystem/tests/moz.build create mode 100644 dom/filesystem/tests/script_fileList.js create mode 100644 dom/filesystem/tests/test_basic.html create mode 100644 dom/filesystem/tests/test_bug1319088.html create mode 100644 dom/filesystem/tests/test_webkitdirectory.html create mode 100644 dom/filesystem/tests/test_worker_basic.html create mode 100644 dom/filesystem/tests/worker_basic.js (limited to 'dom/filesystem') diff --git a/dom/filesystem/Directory.cpp b/dom/filesystem/Directory.cpp new file mode 100644 index 000000000..59c78fb2c --- /dev/null +++ b/dom/filesystem/Directory.cpp @@ -0,0 +1,257 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/Directory.h" + +#include "GetDirectoryListingTask.h" +#include "GetFilesTask.h" +#include "WorkerPrivate.h" + +#include "nsCharSeparatedTokenizer.h" +#include "nsString.h" +#include "mozilla/dom/DirectoryBinding.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/OSFileSystem.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_CLASS(Directory) + +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Directory) + if (tmp->mFileSystem) { + tmp->mFileSystem->Unlink(); + tmp->mFileSystem = nullptr; + } + NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent) + NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER +NS_IMPL_CYCLE_COLLECTION_UNLINK_END + +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Directory) + if (tmp->mFileSystem) { + tmp->mFileSystem->Traverse(cb); + } + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Directory) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory) +NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory) +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +/* static */ bool +Directory::WebkitBlinkDirectoryPickerEnabled(JSContext* aCx, JSObject* aObj) +{ + if (NS_IsMainThread()) { + return Preferences::GetBool("dom.webkitBlink.dirPicker.enabled", false); + } + + // aCx can be null when this function is called by something else than WebIDL + // binding code. + workers::WorkerPrivate* workerPrivate = + workers::GetCurrentThreadWorkerPrivate(); + if (!workerPrivate) { + return false; + } + + return workerPrivate->WebkitBlinkDirectoryPickerEnabled(); +} + +/* static */ already_AddRefed +Directory::Constructor(const GlobalObject& aGlobal, + const nsAString& aRealPath, + ErrorResult& aRv) +{ + nsCOMPtr path; + aRv = NS_NewLocalFile(aRealPath, true, getter_AddRefs(path)); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return Create(aGlobal.GetAsSupports(), path); +} + +/* static */ already_AddRefed +Directory::Create(nsISupports* aParent, nsIFile* aFile, + FileSystemBase* aFileSystem) +{ + MOZ_ASSERT(aParent); + MOZ_ASSERT(aFile); + +#ifdef DEBUG + bool isDir; + nsresult rv = aFile->IsDirectory(&isDir); + MOZ_ASSERT(NS_SUCCEEDED(rv) && isDir); +#endif + + RefPtr directory = new Directory(aParent, aFile, aFileSystem); + return directory.forget(); +} + +Directory::Directory(nsISupports* aParent, + nsIFile* aFile, + FileSystemBase* aFileSystem) + : mParent(aParent) + , mFile(aFile) +{ + MOZ_ASSERT(aFile); + + // aFileSystem can be null. In this case we create a OSFileSystem when needed. + if (aFileSystem) { + // More likely, this is a OSFileSystem. This object keeps a reference of + // mParent but it's not cycle collectable and to avoid manual + // addref/release, it's better to have 1 object per directory. For this + // reason we clone it here. + mFileSystem = aFileSystem->Clone(); + } +} + +Directory::~Directory() +{ +} + +nsISupports* +Directory::GetParentObject() const +{ + return mParent; +} + +JSObject* +Directory::WrapObject(JSContext* aCx, JS::Handle aGivenProto) +{ + return DirectoryBinding::Wrap(aCx, this, aGivenProto); +} + +void +Directory::GetName(nsAString& aRetval, ErrorResult& aRv) +{ + aRetval.Truncate(); + + RefPtr fs = GetFileSystem(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + fs->GetDirectoryName(mFile, aRetval, aRv); +} + +void +Directory::GetPath(nsAString& aRetval, ErrorResult& aRv) +{ + // This operation is expensive. Better to cache the result. + if (mPath.IsEmpty()) { + RefPtr fs = GetFileSystem(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + fs->GetDOMPath(mFile, mPath, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + } + + aRetval = mPath; +} + +nsresult +Directory::GetFullRealPath(nsAString& aPath) +{ + nsresult rv = mFile->GetPath(aPath); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + return NS_OK; +} + +already_AddRefed +Directory::GetFilesAndDirectories(ErrorResult& aRv) +{ + RefPtr fs = GetFileSystem(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr task = + GetDirectoryListingTaskChild::Create(fs, this, mFile, mFilters, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + task->Start(); + + return task->GetPromise(); +} + +already_AddRefed +Directory::GetFiles(bool aRecursiveFlag, ErrorResult& aRv) +{ + ErrorResult rv; + RefPtr fs = GetFileSystem(rv); + if (NS_WARN_IF(rv.Failed())) { + aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); + return nullptr; + } + + RefPtr task = + GetFilesTaskChild::Create(fs, this, mFile, aRecursiveFlag, rv); + if (NS_WARN_IF(rv.Failed())) { + aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); + return nullptr; + } + + task->Start(); + + return task->GetPromise(); +} + +void +Directory::SetContentFilters(const nsAString& aFilters) +{ + mFilters = aFilters; +} + +FileSystemBase* +Directory::GetFileSystem(ErrorResult& aRv) +{ + if (!mFileSystem) { + nsAutoString path; + aRv = mFile->GetPath(path); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr fs = new OSFileSystem(path); + fs->Init(mParent); + + mFileSystem = fs; + } + + return mFileSystem; +} + + +bool +Directory::ClonableToDifferentThreadOrProcess() const +{ + // If we don't have a fileSystem we are going to create a OSFileSystem that is + // clonable everywhere. + if (!mFileSystem) { + return true; + } + + return mFileSystem->ClonableToDifferentThreadOrProcess(); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/Directory.h b/dom/filesystem/Directory.h new file mode 100644 index 000000000..3a532e043 --- /dev/null +++ b/dom/filesystem/Directory.h @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_Directory_h +#define mozilla_dom_Directory_h + +#include "mozilla/Attributes.h" +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/File.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" + +namespace mozilla { +namespace dom { + +class FileSystemBase; +class Promise; +class StringOrFileOrDirectory; + +class Directory final + : public nsISupports + , public nsWrapperCache +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Directory) + + static bool + WebkitBlinkDirectoryPickerEnabled(JSContext* aCx, JSObject* aObj); + + static already_AddRefed + Constructor(const GlobalObject& aGlobal, + const nsAString& aRealPath, + ErrorResult& aRv); + + static already_AddRefed + Create(nsISupports* aParent, nsIFile* aDirectory, + FileSystemBase* aFileSystem = 0); + + // ========= Begin WebIDL bindings. =========== + + nsISupports* + GetParentObject() const; + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + void + GetName(nsAString& aRetval, ErrorResult& aRv); + + // From https://microsoftedge.github.io/directory-upload/proposal.html#directory-interface : + + void + GetPath(nsAString& aRetval, ErrorResult& aRv); + + nsresult + GetFullRealPath(nsAString& aPath); + + already_AddRefed + GetFilesAndDirectories(ErrorResult& aRv); + + already_AddRefed + GetFiles(bool aRecursiveFlag, ErrorResult& aRv); + + // =========== End WebIDL bindings.============ + + /** + * Sets a semi-colon separated list of filters to filter-in or filter-out + * certain types of files when the contents of this directory are requested + * via a GetFilesAndDirectories() call. + * + * Currently supported keywords: + * + * * filter-out-sensitive + * This keyword filters out files or directories that we don't wish to + * make available to Web content because we are concerned that there is + * a risk that users may unwittingly give Web content access to them + * and suffer undesirable consequences. The details of what is + * filtered out can be found in GetDirectoryListingTask::Work. + * + * In future, we will likely support filtering based on filename extensions + * (for example, aFilters could be "*.jpg; *.jpeg; *.gif"), but that isn't + * supported yet. Once supported, files that don't match a specified + * extension (if any are specified) would be filtered out. This + * functionality would allow us to apply the 'accept' attribute from + * to the results of a directory + * picker operation. + */ + void + SetContentFilters(const nsAString& aFilters); + + FileSystemBase* + GetFileSystem(ErrorResult& aRv); + + bool + ClonableToDifferentThreadOrProcess() const; + + nsIFile* + GetInternalNsIFile() const + { + return mFile; + } + +private: + Directory(nsISupports* aParent, + nsIFile* aFile, + FileSystemBase* aFileSystem = nullptr); + ~Directory(); + + /* + * Convert relative DOM path to the absolute real path. + */ + nsresult + DOMPathToRealPath(const nsAString& aPath, nsIFile** aFile) const; + + nsCOMPtr mParent; + RefPtr mFileSystem; + nsCOMPtr mFile; + + nsString mFilters; + nsString mPath; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_Directory_h diff --git a/dom/filesystem/FileSystemBase.cpp b/dom/filesystem/FileSystemBase.cpp new file mode 100644 index 000000000..f44f4fbf1 --- /dev/null +++ b/dom/filesystem/FileSystemBase.cpp @@ -0,0 +1,169 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/FileSystemBase.h" + +#include "nsCharSeparatedTokenizer.h" +#include "OSFileSystem.h" + +namespace mozilla { +namespace dom { + +FileSystemBase::FileSystemBase() + : mShutdown(false) +#ifdef DEBUG + , mOwningThread(PR_GetCurrentThread()) +#endif +{ +} + +FileSystemBase::~FileSystemBase() +{ + AssertIsOnOwningThread(); +} + +void +FileSystemBase::Shutdown() +{ + AssertIsOnOwningThread(); + mShutdown = true; +} + +nsISupports* +FileSystemBase::GetParentObject() const +{ + AssertIsOnOwningThread(); + return nullptr; +} + +bool +FileSystemBase::GetRealPath(BlobImpl* aFile, nsIFile** aPath) const +{ + AssertIsOnOwningThread(); + MOZ_ASSERT(aFile, "aFile Should not be null."); + MOZ_ASSERT(aPath); + + nsAutoString filePath; + ErrorResult rv; + aFile->GetMozFullPathInternal(filePath, rv); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + return false; + } + + rv = NS_NewLocalFile(filePath, true, aPath); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + return false; + } + + return true; +} + +bool +FileSystemBase::IsSafeFile(nsIFile* aFile) const +{ + AssertIsOnOwningThread(); + return false; +} + +bool +FileSystemBase::IsSafeDirectory(Directory* aDir) const +{ + AssertIsOnOwningThread(); + return false; +} + +void +FileSystemBase::GetDirectoryName(nsIFile* aFile, nsAString& aRetval, + ErrorResult& aRv) const +{ + AssertIsOnOwningThread(); + MOZ_ASSERT(aFile); + + aRv = aFile->GetLeafName(aRetval); + NS_WARNING_ASSERTION(!aRv.Failed(), "GetLeafName failed"); +} + +void +FileSystemBase::GetDOMPath(nsIFile* aFile, + nsAString& aRetval, + ErrorResult& aRv) const +{ + AssertIsOnOwningThread(); + MOZ_ASSERT(aFile); + + aRetval.Truncate(); + + nsCOMPtr fileSystemPath; + aRv = NS_NewLocalFile(LocalRootPath(), true, getter_AddRefs(fileSystemPath)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + nsCOMPtr path; + aRv = aFile->Clone(getter_AddRefs(path)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + nsTArray parts; + + while (true) { + nsAutoString leafName; + aRv = path->GetLeafName(leafName); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + if (!leafName.IsEmpty()) { + parts.AppendElement(leafName); + } + + bool equal = false; + aRv = fileSystemPath->Equals(path, &equal); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + if (equal) { + break; + } + + nsCOMPtr parentPath; + aRv = path->GetParent(getter_AddRefs(parentPath)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(parentPath); + + aRv = parentPath->Clone(getter_AddRefs(path)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + } + + if (parts.IsEmpty()) { + aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + return; + } + + for (int32_t i = parts.Length() - 1; i >= 0; --i) { + aRetval.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + aRetval.Append(parts[i]); + } +} + +void +FileSystemBase::AssertIsOnOwningThread() const +{ + MOZ_ASSERT(mOwningThread); + MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/FileSystemBase.h b/dom/filesystem/FileSystemBase.h new file mode 100644 index 000000000..0b875eb14 --- /dev/null +++ b/dom/filesystem/FileSystemBase.h @@ -0,0 +1,118 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemBase_h +#define mozilla_dom_FileSystemBase_h + +#include "nsString.h" +#include "Directory.h" + +namespace mozilla { +namespace dom { + +class BlobImpl; + +class FileSystemBase +{ +public: + NS_INLINE_DECL_REFCOUNTING(FileSystemBase) + + FileSystemBase(); + + virtual void + Shutdown(); + + // SerializeDOMPath the FileSystem to string. + virtual void + SerializeDOMPath(nsAString& aOutput) const = 0; + + virtual already_AddRefed + Clone() = 0; + + virtual bool + ShouldCreateDirectory() = 0; + + virtual nsISupports* + GetParentObject() const; + + virtual void + GetDirectoryName(nsIFile* aFile, nsAString& aRetval, + ErrorResult& aRv) const; + + void + GetDOMPath(nsIFile* aFile, nsAString& aRetval, ErrorResult& aRv) const; + + /* + * Return the local root path of the FileSystem implementation. + * For OSFileSystem, this is equal to the path of the root Directory; + * For DeviceStorageFileSystem, this is the path of the SDCard, parent + * directory of the exposed root Directory (per type). + */ + const nsAString& + LocalRootPath() const + { + return mLocalRootPath; + } + + bool + IsShutdown() const + { + return mShutdown; + } + + virtual bool + IsSafeFile(nsIFile* aFile) const; + + virtual bool + IsSafeDirectory(Directory* aDir) const; + + bool + GetRealPath(BlobImpl* aFile, nsIFile** aPath) const; + + // IPC initialization + // See how these 2 methods are used in FileSystemTaskChildBase. + + virtual bool + NeedToGoToMainThread() const { return false; } + + virtual nsresult + MainThreadWork() { return NS_ERROR_FAILURE; } + + virtual bool + ClonableToDifferentThreadOrProcess() const { return false; } + + // CC methods + virtual void Unlink() {} + virtual void Traverse(nsCycleCollectionTraversalCallback &cb) {} + + void + AssertIsOnOwningThread() const; + +protected: + virtual ~FileSystemBase(); + + // The local path of the root (i.e. the OS path, with OS path separators, of + // the OS directory that acts as the root of this OSFileSystem). + // This path must be set by the FileSystem implementation immediately + // because it will be used for the validation of any FileSystemTaskChildBase. + // The concept of this path is that, any task will never go out of it and this + // must be considered the OS 'root' of the current FileSystem. Different + // Directory object can have different OS 'root' path. + // To be more clear, any path managed by this FileSystem implementation must + // be discendant of this local root path. + nsString mLocalRootPath; + + bool mShutdown; + +#ifdef DEBUG + PRThread* mOwningThread; +#endif +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemBase_h diff --git a/dom/filesystem/FileSystemRequestParent.cpp b/dom/filesystem/FileSystemRequestParent.cpp new file mode 100644 index 000000000..0b9d6c18a --- /dev/null +++ b/dom/filesystem/FileSystemRequestParent.cpp @@ -0,0 +1,194 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/FileSystemRequestParent.h" +#include "mozilla/dom/PFileSystemParams.h" + +#include "GetDirectoryListingTask.h" +#include "GetFileOrDirectoryTask.h" + +#include "mozilla/AppProcessChecker.h" +#include "mozilla/dom/ContentParent.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemSecurity.h" +#include "mozilla/ipc/BackgroundParent.h" +#include "mozilla/Unused.h" +#include "nsProxyRelease.h" + +using namespace mozilla::ipc; + +namespace mozilla { +namespace dom { + +FileSystemRequestParent::FileSystemRequestParent() + : mDestroyed(false) +{ + AssertIsOnBackgroundThread(); +} + +FileSystemRequestParent::~FileSystemRequestParent() +{ + AssertIsOnBackgroundThread(); +} + +#define FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(name) \ + case FileSystemParams::TFileSystem##name##Params: { \ + const FileSystem##name##Params& p = aParams; \ + mFileSystem = new OSFileSystemParent(p.filesystem()); \ + MOZ_ASSERT(mFileSystem); \ + mTask = name##TaskParent::Create(mFileSystem, p, this, rv); \ + if (NS_WARN_IF(rv.Failed())) { \ + rv.SuppressException(); \ + return false; \ + } \ + break; \ + } + +bool +FileSystemRequestParent::Initialize(const FileSystemParams& aParams) +{ + AssertIsOnBackgroundThread(); + + ErrorResult rv; + + switch (aParams.type()) { + + FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetDirectoryListing) + FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFileOrDirectory) + FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFiles) + + default: { + NS_RUNTIMEABORT("not reached"); + break; + } + } + + if (NS_WARN_IF(!mTask || !mFileSystem)) { + // Should never reach here. + return false; + } + + return true; +} + +namespace { + +class CheckPermissionRunnable final : public Runnable +{ +public: + CheckPermissionRunnable(already_AddRefed aParent, + FileSystemRequestParent* aActor, + FileSystemTaskParentBase* aTask, + const nsAString& aPath) + : mContentParent(aParent) + , mActor(aActor) + , mTask(aTask) + , mPath(aPath) + , mBackgroundEventTarget(NS_GetCurrentThread()) + { + AssertIsInMainProcess(); + AssertIsOnBackgroundThread(); + + MOZ_ASSERT(mContentParent); + MOZ_ASSERT(mActor); + MOZ_ASSERT(mTask); + MOZ_ASSERT(mBackgroundEventTarget); + } + + NS_IMETHOD + Run() override + { + if (NS_IsMainThread()) { + auto raii = mozilla::MakeScopeExit([&] { mContentParent = nullptr; }); + + + if (!mozilla::Preferences::GetBool("dom.filesystem.pathcheck.disabled", false)) { + RefPtr fss = FileSystemSecurity::Get(); + if (NS_WARN_IF(!fss || + !fss->ContentProcessHasAccessTo(mContentParent->ChildID(), + mPath))) { + mContentParent->KillHard("This path is not allowed."); + return NS_OK; + } + } + + return mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL); + } + + AssertIsOnBackgroundThread(); + + // It can happen that this actor has been destroyed in the meantime we were + // on the main-thread. + if (!mActor->Destroyed()) { + mTask->Start(); + } + + return NS_OK; + } + +private: + ~CheckPermissionRunnable() + { + NS_ProxyRelease(mBackgroundEventTarget, mActor.forget()); + } + + RefPtr mContentParent; + RefPtr mActor; + RefPtr mTask; + const nsString mPath; + + nsCOMPtr mBackgroundEventTarget; +}; + +} // anonymous + +void +FileSystemRequestParent::Start() +{ + AssertIsInMainProcess(); + AssertIsOnBackgroundThread(); + + MOZ_ASSERT(!mDestroyed); + MOZ_ASSERT(mFileSystem); + MOZ_ASSERT(mTask); + + nsAutoString path; + if (NS_WARN_IF(NS_FAILED(mTask->GetTargetPath(path)))) { + Unused << Send__delete__(this, FileSystemErrorResponse(NS_ERROR_DOM_SECURITY_ERR)); + return; + } + + RefPtr parent = BackgroundParent::GetContentParent(Manager()); + + // If the ContentParent is null we are dealing with a same-process actor. + if (!parent) { + mTask->Start(); + return; + } + + RefPtr runnable = + new CheckPermissionRunnable(parent.forget(), this, mTask, path); + NS_DispatchToMainThread(runnable); +} + +void +FileSystemRequestParent::ActorDestroy(ActorDestroyReason aWhy) +{ + AssertIsOnBackgroundThread(); + MOZ_ASSERT(!mDestroyed); + + if (!mFileSystem) { + return; + } + + mFileSystem->Shutdown(); + mFileSystem = nullptr; + mTask = nullptr; + mDestroyed = true; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/FileSystemRequestParent.h b/dom/filesystem/FileSystemRequestParent.h new file mode 100644 index 000000000..a5f4cd6cc --- /dev/null +++ b/dom/filesystem/FileSystemRequestParent.h @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemRequestParent_h +#define mozilla_dom_FileSystemRequestParent_h + +#include "mozilla/dom/PFileSystemRequestParent.h" +#include "mozilla/dom/FileSystemBase.h" + +namespace mozilla { +namespace dom { + +class FileSystemParams; +class FileSystemTaskParentBase; + +class FileSystemRequestParent final : public PFileSystemRequestParent +{ + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FileSystemRequestParent) + +public: + FileSystemRequestParent(); + + bool + Initialize(const FileSystemParams& aParams); + + void + Start(); + + bool Destroyed() const + { + return mDestroyed; + } + + virtual void + ActorDestroy(ActorDestroyReason why) override; + +private: + ~FileSystemRequestParent(); + + RefPtr mFileSystem; + RefPtr mTask; + + bool mDestroyed; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemRequestParent_h diff --git a/dom/filesystem/FileSystemSecurity.cpp b/dom/filesystem/FileSystemSecurity.cpp new file mode 100644 index 000000000..b3d425817 --- /dev/null +++ b/dom/filesystem/FileSystemSecurity.cpp @@ -0,0 +1,119 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemSecurity.h" +#include "FileSystemUtils.h" +#include "mozilla/ClearOnShutdown.h" +#include "mozilla/StaticPtr.h" + +namespace mozilla { +namespace dom { + +namespace { + +StaticRefPtr gFileSystemSecurity; + +} // anonymous + +/* static */ already_AddRefed +FileSystemSecurity::Get() +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); + + RefPtr service = gFileSystemSecurity.get(); + return service.forget(); +} + +/* static */ already_AddRefed +FileSystemSecurity::GetOrCreate() +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); + + if (!gFileSystemSecurity) { + gFileSystemSecurity = new FileSystemSecurity(); + ClearOnShutdown(&gFileSystemSecurity); + } + + RefPtr service = gFileSystemSecurity.get(); + return service.forget(); +} + +FileSystemSecurity::FileSystemSecurity() +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); +} + +FileSystemSecurity::~FileSystemSecurity() +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); +} + +void +FileSystemSecurity::GrantAccessToContentProcess(ContentParentId aId, + const nsAString& aDirectoryPath) +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); + + nsTArray* paths; + if (!mPaths.Get(aId, &paths)) { + paths = new nsTArray(); + mPaths.Put(aId, paths); + } else if (paths->Contains(aDirectoryPath)) { + return; + } + + paths->AppendElement(aDirectoryPath); +} + +void +FileSystemSecurity::Forget(ContentParentId aId) +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); + + mPaths.Remove(aId); +} + +bool +FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId, + const nsAString& aPath) +{ + MOZ_ASSERT(NS_IsMainThread()); + AssertIsInMainProcess(); + +#if defined(XP_WIN) + if (StringBeginsWith(aPath, NS_LITERAL_STRING("..\\")) || + FindInReadable(NS_LITERAL_STRING("\\..\\"), aPath)) { + return false; + } +#elif defined(XP_UNIX) + if (StringBeginsWith(aPath, NS_LITERAL_STRING("../")) || + FindInReadable(NS_LITERAL_STRING("/../"), aPath)) { + return false; + } +#endif + + nsTArray* paths; + if (!mPaths.Get(aId, &paths)) { + return false; + } + + for (uint32_t i = 0, len = paths->Length(); i < len; ++i) { + if (FileSystemUtils::IsDescendantPath(paths->ElementAt(i), aPath)) { + return true; + } + } + + return false; +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/FileSystemSecurity.h b/dom/filesystem/FileSystemSecurity.h new file mode 100644 index 000000000..fe61dc7c6 --- /dev/null +++ b/dom/filesystem/FileSystemSecurity.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemSecurity_h +#define mozilla_dom_FileSystemSecurity_h + +#include "mozilla/dom/ipc/IdType.h" +#include "nsClassHashtable.h" +#include "nsISupportsImpl.h" + +namespace mozilla { +namespace dom { + +class FileSystemSecurity final +{ +public: + NS_INLINE_DECL_REFCOUNTING(FileSystemSecurity) + + static already_AddRefed + Get(); + + static already_AddRefed + GetOrCreate(); + + void + GrantAccessToContentProcess(ContentParentId aId, + const nsAString& aDirectoryPath); + + void + Forget(ContentParentId aId); + + bool + ContentProcessHasAccessTo(ContentParentId aId, const nsAString& aPath); + +private: + FileSystemSecurity(); + ~FileSystemSecurity(); + + nsClassHashtable> mPaths; +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_FileSystemSecurity_h diff --git a/dom/filesystem/FileSystemTaskBase.cpp b/dom/filesystem/FileSystemTaskBase.cpp new file mode 100644 index 000000000..481002b7d --- /dev/null +++ b/dom/filesystem/FileSystemTaskBase.cpp @@ -0,0 +1,379 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/FileSystemTaskBase.h" + +#include "nsNetCID.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemRequestParent.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/ipc/BlobParent.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/BackgroundParent.h" +#include "mozilla/ipc/PBackgroundChild.h" +#include "mozilla/Unused.h" +#include "nsProxyRelease.h" + +namespace mozilla { +namespace dom { + +namespace { + +nsresult +FileSystemErrorFromNsError(const nsresult& aErrorValue) +{ + uint16_t module = NS_ERROR_GET_MODULE(aErrorValue); + if (module == NS_ERROR_MODULE_DOM_FILESYSTEM || + module == NS_ERROR_MODULE_DOM_FILE || + module == NS_ERROR_MODULE_DOM) { + return aErrorValue; + } + + switch (aErrorValue) { + case NS_OK: + return NS_OK; + + case NS_ERROR_FILE_INVALID_PATH: + case NS_ERROR_FILE_UNRECOGNIZED_PATH: + return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; + + case NS_ERROR_FILE_DESTINATION_NOT_DIR: + return NS_ERROR_DOM_FILESYSTEM_INVALID_MODIFICATION_ERR; + + case NS_ERROR_FILE_ACCESS_DENIED: + case NS_ERROR_FILE_DIR_NOT_EMPTY: + return NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR; + + case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST: + case NS_ERROR_NOT_AVAILABLE: + return NS_ERROR_DOM_FILE_NOT_FOUND_ERR; + + case NS_ERROR_FILE_ALREADY_EXISTS: + return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR; + + case NS_ERROR_FILE_NOT_DIRECTORY: + return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; + + case NS_ERROR_UNEXPECTED: + default: + return NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR; + } +} + +nsresult +DispatchToIOThread(nsIRunnable* aRunnable) +{ + MOZ_ASSERT(aRunnable); + + nsCOMPtr target + = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); + MOZ_ASSERT(target); + + return target->Dispatch(aRunnable, NS_DISPATCH_NORMAL); +} + +// This runnable is used when an error value is set before doing any real +// operation on the I/O thread. In this case we skip all and we directly +// communicate the error. +class ErrorRunnable final : public CancelableRunnable +{ +public: + explicit ErrorRunnable(FileSystemTaskChildBase* aTask) + : mTask(aTask) + { + MOZ_ASSERT(aTask); + } + + NS_IMETHOD + Run() override + { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mTask->HasError()); + + mTask->HandlerCallback(); + return NS_OK; + } + +private: + RefPtr mTask; +}; + +} // anonymous namespace + +NS_IMPL_ISUPPORTS(FileSystemTaskChildBase, nsIIPCBackgroundChildCreateCallback) + +/** + * FileSystemTaskBase class + */ + +FileSystemTaskChildBase::FileSystemTaskChildBase(FileSystemBase* aFileSystem) + : mErrorValue(NS_OK) + , mFileSystem(aFileSystem) +{ + MOZ_ASSERT(aFileSystem, "aFileSystem should not be null."); + aFileSystem->AssertIsOnOwningThread(); +} + +FileSystemTaskChildBase::~FileSystemTaskChildBase() +{ + mFileSystem->AssertIsOnOwningThread(); +} + +FileSystemBase* +FileSystemTaskChildBase::GetFileSystem() const +{ + mFileSystem->AssertIsOnOwningThread(); + return mFileSystem.get(); +} + +void +FileSystemTaskChildBase::Start() +{ + mFileSystem->AssertIsOnOwningThread(); + + mozilla::ipc::PBackgroundChild* actor = + mozilla::ipc::BackgroundChild::GetForCurrentThread(); + if (actor) { + ActorCreated(actor); + } else { + if (NS_WARN_IF( + !mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(this))) { + MOZ_CRASH(); + } + } +} + +void +FileSystemTaskChildBase::ActorFailed() +{ + MOZ_CRASH("Failed to create a PBackgroundChild actor!"); +} + +void +FileSystemTaskChildBase::ActorCreated(mozilla::ipc::PBackgroundChild* aActor) +{ + if (HasError()) { + // In this case we don't want to use IPC at all. + RefPtr runnable = new ErrorRunnable(this); + DebugOnly rv = NS_DispatchToCurrentThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToCurrentThread failed"); + return; + } + + if (mFileSystem->IsShutdown()) { + return; + } + + nsAutoString serialization; + mFileSystem->SerializeDOMPath(serialization); + + ErrorResult rv; + FileSystemParams params = GetRequestParams(serialization, rv); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + return; + } + + // Retain a reference so the task object isn't deleted without IPDL's + // knowledge. The reference will be released by + // mozilla::ipc::BackgroundChildImpl::DeallocPFileSystemRequestChild. + NS_ADDREF_THIS(); + + mozilla::ipc::PBackgroundChild* actor = + mozilla::ipc::BackgroundChild::GetForCurrentThread(); + MOZ_ASSERT(actor); + + actor->SendPFileSystemRequestConstructor(this, params); +} + +void +FileSystemTaskChildBase::SetRequestResult(const FileSystemResponseValue& aValue) +{ + mFileSystem->AssertIsOnOwningThread(); + + if (aValue.type() == FileSystemResponseValue::TFileSystemErrorResponse) { + FileSystemErrorResponse r = aValue; + mErrorValue = r.error(); + } else { + ErrorResult rv; + SetSuccessRequestResult(aValue, rv); + mErrorValue = rv.StealNSResult(); + } +} + +bool +FileSystemTaskChildBase::Recv__delete__(const FileSystemResponseValue& aValue) +{ + mFileSystem->AssertIsOnOwningThread(); + + SetRequestResult(aValue); + HandlerCallback(); + return true; +} + +void +FileSystemTaskChildBase::SetError(const nsresult& aErrorValue) +{ + mErrorValue = FileSystemErrorFromNsError(aErrorValue); +} + +/** + * FileSystemTaskParentBase class + */ + +FileSystemTaskParentBase::FileSystemTaskParentBase(FileSystemBase* aFileSystem, + const FileSystemParams& aParam, + FileSystemRequestParent* aParent) + : mErrorValue(NS_OK) + , mFileSystem(aFileSystem) + , mRequestParent(aParent) + , mBackgroundEventTarget(NS_GetCurrentThread()) +{ + MOZ_ASSERT(XRE_IsParentProcess(), + "Only call from parent process!"); + MOZ_ASSERT(aFileSystem, "aFileSystem should not be null."); + MOZ_ASSERT(aParent); + MOZ_ASSERT(mBackgroundEventTarget); + AssertIsOnBackgroundThread(); +} + +FileSystemTaskParentBase::~FileSystemTaskParentBase() +{ + // This task can be released on different threads because we dispatch it (as + // runnable) to main-thread, I/O and then back to the PBackground thread. + NS_ProxyRelease(mBackgroundEventTarget, mFileSystem.forget()); + NS_ProxyRelease(mBackgroundEventTarget, mRequestParent.forget()); +} + +void +FileSystemTaskParentBase::Start() +{ + AssertIsOnBackgroundThread(); + mFileSystem->AssertIsOnOwningThread(); + + if (NeedToGoToMainThread()) { + DebugOnly rv = NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToCurrentThread failed"); + return; + } + + DebugOnly rv = DispatchToIOThread(this); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "DispatchToIOThread failed"); +} + +void +FileSystemTaskParentBase::HandleResult() +{ + AssertIsOnBackgroundThread(); + mFileSystem->AssertIsOnOwningThread(); + + if (mFileSystem->IsShutdown()) { + return; + } + + MOZ_ASSERT(mRequestParent); + Unused << mRequestParent->Send__delete__(mRequestParent, GetRequestResult()); +} + +FileSystemResponseValue +FileSystemTaskParentBase::GetRequestResult() const +{ + AssertIsOnBackgroundThread(); + mFileSystem->AssertIsOnOwningThread(); + + if (HasError()) { + return FileSystemErrorResponse(mErrorValue); + } + + ErrorResult rv; + FileSystemResponseValue value = GetSuccessRequestResult(rv); + if (NS_WARN_IF(rv.Failed())) { + return FileSystemErrorResponse(rv.StealNSResult()); + } + + return value; +} + +void +FileSystemTaskParentBase::SetError(const nsresult& aErrorValue) +{ + mErrorValue = FileSystemErrorFromNsError(aErrorValue); +} + +bool +FileSystemTaskParentBase::NeedToGoToMainThread() const +{ + return mFileSystem->NeedToGoToMainThread(); +} + +nsresult +FileSystemTaskParentBase::MainThreadWork() +{ + MOZ_ASSERT(NS_IsMainThread()); + return mFileSystem->MainThreadWork(); +} + +NS_IMETHODIMP +FileSystemTaskParentBase::Run() +{ + // This method can run in 3 different threads. Here why: + // 1. if we are on the main-thread it's because the task must do something + // here. If no errors are returned we go the step 2. + // 2. We can be here directly if the task doesn't have nothing to do on the + // main-thread. We are are on the I/O thread and we call IOWork(). + // 3. Both step 1 (in case of error) and step 2 end up here where return the + // value back to the PBackground thread. + if (NS_IsMainThread()) { + MOZ_ASSERT(NeedToGoToMainThread()); + + nsresult rv = MainThreadWork(); + if (NS_WARN_IF(NS_FAILED(rv))) { + SetError(rv); + + // Something when wrong. Let's go to the Background thread directly + // skipping the I/O thread step. + rv = mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + // Next step must happen on the I/O thread. + rv = DispatchToIOThread(this); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + return NS_OK; + } + + // Run I/O thread tasks + if (!IsOnBackgroundThread()) { + nsresult rv = IOWork(); + if (NS_WARN_IF(NS_FAILED(rv))) { + SetError(rv); + } + + // Let's go back to PBackground thread to finish the work. + rv = mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + return NS_OK; + } + + // If we are here, it's because the I/O work has been done and we have to + // handle the result back via IPC. + AssertIsOnBackgroundThread(); + HandleResult(); + return NS_OK; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/FileSystemTaskBase.h b/dom/filesystem/FileSystemTaskBase.h new file mode 100644 index 000000000..9910f4c1f --- /dev/null +++ b/dom/filesystem/FileSystemTaskBase.h @@ -0,0 +1,279 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemTaskBase_h +#define mozilla_dom_FileSystemTaskBase_h + +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/FileSystemRequestParent.h" +#include "mozilla/dom/PFileSystemRequestChild.h" +#include "nsIIPCBackgroundChildCreateCallback.h" +#include "nsThreadUtils.h" + +namespace mozilla { +namespace dom { + +class BlobImpl; +class FileSystemBase; +class FileSystemParams; +class PBlobParent; + +/* + * The base class to implement a Task class. + * The file system operations can only be performed in the parent process. In + * order to avoid duplicated code, we used PBackground for child-parent and + * parent-parent communications. + * + * The following diagram illustrates the how a API call from the content page + * starts a task and gets call back results. + * + * The left block is the call sequence inside any process loading content, while + * the right block is the call sequence only inside the parent process. + * + * Page + * | + * | (1) + * ______|_________________________ | _________________________________ + * | | | | | | + * | | | | | | + * | V | IPC | PBackground thread on | + * | [new FileSystemTaskChildBase()] | | | the parent process | + * | | | | | | + * | | (2) | | | + * | V | (3) | | + * | [GetRequestParams]------------------->[new FileSystemTaskParentBase()] | + * | | | | | + * | | | | | (4) _____________ | + * | | | | | | | | + * | | | | | | I/O Thread | | + * | | | | | | | | + * | | | | ---------> [IOWork] | | + * | | IPC | | | | | + * | | | | | | (5) | | + * | | | | -------------- | | + * | | | | | |_____________| | + * | | | | | | + * | | | | V | + * | | | | [HandleResult] | + * | | | | | | + * | | | | (6) | + * | | (7) | V | + * | [SetRequestResult]<---------------------[GetRequestResult] | + * | | | | | + * | | (8) | | | | + * | V | | | | + * |[HandlerCallback] | IPC | | + * |_______|_________________________| | |_________________________________| + * | | + * V + * Page + * + * 1. From the process that is handling the request + * Child/Parent (it can be in any process): + * (1) Call FileSystem API from content page with JS. Create a task and run. + * The base constructor [FileSystemTaskChildBase()] of the task should be + * called. + * (2) Forward the task to the parent process through the IPC and call + * [GetRequestParams] to prepare the parameters of the IPC. + * Parent: + * (3) The parent process receives IPC and handle it in + * FileystemRequestParent. Get the IPC parameters and create a task to run the + * IPC task. + * (4) The task operation will be performed in the member function of [IOWork]. + * A I/O thread will be created to run that function. If error occurs + * during the operation, call [SetError] to record the error and then abort. + * (5) After finishing the task operation, call [HandleResult] to send the + * result back to the child process though the IPC. + * (6) Call [GetRequestResult] request result to prepare the parameters of the + * IPC. Because the formats of the error result for different task are the + * same, FileSystemTaskChildBase can handle the error message without + * interfering. + * Each task only needs to implement its specific success result preparation + * function -[GetSuccessRequestResult]. + * Child/Parent: + * (7) The process receives IPC and calls [SetRequestResult] to get the + * task result. Each task needs to implement its specific success result + * parsing function [SetSuccessRequestResult] to get the success result. + * (8) Call [HandlerCallback] to send the task result to the content page. + */ +class FileSystemTaskChildBase : public PFileSystemRequestChild + , public nsIIPCBackgroundChildCreateCallback +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK + + /* + * Start the task. It will dispatch all the information to the parent process, + * PBackground thread. This method must be called from the owning thread. + */ + void + Start(); + + /* + * The error codes are defined in xpcom/base/ErrorList.h and their + * corresponding error name and message are defined in dom/base/domerr.msg. + */ + void + SetError(const nsresult& aErrorCode); + + FileSystemBase* + GetFileSystem() const; + + /* + * After the task is completed, this function will be called to pass the task + * result to the content page. This method is called in the owning thread. + * Override this function to handle the call back to the content page. + */ + virtual void + HandlerCallback() = 0; + + bool + HasError() const { return NS_FAILED(mErrorValue); } + +protected: + /* + * To create a task to handle the page content request. + */ + explicit FileSystemTaskChildBase(FileSystemBase* aFileSystem); + + virtual + ~FileSystemTaskChildBase(); + + /* + * Wrap the task parameter to FileSystemParams for sending it through IPC. + * It will be called when we need to forward a task from the child process to + * the parent process. This method runs in the owning thread. + * @param filesystem The string representation of the file system. + */ + virtual FileSystemParams + GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const = 0; + + /* + * Unwrap the IPC message to get the task success result. + * It will be called when the task is completed successfully and an IPC + * message is received in the child process and we want to get the task + * success result. This method runs in the owning thread. + */ + virtual void + SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) = 0; + + // Overrides PFileSystemRequestChild + virtual bool + Recv__delete__(const FileSystemResponseValue& value) override; + + nsresult mErrorValue; + RefPtr mFileSystem; + +private: + + /* + * Unwrap the IPC message to get the task result. + * It will be called when the task is completed and an IPC message is received + * in the content process and we want to get the task result. This runs on the + * owning thread. + */ + void + SetRequestResult(const FileSystemResponseValue& aValue); +}; + +// This class is the 'alter ego' of FileSystemTaskChildBase in the PBackground +// world. +class FileSystemTaskParentBase : public Runnable +{ +public: + /* + * Start the task. This must be called from the PBackground thread only. + */ + void + Start(); + + /* + * The error codes are defined in xpcom/base/ErrorList.h and their + * corresponding error name and message are defined in dom/base/domerr.msg. + */ + void + SetError(const nsresult& aErrorCode); + + /* + * The function to perform task operation. It will be run on the I/O + * thread of the parent process. + * Overrides this function to define the task operation for individual task. + */ + virtual nsresult + IOWork() = 0; + + /* + * Wrap the task success result to FileSystemResponseValue for sending it + * through IPC. This method runs in the PBackground thread. + * It will be called when the task is completed successfully and we need to + * send the task success result back to the child process. + */ + virtual FileSystemResponseValue + GetSuccessRequestResult(ErrorResult& aRv) const = 0; + + /* + * After finishing the task operation, handle the task result. + * If it is an IPC task, send back the IPC result. It runs on the PBackground + * thread. + */ + void + HandleResult(); + + // If this task must do something on the main-thread before IOWork(), it must + // overwrite this method. Otherwise it returns true if the FileSystem must be + // initialized on the main-thread. It's called from the Background thread. + virtual bool + NeedToGoToMainThread() const; + + // This method is called only if NeedToGoToMainThread() returns true. + // Of course, it runs on the main-thread. + virtual nsresult + MainThreadWork(); + + bool + HasError() const { return NS_FAILED(mErrorValue); } + + NS_IMETHOD + Run() override; + + virtual nsresult + GetTargetPath(nsAString& aPath) const = 0; + +private: + /* + * Wrap the task result to FileSystemResponseValue for sending it through IPC. + * It will be called when the task is completed and we need to + * send the task result back to the content. This runs on the PBackground + * thread. + */ + FileSystemResponseValue + GetRequestResult() const; + +protected: + /* + * To create a parent process task delivered from the child process through + * IPC. + */ + FileSystemTaskParentBase(FileSystemBase* aFileSystem, + const FileSystemParams& aParam, + FileSystemRequestParent* aParent); + + virtual + ~FileSystemTaskParentBase(); + + nsresult mErrorValue; + RefPtr mFileSystem; + RefPtr mRequestParent; + nsCOMPtr mBackgroundEventTarget; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemTaskBase_h diff --git a/dom/filesystem/FileSystemUtils.cpp b/dom/filesystem/FileSystemUtils.cpp new file mode 100644 index 000000000..872e049ee --- /dev/null +++ b/dom/filesystem/FileSystemUtils.cpp @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/FileSystemUtils.h" + +namespace mozilla { +namespace dom { + +namespace { + +bool +TokenizerIgnoreNothing(char16_t /* aChar */) +{ + return false; +} + +} // anonymous namespace + +/* static */ bool +FileSystemUtils::IsDescendantPath(const nsAString& aPath, + const nsAString& aDescendantPath) +{ + // Check the sub-directory path to see if it has the parent path as prefix. + if (!aDescendantPath.Equals(aPath) && + !StringBeginsWith(aDescendantPath, aPath)) { + return false; + } + + return true; +} + +/* static */ bool +FileSystemUtils::IsValidRelativeDOMPath(const nsAString& aPath, + nsTArray& aParts) +{ + // We don't allow empty relative path to access the root. + if (aPath.IsEmpty()) { + return false; + } + + // Leading and trailing "/" are not allowed. + if (aPath.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR || + aPath.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR) { + return false; + } + + NS_NAMED_LITERAL_STRING(kCurrentDir, "."); + NS_NAMED_LITERAL_STRING(kParentDir, ".."); + + // Split path and check each path component. + nsCharSeparatedTokenizerTemplate + tokenizer(aPath, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR); + + while (tokenizer.hasMoreTokens()) { + nsDependentSubstring pathComponent = tokenizer.nextToken(); + // The path containing empty components, such as "foo//bar", is invalid. + // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar", + // to walk up the directory. + if (pathComponent.IsEmpty() || + pathComponent.Equals(kCurrentDir) || + pathComponent.Equals(kParentDir)) { + return false; + } + + aParts.AppendElement(pathComponent); + } + + return true; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/FileSystemUtils.h b/dom/filesystem/FileSystemUtils.h new file mode 100644 index 000000000..ae302e07c --- /dev/null +++ b/dom/filesystem/FileSystemUtils.h @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemUtils_h +#define mozilla_dom_FileSystemUtils_h + +class nsIFile; + +namespace mozilla { +namespace dom { + +#define FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL "/" +#define FILESYSTEM_DOM_PATH_SEPARATOR_CHAR '/' + +/* + * This class is for error handling. + * All methods in this class are static. + */ +class FileSystemUtils +{ +public: + /* + * Return true if aDescendantPath is a descendant of aPath. + */ + static bool + IsDescendantPath(const nsAString& aPath, + const nsAString& aDescendantPath); + + /** + * Return true if this is valid DOMPath. It also splits the path in + * subdirectories and stores them in aParts. + */ + static bool + IsValidRelativeDOMPath(const nsAString& aPath, + nsTArray& aParts); +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemUtils_h diff --git a/dom/filesystem/GetDirectoryListingTask.cpp b/dom/filesystem/GetDirectoryListingTask.cpp new file mode 100644 index 000000000..e3ac2933f --- /dev/null +++ b/dom/filesystem/GetDirectoryListingTask.cpp @@ -0,0 +1,390 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "GetDirectoryListingTask.h" + +#include "HTMLSplitOnSpacesTokenizer.h" +#include "js/Value.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/PFileSystemParams.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/UnionTypes.h" +#include "mozilla/dom/ipc/BlobChild.h" +#include "mozilla/dom/ipc/BlobParent.h" +#include "nsIFile.h" +#include "nsISimpleEnumerator.h" +#include "nsStringGlue.h" + +namespace mozilla { +namespace dom { + +/** + * GetDirectoryListingTaskChild + */ + +/* static */ already_AddRefed +GetDirectoryListingTaskChild::Create(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + const nsAString& aFilters, + ErrorResult& aRv) +{ + MOZ_ASSERT(aFileSystem); + MOZ_ASSERT(aDirectory); + aFileSystem->AssertIsOnOwningThread(); + + RefPtr task = + new GetDirectoryListingTaskChild(aFileSystem, aDirectory, aTargetPath, + aFilters); + + // aTargetPath can be null. In this case SetError will be called. + + nsCOMPtr globalObject = + do_QueryInterface(aFileSystem->GetParentObject()); + if (NS_WARN_IF(!globalObject)) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + task->mPromise = Promise::Create(globalObject, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetDirectoryListingTaskChild::GetDirectoryListingTaskChild(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + const nsAString& aFilters) + : FileSystemTaskChildBase(aFileSystem) + , mDirectory(aDirectory) + , mTargetPath(aTargetPath) + , mFilters(aFilters) +{ + MOZ_ASSERT(aFileSystem); + aFileSystem->AssertIsOnOwningThread(); +} + +GetDirectoryListingTaskChild::~GetDirectoryListingTaskChild() +{ + mFileSystem->AssertIsOnOwningThread(); +} + +already_AddRefed +GetDirectoryListingTaskChild::GetPromise() +{ + mFileSystem->AssertIsOnOwningThread(); + return RefPtr(mPromise).forget(); +} + +FileSystemParams +GetDirectoryListingTaskChild::GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const +{ + mFileSystem->AssertIsOnOwningThread(); + + // this is the real path. + nsAutoString path; + aRv = mTargetPath->GetPath(path); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemGetDirectoryListingParams(); + } + + // this is the dom path. + nsAutoString directoryPath; + mDirectory->GetPath(directoryPath, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemGetDirectoryListingParams(); + } + + return FileSystemGetDirectoryListingParams(aSerializedDOMPath, path, + directoryPath, mFilters); +} + +void +GetDirectoryListingTaskChild::SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) +{ + mFileSystem->AssertIsOnOwningThread(); + MOZ_ASSERT(aValue.type() == + FileSystemResponseValue::TFileSystemDirectoryListingResponse); + + FileSystemDirectoryListingResponse r = aValue; + for (uint32_t i = 0; i < r.data().Length(); ++i) { + const FileSystemDirectoryListingResponseData& data = r.data()[i]; + + OwningFileOrDirectory* ofd = mTargetData.AppendElement(fallible); + if (!ofd) { + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); + return; + } + + if (data.type() == FileSystemDirectoryListingResponseData::TFileSystemDirectoryListingResponseFile) { + const FileSystemDirectoryListingResponseFile& d = + data.get_FileSystemDirectoryListingResponseFile(); + + RefPtr blobImpl = + static_cast(d.blobChild())->GetBlobImpl(); + MOZ_ASSERT(blobImpl); + + RefPtr file = File::Create(mFileSystem->GetParentObject(), blobImpl); + MOZ_ASSERT(file); + + ofd->SetAsFile() = file; + } else { + MOZ_ASSERT(data.type() == FileSystemDirectoryListingResponseData::TFileSystemDirectoryListingResponseDirectory); + const FileSystemDirectoryListingResponseDirectory& d = + data.get_FileSystemDirectoryListingResponseDirectory(); + + nsCOMPtr path; + aRv = NS_NewLocalFile(d.directoryRealPath(), true, getter_AddRefs(path)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + RefPtr directory = + Directory::Create(mFileSystem->GetParentObject(), path, mFileSystem); + MOZ_ASSERT(directory); + + ofd->SetAsDirectory() = directory; + } + } +} + +void +GetDirectoryListingTaskChild::HandlerCallback() +{ + mFileSystem->AssertIsOnOwningThread(); + + if (mFileSystem->IsShutdown()) { + mPromise = nullptr; + return; + } + + if (HasError()) { + mPromise->MaybeReject(mErrorValue); + mPromise = nullptr; + return; + } + + mPromise->MaybeResolve(mTargetData); + mPromise = nullptr; +} + +/** + * GetDirectoryListingTaskParent + */ + +/* static */ already_AddRefed +GetDirectoryListingTaskParent::Create(FileSystemBase* aFileSystem, + const FileSystemGetDirectoryListingParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); + + RefPtr task = + new GetDirectoryListingTaskParent(aFileSystem, aParam, aParent); + + aRv = NS_NewLocalFile(aParam.realPath(), true, + getter_AddRefs(task->mTargetPath)); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetDirectoryListingTaskParent::GetDirectoryListingTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetDirectoryListingParams& aParam, + FileSystemRequestParent* aParent) + : FileSystemTaskParentBase(aFileSystem, aParam, aParent) + , mDOMPath(aParam.domPath()) + , mFilters(aParam.filters()) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); +} + +FileSystemResponseValue +GetDirectoryListingTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const +{ + AssertIsOnBackgroundThread(); + + InfallibleTArray blobs; + + nsTArray inputs; + + for (unsigned i = 0; i < mTargetData.Length(); i++) { + if (mTargetData[i].mType == FileOrDirectoryPath::eFilePath) { + nsCOMPtr path; + nsresult rv = NS_NewLocalFile(mTargetData[i].mPath, true, + getter_AddRefs(path)); + if (NS_WARN_IF(NS_FAILED(rv))) { + return FileSystemErrorResponse(rv); + } + + FileSystemDirectoryListingResponseFile fileData; + RefPtr blobImpl = new BlobImplFile(path); + + nsAutoString filePath; + filePath.Assign(mDOMPath); + + // This is specific for unix root filesystem. + if (!mDOMPath.EqualsLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL)) { + filePath.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + } + + nsAutoString name; + blobImpl->GetName(name); + filePath.Append(name); + blobImpl->SetDOMPath(filePath); + + fileData.blobParent() = + BlobParent::GetOrCreate(mRequestParent->Manager(), blobImpl); + inputs.AppendElement(fileData); + } else { + MOZ_ASSERT(mTargetData[i].mType == FileOrDirectoryPath::eDirectoryPath); + FileSystemDirectoryListingResponseDirectory directoryData; + directoryData.directoryRealPath() = mTargetData[i].mPath; + inputs.AppendElement(directoryData); + } + } + + FileSystemDirectoryListingResponse response; + response.data().SwapElements(inputs); + return response; +} + +nsresult +GetDirectoryListingTaskParent::IOWork() +{ + MOZ_ASSERT(XRE_IsParentProcess(), + "Only call from parent process!"); + MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!"); + + if (mFileSystem->IsShutdown()) { + return NS_ERROR_FAILURE; + } + + bool exists; + nsresult rv = mTargetPath->Exists(&exists); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!exists) { + if (!mFileSystem->ShouldCreateDirectory()) { + return NS_ERROR_DOM_FILE_NOT_FOUND_ERR; + } + + rv = mTargetPath->Create(nsIFile::DIRECTORY_TYPE, 0777); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + // Get isDirectory. + bool isDir; + rv = mTargetPath->IsDirectory(&isDir); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!isDir) { + return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; + } + + nsCOMPtr entries; + rv = mTargetPath->GetDirectoryEntries(getter_AddRefs(entries)); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + bool filterOutSensitive = false; + { + HTMLSplitOnSpacesTokenizer tokenizer(mFilters, ';'); + nsAutoString token; + while (tokenizer.hasMoreTokens()) { + token = tokenizer.nextToken(); + if (token.EqualsLiteral("filter-out-sensitive")) { + filterOutSensitive = true; + } else { + MOZ_CRASH("Unrecognized filter"); + } + } + } + + for (;;) { + bool hasMore = false; + if (NS_WARN_IF(NS_FAILED(entries->HasMoreElements(&hasMore))) || !hasMore) { + break; + } + nsCOMPtr supp; + if (NS_WARN_IF(NS_FAILED(entries->GetNext(getter_AddRefs(supp))))) { + break; + } + + nsCOMPtr currFile = do_QueryInterface(supp); + MOZ_ASSERT(currFile); + + bool isSpecial, isFile; + if (NS_WARN_IF(NS_FAILED(currFile->IsSpecial(&isSpecial))) || + isSpecial) { + continue; + } + if (NS_WARN_IF(NS_FAILED(currFile->IsFile(&isFile)) || + NS_FAILED(currFile->IsDirectory(&isDir))) || + !(isFile || isDir)) { + continue; + } + + if (filterOutSensitive) { + bool isHidden; + if (NS_WARN_IF(NS_FAILED(currFile->IsHidden(&isHidden))) || isHidden) { + continue; + } + nsAutoString leafName; + if (NS_WARN_IF(NS_FAILED(currFile->GetLeafName(leafName)))) { + continue; + } + if (leafName[0] == char16_t('.')) { + continue; + } + } + + nsAutoString path; + if (NS_WARN_IF(NS_FAILED(currFile->GetPath(path)))) { + continue; + } + + FileOrDirectoryPath element; + element.mPath = path; + element.mType = isDir ? FileOrDirectoryPath::eDirectoryPath + : FileOrDirectoryPath::eFilePath; + + if (!mTargetData.AppendElement(element, fallible)) { + return NS_ERROR_OUT_OF_MEMORY; + } + } + return NS_OK; +} + +nsresult +GetDirectoryListingTaskParent::GetTargetPath(nsAString& aPath) const +{ + return mTargetPath->GetPath(aPath); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/GetDirectoryListingTask.h b/dom/filesystem/GetDirectoryListingTask.h new file mode 100644 index 000000000..7692c8bae --- /dev/null +++ b/dom/filesystem/GetDirectoryListingTask.h @@ -0,0 +1,106 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_GetDirectoryListing_h +#define mozilla_dom_GetDirectoryListing_h + +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/FileSystemTaskBase.h" +#include "mozilla/ErrorResult.h" + +namespace mozilla { +namespace dom { + +class BlobImpl; +class FileSystemGetDirectoryListingParams; +class OwningFileOrDirectory; + +class GetDirectoryListingTaskChild final : public FileSystemTaskChildBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + const nsAString& aFilters, + ErrorResult& aRv); + + virtual + ~GetDirectoryListingTaskChild(); + + already_AddRefed + GetPromise(); + +private: + // If aDirectoryOnly is set, we should ensure that the target is a directory. + GetDirectoryListingTaskChild(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + const nsAString& aFilters); + + virtual FileSystemParams + GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const override; + + virtual void + SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) override; + + virtual void + HandlerCallback() override; + + RefPtr mPromise; + RefPtr mDirectory; + nsCOMPtr mTargetPath; + nsString mFilters; + + FallibleTArray mTargetData; +}; + +class GetDirectoryListingTaskParent final : public FileSystemTaskParentBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + const FileSystemGetDirectoryListingParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv); + + nsresult + GetTargetPath(nsAString& aPath) const override; + +private: + GetDirectoryListingTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetDirectoryListingParams& aParam, + FileSystemRequestParent* aParent); + + virtual FileSystemResponseValue + GetSuccessRequestResult(ErrorResult& aRv) const override; + + virtual nsresult + IOWork() override; + + nsCOMPtr mTargetPath; + nsString mDOMPath; + nsString mFilters; + + struct FileOrDirectoryPath + { + nsString mPath; + + enum { + eFilePath, + eDirectoryPath + } mType; + }; + + FallibleTArray mTargetData; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_GetDirectoryListing_h diff --git a/dom/filesystem/GetFileOrDirectoryTask.cpp b/dom/filesystem/GetFileOrDirectoryTask.cpp new file mode 100644 index 000000000..f71976503 --- /dev/null +++ b/dom/filesystem/GetFileOrDirectoryTask.cpp @@ -0,0 +1,280 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "GetFileOrDirectoryTask.h" + +#include "js/Value.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/PFileSystemParams.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/ipc/BlobChild.h" +#include "mozilla/dom/ipc/BlobParent.h" +#include "nsIFile.h" +#include "nsStringGlue.h" + +namespace mozilla { +namespace dom { + +/** + * GetFileOrDirectoryTaskChild + */ + +/* static */ already_AddRefed +GetFileOrDirectoryTaskChild::Create(FileSystemBase* aFileSystem, + nsIFile* aTargetPath, + ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + MOZ_ASSERT(aFileSystem); + + RefPtr task = + new GetFileOrDirectoryTaskChild(aFileSystem, aTargetPath); + + // aTargetPath can be null. In this case SetError will be called. + + nsCOMPtr globalObject = + do_QueryInterface(aFileSystem->GetParentObject()); + if (NS_WARN_IF(!globalObject)) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + task->mPromise = Promise::Create(globalObject, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetFileOrDirectoryTaskChild::GetFileOrDirectoryTaskChild(FileSystemBase* aFileSystem, + nsIFile* aTargetPath) + : FileSystemTaskChildBase(aFileSystem) + , mTargetPath(aTargetPath) +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + MOZ_ASSERT(aFileSystem); +} + +GetFileOrDirectoryTaskChild::~GetFileOrDirectoryTaskChild() +{ + MOZ_ASSERT(NS_IsMainThread()); +} + +already_AddRefed +GetFileOrDirectoryTaskChild::GetPromise() +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + return RefPtr(mPromise).forget(); +} + +FileSystemParams +GetFileOrDirectoryTaskChild::GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + + nsAutoString path; + aRv = mTargetPath->GetPath(path); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemGetFileOrDirectoryParams(); + } + + return FileSystemGetFileOrDirectoryParams(aSerializedDOMPath, path); +} + +void +GetFileOrDirectoryTaskChild::SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + switch (aValue.type()) { + case FileSystemResponseValue::TFileSystemFileResponse: { + FileSystemFileResponse r = aValue; + + RefPtr blobImpl = + static_cast(r.blobChild())->GetBlobImpl(); + MOZ_ASSERT(blobImpl); + + mResultFile = File::Create(mFileSystem->GetParentObject(), blobImpl); + MOZ_ASSERT(mResultFile); + break; + } + case FileSystemResponseValue::TFileSystemDirectoryResponse: { + FileSystemDirectoryResponse r = aValue; + + nsCOMPtr file; + aRv = NS_NewLocalFile(r.realPath(), true, getter_AddRefs(file)); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + mResultDirectory = Directory::Create(mFileSystem->GetParentObject(), + file, mFileSystem); + MOZ_ASSERT(mResultDirectory); + break; + } + default: { + NS_RUNTIMEABORT("not reached"); + break; + } + } +} + +void +GetFileOrDirectoryTaskChild::HandlerCallback() +{ + MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); + if (mFileSystem->IsShutdown()) { + mPromise = nullptr; + return; + } + + if (HasError()) { + mPromise->MaybeReject(mErrorValue); + mPromise = nullptr; + return; + } + + if (mResultDirectory) { + mPromise->MaybeResolve(mResultDirectory); + mResultDirectory = nullptr; + mPromise = nullptr; + return; + } + + MOZ_ASSERT(mResultFile); + mPromise->MaybeResolve(mResultFile); + mResultFile = nullptr; + mPromise = nullptr; +} + +/** + * GetFileOrDirectoryTaskParent + */ + +/* static */ already_AddRefed +GetFileOrDirectoryTaskParent::Create(FileSystemBase* aFileSystem, + const FileSystemGetFileOrDirectoryParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); + + RefPtr task = + new GetFileOrDirectoryTaskParent(aFileSystem, aParam, aParent); + + aRv = NS_NewLocalFile(aParam.realPath(), true, + getter_AddRefs(task->mTargetPath)); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetFileOrDirectoryTaskParent::GetFileOrDirectoryTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetFileOrDirectoryParams& aParam, + FileSystemRequestParent* aParent) + : FileSystemTaskParentBase(aFileSystem, aParam, aParent) + , mIsDirectory(false) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); +} + +FileSystemResponseValue +GetFileOrDirectoryTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const +{ + AssertIsOnBackgroundThread(); + + nsAutoString path; + aRv = mTargetPath->GetPath(path); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemDirectoryResponse(); + } + + if (mIsDirectory) { + return FileSystemDirectoryResponse(path); + } + + RefPtr blobImpl = new BlobImplFile(mTargetPath); + BlobParent* blobParent = + BlobParent::GetOrCreate(mRequestParent->Manager(), blobImpl); + return FileSystemFileResponse(blobParent, nullptr); +} + +nsresult +GetFileOrDirectoryTaskParent::IOWork() +{ + MOZ_ASSERT(XRE_IsParentProcess(), + "Only call from parent process!"); + MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!"); + + if (mFileSystem->IsShutdown()) { + return NS_ERROR_FAILURE; + } + + // Whether we want to get the root directory. + bool exists; + nsresult rv = mTargetPath->Exists(&exists); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!exists) { + if (!mFileSystem->ShouldCreateDirectory()) { + return NS_ERROR_DOM_FILE_NOT_FOUND_ERR; + } + + rv = mTargetPath->Create(nsIFile::DIRECTORY_TYPE, 0777); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + // Get isDirectory. + rv = mTargetPath->IsDirectory(&mIsDirectory); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (mIsDirectory) { + return NS_OK; + } + + bool isFile; + // Get isFile + rv = mTargetPath->IsFile(&isFile); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!isFile) { + // Neither directory or file. + return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; + } + + if (!mFileSystem->IsSafeFile(mTargetPath)) { + return NS_ERROR_DOM_SECURITY_ERR; + } + + return NS_OK; +} + +nsresult +GetFileOrDirectoryTaskParent::GetTargetPath(nsAString& aPath) const +{ + return mTargetPath->GetPath(aPath); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/GetFileOrDirectoryTask.h b/dom/filesystem/GetFileOrDirectoryTask.h new file mode 100644 index 000000000..2a53df158 --- /dev/null +++ b/dom/filesystem/GetFileOrDirectoryTask.h @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_GetFileOrDirectory_h +#define mozilla_dom_GetFileOrDirectory_h + +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/FileSystemTaskBase.h" +#include "mozilla/ErrorResult.h" + +namespace mozilla { +namespace dom { + +class BlobImpl; +class FileSystemGetFileOrDirectoryParams; + +class GetFileOrDirectoryTaskChild final : public FileSystemTaskChildBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + nsIFile* aTargetPath, + ErrorResult& aRv); + + virtual + ~GetFileOrDirectoryTaskChild(); + + already_AddRefed + GetPromise(); + +protected: + virtual FileSystemParams + GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const override; + + virtual void + SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) override; + virtual void + HandlerCallback() override; + +private: + GetFileOrDirectoryTaskChild(FileSystemBase* aFileSystem, + nsIFile* aTargetPath); + + RefPtr mPromise; + nsCOMPtr mTargetPath; + + RefPtr mResultFile; + RefPtr mResultDirectory; +}; + +class GetFileOrDirectoryTaskParent final : public FileSystemTaskParentBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + const FileSystemGetFileOrDirectoryParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv); + + nsresult + GetTargetPath(nsAString& aPath) const override; + +protected: + virtual FileSystemResponseValue + GetSuccessRequestResult(ErrorResult& aRv) const override; + + virtual nsresult + IOWork() override; + +private: + GetFileOrDirectoryTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetFileOrDirectoryParams& aParam, + FileSystemRequestParent* aParent); + + nsCOMPtr mTargetPath; + + // Whether we get a directory. + bool mIsDirectory; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_GetFileOrDirectory_h diff --git a/dom/filesystem/GetFilesHelper.cpp b/dom/filesystem/GetFilesHelper.cpp new file mode 100644 index 000000000..21d228a60 --- /dev/null +++ b/dom/filesystem/GetFilesHelper.cpp @@ -0,0 +1,643 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "GetFilesHelper.h" +#include "mozilla/dom/ContentChild.h" +#include "mozilla/dom/ContentParent.h" +#include "nsProxyRelease.h" + +namespace mozilla { +namespace dom { + +namespace { + +// This class is used in the DTOR of GetFilesHelper to release resources in the +// correct thread. +class ReleaseRunnable final : public Runnable +{ +public: + static void + MaybeReleaseOnMainThread(nsTArray>& aPromises, + nsTArray>& aCallbacks, + Sequence>& aFiles, + already_AddRefed aGlobal) + { + nsCOMPtr global(aGlobal); + if (NS_IsMainThread()) { + return; + } + + RefPtr runnable = + new ReleaseRunnable(aPromises, aCallbacks, aFiles, global.forget()); + NS_DispatchToMainThread(runnable); + } + + NS_IMETHOD + Run() override + { + MOZ_ASSERT(NS_IsMainThread()); + + mPromises.Clear(); + mCallbacks.Clear(); + mFiles.Clear(); + mGlobal = nullptr; + + return NS_OK; + } + +private: + ReleaseRunnable(nsTArray>& aPromises, + nsTArray>& aCallbacks, + Sequence>& aFiles, + already_AddRefed aGlobal) + { + mPromises.SwapElements(aPromises); + mCallbacks.SwapElements(aCallbacks); + mFiles.SwapElements(aFiles); + mGlobal = aGlobal; + } + + nsTArray> mPromises; + nsTArray> mCallbacks; + Sequence> mFiles; + nsCOMPtr mGlobal; +}; + +} // anonymous + +/////////////////////////////////////////////////////////////////////////////// +// GetFilesHelper Base class + +already_AddRefed +GetFilesHelper::Create(nsIGlobalObject* aGlobal, + const nsTArray& aFilesOrDirectory, + bool aRecursiveFlag, ErrorResult& aRv) +{ + RefPtr helper; + + if (XRE_IsParentProcess()) { + helper = new GetFilesHelper(aGlobal, aRecursiveFlag); + } else { + helper = new GetFilesHelperChild(aGlobal, aRecursiveFlag); + } + + nsAutoString directoryPath; + + for (uint32_t i = 0; i < aFilesOrDirectory.Length(); ++i) { + const OwningFileOrDirectory& data = aFilesOrDirectory[i]; + if (data.IsFile()) { + if (!helper->mFiles.AppendElement(data.GetAsFile(), fallible)) { + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); + return nullptr; + } + } else { + MOZ_ASSERT(data.IsDirectory()); + + // We support the upload of only 1 top-level directory from our + // directory picker. This means that we cannot have more than 1 + // Directory object in aFilesOrDirectory array. + MOZ_ASSERT(directoryPath.IsEmpty()); + + RefPtr directory = data.GetAsDirectory(); + MOZ_ASSERT(directory); + + aRv = directory->GetFullRealPath(directoryPath); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + } + } + + // No directories to explore. + if (directoryPath.IsEmpty()) { + helper->mListingCompleted = true; + return helper.forget(); + } + + MOZ_ASSERT(helper->mFiles.IsEmpty()); + helper->SetDirectoryPath(directoryPath); + + helper->Work(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return helper.forget(); +} + +GetFilesHelper::GetFilesHelper(nsIGlobalObject* aGlobal, bool aRecursiveFlag) + : GetFilesHelperBase(aRecursiveFlag) + , mGlobal(aGlobal) + , mListingCompleted(false) + , mErrorResult(NS_OK) + , mMutex("GetFilesHelper::mMutex") + , mCanceled(false) +{ +} + +GetFilesHelper::~GetFilesHelper() +{ + ReleaseRunnable::MaybeReleaseOnMainThread(mPromises, mCallbacks, mFiles, + mGlobal.forget()); +} + +void +GetFilesHelper::AddPromise(Promise* aPromise) +{ + MOZ_ASSERT(aPromise); + + // Still working. + if (!mListingCompleted) { + mPromises.AppendElement(aPromise); + return; + } + + MOZ_ASSERT(mPromises.IsEmpty()); + ResolveOrRejectPromise(aPromise); +} + +void +GetFilesHelper::AddCallback(GetFilesCallback* aCallback) +{ + MOZ_ASSERT(aCallback); + + // Still working. + if (!mListingCompleted) { + mCallbacks.AppendElement(aCallback); + return; + } + + MOZ_ASSERT(mCallbacks.IsEmpty()); + RunCallback(aCallback); +} + +void +GetFilesHelper::Unlink() +{ + mGlobal = nullptr; + mFiles.Clear(); + mPromises.Clear(); + mCallbacks.Clear(); + + { + MutexAutoLock lock(mMutex); + mCanceled = true; + } + + Cancel(); +} + +void +GetFilesHelper::Traverse(nsCycleCollectionTraversalCallback &cb) +{ + GetFilesHelper* tmp = this; + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal); + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFiles); + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromises); +} + +void +GetFilesHelper::Work(ErrorResult& aRv) +{ + nsCOMPtr target = + do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); + MOZ_ASSERT(target); + + aRv = target->Dispatch(this, NS_DISPATCH_NORMAL); +} + +NS_IMETHODIMP +GetFilesHelper::Run() +{ + MOZ_ASSERT(!mDirectoryPath.IsEmpty()); + MOZ_ASSERT(!mListingCompleted); + + // First step is to retrieve the list of file paths. + // This happens in the I/O thread. + if (!NS_IsMainThread()) { + RunIO(); + + // If this operation has been canceled, we don't have to go back to + // main-thread. + if (IsCanceled()) { + return NS_OK; + } + + return NS_DispatchToMainThread(this); + } + + // We are here, but we should not do anything on this thread because, in the + // meantime, the operation has been canceled. + if (IsCanceled()) { + return NS_OK; + } + + RunMainThread(); + + OperationCompleted(); + return NS_OK; +} + +void +GetFilesHelper::OperationCompleted() +{ + // We mark the operation as completed here. + mListingCompleted = true; + + // Let's process the pending promises. + nsTArray> promises; + promises.SwapElements(mPromises); + + for (uint32_t i = 0; i < promises.Length(); ++i) { + ResolveOrRejectPromise(promises[i]); + } + + // Let's process the pending callbacks. + nsTArray> callbacks; + callbacks.SwapElements(mCallbacks); + + for (uint32_t i = 0; i < callbacks.Length(); ++i) { + RunCallback(callbacks[i]); + } +} + +void +GetFilesHelper::RunIO() +{ + MOZ_ASSERT(!NS_IsMainThread()); + MOZ_ASSERT(!mDirectoryPath.IsEmpty()); + MOZ_ASSERT(!mListingCompleted); + + nsCOMPtr file; + mErrorResult = NS_NewLocalFile(mDirectoryPath, true, getter_AddRefs(file)); + if (NS_WARN_IF(NS_FAILED(mErrorResult))) { + return; + } + + nsAutoString leafName; + mErrorResult = file->GetLeafName(leafName); + if (NS_WARN_IF(NS_FAILED(mErrorResult))) { + return; + } + + nsAutoString domPath; + domPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + domPath.Append(leafName); + + mErrorResult = ExploreDirectory(domPath, file); +} + +void +GetFilesHelper::RunMainThread() +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(!mDirectoryPath.IsEmpty()); + MOZ_ASSERT(!mListingCompleted); + + // If there is an error, do nothing. + if (NS_FAILED(mErrorResult)) { + return; + } + + // Create the sequence of Files. + for (uint32_t i = 0; i < mTargetBlobImplArray.Length(); ++i) { + RefPtr domFile = File::Create(mGlobal, mTargetBlobImplArray[i]); + MOZ_ASSERT(domFile); + + if (!mFiles.AppendElement(domFile, fallible)) { + mErrorResult = NS_ERROR_OUT_OF_MEMORY; + mFiles.Clear(); + return; + } + } +} + +nsresult +GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile) +{ + MOZ_ASSERT(!NS_IsMainThread()); + MOZ_ASSERT(aFile); + + // We check if this operation has to be terminated at each recursion. + if (IsCanceled()) { + return NS_OK; + } + + nsresult rv = AddExploredDirectory(aFile); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + nsCOMPtr entries; + rv = aFile->GetDirectoryEntries(getter_AddRefs(entries)); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + for (;;) { + bool hasMore = false; + if (NS_WARN_IF(NS_FAILED(entries->HasMoreElements(&hasMore))) || !hasMore) { + break; + } + + nsCOMPtr supp; + if (NS_WARN_IF(NS_FAILED(entries->GetNext(getter_AddRefs(supp))))) { + break; + } + + nsCOMPtr currFile = do_QueryInterface(supp); + MOZ_ASSERT(currFile); + + bool isLink, isSpecial, isFile, isDir; + if (NS_WARN_IF(NS_FAILED(currFile->IsSymlink(&isLink)) || + NS_FAILED(currFile->IsSpecial(&isSpecial))) || + isSpecial) { + continue; + } + + if (NS_WARN_IF(NS_FAILED(currFile->IsFile(&isFile)) || + NS_FAILED(currFile->IsDirectory(&isDir))) || + !(isFile || isDir)) { + continue; + } + + // We don't want to explore loops of links. + if (isDir && isLink && !ShouldFollowSymLink(currFile)) { + continue; + } + + // The new domPath + nsAutoString domPath; + domPath.Assign(aDOMPath); + if (!aDOMPath.EqualsLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL)) { + domPath.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + } + + nsAutoString leafName; + if (NS_WARN_IF(NS_FAILED(currFile->GetLeafName(leafName)))) { + continue; + } + domPath.Append(leafName); + + if (isFile) { + RefPtr blobImpl = new BlobImplFile(currFile); + blobImpl->SetDOMPath(domPath); + + if (!mTargetBlobImplArray.AppendElement(blobImpl, fallible)) { + return NS_ERROR_OUT_OF_MEMORY; + } + + continue; + } + + MOZ_ASSERT(isDir); + if (!mRecursiveFlag) { + continue; + } + + // Recursive. + rv = ExploreDirectory(domPath, currFile); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + return NS_OK; +} + +nsresult +GetFilesHelperBase::AddExploredDirectory(nsIFile* aDir) +{ + nsresult rv; + +#ifdef DEBUG + bool isDir; + rv = aDir->IsDirectory(&isDir); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + MOZ_ASSERT(isDir, "Why are we here?"); +#endif + + bool isLink; + rv = aDir->IsSymlink(&isLink); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + nsAutoCString path; + + if (!isLink) { + nsAutoString path16; + rv = aDir->GetPath(path16); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + path = NS_ConvertUTF16toUTF8(path16); + } else { + rv = aDir->GetNativeTarget(path); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + mExploredDirectories.PutEntry(path); + return NS_OK; +} + +bool +GetFilesHelperBase::ShouldFollowSymLink(nsIFile* aDir) +{ +#ifdef DEBUG + bool isLink, isDir; + if (NS_WARN_IF(NS_FAILED(aDir->IsSymlink(&isLink)) || + NS_FAILED(aDir->IsDirectory(&isDir)))) { + return false; + } + + MOZ_ASSERT(isLink && isDir, "Why are we here?"); +#endif + + nsAutoCString targetPath; + if (NS_WARN_IF(NS_FAILED(aDir->GetNativeTarget(targetPath)))) { + return false; + } + + return !mExploredDirectories.Contains(targetPath); +} + +void +GetFilesHelper::ResolveOrRejectPromise(Promise* aPromise) +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mListingCompleted); + MOZ_ASSERT(aPromise); + + // Error propagation. + if (NS_FAILED(mErrorResult)) { + aPromise->MaybeReject(mErrorResult); + return; + } + + aPromise->MaybeResolve(mFiles); +} + +void +GetFilesHelper::RunCallback(GetFilesCallback* aCallback) +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mListingCompleted); + MOZ_ASSERT(aCallback); + + aCallback->Callback(mErrorResult, mFiles); +} + +/////////////////////////////////////////////////////////////////////////////// +// GetFilesHelperChild class + +void +GetFilesHelperChild::Work(ErrorResult& aRv) +{ + ContentChild* cc = ContentChild::GetSingleton(); + if (NS_WARN_IF(!cc)) { + aRv.Throw(NS_ERROR_FAILURE); + return; + } + + aRv = nsContentUtils::GenerateUUIDInPlace(mUUID); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + mPendingOperation = true; + cc->CreateGetFilesRequest(mDirectoryPath, mRecursiveFlag, mUUID, this); +} + +void +GetFilesHelperChild::Cancel() +{ + if (!mPendingOperation) { + return; + } + + ContentChild* cc = ContentChild::GetSingleton(); + if (NS_WARN_IF(!cc)) { + return; + } + + mPendingOperation = false; + cc->DeleteGetFilesRequest(mUUID, this); +} + +bool +GetFilesHelperChild::AppendBlobImpl(BlobImpl* aBlobImpl) +{ + MOZ_ASSERT(mPendingOperation); + MOZ_ASSERT(aBlobImpl); + MOZ_ASSERT(aBlobImpl->IsFile()); + + RefPtr file = File::Create(mGlobal, aBlobImpl); + MOZ_ASSERT(file); + + return mFiles.AppendElement(file, fallible); +} + +void +GetFilesHelperChild::Finished(nsresult aError) +{ + MOZ_ASSERT(mPendingOperation); + MOZ_ASSERT(NS_SUCCEEDED(mErrorResult)); + + mPendingOperation = false; + mErrorResult = aError; + + OperationCompleted(); +} + +/////////////////////////////////////////////////////////////////////////////// +// GetFilesHelperParent class + +class GetFilesHelperParentCallback final : public GetFilesCallback +{ +public: + explicit GetFilesHelperParentCallback(GetFilesHelperParent* aParent) + : mParent(aParent) + { + MOZ_ASSERT(aParent); + } + + void + Callback(nsresult aStatus, const Sequence>& aFiles) override + { + if (NS_FAILED(aStatus)) { + mParent->mContentParent->SendGetFilesResponseAndForget(mParent->mUUID, + GetFilesResponseFailure(aStatus)); + return; + } + + GetFilesResponseSuccess success; + nsTArray& blobsParent = success.blobsParent(); + blobsParent.SetLength(aFiles.Length()); + + for (uint32_t i = 0; i < aFiles.Length(); ++i) { + blobsParent[i] = + mParent->mContentParent->GetOrCreateActorForBlob(aFiles[i]); + if (!blobsParent[i]) { + mParent->mContentParent->SendGetFilesResponseAndForget(mParent->mUUID, + GetFilesResponseFailure(NS_ERROR_OUT_OF_MEMORY)); + return; + } + } + + mParent->mContentParent->SendGetFilesResponseAndForget(mParent->mUUID, + success); + } + +private: + // Raw pointer because this callback is kept alive by this parent object. + GetFilesHelperParent* mParent; +}; + +GetFilesHelperParent::GetFilesHelperParent(const nsID& aUUID, + ContentParent* aContentParent, + bool aRecursiveFlag) + : GetFilesHelper(nullptr, aRecursiveFlag) + , mContentParent(aContentParent) + , mUUID(aUUID) +{} + +GetFilesHelperParent::~GetFilesHelperParent() +{ + NS_ReleaseOnMainThread(mContentParent.forget()); +} + +/* static */ already_AddRefed +GetFilesHelperParent::Create(const nsID& aUUID, const nsAString& aDirectoryPath, + bool aRecursiveFlag, ContentParent* aContentParent, + ErrorResult& aRv) +{ + MOZ_ASSERT(aContentParent); + + RefPtr helper = + new GetFilesHelperParent(aUUID, aContentParent, aRecursiveFlag); + helper->SetDirectoryPath(aDirectoryPath); + + helper->Work(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr callback = + new GetFilesHelperParentCallback(helper); + helper->AddCallback(callback); + + return helper.forget(); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/GetFilesHelper.h b/dom/filesystem/GetFilesHelper.h new file mode 100644 index 000000000..a1bb1b27a --- /dev/null +++ b/dom/filesystem/GetFilesHelper.h @@ -0,0 +1,205 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_GetFilesHelper_h +#define mozilla_dom_GetFilesHelper_h + +#include "mozilla/Mutex.h" +#include "mozilla/RefPtr.h" +#include "nsCycleCollectionTraversalCallback.h" +#include "nsTArray.h" +#include "nsTHashtable.h" + +class nsIGlobalObject; + +namespace mozilla { +namespace dom { + +class BlobImpl; +class ContentParent; +class File; +class GetFilesHelperParent; +class OwningFileOrDirectory; +class Promise; + +class GetFilesCallback +{ +public: + NS_INLINE_DECL_REFCOUNTING(GetFilesCallback); + + virtual void + Callback(nsresult aStatus, const Sequence>& aFiles) = 0; + +protected: + virtual ~GetFilesCallback() {} +}; + +class GetFilesHelperBase +{ +protected: + explicit GetFilesHelperBase(bool aRecursiveFlag) + : mRecursiveFlag(aRecursiveFlag) + {} + + virtual ~GetFilesHelperBase() {} + + virtual bool + IsCanceled() + { + return false; + } + + nsresult + ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile); + + nsresult + AddExploredDirectory(nsIFile* aDirectory); + + bool + ShouldFollowSymLink(nsIFile* aDirectory); + + bool mRecursiveFlag; + + // We populate this array in the I/O thread with the BlobImpl. + FallibleTArray> mTargetBlobImplArray; + nsTHashtable mExploredDirectories; +}; + +// Retrieving the list of files can be very time/IO consuming. We use this +// helper class to do it just once. +class GetFilesHelper : public Runnable + , public GetFilesHelperBase +{ + friend class GetFilesHelperParent; + +public: + static already_AddRefed + Create(nsIGlobalObject* aGlobal, + const nsTArray& aFilesOrDirectory, + bool aRecursiveFlag, ErrorResult& aRv); + + void + AddPromise(Promise* aPromise); + + void + AddCallback(GetFilesCallback* aCallback); + + // CC methods + void Unlink(); + void Traverse(nsCycleCollectionTraversalCallback &cb); + +protected: + GetFilesHelper(nsIGlobalObject* aGlobal, bool aRecursiveFlag); + + virtual ~GetFilesHelper(); + + void + SetDirectoryPath(const nsAString& aDirectoryPath) + { + mDirectoryPath = aDirectoryPath; + } + + virtual bool + IsCanceled() override + { + MutexAutoLock lock(mMutex); + return mCanceled; + } + + virtual void + Work(ErrorResult& aRv); + + virtual void + Cancel() {}; + + NS_IMETHOD + Run() override; + + void + RunIO(); + + void + RunMainThread(); + + void + OperationCompleted(); + + void + ResolveOrRejectPromise(Promise* aPromise); + + void + RunCallback(GetFilesCallback* aCallback); + + nsCOMPtr mGlobal; + + bool mListingCompleted; + nsString mDirectoryPath; + + // This is the real File sequence that we expose via Promises. + Sequence> mFiles; + + // Error code to propagate. + nsresult mErrorResult; + + nsTArray> mPromises; + nsTArray> mCallbacks; + + Mutex mMutex; + + // This variable is protected by mutex. + bool mCanceled; +}; + +class GetFilesHelperChild final : public GetFilesHelper +{ +public: + GetFilesHelperChild(nsIGlobalObject* aGlobal, bool aRecursiveFlag) + : GetFilesHelper(aGlobal, aRecursiveFlag) + , mPendingOperation(false) + {} + + virtual void + Work(ErrorResult& aRv) override; + + virtual void + Cancel() override; + + bool + AppendBlobImpl(BlobImpl* aBlobImpl); + + void + Finished(nsresult aResult); + +private: + nsID mUUID; + bool mPendingOperation; +}; + +class GetFilesHelperParentCallback; + +class GetFilesHelperParent final : public GetFilesHelper +{ + friend class GetFilesHelperParentCallback; + +public: + static already_AddRefed + Create(const nsID& aUUID, const nsAString& aDirectoryPath, + bool aRecursiveFlag, ContentParent* aContentParent, ErrorResult& aRv); + +private: + GetFilesHelperParent(const nsID& aUUID, ContentParent* aContentParent, + bool aRecursiveFlag); + + ~GetFilesHelperParent(); + + RefPtr mContentParent; + nsID mUUID; +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_GetFilesHelper_h diff --git a/dom/filesystem/GetFilesTask.cpp b/dom/filesystem/GetFilesTask.cpp new file mode 100644 index 000000000..6682fb7d3 --- /dev/null +++ b/dom/filesystem/GetFilesTask.cpp @@ -0,0 +1,263 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "GetFilesTask.h" + +#include "HTMLSplitOnSpacesTokenizer.h" +#include "js/Value.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileSystemBase.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/PFileSystemParams.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/ipc/BlobChild.h" +#include "mozilla/dom/ipc/BlobParent.h" +#include "nsIFile.h" +#include "nsStringGlue.h" + +namespace mozilla { +namespace dom { + +/** + * GetFilesTaskChild + */ + +/* static */ already_AddRefed +GetFilesTaskChild::Create(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + bool aRecursiveFlag, + ErrorResult& aRv) +{ + MOZ_ASSERT(aFileSystem); + MOZ_ASSERT(aDirectory); + aFileSystem->AssertIsOnOwningThread(); + + nsCOMPtr globalObject = + do_QueryInterface(aFileSystem->GetParentObject()); + if (NS_WARN_IF(!globalObject)) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + RefPtr task = + new GetFilesTaskChild(aFileSystem, aDirectory, aTargetPath, + aRecursiveFlag); + + // aTargetPath can be null. In this case SetError will be called. + + task->mPromise = Promise::Create(globalObject, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetFilesTaskChild::GetFilesTaskChild(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + bool aRecursiveFlag) + : FileSystemTaskChildBase(aFileSystem) + , mDirectory(aDirectory) + , mTargetPath(aTargetPath) + , mRecursiveFlag(aRecursiveFlag) +{ + MOZ_ASSERT(aFileSystem); + MOZ_ASSERT(aDirectory); + aFileSystem->AssertIsOnOwningThread(); +} + +GetFilesTaskChild::~GetFilesTaskChild() +{ + mFileSystem->AssertIsOnOwningThread(); +} + +already_AddRefed +GetFilesTaskChild::GetPromise() +{ + mFileSystem->AssertIsOnOwningThread(); + return RefPtr(mPromise).forget(); +} + +FileSystemParams +GetFilesTaskChild::GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const +{ + mFileSystem->AssertIsOnOwningThread(); + + nsAutoString path; + aRv = mTargetPath->GetPath(path); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemGetFilesParams(); + } + + nsAutoString domPath; + mDirectory->GetPath(domPath, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return FileSystemGetFilesParams(); + } + + return FileSystemGetFilesParams(aSerializedDOMPath, path, domPath, + mRecursiveFlag); +} + +void +GetFilesTaskChild::SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) +{ + mFileSystem->AssertIsOnOwningThread(); + MOZ_ASSERT(aValue.type() == + FileSystemResponseValue::TFileSystemFilesResponse); + + FileSystemFilesResponse r = aValue; + + if (!mTargetData.SetLength(r.data().Length(), mozilla::fallible_t())) { + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); + return; + } + + for (uint32_t i = 0; i < r.data().Length(); ++i) { + const FileSystemFileResponse& data = r.data()[i]; + RefPtr blobImpl = + static_cast(data.blobChild())->GetBlobImpl(); + MOZ_ASSERT(blobImpl); + + mTargetData[i] = File::Create(mFileSystem->GetParentObject(), blobImpl); + } +} + +void +GetFilesTaskChild::HandlerCallback() +{ + mFileSystem->AssertIsOnOwningThread(); + if (mFileSystem->IsShutdown()) { + mPromise = nullptr; + return; + } + + if (HasError()) { + mPromise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR); + mPromise = nullptr; + return; + } + + mPromise->MaybeResolve(mTargetData); + mPromise = nullptr; +} + +/** + * GetFilesTaskParent + */ + +/* static */ already_AddRefed +GetFilesTaskParent::Create(FileSystemBase* aFileSystem, + const FileSystemGetFilesParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); + + RefPtr task = + new GetFilesTaskParent(aFileSystem, aParam, aParent); + + aRv = NS_NewLocalFile(aParam.realPath(), true, + getter_AddRefs(task->mTargetPath)); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return task.forget(); +} + +GetFilesTaskParent::GetFilesTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetFilesParams& aParam, + FileSystemRequestParent* aParent) + : FileSystemTaskParentBase(aFileSystem, aParam, aParent) + , GetFilesHelperBase(aParam.recursiveFlag()) + , mDirectoryDOMPath(aParam.domPath()) +{ + MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!"); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aFileSystem); +} + +FileSystemResponseValue +GetFilesTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const +{ + AssertIsOnBackgroundThread(); + + InfallibleTArray blobs; + + FallibleTArray inputs; + if (!inputs.SetLength(mTargetBlobImplArray.Length(), mozilla::fallible_t())) { + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); + FileSystemFilesResponse response; + return response; + } + + for (unsigned i = 0; i < mTargetBlobImplArray.Length(); i++) { + BlobParent* blobParent = + BlobParent::GetOrCreate(mRequestParent->Manager(), + mTargetBlobImplArray[i]); + inputs[i] = FileSystemFileResponse(blobParent, nullptr); + } + + FileSystemFilesResponse response; + response.data().SwapElements(inputs); + return response; +} + +nsresult +GetFilesTaskParent::IOWork() +{ + MOZ_ASSERT(XRE_IsParentProcess(), + "Only call from parent process!"); + MOZ_ASSERT(!NS_IsMainThread(), "Only call on I/O thread!"); + + if (mFileSystem->IsShutdown()) { + return NS_ERROR_FAILURE; + } + + bool exists; + nsresult rv = mTargetPath->Exists(&exists); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!exists) { + return NS_OK; + } + + bool isDir; + rv = mTargetPath->IsDirectory(&isDir); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!isDir) { + return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; + } + + // Get isDirectory. + rv = ExploreDirectory(mDirectoryDOMPath, mTargetPath); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + return NS_OK; +} + +nsresult +GetFilesTaskParent::GetTargetPath(nsAString& aPath) const +{ + return mTargetPath->GetPath(aPath); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/GetFilesTask.h b/dom/filesystem/GetFilesTask.h new file mode 100644 index 000000000..be08a5d18 --- /dev/null +++ b/dom/filesystem/GetFilesTask.h @@ -0,0 +1,95 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_GetFilesTask_h +#define mozilla_dom_GetFilesTask_h + +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/FileSystemTaskBase.h" +#include "mozilla/dom/GetFilesHelper.h" +#include "mozilla/ErrorResult.h" + +namespace mozilla { +namespace dom { + +class BlobImpl; +class FileSystemGetFilesParams; + +class GetFilesTaskChild final : public FileSystemTaskChildBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + bool aRecursiveFlag, + ErrorResult& aRv); + + virtual + ~GetFilesTaskChild(); + + already_AddRefed + GetPromise(); + +private: + // If aDirectoryOnly is set, we should ensure that the target is a directory. + GetFilesTaskChild(FileSystemBase* aFileSystem, + Directory* aDirectory, + nsIFile* aTargetPath, + bool aRecursiveFlag); + + virtual FileSystemParams + GetRequestParams(const nsString& aSerializedDOMPath, + ErrorResult& aRv) const override; + + virtual void + SetSuccessRequestResult(const FileSystemResponseValue& aValue, + ErrorResult& aRv) override; + + virtual void + HandlerCallback() override; + + RefPtr mPromise; + RefPtr mDirectory; + nsCOMPtr mTargetPath; + bool mRecursiveFlag; + + // We store the fullpath and the dom path of Files. + FallibleTArray> mTargetData; +}; + +class GetFilesTaskParent final : public FileSystemTaskParentBase + , public GetFilesHelperBase +{ +public: + static already_AddRefed + Create(FileSystemBase* aFileSystem, + const FileSystemGetFilesParams& aParam, + FileSystemRequestParent* aParent, + ErrorResult& aRv); + + nsresult + GetTargetPath(nsAString& aPath) const override; + +private: + GetFilesTaskParent(FileSystemBase* aFileSystem, + const FileSystemGetFilesParams& aParam, + FileSystemRequestParent* aParent); + + virtual FileSystemResponseValue + GetSuccessRequestResult(ErrorResult& aRv) const override; + + virtual nsresult + IOWork() override; + + nsString mDirectoryDOMPath; + nsCOMPtr mTargetPath; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_GetFilesTask_h diff --git a/dom/filesystem/OSFileSystem.cpp b/dom/filesystem/OSFileSystem.cpp new file mode 100644 index 000000000..1e9a5ddbd --- /dev/null +++ b/dom/filesystem/OSFileSystem.cpp @@ -0,0 +1,113 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/OSFileSystem.h" + +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "nsIGlobalObject.h" +#include "nsCOMPtr.h" +#include "nsDebug.h" +#include "nsIFile.h" + +namespace mozilla { +namespace dom { + +OSFileSystem::OSFileSystem(const nsAString& aRootDir) +{ + mLocalRootPath = aRootDir; +} + +already_AddRefed +OSFileSystem::Clone() +{ + AssertIsOnOwningThread(); + + RefPtr fs = new OSFileSystem(mLocalRootPath); + if (mParent) { + fs->Init(mParent); + } + + return fs.forget(); +} + +void +OSFileSystem::Init(nsISupports* aParent) +{ + AssertIsOnOwningThread(); + MOZ_ASSERT(!mParent, "No duple Init() calls"); + MOZ_ASSERT(aParent); + + mParent = aParent; + +#ifdef DEBUG + nsCOMPtr obj = do_QueryInterface(aParent); + MOZ_ASSERT(obj); +#endif +} + +nsISupports* +OSFileSystem::GetParentObject() const +{ + AssertIsOnOwningThread(); + return mParent; +} + +bool +OSFileSystem::IsSafeFile(nsIFile* aFile) const +{ + // The concept of "safe files" is specific to the Device Storage API where + // files are only "safe" if they're of a type that is appropriate for the + // area of device storage that is being used. + MOZ_CRASH("Don't use OSFileSystem with the Device Storage API"); + return true; +} + +bool +OSFileSystem::IsSafeDirectory(Directory* aDir) const +{ + // The concept of "safe directory" is specific to the Device Storage API + // where a directory is only "safe" if it belongs to the area of device + // storage that it is being used with. + MOZ_CRASH("Don't use OSFileSystem with the Device Storage API"); + return true; +} + +void +OSFileSystem::Unlink() +{ + AssertIsOnOwningThread(); + mParent = nullptr; +} + +void +OSFileSystem::Traverse(nsCycleCollectionTraversalCallback &cb) +{ + AssertIsOnOwningThread(); + + OSFileSystem* tmp = this; + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent); +} + +void +OSFileSystem::SerializeDOMPath(nsAString& aOutput) const +{ + AssertIsOnOwningThread(); + aOutput = mLocalRootPath; +} + +/** + * OSFileSystemParent + */ + +OSFileSystemParent::OSFileSystemParent(const nsAString& aRootDir) +{ + mLocalRootPath = aRootDir; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/OSFileSystem.h b/dom/filesystem/OSFileSystem.h new file mode 100644 index 000000000..165f6b99a --- /dev/null +++ b/dom/filesystem/OSFileSystem.h @@ -0,0 +1,132 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_OSFileSystem_h +#define mozilla_dom_OSFileSystem_h + +#include "mozilla/dom/FileSystemBase.h" + +namespace mozilla { +namespace dom { + +class OSFileSystem final : public FileSystemBase +{ +public: + explicit OSFileSystem(const nsAString& aRootDir); + + void + Init(nsISupports* aParent); + + // Overrides FileSystemBase + + virtual already_AddRefed + Clone() override; + + virtual bool + ShouldCreateDirectory() override + { + MOZ_CRASH("This should not be called."); + // Because OSFileSystem should not be used when the creation of directories + // is needed. For that we have OSFileSystemParent. + return false; + } + + virtual nsISupports* + GetParentObject() const override; + + virtual bool + IsSafeFile(nsIFile* aFile) const override; + + virtual bool + IsSafeDirectory(Directory* aDir) const override; + + virtual void + SerializeDOMPath(nsAString& aOutput) const override; + + virtual bool + ClonableToDifferentThreadOrProcess() const override { return true; } + + // CC methods + virtual void Unlink() override; + virtual void Traverse(nsCycleCollectionTraversalCallback &cb) override; + +private: + virtual ~OSFileSystem() {} + + nsCOMPtr mParent; +}; + +class OSFileSystemParent final : public FileSystemBase +{ +public: + explicit OSFileSystemParent(const nsAString& aRootDir); + + // Overrides FileSystemBase + + virtual already_AddRefed + Clone() override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + return nullptr; + } + + virtual bool + ShouldCreateDirectory() override { return false; } + + virtual nsISupports* + GetParentObject() const override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + return nullptr; + } + + virtual void + GetDirectoryName(nsIFile* aFile, nsAString& aRetval, + ErrorResult& aRv) const override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + } + + virtual bool + IsSafeFile(nsIFile* aFile) const override + { + return true; + } + + virtual bool + IsSafeDirectory(Directory* aDir) const override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + return true; + } + + virtual void + SerializeDOMPath(nsAString& aOutput) const override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + } + + // CC methods + virtual void + Unlink() override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + } + + virtual void + Traverse(nsCycleCollectionTraversalCallback &cb) override + { + MOZ_CRASH("This should not be called on the PBackground thread."); + } + +private: + virtual ~OSFileSystemParent() {} +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_OSFileSystem_h diff --git a/dom/filesystem/PFileSystemParams.ipdlh b/dom/filesystem/PFileSystemParams.ipdlh new file mode 100644 index 000000000..08929b6f2 --- /dev/null +++ b/dom/filesystem/PFileSystemParams.ipdlh @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +include protocol PBlob; + +namespace mozilla { +namespace dom { + +struct FileSystemGetDirectoryListingParams +{ + nsString filesystem; + nsString realPath; + nsString domPath; + + // 'filters' could be an array rather than a semicolon separated string + // (we'd then use InfallibleTArray internally), but that is + // wasteful. E10s requires us to pass the filters over as a string anyway, + // so avoiding using an array avoids serialization on the side passing the + // filters. Since an nsString can share its buffer when copied, + // using that instead of InfallibleTArray makes copying the filters + // around in any given process a bit more efficient too, since copying a + // single nsString is cheaper than copying InfallibleTArray member data and + // each nsString that it contains. + nsString filters; +}; + +struct FileSystemGetFilesParams +{ + nsString filesystem; + nsString realPath; + nsString domPath; + bool recursiveFlag; +}; + +struct FileSystemGetFileOrDirectoryParams +{ + nsString filesystem; + nsString realPath; +}; + +union FileSystemParams +{ + FileSystemGetDirectoryListingParams; + FileSystemGetFilesParams; + FileSystemGetFileOrDirectoryParams; +}; + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/PFileSystemRequest.ipdl b/dom/filesystem/PFileSystemRequest.ipdl new file mode 100644 index 000000000..49803a770 --- /dev/null +++ b/dom/filesystem/PFileSystemRequest.ipdl @@ -0,0 +1,73 @@ +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +include protocol PBackground; +include protocol PBlob; + +namespace mozilla { +namespace dom { + +struct FileSystemFileResponse +{ + PBlob blob; +}; + +struct FileSystemDirectoryResponse +{ + nsString realPath; +}; + +struct FileSystemDirectoryListingResponseFile +{ + PBlob blob; +}; + +struct FileSystemDirectoryListingResponseDirectory +{ + // This is the full real path for the directory that we are sending via IPC. + nsString directoryRealPath; +}; + +union FileSystemDirectoryListingResponseData +{ + FileSystemDirectoryListingResponseFile; + FileSystemDirectoryListingResponseDirectory; +}; + +struct FileSystemDirectoryListingResponse +{ + FileSystemDirectoryListingResponseData[] data; +}; + +struct FileSystemFilesResponse +{ + FileSystemFileResponse[] data; +}; + +struct FileSystemErrorResponse +{ + nsresult error; +}; + +union FileSystemResponseValue +{ + FileSystemDirectoryResponse; + FileSystemDirectoryListingResponse; + FileSystemFileResponse; + FileSystemFilesResponse; + FileSystemErrorResponse; +}; + +protocol PFileSystemRequest +{ + manager PBackground; + +child: + async __delete__(FileSystemResponseValue response); +}; + +} // namespace dom +} // namespace mozilla diff --git a/dom/filesystem/compat/CallbackRunnables.cpp b/dom/filesystem/compat/CallbackRunnables.cpp new file mode 100644 index 000000000..efd14b03e --- /dev/null +++ b/dom/filesystem/compat/CallbackRunnables.cpp @@ -0,0 +1,306 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "CallbackRunnables.h" +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/DirectoryBinding.h" +#include "mozilla/dom/DOMException.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/FileBinding.h" +#include "mozilla/dom/FileSystemDirectoryReaderBinding.h" +#include "mozilla/dom/FileSystemFileEntry.h" +#include "mozilla/dom/FileSystemUtils.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/Unused.h" +#include "nsIGlobalObject.h" +#include "nsIFile.h" +#include "nsPIDOMWindow.h" + +#include "../GetFileOrDirectoryTask.h" + +namespace mozilla { +namespace dom { + +EntryCallbackRunnable::EntryCallbackRunnable(FileSystemEntryCallback* aCallback, + FileSystemEntry* aEntry) + : mCallback(aCallback) + , mEntry(aEntry) +{ + MOZ_ASSERT(aCallback); + MOZ_ASSERT(aEntry); +} + +NS_IMETHODIMP +EntryCallbackRunnable::Run() +{ + mCallback->HandleEvent(*mEntry); + return NS_OK; +} + +ErrorCallbackRunnable::ErrorCallbackRunnable(nsIGlobalObject* aGlobalObject, + ErrorCallback* aCallback, + nsresult aError) + : mGlobal(aGlobalObject) + , mCallback(aCallback) + , mError(aError) +{ + MOZ_ASSERT(aGlobalObject); + MOZ_ASSERT(aCallback); + MOZ_ASSERT(NS_FAILED(aError)); +} + +NS_IMETHODIMP +ErrorCallbackRunnable::Run() +{ + nsCOMPtr window = do_QueryInterface(mGlobal); + if (NS_WARN_IF(!window)) { + return NS_ERROR_FAILURE; + } + + RefPtr exception = DOMException::Create(mError); + mCallback->HandleEvent(*exception); + return NS_OK; +} + +EmptyEntriesCallbackRunnable::EmptyEntriesCallbackRunnable(FileSystemEntriesCallback* aCallback) + : mCallback(aCallback) +{ + MOZ_ASSERT(aCallback); +} + +NS_IMETHODIMP +EmptyEntriesCallbackRunnable::Run() +{ + Sequence> sequence; + mCallback->HandleEvent(sequence); + return NS_OK; +} + +GetEntryHelper::GetEntryHelper(FileSystemDirectoryEntry* aParentEntry, + Directory* aDirectory, + nsTArray& aParts, + FileSystem* aFileSystem, + FileSystemEntryCallback* aSuccessCallback, + ErrorCallback* aErrorCallback, + FileSystemDirectoryEntry::GetInternalType aType) + : mParentEntry(aParentEntry) + , mDirectory(aDirectory) + , mParts(aParts) + , mFileSystem(aFileSystem) + , mSuccessCallback(aSuccessCallback) + , mErrorCallback(aErrorCallback) + , mType(aType) +{ + MOZ_ASSERT(aParentEntry); + MOZ_ASSERT(aDirectory); + MOZ_ASSERT(!aParts.IsEmpty()); + MOZ_ASSERT(aFileSystem); + MOZ_ASSERT(aSuccessCallback || aErrorCallback); +} + +GetEntryHelper::~GetEntryHelper() +{} + +namespace { + +nsresult +DOMPathToRealPath(Directory* aDirectory, const nsAString& aPath, + nsIFile** aFile) +{ + nsString relativePath; + relativePath = aPath; + + // Trim white spaces. + static const char kWhitespace[] = "\b\t\r\n "; + relativePath.Trim(kWhitespace); + + nsTArray parts; + if (!FileSystemUtils::IsValidRelativeDOMPath(relativePath, parts)) { + return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; + } + + nsCOMPtr file; + nsresult rv = aDirectory->GetInternalNsIFile()->Clone(getter_AddRefs(file)); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + for (uint32_t i = 0; i < parts.Length(); ++i) { + rv = file->AppendRelativePath(parts[i]); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + file.forget(aFile); + return NS_OK; +} + +} // anonymous + +void +GetEntryHelper::Run() +{ + MOZ_ASSERT(!mParts.IsEmpty()); + + nsCOMPtr realPath; + nsresult error = DOMPathToRealPath(mDirectory, mParts[0], + getter_AddRefs(realPath)); + + ErrorResult rv; + RefPtr fs = mDirectory->GetFileSystem(rv); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + Error(NS_ERROR_DOM_INVALID_STATE_ERR); + return; + } + + RefPtr task = + GetFileOrDirectoryTaskChild::Create(fs, realPath, rv); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + Error(NS_ERROR_DOM_INVALID_STATE_ERR); + return; + } + + task->SetError(error); + task->Start(); + + RefPtr promise = task->GetPromise(); + + mParts.RemoveElementAt(0); + promise->AppendNativeHandler(this); +} + +void +GetEntryHelper::ResolvedCallback(JSContext* aCx, JS::Handle aValue) +{ + if(NS_WARN_IF(!aValue.isObject())) { + return; + } + + JS::Rooted obj(aCx, &aValue.toObject()); + + // This is not the last part of the path. + if (!mParts.IsEmpty()) { + ContinueRunning(obj); + return; + } + + CompleteOperation(obj); +} + +void +GetEntryHelper::CompleteOperation(JSObject* aObj) +{ + MOZ_ASSERT(mParts.IsEmpty()); + + if (mType == FileSystemDirectoryEntry::eGetFile) { + RefPtr file; + if (NS_FAILED(UNWRAP_OBJECT(File, aObj, file))) { + Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR); + return; + } + + RefPtr entry = + new FileSystemFileEntry(mParentEntry->GetParentObject(), file, + mParentEntry, mFileSystem); + mSuccessCallback->HandleEvent(*entry); + return; + } + + MOZ_ASSERT(mType == FileSystemDirectoryEntry::eGetDirectory); + + RefPtr directory; + if (NS_FAILED(UNWRAP_OBJECT(Directory, aObj, directory))) { + Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR); + return; + } + + RefPtr entry = + new FileSystemDirectoryEntry(mParentEntry->GetParentObject(), directory, + mParentEntry, mFileSystem); + mSuccessCallback->HandleEvent(*entry); +} + +void +GetEntryHelper::ContinueRunning(JSObject* aObj) +{ + MOZ_ASSERT(!mParts.IsEmpty()); + + RefPtr directory; + if (NS_FAILED(UNWRAP_OBJECT(Directory, aObj, directory))) { + Error(NS_ERROR_DOM_TYPE_MISMATCH_ERR); + return; + } + + RefPtr entry = + new FileSystemDirectoryEntry(mParentEntry->GetParentObject(), directory, + mParentEntry, mFileSystem); + + // Update the internal values. + mParentEntry = entry; + mDirectory = directory; + + Run(); +} + +void +GetEntryHelper::RejectedCallback(JSContext* aCx, JS::Handle aValue) +{ + Error(NS_ERROR_DOM_NOT_FOUND_ERR); +} + +void +GetEntryHelper::Error(nsresult aError) +{ + MOZ_ASSERT(NS_FAILED(aError)); + + if (mErrorCallback) { + RefPtr runnable = + new ErrorCallbackRunnable(mParentEntry->GetParentObject(), + mErrorCallback, aError); + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); + } +} + +NS_IMPL_ISUPPORTS0(GetEntryHelper); + +/* static */ void +FileSystemEntryCallbackHelper::Call(const Optional>& aEntryCallback, + FileSystemEntry* aEntry) +{ + MOZ_ASSERT(aEntry); + + if (aEntryCallback.WasPassed()) { + RefPtr runnable = + new EntryCallbackRunnable(&aEntryCallback.Value(), aEntry); + + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); + } +} + +/* static */ void +ErrorCallbackHelper::Call(nsIGlobalObject* aGlobal, + const Optional>& aErrorCallback, + nsresult aError) +{ + MOZ_ASSERT(aGlobal); + MOZ_ASSERT(NS_FAILED(aError)); + + if (aErrorCallback.WasPassed()) { + RefPtr runnable = + new ErrorCallbackRunnable(aGlobal, &aErrorCallback.Value(), aError); + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); + } +} + +} // dom namespace +} // mozilla namespace + diff --git a/dom/filesystem/compat/CallbackRunnables.h b/dom/filesystem/compat/CallbackRunnables.h new file mode 100644 index 000000000..3ff77f1a9 --- /dev/null +++ b/dom/filesystem/compat/CallbackRunnables.h @@ -0,0 +1,128 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_ErrorCallbackRunnable_h +#define mozilla_dom_ErrorCallbackRunnable_h + +#include "FileSystemDirectoryEntry.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/PromiseNativeHandler.h" + +class nsIGlobalObject; + +namespace mozilla { +namespace dom { + +class FileSystemEntriesCallback; + +class EntryCallbackRunnable final : public Runnable +{ +public: + EntryCallbackRunnable(FileSystemEntryCallback* aCallback, + FileSystemEntry* aEntry); + + NS_IMETHOD + Run() override; + +private: + RefPtr mCallback; + RefPtr mEntry; +}; + +class ErrorCallbackRunnable final : public Runnable +{ +public: + ErrorCallbackRunnable(nsIGlobalObject* aGlobalObject, + ErrorCallback* aCallback, + nsresult aError); + + NS_IMETHOD + Run() override; + +private: + nsCOMPtr mGlobal; + RefPtr mCallback; + nsresult mError; +}; + +class EmptyEntriesCallbackRunnable final : public Runnable +{ +public: + explicit EmptyEntriesCallbackRunnable(FileSystemEntriesCallback* aCallback); + + NS_IMETHOD + Run() override; + +private: + RefPtr mCallback; +}; + +class GetEntryHelper final : public PromiseNativeHandler +{ +public: + NS_DECL_ISUPPORTS + + GetEntryHelper(FileSystemDirectoryEntry* aParentEntry, + Directory* aDirectory, + nsTArray& aParts, + FileSystem* aFileSystem, + FileSystemEntryCallback* aSuccessCallback, + ErrorCallback* aErrorCallback, + FileSystemDirectoryEntry::GetInternalType aType); + + void + Run(); + + virtual void + ResolvedCallback(JSContext* aCx, JS::Handle aValue) override; + + virtual void + RejectedCallback(JSContext* aCx, JS::Handle aValue) override; + +private: + ~GetEntryHelper(); + + void + Error(nsresult aError); + + void + ContinueRunning(JSObject* aObj); + + void + CompleteOperation(JSObject* aObj); + + RefPtr mParentEntry; + RefPtr mDirectory; + nsTArray mParts; + RefPtr mFileSystem; + + RefPtr mSuccessCallback; + RefPtr mErrorCallback; + + FileSystemDirectoryEntry::GetInternalType mType; +}; + +class FileSystemEntryCallbackHelper +{ +public: + static void + Call(const Optional>& aEntryCallback, + FileSystemEntry* aEntry); +}; + +class ErrorCallbackHelper +{ +public: + static void + Call(nsIGlobalObject* aGlobal, + const Optional>& aErrorCallback, + nsresult aError); +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_CallbackRunnables_h diff --git a/dom/filesystem/compat/FileSystem.cpp b/dom/filesystem/compat/FileSystem.cpp new file mode 100644 index 000000000..b45980fc6 --- /dev/null +++ b/dom/filesystem/compat/FileSystem.cpp @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystem.h" +#include "FileSystemRootDirectoryEntry.h" +#include "mozilla/dom/FileSystemBinding.h" +#include "nsContentUtils.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileSystem, mParent, mRoot) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystem) +NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystem) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystem) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +/* static */ already_AddRefed +FileSystem::Create(nsIGlobalObject* aGlobalObject) + +{ + MOZ_ASSERT(aGlobalObject); + + + nsID id; + nsresult rv = nsContentUtils::GenerateUUIDInPlace(id); + if (NS_WARN_IF(NS_FAILED(rv))) { + return nullptr; + } + + char chars[NSID_LENGTH]; + id.ToProvidedString(chars); + + // Any fileSystem has an unique ID. We use UUID, but our generator produces + // UUID in this format '{' + UUID + '}'. We remove them with these +1 and -2. + nsAutoCString name(Substring(chars + 1, chars + NSID_LENGTH - 2)); + + RefPtr fs = + new FileSystem(aGlobalObject, NS_ConvertUTF8toUTF16(name)); + + return fs.forget(); +} + +FileSystem::FileSystem(nsIGlobalObject* aGlobal, const nsAString& aName) + : mParent(aGlobal) + , mName(aName) +{ + MOZ_ASSERT(aGlobal); +} + +FileSystem::~FileSystem() +{} + +JSObject* +FileSystem::WrapObject(JSContext* aCx, JS::Handle aGivenProto) +{ + return FileSystemBinding::Wrap(aCx, this, aGivenProto); +} + +void +FileSystem::CreateRoot(const Sequence>& aEntries) +{ + MOZ_ASSERT(!mRoot); + mRoot = new FileSystemRootDirectoryEntry(mParent, aEntries, this); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystem.h b/dom/filesystem/compat/FileSystem.h new file mode 100644 index 000000000..43ce2ea38 --- /dev/null +++ b/dom/filesystem/compat/FileSystem.h @@ -0,0 +1,73 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystem_h +#define mozilla_dom_FileSystem_h + +#include "mozilla/Attributes.h" +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" + +class nsIGlobalObject; + +namespace mozilla { +namespace dom { + +class FileSystemDirectoryEntry; +class FileSystemEntry; +class OwningFileOrDirectory; + +class FileSystem final + : public nsISupports + , public nsWrapperCache +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FileSystem) + + static already_AddRefed + Create(nsIGlobalObject* aGlobalObject); + + nsIGlobalObject* + GetParentObject() const + { + return mParent; + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + void + GetName(nsAString& aName) const + { + aName = mName; + } + + FileSystemDirectoryEntry* + Root() const + { + return mRoot; + } + + void + CreateRoot(const Sequence>& aEntries); + +private: + explicit FileSystem(nsIGlobalObject* aGlobalObject, + const nsAString& aName); + ~FileSystem(); + + nsCOMPtr mParent; + RefPtr mRoot; + nsString mName; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystem_h diff --git a/dom/filesystem/compat/FileSystemDirectoryEntry.cpp b/dom/filesystem/compat/FileSystemDirectoryEntry.cpp new file mode 100644 index 000000000..3157ef654 --- /dev/null +++ b/dom/filesystem/compat/FileSystemDirectoryEntry.cpp @@ -0,0 +1,107 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemDirectoryEntry.h" +#include "CallbackRunnables.h" +#include "FileSystemDirectoryReader.h" +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/FileSystemDirectoryEntryBinding.h" +#include "mozilla/dom/FileSystemUtils.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemDirectoryEntry, FileSystemEntry, + mDirectory) + +NS_IMPL_ADDREF_INHERITED(FileSystemDirectoryEntry, FileSystemEntry) +NS_IMPL_RELEASE_INHERITED(FileSystemDirectoryEntry, FileSystemEntry) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemDirectoryEntry) +NS_INTERFACE_MAP_END_INHERITING(FileSystemEntry) + +FileSystemDirectoryEntry::FileSystemDirectoryEntry(nsIGlobalObject* aGlobal, + Directory* aDirectory, + FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem) + : FileSystemEntry(aGlobal, aParentEntry, aFileSystem) + , mDirectory(aDirectory) +{ + MOZ_ASSERT(aGlobal); +} + +FileSystemDirectoryEntry::~FileSystemDirectoryEntry() +{} + +JSObject* +FileSystemDirectoryEntry::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) +{ + return FileSystemDirectoryEntryBinding::Wrap(aCx, this, aGivenProto); +} + +void +FileSystemDirectoryEntry::GetName(nsAString& aName, ErrorResult& aRv) const +{ + MOZ_ASSERT(mDirectory); + mDirectory->GetName(aName, aRv); +} + +void +FileSystemDirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const +{ + MOZ_ASSERT(mDirectory); + mDirectory->GetPath(aPath, aRv); +} + +already_AddRefed +FileSystemDirectoryEntry::CreateReader() +{ + MOZ_ASSERT(mDirectory); + + RefPtr reader = + new FileSystemDirectoryReader(this, Filesystem(), mDirectory); + return reader.forget(); +} + +void +FileSystemDirectoryEntry::GetInternal(const nsAString& aPath, + const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback, + GetInternalType aType) +{ + MOZ_ASSERT(mDirectory); + + if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) { + return; + } + + if (aFlag.mCreate) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_SECURITY_ERR); + return; + } + + nsTArray parts; + if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_NOT_FOUND_ERR); + return; + } + + RefPtr helper = + new GetEntryHelper(this, mDirectory, parts, Filesystem(), + aSuccessCallback.WasPassed() + ? &aSuccessCallback.Value() : nullptr, + aErrorCallback.WasPassed() + ? &aErrorCallback.Value() : nullptr, + aType); + helper->Run(); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemDirectoryEntry.h b/dom/filesystem/compat/FileSystemDirectoryEntry.h new file mode 100644 index 000000000..96dc21831 --- /dev/null +++ b/dom/filesystem/compat/FileSystemDirectoryEntry.h @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemDirectoryEntry_h +#define mozilla_dom_FileSystemDirectoryEntry_h + +#include "mozilla/dom/FileSystemEntry.h" + +namespace mozilla { +namespace dom { + +class Directory; +class FileSystemDirectoryReader; + +class FileSystemDirectoryEntry : public FileSystemEntry +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileSystemDirectoryEntry, + FileSystemEntry) + + FileSystemDirectoryEntry(nsIGlobalObject* aGlobalObject, + Directory* aDirectory, + FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem); + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + virtual bool + IsDirectory() const override + { + return true; + } + + virtual void + GetName(nsAString& aName, ErrorResult& aRv) const override; + + virtual void + GetFullPath(nsAString& aFullPath, ErrorResult& aRv) const override; + + virtual already_AddRefed + CreateReader(); + + void + GetFile(const Optional& aPath, const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback) + { + GetInternal(aPath.WasPassed() ? aPath.Value() : EmptyString(), + aFlag, aSuccessCallback, aErrorCallback, eGetFile); + } + + void + GetDirectory(const Optional& aPath, const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback) + { + GetInternal(aPath.WasPassed() ? aPath.Value() : EmptyString(), + aFlag, aSuccessCallback, aErrorCallback, eGetDirectory); + } + + enum GetInternalType { eGetFile, eGetDirectory }; + + virtual void + GetInternal(const nsAString& aPath, const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback, + GetInternalType aType); + +protected: + virtual ~FileSystemDirectoryEntry(); + +private: + RefPtr mDirectory; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemDirectoryEntry_h diff --git a/dom/filesystem/compat/FileSystemDirectoryReader.cpp b/dom/filesystem/compat/FileSystemDirectoryReader.cpp new file mode 100644 index 000000000..137437378 --- /dev/null +++ b/dom/filesystem/compat/FileSystemDirectoryReader.cpp @@ -0,0 +1,188 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemDirectoryReader.h" +#include "CallbackRunnables.h" +#include "FileSystemFileEntry.h" +#include "mozilla/dom/FileBinding.h" +#include "mozilla/dom/Directory.h" +#include "mozilla/dom/DirectoryBinding.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/PromiseNativeHandler.h" + +namespace mozilla { +namespace dom { + +namespace { + +class PromiseHandler final : public PromiseNativeHandler +{ +public: + NS_DECL_ISUPPORTS + + PromiseHandler(FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem, + FileSystemEntriesCallback* aSuccessCallback, + ErrorCallback* aErrorCallback) + : mParentEntry(aParentEntry) + , mFileSystem(aFileSystem) + , mSuccessCallback(aSuccessCallback) + , mErrorCallback(aErrorCallback) + { + MOZ_ASSERT(aParentEntry); + MOZ_ASSERT(aFileSystem); + MOZ_ASSERT(aSuccessCallback); + } + + virtual void + ResolvedCallback(JSContext* aCx, JS::Handle aValue) override + { + if(NS_WARN_IF(!aValue.isObject())) { + return; + } + + JS::Rooted obj(aCx, &aValue.toObject()); + + uint32_t length; + if (NS_WARN_IF(!JS_GetArrayLength(aCx, obj, &length))) { + return; + } + + Sequence> sequence; + if (NS_WARN_IF(!sequence.SetLength(length, fallible))) { + return; + } + + for (uint32_t i = 0; i < length; ++i) { + JS::Rooted value(aCx); + if (NS_WARN_IF(!JS_GetElement(aCx, obj, i, &value))) { + return; + } + + if(NS_WARN_IF(!value.isObject())) { + return; + } + + JS::Rooted valueObj(aCx, &value.toObject()); + + RefPtr file; + if (NS_SUCCEEDED(UNWRAP_OBJECT(File, valueObj, file))) { + RefPtr entry = + new FileSystemFileEntry(mParentEntry->GetParentObject(), file, + mParentEntry, mFileSystem); + sequence[i] = entry; + continue; + } + + RefPtr directory; + if (NS_WARN_IF(NS_FAILED(UNWRAP_OBJECT(Directory, valueObj, + directory)))) { + return; + } + + RefPtr entry = + new FileSystemDirectoryEntry(mParentEntry->GetParentObject(), directory, + mParentEntry, mFileSystem); + sequence[i] = entry; + } + + mSuccessCallback->HandleEvent(sequence); + } + + virtual void + RejectedCallback(JSContext* aCx, JS::Handle aValue) override + { + if (mErrorCallback) { + RefPtr runnable = + new ErrorCallbackRunnable(mParentEntry->GetParentObject(), + mErrorCallback, + NS_ERROR_DOM_INVALID_STATE_ERR); + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); + } + } + +private: + ~PromiseHandler() {} + + RefPtr mParentEntry; + RefPtr mFileSystem; + RefPtr mSuccessCallback; + RefPtr mErrorCallback; +}; + +NS_IMPL_ISUPPORTS0(PromiseHandler); + +} // anonymous namespace + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileSystemDirectoryReader, mParentEntry, + mDirectory, mFileSystem) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystemDirectoryReader) +NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystemDirectoryReader) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemDirectoryReader) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +FileSystemDirectoryReader::FileSystemDirectoryReader(FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem, + Directory* aDirectory) + : mParentEntry(aParentEntry) + , mFileSystem(aFileSystem) + , mDirectory(aDirectory) + , mAlreadyRead(false) +{ + MOZ_ASSERT(aParentEntry); + MOZ_ASSERT(aFileSystem); +} + +FileSystemDirectoryReader::~FileSystemDirectoryReader() +{} + +JSObject* +FileSystemDirectoryReader::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) +{ + return FileSystemDirectoryReaderBinding::Wrap(aCx, this, aGivenProto); +} + +void +FileSystemDirectoryReader::ReadEntries(FileSystemEntriesCallback& aSuccessCallback, + const Optional>& aErrorCallback, + ErrorResult& aRv) +{ + MOZ_ASSERT(mDirectory); + + if (mAlreadyRead) { + RefPtr runnable = + new EmptyEntriesCallbackRunnable(&aSuccessCallback); + aRv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(!aRv.Failed(), "NS_DispatchToMainThread failed"); + return; + } + + // This object can be used only once. + mAlreadyRead = true; + + ErrorResult rv; + RefPtr promise = mDirectory->GetFilesAndDirectories(rv); + if (NS_WARN_IF(rv.Failed())) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + rv.StealNSResult()); + return; + } + + RefPtr handler = + new PromiseHandler(mParentEntry, mFileSystem, &aSuccessCallback, + aErrorCallback.WasPassed() + ? &aErrorCallback.Value() : nullptr); + promise->AppendNativeHandler(handler); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemDirectoryReader.h b/dom/filesystem/compat/FileSystemDirectoryReader.h new file mode 100644 index 000000000..391a79329 --- /dev/null +++ b/dom/filesystem/compat/FileSystemDirectoryReader.h @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemDirectoryReader_h +#define mozilla_dom_FileSystemDirectoryReader_h + +#include "mozilla/Attributes.h" +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/FileSystemDirectoryEntry.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" + +namespace mozilla { +namespace dom { + +class Directory; +class FileSystem; +class FileSystemEntriesCallback; + +class FileSystemDirectoryReader + : public nsISupports + , public nsWrapperCache +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FileSystemDirectoryReader) + + explicit FileSystemDirectoryReader(FileSystemDirectoryEntry* aDirectoryEntry, + FileSystem* aFileSystem, + Directory* aDirectory); + + nsIGlobalObject* + GetParentObject() const + { + return mParentEntry->GetParentObject(); + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + virtual void + ReadEntries(FileSystemEntriesCallback& aSuccessCallback, + const Optional>& aErrorCallback, + ErrorResult& aRv); + +protected: + virtual ~FileSystemDirectoryReader(); + +private: + RefPtr mParentEntry; + RefPtr mFileSystem; + RefPtr mDirectory; + + bool mAlreadyRead; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemDirectoryReader_h diff --git a/dom/filesystem/compat/FileSystemEntry.cpp b/dom/filesystem/compat/FileSystemEntry.cpp new file mode 100644 index 000000000..638c2c6db --- /dev/null +++ b/dom/filesystem/compat/FileSystemEntry.cpp @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemEntry.h" +#include "FileSystemDirectoryEntry.h" +#include "FileSystemFileEntry.h" +#include "mozilla/dom/FileSystemEntryBinding.h" +#include "mozilla/dom/UnionTypes.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileSystemEntry, mParent, mParentEntry, + mFileSystem) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystemEntry) +NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystemEntry) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemEntry) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +/* static */ already_AddRefed +FileSystemEntry::Create(nsIGlobalObject* aGlobalObject, + const OwningFileOrDirectory& aFileOrDirectory, + FileSystem* aFileSystem) +{ + MOZ_ASSERT(aGlobalObject); + MOZ_ASSERT(aFileSystem); + + RefPtr entry; + if (aFileOrDirectory.IsFile()) { + entry = new FileSystemFileEntry(aGlobalObject, + aFileOrDirectory.GetAsFile(), + nullptr, + aFileSystem); + } else { + MOZ_ASSERT(aFileOrDirectory.IsDirectory()); + entry = new FileSystemDirectoryEntry(aGlobalObject, + aFileOrDirectory.GetAsDirectory(), + nullptr, + aFileSystem); + } + + return entry.forget(); +} + +FileSystemEntry::FileSystemEntry(nsIGlobalObject* aGlobal, + FileSystemEntry* aParentEntry, + FileSystem* aFileSystem) + : mParent(aGlobal) + , mParentEntry(aParentEntry) + , mFileSystem(aFileSystem) +{ + MOZ_ASSERT(aGlobal); + MOZ_ASSERT(aFileSystem); +} + +FileSystemEntry::~FileSystemEntry() +{} + +JSObject* +FileSystemEntry::WrapObject(JSContext* aCx, JS::Handle aGivenProto) +{ + return FileSystemEntryBinding::Wrap(aCx, this, aGivenProto); +} + +void +FileSystemEntry::GetParent(const Optional>& aSuccessCallback, + const Optional>& aErrorCallback) +{ + if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) { + return; + } + + if (mParentEntry) { + FileSystemEntryCallbackHelper::Call(aSuccessCallback, mParentEntry); + return; + } + + FileSystemEntryCallbackHelper::Call(aSuccessCallback, this); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemEntry.h b/dom/filesystem/compat/FileSystemEntry.h new file mode 100644 index 000000000..769fb8f3f --- /dev/null +++ b/dom/filesystem/compat/FileSystemEntry.h @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemEntry_h +#define mozilla_dom_FileSystemEntry_h + +#include "mozilla/Attributes.h" +#include "mozilla/ErrorResult.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/FileSystemBinding.h" +#include "nsCycleCollectionParticipant.h" +#include "nsIGlobalObject.h" +#include "nsWrapperCache.h" + +namespace mozilla { +namespace dom { + +class FileSystem; +class OwningFileOrDirectory; + +class FileSystemEntry + : public nsISupports + , public nsWrapperCache +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FileSystemEntry) + + static already_AddRefed + Create(nsIGlobalObject* aGlobalObject, + const OwningFileOrDirectory& aFileOrDirectory, + FileSystem* aFileSystem); + + nsIGlobalObject* + GetParentObject() const + { + return mParent; + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + virtual bool + IsFile() const + { + return false; + } + + virtual bool + IsDirectory() const + { + return false; + } + + virtual void + GetName(nsAString& aName, ErrorResult& aRv) const = 0; + + virtual void + GetFullPath(nsAString& aFullPath, ErrorResult& aRv) const = 0; + + void + GetParent(const Optional>& aSuccessCallback, + const Optional>& aErrorCallback); + + FileSystem* + Filesystem() const + { + return mFileSystem; + } + +protected: + FileSystemEntry(nsIGlobalObject* aGlobalObject, + FileSystemEntry* aParentEntry, + FileSystem* aFileSystem); + virtual ~FileSystemEntry(); + +private: + nsCOMPtr mParent; + RefPtr mParentEntry; + RefPtr mFileSystem; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemEntry_h diff --git a/dom/filesystem/compat/FileSystemFileEntry.cpp b/dom/filesystem/compat/FileSystemFileEntry.cpp new file mode 100644 index 000000000..6302df06d --- /dev/null +++ b/dom/filesystem/compat/FileSystemFileEntry.cpp @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemFileEntry.h" +#include "CallbackRunnables.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/MultipartBlobImpl.h" +#include "mozilla/dom/FileSystemFileEntryBinding.h" + +namespace mozilla { +namespace dom { + +namespace { + +class FileCallbackRunnable final : public Runnable +{ +public: + FileCallbackRunnable(FileCallback* aCallback, ErrorCallback* aErrorCallback, + File* aFile) + : mCallback(aCallback) + , mErrorCallback(aErrorCallback) + , mFile(aFile) + { + MOZ_ASSERT(aCallback); + MOZ_ASSERT(aFile); + } + + NS_IMETHOD + Run() override + { + // Here we clone the File object. + + nsAutoString name; + mFile->GetName(name); + + nsAutoString type; + mFile->GetType(type); + + nsTArray> blobImpls; + blobImpls.AppendElement(mFile->Impl()); + + ErrorResult rv; + RefPtr blobImpl = + MultipartBlobImpl::Create(Move(blobImpls), name, type, rv); + if (NS_WARN_IF(rv.Failed())) { + if (mErrorCallback) { + RefPtr exception = + DOMException::Create(rv.StealNSResult()); + mErrorCallback->HandleEvent(*exception); + } + + return NS_OK; + } + + RefPtr file = File::Create(mFile->GetParentObject(), blobImpl); + MOZ_ASSERT(file); + + mCallback->HandleEvent(*file); + return NS_OK; + } + +private: + RefPtr mCallback; + RefPtr mErrorCallback; + RefPtr mFile; +}; + +} // anonymous namespace + +NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemFileEntry, FileSystemEntry, mFile) + +NS_IMPL_ADDREF_INHERITED(FileSystemFileEntry, FileSystemEntry) +NS_IMPL_RELEASE_INHERITED(FileSystemFileEntry, FileSystemEntry) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemFileEntry) +NS_INTERFACE_MAP_END_INHERITING(FileSystemEntry) + +FileSystemFileEntry::FileSystemFileEntry(nsIGlobalObject* aGlobal, + File* aFile, + FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem) + : FileSystemEntry(aGlobal, aParentEntry, aFileSystem) + , mFile(aFile) +{ + MOZ_ASSERT(aGlobal); + MOZ_ASSERT(mFile); +} + +FileSystemFileEntry::~FileSystemFileEntry() +{} + +JSObject* +FileSystemFileEntry::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) +{ + return FileSystemFileEntryBinding::Wrap(aCx, this, aGivenProto); +} + +void +FileSystemFileEntry::GetName(nsAString& aName, ErrorResult& aRv) const +{ + mFile->GetName(aName); +} + +void +FileSystemFileEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const +{ + mFile->Impl()->GetDOMPath(aPath); + if (aPath.IsEmpty()) { + // We're under the root directory. webkitRelativePath + // (implemented as GetPath) is for cases when file is selected because its + // ancestor directory is selected. But that is not the case here, so need to + // manually prepend '/'. + nsAutoString name; + mFile->GetName(name); + aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + aPath.Append(name); + } +} + +void +FileSystemFileEntry::GetFile(FileCallback& aSuccessCallback, + const Optional>& aErrorCallback) const +{ + RefPtr runnable = + new FileCallbackRunnable(&aSuccessCallback, + aErrorCallback.WasPassed() + ? &aErrorCallback.Value() : nullptr, + mFile); + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemFileEntry.h b/dom/filesystem/compat/FileSystemFileEntry.h new file mode 100644 index 000000000..06b76e9f8 --- /dev/null +++ b/dom/filesystem/compat/FileSystemFileEntry.h @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemFileEntry_h +#define mozilla_dom_FileSystemFileEntry_h + +#include "mozilla/dom/FileSystemEntry.h" + +namespace mozilla { +namespace dom { + +class File; +class FileCallback; +class FileSystemDirectoryEntry; + +class FileSystemFileEntry final : public FileSystemEntry +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileSystemFileEntry, FileSystemEntry) + + FileSystemFileEntry(nsIGlobalObject* aGlobalObject, File* aFile, + FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem); + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + + virtual bool + IsFile() const override + { + return true; + } + + virtual void + GetName(nsAString& aName, ErrorResult& aRv) const override; + + virtual void + GetFullPath(nsAString& aFullPath, ErrorResult& aRv) const override; + + void + GetFile(FileCallback& aSuccessCallback, + const Optional>& aErrorCallback) const; + +private: + ~FileSystemFileEntry(); + + RefPtr mFile; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemFileEntry_h diff --git a/dom/filesystem/compat/FileSystemRootDirectoryEntry.cpp b/dom/filesystem/compat/FileSystemRootDirectoryEntry.cpp new file mode 100644 index 000000000..68ce62aa2 --- /dev/null +++ b/dom/filesystem/compat/FileSystemRootDirectoryEntry.cpp @@ -0,0 +1,146 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemRootDirectoryEntry.h" +#include "FileSystemRootDirectoryReader.h" +#include "mozilla/dom/FileSystemUtils.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry, + FileSystemDirectoryEntry, mEntries) + +NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry) +NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry) +NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryEntry) + +FileSystemRootDirectoryEntry::FileSystemRootDirectoryEntry(nsIGlobalObject* aGlobal, + const Sequence>& aEntries, + FileSystem* aFileSystem) + : FileSystemDirectoryEntry(aGlobal, nullptr, nullptr, aFileSystem) + , mEntries(aEntries) +{ + MOZ_ASSERT(aGlobal); +} + +FileSystemRootDirectoryEntry::~FileSystemRootDirectoryEntry() +{} + +void +FileSystemRootDirectoryEntry::GetName(nsAString& aName, ErrorResult& aRv) const +{ + aName.Truncate(); +} + +void +FileSystemRootDirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const +{ + aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); +} + +already_AddRefed +FileSystemRootDirectoryEntry::CreateReader() +{ + RefPtr reader = + new FileSystemRootDirectoryReader(this, Filesystem(), mEntries); + return reader.forget(); +} + +void +FileSystemRootDirectoryEntry::GetInternal(const nsAString& aPath, + const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback, + GetInternalType aType) +{ + if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) { + return; + } + + if (aFlag.mCreate) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_SECURITY_ERR); + return; + } + + nsTArray parts; + if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_NOT_FOUND_ERR); + return; + } + + MOZ_ASSERT(!parts.IsEmpty()); + + RefPtr entry; + for (uint32_t i = 0; i < mEntries.Length(); ++i) { + ErrorResult rv; + nsAutoString name; + mEntries[i]->GetName(name, rv); + + if (NS_WARN_IF(rv.Failed())) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + rv.StealNSResult()); + return; + } + + if (name == parts[0]) { + entry = mEntries[i]; + break; + } + } + + // Not found. + if (!entry) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_NOT_FOUND_ERR); + return; + } + + // No subdirectory in the path. + if (parts.Length() == 1) { + if ((entry->IsFile() && aType == eGetDirectory) || + (entry->IsDirectory() && aType == eGetFile)) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_TYPE_MISMATCH_ERR); + return; + } + + if (aSuccessCallback.WasPassed()) { + RefPtr runnable = + new EntryCallbackRunnable(&aSuccessCallback.Value(), entry); + DebugOnly rv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread failed"); + } + return; + } + + // Subdirectories, but this is a file. + if (entry->IsFile()) { + ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback, + NS_ERROR_DOM_NOT_FOUND_ERR); + return; + } + + // Let's recreate a path without the first directory. + nsAutoString path; + for (uint32_t i = 1, len = parts.Length(); i < len; ++i) { + path.Append(parts[i]); + if (i < len - 1) { + path.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL); + } + } + + auto* directoryEntry = static_cast(entry.get()); + directoryEntry->GetInternal(path, aFlag, aSuccessCallback, aErrorCallback, + aType); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemRootDirectoryEntry.h b/dom/filesystem/compat/FileSystemRootDirectoryEntry.h new file mode 100644 index 000000000..28c151ea2 --- /dev/null +++ b/dom/filesystem/compat/FileSystemRootDirectoryEntry.h @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemRootDirectoryEntry_h +#define mozilla_dom_FileSystemRootDirectoryEntry_h + +#include "mozilla/dom/FileSystemDirectoryEntry.h" + +namespace mozilla { +namespace dom { + +class FileSystemRootDirectoryEntry final : public FileSystemDirectoryEntry +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry) + + FileSystemRootDirectoryEntry(nsIGlobalObject* aGlobalObject, + const Sequence>& aEntries, + FileSystem* aFileSystem); + + virtual void + GetName(nsAString& aName, ErrorResult& aRv) const override; + + virtual void + GetFullPath(nsAString& aFullPath, ErrorResult& aRv) const override; + + virtual already_AddRefed + CreateReader() override; + +private: + ~FileSystemRootDirectoryEntry(); + + virtual void + GetInternal(const nsAString& aPath, const FileSystemFlags& aFlag, + const Optional>& aSuccessCallback, + const Optional>& aErrorCallback, + GetInternalType aType) override; + + void + Error(const Optional>& aErrorCallback, + nsresult aError) const; + + Sequence> mEntries; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemRootDirectoryEntry_h diff --git a/dom/filesystem/compat/FileSystemRootDirectoryReader.cpp b/dom/filesystem/compat/FileSystemRootDirectoryReader.cpp new file mode 100644 index 000000000..5b4a41752 --- /dev/null +++ b/dom/filesystem/compat/FileSystemRootDirectoryReader.cpp @@ -0,0 +1,96 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "FileSystemRootDirectoryReader.h" +#include "CallbackRunnables.h" +#include "nsIGlobalObject.h" + +namespace mozilla { +namespace dom { + +namespace { + +class EntriesCallbackRunnable final : public Runnable +{ +public: + EntriesCallbackRunnable(FileSystemEntriesCallback* aCallback, + const Sequence>& aEntries) + : mCallback(aCallback) + , mEntries(aEntries) + { + MOZ_ASSERT(aCallback); + } + + NS_IMETHOD + Run() override + { + Sequence> entries; + for (uint32_t i = 0; i < mEntries.Length(); ++i) { + if (!entries.AppendElement(mEntries[i].forget(), fallible)) { + return NS_ERROR_OUT_OF_MEMORY; + } + } + + mCallback->HandleEvent(entries); + return NS_OK; + } + +private: + RefPtr mCallback; + Sequence> mEntries; +}; + +} // anonymous namespace + +NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryReader, + FileSystemDirectoryReader, mEntries) + +NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryReader, + FileSystemDirectoryReader) +NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryReader, + FileSystemDirectoryReader) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryReader) +NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryReader) + +FileSystemRootDirectoryReader::FileSystemRootDirectoryReader(FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem, + const Sequence>& aEntries) + : FileSystemDirectoryReader(aParentEntry, aFileSystem, nullptr) + , mEntries(aEntries) + , mAlreadyRead(false) +{ + MOZ_ASSERT(aParentEntry); + MOZ_ASSERT(aFileSystem); +} + +FileSystemRootDirectoryReader::~FileSystemRootDirectoryReader() +{} + +void +FileSystemRootDirectoryReader::ReadEntries(FileSystemEntriesCallback& aSuccessCallback, + const Optional>& aErrorCallback, + ErrorResult& aRv) +{ + if (mAlreadyRead) { + RefPtr runnable = + new EmptyEntriesCallbackRunnable(&aSuccessCallback); + aRv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(!aRv.Failed(), "NS_DispatchToMainThread failed"); + return; + } + + // This object can be used only once. + mAlreadyRead = true; + + RefPtr runnable = + new EntriesCallbackRunnable(&aSuccessCallback, mEntries); + aRv = NS_DispatchToMainThread(runnable); + NS_WARNING_ASSERTION(!aRv.Failed(), "NS_DispatchToMainThread failed"); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/filesystem/compat/FileSystemRootDirectoryReader.h b/dom/filesystem/compat/FileSystemRootDirectoryReader.h new file mode 100644 index 000000000..54bca7726 --- /dev/null +++ b/dom/filesystem/compat/FileSystemRootDirectoryReader.h @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_FileSystemRootDirectoryReader_h +#define mozilla_dom_FileSystemRootDirectoryReader_h + +#include "FileSystemDirectoryReader.h" + +namespace mozilla { +namespace dom { + +class FileSystemRootDirectoryReader final : public FileSystemDirectoryReader +{ +public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileSystemRootDirectoryReader, + FileSystemDirectoryReader) + + explicit FileSystemRootDirectoryReader(FileSystemDirectoryEntry* aParentEntry, + FileSystem* aFileSystem, + const Sequence>& aEntries); + + virtual void + ReadEntries(FileSystemEntriesCallback& aSuccessCallback, + const Optional>& aErrorCallback, + ErrorResult& aRv) override; + +private: + ~FileSystemRootDirectoryReader(); + + Sequence> mEntries; + bool mAlreadyRead; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileSystemRootDirectoryReader_h diff --git a/dom/filesystem/compat/moz.build b/dom/filesystem/compat/moz.build new file mode 100644 index 000000000..fc3f9482f --- /dev/null +++ b/dom/filesystem/compat/moz.build @@ -0,0 +1,30 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +TEST_DIRS += ['tests'] + +EXPORTS.mozilla.dom += [ + 'FileSystem.h', + 'FileSystemDirectoryEntry.h', + 'FileSystemDirectoryReader.h', + 'FileSystemEntry.h', + 'FileSystemFileEntry.h', +] + +UNIFIED_SOURCES += [ + 'CallbackRunnables.cpp', + 'FileSystem.cpp', + 'FileSystemDirectoryEntry.cpp', + 'FileSystemDirectoryReader.cpp', + 'FileSystemEntry.cpp', + 'FileSystemFileEntry.cpp', + 'FileSystemRootDirectoryEntry.cpp', + 'FileSystemRootDirectoryReader.cpp', +] + +FINAL_LIBRARY = 'xul' + +include('/ipc/chromium/chromium-config.mozbuild') diff --git a/dom/filesystem/compat/tests/mochitest.ini b/dom/filesystem/compat/tests/mochitest.ini new file mode 100644 index 000000000..2b6eafb55 --- /dev/null +++ b/dom/filesystem/compat/tests/mochitest.ini @@ -0,0 +1,8 @@ +[DEFAULT] +support-files = + script_entries.js + !/dom/html/test/form_submit_server.sjs + +[test_basic.html] +[test_no_dnd.html] +[test_formSubmission.html] diff --git a/dom/filesystem/compat/tests/moz.build b/dom/filesystem/compat/tests/moz.build new file mode 100644 index 000000000..3b13ba431 --- /dev/null +++ b/dom/filesystem/compat/tests/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +MOCHITEST_MANIFESTS += ['mochitest.ini'] diff --git a/dom/filesystem/compat/tests/script_entries.js b/dom/filesystem/compat/tests/script_entries.js new file mode 100644 index 000000000..8083214c9 --- /dev/null +++ b/dom/filesystem/compat/tests/script_entries.js @@ -0,0 +1,47 @@ +var { classes: Cc, interfaces: Ci, utils: Cu } = Components; +Cu.importGlobalProperties(["File", "Directory"]); + +var tmpFile, tmpDir; + +addMessageListener("entries.open", function (e) { + tmpFile = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get('TmpD', Ci.nsIFile) + tmpFile.append('file.txt'); + tmpFile.createUnique(Components.interfaces.nsIFile.FILE_TYPE, 0o600); + + tmpDir = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get('TmpD', Ci.nsIFile) + + tmpDir.append('dir-test'); + tmpDir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + + var file1 = tmpDir.clone(); + file1.append('foo.txt'); + file1.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600); + + var dir1 = tmpDir.clone(); + dir1.append('subdir'); + dir1.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + + var file2 = dir1.clone(); + file2.append('bar..txt'); // Note the double .. + file2.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600); + + var dir2 = dir1.clone(); + dir2.append('subsubdir'); + dir2.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + + sendAsyncMessage("entries.opened", { + data: [ new Directory(tmpDir.path), File.createFromNsIFile(tmpFile) ] + }); +}); + +addMessageListener("entries.delete", function(e) { + tmpFile.remove(true); + tmpDir.remove(true); + sendAsyncMessage("entries.deleted"); +}); diff --git a/dom/filesystem/compat/tests/test_basic.html b/dom/filesystem/compat/tests/test_basic.html new file mode 100644 index 000000000..85a7418d5 --- /dev/null +++ b/dom/filesystem/compat/tests/test_basic.html @@ -0,0 +1,494 @@ + + + + Test for Blink FileSystem API - subset + + + + + + + + + diff --git a/dom/filesystem/compat/tests/test_formSubmission.html b/dom/filesystem/compat/tests/test_formSubmission.html new file mode 100644 index 000000000..0c04b8bf1 --- /dev/null +++ b/dom/filesystem/compat/tests/test_formSubmission.html @@ -0,0 +1,267 @@ + + + + Test for Directory form submission + + + + + + + + +
+
+ + + + diff --git a/dom/filesystem/compat/tests/test_no_dnd.html b/dom/filesystem/compat/tests/test_no_dnd.html new file mode 100644 index 000000000..a78ac108f --- /dev/null +++ b/dom/filesystem/compat/tests/test_no_dnd.html @@ -0,0 +1,85 @@ + + + + Test for Blink FileSystem API - no DND == no webkitEntries + + + + + + + + diff --git a/dom/filesystem/moz.build b/dom/filesystem/moz.build new file mode 100644 index 000000000..6e516c3a7 --- /dev/null +++ b/dom/filesystem/moz.build @@ -0,0 +1,48 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +DIRS += ['compat'] + +TEST_DIRS += ['tests'] + +EXPORTS.mozilla.dom += [ + 'Directory.h', + 'FileSystemBase.h', + 'FileSystemRequestParent.h', + 'FileSystemSecurity.h', + 'FileSystemTaskBase.h', + 'FileSystemUtils.h', + 'GetFilesHelper.h', + 'OSFileSystem.h', +] + +UNIFIED_SOURCES += [ + 'Directory.cpp', + 'FileSystemBase.cpp', + 'FileSystemRequestParent.cpp', + 'FileSystemSecurity.cpp', + 'FileSystemTaskBase.cpp', + 'FileSystemUtils.cpp', + 'GetDirectoryListingTask.cpp', + 'GetFileOrDirectoryTask.cpp', + 'GetFilesHelper.cpp', + 'GetFilesTask.cpp', + 'OSFileSystem.cpp', +] + +FINAL_LIBRARY = 'xul' + +IPDL_SOURCES += [ + 'PFileSystemParams.ipdlh', + 'PFileSystemRequest.ipdl', +] + +include('/ipc/chromium/chromium-config.mozbuild') + +LOCAL_INCLUDES += [ + '/dom/base', + '/dom/workers', +] diff --git a/dom/filesystem/tests/filesystem_commons.js b/dom/filesystem/tests/filesystem_commons.js new file mode 100644 index 000000000..4f7234121 --- /dev/null +++ b/dom/filesystem/tests/filesystem_commons.js @@ -0,0 +1,103 @@ +function createPath(parentDir, dirOrFile) { + return parentDir.path + (parentDir.path == '/' ? '' : '/') + dirOrFile.name; +} + +function createRelativePath(parentDir, dirOrFile) { + let path = createPath(parentDir, dirOrFile); + is(path[0], "/", "The full path should start with '/'"); + return path.substring(1); +} + +function setup_tests(aNext) { + SimpleTest.requestLongerTimeout(2); + SpecialPowers.pushPrefEnv({"set": [["dom.input.dirpicker", true], + ["dom.filesystem.pathcheck.disabled", true], + ["dom.webkitBlink.dirPicker.enabled", true]]}, aNext); +} + +function test_basic(aDirectory, aNext) { + ok(aDirectory, "Directory exists."); + ok(aDirectory instanceof Directory, "We have a directory."); + is(aDirectory.path, '/' + aDirectory.name, "directory.path must be '/'+name"); + aNext(); +} + +function test_getFilesAndDirectories(aDirectory, aRecursive, aNext) { + function checkSubDir(dir) { + return dir.getFilesAndDirectories().then( + function(data) { + for (var i = 0; i < data.length; ++i) { + ok (data[i] instanceof File || data[i] instanceof Directory, "Just Files or Directories"); + if (data[i] instanceof Directory) { + isnot(data[i].name, '/', "Subdirectory should be called with the leafname"); + isnot(data[i].path, '/', "Subdirectory path should be called with the leafname"); + isnot(data[i].path, dir.path, "Subdirectory path should contain the parent path."); + is(data[i].path, createPath(dir, data[i]), "Subdirectory path should be called parentdir.path + '/' + leafname: " + data[i].path); + } + + if (data[i] instanceof File) { + is(data[i].webkitRelativePath, createRelativePath(dir, data[i]), "File.webkitRelativePath should be called: parentdir.path + '/' + file.name: " + data[i].webkitRelativePath); + } + } + } + ); + } + + aDirectory.getFilesAndDirectories().then( + function(data) { + ok(data.length, "We should have some data."); + var promises = []; + for (var i = 0; i < data.length; ++i) { + ok (data[i] instanceof File || data[i] instanceof Directory, "Just Files or Directories: " + data[i].name); + if (data[i] instanceof Directory) { + isnot(data[i].name, '/', "Subdirectory should be called with the leafname"); + is(data[i].path, createPath(aDirectory, data[i]), "Subdirectory path should be called parentdir.path + '/' + leafname: " + data[i].path); + if (aRecursive) { + promises.push(checkSubDir(data[i])); + } + } + + if (data[i] instanceof File) { + is(data[i].webkitRelativePath, createRelativePath(aDirectory, data[i]), "File.webkitRelativePath should be called file.name: " + data[i].webkitRelativePath); + } + } + + return Promise.all(promises); + }, + function() { + ok(false, "Something when wrong"); + } + ).then(aNext); +} + +function test_getFiles(aDirectory, aRecursive, aNext) { + aDirectory.getFiles(aRecursive).then( + function(data) { + for (var i = 0; i < data.length; ++i) { + ok(data[i] instanceof File, "File: " + data[i].name); + is(aDirectory.path[0], '/', "Directory path must start with '/'"); + ok(data[i].webkitRelativePath.indexOf(aDirectory.path.substring(1)) == 0 && + data[i].webkitRelativePath.indexOf('/' + data[i].name) + ('/' + data[i].name).length == data[i].webkitRelativePath.length, + "File.webkitRelativePath should be called dir.path + '/' + file.name: " + data[i].webkitRelativePath); + } + }, + function() { + ok(false, "Something when wrong"); + } + ).then(aNext); +} + +function test_getFiles_recursiveComparison(aDirectory, aNext) { + aDirectory.getFiles(true).then(function(data) { + is(data.length, 2, "Only 2 files for this test."); + ok(data[0].name == 'foo.txt' || data[0].name == 'bar.txt', "First filename matches"); + ok(data[1].name == 'foo.txt' || data[1].name == 'bar.txt', "Second filename matches"); + }).then(function() { + return aDirectory.getFiles(false); + }).then(function(data) { + is(data.length, 1, "Only 1 file for this test."); + ok(data[0].name == 'foo.txt' || data[0].name == 'bar.txt', "First filename matches"); + }).catch(function() { + ok(false, "Something when wrong"); + }).then(aNext); +} diff --git a/dom/filesystem/tests/mochitest.ini b/dom/filesystem/tests/mochitest.ini new file mode 100644 index 000000000..649016886 --- /dev/null +++ b/dom/filesystem/tests/mochitest.ini @@ -0,0 +1,10 @@ +[DEFAULT] +support-files = + filesystem_commons.js + script_fileList.js + worker_basic.js + +[test_basic.html] +[test_webkitdirectory.html] +[test_worker_basic.html] +[test_bug1319088.html] diff --git a/dom/filesystem/tests/moz.build b/dom/filesystem/tests/moz.build new file mode 100644 index 000000000..3b13ba431 --- /dev/null +++ b/dom/filesystem/tests/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +MOCHITEST_MANIFESTS += ['mochitest.ini'] diff --git a/dom/filesystem/tests/script_fileList.js b/dom/filesystem/tests/script_fileList.js new file mode 100644 index 000000000..89fd04cab --- /dev/null +++ b/dom/filesystem/tests/script_fileList.js @@ -0,0 +1,129 @@ +var { classes: Cc, interfaces: Ci, utils: Cu } = Components; +Cu.importGlobalProperties(["File"]); + +function createProfDFile() { + return Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get('ProfD', Ci.nsIFile); +} + +// Creates a parametric arity directory hierarchy as a function of depth. +// Each directory contains one leaf file, and subdirectories of depth [1, depth). +// e.g. for depth 3: +// +// subdir3 +// - file.txt +// - subdir2 +// - file.txt +// - subdir1 +// - file.txt +// - subdir1 +// - file.txt +// +// Returns the parent directory of the subtree. +function createTreeFile(depth, parent) { + if (!parent) { + parent = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get('TmpD', Ci.nsIFile); + parent.append('dir-tree-test'); + parent.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + } + + var nextFile = parent.clone(); + if (depth == 0) { + nextFile.append('file.txt'); + nextFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600); + } else { + nextFile.append('subdir' + depth); + nextFile.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + // Decrement the maximal depth by one for each level of nesting. + for (i = 0; i < depth; i++) { + createTreeFile(i, nextFile); + } + } + + return parent; +} + +function createRootFile() { + var testFile = createProfDFile(); + + // Let's go back to the root of the FileSystem + while (true) { + var parent = testFile.parent; + if (!parent) { + break; + } + + testFile = parent; + } + + return testFile; +} + +function createTestFile() { + var tmpFile = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get('TmpD', Ci.nsIFile) + tmpFile.append('dir-test'); + tmpFile.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + + var file1 = tmpFile.clone(); + file1.append('foo.txt'); + file1.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600); + + var dir = tmpFile.clone(); + dir.append('subdir'); + dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700); + + var file2 = dir.clone(); + file2.append('bar.txt'); + file2.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600); + + return tmpFile; +} + +addMessageListener("dir.open", function (e) { + var testFile; + + switch (e.path) { + case 'ProfD': + // Note that files in the profile directory are not guaranteed to persist- + // see bug 1284742. + testFile = createProfDFile(); + break; + + case 'root': + testFile = createRootFile(); + break; + + case 'test': + testFile = createTestFile(); + break; + + case 'tree': + testFile = createTreeFile(3); + break; + } + + sendAsyncMessage("dir.opened", { + dir: testFile.path, + name: testFile.leafName + }); +}); + +addMessageListener("file.open", function (e) { + var testFile = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIDirectoryService) + .QueryInterface(Ci.nsIProperties) + .get("ProfD", Ci.nsIFile); + testFile.append("prefs.js"); + + sendAsyncMessage("file.opened", { + file: File.createFromNsIFile(testFile) + }); +}); diff --git a/dom/filesystem/tests/test_basic.html b/dom/filesystem/tests/test_basic.html new file mode 100644 index 000000000..1308b9e3c --- /dev/null +++ b/dom/filesystem/tests/test_basic.html @@ -0,0 +1,167 @@ + + + + Test for Directory API + + + + + + + + + diff --git a/dom/filesystem/tests/test_bug1319088.html b/dom/filesystem/tests/test_bug1319088.html new file mode 100644 index 000000000..e0a53223f --- /dev/null +++ b/dom/filesystem/tests/test_bug1319088.html @@ -0,0 +1,66 @@ + + + + Test for bug 1319088 + + + + + + + + + + diff --git a/dom/filesystem/tests/test_webkitdirectory.html b/dom/filesystem/tests/test_webkitdirectory.html new file mode 100644 index 000000000..825f5e8fb --- /dev/null +++ b/dom/filesystem/tests/test_webkitdirectory.html @@ -0,0 +1,191 @@ + + + + Test for webkitdirectory and webkitRelativePath + + + + + + + + + + + + + diff --git a/dom/filesystem/tests/test_worker_basic.html b/dom/filesystem/tests/test_worker_basic.html new file mode 100644 index 000000000..fd36faeeb --- /dev/null +++ b/dom/filesystem/tests/test_worker_basic.html @@ -0,0 +1,72 @@ + + + + Test for Directory API in workers + + + + + + + + + diff --git a/dom/filesystem/tests/worker_basic.js b/dom/filesystem/tests/worker_basic.js new file mode 100644 index 000000000..01df3fbd1 --- /dev/null +++ b/dom/filesystem/tests/worker_basic.js @@ -0,0 +1,41 @@ +importScripts('filesystem_commons.js'); + +function finish() { + postMessage({ type: 'finish' }); +} + +function ok(a, msg) { + postMessage({ type: 'test', test: !!a, message: msg }); +} + +function is(a, b, msg) { + ok(a === b, msg); +} + +function isnot(a, b, msg) { + ok(a != b, msg); +} + +var tests = [ + function() { test_basic(directory, next); }, + function() { test_getFilesAndDirectories(directory, true, next); }, + function() { test_getFiles(directory, false, next); }, + function() { test_getFiles(directory, true, next); }, +]; + +function next() { + if (!tests.length) { + finish(); + return; + } + + var test = tests.shift(); + test(); +} + +var directory; + +onmessage = function(e) { + directory = e.data; + next(); +} -- cgit v1.2.3