1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_GetFilesHelper_h
8 #define mozilla_dom_GetFilesHelper_h
10 #include "mozilla/Mutex.h"
11 #include "mozilla/RefPtr.h"
12 #include "nsCycleCollectionTraversalCallback.h"
13 #include "nsHashKeys.h"
15 #include "nsTHashSet.h"
16 #include "nsThreadUtils.h"
18 class nsIGlobalObject
;
28 class GetFilesHelperParent
;
29 class OwningFileOrDirectory
;
32 class GetFilesCallback
{
34 NS_INLINE_DECL_REFCOUNTING(GetFilesCallback
);
36 virtual void Callback(nsresult aStatus
,
37 const FallibleTArray
<RefPtr
<BlobImpl
>>& aBlobImpls
) = 0;
40 virtual ~GetFilesCallback() = default;
43 class GetFilesHelperBase
{
45 explicit GetFilesHelperBase(bool aRecursiveFlag
)
46 : mRecursiveFlag(aRecursiveFlag
) {}
48 virtual ~GetFilesHelperBase() = default;
50 virtual bool IsCanceled() { return false; }
52 nsresult
ExploreDirectory(const nsAString
& aDOMPath
, nsIFile
* aFile
);
54 nsresult
AddExploredDirectory(nsIFile
* aDirectory
);
56 bool ShouldFollowSymLink(nsIFile
* aDirectory
);
60 // We populate this array in the I/O thread with the BlobImpl.
61 FallibleTArray
<RefPtr
<BlobImpl
>> mTargetBlobImplArray
;
62 nsTHashSet
<nsString
> mExploredDirectories
;
65 // Retrieving the list of files can be very time/IO consuming. We use this
66 // helper class to do it just once.
67 class GetFilesHelper
: public Runnable
, public GetFilesHelperBase
{
68 friend class GetFilesHelperParent
;
71 static already_AddRefed
<GetFilesHelper
> Create(
72 const nsTArray
<OwningFileOrDirectory
>& aFilesOrDirectory
,
73 bool aRecursiveFlag
, ErrorResult
& aRv
);
75 void AddPromise(Promise
* aPromise
);
77 void AddCallback(GetFilesCallback
* aCallback
);
81 void Traverse(nsCycleCollectionTraversalCallback
& cb
);
84 explicit GetFilesHelper(bool aRecursiveFlag
);
86 virtual ~GetFilesHelper();
88 void SetDirectoryPath(const nsAString
& aDirectoryPath
) {
89 mDirectoryPath
= aDirectoryPath
;
92 virtual bool IsCanceled() override
{
93 MutexAutoLock
lock(mMutex
);
97 virtual void Work(ErrorResult
& aRv
);
99 virtual void Cancel(){};
106 void OperationCompleted();
108 void ResolveOrRejectPromise(Promise
* aPromise
);
110 void RunCallback(GetFilesCallback
* aCallback
);
112 bool mListingCompleted
;
113 nsString mDirectoryPath
;
115 // Error code to propagate.
116 nsresult mErrorResult
;
118 nsTArray
<RefPtr
<Promise
>> mPromises
;
119 nsTArray
<RefPtr
<GetFilesCallback
>> mCallbacks
;
121 Mutex mMutex MOZ_UNANNOTATED
;
123 // This variable is protected by mutex.
127 class GetFilesHelperChild final
: public GetFilesHelper
{
129 explicit GetFilesHelperChild(bool aRecursiveFlag
)
130 : GetFilesHelper(aRecursiveFlag
), mPendingOperation(false) {}
132 virtual void Work(ErrorResult
& aRv
) override
;
134 virtual void Cancel() override
;
136 bool AppendBlobImpl(BlobImpl
* aBlobImpl
);
138 void Finished(nsresult aResult
);
142 bool mPendingOperation
;
145 class GetFilesHelperParentCallback
;
147 class GetFilesHelperParent final
: public GetFilesHelper
{
148 friend class GetFilesHelperParentCallback
;
151 static already_AddRefed
<GetFilesHelperParent
> Create(
152 const nsID
& aUUID
, const nsAString
& aDirectoryPath
, bool aRecursiveFlag
,
153 ContentParent
* aContentParent
, ErrorResult
& aRv
);
156 GetFilesHelperParent(const nsID
& aUUID
, ContentParent
* aContentParent
,
157 bool aRecursiveFlag
);
159 ~GetFilesHelperParent();
161 RefPtr
<ContentParent
> mContentParent
;
166 } // namespace mozilla
168 #endif // mozilla_dom_GetFilesHelper_h