Source code

Revision control

Other Tools

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/. */
6
7
#include "ServiceWorkerPrivate.h"
8
9
#include <utility>
10
11
#include "ServiceWorkerCloneData.h"
12
#include "ServiceWorkerManager.h"
13
#include "ServiceWorkerPrivateImpl.h"
14
#include "ServiceWorkerUtils.h"
15
#include "nsContentUtils.h"
16
#include "nsICacheInfoChannel.h"
17
#include "nsIHttpChannelInternal.h"
18
#include "nsIHttpHeaderVisitor.h"
19
#include "nsINamed.h"
20
#include "nsINetworkInterceptController.h"
21
#include "nsIPushErrorReporter.h"
22
#include "nsISupportsImpl.h"
23
#include "nsIUploadChannel2.h"
24
#include "nsNetUtil.h"
25
#include "nsProxyRelease.h"
26
#include "nsQueryObject.h"
27
#include "nsStreamUtils.h"
28
#include "nsStringStream.h"
29
#include "mozilla/Assertions.h"
30
#include "mozilla/CycleCollectedJSContext.h" // for MicroTaskRunnable
31
#include "mozilla/JSObjectHolder.h"
32
#include "mozilla/dom/Client.h"
33
#include "mozilla/dom/ClientIPCTypes.h"
34
#include "mozilla/dom/FetchUtil.h"
35
#include "mozilla/dom/IndexedDatabaseManager.h"
36
#include "mozilla/dom/InternalHeaders.h"
37
#include "mozilla/dom/NotificationEvent.h"
38
#include "mozilla/dom/PromiseNativeHandler.h"
39
#include "mozilla/dom/PushEventBinding.h"
40
#include "mozilla/dom/RequestBinding.h"
41
#include "mozilla/dom/WorkerDebugger.h"
42
#include "mozilla/dom/WorkerRef.h"
43
#include "mozilla/dom/WorkerRunnable.h"
44
#include "mozilla/dom/WorkerScope.h"
45
#include "mozilla/dom/ipc/StructuredCloneData.h"
46
#include "mozilla/ipc/BackgroundUtils.h"
47
#include "mozilla/net/CookieSettings.h"
48
#include "mozilla/net/NeckoChannelParams.h"
49
#include "mozilla/Services.h"
50
#include "mozilla/DebugOnly.h"
51
#include "mozilla/StaticPrefs_dom.h"
52
#include "mozilla/Unused.h"
53
#include "nsIReferrerInfo.h"
54
55
using namespace mozilla;
56
using namespace mozilla::dom;
57
58
namespace mozilla {
59
namespace dom {
60
61
using mozilla::ipc::PrincipalInfo;
62
63
NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(ServiceWorkerPrivate)
64
NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(ServiceWorkerPrivate)
65
NS_IMPL_CYCLE_COLLECTION(ServiceWorkerPrivate, mSupportsArray)
66
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ServiceWorkerPrivate, AddRef)
67
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ServiceWorkerPrivate, Release)
68
69
// Tracks the "dom.serviceWorkers.disable_open_click_delay" preference. Modified
70
// on main thread, read on worker threads.
71
// It is updated every time a "notificationclick" event is dispatched. While
72
// this is done without synchronization, at the worst, the thread will just get
73
// an older value within which a popup is allowed to be displayed, which will
74
// still be a valid value since it was set prior to dispatching the runnable.
75
Atomic<uint32_t> gDOMDisableOpenClickDelay(0);
76
77
KeepAliveToken::KeepAliveToken(ServiceWorkerPrivate* aPrivate)
78
: mPrivate(aPrivate) {
79
MOZ_ASSERT(NS_IsMainThread());
80
MOZ_ASSERT(aPrivate);
81
mPrivate->AddToken();
82
}
83
84
KeepAliveToken::~KeepAliveToken() {
85
MOZ_ASSERT(NS_IsMainThread());
86
mPrivate->ReleaseToken();
87
}
88
89
NS_IMPL_ISUPPORTS0(KeepAliveToken)
90
91
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo)
92
: mInfo(aInfo), mDebuggerCount(0), mTokenCount(0) {
93
MOZ_ASSERT(NS_IsMainThread());
94
MOZ_ASSERT(aInfo);
95
96
mIdleWorkerTimer = NS_NewTimer();
97
MOZ_ASSERT(mIdleWorkerTimer);
98
99
if (ServiceWorkerParentInterceptEnabled()) {
100
RefPtr<ServiceWorkerPrivateImpl> inner = new ServiceWorkerPrivateImpl(this);
101
102
// Assert in all debug builds as well as non-debug Nightly and Dev Edition.
103
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
104
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(inner->Initialize()));
105
#else
106
MOZ_ALWAYS_SUCCEEDS(inner->Initialize());
107
#endif
108
109
mInner = inner.forget();
110
}
111
}
112
113
ServiceWorkerPrivate::~ServiceWorkerPrivate() {
114
MOZ_ASSERT(!mWorkerPrivate);
115
MOZ_ASSERT(!mTokenCount);
116
MOZ_ASSERT(!mInner);
117
MOZ_ASSERT(!mInfo);
118
MOZ_ASSERT(mSupportsArray.IsEmpty());
119
MOZ_ASSERT(mIdlePromiseHolder.IsEmpty());
120
121
mIdleWorkerTimer->Cancel();
122
}
123
124
namespace {
125
126
class CheckScriptEvaluationWithCallback final : public WorkerDebuggeeRunnable {
127
nsMainThreadPtrHandle<ServiceWorkerPrivate> mServiceWorkerPrivate;
128
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
129
130
// The script evaluation result must be reported even if the runnable
131
// is cancelled.
132
RefPtr<LifeCycleEventCallback> mScriptEvaluationCallback;
133
134
#ifdef DEBUG
135
bool mDone;
136
#endif
137
138
public:
139
CheckScriptEvaluationWithCallback(
140
WorkerPrivate* aWorkerPrivate,
141
ServiceWorkerPrivate* aServiceWorkerPrivate,
142
KeepAliveToken* aKeepAliveToken,
143
LifeCycleEventCallback* aScriptEvaluationCallback)
144
: WorkerDebuggeeRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
145
mServiceWorkerPrivate(new nsMainThreadPtrHolder<ServiceWorkerPrivate>(
146
"CheckScriptEvaluationWithCallback::mServiceWorkerPrivate",
147
aServiceWorkerPrivate)),
148
mKeepAliveToken(new nsMainThreadPtrHolder<KeepAliveToken>(
149
"CheckScriptEvaluationWithCallback::mKeepAliveToken",
150
aKeepAliveToken)),
151
mScriptEvaluationCallback(aScriptEvaluationCallback)
152
#ifdef DEBUG
153
,
154
mDone(false)
155
#endif
156
{
157
MOZ_ASSERT(NS_IsMainThread());
158
}
159
160
~CheckScriptEvaluationWithCallback() { MOZ_ASSERT(mDone); }
161
162
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
163
aWorkerPrivate->AssertIsOnWorkerThread();
164
165
bool fetchHandlerWasAdded = aWorkerPrivate->FetchHandlerWasAdded();
166
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<bool>(
167
"dom::CheckScriptEvaluationWithCallback::ReportFetchFlag", this,
168
&CheckScriptEvaluationWithCallback::ReportFetchFlag,
169
fetchHandlerWasAdded);
170
aWorkerPrivate->DispatchToMainThread(runnable.forget());
171
172
ReportScriptEvaluationResult(
173
aWorkerPrivate->WorkerScriptExecutedSuccessfully());
174
175
return true;
176
}
177
178
void ReportFetchFlag(bool aFetchHandlerWasAdded) {
179
MOZ_ASSERT(NS_IsMainThread());
180
mServiceWorkerPrivate->SetHandlesFetch(aFetchHandlerWasAdded);
181
}
182
183
nsresult Cancel() override {
184
ReportScriptEvaluationResult(false);
185
return WorkerRunnable::Cancel();
186
}
187
188
private:
189
void ReportScriptEvaluationResult(bool aScriptEvaluationResult) {
190
#ifdef DEBUG
191
mDone = true;
192
#endif
193
mScriptEvaluationCallback->SetResult(aScriptEvaluationResult);
194
MOZ_ALWAYS_SUCCEEDS(
195
mWorkerPrivate->DispatchToMainThread(mScriptEvaluationCallback));
196
}
197
};
198
199
} // anonymous namespace
200
201
nsresult ServiceWorkerPrivate::CheckScriptEvaluation(
202
LifeCycleEventCallback* aScriptEvaluationCallback) {
203
MOZ_ASSERT(NS_IsMainThread());
204
205
if (mInner) {
206
return mInner->CheckScriptEvaluation(aScriptEvaluationCallback);
207
}
208
209
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
210
NS_ENSURE_SUCCESS(rv, rv);
211
212
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
213
RefPtr<WorkerRunnable> r = new CheckScriptEvaluationWithCallback(
214
mWorkerPrivate, this, token, aScriptEvaluationCallback);
215
if (NS_WARN_IF(!r->Dispatch())) {
216
return NS_ERROR_FAILURE;
217
}
218
219
return NS_OK;
220
}
221
222
namespace {
223
224
class KeepAliveHandler final : public ExtendableEvent::ExtensionsHandler,
225
public PromiseNativeHandler {
226
// This class manages lifetime extensions added by calling WaitUntil()
227
// or RespondWith(). We allow new extensions as long as we still hold
228
// |mKeepAliveToken|. Once the last promise was settled, we queue a microtask
229
// which releases the token and prevents further extensions. By doing this,
230
// we give other pending microtasks a chance to continue adding extensions.
231
232
RefPtr<StrongWorkerRef> mWorkerRef;
233
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
234
235
// We start holding a self reference when the first extension promise is
236
// added. As far as I can tell, the only case where this is useful is when
237
// we're waiting indefinitely on a promise that's no longer reachable
238
// and will never be settled.
239
// The cycle is broken when the last promise was settled or when the
240
// worker is shutting down.
241
RefPtr<KeepAliveHandler> mSelfRef;
242
243
// Called when the last promise was settled.
244
RefPtr<ExtendableEventCallback> mCallback;
245
246
uint32_t mPendingPromisesCount;
247
248
// We don't actually care what values the promises resolve to, only whether
249
// any of them were rejected.
250
bool mRejected;
251
252
public:
253
NS_DECL_ISUPPORTS
254
255
explicit KeepAliveHandler(
256
const nsMainThreadPtrHandle<KeepAliveToken>& aKeepAliveToken,
257
ExtendableEventCallback* aCallback)
258
: mKeepAliveToken(aKeepAliveToken),
259
mCallback(aCallback),
260
mPendingPromisesCount(0),
261
mRejected(false) {
262
MOZ_ASSERT(mKeepAliveToken);
263
}
264
265
bool Init() {
266
MOZ_ASSERT(IsCurrentThreadRunningWorker());
267
268
RefPtr<KeepAliveHandler> self = this;
269
mWorkerRef = StrongWorkerRef::Create(GetCurrentThreadWorkerPrivate(),
270
"KeepAliveHandler",
271
[self]() { self->MaybeCleanup(); });
272
273
if (NS_WARN_IF(!mWorkerRef)) {
274
return false;
275
}
276
277
return true;
278
}
279
280
bool WaitOnPromise(Promise& aPromise) override {
281
if (!mKeepAliveToken) {
282
MOZ_ASSERT(!mSelfRef, "We shouldn't be holding a self reference!");
283
return false;
284
}
285
if (!mSelfRef) {
286
MOZ_ASSERT(!mPendingPromisesCount);
287
mSelfRef = this;
288
}
289
290
++mPendingPromisesCount;
291
aPromise.AppendNativeHandler(this);
292
293
return true;
294
}
295
296
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
297
RemovePromise(Resolved);
298
}
299
300
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
301
RemovePromise(Rejected);
302
}
303
304
void MaybeDone() {
305
MOZ_ASSERT(IsCurrentThreadRunningWorker());
306
307
if (mPendingPromisesCount || !mKeepAliveToken) {
308
return;
309
}
310
if (mCallback) {
311
mCallback->FinishedWithResult(mRejected ? Rejected : Resolved);
312
}
313
314
MaybeCleanup();
315
}
316
317
private:
318
~KeepAliveHandler() { MaybeCleanup(); }
319
320
void MaybeCleanup() {
321
MOZ_ASSERT(IsCurrentThreadRunningWorker());
322
323
if (!mKeepAliveToken) {
324
return;
325
}
326
327
mWorkerRef = nullptr;
328
mKeepAliveToken = nullptr;
329
mSelfRef = nullptr;
330
}
331
332
class MaybeDoneRunner : public MicroTaskRunnable {
333
public:
334
explicit MaybeDoneRunner(KeepAliveHandler* aHandler) : mHandler(aHandler) {}
335
virtual void Run(AutoSlowOperation& aAso) override {
336
mHandler->MaybeDone();
337
}
338
339
RefPtr<KeepAliveHandler> mHandler;
340
};
341
342
void RemovePromise(ExtendableEventResult aResult) {
343
MOZ_ASSERT(IsCurrentThreadRunningWorker());
344
MOZ_DIAGNOSTIC_ASSERT(mPendingPromisesCount > 0);
345
346
// Note: mSelfRef and mKeepAliveToken can be nullptr here
347
// if MaybeCleanup() was called just before a promise
348
// settled. This can happen, for example, if the
349
// worker thread is being terminated for running too
350
// long, browser shutdown, etc.
351
352
mRejected |= (aResult == Rejected);
353
354
--mPendingPromisesCount;
355
if (mPendingPromisesCount) {
356
return;
357
}
358
359
CycleCollectedJSContext* cx = CycleCollectedJSContext::Get();
360
MOZ_ASSERT(cx);
361
362
RefPtr<MaybeDoneRunner> r = new MaybeDoneRunner(this);
363
cx->DispatchToMicroTask(r.forget());
364
}
365
};
366
367
NS_IMPL_ISUPPORTS0(KeepAliveHandler)
368
369
class RegistrationUpdateRunnable : public Runnable {
370
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
371
const bool mNeedTimeCheck;
372
373
public:
374
RegistrationUpdateRunnable(
375
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
376
bool aNeedTimeCheck)
377
: Runnable("dom::RegistrationUpdateRunnable"),
378
mRegistration(aRegistration),
379
mNeedTimeCheck(aNeedTimeCheck) {
380
MOZ_DIAGNOSTIC_ASSERT(mRegistration);
381
}
382
383
NS_IMETHOD
384
Run() override {
385
if (mNeedTimeCheck) {
386
mRegistration->MaybeScheduleTimeCheckAndUpdate();
387
} else {
388
mRegistration->MaybeScheduleUpdate();
389
}
390
return NS_OK;
391
}
392
};
393
394
class ExtendableEventWorkerRunnable : public WorkerRunnable {
395
protected:
396
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
397
398
public:
399
ExtendableEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
400
KeepAliveToken* aKeepAliveToken)
401
: WorkerRunnable(aWorkerPrivate) {
402
MOZ_ASSERT(NS_IsMainThread());
403
MOZ_ASSERT(aWorkerPrivate);
404
MOZ_ASSERT(aKeepAliveToken);
405
406
mKeepAliveToken = new nsMainThreadPtrHolder<KeepAliveToken>(
407
"ExtendableEventWorkerRunnable::mKeepAliveToken", aKeepAliveToken);
408
}
409
410
nsresult DispatchExtendableEventOnWorkerScope(
411
JSContext* aCx, WorkerGlobalScope* aWorkerScope, ExtendableEvent* aEvent,
412
ExtendableEventCallback* aCallback) {
413
MOZ_ASSERT(aWorkerScope);
414
MOZ_ASSERT(aEvent);
415
nsCOMPtr<nsIGlobalObject> sgo = aWorkerScope;
416
WidgetEvent* internalEvent = aEvent->WidgetEventPtr();
417
418
RefPtr<KeepAliveHandler> keepAliveHandler =
419
new KeepAliveHandler(mKeepAliveToken, aCallback);
420
if (NS_WARN_IF(!keepAliveHandler->Init())) {
421
return NS_ERROR_FAILURE;
422
}
423
424
// This must always be set *before* dispatching the event, otherwise
425
// waitUntil calls will fail.
426
aEvent->SetKeepAliveHandler(keepAliveHandler);
427
428
ErrorResult result;
429
aWorkerScope->DispatchEvent(*aEvent, result);
430
if (NS_WARN_IF(result.Failed())) {
431
result.SuppressException();
432
return NS_ERROR_FAILURE;
433
}
434
435
// [[ If e’s extend lifetime promises is empty, unset e’s extensions allowed
436
// flag and abort these steps. ]]
437
keepAliveHandler->MaybeDone();
438
439
// We don't block the event when getting an exception but still report the
440
// error message.
441
// Report exception message. Note: This will not stop the event.
442
if (internalEvent->mFlags.mExceptionWasRaised) {
443
result.SuppressException();
444
return NS_ERROR_XPC_JS_THREW_EXCEPTION;
445
}
446
447
return NS_OK;
448
}
449
};
450
451
class SendMessageEventRunnable final : public ExtendableEventWorkerRunnable {
452
const ClientInfoAndState mClientInfoAndState;
453
RefPtr<ServiceWorkerCloneData> mData;
454
455
public:
456
SendMessageEventRunnable(WorkerPrivate* aWorkerPrivate,
457
KeepAliveToken* aKeepAliveToken,
458
const ClientInfoAndState& aClientInfoAndState,
459
RefPtr<ServiceWorkerCloneData>&& aData)
460
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
461
mClientInfoAndState(aClientInfoAndState),
462
mData(std::move(aData)) {
463
MOZ_ASSERT(NS_IsMainThread());
464
MOZ_DIAGNOSTIC_ASSERT(mData);
465
}
466
467
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
468
JS::Rooted<JS::Value> messageData(aCx);
469
nsCOMPtr<nsIGlobalObject> sgo = aWorkerPrivate->GlobalScope();
470
ErrorResult rv;
471
mData->Read(aCx, &messageData, rv);
472
473
// If deserialization fails, we will fire a messageerror event
474
bool deserializationFailed = rv.ErrorCodeIs(NS_ERROR_DOM_DATA_CLONE_ERR);
475
476
if (!deserializationFailed && NS_WARN_IF(rv.Failed())) {
477
rv.SuppressException();
478
return true;
479
}
480
481
Sequence<OwningNonNull<MessagePort>> ports;
482
if (!mData->TakeTransferredPortsAsSequence(ports)) {
483
return true;
484
}
485
486
RootedDictionary<ExtendableMessageEventInit> init(aCx);
487
488
init.mBubbles = false;
489
init.mCancelable = false;
490
491
// On a messageerror event, we disregard ports:
493
if (!deserializationFailed) {
494
init.mData = messageData;
495
init.mPorts = ports;
496
}
497
498
init.mSource.SetValue().SetAsClient() =
499
new Client(sgo, mClientInfoAndState);
500
501
rv.SuppressException();
502
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
503
RefPtr<ExtendableMessageEvent> extendableEvent =
504
ExtendableMessageEvent::Constructor(
505
target,
506
deserializationFailed ? NS_LITERAL_STRING("messageerror")
507
: NS_LITERAL_STRING("message"),
508
init);
509
510
extendableEvent->SetTrusted(true);
511
512
return NS_SUCCEEDED(DispatchExtendableEventOnWorkerScope(
513
aCx, aWorkerPrivate->GlobalScope(), extendableEvent, nullptr));
514
}
515
};
516
517
} // anonymous namespace
518
519
nsresult ServiceWorkerPrivate::SendMessageEvent(
520
RefPtr<ServiceWorkerCloneData>&& aData,
521
const ClientInfoAndState& aClientInfoAndState) {
522
MOZ_ASSERT(NS_IsMainThread());
523
524
if (mInner) {
525
return mInner->SendMessageEvent(std::move(aData), aClientInfoAndState);
526
}
527
528
nsresult rv = SpawnWorkerIfNeeded(MessageEvent);
529
NS_ENSURE_SUCCESS(rv, rv);
530
531
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
532
RefPtr<SendMessageEventRunnable> runnable = new SendMessageEventRunnable(
533
mWorkerPrivate, token, aClientInfoAndState, std::move(aData));
534
535
if (!runnable->Dispatch()) {
536
return NS_ERROR_FAILURE;
537
}
538
539
return NS_OK;
540
}
541
542
namespace {
543
544
// Handle functional event
545
// 9.9.7 If the time difference in seconds calculated by the current time minus
546
// registration's last update check time is greater than 86400, invoke Soft
547
// Update algorithm.
548
class ExtendableFunctionalEventWorkerRunnable
549
: public ExtendableEventWorkerRunnable {
550
protected:
551
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
552
553
public:
554
ExtendableFunctionalEventWorkerRunnable(
555
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
556
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration)
557
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
558
mRegistration(aRegistration) {
559
MOZ_DIAGNOSTIC_ASSERT(aRegistration);
560
}
561
562
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
563
bool aRunResult) override {
564
// Sub-class PreRun() or WorkerRun() methods could clear our mRegistration.
565
if (mRegistration) {
566
nsCOMPtr<nsIRunnable> runnable =
567
new RegistrationUpdateRunnable(mRegistration, true /* time check */);
568
aWorkerPrivate->DispatchToMainThread(runnable.forget());
569
}
570
571
ExtendableEventWorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
572
}
573
};
574
575
/*
576
* Fires 'install' event on the ServiceWorkerGlobalScope. Modifies busy count
577
* since it fires the event. This is ok since there can't be nested
578
* ServiceWorkers, so the parent thread -> worker thread requirement for
579
* runnables is satisfied.
580
*/
581
class LifecycleEventWorkerRunnable : public ExtendableEventWorkerRunnable {
582
nsString mEventName;
583
RefPtr<LifeCycleEventCallback> mCallback;
584
585
public:
586
LifecycleEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
587
KeepAliveToken* aToken,
588
const nsAString& aEventName,
589
LifeCycleEventCallback* aCallback)
590
: ExtendableEventWorkerRunnable(aWorkerPrivate, aToken),
591
mEventName(aEventName),
592
mCallback(aCallback) {
593
MOZ_ASSERT(NS_IsMainThread());
594
}
595
596
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
597
MOZ_ASSERT(aWorkerPrivate);
598
return DispatchLifecycleEvent(aCx, aWorkerPrivate);
599
}
600
601
nsresult Cancel() override {
602
mCallback->SetResult(false);
603
MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback));
604
605
return WorkerRunnable::Cancel();
606
}
607
608
private:
609
bool DispatchLifecycleEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
610
};
611
612
/*
613
* Used to handle ExtendableEvent::waitUntil() and catch abnormal worker
614
* termination during the execution of life cycle events. It is responsible
615
* with advancing the job queue for install/activate tasks.
616
*/
617
class LifeCycleEventWatcher final : public ExtendableEventCallback {
618
RefPtr<StrongWorkerRef> mWorkerRef;
619
RefPtr<LifeCycleEventCallback> mCallback;
620
621
~LifeCycleEventWatcher() {
622
// XXXcatalinb: If all the promises passed to waitUntil go out of scope,
623
// the resulting Promise.all will be cycle collected and it will drop its
624
// native handlers (including this object). Instead of waiting for a timeout
625
// we report the failure now.
626
ReportResult(false);
627
}
628
629
public:
630
NS_INLINE_DECL_REFCOUNTING(LifeCycleEventWatcher, override)
631
632
explicit LifeCycleEventWatcher(LifeCycleEventCallback* aCallback)
633
: mCallback(aCallback) {
634
MOZ_ASSERT(IsCurrentThreadRunningWorker());
635
}
636
637
bool Init() {
638
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
639
MOZ_ASSERT(workerPrivate);
640
641
// We need to listen for worker termination in case the event handler
642
// never completes or never resolves the waitUntil promise. There are
643
// two possible scenarios:
644
// 1. The keepAlive token expires and the worker is terminated, in which
645
// case the registration/update promise will be rejected
646
// 2. A new service worker is registered which will terminate the current
647
// installing worker.
648
RefPtr<LifeCycleEventWatcher> self = this;
649
mWorkerRef =
650
StrongWorkerRef::Create(workerPrivate, "LifeCycleEventWatcher",
651
[self]() { self->ReportResult(false); });
652
if (NS_WARN_IF(!mWorkerRef)) {
653
mCallback->SetResult(false);
654
// Using DispatchToMainThreadForMessaging so that state update on
655
// the main thread doesn't happen too soon.
656
nsresult rv = workerPrivate->DispatchToMainThreadForMessaging(mCallback);
657
Unused << NS_WARN_IF(NS_FAILED(rv));
658
return false;
659
}
660
661
return true;
662
}
663
664
void ReportResult(bool aResult) {
665
MOZ_ASSERT(IsCurrentThreadRunningWorker());
666
667
if (!mWorkerRef) {
668
return;
669
}
670
671
mCallback->SetResult(aResult);
672
// Using DispatchToMainThreadForMessaging so that state update on
673
// the main thread doesn't happen too soon.
674
nsresult rv =
675
mWorkerRef->Private()->DispatchToMainThreadForMessaging(mCallback);
676
if (NS_WARN_IF(NS_FAILED(rv))) {
677
MOZ_CRASH("Failed to dispatch life cycle event handler.");
678
}
679
680
mWorkerRef = nullptr;
681
}
682
683
void FinishedWithResult(ExtendableEventResult aResult) override {
684
MOZ_ASSERT(IsCurrentThreadRunningWorker());
685
ReportResult(aResult == Resolved);
686
687
// Note, all WaitUntil() rejections are reported to client consoles
688
// by the WaitUntilHandler in ServiceWorkerEvents. This ensures that
689
// errors in non-lifecycle events like FetchEvent and PushEvent are
690
// reported properly.
691
}
692
};
693
694
bool LifecycleEventWorkerRunnable::DispatchLifecycleEvent(
695
JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
696
aWorkerPrivate->AssertIsOnWorkerThread();
697
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
698
699
RefPtr<ExtendableEvent> event;
700
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
701
702
if (mEventName.EqualsASCII("install") || mEventName.EqualsASCII("activate")) {
703
ExtendableEventInit init;
704
init.mBubbles = false;
705
init.mCancelable = false;
706
event = ExtendableEvent::Constructor(target, mEventName, init);
707
} else {
708
MOZ_CRASH("Unexpected lifecycle event");
709
}
710
711
event->SetTrusted(true);
712
713
// It is important to initialize the watcher before actually dispatching
714
// the event in order to catch worker termination while the event handler
715
// is still executing. This can happen with infinite loops, for example.
716
RefPtr<LifeCycleEventWatcher> watcher = new LifeCycleEventWatcher(mCallback);
717
718
if (!watcher->Init()) {
719
return true;
720
}
721
722
nsresult rv = DispatchExtendableEventOnWorkerScope(
723
aCx, aWorkerPrivate->GlobalScope(), event, watcher);
724
// Do not fail event processing when an exception is thrown.
725
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION) {
726
watcher->ReportResult(false);
727
}
728
729
return true;
730
}
731
732
} // anonymous namespace
733
734
nsresult ServiceWorkerPrivate::SendLifeCycleEvent(
735
const nsAString& aEventType, LifeCycleEventCallback* aCallback) {
736
MOZ_ASSERT(NS_IsMainThread());
737
738
if (mInner) {
739
return mInner->SendLifeCycleEvent(aEventType, aCallback);
740
}
741
742
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
743
NS_ENSURE_SUCCESS(rv, rv);
744
745
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
746
RefPtr<WorkerRunnable> r = new LifecycleEventWorkerRunnable(
747
mWorkerPrivate, token, aEventType, aCallback);
748
if (NS_WARN_IF(!r->Dispatch())) {
749
return NS_ERROR_FAILURE;
750
}
751
752
return NS_OK;
753
}
754
755
namespace {
756
757
class PushErrorReporter final : public ExtendableEventCallback {
758
WorkerPrivate* mWorkerPrivate;
759
nsString mMessageId;
760
761
~PushErrorReporter() {}
762
763
public:
764
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PushErrorReporter, override)
765
766
PushErrorReporter(WorkerPrivate* aWorkerPrivate, const nsAString& aMessageId)
767
: mWorkerPrivate(aWorkerPrivate), mMessageId(aMessageId) {
768
mWorkerPrivate->AssertIsOnWorkerThread();
769
}
770
771
void FinishedWithResult(ExtendableEventResult aResult) override {
772
if (aResult == Rejected) {
773
Report(nsIPushErrorReporter::DELIVERY_UNHANDLED_REJECTION);
774
}
775
}
776
777
void Report(
778
uint16_t aReason = nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) {
779
WorkerPrivate* workerPrivate = mWorkerPrivate;
780
mWorkerPrivate->AssertIsOnWorkerThread();
781
782
if (NS_WARN_IF(aReason > nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) ||
783
mMessageId.IsEmpty()) {
784
return;
785
}
786
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<uint16_t>(
787
"dom::PushErrorReporter::ReportOnMainThread", this,
788
&PushErrorReporter::ReportOnMainThread, aReason);
789
MOZ_ALWAYS_TRUE(
790
NS_SUCCEEDED(workerPrivate->DispatchToMainThread(runnable.forget())));
791
}
792
793
void ReportOnMainThread(uint16_t aReason) {
794
MOZ_ASSERT(NS_IsMainThread());
795
nsCOMPtr<nsIPushErrorReporter> reporter =
796
do_GetService("@mozilla.org/push/Service;1");
797
if (reporter) {
798
nsresult rv = reporter->ReportDeliveryError(mMessageId, aReason);
799
Unused << NS_WARN_IF(NS_FAILED(rv));
800
}
801
}
802
};
803
804
class SendPushEventRunnable final
805
: public ExtendableFunctionalEventWorkerRunnable {
806
nsString mMessageId;
807
Maybe<nsTArray<uint8_t>> mData;
808
809
public:
810
SendPushEventRunnable(
811
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
812
const nsAString& aMessageId, const Maybe<nsTArray<uint8_t>>& aData,
813
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> aRegistration)
814
: ExtendableFunctionalEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken,
815
aRegistration),
816
mMessageId(aMessageId),
817
mData(aData) {
818
MOZ_ASSERT(NS_IsMainThread());
819
MOZ_ASSERT(aWorkerPrivate);
820
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
821
}
822
823
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
824
MOZ_ASSERT(aWorkerPrivate);
825
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
826
827
RefPtr<PushErrorReporter> errorReporter =
828
new PushErrorReporter(aWorkerPrivate, mMessageId);
829
830
PushEventInit pei;
831
if (mData) {
832
const nsTArray<uint8_t>& bytes = mData.ref();
833
JSObject* data =
834
Uint8Array::Create(aCx, bytes.Length(), bytes.Elements());
835
if (!data) {
836
errorReporter->Report();
837
return false;
838
}
839
pei.mData.Construct().SetAsArrayBufferView().Init(data);
840
}
841
pei.mBubbles = false;
842
pei.mCancelable = false;
843
844
ErrorResult result;
845
RefPtr<PushEvent> event = PushEvent::Constructor(
846
globalObj, NS_LITERAL_STRING("push"), pei, result);
847
if (NS_WARN_IF(result.Failed())) {
848
result.SuppressException();
849
errorReporter->Report();
850
return false;
851
}
852
event->SetTrusted(true);
853
854
nsresult rv = DispatchExtendableEventOnWorkerScope(
855
aCx, aWorkerPrivate->GlobalScope(), event, errorReporter);
856
if (NS_FAILED(rv)) {
857
// We don't cancel WorkerPrivate when catching an excetpion.
858
errorReporter->Report(nsIPushErrorReporter::DELIVERY_UNCAUGHT_EXCEPTION);
859
}
860
861
return true;
862
}
863
};
864
865
class SendPushSubscriptionChangeEventRunnable final
866
: public ExtendableEventWorkerRunnable {
867
public:
868
explicit SendPushSubscriptionChangeEventRunnable(
869
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken)
870
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken) {
871
MOZ_ASSERT(NS_IsMainThread());
872
MOZ_ASSERT(aWorkerPrivate);
873
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
874
}
875
876
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
877
MOZ_ASSERT(aWorkerPrivate);
878
879
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
880
881
ExtendableEventInit init;
882
init.mBubbles = false;
883
init.mCancelable = false;
884
885
RefPtr<ExtendableEvent> event = ExtendableEvent::Constructor(
886
target, NS_LITERAL_STRING("pushsubscriptionchange"), init);
887
888
event->SetTrusted(true);
889
890
DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
891
event, nullptr);
892
893
return true;
894
}
895
};
896
897
} // anonymous namespace
898
899
nsresult ServiceWorkerPrivate::SendPushEvent(
900
const nsAString& aMessageId, const Maybe<nsTArray<uint8_t>>& aData,
901
ServiceWorkerRegistrationInfo* aRegistration) {
902
MOZ_ASSERT(NS_IsMainThread());
903
904
if (mInner) {
905
return mInner->SendPushEvent(aRegistration, aMessageId, aData);
906
}
907
908
nsresult rv = SpawnWorkerIfNeeded(PushEvent);
909
NS_ENSURE_SUCCESS(rv, rv);
910
911
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
912
913
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
914
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
915
"ServiceWorkerRegistrationInfoProxy", aRegistration, false));
916
917
RefPtr<WorkerRunnable> r = new SendPushEventRunnable(
918
mWorkerPrivate, token, aMessageId, aData, regInfo);
919
920
if (mInfo->State() == ServiceWorkerState::Activating) {
921
mPendingFunctionalEvents.AppendElement(r.forget());
922
return NS_OK;
923
}
924
925
MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
926
927
if (NS_WARN_IF(!r->Dispatch())) {
928
return NS_ERROR_FAILURE;
929
}
930
931
return NS_OK;
932
}
933
934
nsresult ServiceWorkerPrivate::SendPushSubscriptionChangeEvent() {
935
MOZ_ASSERT(NS_IsMainThread());
936
937
if (mInner) {
938
return mInner->SendPushSubscriptionChangeEvent();
939
}
940
941
nsresult rv = SpawnWorkerIfNeeded(PushSubscriptionChangeEvent);
942
NS_ENSURE_SUCCESS(rv, rv);
943
944
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
945
RefPtr<WorkerRunnable> r =
946
new SendPushSubscriptionChangeEventRunnable(mWorkerPrivate, token);
947
if (NS_WARN_IF(!r->Dispatch())) {
948
return NS_ERROR_FAILURE;
949
}
950
951
return NS_OK;
952
}
953
954
namespace {
955
956
class AllowWindowInteractionHandler final : public ExtendableEventCallback,
957
public nsITimerCallback,
958
public nsINamed {
959
nsCOMPtr<nsITimer> mTimer;
960
RefPtr<StrongWorkerRef> mWorkerRef;
961
962
~AllowWindowInteractionHandler() {
963
// We must either fail to initialize or call ClearWindowAllowed.
964
MOZ_DIAGNOSTIC_ASSERT(!mTimer);
965
MOZ_DIAGNOSTIC_ASSERT(!mWorkerRef);
966
}
967
968
void ClearWindowAllowed(WorkerPrivate* aWorkerPrivate) {
969
MOZ_ASSERT(aWorkerPrivate);
970
aWorkerPrivate->AssertIsOnWorkerThread();
971
972
if (!mTimer) {
973
return;
974
}
975
976
// XXXcatalinb: This *might* be executed after the global was unrooted, in
977
// which case GlobalScope() will return null. Making the check here just
978
// to be safe.
979
WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
980
if (!globalScope) {
981
return;
982
}
983
984
globalScope->ConsumeWindowInteraction();
985
mTimer->Cancel();
986
mTimer = nullptr;
987
988
mWorkerRef = nullptr;
989
}
990
991
void StartClearWindowTimer(WorkerPrivate* aWorkerPrivate) {
992
MOZ_ASSERT(aWorkerPrivate);
993
aWorkerPrivate->AssertIsOnWorkerThread();
994
MOZ_ASSERT(!mTimer);
995
996
nsresult rv;
997
nsCOMPtr<nsITimer> timer =
998
NS_NewTimer(aWorkerPrivate->ControlEventTarget());
999
if (NS_WARN_IF(!timer)) {
1000
return;
1001
}
1002
1003
MOZ_ASSERT(!mWorkerRef);
1004
RefPtr<AllowWindowInteractionHandler> self = this;
1005
mWorkerRef = StrongWorkerRef::Create(
1006
aWorkerPrivate, "AllowWindowInteractionHandler", [self]() {
1007
// We could try to hold the worker alive until the timer fires, but
1008
// other APIs are not likely to work in this partially shutdown state.
1009
// We might as well let the worker thread exit.
1010
self->ClearWindowAllowed(self->mWorkerRef->Private());
1011
});
1012
1013
if (!mWorkerRef) {
1014
return;
1015
}
1016
1017
aWorkerPrivate->GlobalScope()->AllowWindowInteraction();
1018
timer.swap(mTimer);
1019
1020
// We swap first and then initialize the timer so that even if initializing
1021
// fails, we still clean the busy count and interaction count correctly.
1022
// The timer can't be initialized before modifying the busy count since the
1023
// timer thread could run and call the timeout but the worker may
1024
// already be terminating and modifying the busy count could fail.
1025
rv = mTimer->InitWithCallback(this, gDOMDisableOpenClickDelay,
1026
nsITimer::TYPE_ONE_SHOT);
1027
if (NS_WARN_IF(NS_FAILED(rv))) {
1028
ClearWindowAllowed(aWorkerPrivate);
1029
return;
1030
}
1031
}
1032
1033
// nsITimerCallback virtual methods
1034
NS_IMETHOD
1035
Notify(nsITimer* aTimer) override {
1036
MOZ_DIAGNOSTIC_ASSERT(mTimer == aTimer);
1037
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
1038
ClearWindowAllowed(workerPrivate);
1039
return NS_OK;
1040
}
1041
1042
// nsINamed virtual methods
1043
NS_IMETHOD
1044
GetName(nsACString& aName) override {
1045
aName.AssignLiteral("AllowWindowInteractionHandler");
1046
return NS_OK;
1047
}
1048
1049
public:
1050
NS_DECL_THREADSAFE_ISUPPORTS
1051
1052
explicit AllowWindowInteractionHandler(WorkerPrivate* aWorkerPrivate) {
1053
StartClearWindowTimer(aWorkerPrivate);
1054
}
1055
1056
void FinishedWithResult(ExtendableEventResult /* aResult */) override {
1057
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
1058
ClearWindowAllowed(workerPrivate);
1059
}
1060
};
1061
1062
NS_IMPL_ISUPPORTS(AllowWindowInteractionHandler, nsITimerCallback, nsINamed)
1063
1064
class SendNotificationEventRunnable final
1065
: public ExtendableEventWorkerRunnable {
1066
const nsString mEventName;
1067
const nsString mID;
1068
const nsString mTitle;
1069
const nsString mDir;
1070
const nsString mLang;
1071
const nsString mBody;
1072
const nsString mTag;
1073
const nsString mIcon;
1074
const nsString mData;
1075
const nsString mBehavior;
1076
const nsString mScope;
1077
1078
public:
1079
SendNotificationEventRunnable(WorkerPrivate* aWorkerPrivate,
1080
KeepAliveToken* aKeepAliveToken,
1081
const nsAString& aEventName,
1082
const nsAString& aID, const nsAString& aTitle,
1083
const nsAString& aDir, const nsAString& aLang,
1084
const nsAString& aBody, const nsAString& aTag,
1085
const nsAString& aIcon, const nsAString& aData,
1086
const nsAString& aBehavior,
1087
const nsAString& aScope)
1088
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
1089
mEventName(aEventName),
1090
mID(aID),
1091
mTitle(aTitle),
1092
mDir(aDir),
1093
mLang(aLang),
1094
mBody(aBody),
1095
mTag(aTag),
1096
mIcon(aIcon),
1097
mData(aData),
1098
mBehavior(aBehavior),
1099
mScope(aScope) {
1100
MOZ_ASSERT(NS_IsMainThread());
1101
MOZ_ASSERT(aWorkerPrivate);
1102
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
1103
}
1104
1105
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
1106
MOZ_ASSERT(aWorkerPrivate);
1107
1108
RefPtr<EventTarget> target = do_QueryObject(aWorkerPrivate->GlobalScope());
1109
1110
ErrorResult result;
1111
RefPtr<Notification> notification = Notification::ConstructFromFields(
1112
aWorkerPrivate->GlobalScope(), mID, mTitle, mDir, mLang, mBody, mTag,
1113
mIcon, mData, mScope, result);
1114
if (NS_WARN_IF(result.Failed())) {
1115
return false;
1116
}
1117
1118
NotificationEventInit nei;
1119
nei.mNotification = notification;
1120
nei.mBubbles = false;
1121
nei.mCancelable = false;
1122
1123
RefPtr<NotificationEvent> event =
1124
NotificationEvent::Constructor(target, mEventName, nei);
1125
1126
event->SetTrusted(true);
1127
1128
RefPtr<AllowWindowInteractionHandler> allowWindowInteraction;
1129
if (mEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
1130
allowWindowInteraction =
1131
new AllowWindowInteractionHandler(aWorkerPrivate);
1132
}
1133
1134
nsresult rv = DispatchExtendableEventOnWorkerScope(
1135
aCx, aWorkerPrivate->GlobalScope(), event, allowWindowInteraction);
1136
// Don't reject when catching an exception
1137
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION &&
1138
allowWindowInteraction) {
1139
allowWindowInteraction->FinishedWithResult(Rejected);
1140
}
1141
1142
return true;
1143
}
1144
};
1145
1146
} // namespace
1147
1148
nsresult ServiceWorkerPrivate::SendNotificationEvent(
1149
const nsAString& aEventName, const nsAString& aID, const nsAString& aTitle,
1150
const nsAString& aDir, const nsAString& aLang, const nsAString& aBody,
1151
const nsAString& aTag, const nsAString& aIcon, const nsAString& aData,
1152
const nsAString& aBehavior, const nsAString& aScope) {
1153
MOZ_ASSERT(NS_IsMainThread());
1154
1155
WakeUpReason why;
1156
if (aEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
1157
why = NotificationClickEvent;
1158
gDOMDisableOpenClickDelay =
1159
Preferences::GetInt("dom.serviceWorkers.disable_open_click_delay");
1160
} else if (aEventName.EqualsLiteral(NOTIFICATION_CLOSE_EVENT_NAME)) {
1161
why = NotificationCloseEvent;
1162
} else {
1163
MOZ_ASSERT_UNREACHABLE("Invalid notification event name");
1164
return NS_ERROR_FAILURE;
1165
}
1166
1167
if (mInner) {
1168
return mInner->SendNotificationEvent(aEventName, aID, aTitle, aDir, aLang,
1169
aBody, aTag, aIcon, aData, aBehavior,
1170
aScope, gDOMDisableOpenClickDelay);
1171
}
1172
1173
nsresult rv = SpawnWorkerIfNeeded(why);
1174
NS_ENSURE_SUCCESS(rv, rv);
1175
1176
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1177
1178
RefPtr<WorkerRunnable> r = new SendNotificationEventRunnable(
1179
mWorkerPrivate, token, aEventName, aID, aTitle, aDir, aLang, aBody, aTag,
1180
aIcon, aData, aBehavior, aScope);
1181
if (NS_WARN_IF(!r->Dispatch())) {
1182
return NS_ERROR_FAILURE;
1183
}
1184
1185
return NS_OK;
1186
}
1187
1188
namespace {
1189
1190
// Inheriting ExtendableEventWorkerRunnable so that the worker is not terminated
1191
// while handling the fetch event, though that's very unlikely.
1192
class FetchEventRunnable : public ExtendableFunctionalEventWorkerRunnable,
1193
public nsIHttpHeaderVisitor {
1194
nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
1195
const nsCString mScriptSpec;
1196
nsTArray<nsCString> mHeaderNames;
1197
nsTArray<nsCString> mHeaderValues;
1198
nsCString mSpec;
1199
nsCString mFragment;
1200
nsCString mMethod;
1201
nsString mClientId;
1202
nsString mResultingClientId;
1203
bool mIsReload;
1204
bool mMarkLaunchServiceWorkerEnd;
1205
RequestCache mCacheMode;
1206
RequestMode mRequestMode;
1207
RequestRedirect mRequestRedirect;
1208
RequestCredentials mRequestCredentials;
1209
nsContentPolicyType mContentPolicyType;
1210
nsCOMPtr<nsIInputStream> mUploadStream;
1211
int64_t mUploadStreamContentLength;
1212
nsString mReferrer;
1213
ReferrerPolicy mReferrerPolicy;
1214
nsString mIntegrity;
1215
const bool mIsNonSubresourceRequest;
1216
1217
public:
1218
FetchEventRunnable(
1219
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
1220
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
1221
// CSP checks might require the worker script spec
1222
// later on.
1223
const nsACString& aScriptSpec,
1224
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
1225
const nsAString& aClientId, const nsAString& aResultingClientId,
1226
bool aIsReload, bool aMarkLaunchServiceWorkerEnd,
1227
bool aIsNonSubresourceRequest)
1228
: ExtendableFunctionalEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken,
1229
aRegistration),
1230
mInterceptedChannel(aChannel),
1231
mScriptSpec(aScriptSpec),
1232
mClientId(aClientId),
1233
mResultingClientId(aResultingClientId),
1234
mIsReload(aIsReload),
1235
mMarkLaunchServiceWorkerEnd(aMarkLaunchServiceWorkerEnd),
1236
mCacheMode(RequestCache::Default),
1237
mRequestMode(RequestMode::No_cors),
1238
mRequestRedirect(RequestRedirect::Follow)
1239
// By default we set it to same-origin since normal HTTP fetches always
1240
// send credentials to same-origin websites unless explicitly forbidden.
1241
,
1242
mRequestCredentials(RequestCredentials::Same_origin),
1243
mContentPolicyType(nsIContentPolicy::TYPE_INVALID),
1244
mUploadStreamContentLength(-1),
1245
mReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR)),
1246
mReferrerPolicy(ReferrerPolicy::_empty),
1247
mIsNonSubresourceRequest(aIsNonSubresourceRequest) {
1248
MOZ_ASSERT(aWorkerPrivate);
1249
}
1250
1251
NS_DECL_ISUPPORTS_INHERITED
1252
1253
NS_IMETHOD
1254
VisitHeader(const nsACString& aHeader, const nsACString& aValue) override {
1255
mHeaderNames.AppendElement(aHeader);
1256
mHeaderValues.AppendElement(aValue);
1257
return NS_OK;
1258
}
1259
1260
nsresult Init() {
1261
MOZ_ASSERT(NS_IsMainThread());
1262
nsCOMPtr<nsIChannel> channel;
1263
nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
1264
NS_ENSURE_SUCCESS(rv, rv);
1265
1266
nsCOMPtr<nsIURI> uri;
1267
rv = mInterceptedChannel->GetSecureUpgradedChannelURI(getter_AddRefs(uri));
1268
NS_ENSURE_SUCCESS(rv, rv);
1269
1270
// Normally we rely on the Request constructor to strip the fragment, but
1271
// when creating the FetchEvent we bypass the constructor. So strip the
1272
// fragment manually here instead. We can't do it later when we create
1273
// the Request because that code executes off the main thread.
1274
nsCOMPtr<nsIURI> uriNoFragment;
1275
rv = NS_GetURIWithoutRef(uri, getter_AddRefs(uriNoFragment));
1276
NS_ENSURE_SUCCESS(rv, rv);
1277
rv = uriNoFragment->GetSpec(mSpec);
1278
NS_ENSURE_SUCCESS(rv, rv);
1279
rv = uri->GetRef(mFragment);
1280
NS_ENSURE_SUCCESS(rv, rv);
1281
1282
uint32_t loadFlags;
1283
rv = channel->GetLoadFlags(&loadFlags);
1284
NS_ENSURE_SUCCESS(rv, rv);
1285
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
1286
mContentPolicyType = loadInfo->InternalContentPolicyType();
1287
1288
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel);
1289
MOZ_ASSERT(httpChannel, "How come we don't have an HTTP channel?");
1290
1291
mReferrerPolicy = ReferrerPolicy::_empty;
1292
mReferrer = EmptyString();
1293
nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo();
1294
if (referrerInfo) {
1295
mReferrerPolicy = referrerInfo->ReferrerPolicy();
1296
Unused << referrerInfo->GetComputedReferrerSpec(mReferrer);
1297
}
1298
1299
rv = httpChannel->GetRequestMethod(mMethod);
1300
NS_ENSURE_SUCCESS(rv, rv);
1301
1302
nsCOMPtr<nsIHttpChannelInternal> internalChannel =
1303
do_QueryInterface(httpChannel);
1304
NS_ENSURE_TRUE(internalChannel, NS_ERROR_NOT_AVAILABLE);
1305
1306
mRequestMode = InternalRequest::MapChannelToRequestMode(channel);
1307
1308
// This is safe due to static_asserts in ServiceWorkerManager.cpp.
1309
uint32_t redirectMode;
1310
rv = internalChannel->GetRedirectMode(&redirectMode);
1311
MOZ_ASSERT(NS_SUCCEEDED(rv));
1312
mRequestRedirect = static_cast<RequestRedirect>(redirectMode);
1313
1314
// This is safe due to static_asserts in ServiceWorkerManager.cpp.
1315
uint32_t cacheMode;
1316
rv = internalChannel->GetFetchCacheMode(&cacheMode);
1317
MOZ_ASSERT(NS_SUCCEEDED(rv));
1318
mCacheMode = static_cast<RequestCache>(cacheMode);
1319
1320
rv = internalChannel->GetIntegrityMetadata(mIntegrity);
1321
MOZ_ASSERT(NS_SUCCEEDED(rv));
1322
1323
mRequestCredentials =
1324
InternalRequest::MapChannelToRequestCredentials(channel);
1325
1326
rv = httpChannel->VisitNonDefaultRequestHeaders(this);
1327
NS_ENSURE_SUCCESS(rv, rv);
1328
1329
nsCOMPtr<nsIUploadChannel2> uploadChannel = do_QueryInterface(httpChannel);
1330
if (uploadChannel) {
1331
MOZ_ASSERT(!mUploadStream);
1332
nsCOMPtr<nsIInputStream> uploadStream;
1333
rv = uploadChannel->CloneUploadStream(&mUploadStreamContentLength,
1334
getter_AddRefs(uploadStream));
1335
NS_ENSURE_SUCCESS(rv, rv);
1336
mUploadStream = uploadStream;
1337
}
1338
1339
return NS_OK;
1340
}
1341
1342
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
1343
MOZ_ASSERT(aWorkerPrivate);
1344
1345
if (mMarkLaunchServiceWorkerEnd) {
1346
mInterceptedChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
1347
1348
// A probe to measure sw launch time for telemetry.
1349
TimeStamp launchStartTime = TimeStamp();
1350
mInterceptedChannel->GetLaunchServiceWorkerStart(&launchStartTime);
1351
1352
TimeStamp launchEndTime = TimeStamp();
1353
mInterceptedChannel->GetLaunchServiceWorkerEnd(&launchEndTime);
1354
Telemetry::AccumulateTimeDelta(Telemetry::SERVICE_WORKER_LAUNCH_TIME,
1355
launchStartTime, launchEndTime);
1356
}
1357
1358
mInterceptedChannel->SetDispatchFetchEventEnd(TimeStamp::Now());
1359
return DispatchFetchEvent(aCx, aWorkerPrivate);
1360
}
1361
1362
nsresult Cancel() override {
1363
nsCOMPtr<nsIRunnable> runnable = new ResumeRequest(mInterceptedChannel);
1364
if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable))) {
1365
NS_WARNING("Failed to resume channel on FetchEventRunnable::Cancel()!\n");
1366
}
1367
WorkerRunnable::Cancel();
1368
return NS_OK;
1369
}
1370
1371
private:
1372
~FetchEventRunnable() {}
1373
1374
class ResumeRequest final : public Runnable {
1375
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
1376
1377
public:
1378
explicit ResumeRequest(
1379
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel)
1380
: Runnable("dom::FetchEventRunnable::ResumeRequest"),
1381
mChannel(aChannel) {
1382
mChannel->SetFinishResponseStart(TimeStamp::Now());
1383
}
1384
1385
NS_IMETHOD Run() override {
1386
MOZ_ASSERT(NS_IsMainThread());
1387
1388
TimeStamp timeStamp = TimeStamp::Now();
1389
mChannel->SetHandleFetchEventEnd(timeStamp);
1390
mChannel->SetChannelResetEnd(timeStamp);
1391
mChannel->SaveTimeStamps();
1392
1393
nsresult rv = mChannel->ResetInterception();
1394
if (NS_FAILED(rv)) {
1395
NS_WARNING("Failed to resume intercepted network request");
1396
mChannel->CancelInterception(rv);
1397
}
1398
return rv;
1399
}
1400
};
1401
1402
bool DispatchFetchEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
1403
MOZ_ASSERT(aCx);
1404
MOZ_ASSERT(aWorkerPrivate);
1405
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
1406
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
1407
1408
RefPtr<InternalHeaders> internalHeaders =
1409
new InternalHeaders(HeadersGuardEnum::Request);
1410
MOZ_ASSERT(mHeaderNames.Length() == mHeaderValues.Length());
1411
for (uint32_t i = 0; i < mHeaderNames.Length(); i++) {
1412
ErrorResult result;
1413
internalHeaders->Set(mHeaderNames[i], mHeaderValues[i], result);
1414
if (NS_WARN_IF(result.Failed())) {
1415
result.SuppressException();
1416
return false;
1417
}
1418
}
1419
1420
ErrorResult result;
1421
internalHeaders->SetGuard(HeadersGuardEnum::Immutable, result);
1422
if (NS_WARN_IF(result.Failed())) {
1423
result.SuppressException();
1424
return false;
1425
}
1426
RefPtr<InternalRequest> internalReq = new InternalRequest(
1427
mSpec, mFragment, mMethod, internalHeaders.forget(), mCacheMode,
1428
mRequestMode, mRequestRedirect, mRequestCredentials, mReferrer,
1429
mReferrerPolicy, mContentPolicyType, mIntegrity);
1430
internalReq->SetBody(mUploadStream, mUploadStreamContentLength);
1431
1432
nsCOMPtr<nsIChannel> channel;
1433
nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
1434
NS_ENSURE_SUCCESS(rv, false);
1435
1436
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(channel);
1437
if (cic && !cic->PreferredAlternativeDataTypes().IsEmpty()) {
1438
// TODO: the internal request probably needs all the preferred types.
1439
nsAutoCString alternativeDataType;
1440
alternativeDataType.Assign(
1441
cic->PreferredAlternativeDataTypes()[0].type());
1442
internalReq->SetPreferredAlternativeDataType(alternativeDataType);
1443
}
1444
1445
nsCOMPtr<nsIGlobalObject> global =
1446
do_QueryInterface(globalObj.GetAsSupports());
1447
if (NS_WARN_IF(!global)) {
1448
return false;
1449
}
1450
1451
// TODO This request object should be created with a AbortSignal object
1452
// which should be aborted if the loading is aborted. See bug 1394102.
1453
RefPtr<Request> request = new Request(global, internalReq, nullptr);
1454
1455
MOZ_ASSERT_IF(internalReq->IsNavigationRequest(),
1456
request->Redirect() == RequestRedirect::Manual);
1457
1458
RootedDictionary<FetchEventInit> init(aCx);
1459
init.mRequest = request;
1460
init.mBubbles = false;
1461
init.mCancelable = true;
1462
// Only expose the FetchEvent.clientId on subresource requests for now.
1463
// Once we implement .targetClientId we can then start exposing .clientId
1464
// on non-subresource requests as well. See bug 1487534.
1465
if (!mClientId.IsEmpty() && !internalReq->IsNavigationRequest()) {
1466
init.mClientId = mClientId;
1467
}
1468
1469
/*
1471
*
1472
* "If request is a non-subresource request and request’s
1473
* destination is not "report", initialize e’s resultingClientId attribute
1474
* to reservedClient’s [resultingClient's] id, and to the empty string
1475
* otherwise." (Step 18.8)
1476
*/
1477
if (!mResultingClientId.IsEmpty() && mIsNonSubresourceRequest &&
1478
internalReq->Destination() != RequestDestination::Report) {
1479
init.mResultingClientId = mResultingClientId;
1480
}
1481
1482
init.mIsReload = mIsReload;
1483
RefPtr<FetchEvent> event =
1484
FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init);
1485
1486
event->PostInit(mInterceptedChannel, mRegistration, mScriptSpec);
1487
event->SetTrusted(true);
1488
1489
mInterceptedChannel->SetHandleFetchEventStart(TimeStamp::Now());
1490
1491
nsresult rv2 = DispatchExtendableEventOnWorkerScope(
1492
aCx, aWorkerPrivate->GlobalScope(), event, nullptr);
1493
if ((NS_WARN_IF(NS_FAILED(rv2)) &&
1494
rv2 != NS_ERROR_XPC_JS_THREW_EXCEPTION) ||
1495
!event->WaitToRespond()) {
1496
nsCOMPtr<nsIRunnable> runnable;
1497
MOZ_ASSERT(!aWorkerPrivate->UsesSystemPrincipal(),
1498
"We don't support system-principal serviceworkers");
1499
if (event->DefaultPrevented(CallerType::NonSystem)) {
1500
runnable = new CancelChannelRunnable(mInterceptedChannel, mRegistration,
1501
NS_ERROR_INTERCEPTION_FAILED);
1502
} else {
1503
runnable = new ResumeRequest(mInterceptedChannel);
1504
}
1505
1506
MOZ_ALWAYS_SUCCEEDS(
1507
mWorkerPrivate->DispatchToMainThread(runnable.forget()));
1508
}
1509
1510
return true;
1511
}
1512
};
1513
1514
NS_IMPL_ISUPPORTS_INHERITED(FetchEventRunnable, WorkerRunnable,
1515
nsIHttpHeaderVisitor)
1516
1517
} // anonymous namespace
1518
1519
nsresult ServiceWorkerPrivate::SendFetchEvent(
1520
nsIInterceptedChannel* aChannel, nsILoadGroup* aLoadGroup,
1521
const nsAString& aClientId, const nsAString& aResultingClientId,
1522
bool aIsReload) {
1523
MOZ_ASSERT(NS_IsMainThread());
1524
1525
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
1526
if (NS_WARN_IF(!mInfo || !swm)) {
1527
return NS_ERROR_FAILURE;
1528
}
1529
1530
nsCOMPtr<nsIChannel> channel;
1531
nsresult rv = aChannel->GetChannel(getter_AddRefs(channel));
1532
NS_ENSURE_SUCCESS(rv, rv);
1533
bool isNonSubresourceRequest =
1534
nsContentUtils::IsNonSubresourceRequest(channel);
1535
1536
RefPtr<ServiceWorkerRegistrationInfo> registration;
1537
if (isNonSubresourceRequest) {
1538
registration = swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
1539
} else {
1540
nsCOMPtr<nsILoadInfo> loadInfo;
1541
channel->GetLoadInfo(getter_AddRefs(loadInfo));
1542
1543
// We'll check for a null registration below rather than an error code here.
1544
Unused << swm->GetClientRegistration(loadInfo->GetClientInfo().ref(),
1545
getter_AddRefs(registration));
1546
}
1547
1548
// Its possible the registration is removed between starting the interception
1549
// and actually dispatching the fetch event. In these cases we simply
1550
// want to restart the original network request. Since this is a normal
1551
// condition we handle the reset here instead of returning an error which
1552
// would in turn trigger a console report.
1553
if (!registration) {
1554
nsresult rv = aChannel->ResetInterception();
1555
if (NS_FAILED(rv)) {
1556
NS_WARNING("Failed to resume intercepted network request");
1557
aChannel->CancelInterception(rv);
1558
}
1559
return NS_OK;
1560
}
1561
1562
// Handle Fetch algorithm - step 16. If the service worker didn't register
1563
// any fetch event handlers, then abort the interception and maybe trigger
1564
// the soft update algorithm.
1565
if (!mInfo->HandlesFetch()) {
1566
nsresult rv = aChannel->ResetInterception();
1567
if (NS_FAILED(rv)) {
1568
NS_WARNING("Failed to resume intercepted network request");
1569
aChannel->CancelInterception(rv);
1570
}
1571
1572
// Trigger soft updates if necessary.
1573
registration->MaybeScheduleTimeCheckAndUpdate();
1574
1575
return NS_OK;
1576
}
1577
1578
if (mInner) {
1579
return mInner->SendFetchEvent(std::move(registration), aChannel, aClientId,
1580
aResultingClientId, aIsReload);
1581
}
1582
1583
aChannel->SetLaunchServiceWorkerStart(TimeStamp::Now());
1584
aChannel->SetDispatchFetchEventStart(TimeStamp::Now());
1585
1586
bool newWorkerCreated = false;
1587
rv = SpawnWorkerIfNeeded(FetchEvent, &newWorkerCreated, aLoadGroup);
1588
NS_ENSURE_SUCCESS(rv, rv);
1589
1590
if (!newWorkerCreated) {
1591
aChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
1592
}
1593
1594
nsMainThreadPtrHandle<nsIInterceptedChannel> handle(
1595
new nsMainThreadPtrHolder<nsIInterceptedChannel>("nsIInterceptedChannel",
1596
aChannel, false));
1597
1598
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
1599
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
1600
"ServiceWorkerRegistrationInfoProxy", registration, false));
1601
1602
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1603
1604
RefPtr<FetchEventRunnable> r = new FetchEventRunnable(
1605
mWorkerPrivate, token, handle, mInfo->ScriptSpec(), regInfo, aClientId,
1606
aResultingClientId, aIsReload, newWorkerCreated, isNonSubresourceRequest);
1607
rv = r->Init();
1608
if (NS_WARN_IF(NS_FAILED(rv))) {
1609
return rv;
1610
}
1611
1612
if (mInfo->State() == ServiceWorkerState::Activating) {
1613
mPendingFunctionalEvents.AppendElement(r.forget());
1614
return NS_OK;
1615
}
1616
1617
MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
1618
1619
if (NS_WARN_IF(!r->Dispatch())) {
1620
return NS_ERROR_FAILURE;
1621
}
1622
1623
return NS_OK;
1624
}
1625
1626
nsresult ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
1627
bool* aNewWorkerCreated,
1628
nsILoadGroup* aLoadGroup) {
1629
MOZ_ASSERT(NS_IsMainThread());
1630
MOZ_ASSERT(!mInner);
1631
1632
// Defaults to no new worker created, but if there is one, we'll set the value
1633
// to true at the end of this function.
1634
if (aNewWorkerCreated) {
1635
*aNewWorkerCreated = false;
1636
}
1637
1638
// If the worker started shutting down on itself we may have a stale
1639
// reference here. Invoke our termination code to clean it out.
1640
if (mWorkerPrivate && mWorkerPrivate->ParentStatusProtected() > Running) {
1641
TerminateWorker();
1642
MOZ_DIAGNOSTIC_ASSERT(!mWorkerPrivate);
1643
}
1644
1645
if (mWorkerPrivate) {
1646
// If we have a load group here then use it to update the service worker
1647
// load group. This was added when we needed the load group's tab child
1648
// to pass some security checks. Those security checks are gone, though,
1649
// and we could possibly remove this now. For now we just do it
1650
// opportunistically. When the service worker is running in a separate
1651
// process from the client that initiated the intercepted channel, then
1652
// the load group will be nullptr. UpdateOverrideLoadGroup ignores nullptr
1653
// load groups.
1654
mWorkerPrivate->UpdateOverridenLoadGroup(aLoadGroup);
1655
RenewKeepAliveToken(aWhy);
1656
1657
return NS_OK;
1658
}
1659
1660
// Sanity check: mSupportsArray should be empty if we're about to
1661
// spin up a new worker.
1662
MOZ_ASSERT(mSupportsArray.IsEmpty());
1663
1664
if (NS_WARN_IF(!mInfo)) {
1665
NS_WARNING("Trying to wake up a dead service worker.");
1666
return NS_ERROR_FAILURE;
1667
}
1668
1669
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
1670
NS_ENSURE_TRUE(swm, NS_ERROR_FAILURE);
1671
1672
RefPtr<ServiceWorkerRegistrationInfo> reg =
1673
swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
1674
NS_ENSURE_TRUE(reg, NS_ERROR_FAILURE);
1675
1676
// TODO(catalinb): Bug 1192138 - Add telemetry for service worker wake-ups.
1677
1678
// Ensure that the IndexedDatabaseManager is initialized
1679
Unused << NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate());
1680
1681
WorkerLoadInfo info;
1682
nsresult rv = NS_NewURI(getter_AddRefs(info.mBaseURI), mInfo->ScriptSpec());
1683
1684
if (NS_WARN_IF(NS_FAILED(rv))) {
1685
return rv;
1686
}
1687
1688
info.mResolvedScriptURI = info.mBaseURI;
1689
MOZ_ASSERT(!mInfo->CacheName().IsEmpty());
1690
info.mServiceWorkerCacheName = mInfo->CacheName();
1691
1692
info.mServiceWorkerDescriptor.emplace(mInfo->Descriptor());
1693
info.mServiceWorkerRegistrationDescriptor.emplace(reg->Descriptor());
1694
1695
info.mLoadGroup = aLoadGroup;
1696
1697
// If we are loading a script for a ServiceWorker then we must not
1698
// try to intercept it. If the interception matches the current
1699
// ServiceWorker's scope then we could deadlock the load.
1700
info.mLoadFlags =
1701
mInfo->GetImportsLoadFlags() | nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
1702
1703
rv = info.mBaseURI->GetHost(info.mDomain);
1704
if (NS_WARN_IF(NS_FAILED(rv))) {
1705
return rv;
1706
}
1707
1708
info.mPrincipal = mInfo->Principal();
1709
info.mLoadingPrincipal = info.mPrincipal;
1710
// StoragePrincipal for ServiceWorkers is equal to mPrincipal because, at the
1711
// moment, ServiceWorkers are not exposed in partitioned contexts.