Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/ServiceWorkerPrivate.cpp
Line
Count
Source (jump to first uncovered line)
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 "ServiceWorkerCloneData.h"
10
#include "ServiceWorkerManager.h"
11
#include "nsContentUtils.h"
12
#include "nsICacheInfoChannel.h"
13
#include "nsIHttpChannelInternal.h"
14
#include "nsIHttpHeaderVisitor.h"
15
#include "nsINamed.h"
16
#include "nsINetworkInterceptController.h"
17
#include "nsIPushErrorReporter.h"
18
#include "nsISupportsImpl.h"
19
#include "nsITimedChannel.h"
20
#include "nsIUploadChannel2.h"
21
#include "nsNetUtil.h"
22
#include "nsProxyRelease.h"
23
#include "nsQueryObject.h"
24
#include "nsStreamUtils.h"
25
#include "nsStringStream.h"
26
#include "mozilla/Assertions.h"
27
#include "mozilla/CycleCollectedJSContext.h" // for MicroTaskRunnable
28
#include "mozilla/JSObjectHolder.h"
29
#include "mozilla/dom/Client.h"
30
#include "mozilla/dom/ClientIPCTypes.h"
31
#include "mozilla/dom/FetchUtil.h"
32
#include "mozilla/dom/IndexedDatabaseManager.h"
33
#include "mozilla/dom/InternalHeaders.h"
34
#include "mozilla/dom/NotificationEvent.h"
35
#include "mozilla/dom/PromiseNativeHandler.h"
36
#include "mozilla/dom/PushEventBinding.h"
37
#include "mozilla/dom/RequestBinding.h"
38
#include "mozilla/dom/WorkerDebugger.h"
39
#include "mozilla/dom/WorkerRef.h"
40
#include "mozilla/dom/WorkerRunnable.h"
41
#include "mozilla/dom/WorkerScope.h"
42
#include "mozilla/dom/ipc/StructuredCloneData.h"
43
#include "mozilla/StaticPrefs.h"
44
#include "mozilla/Unused.h"
45
46
using namespace mozilla;
47
using namespace mozilla::dom;
48
49
namespace mozilla {
50
namespace dom {
51
52
using mozilla::ipc::PrincipalInfo;
53
54
NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(ServiceWorkerPrivate)
55
NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(ServiceWorkerPrivate)
56
NS_IMPL_CYCLE_COLLECTION(ServiceWorkerPrivate, mSupportsArray)
57
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ServiceWorkerPrivate, AddRef)
58
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ServiceWorkerPrivate, Release)
59
60
// Tracks the "dom.serviceWorkers.disable_open_click_delay" preference.  Modified
61
// on main thread, read on worker threads.
62
// It is updated every time a "notificationclick" event is dispatched. While
63
// this is done without synchronization, at the worst, the thread will just get
64
// an older value within which a popup is allowed to be displayed, which will
65
// still be a valid value since it was set prior to dispatching the runnable.
66
Atomic<uint32_t> gDOMDisableOpenClickDelay(0);
67
68
// Used to keep track of pending waitUntil as well as in-flight extendable events.
69
// When the last token is released, we attempt to terminate the worker.
70
class KeepAliveToken final : public nsISupports
71
{
72
public:
73
  NS_DECL_ISUPPORTS
74
75
  explicit KeepAliveToken(ServiceWorkerPrivate* aPrivate)
76
    : mPrivate(aPrivate)
77
0
  {
78
0
    MOZ_ASSERT(NS_IsMainThread());
79
0
    MOZ_ASSERT(aPrivate);
80
0
    mPrivate->AddToken();
81
0
  }
82
83
private:
84
  ~KeepAliveToken()
85
0
  {
86
0
    MOZ_ASSERT(NS_IsMainThread());
87
0
    mPrivate->ReleaseToken();
88
0
  }
89
90
  RefPtr<ServiceWorkerPrivate> mPrivate;
91
};
92
93
NS_IMPL_ISUPPORTS0(KeepAliveToken)
94
95
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo)
96
  : mInfo(aInfo)
97
  , mDebuggerCount(0)
98
  , mTokenCount(0)
99
0
{
100
0
  MOZ_ASSERT(NS_IsMainThread());
101
0
  MOZ_ASSERT(aInfo);
102
0
103
0
  mIdleWorkerTimer = NS_NewTimer();
104
0
  MOZ_ASSERT(mIdleWorkerTimer);
105
0
}
106
107
ServiceWorkerPrivate::~ServiceWorkerPrivate()
108
0
{
109
0
  MOZ_ASSERT(!mWorkerPrivate);
110
0
  MOZ_ASSERT(!mTokenCount);
111
0
  MOZ_ASSERT(!mInfo);
112
0
  MOZ_ASSERT(mSupportsArray.IsEmpty());
113
0
114
0
  mIdleWorkerTimer->Cancel();
115
0
}
116
117
namespace {
118
119
class CheckScriptEvaluationWithCallback final : public WorkerRunnable
120
{
121
  nsMainThreadPtrHandle<ServiceWorkerPrivate> mServiceWorkerPrivate;
122
  nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
123
124
  // The script evaluation result must be reported even if the runnable
125
  // is cancelled.
126
  RefPtr<LifeCycleEventCallback> mScriptEvaluationCallback;
127
128
#ifdef DEBUG
129
  bool mDone;
130
#endif
131
132
public:
133
  CheckScriptEvaluationWithCallback(WorkerPrivate* aWorkerPrivate,
134
                                    ServiceWorkerPrivate* aServiceWorkerPrivate,
135
                                    KeepAliveToken* aKeepAliveToken,
136
                                    LifeCycleEventCallback* aScriptEvaluationCallback)
137
    : WorkerRunnable(aWorkerPrivate)
138
    , mServiceWorkerPrivate(new nsMainThreadPtrHolder<ServiceWorkerPrivate>(
139
        "CheckScriptEvaluationWithCallback::mServiceWorkerPrivate", aServiceWorkerPrivate))
140
    , mKeepAliveToken(new nsMainThreadPtrHolder<KeepAliveToken>(
141
        "CheckScriptEvaluationWithCallback::mKeepAliveToken", aKeepAliveToken))
142
    , mScriptEvaluationCallback(aScriptEvaluationCallback)
143
#ifdef DEBUG
144
    , mDone(false)
145
#endif
146
0
  {
147
0
    MOZ_ASSERT(NS_IsMainThread());
148
0
  }
149
150
  ~CheckScriptEvaluationWithCallback()
151
0
  {
152
0
    MOZ_ASSERT(mDone);
153
0
  }
154
155
  bool
156
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
157
0
  {
158
0
    aWorkerPrivate->AssertIsOnWorkerThread();
159
0
160
0
    bool fetchHandlerWasAdded = aWorkerPrivate->FetchHandlerWasAdded();
161
0
    nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<bool>(
162
0
      "dom::CheckScriptEvaluationWithCallback::ReportFetchFlag",
163
0
      this,
164
0
      &CheckScriptEvaluationWithCallback::ReportFetchFlag,
165
0
      fetchHandlerWasAdded);
166
0
    aWorkerPrivate->DispatchToMainThread(runnable.forget());
167
0
168
0
    ReportScriptEvaluationResult(aWorkerPrivate->WorkerScriptExecutedSuccessfully());
169
0
170
0
    return true;
171
0
  }
172
173
  void
174
  ReportFetchFlag(bool aFetchHandlerWasAdded)
175
0
  {
176
0
    MOZ_ASSERT(NS_IsMainThread());
177
0
    mServiceWorkerPrivate->SetHandlesFetch(aFetchHandlerWasAdded);
178
0
  }
179
180
  nsresult
181
  Cancel() override
182
0
  {
183
0
    ReportScriptEvaluationResult(false);
184
0
    return WorkerRunnable::Cancel();
185
0
  }
186
187
private:
188
  void
189
  ReportScriptEvaluationResult(bool aScriptEvaluationResult)
190
0
  {
191
#ifdef DEBUG
192
    mDone = true;
193
#endif
194
    mScriptEvaluationCallback->SetResult(aScriptEvaluationResult);
195
0
    MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mScriptEvaluationCallback));
196
0
  }
