Bug 1824753 [wpt PR 39216] - [FLEDGE] Add WPT test that FLEDGE is not allowed in...
[gecko.git] / dom / filesystem / GetFilesHelper.h
blobdef05767192f7a0512f43d3d0f4fcca61cc652be
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"
14 #include "nsTArray.h"
15 #include "nsTHashSet.h"
16 #include "nsThreadUtils.h"
18 class nsIGlobalObject;
20 namespace mozilla {
21 class ErrorResult;
23 namespace dom {
25 class BlobImpl;
26 class ContentParent;
27 class File;
28 class GetFilesHelperParent;
29 class OwningFileOrDirectory;
30 class Promise;
32 class GetFilesCallback {
33 public:
34 NS_INLINE_DECL_REFCOUNTING(GetFilesCallback);
36 virtual void Callback(nsresult aStatus,
37 const FallibleTArray<RefPtr<BlobImpl>>& aBlobImpls) = 0;
39 protected:
40 virtual ~GetFilesCallback() = default;
43 class GetFilesHelperBase {
44 protected:
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);
58 bool mRecursiveFlag;
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;
70 public:
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);
79 // CC methods
80 void Unlink();
81 void Traverse(nsCycleCollectionTraversalCallback& cb);
83 protected:
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);
94 return mCanceled;
97 virtual void Work(ErrorResult& aRv);
99 virtual void Cancel(){};
101 NS_IMETHOD
102 Run() override;
104 void RunIO();
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.
124 bool mCanceled;
127 class GetFilesHelperChild final : public GetFilesHelper {
128 public:
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);
140 private:
141 nsID mUUID;
142 bool mPendingOperation;
145 class GetFilesHelperParentCallback;
147 class GetFilesHelperParent final : public GetFilesHelper {
148 friend class GetFilesHelperParentCallback;
150 public:
151 static already_AddRefed<GetFilesHelperParent> Create(
152 const nsID& aUUID, const nsAString& aDirectoryPath, bool aRecursiveFlag,
153 ContentParent* aContentParent, ErrorResult& aRv);
155 private:
156 GetFilesHelperParent(const nsID& aUUID, ContentParent* aContentParent,
157 bool aRecursiveFlag);
159 ~GetFilesHelperParent();
161 RefPtr<ContentParent> mContentParent;
162 nsID mUUID;
165 } // namespace dom
166 } // namespace mozilla
168 #endif // mozilla_dom_GetFilesHelper_h