2016-07-14 10:03:10 +03:00
|
|
|
/* -*- 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"
|
2016-07-23 11:35:26 +03:00
|
|
|
#include "nsTHashtable.h"
|
2016-07-14 10:03:10 +03:00
|
|
|
|
|
|
|
class nsIGlobalObject;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-07-14 10:04:21 +03:00
|
|
|
class BlobImpl;
|
|
|
|
class ContentParent;
|
2016-07-14 10:03:10 +03:00
|
|
|
class File;
|
|
|
|
class GetFilesHelperParent;
|
|
|
|
class OwningFileOrDirectory;
|
|
|
|
class Promise;
|
|
|
|
|
|
|
|
class GetFilesCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(GetFilesCallback);
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
Callback(nsresult aStatus, const Sequence<RefPtr<File>>& aFiles) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~GetFilesCallback() {}
|
|
|
|
};
|
|
|
|
|
2016-07-23 11:35:06 +03:00
|
|
|
class GetFilesHelperBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
explicit GetFilesHelperBase(bool aRecursiveFlag)
|
|
|
|
: mRecursiveFlag(aRecursiveFlag)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~GetFilesHelperBase() {}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
IsCanceled()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile);
|
|
|
|
|
2016-07-23 11:35:26 +03:00
|
|
|
nsresult
|
|
|
|
AddExploredDirectory(nsIFile* aDirectory);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldFollowSymLink(nsIFile* aDirectory);
|
|
|
|
|
2016-07-23 11:35:06 +03:00
|
|
|
bool mRecursiveFlag;
|
|
|
|
|
2017-02-01 09:33:18 +03:00
|
|
|
// We populate this array in the I/O thread with the BlobImpl.
|
|
|
|
FallibleTArray<RefPtr<BlobImpl>> mTargetBlobImplArray;
|
2017-01-26 18:48:52 +03:00
|
|
|
nsTHashtable<nsStringHashKey> mExploredDirectories;
|
2016-07-23 11:35:06 +03:00
|
|
|
};
|
|
|
|
|
2016-07-14 10:03:10 +03:00
|
|
|
// 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
|
2016-07-23 11:35:06 +03:00
|
|
|
, public GetFilesHelperBase
|
2016-07-14 10:03:10 +03:00
|
|
|
{
|
|
|
|
friend class GetFilesHelperParent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static already_AddRefed<GetFilesHelper>
|
|
|
|
Create(nsIGlobalObject* aGlobal,
|
|
|
|
const nsTArray<OwningFileOrDirectory>& 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);
|
|
|
|
|
2016-09-11 10:56:54 +03:00
|
|
|
virtual ~GetFilesHelper();
|
2016-07-14 10:03:10 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
SetDirectoryPath(const nsAString& aDirectoryPath)
|
|
|
|
{
|
|
|
|
mDirectoryPath = aDirectoryPath;
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:35:06 +03:00
|
|
|
virtual bool
|
|
|
|
IsCanceled() override
|
2016-07-14 10:03:10 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mCanceled;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:04:21 +03:00
|
|
|
virtual void
|
|
|
|
Work(ErrorResult& aRv);
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
Cancel() {};
|
|
|
|
|
2016-07-14 10:03:10 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override;
|
|
|
|
|
|
|
|
void
|
|
|
|
RunIO();
|
|
|
|
|
|
|
|
void
|
|
|
|
RunMainThread();
|
|
|
|
|
2016-07-14 10:04:21 +03:00
|
|
|
void
|
|
|
|
OperationCompleted();
|
|
|
|
|
2016-07-14 10:03:10 +03:00
|
|
|
void
|
|
|
|
ResolveOrRejectPromise(Promise* aPromise);
|
|
|
|
|
|
|
|
void
|
|
|
|
RunCallback(GetFilesCallback* aCallback);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
|
|
|
|
bool mListingCompleted;
|
|
|
|
nsString mDirectoryPath;
|
|
|
|
|
|
|
|
// This is the real File sequence that we expose via Promises.
|
|
|
|
Sequence<RefPtr<File>> mFiles;
|
|
|
|
|
|
|
|
// Error code to propagate.
|
|
|
|
nsresult mErrorResult;
|
|
|
|
|
|
|
|
nsTArray<RefPtr<Promise>> mPromises;
|
|
|
|
nsTArray<RefPtr<GetFilesCallback>> mCallbacks;
|
|
|
|
|
|
|
|
Mutex mMutex;
|
|
|
|
|
|
|
|
// This variable is protected by mutex.
|
|
|
|
bool mCanceled;
|
|
|
|
};
|
|
|
|
|
2016-07-14 10:04:21 +03:00
|
|
|
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<GetFilesHelperParent>
|
|
|
|
Create(const nsID& aUUID, const nsAString& aDirectoryPath,
|
|
|
|
bool aRecursiveFlag, ContentParent* aContentParent, ErrorResult& aRv);
|
|
|
|
|
|
|
|
private:
|
|
|
|
GetFilesHelperParent(const nsID& aUUID, ContentParent* aContentParent,
|
|
|
|
bool aRecursiveFlag);
|
|
|
|
|
2016-09-14 09:34:22 +03:00
|
|
|
~GetFilesHelperParent();
|
|
|
|
|
2016-07-14 10:04:21 +03:00
|
|
|
RefPtr<ContentParent> mContentParent;
|
|
|
|
nsID mUUID;
|
|
|
|
};
|
|
|
|
|
2016-07-14 10:03:10 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|
|
|
|
|
|
|
|
#endif // mozilla_dom_GetFilesHelper_h
|