197
};
198
199
} // anonymous namespace
200
201
nsresult
202
ServiceWorkerPrivate::CheckScriptEvaluation(LifeCycleEventCallback* aScriptEvaluationCallback)
203
0
{
204
0
  nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
205
0
  NS_ENSURE_SUCCESS(rv, rv);
206
0
207
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
208
0
  RefPtr<WorkerRunnable> r = new CheckScriptEvaluationWithCallback(mWorkerPrivate,
209
0
                                                                   this, token,
210
0
                                                                   aScriptEvaluationCallback);
211
0
  if (NS_WARN_IF(!r->Dispatch())) {
212
0
    return NS_ERROR_FAILURE;
213
0
  }
214
0
215
0
  return NS_OK;
216
0
}
217
218
namespace {
219
220
enum ExtendableEventResult {
221
    Rejected = 0,
222
    Resolved
223
};
224
225
class ExtendableEventCallback {
226
public:
227
  virtual void
228
  FinishedWithResult(ExtendableEventResult aResult) = 0;
229
230
  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
231
};
232
233
class KeepAliveHandler final : public ExtendableEvent::ExtensionsHandler
234
                             , public PromiseNativeHandler
235
{
236
  // This class manages lifetime extensions added by calling WaitUntil()
237
  // or RespondWith(). We allow new extensions as long as we still hold
238
  // |mKeepAliveToken|. Once the last promise was settled, we queue a microtask
239
  // which releases the token and prevents further extensions. By doing this,
240
  // we give other pending microtasks a chance to continue adding extensions.
241
242
  RefPtr<StrongWorkerRef> mWorkerRef;
243
  nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
244
245
  // We start holding a self reference when the first extension promise is
246
  // added. As far as I can tell, the only case where this is useful is when
247
  // we're waiting indefinitely on a promise that's no longer reachable
248
  // and will never be settled.
249
  // The cycle is broken when the last promise was settled or when the
250
  // worker is shutting down.
251
  RefPtr<KeepAliveHandler> mSelfRef;
252
253
  // Called when the last promise was settled.
254
  RefPtr<ExtendableEventCallback> mCallback;
255
256
  uint32_t mPendingPromisesCount;
257
258
  // We don't actually care what values the promises resolve to, only whether
259
  // any of them were rejected.
260
  bool mRejected;
261
262
public:
263
  NS_DECL_ISUPPORTS
264
265
  explicit KeepAliveHandler(const nsMainThreadPtrHandle<KeepAliveToken>& aKeepAliveToken,
266
                            ExtendableEventCallback* aCallback)
267
    : mKeepAliveToken(aKeepAliveToken)
268
    , mCallback(aCallback)
269
    , mPendingPromisesCount(0)
270
    , mRejected(false)
271
0
  {
272
0
    MOZ_ASSERT(mKeepAliveToken);
273
0
  }
274
275
  bool
276
  Init()
277
0
  {
278
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
279
0
280
0
    RefPtr<KeepAliveHandler> self = this;
281
0
    mWorkerRef =
282
0
      StrongWorkerRef::Create(GetCurrentThreadWorkerPrivate(),
283
0
                              "KeepAliveHandler",
284
0
                              [self]() {
285
0
        self->MaybeCleanup();
286
0
      });
287
0
288
0
    if (NS_WARN_IF(!mWorkerRef)) {
289
0
      return false;
290
0
    }
291
0
292
0
    return true;
293
0
  }
294
295
  bool
296
  WaitOnPromise(Promise& aPromise) override
297
0
  {
298
0
    if (!mKeepAliveToken) {
299
0
      MOZ_ASSERT(!mSelfRef, "We shouldn't be holding a self reference!");
300
0
      return false;
301
0
    }
302
0
    if (!mSelfRef) {
303
0
      MOZ_ASSERT(!mPendingPromisesCount);
304
0
      mSelfRef = this;
305
0
    }
306
0
307
0
    ++mPendingPromisesCount;
308
0
    aPromise.AppendNativeHandler(this);
309
0
310
0
    return true;
311
0
  }
312
313
  void
314
  ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
315
0
  {
316
0
    RemovePromise(Resolved);
317
0
  }
318
319
  void
320
  RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
321
0
  {
322
0
    RemovePromise(Rejected);
323
0
  }
324
325
  void
326
  MaybeDone()
327
0
  {
328
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
329
0
330
0
    if (mPendingPromisesCount || !mKeepAliveToken) {
331
0
      return;
332
0
    }
333
0
    if (mCallback) {
334
0
      mCallback->FinishedWithResult(mRejected ? Rejected : Resolved);
335
0
    }
336
0
337
0
    MaybeCleanup();
338
0
  }
339
340
private:
341
  ~KeepAliveHandler()
342
0
  {
343
0
    MaybeCleanup();
344
0
  }
345
346
  void
347
  MaybeCleanup()
348
0
  {
349
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
350
0
351
0
    if (!mKeepAliveToken) {
352
0
      return;
353
0
    }
354
0
355
0
    mWorkerRef = nullptr;
356
0
    mKeepAliveToken = nullptr;
357
0
    mSelfRef = nullptr;
358
0
  }
359
360
  class MaybeDoneRunner : public MicroTaskRunnable
361
  {
362
  public:
363
0
    explicit MaybeDoneRunner(KeepAliveHandler* aHandler) : mHandler(aHandler) {}
364
    virtual void Run(AutoSlowOperation& aAso) override
365
0
    {
366
0
      mHandler->MaybeDone();
367
0
    }
368
369
    RefPtr<KeepAliveHandler> mHandler;
370
  };
371
372
  void
373
  RemovePromise(ExtendableEventResult aResult)
374
0
  {
375
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
376
0
    MOZ_DIAGNOSTIC_ASSERT(mPendingPromisesCount > 0);
377
0
378
0
    // Note: mSelfRef and mKeepAliveToken can be nullptr here
379
0
    //       if MaybeCleanup() was called just before a promise
380
0
    //       settled.  This can happen, for example, if the
381
0
    //       worker thread is being terminated for running too
382
0
    //       long, browser shutdown, etc.
383
0
384
0
    mRejected |= (aResult == Rejected);
385
0
386
0
    --mPendingPromisesCount;
387
0
    if (mPendingPromisesCount) {
388
0
      return;
389
0
    }
390
0
391
0
    CycleCollectedJSContext* cx = CycleCollectedJSContext::Get();
392
0
    MOZ_ASSERT(cx);
393
0
394
0
    RefPtr<MaybeDoneRunner> r = new MaybeDoneRunner(this);
395
0
    cx->DispatchToMicroTask(r.forget());
396
0
  }
397
};
398
399
NS_IMPL_ISUPPORTS0(KeepAliveHandler)
400
401
class RegistrationUpdateRunnable : public Runnable
402
{
403
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
404
  const bool mNeedTimeCheck;
405
406
public:
407
  RegistrationUpdateRunnable(
408
    nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
409
    bool aNeedTimeCheck)
410
    : Runnable("dom::RegistrationUpdateRunnable")
411
    , mRegistration(aRegistration)
412
    , mNeedTimeCheck(aNeedTimeCheck)
413
0
  {
414
0
    MOZ_DIAGNOSTIC_ASSERT(mRegistration);
415
0
  }
416
417
  NS_IMETHOD
418
  Run() override
419
0
  {
420
0
    if (mNeedTimeCheck) {
421
0
      mRegistration->MaybeScheduleTimeCheckAndUpdate();
422
0
    } else {
423
0
      mRegistration->MaybeScheduleUpdate();
424
0
    }
425
0
    return NS_OK;
426
0
  }
427
};
428
429
class ExtendableEventWorkerRunnable : public WorkerRunnable
430
{
431
protected:
432
  nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
433
434
public:
435
  ExtendableEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
436
                                KeepAliveToken* aKeepAliveToken)
437
    : WorkerRunnable(aWorkerPrivate)
438
0
  {
439
0
    MOZ_ASSERT(NS_IsMainThread());
440
0
    MOZ_ASSERT(aWorkerPrivate);
441
0
    MOZ_ASSERT(aKeepAliveToken);
442
0
443
0
    mKeepAliveToken =
444
0
      new nsMainThreadPtrHolder<KeepAliveToken>(
445
0
        "ExtendableEventWorkerRunnable::mKeepAliveToken", aKeepAliveToken);
446
0
  }
447
448
  nsresult
449
  DispatchExtendableEventOnWorkerScope(JSContext* aCx,
450
                                       WorkerGlobalScope* aWorkerScope,
451
                                       ExtendableEvent* aEvent,
452
                                       ExtendableEventCallback* aCallback)
453
0
  {
454
0
    MOZ_ASSERT(aWorkerScope);
455
0
    MOZ_ASSERT(aEvent);
456
0
    nsCOMPtr<nsIGlobalObject> sgo = aWorkerScope;
457
0
    WidgetEvent* internalEvent = aEvent->WidgetEventPtr();
458
0
459
0
    RefPtr<KeepAliveHandler> keepAliveHandler =
460
0
      new KeepAliveHandler(mKeepAliveToken, aCallback);
461
0
    if (NS_WARN_IF(!keepAliveHandler->Init())) {
462
0
      return NS_ERROR_FAILURE;
463
0
    }
464
0
465
0
    // This must always be set *before* dispatching the event, otherwise
466
0
    // waitUntil calls will fail.
467
0
    aEvent->SetKeepAliveHandler(keepAliveHandler);
468
0
469
0
    ErrorResult result;
470
0
    aWorkerScope->DispatchEvent(*aEvent, result);
471
0
    if (NS_WARN_IF(result.Failed())) {
472
0
      result.SuppressException();
473
0
      return NS_ERROR_FAILURE;
474
0
    }
475
0
476
0
    // [[ If e’s extend lifetime promises is empty, unset e’s extensions allowed
477
0
    //    flag and abort these steps. ]]
478
0
    keepAliveHandler->MaybeDone();
479
0
480
0
    // We don't block the event when getting an exception but still report the
481
0
    // error message.
482
0
    // Report exception message. Note: This will not stop the event.
483
0
    if (internalEvent->mFlags.mExceptionWasRaised) {
484
0
      result.SuppressException();
485
0
      return NS_ERROR_XPC_JS_THREW_EXCEPTION;
486
0
    }
487
0
488
0
    return NS_OK;
489
0
  }
490
};
491
492
class SendMessageEventRunnable final : public ExtendableEventWorkerRunnable
493
{
494
  const ClientInfoAndState mClientInfoAndState;
495
  RefPtr<ServiceWorkerCloneData> mData;
496
497
public:
498
  SendMessageEventRunnable(WorkerPrivate*  aWorkerPrivate,
499
                           KeepAliveToken* aKeepAliveToken,
500
                           const ClientInfoAndState& aClientInfoAndState,
501
                           RefPtr<ServiceWorkerCloneData>&& aData)
502
    : ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken)
503
    , mClientInfoAndState(aClientInfoAndState)
504
    , mData(std::move(aData))
505
0
  {
506
0
    MOZ_ASSERT(NS_IsMainThread());
507
0
    MOZ_DIAGNOSTIC_ASSERT(mData);
508
0
  }
509
510
  bool
511
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
512
0
  {
513
0
    JS::Rooted<JS::Value> messageData(aCx);
514
0
    nsCOMPtr<nsIGlobalObject> sgo = aWorkerPrivate->GlobalScope();
515
0
    ErrorResult rv;
516
0
    mData->Read(aCx, &messageData, rv);
517
0
    if (NS_WARN_IF(rv.Failed())) {
518
0
      return true;
519
0
    }
520
0
521
0
    Sequence<OwningNonNull<MessagePort>> ports;
522
0
    if (!mData->TakeTransferredPortsAsSequence(ports)) {
523
0
      return true;
524
0
    }
525
0
526
0
    RootedDictionary<ExtendableMessageEventInit> init(aCx);
527
0
528
0
    init.mBubbles = false;
529
0
    init.mCancelable = false;
530
0
531
0
    init.mData = messageData;
532
0
    init.mPorts = ports;
533
0
    init.mSource.SetValue().SetAsClient() =
534
0
      new Client(sgo, mClientInfoAndState);
535
0
536
0
    RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
537
0
    RefPtr<ExtendableMessageEvent> extendableEvent =
538
0
      ExtendableMessageEvent::Constructor(target, NS_LITERAL_STRING("message"),
539
0
                                          init, rv);
540
0
    if (NS_WARN_IF(rv.Failed())) {
541
0
      rv.SuppressException();
542
0
      return false;
543
0
    }
544
0
545
0
    extendableEvent->SetTrusted(true);
546
0
547
0
    return NS_SUCCEEDED(DispatchExtendableEventOnWorkerScope(aCx,
548
0
                                                             aWorkerPrivate->GlobalScope(),
549
0
                                                             extendableEvent,
550
0
                                                             nullptr));
551
0
  }
552
};
553
554
} // anonymous namespace
555
556
nsresult
557
ServiceWorkerPrivate::SendMessageEvent(RefPtr<ServiceWorkerCloneData>&& aData,
558
                                       const ClientInfoAndState& aClientInfoAndState)
559
0
{
560
0
  MOZ_ASSERT(NS_IsMainThread());
561
0
562
0
  nsresult rv = SpawnWorkerIfNeeded(MessageEvent);
563
0
  NS_ENSURE_SUCCESS(rv, rv);
564
0
565
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
566
0
  RefPtr<SendMessageEventRunnable> runnable =
567
0
    new SendMessageEventRunnable(mWorkerPrivate, token, aClientInfoAndState,
568
0
                                 std::move(aData));
569
0
570
0
  if (!runnable->Dispatch()) {
571
0
    return NS_ERROR_FAILURE;
572
0
  }
573
0
574
0
  return NS_OK;
575
0
}
576
577
namespace {
578
579
// Handle functional event
580
// 9.9.7 If the time difference in seconds calculated by the current time minus
581
// registration's last update check time is greater than 86400, invoke Soft Update
582
// algorithm.
583
class ExtendableFunctionalEventWorkerRunnable : public ExtendableEventWorkerRunnable
584
{
585
protected:
586
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
587
public:
588
  ExtendableFunctionalEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
589
                                          KeepAliveToken* aKeepAliveToken,
590
                                          nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration)
591
    : ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken)
592
    , mRegistration(aRegistration)
593
0
  {
594
0
    MOZ_DIAGNOSTIC_ASSERT(aRegistration);
595
0
  }
596
597
  void
598
  PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult) override
599
0
  {
600
0
    // Sub-class PreRun() or WorkerRun() methods could clear our mRegistration.
601
0
    if (mRegistration) {
602
0
      nsCOMPtr<nsIRunnable> runnable =
603
0
        new RegistrationUpdateRunnable(mRegistration, true /* time check */);
604
0
      aWorkerPrivate->DispatchToMainThread(runnable.forget());
605
0
    }
606
0
607
0
    ExtendableEventWorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
608
0
  }
609
};
610
611
/*
612
 * Fires 'install' event on the ServiceWorkerGlobalScope. Modifies busy count
613
 * since it fires the event. This is ok since there can't be nested
614
 * ServiceWorkers, so the parent thread -> worker thread requirement for
615
 * runnables is satisfied.
616
 */
