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
|
/* -*- 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/. */
#include "FileSystemBackgroundRequestHandler.h"
#include "fs/FileSystemChildFactory.h"
#include "mozilla/dom/FileSystemManagerChild.h"
#include "mozilla/dom/PFileSystemManager.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/ipc/PBackgroundChild.h"
namespace mozilla::dom {
FileSystemBackgroundRequestHandler::FileSystemBackgroundRequestHandler(
fs::FileSystemChildFactory* aChildFactory)
: mChildFactory(aChildFactory), mCreatingFileSystemManagerChild(false) {}
FileSystemBackgroundRequestHandler::FileSystemBackgroundRequestHandler(
RefPtr<FileSystemManagerChild> aFileSystemManagerChild)
: mFileSystemManagerChild(std::move(aFileSystemManagerChild)),
mCreatingFileSystemManagerChild(false) {}
FileSystemBackgroundRequestHandler::FileSystemBackgroundRequestHandler()
: FileSystemBackgroundRequestHandler(new fs::FileSystemChildFactory()) {}
FileSystemBackgroundRequestHandler::~FileSystemBackgroundRequestHandler() =
default;
void FileSystemBackgroundRequestHandler::ClearActor() {
MOZ_ASSERT(mFileSystemManagerChild);
mFileSystemManagerChild = nullptr;
}
void FileSystemBackgroundRequestHandler::Shutdown() {
mShutdown.Flip();
if (mFileSystemManagerChild) {
MOZ_ASSERT(!mCreatingFileSystemManagerChild);
mFileSystemManagerChild->Shutdown();
mFileSystemManagerChild = nullptr;
return;
}
if (mCreatingFileSystemManagerChild) {
MOZ_ASSERT(!mFileSystemManagerChild);
mCreateFileSystemManagerParentPromiseRequestHolder.Disconnect();
mCreatingFileSystemManagerChild = false;
// We must either resolve/reject the promise or steal the internal promise
// before the holder is destroyed. The former isn't possible during
// shutdown.
Unused << mCreateFileSystemManagerChildPromiseHolder.Steal();
}
}
const RefPtr<FileSystemManagerChild>&
FileSystemBackgroundRequestHandler::FileSystemManagerChildStrongRef() const {
return mFileSystemManagerChild;
}
RefPtr<BoolPromise>
FileSystemBackgroundRequestHandler::CreateFileSystemManagerChild(
const mozilla::ipc::PrincipalInfo& aPrincipalInfo) {
MOZ_ASSERT(!mFileSystemManagerChild);
MOZ_ASSERT(!mShutdown);
using mozilla::ipc::BackgroundChild;
using mozilla::ipc::Endpoint;
using mozilla::ipc::PBackgroundChild;
if (!mCreatingFileSystemManagerChild) {
PBackgroundChild* backgroundChild =
BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!backgroundChild)) {
return BoolPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
}
// Create a new IPC connection
Endpoint<PFileSystemManagerParent> parentEndpoint;
Endpoint<PFileSystemManagerChild> childEndpoint;
MOZ_ALWAYS_SUCCEEDS(
PFileSystemManager::CreateEndpoints(&parentEndpoint, &childEndpoint));
RefPtr<FileSystemManagerChild> child = mChildFactory->Create();
if (!childEndpoint.Bind(child)) {
return BoolPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
}
mCreatingFileSystemManagerChild = true;
backgroundChild
->SendCreateFileSystemManagerParent(aPrincipalInfo,
std::move(parentEndpoint))
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr<FileSystemBackgroundRequestHandler>(this),
child](nsresult rv) {
self->mCreateFileSystemManagerParentPromiseRequestHolder
.Complete();
self->mCreatingFileSystemManagerChild = false;
if (NS_FAILED(rv)) {
self->mCreateFileSystemManagerChildPromiseHolder.RejectIfExists(
rv, __func__);
} else {
self->mFileSystemManagerChild = child;
self->mFileSystemManagerChild->SetBackgroundRequestHandler(
self);
self->mCreateFileSystemManagerChildPromiseHolder
.ResolveIfExists(true, __func__);
}
},
[self = RefPtr<FileSystemBackgroundRequestHandler>(this)](
const mozilla::ipc::ResponseRejectReason&) {
self->mCreateFileSystemManagerParentPromiseRequestHolder
.Complete();
self->mCreatingFileSystemManagerChild = false;
self->mCreateFileSystemManagerChildPromiseHolder.RejectIfExists(
NS_ERROR_FAILURE, __func__);
})
->Track(mCreateFileSystemManagerParentPromiseRequestHolder);
}
return mCreateFileSystemManagerChildPromiseHolder.Ensure(__func__);
}
} // namespace mozilla::dom
|