зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1274959 - Support symlinks in Directory API - part 2 - Unify GetFilesHelper and GetFilesTaskParent, r=smaug
This commit is contained in:
Родитель
ce2d912270
Коммит
805b8ba685
|
@ -72,8 +72,8 @@ GetFilesHelper::Create(nsIGlobalObject* aGlobal,
|
|||
}
|
||||
|
||||
GetFilesHelper::GetFilesHelper(nsIGlobalObject* aGlobal, bool aRecursiveFlag)
|
||||
: mGlobal(aGlobal)
|
||||
, mRecursiveFlag(aRecursiveFlag)
|
||||
: GetFilesHelperBase(aRecursiveFlag)
|
||||
, mGlobal(aGlobal)
|
||||
, mListingCompleted(false)
|
||||
, mErrorResult(NS_OK)
|
||||
, mMutex("GetFilesHelper::mMutex")
|
||||
|
@ -259,7 +259,7 @@ GetFilesHelper::RunMainThread()
|
|||
}
|
||||
|
||||
nsresult
|
||||
GetFilesHelper::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
|
||||
GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
|
||||
{
|
||||
MOZ_ASSERT(!NS_IsMainThread());
|
||||
MOZ_ASSERT(aFile);
|
||||
|
|
|
@ -36,9 +36,39 @@ 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);
|
||||
|
||||
bool mRecursiveFlag;
|
||||
|
||||
// We populate this array in the I/O thread with the paths of the Files that
|
||||
// we want to send as result to the promise objects.
|
||||
struct FileData {
|
||||
nsString mDomPath;
|
||||
nsString mRealPath;
|
||||
};
|
||||
FallibleTArray<FileData> mTargetPathArray;
|
||||
};
|
||||
|
||||
// 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;
|
||||
|
||||
|
@ -69,8 +99,8 @@ protected:
|
|||
mDirectoryPath = aDirectoryPath;
|
||||
}
|
||||
|
||||
bool
|
||||
IsCanceled()
|
||||
virtual bool
|
||||
IsCanceled() override
|
||||
{
|
||||
MutexAutoLock lock(mMutex);
|
||||
return mCanceled;
|
||||
|
@ -94,8 +124,6 @@ protected:
|
|||
void
|
||||
OperationCompleted();
|
||||
|
||||
nsresult
|
||||
ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile);
|
||||
void
|
||||
ResolveOrRejectPromise(Promise* aPromise);
|
||||
|
||||
|
@ -104,18 +132,9 @@ protected:
|
|||
|
||||
nsCOMPtr<nsIGlobalObject> mGlobal;
|
||||
|
||||
bool mRecursiveFlag;
|
||||
bool mListingCompleted;
|
||||
nsString mDirectoryPath;
|
||||
|
||||
// We populate this array in the I/O thread with the paths of the Files that
|
||||
// we want to send as result to the promise objects.
|
||||
struct FileData {
|
||||
nsString mDomPath;
|
||||
nsString mRealPath;
|
||||
};
|
||||
FallibleTArray<FileData> mTargetPathArray;
|
||||
|
||||
// This is the real File sequence that we expose via Promises.
|
||||
Sequence<RefPtr<File>> mFiles;
|
||||
|
||||
|
|
|
@ -223,8 +223,8 @@ GetFilesTaskParent::GetFilesTaskParent(FileSystemBase* aFileSystem,
|
|||
const FileSystemGetFilesParams& aParam,
|
||||
FileSystemRequestParent* aParent)
|
||||
: FileSystemTaskParentBase(aFileSystem, aParam, aParent)
|
||||
, GetFilesHelperBase(aParam.recursiveFlag())
|
||||
, mDirectoryDOMPath(aParam.domPath())
|
||||
, mRecursiveFlag(aParam.recursiveFlag())
|
||||
{
|
||||
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
|
||||
AssertIsOnBackgroundThread();
|
||||
|
@ -239,16 +239,16 @@ GetFilesTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const
|
|||
InfallibleTArray<PBlobParent*> blobs;
|
||||
|
||||
FallibleTArray<FileSystemFileResponse> inputs;
|
||||
if (!inputs.SetLength(mTargetData.Length(), mozilla::fallible_t())) {
|
||||
if (!inputs.SetLength(mTargetPathArray.Length(), mozilla::fallible_t())) {
|
||||
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
||||
FileSystemFilesResponse response;
|
||||
return response;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < mTargetData.Length(); i++) {
|
||||
for (unsigned i = 0; i < mTargetPathArray.Length(); i++) {
|
||||
FileSystemFileResponse fileData;
|
||||
fileData.realPath() = mTargetData[i].mRealPath;
|
||||
fileData.domPath() = mTargetData[i].mDOMPath;
|
||||
fileData.realPath() = mTargetPathArray[i].mRealPath;
|
||||
fileData.domPath() = mTargetPathArray[i].mDomPath;
|
||||
inputs[i] = fileData;
|
||||
}
|
||||
|
||||
|
@ -278,25 +278,8 @@ GetFilesTaskParent::IOWork()
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
// Get isDirectory.
|
||||
rv = ExploreDirectory(mDirectoryDOMPath, mTargetPath);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
GetFilesTaskParent::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aPath)
|
||||
{
|
||||
MOZ_ASSERT(XRE_IsParentProcess(),
|
||||
"Only call from parent process!");
|
||||
MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
|
||||
MOZ_ASSERT(aPath);
|
||||
|
||||
bool isDir;
|
||||
nsresult rv = aPath->IsDirectory(&isDir);
|
||||
rv = mTargetPath->IsDirectory(&isDir);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
@ -305,80 +288,12 @@ GetFilesTaskParent::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aPath)
|
|||
return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsISimpleEnumerator> entries;
|
||||
rv = aPath->GetDirectoryEntries(getter_AddRefs(entries));
|
||||
// Get isDirectory.
|
||||
rv = ExploreDirectory(mDirectoryDOMPath, mTargetPath);
|
||||
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<nsISupports> supp;
|
||||
if (NS_WARN_IF(NS_FAILED(entries->GetNext(getter_AddRefs(supp))))) {
|
||||
break;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIFile> currFile = do_QueryInterface(supp);
|
||||
MOZ_ASSERT(currFile);
|
||||
|
||||
bool isLink, isSpecial, isFile;
|
||||
if (NS_WARN_IF(NS_FAILED(currFile->IsSymlink(&isLink)) ||
|
||||
NS_FAILED(currFile->IsSpecial(&isSpecial))) ||
|
||||
isLink || isSpecial) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (NS_WARN_IF(NS_FAILED(currFile->IsFile(&isFile)) ||
|
||||
NS_FAILED(currFile->IsDirectory(&isDir))) ||
|
||||
!(isFile || isDir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nsAutoString domPath;
|
||||
domPath.Assign(aDOMPath);
|
||||
|
||||
// This is specific for unix root filesystem.
|
||||
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) {
|
||||
FileData data;
|
||||
data.mDOMPath.Append(domPath);
|
||||
|
||||
if (NS_WARN_IF(NS_FAILED(currFile->GetPath(data.mRealPath)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!mTargetData.AppendElement(data, 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "mozilla/dom/Directory.h"
|
||||
#include "mozilla/dom/FileSystemTaskBase.h"
|
||||
#include "mozilla/dom/GetFilesHelper.h"
|
||||
#include "mozilla/ErrorResult.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
@ -68,6 +69,7 @@ private:
|
|||
};
|
||||
|
||||
class GetFilesTaskParent final : public FileSystemTaskParentBase
|
||||
, public GetFilesHelperBase
|
||||
{
|
||||
public:
|
||||
static already_AddRefed<GetFilesTaskParent>
|
||||
|
@ -90,20 +92,8 @@ private:
|
|||
virtual nsresult
|
||||
IOWork() override;
|
||||
|
||||
nsresult
|
||||
ExploreDirectory(const nsAString& aDOMPath, nsIFile* aPath);
|
||||
|
||||
nsString mDirectoryDOMPath;
|
||||
nsCOMPtr<nsIFile> mTargetPath;
|
||||
bool mRecursiveFlag;
|
||||
|
||||
// We store the fullpath and the dom path of Files.
|
||||
struct FileData {
|
||||
nsString mRealPath;
|
||||
nsString mDOMPath;
|
||||
};
|
||||
|
||||
FallibleTArray<FileData> mTargetData;
|
||||
};
|
||||
|
||||
} // namespace dom
|
||||
|
|
Загрузка…
Ссылка в новой задаче