617
class LifecycleEventWorkerRunnable : public ExtendableEventWorkerRunnable
618
{
619
  nsString mEventName;
620
  RefPtr<LifeCycleEventCallback> mCallback;
621
622
public:
623
  LifecycleEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
624
                               KeepAliveToken* aToken,
625
                               const nsAString& aEventName,
626
                               LifeCycleEventCallback* aCallback)
627
      : ExtendableEventWorkerRunnable(aWorkerPrivate, aToken)
628
      , mEventName(aEventName)
629
      , mCallback(aCallback)
630
0
  {
631
0
    MOZ_ASSERT(NS_IsMainThread());
632
0
  }
633
634
  bool
635
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
636
0
  {
637
0
    MOZ_ASSERT(aWorkerPrivate);
638
0
    return DispatchLifecycleEvent(aCx, aWorkerPrivate);
639
0
  }
640
641
  nsresult
642
  Cancel() override
643
0
  {
644
0
    mCallback->SetResult(false);
645
0
    MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback));
646
0
647
0
    return WorkerRunnable::Cancel();
648
0
  }
649
650
private:
651
  bool
652
  DispatchLifecycleEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
653
654
};
655
656
/*
657
 * Used to handle ExtendableEvent::waitUntil() and catch abnormal worker
658
 * termination during the execution of life cycle events. It is responsible
659
 * with advancing the job queue for install/activate tasks.
660
 */
661
class LifeCycleEventWatcher final : public ExtendableEventCallback
662
{
663
  RefPtr<StrongWorkerRef> mWorkerRef;
664
  RefPtr<LifeCycleEventCallback> mCallback;
665
666
  ~LifeCycleEventWatcher()
667
0
  {
668
0
    // XXXcatalinb: If all the promises passed to waitUntil go out of scope,
669
0
    // the resulting Promise.all will be cycle collected and it will drop its
670
0
    // native handlers (including this object). Instead of waiting for a timeout
671
0
    // we report the failure now.
672
0
    ReportResult(false);
673
0
  }
674
675
public:
676
  NS_INLINE_DECL_REFCOUNTING(LifeCycleEventWatcher, override)
677
678
  explicit LifeCycleEventWatcher(LifeCycleEventCallback* aCallback)
679
    : mCallback(aCallback)
680
0
  {
681
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
682
0
  }
683
684
  bool
685
  Init()
686
0
  {
687
0
    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
688
0
    MOZ_ASSERT(workerPrivate);
689
0
690
0
    // We need to listen for worker termination in case the event handler
691
0
    // never completes or never resolves the waitUntil promise. There are
692
0
    // two possible scenarios:
693
0
    // 1. The keepAlive token expires and the worker is terminated, in which
694
0
    //    case the registration/update promise will be rejected
695
0
    // 2. A new service worker is registered which will terminate the current
696
0
    //    installing worker.
697
0
    RefPtr<LifeCycleEventWatcher> self = this;
698
0
    mWorkerRef =
699
0
      StrongWorkerRef::Create(workerPrivate, "LifeCycleEventWatcher",
700
0
                              [self]() {
701
0
      self->ReportResult(false);
702
0
    });
703
0
    if (NS_WARN_IF(!mWorkerRef)) {
704
0
      mCallback->SetResult(false);
705
0
      nsresult rv = workerPrivate->DispatchToMainThread(mCallback);
706
0
      Unused << NS_WARN_IF(NS_FAILED(rv));
707
0
      return false;
708
0
    }
709
0
710
0
    return true;
711
0
  }
712
713
  void
714
  ReportResult(bool aResult)
715
0
  {
716
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
717
0
718
0
    if (!mWorkerRef) {
719
0
      return;
720
0
    }
721
0
722
0
    mCallback->SetResult(aResult);
723
0
    nsresult rv = mWorkerRef->Private()->DispatchToMainThread(mCallback);
724
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
725
0
      MOZ_CRASH("Failed to dispatch life cycle event handler.");
726
0
    }
727
0
728
0
    mWorkerRef = nullptr;
729
0
  }
730
731
  void
732
  FinishedWithResult(ExtendableEventResult aResult) override
733
0
  {
734
0
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
735
0
    ReportResult(aResult == Resolved);
736
0
737
0
    // Note, all WaitUntil() rejections are reported to client consoles
738
0
    // by the WaitUntilHandler in ServiceWorkerEvents.  This ensures that
739
0
    // errors in non-lifecycle events like FetchEvent and PushEvent are
740
0
    // reported properly.
741
0
  }
742
};
743
744
bool
745
LifecycleEventWorkerRunnable::DispatchLifecycleEvent(JSContext* aCx,
746
                                                     WorkerPrivate* aWorkerPrivate)
747
0
{
748
0
  aWorkerPrivate->AssertIsOnWorkerThread();
749
0
  MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
750
0
751
0
  RefPtr<ExtendableEvent> event;
752
0
  RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
753
0
754
0
  if (mEventName.EqualsASCII("install") || mEventName.EqualsASCII("activate")) {
755
0
    ExtendableEventInit init;
756
0
    init.mBubbles = false;
757
0
    init.mCancelable = false;
758
0
    event = ExtendableEvent::Constructor(target, mEventName, init);
759
0
  } else {
760
0
    MOZ_CRASH("Unexpected lifecycle event");
761
0
  }
762
0
763
0
  event->SetTrusted(true);
764
0
765
0
  // It is important to initialize the watcher before actually dispatching
766
0
  // the event in order to catch worker termination while the event handler
767
0
  // is still executing. This can happen with infinite loops, for example.
768
0
  RefPtr<LifeCycleEventWatcher> watcher = new LifeCycleEventWatcher(mCallback);
769
0
770
0
  if (!watcher->Init()) {
771
0
    return true;
772
0
  }
773
0
774
0
  nsresult rv = DispatchExtendableEventOnWorkerScope(aCx,
775
0
                                                     aWorkerPrivate->GlobalScope(),
776
0
                                                     event,
777
0
                                                     watcher);
778
0
  // Do not fail event processing when an exception is thrown.
779
0
  if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION) {
780
0
    watcher->ReportResult(false);
781
0
  }
782
0
783
0
  return true;
784
0
}
785
786
} // anonymous namespace
787
788
nsresult
789
ServiceWorkerPrivate::SendLifeCycleEvent(const nsAString& aEventType,
790
                                         LifeCycleEventCallback* aCallback)
791
0
{
792
0
  nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
793
0
  NS_ENSURE_SUCCESS(rv, rv);
794
0
795
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
796
0
  RefPtr<WorkerRunnable> r = new LifecycleEventWorkerRunnable(mWorkerPrivate,
797
0
                                                              token,
798
0
                                                              aEventType,
799
0
                                                              aCallback);
800
0
  if (NS_WARN_IF(!r->Dispatch())) {
801
0
    return NS_ERROR_FAILURE;
802
0
  }
803
0
804
0
  return NS_OK;
805
0
}
806
807
namespace {
808
809
class PushErrorReporter final : public ExtendableEventCallback
810
{
811
  WorkerPrivate* mWorkerPrivate;
812
  nsString mMessageId;
813
814
  ~PushErrorReporter()
815
0
  {
816
0
  }
817
818
public:
819
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PushErrorReporter, override)
820
821
  PushErrorReporter(WorkerPrivate* aWorkerPrivate,
822
                    const nsAString& aMessageId)
823
    : mWorkerPrivate(aWorkerPrivate)
824
    , mMessageId(aMessageId)
825
0
  {
826
0
    mWorkerPrivate->AssertIsOnWorkerThread();
827
0
  }
828
829
  void
830
  FinishedWithResult(ExtendableEventResult aResult) override
831
0
  {
832
0
    if (aResult == Rejected) {
833
0
      Report(nsIPushErrorReporter::DELIVERY_UNHANDLED_REJECTION);
834
0
    }
835
0
  }
836
837
  void Report(uint16_t aReason = nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR)
838
0
  {
839
0
    WorkerPrivate* workerPrivate = mWorkerPrivate;
840
0
    mWorkerPrivate->AssertIsOnWorkerThread();
841
0
842
0
    if (NS_WARN_IF(aReason > nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) ||
843
0
        mMessageId.IsEmpty()) {
844
0
      return;
845
0
    }
846
0
    nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<uint16_t>(
847
0
      "dom::PushErrorReporter::ReportOnMainThread",
848
0
      this,
849
0
      &PushErrorReporter::ReportOnMainThread,
850
0
      aReason);
851
0
    MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
852
0
      workerPrivate->DispatchToMainThread(runnable.forget())));
853
0
  }
854
855
  void ReportOnMainThread(uint16_t aReason)
856
0
  {
857
0
    MOZ_ASSERT(NS_IsMainThread());
858
0
    nsCOMPtr<nsIPushErrorReporter> reporter =
859
0
      do_GetService("@mozilla.org/push/Service;1");
860
0
    if (reporter) {
861
0
      nsresult rv = reporter->ReportDeliveryError(mMessageId, aReason);
862
0
      Unused << NS_WARN_IF(NS_FAILED(rv));
863
0
    }
864
0
  }
865
};
866
867
class SendPushEventRunnable final : public ExtendableFunctionalEventWorkerRunnable
868
{
869
  nsString mMessageId;
870
  Maybe<nsTArray<uint8_t>> mData;
871
872
public:
873
  SendPushEventRunnable(WorkerPrivate* aWorkerPrivate,
874
                        KeepAliveToken* aKeepAliveToken,
875
                        const nsAString& aMessageId,
876
                        const Maybe<nsTArray<uint8_t>>& aData,
877
                        nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> aRegistration)
878
      : ExtendableFunctionalEventWorkerRunnable(
879
          aWorkerPrivate, aKeepAliveToken, aRegistration)
880
      , mMessageId(aMessageId)
881
      , mData(aData)
882
0
  {
883
0
    MOZ_ASSERT(NS_IsMainThread());
884
0
    MOZ_ASSERT(aWorkerPrivate);
885
0
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
886
0
  }
887
888
  bool
889
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
890
0
  {
891
0
    MOZ_ASSERT(aWorkerPrivate);
892
0
    GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
893
0
894
0
    RefPtr<PushErrorReporter> errorReporter =
895
0
      new PushErrorReporter(aWorkerPrivate, mMessageId);
896
0
897
0
    PushEventInit pei;
898
0
    if (mData) {
899
0
      const nsTArray<uint8_t>& bytes = mData.ref();
900
0
      JSObject* data = Uint8Array::Create(aCx, bytes.Length(), bytes.Elements());
901
0
      if (!data) {
902
0
        errorReporter->Report();
903
0
        return false;
904
0
      }
905
0
      pei.mData.Construct().SetAsArrayBufferView().Init(data);
906
0
    }
907
0
    pei.mBubbles = false;
908
0
    pei.mCancelable = false;
909
0
910
0
    ErrorResult result;
911
0
    RefPtr<PushEvent> event =
912
0
      PushEvent::Constructor(globalObj, NS_LITERAL_STRING("push"), pei, result);
913
0
    if (NS_WARN_IF(result.Failed())) {
914
0
      result.SuppressException();
915
0
      errorReporter->Report();
916
0
      return false;
917
0
    }
918
0
    event->SetTrusted(true);
919
0
920
0
    nsresult rv = DispatchExtendableEventOnWorkerScope(aCx,
921
0
                                                       aWorkerPrivate->GlobalScope(),
922
0
                                                       event,
923
0
                                                       errorReporter);
924
0
    if (NS_FAILED(rv)) {
925
0
      // We don't cancel WorkerPrivate when catching an excetpion.
926
0
      errorReporter->Report(nsIPushErrorReporter::DELIVERY_UNCAUGHT_EXCEPTION);
927
0
    }
928
0
929
0
    return true;
930
0
  }
931
};
932
933
class SendPushSubscriptionChangeEventRunnable final : public ExtendableEventWorkerRunnable
934
{
935
936
public:
937
  explicit SendPushSubscriptionChangeEventRunnable(
938
    WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken)
939
      : ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken)
