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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "FileSystemManagerChild.h"
9 #include "FileSystemAccessHandleChild.h"
10 #include "FileSystemBackgroundRequestHandler.h"
11 #include "FileSystemWritableFileStreamChild.h"
12 #include "mozilla/dom/FileSystemSyncAccessHandle.h"
13 #include "mozilla/dom/FileSystemWritableFileStream.h"
15 namespace mozilla::dom
{
17 void FileSystemManagerChild::SetBackgroundRequestHandler(
18 FileSystemBackgroundRequestHandler
* aBackgroundRequestHandler
) {
19 MOZ_ASSERT(aBackgroundRequestHandler
);
20 MOZ_ASSERT(!mBackgroundRequestHandler
);
22 mBackgroundRequestHandler
= aBackgroundRequestHandler
;
25 void FileSystemManagerChild::CloseAllWritables(
26 std::function
<void()>&& aCallback
) {
27 nsTArray
<RefPtr
<BoolPromise
>> promises
;
28 CloseAllWritablesImpl(promises
);
30 BoolPromise::AllSettled(GetCurrentSerialEventTarget(), promises
)
31 ->Then(GetCurrentSerialEventTarget(), __func__
,
32 [callback
= std::move(aCallback
)](
33 const BoolPromise::AllSettledPromiseType::ResolveOrRejectValue
&
34 /* aValues */) { callback(); });
38 bool FileSystemManagerChild::AllSyncAccessHandlesClosed() const {
39 for (const auto& item
: ManagedPFileSystemAccessHandleChild()) {
40 auto* child
= static_cast<FileSystemAccessHandleChild
*>(item
);
41 auto* handle
= child
->MutableAccessHandlePtr();
43 if (!handle
->IsClosed()) {
51 bool FileSystemManagerChild::AllWritableFileStreamsClosed() const {
52 for (const auto& item
: ManagedPFileSystemWritableFileStreamChild()) {
53 auto* const child
= static_cast<FileSystemWritableFileStreamChild
*>(item
);
54 auto* const handle
= child
->MutableWritableFileStreamPtr();
59 if (!handle
->IsDone()) {
69 void FileSystemManagerChild::Shutdown() {
77 already_AddRefed
<PFileSystemWritableFileStreamChild
>
78 FileSystemManagerChild::AllocPFileSystemWritableFileStreamChild() {
79 return MakeAndAddRef
<FileSystemWritableFileStreamChild
>();
82 ::mozilla::ipc::IPCResult
FileSystemManagerChild::RecvCloseAll(
83 CloseAllResolver
&& aResolver
) {
84 nsTArray
<RefPtr
<BoolPromise
>> promises
;
86 // NOTE: getFile() creates blobs that read the data from the child;
87 // we'll need to abort any reads and resolve this call only when all
90 for (const auto& item
: ManagedPFileSystemAccessHandleChild()) {
91 auto* child
= static_cast<FileSystemAccessHandleChild
*>(item
);
92 auto* handle
= child
->MutableAccessHandlePtr();
94 if (handle
->IsOpen()) {
95 promises
.AppendElement(handle
->BeginClose());
96 } else if (handle
->IsClosing()) {
97 promises
.AppendElement(handle
->OnClose());
101 CloseAllWritablesImpl(promises
);
103 BoolPromise::AllSettled(GetCurrentSerialEventTarget(), promises
)
104 ->Then(GetCurrentSerialEventTarget(), __func__
,
105 [resolver
= std::move(aResolver
)](
106 const BoolPromise::AllSettledPromiseType::ResolveOrRejectValue
&
107 /* aValues */) { resolver(NS_OK
); });
112 void FileSystemManagerChild::ActorDestroy(ActorDestroyReason aWhy
) {
113 if (mBackgroundRequestHandler
) {
114 mBackgroundRequestHandler
->ClearActor();
115 mBackgroundRequestHandler
= nullptr;
120 void FileSystemManagerChild::CloseAllWritablesImpl(T
& aPromises
) {
121 for (const auto& item
: ManagedPFileSystemWritableFileStreamChild()) {
122 auto* const child
= static_cast<FileSystemWritableFileStreamChild
*>(item
);
123 auto* const handle
= child
->MutableWritableFileStreamPtr();
126 if (handle
->IsOpen()) {
127 aPromises
.AppendElement(handle
->BeginAbort());
128 } else if (handle
->IsFinishing()) {
129 aPromises
.AppendElement(handle
->OnDone());
135 } // namespace mozilla::dom