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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/* -*- 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 "FetchEventOpProxyChild.h"
#include <utility>
#include "mozilla/dom/FetchTypes.h"
#include "mozilla/dom/ServiceWorkerOpPromise.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsThreadUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/InternalRequest.h"
#include "mozilla/dom/InternalResponse.h"
#include "mozilla/dom/RemoteWorkerChild.h"
#include "mozilla/dom/RemoteWorkerService.h"
#include "mozilla/dom/ServiceWorkerOp.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/IPCStreamUtils.h"
namespace mozilla {
using namespace ipc;
namespace dom {
namespace {
nsresult GetIPCSynthesizeResponseArgs(
ChildToParentSynthesizeResponseArgs* aIPCArgs,
SynthesizeResponseArgs&& aArgs) {
MOZ_ASSERT(RemoteWorkerService::Thread()->IsOnCurrentThread());
auto [internalResponse, closure, timeStamps] = std::move(aArgs);
aIPCArgs->closure() = std::move(closure);
aIPCArgs->timeStamps() = std::move(timeStamps);
PBackgroundChild* bgChild = BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!bgChild)) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
internalResponse->ToChildToParentInternalResponse(
&aIPCArgs->internalResponse(), bgChild);
return NS_OK;
}
} // anonymous namespace
void FetchEventOpProxyChild::Initialize(
const ParentToChildServiceWorkerFetchEventOpArgs& aArgs) {
MOZ_ASSERT(RemoteWorkerService::Thread()->IsOnCurrentThread());
MOZ_ASSERT(!mOp);
mInternalRequest =
MakeSafeRefPtr<InternalRequest>(aArgs.common().internalRequest());
if (aArgs.common().preloadNavigation()) {
// We use synchronous task dispatch here to make sure that if the preload
// response arrived before we dispatch the fetch event, then the JS preload
// response promise will get resolved immediately.
mPreloadResponseAvailablePromise =
MakeRefPtr<FetchEventPreloadResponseAvailablePromise::Private>(
__func__);
mPreloadResponseAvailablePromise->UseSynchronousTaskDispatch(__func__);
if (aArgs.preloadResponse().isSome()) {
mPreloadResponseAvailablePromise->Resolve(
InternalResponse::FromIPC(aArgs.preloadResponse().ref()), __func__);
}
mPreloadResponseTimingPromise =
MakeRefPtr<FetchEventPreloadResponseTimingPromise::Private>(__func__);
mPreloadResponseTimingPromise->UseSynchronousTaskDispatch(__func__);
if (aArgs.preloadResponseTiming().isSome()) {
mPreloadResponseTimingPromise->Resolve(
aArgs.preloadResponseTiming().ref(), __func__);
}
mPreloadResponseEndPromise =
MakeRefPtr<FetchEventPreloadResponseEndPromise::Private>(__func__);
mPreloadResponseEndPromise->UseSynchronousTaskDispatch(__func__);
if (aArgs.preloadResponseEndArgs().isSome()) {
mPreloadResponseEndPromise->Resolve(aArgs.preloadResponseEndArgs().ref(),
__func__);
}
}
RemoteWorkerChild* manager = static_cast<RemoteWorkerChild*>(Manager());
MOZ_ASSERT(manager);
RefPtr<FetchEventOpProxyChild> self = this;
auto callback = [self](const ServiceWorkerOpResult& aResult) {
// FetchEventOp could finish before NavigationPreload fetch finishes.
// If NavigationPreload is available in FetchEvent, caching FetchEventOp
// result until RecvPreloadResponseEnd is called, such that the preload
// response could be completed.
if (self->mPreloadResponseEndPromise &&
!self->mPreloadResponseEndPromise->IsResolved() &&
self->mPreloadResponseAvailablePromise->IsResolved()) {
self->mCachedOpResult = Some(aResult);
return;
}
if (!self->CanSend()) {
return;
}
if (NS_WARN_IF(aResult.type() == ServiceWorkerOpResult::Tnsresult)) {
Unused << self->Send__delete__(self, aResult.get_nsresult());
return;
}
MOZ_ASSERT(aResult.type() ==
ServiceWorkerOpResult::TServiceWorkerFetchEventOpResult);
Unused << self->Send__delete__(self, aResult);
};
RefPtr<FetchEventOp> op = ServiceWorkerOp::Create(aArgs, std::move(callback))
.template downcast<FetchEventOp>();
MOZ_ASSERT(op);
op->SetActor(this);
mOp = op;
op->GetRespondWithPromise()
->Then(GetCurrentSerialEventTarget(), __func__,
[self = std::move(self)](
FetchEventRespondWithPromise::ResolveOrRejectValue&& aResult) {
self->mRespondWithPromiseRequestHolder.Complete();
if (NS_WARN_IF(aResult.IsReject())) {
MOZ_ASSERT(NS_FAILED(aResult.RejectValue().status()));
Unused << self->SendRespondWith(aResult.RejectValue());
return;
}
auto& result = aResult.ResolveValue();
if (result.is<SynthesizeResponseArgs>()) {
ChildToParentSynthesizeResponseArgs ipcArgs;
nsresult rv = GetIPCSynthesizeResponseArgs(
&ipcArgs, result.extract<SynthesizeResponseArgs>());
if (NS_WARN_IF(NS_FAILED(rv))) {
Unused << self->SendRespondWith(
CancelInterceptionArgs(rv, ipcArgs.timeStamps()));
return;
}
Unused << self->SendRespondWith(ipcArgs);
} else if (result.is<ResetInterceptionArgs>()) {
Unused << self->SendRespondWith(
result.extract<ResetInterceptionArgs>());
} else {
Unused << self->SendRespondWith(
result.extract<CancelInterceptionArgs>());
}
})
->Track(mRespondWithPromiseRequestHolder);
manager->MaybeStartOp(std::move(op));
}
SafeRefPtr<InternalRequest> FetchEventOpProxyChild::ExtractInternalRequest() {
MOZ_ASSERT(IsCurrentThreadRunningWorker());
MOZ_ASSERT(mInternalRequest);
return std::move(mInternalRequest);
}
RefPtr<FetchEventPreloadResponseAvailablePromise>
FetchEventOpProxyChild::GetPreloadResponseAvailablePromise() {
return mPreloadResponseAvailablePromise;
}
RefPtr<FetchEventPreloadResponseTimingPromise>
FetchEventOpProxyChild::GetPreloadResponseTimingPromise() {
return mPreloadResponseTimingPromise;
}
RefPtr<FetchEventPreloadResponseEndPromise>
FetchEventOpProxyChild::GetPreloadResponseEndPromise() {
return mPreloadResponseEndPromise;
}
mozilla::ipc::IPCResult FetchEventOpProxyChild::RecvPreloadResponse(
ParentToChildInternalResponse&& aResponse) {
// Receiving this message implies that navigation preload is enabled, so
// Initialize() should have created this promise.
MOZ_ASSERT(mPreloadResponseAvailablePromise);
mPreloadResponseAvailablePromise->Resolve(
InternalResponse::FromIPC(aResponse), __func__);
return IPC_OK();
}
mozilla::ipc::IPCResult FetchEventOpProxyChild::RecvPreloadResponseTiming(
ResponseTiming&& aTiming) {
// Receiving this message implies that navigation preload is enabled, so
// Initialize() should have created this promise.
MOZ_ASSERT(mPreloadResponseTimingPromise);
mPreloadResponseTimingPromise->Resolve(std::move(aTiming), __func__);
return IPC_OK();
}
mozilla::ipc::IPCResult FetchEventOpProxyChild::RecvPreloadResponseEnd(
ResponseEndArgs&& aArgs) {
// Receiving this message implies that navigation preload is enabled, so
// Initialize() should have created this promise.
MOZ_ASSERT(mPreloadResponseEndPromise);
mPreloadResponseEndPromise->Resolve(std::move(aArgs), __func__);
// If mCachedOpResult is not nothing, it means FetchEventOp had already done
// and the operation result is cached. Continue closing IPC here.
if (mCachedOpResult.isNothing()) {
return IPC_OK();
}
if (!CanSend()) {
return IPC_OK();
}
if (NS_WARN_IF(mCachedOpResult.ref().type() ==
ServiceWorkerOpResult::Tnsresult)) {
Unused << Send__delete__(this, mCachedOpResult.ref().get_nsresult());
return IPC_OK();
}
MOZ_ASSERT(mCachedOpResult.ref().type() ==
ServiceWorkerOpResult::TServiceWorkerFetchEventOpResult);
Unused << Send__delete__(this, mCachedOpResult.ref());
return IPC_OK();
}
void FetchEventOpProxyChild::ActorDestroy(ActorDestroyReason) {
Unused << NS_WARN_IF(mRespondWithPromiseRequestHolder.Exists());
mRespondWithPromiseRequestHolder.DisconnectIfExists();
// If mPreloadResponseAvailablePromise exists, navigation preloading response
// will not be valid anymore since it is too late to respond to the
// FetchEvent. Resolve the preload response promise with
// NS_ERROR_DOM_ABORT_ERR.
if (mPreloadResponseAvailablePromise) {
mPreloadResponseAvailablePromise->Resolve(
InternalResponse::NetworkError(NS_ERROR_DOM_ABORT_ERR), __func__);
}
if (mPreloadResponseTimingPromise) {
mPreloadResponseTimingPromise->Resolve(ResponseTiming(), __func__);
}
if (mPreloadResponseEndPromise) {
ResponseEndArgs args(FetchDriverObserver::eAborted);
mPreloadResponseEndPromise->Resolve(args, __func__);
}
mOp->RevokeActor(this);
mOp = nullptr;
}
} // namespace dom
} // namespace mozilla
|