940
0
  {
941
0
    MOZ_ASSERT(NS_IsMainThread());
942
0
    MOZ_ASSERT(aWorkerPrivate);
943
0
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
944
0
  }
945
946
  bool
947
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
948
0
  {
949
0
    MOZ_ASSERT(aWorkerPrivate);
950
0
951
0
    RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
952
0
953
0
    ExtendableEventInit init;
954
0
    init.mBubbles = false;
955
0
    init.mCancelable = false;
956
0
957
0
    RefPtr<ExtendableEvent> event =
958
0
      ExtendableEvent::Constructor(target,
959
0
                                   NS_LITERAL_STRING("pushsubscriptionchange"),
960
0
                                   init);
961
0
962
0
    event->SetTrusted(true);
963
0
964
0
    DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
965
0
                                         event, nullptr);
966
0
967
0
    return true;
968
0
  }
969
};
970
971
} // anonymous namespace
972
973
nsresult
974
ServiceWorkerPrivate::SendPushEvent(const nsAString& aMessageId,
975
                                    const Maybe<nsTArray<uint8_t>>& aData,
976
                                    ServiceWorkerRegistrationInfo* aRegistration)
977
0
{
978
0
  nsresult rv = SpawnWorkerIfNeeded(PushEvent);
979
0
  NS_ENSURE_SUCCESS(rv, rv);
980
0
981
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
982
0
983
0
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
984
0
    new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
985
0
      "ServiceWorkerRegistrationInfoProxy", aRegistration, false));
986
0
987
0
  RefPtr<WorkerRunnable> r = new SendPushEventRunnable(mWorkerPrivate,
988
0
                                                       token,
989
0
                                                       aMessageId,
990
0
                                                       aData,
991
0
                                                       regInfo);
992
0
993
0
  if (mInfo->State() == ServiceWorkerState::Activating) {
994
0
    mPendingFunctionalEvents.AppendElement(r.forget());
995
0
    return NS_OK;
996
0
  }
997
0
998
0
  MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
999
0
1000
0
  if (NS_WARN_IF(!r->Dispatch())) {
1001
0
    return NS_ERROR_FAILURE;
1002
0
  }
1003
0
1004
0
  return NS_OK;
1005
0
}
1006
1007
nsresult
1008
ServiceWorkerPrivate::SendPushSubscriptionChangeEvent()
1009
0
{
1010
0
  nsresult rv = SpawnWorkerIfNeeded(PushSubscriptionChangeEvent);
1011
0
  NS_ENSURE_SUCCESS(rv, rv);
1012
0
1013
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1014
0
  RefPtr<WorkerRunnable> r =
1015
0
    new SendPushSubscriptionChangeEventRunnable(mWorkerPrivate, token);
1016
0
  if (NS_WARN_IF(!r->Dispatch())) {
1017
0
    return NS_ERROR_FAILURE;
1018
0
  }
1019
0
1020
0
  return NS_OK;
1021
0
}
1022
1023
namespace {
1024
1025
class AllowWindowInteractionHandler final : public ExtendableEventCallback
1026
                                          , public nsITimerCallback
1027
                                          , public nsINamed
1028
{
1029
  nsCOMPtr<nsITimer> mTimer;
1030
  RefPtr<StrongWorkerRef> mWorkerRef;
1031
1032
  ~AllowWindowInteractionHandler()
1033
0
  {
1034
0
    // We must either fail to initialize or call ClearWindowAllowed.
1035
0
    MOZ_DIAGNOSTIC_ASSERT(!mTimer);
1036
0
    MOZ_DIAGNOSTIC_ASSERT(!mWorkerRef);
1037
0
  }
1038
1039
  void
1040
  ClearWindowAllowed(WorkerPrivate* aWorkerPrivate)
1041
0
  {
1042
0
    MOZ_ASSERT(aWorkerPrivate);
1043
0
    aWorkerPrivate->AssertIsOnWorkerThread();
1044
0
1045
0
    if (!mTimer) {
1046
0
      return;
1047
0
    }
1048
0
1049
0
    // XXXcatalinb: This *might* be executed after the global was unrooted, in
1050
0
    // which case GlobalScope() will return null. Making the check here just
1051
0
    // to be safe.
1052
0
    WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
1053
0
    if (!globalScope) {
1054
0
      return;
1055
0
    }
1056
0
1057
0
    globalScope->ConsumeWindowInteraction();
1058
0
    mTimer->Cancel();
1059
0
    mTimer = nullptr;
1060
0
1061
0
    mWorkerRef = nullptr;
1062
0
  }
1063
1064
  void
1065
  StartClearWindowTimer(WorkerPrivate* aWorkerPrivate)
1066
0
  {
1067
0
    MOZ_ASSERT(aWorkerPrivate);
1068
0
    aWorkerPrivate->AssertIsOnWorkerThread();
1069
0
    MOZ_ASSERT(!mTimer);
1070
0
1071
0
    nsresult rv;
1072
0
    nsCOMPtr<nsITimer> timer = NS_NewTimer(aWorkerPrivate->ControlEventTarget());
1073
0
    if (NS_WARN_IF(!timer)) {
1074
0
      return;
1075
0
    }
1076
0
1077
0
    MOZ_ASSERT(!mWorkerRef);
1078
0
    RefPtr<AllowWindowInteractionHandler> self = this;
1079
0
    mWorkerRef =
1080
0
      StrongWorkerRef::Create(aWorkerPrivate,
1081
0
                              "AllowWindowInteractionHandler",
1082
0
                              [self]() {
1083
0
        // We could try to hold the worker alive until the timer fires, but
1084
0
        // other APIs are not likely to work in this partially shutdown state.
1085
0
        // We might as well let the worker thread exit.
1086
0
        self->ClearWindowAllowed(self->mWorkerRef->Private());
1087
0
      });
1088
0
1089
0
    if (!mWorkerRef) {
1090
0
      return;
1091
0
    }
1092
0
1093
0
    aWorkerPrivate->GlobalScope()->AllowWindowInteraction();
1094
0
    timer.swap(mTimer);
1095
0
1096
0
    // We swap first and then initialize the timer so that even if initializing
1097
0
    // fails, we still clean the busy count and interaction count correctly.
1098
0
    // The timer can't be initialized before modifying the busy count since the
1099
0
    // timer thread could run and call the timeout but the worker may
1100
0
    // already be terminating and modifying the busy count could fail.
1101
0
    rv = mTimer->InitWithCallback(this,
1102
0
                                  gDOMDisableOpenClickDelay,
1103
0
                                  nsITimer::TYPE_ONE_SHOT);
1104
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
1105
0
      ClearWindowAllowed(aWorkerPrivate);
1106
0
      return;
1107
0
    }
1108
0
  }
1109
1110
  // nsITimerCallback virtual methods
1111
  NS_IMETHOD
1112
  Notify(nsITimer* aTimer) override
1113
0
  {
1114
0
    MOZ_DIAGNOSTIC_ASSERT(mTimer == aTimer);
1115
0
    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
1116
0
    ClearWindowAllowed(workerPrivate);
1117
0
    return NS_OK;
1118
0
  }
1119
1120
  // nsINamed virtual methods
1121
  NS_IMETHOD
1122
  GetName(nsACString& aName) override
1123
0
  {
1124
0
    aName.AssignLiteral("AllowWindowInteractionHandler");
1125
0
    return NS_OK;
1126
0
  }
1127
1128
public:
1129
  NS_DECL_THREADSAFE_ISUPPORTS
1130
1131
  explicit AllowWindowInteractionHandler(WorkerPrivate* aWorkerPrivate)
1132
0
  {
1133
0
    StartClearWindowTimer(aWorkerPrivate);
1134
0
  }
1135
1136
  void
1137
  FinishedWithResult(ExtendableEventResult /* aResult */) override
1138
0
  {
1139
0
    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
1140
0
    ClearWindowAllowed(workerPrivate);
1141
0
  }
1142
};
1143
1144
NS_IMPL_ISUPPORTS(AllowWindowInteractionHandler, nsITimerCallback, nsINamed)
1145
1146
class SendNotificationEventRunnable final : public ExtendableEventWorkerRunnable
1147
{
1148
  const nsString mEventName;
1149
  const nsString mID;
1150
  const nsString mTitle;
1151
  const nsString mDir;
1152
  const nsString mLang;
1153
  const nsString mBody;
1154
  const nsString mTag;
1155
  const nsString mIcon;
1156
  const nsString mData;
1157
  const nsString mBehavior;
1158
  const nsString mScope;
1159
1160
public:
1161
  SendNotificationEventRunnable(WorkerPrivate* aWorkerPrivate,
1162
                                KeepAliveToken* aKeepAliveToken,
1163
                                const nsAString& aEventName,
1164
                                const nsAString& aID,
1165
                                const nsAString& aTitle,
1166
                                const nsAString& aDir,
1167
                                const nsAString& aLang,
1168
                                const nsAString& aBody,
1169
                                const nsAString& aTag,
1170
                                const nsAString& aIcon,
1171
                                const nsAString& aData,
1172
                                const nsAString& aBehavior,
1173
                                const nsAString& aScope)
1174
      : ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken)
1175
      , mEventName(aEventName)
1176
      , mID(aID)
1177
      , mTitle(aTitle)
1178
      , mDir(aDir)
1179
      , mLang(aLang)
1180
      , mBody(aBody)
1181
      , mTag(aTag)
1182
      , mIcon(aIcon)
1183
      , mData(aData)
1184
      , mBehavior(aBehavior)
1185
      , mScope(aScope)
1186
0
  {
1187
0
    MOZ_ASSERT(NS_IsMainThread());
1188
0
    MOZ_ASSERT(aWorkerPrivate);
1189
0
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
1190
0
  }
1191
1192
  bool
1193
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
1194
0
  {
1195
0
    MOZ_ASSERT(aWorkerPrivate);
1196
0
1197
0
    RefPtr<EventTarget> target = do_QueryObject(aWorkerPrivate->GlobalScope());
1198
0
1199
0
    ErrorResult result;
1200
0
    RefPtr<Notification> notification =
1201
0
      Notification::ConstructFromFields(aWorkerPrivate->GlobalScope(), mID,
1202
0
                                        mTitle, mDir, mLang, mBody, mTag, mIcon,
1203
0
                                        mData, mScope, result);
1204
0
    if (NS_WARN_IF(result.Failed())) {
1205
0
      return false;
1206
0
    }
1207
0
1208
0
    NotificationEventInit nei;
1209
0
    nei.mNotification = notification;
1210
0
    nei.mBubbles = false;
1211
0
    nei.mCancelable = false;
1212
0
1213
0
    RefPtr<NotificationEvent> event =
1214
0
      NotificationEvent::Constructor(target, mEventName,
1215
0
                                     nei, result);
1216
0
    if (NS_WARN_IF(result.Failed())) {
1217
0
      return false;
1218
0
    }
1219
0
1220
0
    event->SetTrusted(true);
1221
0
1222
0
    RefPtr<AllowWindowInteractionHandler> allowWindowInteraction;
1223
0
    if (mEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
1224
0
      allowWindowInteraction =
1225
0
        new AllowWindowInteractionHandler(aWorkerPrivate);
1226
0
    }
1227
0
1228
0
    nsresult rv = DispatchExtendableEventOnWorkerScope(aCx,
1229
0
                                                       aWorkerPrivate->GlobalScope(),
1230
0
                                                       event,
1231
0
                                                       allowWindowInteraction);
1232
0
    // Don't reject when catching an exception
1233
0
    if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION &&
1234
0
        allowWindowInteraction) {
1235
0
      allowWindowInteraction->FinishedWithResult(Rejected);
1236
0
    }
1237
0
1238
0
    return true;
