1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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/. */
#ifndef mozilla_dom_GetFilesHelper_h
#define mozilla_dom_GetFilesHelper_h
#include "mozilla/MozPromise.h"
#include "mozilla/Mutex.h"
#include "mozilla/RefPtr.h"
#include "nsCycleCollectionTraversalCallback.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
class nsIGlobalObject;
namespace mozilla {
class ErrorResult;
namespace dom {
class BlobImpl;
class ContentParent;
class File;
class GetFilesHelperParent;
class OwningFileOrDirectory;
class Promise;
class GetFilesCallback {
public:
NS_INLINE_DECL_REFCOUNTING(GetFilesCallback);
// This is called once per directory scan and is passed an array of all
// of the files found.
virtual void Callback(nsresult aStatus,
const FallibleTArray<RefPtr<BlobImpl>>& aBlobImpls) = 0;
protected:
virtual ~GetFilesCallback() = default;
};
class GetFilesHelperBase {
protected:
explicit GetFilesHelperBase(bool aRecursiveFlag)
: mRecursiveFlag(aRecursiveFlag) {}
virtual ~GetFilesHelperBase() = default;
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 BlobImpl.
FallibleTArray<RefPtr<BlobImpl>> mTargetBlobImplArray;
};
// 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;
class ReleaseRunnable;
public:
static already_AddRefed<GetFilesHelper> Create(
const nsTArray<OwningFileOrDirectory>& aFilesOrDirectory,
bool aRecursiveFlag, ErrorResult& aRv);
void AddPromise(Promise* aPromise);
void AddCallback(GetFilesCallback* aCallback);
using MozPromiseType =
MozPromise<nsTArray<RefPtr<File>>, nsresult, true>::Private;
void AddMozPromise(MozPromiseType* aPromise, nsIGlobalObject* aGlobal);
// CC methods
void Unlink();
void Traverse(nsCycleCollectionTraversalCallback& cb);
protected:
explicit GetFilesHelper(bool aRecursiveFlag);
virtual ~GetFilesHelper();
void SetDirectoryPaths(nsTArray<nsString>&& aDirectoryPaths) {
mDirectoryPaths = std::move(aDirectoryPaths);
}
virtual bool IsCanceled() override {
MutexAutoLock lock(mMutex);
return mCanceled;
}
virtual void Work(ErrorResult& aRv);
virtual void Cancel() {};
NS_IMETHOD
Run() override;
void RunIO();
void OperationCompleted();
struct MozPromiseAndGlobal {
RefPtr<MozPromiseType> mMozPromise;
RefPtr<nsIGlobalObject> mGlobal;
};
class PromiseAdapter {
public:
explicit PromiseAdapter(MozPromiseAndGlobal&& aMozPromise);
explicit PromiseAdapter(Promise* aDomPromise);
~PromiseAdapter();
void Clear();
void Traverse(nsCycleCollectionTraversalCallback& cb);
nsIGlobalObject* GetGlobalObject();
void Resolve(nsTArray<RefPtr<File>>&& aFiles);
void Reject(nsresult aError);
private:
using PromiseVariant = Variant<RefPtr<Promise>, MozPromiseAndGlobal>;
PromiseVariant mPromise;
};
void AddPromiseInternal(PromiseAdapter&& aPromise);
void ResolveOrRejectPromise(PromiseAdapter&& aPromise);
void RunCallback(GetFilesCallback* aCallback);
bool mListingCompleted;
nsTArray<nsString> mDirectoryPaths;
// Error code to propagate.
nsresult mErrorResult;
nsTArray<PromiseAdapter> mPromises;
nsTArray<RefPtr<GetFilesCallback>> mCallbacks;
Mutex mMutex MOZ_UNANNOTATED;
// This variable is protected by mutex.
bool mCanceled;
};
class GetFilesHelperChild final : public GetFilesHelper {
public:
explicit GetFilesHelperChild(bool aRecursiveFlag)
: GetFilesHelper(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, nsTArray<nsString>&& aDirectoryPaths,
bool aRecursiveFlag, ContentParent* aContentParent, ErrorResult& aRv);
private:
GetFilesHelperParent(const nsID& aUUID, ContentParent* aContentParent,
bool aRecursiveFlag);
~GetFilesHelperParent();
RefPtr<ContentParent> mContentParent;
nsID mUUID;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_GetFilesHelper_h
|