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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/* -*- 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 "RemoteWorkerController.h"
#include <utility>
#include "nsDebug.h"
#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/RemoteLazyInputStreamStorage.h"
#include "mozilla/dom/FetchEventOpParent.h"
#include "mozilla/dom/FetchEventOpProxyParent.h"
#include "mozilla/dom/MessagePortParent.h"
#include "mozilla/dom/RemoteWorkerTypes.h"
#include "mozilla/dom/ServiceWorkerCloneData.h"
#include "mozilla/dom/ServiceWorkerShutdownState.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "RemoteWorkerControllerParent.h"
#include "RemoteWorkerManager.h"
#include "RemoteWorkerParent.h"
namespace mozilla {
using namespace ipc;
namespace dom {
/* static */
already_AddRefed<RemoteWorkerController> RemoteWorkerController::Create(
const RemoteWorkerData& aData, RemoteWorkerObserver* aObserver,
base::ProcessId aProcessId) {
AssertIsInMainProcess();
AssertIsOnBackgroundThread();
MOZ_ASSERT(aObserver);
RefPtr<RemoteWorkerController> controller =
new RemoteWorkerController(aData, aObserver);
RefPtr<RemoteWorkerManager> manager = RemoteWorkerManager::GetOrCreate();
MOZ_ASSERT(manager);
// XXX: We do not check for failure here, should we?
manager->Launch(controller, aData, aProcessId);
return controller.forget();
}
RemoteWorkerController::RemoteWorkerController(const RemoteWorkerData& aData,
RemoteWorkerObserver* aObserver)
: mObserver(aObserver),
mState(ePending),
mIsServiceWorker(aData.serviceWorkerData().type() ==
OptionalServiceWorkerData::TServiceWorkerData) {
AssertIsInMainProcess();
AssertIsOnBackgroundThread();
}
RemoteWorkerController::~RemoteWorkerController() {
AssertIsOnBackgroundThread();
MOZ_DIAGNOSTIC_ASSERT(mPendingOps.IsEmpty());
}
void RemoteWorkerController::SetWorkerActor(RemoteWorkerParent* aActor) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(!mActor);
MOZ_ASSERT(aActor);
mActor = aActor;
}
void RemoteWorkerController::NoteDeadWorkerActor() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mActor);
// The actor has been destroyed without a proper close() notification. Let's
// inform the observer.
if (mState == eReady) {
mObserver->Terminated();
}
mActor = nullptr;
Shutdown();
}
void RemoteWorkerController::CreationFailed() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(mState == ePending || mState == eTerminated);
if (mState == eTerminated) {
MOZ_ASSERT(!mActor);
MOZ_ASSERT(mPendingOps.IsEmpty());
// Nothing to do.
return;
}
NoteDeadWorker();
mObserver->CreationFailed();
}
void RemoteWorkerController::CreationSucceeded() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mState == ePending || mState == eTerminated);
if (mState == eTerminated) {
MOZ_ASSERT(!mActor);
MOZ_ASSERT(mPendingOps.IsEmpty());
// Nothing to do.
return;
}
MOZ_ASSERT(mActor);
mState = eReady;
mObserver->CreationSucceeded();
auto pendingOps = std::move(mPendingOps);
for (auto& op : pendingOps) {
DebugOnly<bool> started = op->MaybeStart(this);
MOZ_ASSERT(started);
}
}
void RemoteWorkerController::ErrorPropagation(const ErrorValue& aValue) {
AssertIsOnBackgroundThread();
mObserver->ErrorReceived(aValue);
}
void RemoteWorkerController::NotifyLock(bool aCreated) {
AssertIsOnBackgroundThread();
mObserver->LockNotified(aCreated);
}
void RemoteWorkerController::NotifyWebTransport(bool aCreated) {
AssertIsOnBackgroundThread();
mObserver->WebTransportNotified(aCreated);
}
void RemoteWorkerController::WorkerTerminated() {
AssertIsOnBackgroundThread();
NoteDeadWorker();
mObserver->Terminated();
}
void RemoteWorkerController::CancelAllPendingOps() {
AssertIsOnBackgroundThread();
auto pendingOps = std::move(mPendingOps);
for (auto& op : pendingOps) {
op->Cancel();
}
}
void RemoteWorkerController::Shutdown() {
AssertIsOnBackgroundThread();
Unused << NS_WARN_IF(mIsServiceWorker && !mPendingOps.IsEmpty());
if (mState == eTerminated) {
MOZ_ASSERT(mPendingOps.IsEmpty());
return;
}
mState = eTerminated;
CancelAllPendingOps();
if (!mActor) {
return;
}
mActor->SetController(nullptr);
/**
* The "non-remote-side" of the Service Worker will have ensured that the
* remote worker is terminated before calling `Shutdown().`
*/
if (mIsServiceWorker) {
mActor->MaybeSendDelete();
} else {
Unused << mActor->SendExecOp(RemoteWorkerTerminateOp());
}
mActor = nullptr;
}
void RemoteWorkerController::NoteDeadWorker() {
AssertIsOnBackgroundThread();
CancelAllPendingOps();
/**
* The "non-remote-side" of the Service Worker will initiate `Shutdown()`
* once it's notified that all dispatched operations have either completed
* or canceled. That is, it'll explicitly call `Shutdown()` later.
*/
if (!mIsServiceWorker) {
Shutdown();
}
}
template <typename... Args>
void RemoteWorkerController::MaybeStartSharedWorkerOp(Args&&... aArgs) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(!mIsServiceWorker);
UniquePtr<PendingSharedWorkerOp> op =
MakeUnique<PendingSharedWorkerOp>(std::forward<Args>(aArgs)...);
if (!op->MaybeStart(this)) {
mPendingOps.AppendElement(std::move(op));
}
}
void RemoteWorkerController::AddWindowID(uint64_t aWindowID) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aWindowID);
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eAddWindowID, aWindowID);
}
void RemoteWorkerController::RemoveWindowID(uint64_t aWindowID) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aWindowID);
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eRemoveWindowID, aWindowID);
}
void RemoteWorkerController::AddPortIdentifier(
const MessagePortIdentifier& aPortIdentifier) {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(aPortIdentifier);
}
void RemoteWorkerController::Terminate() {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eTerminate);
}
void RemoteWorkerController::Suspend() {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eSuspend);
}
void RemoteWorkerController::Resume() {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eResume);
}
void RemoteWorkerController::Freeze() {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eFreeze);
}
void RemoteWorkerController::Thaw() {
AssertIsOnBackgroundThread();
MaybeStartSharedWorkerOp(PendingSharedWorkerOp::eThaw);
}
RefPtr<ServiceWorkerOpPromise> RemoteWorkerController::ExecServiceWorkerOp(
ServiceWorkerOpArgs&& aArgs) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mIsServiceWorker);
RefPtr<ServiceWorkerOpPromise::Private> promise =
new ServiceWorkerOpPromise::Private(__func__);
UniquePtr<PendingServiceWorkerOp> op =
MakeUnique<PendingServiceWorkerOp>(std::move(aArgs), promise);
if (!op->MaybeStart(this)) {
mPendingOps.AppendElement(std::move(op));
}
return promise;
}
RefPtr<ServiceWorkerFetchEventOpPromise>
RemoteWorkerController::ExecServiceWorkerFetchEventOp(
const ParentToParentServiceWorkerFetchEventOpArgs& aArgs,
RefPtr<FetchEventOpParent> aReal) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mIsServiceWorker);
RefPtr<ServiceWorkerFetchEventOpPromise::Private> promise =
new ServiceWorkerFetchEventOpPromise::Private(__func__);
UniquePtr<PendingSWFetchEventOp> op =
MakeUnique<PendingSWFetchEventOp>(aArgs, promise, std::move(aReal));
if (!op->MaybeStart(this)) {
mPendingOps.AppendElement(std::move(op));
}
return promise;
}
RefPtr<GenericPromise> RemoteWorkerController::SetServiceWorkerSkipWaitingFlag()
const {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mObserver);
RefPtr<GenericPromise::Private> promise =
new GenericPromise::Private(__func__);
static_cast<RemoteWorkerControllerParent*>(mObserver.get())
->MaybeSendSetServiceWorkerSkipWaitingFlag(
[promise](bool aOk) { promise->Resolve(aOk, __func__); });
return promise;
}
bool RemoteWorkerController::IsTerminated() const {
return mState == eTerminated;
}
RemoteWorkerController::PendingSharedWorkerOp::PendingSharedWorkerOp(
Type aType, uint64_t aWindowID)
: mType(aType), mWindowID(aWindowID) {
AssertIsOnBackgroundThread();
}
RemoteWorkerController::PendingSharedWorkerOp::PendingSharedWorkerOp(
const MessagePortIdentifier& aPortIdentifier)
: mType(ePortIdentifier), mPortIdentifier(aPortIdentifier) {
AssertIsOnBackgroundThread();
}
RemoteWorkerController::PendingSharedWorkerOp::~PendingSharedWorkerOp() {
AssertIsOnBackgroundThread();
MOZ_DIAGNOSTIC_ASSERT(mCompleted);
}
bool RemoteWorkerController::PendingSharedWorkerOp::MaybeStart(
RemoteWorkerController* const aOwner) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(!mCompleted);
MOZ_ASSERT(aOwner);
if (aOwner->mState == RemoteWorkerController::eTerminated) {
Cancel();
return true;
}
if (aOwner->mState == RemoteWorkerController::ePending &&
mType != eTerminate) {
return false;
}
switch (mType) {
case eTerminate:
aOwner->Shutdown();
break;
case eSuspend:
Unused << aOwner->mActor->SendExecOp(RemoteWorkerSuspendOp());
break;
case eResume:
Unused << aOwner->mActor->SendExecOp(RemoteWorkerResumeOp());
break;
case eFreeze:
Unused << aOwner->mActor->SendExecOp(RemoteWorkerFreezeOp());
break;
case eThaw:
Unused << aOwner->mActor->SendExecOp(RemoteWorkerThawOp());
break;
case ePortIdentifier:
Unused << aOwner->mActor->SendExecOp(
RemoteWorkerPortIdentifierOp(mPortIdentifier));
break;
case eAddWindowID:
Unused << aOwner->mActor->SendExecOp(
RemoteWorkerAddWindowIDOp(mWindowID));
break;
case eRemoveWindowID:
Unused << aOwner->mActor->SendExecOp(
RemoteWorkerRemoveWindowIDOp(mWindowID));
break;
default:
MOZ_CRASH("Unknown op.");
}
mCompleted = true;
return true;
}
void RemoteWorkerController::PendingSharedWorkerOp::Cancel() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(!mCompleted);
// We don't want to leak the port if the operation has not been processed.
if (mType == ePortIdentifier) {
MessagePortParent::ForceClose(mPortIdentifier.uuid(),
mPortIdentifier.destinationUuid(),
mPortIdentifier.sequenceId());
}
mCompleted = true;
}
RemoteWorkerController::PendingServiceWorkerOp::PendingServiceWorkerOp(
ServiceWorkerOpArgs&& aArgs,
RefPtr<ServiceWorkerOpPromise::Private> aPromise)
: mArgs(std::move(aArgs)), mPromise(std::move(aPromise)) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
}
RemoteWorkerController::PendingServiceWorkerOp::~PendingServiceWorkerOp() {
AssertIsOnBackgroundThread();
MOZ_DIAGNOSTIC_ASSERT(!mPromise);
}
bool RemoteWorkerController::PendingServiceWorkerOp::MaybeStart(
RemoteWorkerController* const aOwner) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
MOZ_ASSERT(aOwner);
if (NS_WARN_IF(aOwner->mState == RemoteWorkerController::eTerminated)) {
mPromise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
mPromise = nullptr;
return true;
}
// The target content process must still be starting up.
if (!aOwner->mActor) {
// We can avoid starting the worker at all if we know it should be
// terminated.
MOZ_ASSERT(aOwner->mState == RemoteWorkerController::ePending);
if (mArgs.type() ==
ServiceWorkerOpArgs::TServiceWorkerTerminateWorkerOpArgs) {
aOwner->CancelAllPendingOps();
Cancel();
aOwner->mState = RemoteWorkerController::eTerminated;
return true;
}
return false;
}
/**
* Allow termination operations to pass through while pending because the
* remote Service Worker can be terminated while still starting up.
*/
if (aOwner->mState == RemoteWorkerController::ePending &&
mArgs.type() !=
ServiceWorkerOpArgs::TServiceWorkerTerminateWorkerOpArgs) {
return false;
}
MaybeReportServiceWorkerShutdownProgress(mArgs);
aOwner->mActor->SendExecServiceWorkerOp(mArgs)->Then(
GetCurrentSerialEventTarget(), __func__,
[promise = std::move(mPromise)](
PRemoteWorkerParent::ExecServiceWorkerOpPromise::
ResolveOrRejectValue&& aResult) {
if (NS_WARN_IF(aResult.IsReject())) {
promise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
return;
}
promise->Resolve(std::move(aResult.ResolveValue()), __func__);
});
return true;
}
void RemoteWorkerController::PendingServiceWorkerOp::Cancel() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
mPromise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
mPromise = nullptr;
}
RemoteWorkerController::PendingSWFetchEventOp::PendingSWFetchEventOp(
const ParentToParentServiceWorkerFetchEventOpArgs& aArgs,
RefPtr<ServiceWorkerFetchEventOpPromise::Private> aPromise,
RefPtr<FetchEventOpParent>&& aReal)
: mArgs(aArgs), mPromise(std::move(aPromise)), mReal(aReal) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
// If there is a TParentToParentStream in the request body, we need to
// save it to our stream.
IPCInternalRequest& req = mArgs.common().internalRequest();
if (req.body().isSome() &&
req.body().ref().type() == BodyStreamVariant::TParentToParentStream) {
nsCOMPtr<nsIInputStream> stream;
auto streamLength = req.bodySize();
const auto& uuid = req.body().ref().get_ParentToParentStream().uuid();
auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);
MOZ_DIAGNOSTIC_ASSERT(storage);
storage->GetStream(uuid, 0, streamLength, getter_AddRefs(mBodyStream));
storage->ForgetStream(uuid);
MOZ_DIAGNOSTIC_ASSERT(mBodyStream);
req.body() = Nothing();
}
}
RemoteWorkerController::PendingSWFetchEventOp::~PendingSWFetchEventOp() {
AssertIsOnBackgroundThread();
MOZ_DIAGNOSTIC_ASSERT(!mPromise);
}
bool RemoteWorkerController::PendingSWFetchEventOp::MaybeStart(
RemoteWorkerController* const aOwner) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
MOZ_ASSERT(aOwner);
if (NS_WARN_IF(aOwner->mState == RemoteWorkerController::eTerminated)) {
mPromise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
mPromise = nullptr;
// Because the worker has transitioned to terminated, this operation is moot
// and so we should return true because there's no need to queue it.
return true;
}
// The target content process must still be starting up.
if (!aOwner->mActor) {
MOZ_ASSERT(aOwner->mState == RemoteWorkerController::ePending);
return false;
}
// At this point we are handing off responsibility for the promise to the
// actor.
FetchEventOpProxyParent::Create(aOwner->mActor.get(), std::move(mPromise),
mArgs, std::move(mReal),
std::move(mBodyStream));
return true;
}
void RemoteWorkerController::PendingSWFetchEventOp::Cancel() {
AssertIsOnBackgroundThread();
MOZ_ASSERT(mPromise);
if (mPromise) {
mPromise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
mPromise = nullptr;
}
}
} // namespace dom
} // namespace mozilla
|