1239
0
  }
1240
};
1241
1242
} // namespace anonymous
1243
1244
nsresult
1245
ServiceWorkerPrivate::SendNotificationEvent(const nsAString& aEventName,
1246
                                            const nsAString& aID,
1247
                                            const nsAString& aTitle,
1248
                                            const nsAString& aDir,
1249
                                            const nsAString& aLang,
1250
                                            const nsAString& aBody,
1251
                                            const nsAString& aTag,
1252
                                            const nsAString& aIcon,
1253
                                            const nsAString& aData,
1254
                                            const nsAString& aBehavior,
1255
                                            const nsAString& aScope)
1256
0
{
1257
0
  WakeUpReason why;
1258
0
  if (aEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
1259
0
    why = NotificationClickEvent;
1260
0
    gDOMDisableOpenClickDelay =
1261
0
      Preferences::GetInt("dom.serviceWorkers.disable_open_click_delay");
1262
0
  } else if (aEventName.EqualsLiteral(NOTIFICATION_CLOSE_EVENT_NAME)) {
1263
0
    why = NotificationCloseEvent;
1264
0
  } else {
1265
0
    MOZ_ASSERT_UNREACHABLE("Invalid notification event name");
1266
0
    return NS_ERROR_FAILURE;
1267
0
  }
1268
0
1269
0
  nsresult rv = SpawnWorkerIfNeeded(why);
1270
0
  NS_ENSURE_SUCCESS(rv, rv);
1271
0
1272
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1273
0
1274
0
  RefPtr<WorkerRunnable> r =
1275
0
    new SendNotificationEventRunnable(mWorkerPrivate, token,
1276
0
                                      aEventName, aID, aTitle, aDir, aLang,
1277
0
                                      aBody, aTag, aIcon, aData, aBehavior,
1278
0
                                      aScope);
1279
0
  if (NS_WARN_IF(!r->Dispatch())) {
1280
0
    return NS_ERROR_FAILURE;
1281
0
  }
1282
0
1283
0
  return NS_OK;
1284
0
}
1285
1286
namespace {
1287
1288
// Inheriting ExtendableEventWorkerRunnable so that the worker is not terminated
1289
// while handling the fetch event, though that's very unlikely.
1290
class FetchEventRunnable : public ExtendableFunctionalEventWorkerRunnable
1291
                         , public nsIHttpHeaderVisitor {
1292
  nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
1293
  const nsCString mScriptSpec;
1294
  nsTArray<nsCString> mHeaderNames;
1295
  nsTArray<nsCString> mHeaderValues;
1296
  nsCString mSpec;
1297
  nsCString mFragment;
1298
  nsCString mMethod;
1299
  nsString mClientId;
1300
  bool mIsReload;
1301
  bool mMarkLaunchServiceWorkerEnd;
1302
  RequestCache mCacheMode;
1303
  RequestMode mRequestMode;
1304
  RequestRedirect mRequestRedirect;
1305
  RequestCredentials mRequestCredentials;
1306
  nsContentPolicyType mContentPolicyType;
1307
  nsCOMPtr<nsIInputStream> mUploadStream;
1308
  int64_t mUploadStreamContentLength;
1309
  nsCString mReferrer;
1310
  ReferrerPolicy mReferrerPolicy;
1311
  nsString mIntegrity;
1312
public:
1313
  FetchEventRunnable(WorkerPrivate* aWorkerPrivate,
1314
                     KeepAliveToken* aKeepAliveToken,
1315
                     nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
1316
                     // CSP checks might require the worker script spec
1317
                     // later on.
1318
                     const nsACString& aScriptSpec,
1319
                     nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
1320
                     const nsAString& aClientId,
1321
                     bool aIsReload,
1322
                     bool aMarkLaunchServiceWorkerEnd)
1323
    : ExtendableFunctionalEventWorkerRunnable(
1324
        aWorkerPrivate, aKeepAliveToken, aRegistration)
1325
    , mInterceptedChannel(aChannel)
1326
    , mScriptSpec(aScriptSpec)
1327
    , mClientId(aClientId)
1328
    , mIsReload(aIsReload)
1329
    , mMarkLaunchServiceWorkerEnd(aMarkLaunchServiceWorkerEnd)
1330
    , mCacheMode(RequestCache::Default)
1331
    , mRequestMode(RequestMode::No_cors)
1332
    , mRequestRedirect(RequestRedirect::Follow)
1333
    // By default we set it to same-origin since normal HTTP fetches always
1334
    // send credentials to same-origin websites unless explicitly forbidden.
1335
    , mRequestCredentials(RequestCredentials::Same_origin)
1336
    , mContentPolicyType(nsIContentPolicy::TYPE_INVALID)
1337
    , mUploadStreamContentLength(-1)
1338
    , mReferrer(kFETCH_CLIENT_REFERRER_STR)
1339
    , mReferrerPolicy(ReferrerPolicy::_empty)
1340
0
  {
1341
0
    MOZ_ASSERT(aWorkerPrivate);
1342
0
  }
1343
1344
  NS_DECL_ISUPPORTS_INHERITED
1345
1346
  NS_IMETHOD
1347
  VisitHeader(const nsACString& aHeader, const nsACString& aValue) override
1348
0
  {
1349
0
    mHeaderNames.AppendElement(aHeader);
1350
0
    mHeaderValues.AppendElement(aValue);
1351
0
    return NS_OK;
1352
0
  }
1353
1354
  nsresult
1355
  Init()
1356
0
  {
1357
0
    MOZ_ASSERT(NS_IsMainThread());
1358
0
    nsCOMPtr<nsIChannel> channel;
1359
0
    nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
1360
0
    NS_ENSURE_SUCCESS(rv, rv);
1361
0
1362
0
    nsCOMPtr<nsIURI> uri;
1363
0
    rv = mInterceptedChannel->GetSecureUpgradedChannelURI(getter_AddRefs(uri));
1364
0
    NS_ENSURE_SUCCESS(rv, rv);
1365
0
1366
0
    // Normally we rely on the Request constructor to strip the fragment, but
1367
0
    // when creating the FetchEvent we bypass the constructor.  So strip the
1368
0
    // fragment manually here instead.  We can't do it later when we create
1369
0
    // the Request because that code executes off the main thread.
1370
0
    nsCOMPtr<nsIURI> uriNoFragment;
1371
0
    rv = NS_GetURIWithoutRef(uri, getter_AddRefs(uriNoFragment));
1372
0
    NS_ENSURE_SUCCESS(rv, rv);
1373
0
    rv = uriNoFragment->GetSpec(mSpec);
1374
0
    NS_ENSURE_SUCCESS(rv, rv);
1375
0
    rv = uri->GetRef(mFragment);
1376
0
    NS_ENSURE_SUCCESS(rv, rv);
1377
0
1378
0
    uint32_t loadFlags;
1379
0
    rv = channel->GetLoadFlags(&loadFlags);
1380
0
    NS_ENSURE_SUCCESS(rv, rv);
1381
0
    nsCOMPtr<nsILoadInfo> loadInfo;
1382
0
    rv = channel->GetLoadInfo(getter_AddRefs(loadInfo));
1383
0
    NS_ENSURE_SUCCESS(rv, rv);
1384
0
    NS_ENSURE_STATE(loadInfo);
1385
0
    mContentPolicyType = loadInfo->InternalContentPolicyType();
1386
0
1387
0
1388
0
    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel);
1389
0
    MOZ_ASSERT(httpChannel, "How come we don't have an HTTP channel?");
1390
0
1391
0
    nsAutoCString referrer;
1392
0
    // Ignore the return value since the Referer header may not exist.
1393
0
    Unused << httpChannel->GetRequestHeader(NS_LITERAL_CSTRING("Referer"),
1394
0
                                            referrer);
1395
0
    if (!referrer.IsEmpty()) {
1396
0
      mReferrer = referrer;
1397
0
    } else {
1398
0
      // If there's no referrer Header, means the header was omitted for
1399
0
      // security/privacy reason.
1400
0
      mReferrer = EmptyCString();
1401
0
    }
1402
0
1403
0
    uint32_t referrerPolicy = 0;
1404
0
    rv = httpChannel->GetReferrerPolicy(&referrerPolicy);
1405
0
    NS_ENSURE_SUCCESS(rv, rv);
1406
0
    switch (referrerPolicy) {
1407
0
      case nsIHttpChannel::REFERRER_POLICY_UNSET:
1408
0
      mReferrerPolicy = ReferrerPolicy::_empty;
1409
0
      break;
1410
0
    case nsIHttpChannel::REFERRER_POLICY_NO_REFERRER:
1411
0
      mReferrerPolicy = ReferrerPolicy::No_referrer;
1412
0
      break;
1413
0
    case nsIHttpChannel::REFERRER_POLICY_ORIGIN:
1414
0
      mReferrerPolicy = ReferrerPolicy::Origin;
1415
0
      break;
1416
0
    case nsIHttpChannel::REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE:
1417
0
      mReferrerPolicy = ReferrerPolicy::No_referrer_when_downgrade;
1418
0
      break;
1419
0
    case nsIHttpChannel::REFERRER_POLICY_ORIGIN_WHEN_XORIGIN:
1420
0
      mReferrerPolicy = ReferrerPolicy::Origin_when_cross_origin;
1421
0
      break;
1422
0
    case nsIHttpChannel::REFERRER_POLICY_UNSAFE_URL:
1423
0
      mReferrerPolicy = ReferrerPolicy::Unsafe_url;
1424
0
      break;
1425
0
    case nsIHttpChannel::REFERRER_POLICY_SAME_ORIGIN:
1426
0
      mReferrerPolicy = ReferrerPolicy::Same_origin;
1427
0
      break;
1428
0
    case nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN:
1429
0
      mReferrerPolicy = ReferrerPolicy::Strict_origin_when_cross_origin;
1430
0
      break;
1431
0
    case nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN:
1432
0
      mReferrerPolicy = ReferrerPolicy::Strict_origin;
1433
0
      break;
1434
0
    default:
1435
0
      MOZ_ASSERT_UNREACHABLE("Invalid Referrer Policy enum value?");
1436
0
      break;
1437
0
    }
1438
0
1439
0
    rv = httpChannel->GetRequestMethod(mMethod);
1440
0
    NS_ENSURE_SUCCESS(rv, rv);
1441
0
1442
0
    nsCOMPtr<nsIHttpChannelInternal> internalChannel = do_QueryInterface(httpChannel);
1443
0
    NS_ENSURE_TRUE(internalChannel, NS_ERROR_NOT_AVAILABLE);
1444
0
1445
0
    mRequestMode = InternalRequest::MapChannelToRequestMode(channel);
1446
0
1447
0
    // This is safe due to static_asserts in ServiceWorkerManager.cpp.
1448
0
    uint32_t redirectMode;
1449
0
    rv = internalChannel->GetRedirectMode(&redirectMode);
1450
0
    MOZ_ASSERT(NS_SUCCEEDED(rv));
1451
0
    mRequestRedirect = static_cast<RequestRedirect>(redirectMode);
1452
0
1453
0
    // This is safe due to static_asserts in ServiceWorkerManager.cpp.
1454
0
    uint32_t cacheMode;
1455
0
    rv = internalChannel->GetFetchCacheMode(&cacheMode);
1456
0
    MOZ_ASSERT(NS_SUCCEEDED(rv));
1457
0
    mCacheMode = static_cast<RequestCache>(cacheMode);
1458
0
1459
0
    rv = internalChannel->GetIntegrityMetadata(mIntegrity);
1460
0
    MOZ_ASSERT(NS_SUCCEEDED(rv));
1461
0
1462
0
    mRequestCredentials = InternalRequest::MapChannelToRequestCredentials(channel);
1463
0
1464
0
    rv = httpChannel->VisitNonDefaultRequestHeaders(this);
1465
0
    NS_ENSURE_SUCCESS(rv, rv);
1466
0
1467
0
    nsCOMPtr<nsIUploadChannel2> uploadChannel = do_QueryInterface(httpChannel);
1468
0
    if (uploadChannel) {
1469
0
      MOZ_ASSERT(!mUploadStream);
1470
0
      nsCOMPtr<nsIInputStream> uploadStream;
1471
0
      rv = uploadChannel->CloneUploadStream(&mUploadStreamContentLength,
1472
0
                                            getter_AddRefs(uploadStream));
1473
0
      NS_ENSURE_SUCCESS(rv, rv);
1474
0
      mUploadStream = uploadStream;
1475
0
    }
1476
0
1477
0
    return NS_OK;
1478
0
  }
1479
1480
  bool
1481
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
1482
0
  {
1483
0
    MOZ_ASSERT(aWorkerPrivate);
1484
0
1485
0
    if (mMarkLaunchServiceWorkerEnd) {
1486
0
      mInterceptedChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
1487
0
1488
0
      // A probe to measure sw launch time for telemetry.
1489
0
      TimeStamp launchStartTime = TimeStamp();
1490
0
      mInterceptedChannel->GetLaunchServiceWorkerStart(&launchStartTime);
1491
0
1492
0
      TimeStamp launchEndTime = TimeStamp();
1493
0
      mInterceptedChannel->GetLaunchServiceWorkerEnd(&launchEndTime);
1494
0
      Telemetry::AccumulateTimeDelta(Telemetry::SERVICE_WORKER_LAUNCH_TIME,
1495
0
                                     launchStartTime, launchEndTime);
1496
0
    }
1497
0
1498
0
    mInterceptedChannel->SetDispatchFetchEventEnd(TimeStamp::Now());
1499
0
    return DispatchFetchEvent(aCx, aWorkerPrivate);
1500
0
  }
1501
1502
  nsresult
1503
  Cancel() override
1504
0
  {
1505
0
    nsCOMPtr<nsIRunnable> runnable = new ResumeRequest(mInterceptedChannel);
1506
0
    if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable))) {
1507
0
      NS_WARNING("Failed to resume channel on FetchEventRunnable::Cancel()!\n");
1508
0
    }
1509
0
    WorkerRunnable::Cancel();
1510
0
    return NS_OK;
1511
0
  }
1512
1513
private:
1514
0
  ~FetchEventRunnable() {}
1515
1516
  class ResumeRequest final : public Runnable {
1517
    nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
1518
  public:
1519
    explicit ResumeRequest(
1520
      nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel)
1521
      : Runnable("dom::FetchEventRunnable::ResumeRequest")
1522
      , mChannel(aChannel)
1523
0
    {
1524
0
      mChannel->SetFinishResponseStart(TimeStamp::Now());
1525
0
    }
1526
1527
    NS_IMETHOD Run() override
1528
0
    {
1529
0
      MOZ_ASSERT(NS_IsMainThread());
1530
0
1531
0
      TimeStamp timeStamp = TimeStamp::Now();
1532
0
      mChannel->SetHandleFetchEventEnd(timeStamp);
1533
0
      mChannel->SetChannelResetEnd(timeStamp);
1534
0
      mChannel->SaveTimeStamps();
1535
0
1536
0
      nsresult rv = mChannel->ResetInterception();
1537
0
      if (NS_FAILED(rv)) {
1538
0
        NS_WARNING("Failed to resume intercepted network request");
1539
0
        mChannel->CancelInterception(rv);
1540
0
      }
1541
0
      return rv;
1542
0
    }
1543
  };
1544
1545
  bool
1546
  DispatchFetchEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
1547
0
  {
1548
0
    MOZ_ASSERT(aCx);
1549
0
    MOZ_ASSERT(aWorkerPrivate);
1550
0
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
1551
0
    GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
1552
0
1553
0
    RefPtr<InternalHeaders> internalHeaders = new InternalHeaders(HeadersGuardEnum::Request);
1554
0
    MOZ_ASSERT(mHeaderNames.Length() == mHeaderValues.Length());
1555
0
    for (uint32_t i = 0; i < mHeaderNames.Length(); i++) {
1556
0
      ErrorResult result;
1557
0
      internalHeaders->Set(mHeaderNames[i], mHeaderValues[i], result);
1558
0
      if (NS_WARN_IF(result.Failed())) {
1559
0
        result.SuppressException();
1560
0
        return false;
1561
0
      }
1562
0
    }
1563
0
1564
0
    ErrorResult result;
1565
0
    internalHeaders->SetGuard(HeadersGuardEnum::Immutable, result);
1566
0
    if (NS_WARN_IF(result.Failed())) {
1567
0
      result.SuppressException();
1568
0
      return false;
1569
0
    }
1570
0
    RefPtr<InternalRequest> internalReq = new InternalRequest(mSpec,
1571
0
                                                              mFragment,
1572
0
                                                              mMethod,
1573
0
                                                              internalHeaders.forget(),
1574
0
                                                              mCacheMode,
1575
0
                                                              mRequestMode,
1576
0
                                                              mRequestRedirect,
1577
0
                                                              mRequestCredentials,
1578
0
                                                              NS_ConvertUTF8toUTF16(mReferrer),
1579
0
                                                              mReferrerPolicy,
1580
0
                                                              mContentPolicyType,
1581
0
                                                              mIntegrity);
1582
0
    internalReq->SetBody(mUploadStream, mUploadStreamContentLength);
1583
0
    // For Telemetry, note that this Request object was created by a Fetch event.
1584
0
    internalReq->SetCreatedByFetchEvent();
1585
0
1586
0
    nsCOMPtr<nsIChannel> channel;
1587
0
    nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
1588
0
    NS_ENSURE_SUCCESS(rv, false);
1589
0
1590
0
    nsAutoCString alternativeDataType;
1591
0
    nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(channel);
1592
0
    if (cic &&
1593
0
        NS_SUCCEEDED(cic->GetPreferredAlternativeDataType(alternativeDataType)) &&
1594
0
        !alternativeDataType.IsEmpty()) {
1595
0
      internalReq->SetPreferredAlternativeDataType(alternativeDataType);
1596
0
    }
1597
0
1598
0
    nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(globalObj.GetAsSupports());
1599
0
    if (NS_WARN_IF(!global)) {
1600
0
      return false;
1601
0
    }
1602
0
1603
0
    // TODO This request object should be created with a AbortSignal object
1604
0
    // which should be aborted if the loading is aborted. See bug 1394102.
1605
0
    RefPtr<Request> request = new Request(global, internalReq, nullptr);
1606
0
1607
0
    MOZ_ASSERT_IF(internalReq->IsNavigationRequest(),
1608
0
                  request->Redirect() == RequestRedirect::Manual);
1609
0
1610
0
    RootedDictionary<FetchEventInit> init(aCx);
1611
0
    init.mRequest = request;
1612
0
    init.mBubbles = false;
1613
0
    init.mCancelable = true;
1614
0
    // Only expose the FetchEvent.clientId on subresource requests for now.
1615
0
    // Once we implement .resultingClientId and .targetClientId we can then
1616
0
    // start exposing .clientId on non-subresource requests as well.  See
1617
0
    // bug 1264177.
1618
0
    if (!mClientId.IsEmpty() && !internalReq->IsNavigationRequest()) {
1619
0
      init.mClientId = mClientId;
1620
0
    }
1621
0
    init.mIsReload = mIsReload;
1622
0
    RefPtr<FetchEvent> event =
1623
0
      FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init, result);
1624
0
    if (NS_WARN_IF(result.Failed())) {
1625
0
      result.SuppressException();
1626
0
      return false;
1627
0
    }
1628
0
1629
0
    event->PostInit(mInterceptedChannel, mRegistration, mScriptSpec);
1630
0
    event->SetTrusted(true);
1631
0
1632
0
    mInterceptedChannel->SetHandleFetchEventStart(TimeStamp::Now());
1633
0
1634
0
    nsresult rv2 =
1635
0
      DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
1636
0
                                           event, nullptr);
1637
0
    if ((NS_WARN_IF(NS_FAILED(rv2)) && rv2 != NS_ERROR_XPC_JS_THREW_EXCEPTION) ||
1638
0
        !event->WaitToRespond()) {
1639
0
      nsCOMPtr<nsIRunnable> runnable;
1640
0
      MOZ_ASSERT(!aWorkerPrivate->UsesSystemPrincipal(),
1641
0
                 "We don't support system-principal serviceworkers");
1642
0
      if (event->DefaultPrevented(CallerType::NonSystem)) {
1643
0
        runnable = new CancelChannelRunnable(mInterceptedChannel,
1644
0
                                             mRegistration,
1645
0
                                             NS_ERROR_INTERCEPTION_FAILED);
1646
0
      } else {
1647
0
        runnable = new ResumeRequest(mInterceptedChannel);
1648
0
      }
1649
0
1650
0
      MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(runnable.forget()));
1651
0
    }
1652
0
1653
0
    return true;
1654
0
  }
1655
};
1656
1657
NS_IMPL_ISUPPORTS_INHERITED(FetchEventRunnable, WorkerRunnable, nsIHttpHeaderVisitor)
1658
1659
} // anonymous namespace
1660
1661
nsresult
1662
ServiceWorkerPrivate::SendFetchEvent(nsIInterceptedChannel* aChannel,
1663
                                     nsILoadGroup* aLoadGroup,
1664
                                     const nsAString& aClientId, bool aIsReload)
1665
0
{
1666
0
  MOZ_ASSERT(NS_IsMainThread());
1667
0
1668
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
1669
0
  if (NS_WARN_IF(!mInfo || !swm)) {
1670
0
    return NS_ERROR_FAILURE;
1671
0
  }
1672
0
1673
0
  RefPtr<ServiceWorkerRegistrationInfo> registration =
1674
0
    swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
1675
0
1676
0
  // Its possible the registration is removed between starting the interception
1677
0
  // and actually dispatching the fetch event.  In these cases we simply
1678
0
  // want to restart the original network request.  Since this is a normal
1679
0
  // condition we handle the reset here instead of returning an error which
1680
0
  // would in turn trigger a console report.
1681
0
  if (!registration) {
1682
0
    nsresult rv = aChannel->ResetInterception();
1683
0
    if (NS_FAILED(rv)) {
1684
0
      NS_WARNING("Failed to resume intercepted network request");
1685
0
      aChannel->CancelInterception(rv);
1686
0
    }
1687
0
    return NS_OK;
1688
0
  }
1689
0
1690
0
  // Handle Fetch algorithm - step 16. If the service worker didn't register
1691
0
  // any fetch event handlers, then abort the interception and maybe trigger
1692
0
  // the soft update algorithm.
1693
0
  if (!mInfo->HandlesFetch()) {
1694
0
    nsresult rv = aChannel->ResetInterception();
1695
0
    if (NS_FAILED(rv)) {
1696
0
      NS_WARNING("Failed to resume intercepted network request");
1697
0
      aChannel->CancelInterception(rv);
1698
0
    }
1699
0
1700
0
    // Trigger soft updates if necessary.
1701
0
    registration->MaybeScheduleTimeCheckAndUpdate();
1702
0
1703
0
    return NS_OK;
1704
0
  }
1705
0
1706
0
  aChannel->SetLaunchServiceWorkerStart(TimeStamp::Now());
1707
0
  aChannel->SetDispatchFetchEventStart(TimeStamp::Now());
1708
0
1709
0
  bool newWorkerCreated = false;
1710
0
  nsresult rv = SpawnWorkerIfNeeded(FetchEvent,
1711
0
                                    &newWorkerCreated,
1712
0
                                    aLoadGroup);
1713
0
  NS_ENSURE_SUCCESS(rv, rv);
1714
0
1715
0
  if (!newWorkerCreated) {
1716
0
    aChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
1717
0
  }
1718
0
1719
0
  nsMainThreadPtrHandle<nsIInterceptedChannel> handle(
1720
0
    new nsMainThreadPtrHolder<nsIInterceptedChannel>(
1721
0
      "nsIInterceptedChannel", aChannel, false));
1722
0
1723
0
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
1724
0
    new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
1725
0
      "ServiceWorkerRegistrationInfoProxy", registration, false));
1726
0
1727
0
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1728
0
1729
0
1730
0
  RefPtr<FetchEventRunnable> r =
1731
0
    new FetchEventRunnable(mWorkerPrivate, token, handle,
1732
0
                           mInfo->ScriptSpec(), regInfo,
1733
0
                           aClientId, aIsReload, newWorkerCreated);
1734
0
  rv = r->Init();
1735
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1736
0
    return rv;
1737
0
  }
1738
0
1739
0
  if (mInfo->State() == ServiceWorkerState::Activating) {
1740
0
    mPendingFunctionalEvents.AppendElement(r.forget());
1741
0
    return NS_OK;
1742
0
  }
1743
0
1744
0
  MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
1745
0
1746
0
  if (NS_WARN_IF(!r->Dispatch())) {
1747
0
    return NS_ERROR_FAILURE;
1748
0
  }
1749
0
1750
0
  return NS_OK;
1751
0
}
1752
1753
nsresult
1754
ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
1755
                                          bool* aNewWorkerCreated,
1756
                                          nsILoadGroup* aLoadGroup)
1757
0
{
1758
0
  MOZ_ASSERT(NS_IsMainThread());
1759
0
1760
0
  // Defaults to no new worker created, but if there is one, we'll set the value
1761
0
  // to true at the end of this function.
1762
0
  if (aNewWorkerCreated) {
1763
0
    *aNewWorkerCreated = false;
1764
0
  }
1765
0
1766
0
  // If the worker started shutting down on itself we may have a stale
1767
0
  // reference here.  Invoke our termination code to clean it out.
1768
0
  if (mWorkerPrivate && mWorkerPrivate->ParentStatusProtected() > Running) {
1769
0
    TerminateWorker();
1770
0
    MOZ_DIAGNOSTIC_ASSERT(!mWorkerPrivate);
1771
0
  }
1772
0
1773
0
  if (mWorkerPrivate) {
1774
0
    // If we have a load group here then use it to update the service worker
1775
0
    // load group.  This was added when we needed the load group's tab child
1776
0
    // to pass some security checks.  Those security checks are gone, though,
1777
0
    // and we could possibly remove this now.  For now we just do it
1778
0
    // opportunistically.  When the service worker is running in a separate
1779
0
    // process from the client that initiated the intercepted channel, then
1780
0
    // the load group will be nullptr.  UpdateOverrideLoadGroup ignores nullptr
1781
0
    // load groups.
1782
0
    mWorkerPrivate->UpdateOverridenLoadGroup(aLoadGroup);
1783
0
    RenewKeepAliveToken(aWhy);
1784
0
1785
0
    return NS_OK;
1786
0
  }
1787
0
1788
0
  // Sanity check: mSupportsArray should be empty if we're about to
1789
0
  // spin up a new worker.
1790
0
  MOZ_ASSERT(mSupportsArray.IsEmpty());
1791
0
1792
0
  if (NS_WARN_IF(!mInfo)) {
1793
0
    NS_WARNING("Trying to wake up a dead service worker.");
1794
0
    return NS_ERROR_FAILURE;
1795
0
  }
1796
0
1797
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
1798
0
  NS_ENSURE_TRUE(swm, NS_ERROR_FAILURE);
1799
0
1800
0
  RefPtr<ServiceWorkerRegistrationInfo> reg =
1801
0
    swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
1802
0
  NS_ENSURE_TRUE(reg, NS_ERROR_FAILURE);
1803
0
1804
0
  // TODO(catalinb): Bug 1192138 - Add telemetry for service worker wake-ups.
1805
0
1806
0
  // Ensure that the IndexedDatabaseManager is initialized
1807
0
  Unused << NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate());
1808
0
1809
0
  WorkerLoadInfo info;
1810
0
  nsresult rv = NS_NewURI(getter_AddRefs(info.mBaseURI), mInfo->ScriptSpec(),
1811
0
                          nullptr, nullptr);
1812
0
1813
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1814
0
    return rv;
1815
0
  }
1816
0
1817
0
  info.mResolvedScriptURI = info.mBaseURI;
1818
0
  MOZ_ASSERT(!mInfo->CacheName().IsEmpty());
1819
0
  info.mServiceWorkerCacheName = mInfo->CacheName();
1820
0
1821
0
  info.mServiceWorkerDescriptor.emplace(mInfo->Descriptor());
1822
0
  info.mServiceWorkerRegistrationDescriptor.emplace(reg->Descriptor());
1823
0
1824
0
  info.mLoadGroup = aLoadGroup;
1825
0
1826
0
  // If we are loading a script for a ServiceWorker then we must not
1827
0
  // try to intercept it.  If the interception matches the current
1828
0
  // ServiceWorker's scope then we could deadlock the load.
1829
0
  info.mLoadFlags = mInfo->GetImportsLoadFlags() |
1830
0
                    nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
1831
0
1832
0
  rv = info.mBaseURI->GetHost(info.mDomain);
1833
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1834
0
    return rv;
1835
0
  }
1836
0
1837
0
  nsCOMPtr<nsIURI> uri;
1838
0
  rv = mInfo->Principal()->GetURI(getter_AddRefs(uri));
1839
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1840
0
    return rv;
1841
0
  }
1842
0
1843
0
  if (NS_WARN_IF(!uri)) {
1844
0
    return NS_ERROR_FAILURE;
1845
0
  }
1846
0
1847
0
  // Create a pristine codebase principal to avoid any possibility of inheriting
1848
0
  // CSP values.  The principal on the registration may be polluted with CSP
1849
0
  // from the registering page or other places the principal is passed.  If
1850
0
  // bug 965637 is ever fixed this can be removed.
1851
0
  info.mPrincipal =
1852
0
    BasePrincipal::CreateCodebasePrincipal(uri, mInfo->GetOriginAttributes());
1853
0
  if (NS_WARN_IF(!info.mPrincipal)) {
1854
0
    return NS_ERROR_FAILURE;
1855
0
  }
1856
0
  info.mLoadingPrincipal = info.mPrincipal;
1857
0
1858
0
  nsContentUtils::StorageAccess access =
1859
0
    nsContentUtils::StorageAllowedForPrincipal(info.mPrincipal);
1860
0
  info.mStorageAllowed = access > nsContentUtils::StorageAccess::ePrivateBrowsing;
1861
0
  info.mOriginAttributes = mInfo->GetOriginAttributes();
1862
0
1863
0
  // Verify that we don't have any CSP on pristine principal.
1864
0
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1865
0
  nsCOMPtr<nsIContentSecurityPolicy> csp;
1866
0
  Unused << info.mPrincipal->GetCsp(getter_AddRefs(csp));
1867
0
  MOZ_DIAGNOSTIC_ASSERT(!csp);
1868
0
#endif
1869
0
1870
0
  // Default CSP permissions for now.  These will be overrided if necessary
1871
0
  // based on the script CSP headers during load in ScriptLoader.
1872
0
  info.mEvalAllowed = true;
1873
0
  info.mReportCSPViolations = false;
1874
0
1875
0
  WorkerPrivate::OverrideLoadInfoLoadGroup(info, info.mPrincipal);
1876
0
1877
0
  rv = info.SetPrincipalOnMainThread(info.mPrincipal, info.mLoadGroup);
1878
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
1879
0
    return rv;
1880
0
  }
1881
0
1882
0
  AutoJSAPI jsapi;
1883
0
  jsapi.Init();
1884
0
  ErrorResult error;
1885
0
  NS_ConvertUTF8toUTF16 scriptSpec(mInfo->ScriptSpec());
1886
0
1887
0
  mWorkerPrivate = WorkerPrivate::Constructor(jsapi.cx(),
1888
0
                                              scriptSpec,
1889
0
                                              false, WorkerTypeService,
1890
0
                                              VoidString(),
1891
0
                                              EmptyCString(),
1892
0
                                              &info, error);
1893
0
  if (NS_WARN_IF(error.Failed())) {
1894
0
    return error.StealNSResult();
1895
0
  }
1896
0
1897
0
  RenewKeepAliveToken(aWhy);
1898
0
1899
0
  if (aNewWorkerCreated) {
1900
0
    *aNewWorkerCreated = true;
1901
0
  }
1902
0
1903
0
  return NS_OK;
1904
0
}
1905
1906
bool
1907
ServiceWorkerPrivate::MaybeStoreISupports(nsISupports* aSupports)
1908
0
{
1909
0
  MOZ_ASSERT(NS_IsMainThread());
1910
0
1911
0
  if (!mWorkerPrivate) {
1912
0
    MOZ_DIAGNOSTIC_ASSERT(mSupportsArray.IsEmpty());
1913
0
    return false;
1914
0
  }
1915
0
1916
0
  MOZ_ASSERT(!mSupportsArray.Contains(aSupports));
1917
0
  mSupportsArray.AppendElement(aSupports);
1918
0
  return true;
1919
0
}
1920
1921
void
1922
ServiceWorkerPrivate::RemoveISupports(nsISupports* aSupports)
1923
0
{
1924
0
  MOZ_ASSERT(NS_IsMainThread());
1925
0
  mSupportsArray.RemoveElement(aSupports);
1926
0
}
1927
1928
void
1929
ServiceWorkerPrivate::TerminateWorker()
1930
0
{
1931
0
  MOZ_ASSERT(NS_IsMainThread());
1932
0
1933
0
  mIdleWorkerTimer->Cancel();
1934
0
  mIdleKeepAliveToken = nullptr;
1935
0
  if (mWorkerPrivate) {
1936
0
    if (StaticPrefs::dom_serviceWorkers_testing_enabled()) {
1937
0
      nsCOMPtr<nsIObserverService> os = services::GetObserverService();
1938
0
      if (os) {
1939
0
        os->NotifyObservers(nullptr, "service-worker-shutdown", nullptr);
1940
0
      }
1941
0
    }
1942
0
1943
0
    Unused << NS_WARN_IF(!mWorkerPrivate->Cancel());
1944
0
    RefPtr<WorkerPrivate> workerPrivate(mWorkerPrivate.forget());
1945
0
    mSupportsArray.Clear();
1946
0
1947
0
    // Any pending events are never going to fire on this worker.  Cancel
1948
0
    // them so that intercepted channels can be reset and other resources
1949
0
    // cleaned up.
1950
0
    nsTArray<RefPtr<WorkerRunnable>> pendingEvents;
1951
0
    mPendingFunctionalEvents.SwapElements(pendingEvents);
1952
0
    for (uint32_t i = 0; i < pendingEvents.Length(); ++i) {
1953
0
      pendingEvents[i]->Cancel();
1954
0
    }
1955
0
  }
1956
0
}
1957
1958
void
1959
ServiceWorkerPrivate::NoteDeadServiceWorkerInfo()
1960
0
{
1961
0
  MOZ_ASSERT(NS_IsMainThread());
1962
0
  mInfo = nullptr;
1963
0
  TerminateWorker();
1964
0
}
1965
1966
namespace {
1967
1968
class UpdateStateControlRunnable final : public MainThreadWorkerControlRunnable
1969
{
1970
  const ServiceWorkerState mState;
1971
1972
  bool
1973
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
1974
0
  {
1975
0
    MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
1976
0
    aWorkerPrivate->UpdateServiceWorkerState(mState);
1977
0
    return true;
1978
0
  }
1979
1980
public:
1981
  UpdateStateControlRunnable(WorkerPrivate* aWorkerPrivate,
1982
                             ServiceWorkerState aState)
1983
    : MainThreadWorkerControlRunnable(aWorkerPrivate)
1984
    , mState(aState)
1985
0
  {
1986
0
  }
1987
};
1988
1989
} // anonymous namespace
1990
1991
void
1992
ServiceWorkerPrivate::UpdateState(ServiceWorkerState aState)
1993
0
{
1994
0
  MOZ_ASSERT(NS_IsMainThread());
1995
0
1996
0
  if (!mWorkerPrivate) {
1997
0
    MOZ_DIAGNOSTIC_ASSERT(mPendingFunctionalEvents.IsEmpty());
1998
0
    return;
1999
0
  }
2000
0
2001
0
  RefPtr<WorkerRunnable> r =
2002
0
    new UpdateStateControlRunnable(mWorkerPrivate, aState);
2003
0
  Unused << r->Dispatch();
2004
0
2005
0
  if (aState != ServiceWorkerState::Activated) {
2006
0
    return;
2007
0
  }
2008
0
2009
0
  nsTArray<RefPtr<WorkerRunnable>> pendingEvents;
2010
0
  mPendingFunctionalEvents.SwapElements(pendingEvents);
2011
0
2012
0
  for (uint32_t i = 0; i < pendingEvents.Length(); ++i) {
2013
0
    RefPtr<WorkerRunnable> r = pendingEvents[i].forget();
2014
0
    if (NS_WARN_IF(!r->Dispatch())) {
2015
0
      NS_WARNING("Failed to dispatch pending functional event!");
2016
0
    }
2017
0
  }
2018
0
}
2019
2020
nsresult
2021
ServiceWorkerPrivate::GetDebugger(nsIWorkerDebugger** aResult)
2022
0
{
2023
0
  MOZ_ASSERT(NS_IsMainThread());
2024
0
  MOZ_ASSERT(aResult);
2025
0
2026
0
  if (!mDebuggerCount) {
2027
0
    return NS_OK;
2028
0
  }
2029
0
2030
0
  MOZ_ASSERT(mWorkerPrivate);
2031
0
2032
0
  nsCOMPtr<nsIWorkerDebugger> debugger = do_QueryInterface(mWorkerPrivate->Debugger());
2033
0
  debugger.forget(aResult);
2034
0
2035
0
  return NS_OK;
2036
0
}
2037
2038
nsresult
2039
ServiceWorkerPrivate::AttachDebugger()
2040
0
{
2041
0
  MOZ_ASSERT(NS_IsMainThread());
2042
0
2043
0
  // When the first debugger attaches to a worker, we spawn a worker if needed,
2044
0
  // and cancel the idle timeout. The idle timeout should not be reset until
2045
0
  // the last debugger detached from the worker.
2046
0
  if (!mDebuggerCount) {
2047
0
    nsresult rv = SpawnWorkerIfNeeded(AttachEvent);
2048
0
    NS_ENSURE_SUCCESS(rv, rv);
2049
0
2050
0
    mIdleWorkerTimer->Cancel();
2051
0
  }
2052
0
2053
0
  ++mDebuggerCount;
2054
0
2055
0
  return NS_OK;
2056
0
}
2057
2058
nsresult
2059
ServiceWorkerPrivate::DetachDebugger()
2060
0
{
2061
0
  MOZ_ASSERT(NS_IsMainThread());
2062
0
2063
0
  if (!mDebuggerCount) {
2064
0
    return NS_ERROR_UNEXPECTED;
2065
0
  }
2066
0
2067
0
  --mDebuggerCount;
2068
0
2069
0
  // When the last debugger detaches from a worker, we either reset the idle
2070
0
  // timeout, or terminate the worker if there are no more active tokens.
2071
0
  if (!mDebuggerCount) {
2072
0
    if (mTokenCount) {
2073
0
      ResetIdleTimeout();
2074
0
    } else {
2075
0
      TerminateWorker();
2076
0
    }
2077
0
  }
2078
0
2079
0
  return NS_OK;
2080
0
}
2081
2082
bool
2083
ServiceWorkerPrivate::IsIdle() const
2084
0
{
2085
0
  MOZ_ASSERT(NS_IsMainThread());
2086
0
  return mTokenCount == 0 || (mTokenCount == 1 && mIdleKeepAliveToken);
2087
0
}
2088
2089
namespace {
2090
2091
class ServiceWorkerPrivateTimerCallback final : public nsITimerCallback
2092
                                              , public nsINamed
2093
{
2094
public:
2095
  typedef void (ServiceWorkerPrivate::*Method)(nsITimer*);
2096
2097
  ServiceWorkerPrivateTimerCallback(ServiceWorkerPrivate* aServiceWorkerPrivate,
2098
                                    Method aMethod)
2099
    : mServiceWorkerPrivate(aServiceWorkerPrivate)
2100
    , mMethod(aMethod)
2101
0
  {
2102
0
  }
2103
2104
  NS_IMETHOD
2105
  Notify(nsITimer* aTimer) override
2106
0
  {
2107
0
    (mServiceWorkerPrivate->*mMethod)(aTimer);
2108
0
    mServiceWorkerPrivate = nullptr;
2109
0
    return NS_OK;
2110
0
  }
2111
2112
  NS_IMETHOD
2113
  GetName(nsACString& aName) override
2114
0
  {
2115
0
    aName.AssignLiteral("ServiceWorkerPrivateTimerCallback");
2116
0
    return NS_OK;
2117
0
  }
2118
2119
private:
2120
0
  ~ServiceWorkerPrivateTimerCallback() = default;
2121
2122
  RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
2123
  Method mMethod;
2124
2125
  NS_DECL_THREADSAFE_ISUPPORTS
2126
};
2127
2128
NS_IMPL_ISUPPORTS(ServiceWorkerPrivateTimerCallback, nsITimerCallback, nsINamed);
2129
2130
} // anonymous namespace
2131
2132
void
2133
ServiceWorkerPrivate::NoteIdleWorkerCallback(nsITimer* aTimer)
2134
0
{
2135
0
  MOZ_ASSERT(NS_IsMainThread());
2136
0
2137
0
  MOZ_ASSERT(aTimer == mIdleWorkerTimer, "Invalid timer!");
2138
0
2139
0
  // Release ServiceWorkerPrivate's token, since the grace period has ended.
2140
0
  mIdleKeepAliveToken = nullptr;
2141
0
2142
0
  if (mWorkerPrivate) {
2143
0
    // If we still have a workerPrivate at this point it means there are pending
2144
0
    // waitUntil promises. Wait a bit more until we forcibly terminate the
2145
0
    // worker.
2146
0
    uint32_t timeout = Preferences::GetInt("dom.serviceWorkers.idle_extended_timeout");
2147
0
    nsCOMPtr<nsITimerCallback> cb = new ServiceWorkerPrivateTimerCallback(
2148
0
      this, &ServiceWorkerPrivate::TerminateWorkerCallback);
2149
0
    DebugOnly<nsresult> rv =
2150
0
      mIdleWorkerTimer->InitWithCallback(cb, timeout, nsITimer::TYPE_ONE_SHOT);
2151
0
    MOZ_ASSERT(NS_SUCCEEDED(rv));
2152
0
  }
2153
0
}
2154
2155
void
2156
ServiceWorkerPrivate::TerminateWorkerCallback(nsITimer* aTimer)
2157
0
{
2158
0
  MOZ_ASSERT(NS_IsMainThread());
2159
0
2160
0
  MOZ_ASSERT(aTimer == this->mIdleWorkerTimer, "Invalid timer!");
2161
0
2162
0
  // mInfo must be non-null at this point because NoteDeadServiceWorkerInfo
2163
0
  // which zeroes it calls TerminateWorker which cancels our timer which will
2164
0
  // ensure we don't get invoked even if the nsTimerEvent is in the event queue.
2165
0
  ServiceWorkerManager::LocalizeAndReportToAllClients(
2166
0
    mInfo->Scope(),
2167
0
    "ServiceWorkerGraceTimeoutTermination",
2168
0
    nsTArray<nsString> { NS_ConvertUTF8toUTF16(mInfo->Scope()) });
2169
0
2170
0
  TerminateWorker();
2171
0
}
2172
2173
void
2174
ServiceWorkerPrivate::RenewKeepAliveToken(WakeUpReason aWhy)
2175
0
{
2176
0
  // We should have an active worker if we're renewing the keep alive token.
2177
0
  MOZ_ASSERT(mWorkerPrivate);
2178
0
2179
0
  // If there is at least one debugger attached to the worker, the idle worker
2180
0
  // timeout was canceled when the first debugger attached to the worker. It
2181
0
  // should not be reset until the last debugger detaches from the worker.
2182
0
  if (!mDebuggerCount) {
2183
0
    ResetIdleTimeout();
2184
0
  }
2185
0
2186
0
  if (!mIdleKeepAliveToken) {
2187
0
    mIdleKeepAliveToken = new KeepAliveToken(this);
2188
0
  }
2189
0
}
2190
2191
void
2192
ServiceWorkerPrivate::ResetIdleTimeout()
2193
0
{
2194
0
  uint32_t timeout = Preferences::GetInt("dom.serviceWorkers.idle_timeout");
2195
0
  nsCOMPtr<nsITimerCallback> cb = new ServiceWorkerPrivateTimerCallback(
2196
0
    this, &ServiceWorkerPrivate::NoteIdleWorkerCallback);
2197
0
  DebugOnly<nsresult> rv =
2198
0
    mIdleWorkerTimer->InitWithCallback(cb, timeout, nsITimer::TYPE_ONE_SHOT);
2199
0
  MOZ_ASSERT(NS_SUCCEEDED(rv));
2200
0
}
2201
2202
void
2203
ServiceWorkerPrivate::AddToken()
2204
0
{
2205
0
  MOZ_ASSERT(NS_IsMainThread());
2206
0
  ++mTokenCount;
2207
0
}
2208
2209
void
2210
ServiceWorkerPrivate::ReleaseToken()
2211
0
{
2212
0
  MOZ_ASSERT(NS_IsMainThread());
2213
0
2214
0
  MOZ_ASSERT(mTokenCount > 0);
2215
0
  --mTokenCount;
2216
0
  if (!mTokenCount) {
2217
0
    TerminateWorker();
2218
0
  }
2219
0
2220
0
  // mInfo can be nullptr here if NoteDeadServiceWorkerInfo() is called while
2221
0
  // the KeepAliveToken is being proxy released as a runnable.
2222
0
  else if (mInfo && IsIdle()) {
2223
0
    RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
2224
0
    if (swm) {
2225
0
      swm->WorkerIsIdle(mInfo);
2226
0
    }
2227
0
  }
2228
0
}
2229
2230
already_AddRefed<KeepAliveToken>
2231
ServiceWorkerPrivate::CreateEventKeepAliveToken()
2232
0
{
2233
0
  MOZ_ASSERT(NS_IsMainThread());
2234
0
  MOZ_ASSERT(mWorkerPrivate);
2235
0
  MOZ_ASSERT(mIdleKeepAliveToken);
2236
0
  RefPtr<KeepAliveToken> ref = new KeepAliveToken(this);
2237
0
  return ref.forget();
2238
0
}
2239
2240
void
2241
ServiceWorkerPrivate::SetHandlesFetch(bool aValue)
2242
0
{
2243
0
  MOZ_ASSERT(NS_IsMainThread());
2244
0
2245
0
  if (NS_WARN_IF(!mInfo)) {
2246
0
    return;
2247
0
  }
2248
0
2249
0
  mInfo->SetHandlesFetch(aValue);
2250
0
}
2251
2252
} // namespace dom
2253
} // namespace mozilla