summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp
blob: c0e2fa34178a6d3527aef2998f4beb79e1e64895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ServiceWorkerRegistrationImpl.h"

#include "ipc/ErrorIPCUtils.h"
#include "mozilla/dom/DOMMozPromiseRequestHolder.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/PushManagerBinding.h"
#include "mozilla/dom/PushManager.h"
#include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/Services.h"
#include "mozilla/Unused.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIPrincipal.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
#include "ServiceWorker.h"
#include "ServiceWorkerManager.h"
#include "ServiceWorkerPrivate.h"
#include "ServiceWorkerRegistration.h"
#include "ServiceWorkerUnregisterCallback.h"

#include "mozilla/dom/Document.h"
#include "nsIServiceWorkerManager.h"
#include "nsPIDOMWindow.h"
#include "nsContentUtils.h"

namespace mozilla {
namespace dom {

////////////////////////////////////////////////////
// Main Thread implementation

ServiceWorkerRegistrationMainThread::ServiceWorkerRegistrationMainThread(
    const ServiceWorkerRegistrationDescriptor& aDescriptor)
    : mOuter(nullptr),
      mDescriptor(aDescriptor),
      mScope(NS_ConvertUTF8toUTF16(aDescriptor.Scope())),
      mListeningForEvents(false) {
  MOZ_ASSERT(NS_IsMainThread());
}

ServiceWorkerRegistrationMainThread::~ServiceWorkerRegistrationMainThread() {
  MOZ_DIAGNOSTIC_ASSERT(!mListeningForEvents);
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
}

// XXXnsm, maybe this can be optimized to only add when a event handler is
// registered.
void ServiceWorkerRegistrationMainThread::StartListeningForEvents() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mListeningForEvents);
  MOZ_DIAGNOSTIC_ASSERT(!mInfo);

  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  NS_ENSURE_TRUE_VOID(swm);

  mInfo =
      swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
  NS_ENSURE_TRUE_VOID(mInfo);

  mInfo->AddInstance(this, mDescriptor);
  mListeningForEvents = true;
}

void ServiceWorkerRegistrationMainThread::StopListeningForEvents() {
  MOZ_ASSERT(NS_IsMainThread());
  if (!mListeningForEvents) {
    return;
  }

  MOZ_DIAGNOSTIC_ASSERT(mInfo);
  mInfo->RemoveInstance(this);
  mInfo = nullptr;

  mListeningForEvents = false;
}

void ServiceWorkerRegistrationMainThread::RegistrationClearedInternal() {
  MOZ_ASSERT(NS_IsMainThread());
  // Its possible for the binding object to be collected while we the
  // runnable to call this method is in the event queue.  Double check
  // whether there is still anything to do here.
  if (mOuter) {
    mOuter->RegistrationCleared();
  }
  StopListeningForEvents();
}

// NB: These functions use NS_ENSURE_TRUE_VOID to be noisy about preconditions
// that would otherwise cause things to silently not happen if they were false.
void ServiceWorkerRegistrationMainThread::UpdateState(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  NS_ENSURE_TRUE_VOID(mOuter);

  nsIGlobalObject* global = mOuter->GetParentObject();
  NS_ENSURE_TRUE_VOID(global);

  RefPtr<ServiceWorkerRegistrationMainThread> self = this;
  nsCOMPtr<nsIRunnable> r =
      NS_NewRunnableFunction("ServiceWorkerRegistrationMainThread::UpdateState",
                             [self, desc = std::move(aDescriptor)]() mutable {
                               self->mDescriptor = std::move(desc);
                               NS_ENSURE_TRUE_VOID(self->mOuter);
                               self->mOuter->UpdateState(self->mDescriptor);
                             });

  Unused << global->EventTargetFor(TaskCategory::Other)
                ->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
}

void ServiceWorkerRegistrationMainThread::FireUpdateFound() {
  NS_ENSURE_TRUE_VOID(mOuter);

  nsIGlobalObject* global = mOuter->GetParentObject();
  NS_ENSURE_TRUE_VOID(global);

  RefPtr<ServiceWorkerRegistrationMainThread> self = this;
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
      "ServiceWorkerRegistrationMainThread::FireUpdateFound", [self]() mutable {
        NS_ENSURE_TRUE_VOID(self->mOuter);
        self->mOuter->MaybeDispatchUpdateFoundRunnable();
      });

  Unused << global->EventTargetFor(TaskCategory::Other)
                ->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
}

void ServiceWorkerRegistrationMainThread::RegistrationCleared() {
  NS_ENSURE_TRUE_VOID(mOuter);

  nsIGlobalObject* global = mOuter->GetParentObject();
  NS_ENSURE_TRUE_VOID(global);

  // Queue a runnable to clean up the registration.  This is necessary
  // because there may be runnables in the event queue already to
  // update the registration state.  We want to let those run
  // if possible before clearing our mOuter reference.
  nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
      "ServiceWorkerRegistrationMainThread::RegistrationCleared", this,
      &ServiceWorkerRegistrationMainThread::RegistrationClearedInternal);

  Unused << global->EventTargetFor(TaskCategory::Other)
                ->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
}

bool ServiceWorkerRegistrationMainThread::MatchesDescriptor(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  return mOuter->MatchesDescriptor(aDescriptor);
}

void ServiceWorkerRegistrationMainThread::SetServiceWorkerRegistration(
    ServiceWorkerRegistration* aReg) {
  MOZ_DIAGNOSTIC_ASSERT(aReg);
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
  mOuter = aReg;
  StartListeningForEvents();
}

void ServiceWorkerRegistrationMainThread::ClearServiceWorkerRegistration(
    ServiceWorkerRegistration* aReg) {
  MOZ_ASSERT_IF(mOuter, mOuter == aReg);
  StopListeningForEvents();
  mOuter = nullptr;
}

namespace {

void UpdateInternal(nsIPrincipal* aPrincipal, const nsACString& aScope,
                    nsCString aNewestWorkerScriptUrl,
                    ServiceWorkerUpdateFinishCallback* aCallback) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aPrincipal);
  MOZ_ASSERT(aCallback);

  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  if (!swm) {
    // browser shutdown
    return;
  }

  swm->Update(aPrincipal, aScope, std::move(aNewestWorkerScriptUrl), aCallback);
}

class MainThreadUpdateCallback final
    : public ServiceWorkerUpdateFinishCallback {
  RefPtr<ServiceWorkerRegistrationPromise::Private> mPromise;

  ~MainThreadUpdateCallback() {
    mPromise->Reject(NS_ERROR_DOM_ABORT_ERR, __func__);
  }

 public:
  MainThreadUpdateCallback()
      : mPromise(new ServiceWorkerRegistrationPromise::Private(__func__)) {}

  void UpdateSucceeded(ServiceWorkerRegistrationInfo* aRegistration) override {
    mPromise->Resolve(aRegistration->Descriptor(), __func__);
  }

  void UpdateFailed(ErrorResult& aStatus) override {
    mPromise->Reject(std::move(aStatus), __func__);
  }

  RefPtr<ServiceWorkerRegistrationPromise> Promise() const { return mPromise; }
};

class WorkerThreadUpdateCallback final
    : public ServiceWorkerUpdateFinishCallback {
  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
  RefPtr<ServiceWorkerRegistrationPromise::Private> mPromise;

  ~WorkerThreadUpdateCallback() = default;

 public:
  WorkerThreadUpdateCallback(
      RefPtr<ThreadSafeWorkerRef>&& aWorkerRef,
      ServiceWorkerRegistrationPromise::Private* aPromise)
      : mWorkerRef(std::move(aWorkerRef)), mPromise(aPromise) {
    MOZ_ASSERT(NS_IsMainThread());
  }

  void UpdateSucceeded(ServiceWorkerRegistrationInfo* aRegistration) override {
    mPromise->Resolve(aRegistration->Descriptor(), __func__);
    mWorkerRef = nullptr;
  }

  void UpdateFailed(ErrorResult& aStatus) override {
    mPromise->Reject(std::move(aStatus), __func__);
    mWorkerRef = nullptr;
  }
};

class SWRUpdateRunnable final : public Runnable {
  class TimerCallback final : public nsITimerCallback {
    RefPtr<ServiceWorkerPrivate> mPrivate;
    RefPtr<Runnable> mRunnable;

   public:
    TimerCallback(ServiceWorkerPrivate* aPrivate, Runnable* aRunnable)
        : mPrivate(aPrivate), mRunnable(aRunnable) {
      MOZ_ASSERT(mPrivate);
      MOZ_ASSERT(aRunnable);
    }

    NS_IMETHOD
    Notify(nsITimer* aTimer) override {
      mRunnable->Run();
      mPrivate->RemoveISupports(aTimer);

      return NS_OK;
    }

    NS_DECL_THREADSAFE_ISUPPORTS

   private:
    ~TimerCallback() = default;
  };

 public:
  SWRUpdateRunnable(StrongWorkerRef* aWorkerRef,
                    ServiceWorkerRegistrationPromise::Private* aPromise,
                    const ServiceWorkerDescriptor& aDescriptor,
                    const nsCString& aNewestWorkerScriptUrl)
      : Runnable("dom::SWRUpdateRunnable"),
        mMutex("SWRUpdateRunnable"),
        mWorkerRef(new ThreadSafeWorkerRef(aWorkerRef)),
        mPromise(aPromise),
        mDescriptor(aDescriptor),
        mDelayed(false),
        mNewestWorkerScriptUrl(aNewestWorkerScriptUrl) {
    MOZ_DIAGNOSTIC_ASSERT(mWorkerRef);
    MOZ_DIAGNOSTIC_ASSERT(mPromise);
  }

  NS_IMETHOD
  Run() override {
    MOZ_ASSERT(NS_IsMainThread());
    ErrorResult result;

    auto principalOrErr = mDescriptor.GetPrincipal();
    if (NS_WARN_IF(principalOrErr.isErr())) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
      return NS_OK;
    }

    RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
    if (NS_WARN_IF(!swm)) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
      return NS_OK;
    }

    nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();

    // This will delay update jobs originating from a service worker thread.
    // We don't currently handle ServiceWorkerRegistration.update() from other
    // worker types. Also, we assume this registration matches self.registration
    // on the service worker global. This is ok for now because service worker
    // globals are the only worker contexts where we expose
    // ServiceWorkerRegistration.
    RefPtr<ServiceWorkerRegistrationInfo> registration =
        swm->GetRegistration(principal, mDescriptor.Scope());
    if (NS_WARN_IF(!registration)) {
      return NS_OK;
    }

    RefPtr<ServiceWorkerInfo> worker =
        registration->GetByDescriptor(mDescriptor);
    uint32_t delay = registration->GetUpdateDelay();

    // if we have a timer object, it means we've already been delayed once.
    if (delay && !mDelayed) {
      nsCOMPtr<nsITimerCallback> cb =
          new TimerCallback(worker->WorkerPrivate(), this);
      Result<nsCOMPtr<nsITimer>, nsresult> result =
          NS_NewTimerWithCallback(cb, delay, nsITimer::TYPE_ONE_SHOT);

      nsCOMPtr<nsITimer> timer = result.unwrapOr(nullptr);
      if (NS_WARN_IF(!timer)) {
        return NS_OK;
      }

      mDelayed = true;

      // We're storing the timer object on the calling service worker's private.
      // ServiceWorkerPrivate will drop the reference if the worker terminates,
      // which will cancel the timer.
      if (!worker->WorkerPrivate()->MaybeStoreISupports(timer)) {
        // The worker thread is already shutting down.  Just cancel the timer
        // and let the update runnable be destroyed.
        timer->Cancel();
        return NS_OK;
      }

      return NS_OK;
    }

    RefPtr<ServiceWorkerRegistrationPromise::Private> promise;
    {
      MutexAutoLock lock(mMutex);
      promise.swap(mPromise);
    }

    RefPtr<WorkerThreadUpdateCallback> cb =
        new WorkerThreadUpdateCallback(std::move(mWorkerRef), promise);
    UpdateInternal(principal, mDescriptor.Scope(),
                   std::move(mNewestWorkerScriptUrl), cb);

    return NS_OK;
  }

 private:
  ~SWRUpdateRunnable() {
    MutexAutoLock lock(mMutex);
    if (mPromise) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
    }
  }

  // Protects promise access across threads
  Mutex mMutex;

  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
  RefPtr<ServiceWorkerRegistrationPromise::Private> mPromise;
  const ServiceWorkerDescriptor mDescriptor;
  bool mDelayed;
  nsCString mNewestWorkerScriptUrl;
};

NS_IMPL_ISUPPORTS(SWRUpdateRunnable::TimerCallback, nsITimerCallback)

class WorkerUnregisterCallback final
    : public nsIServiceWorkerUnregisterCallback {
  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
  RefPtr<GenericPromise::Private> mPromise;

 public:
  NS_DECL_ISUPPORTS

  WorkerUnregisterCallback(RefPtr<ThreadSafeWorkerRef>&& aWorkerRef,
                           RefPtr<GenericPromise::Private>&& aPromise)
      : mWorkerRef(std::move(aWorkerRef)), mPromise(std::move(aPromise)) {
    MOZ_DIAGNOSTIC_ASSERT(mWorkerRef);
    MOZ_DIAGNOSTIC_ASSERT(mPromise);
  }

  NS_IMETHOD
  UnregisterSucceeded(bool aState) override {
    mPromise->Resolve(aState, __func__);
    mWorkerRef = nullptr;
    return NS_OK;
  }

  NS_IMETHOD
  UnregisterFailed() override {
    mPromise->Reject(NS_ERROR_DOM_SECURITY_ERR, __func__);
    mWorkerRef = nullptr;
    return NS_OK;
  }

 private:
  ~WorkerUnregisterCallback() = default;
};

NS_IMPL_ISUPPORTS(WorkerUnregisterCallback, nsIServiceWorkerUnregisterCallback);

/*
 * If the worker goes away, we still continue to unregister, but we don't try to
 * resolve the worker Promise (which doesn't exist by that point).
 */
class StartUnregisterRunnable final : public Runnable {
  // The promise is protected by the mutex.
  Mutex mMutex;

  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
  RefPtr<GenericPromise::Private> mPromise;
  const ServiceWorkerRegistrationDescriptor mDescriptor;

  ~StartUnregisterRunnable() {
    MutexAutoLock lock(mMutex);
    if (mPromise) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
    }
  }

 public:
  StartUnregisterRunnable(
      StrongWorkerRef* aWorkerRef, GenericPromise::Private* aPromise,
      const ServiceWorkerRegistrationDescriptor& aDescriptor)
      : Runnable("dom::StartUnregisterRunnable"),
        mMutex("StartUnregisterRunnable"),
        mWorkerRef(new ThreadSafeWorkerRef(aWorkerRef)),
        mPromise(aPromise),
        mDescriptor(aDescriptor) {
    MOZ_DIAGNOSTIC_ASSERT(mWorkerRef);
    MOZ_DIAGNOSTIC_ASSERT(mPromise);
  }

  NS_IMETHOD
  Run() override {
    MOZ_ASSERT(NS_IsMainThread());

    auto principalOrErr = mDescriptor.GetPrincipal();
    if (NS_WARN_IF(principalOrErr.isErr())) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
      return NS_OK;
    }

    nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();

    nsCOMPtr<nsIServiceWorkerManager> swm =
        mozilla::services::GetServiceWorkerManager();
    if (!swm) {
      mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
      return NS_OK;
    }

    RefPtr<GenericPromise::Private> promise;
    {
      MutexAutoLock lock(mMutex);
      promise = std::move(mPromise);
    }

    RefPtr<WorkerUnregisterCallback> cb =
        new WorkerUnregisterCallback(std::move(mWorkerRef), std::move(promise));

    nsresult rv = swm->Unregister(principal, cb,
                                  NS_ConvertUTF8toUTF16(mDescriptor.Scope()));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      mPromise->Reject(rv, __func__);
      return NS_OK;
    }

    return NS_OK;
  }
};

}  // namespace

void ServiceWorkerRegistrationMainThread::Update(
    const nsCString& aNewestWorkerScriptUrl,
    ServiceWorkerRegistrationCallback&& aSuccessCB,
    ServiceWorkerFailureCallback&& aFailureCB) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_DIAGNOSTIC_ASSERT(mOuter);

  nsIGlobalObject* global = mOuter->GetParentObject();
  if (!global) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  auto principalOrErr = mDescriptor.GetPrincipal();
  if (NS_WARN_IF(principalOrErr.isErr())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();

  RefPtr<MainThreadUpdateCallback> cb = new MainThreadUpdateCallback();
  UpdateInternal(principal, NS_ConvertUTF16toUTF8(mScope),
                 aNewestWorkerScriptUrl, cb);

  auto holder =
      MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(
          global);

  cb->Promise()
      ->Then(
          global->EventTargetFor(TaskCategory::Other), __func__,
          [successCB = std::move(aSuccessCB),
           holder](const ServiceWorkerRegistrationDescriptor& aDescriptor) {
            holder->Complete();
            successCB(aDescriptor);
          },
          [failureCB = std::move(aFailureCB),
           holder](const CopyableErrorResult& aRv) {
            holder->Complete();
            failureCB(CopyableErrorResult(aRv));
          })
      ->Track(*holder);
}

void ServiceWorkerRegistrationMainThread::Unregister(
    ServiceWorkerBoolCallback&& aSuccessCB,
    ServiceWorkerFailureCallback&& aFailureCB) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_DIAGNOSTIC_ASSERT(mOuter);

  nsIGlobalObject* global = mOuter->GetParentObject();
  if (!global) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  nsCOMPtr<nsIServiceWorkerManager> swm =
      mozilla::services::GetServiceWorkerManager();
  if (!swm) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  auto principalOrErr = mDescriptor.GetPrincipal();
  if (NS_WARN_IF(principalOrErr.isErr())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();

  RefPtr<UnregisterCallback> cb = new UnregisterCallback();

  nsresult rv = swm->Unregister(principal, cb,
                                NS_ConvertUTF8toUTF16(mDescriptor.Scope()));
  if (NS_FAILED(rv)) {
    aFailureCB(CopyableErrorResult(rv));
    return;
  }

  auto holder = MakeRefPtr<DOMMozPromiseRequestHolder<GenericPromise>>(global);

  cb->Promise()
      ->Then(
          global->EventTargetFor(TaskCategory::Other), __func__,
          [successCB = std::move(aSuccessCB), holder](bool aResult) {
            holder->Complete();
            successCB(aResult);
          },
          [failureCB = std::move(aFailureCB), holder](nsresult aRv) {
            holder->Complete();
            failureCB(CopyableErrorResult(aRv));
          })
      ->Track(*holder);
}

////////////////////////////////////////////////////
// Worker Thread implementation

class WorkerListener final : public ServiceWorkerRegistrationListener {
  ServiceWorkerRegistrationDescriptor mDescriptor;
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mInfo;
  nsCOMPtr<nsISerialEventTarget> mEventTarget;
  bool mListeningForEvents;

  // Set and unset on worker thread, used on main-thread and protected by mutex.
  ServiceWorkerRegistrationWorkerThread* mRegistration;

  Mutex mMutex;

 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerListener, override)

  WorkerListener(ServiceWorkerRegistrationWorkerThread* aReg,
                 const ServiceWorkerRegistrationDescriptor& aDescriptor,
                 nsISerialEventTarget* aEventTarget)
      : mDescriptor(aDescriptor),
        mEventTarget(aEventTarget),
        mListeningForEvents(false),
        mRegistration(aReg),
        mMutex("WorkerListener::mMutex") {
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
    MOZ_ASSERT(mEventTarget);
    MOZ_ASSERT(mRegistration);
  }

  void StartListeningForEvents() {
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_DIAGNOSTIC_ASSERT(!mListeningForEvents);
    MOZ_DIAGNOSTIC_ASSERT(!mInfo);

    RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
    NS_ENSURE_TRUE_VOID(swm);

    RefPtr<ServiceWorkerRegistrationInfo> info =
        swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
    NS_ENSURE_TRUE_VOID(info);

    mInfo = new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
        "WorkerListener::mInfo", info);

    mInfo->AddInstance(this, mDescriptor);
    mListeningForEvents = true;
  }

  void StopListeningForEvents() {
    MOZ_ASSERT(NS_IsMainThread());

    if (!mListeningForEvents) {
      return;
    }

    MOZ_DIAGNOSTIC_ASSERT(mInfo);
    mInfo->RemoveInstance(this);
    mListeningForEvents = false;
  }

  // ServiceWorkerRegistrationListener
  void UpdateState(
      const ServiceWorkerRegistrationDescriptor& aDescriptor) override {
    MOZ_ASSERT(NS_IsMainThread());

    mDescriptor = aDescriptor;

    nsCOMPtr<nsIRunnable> r =
        NewCancelableRunnableMethod<ServiceWorkerRegistrationDescriptor>(
            "WorkerListener::UpdateState", this,
            &WorkerListener::UpdateStateOnWorkerThread, aDescriptor);

    Unused << mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
  }

  void FireUpdateFound() override {
    MOZ_ASSERT(NS_IsMainThread());

    nsCOMPtr<nsIRunnable> r = NewCancelableRunnableMethod(
        "WorkerListener::FireUpdateFound", this,
        &WorkerListener::FireUpdateFoundOnWorkerThread);

    Unused << mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
  }

  void UpdateStateOnWorkerThread(
      const ServiceWorkerRegistrationDescriptor& aDescriptor) {
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
    if (mRegistration) {
      mRegistration->UpdateState(aDescriptor);
    }
  }

  void FireUpdateFoundOnWorkerThread() {
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
    if (mRegistration) {
      mRegistration->FireUpdateFound();
    }
  }

  void RegistrationCleared() override;

  void GetScope(nsAString& aScope) const override {
    CopyUTF8toUTF16(mDescriptor.Scope(), aScope);
  }

  bool MatchesDescriptor(
      const ServiceWorkerRegistrationDescriptor& aDescriptor) override {
    // TODO: Not implemented
    return false;
  }

  void ClearRegistration() {
    MOZ_ASSERT(IsCurrentThreadRunningWorker());
    MutexAutoLock lock(mMutex);
    mRegistration = nullptr;
  }

 private:
  ~WorkerListener() { MOZ_ASSERT(!mListeningForEvents); }
};

ServiceWorkerRegistrationWorkerThread::ServiceWorkerRegistrationWorkerThread(
    const ServiceWorkerRegistrationDescriptor& aDescriptor)
    : mOuter(nullptr),
      mDescriptor(aDescriptor),
      mScope(NS_ConvertUTF8toUTF16(aDescriptor.Scope())) {}

ServiceWorkerRegistrationWorkerThread::
    ~ServiceWorkerRegistrationWorkerThread() {
  MOZ_DIAGNOSTIC_ASSERT(!mListener);
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
}

void ServiceWorkerRegistrationWorkerThread::RegistrationCleared() {
  // The SWM notifying us that the registration was removed on the MT may
  // race with ClearServiceWorkerRegistration() on the worker thread.  So
  // double-check that mOuter is still valid.
  if (mOuter) {
    mOuter->RegistrationCleared();
  }
}

void ServiceWorkerRegistrationWorkerThread::SetServiceWorkerRegistration(
    ServiceWorkerRegistration* aReg) {
  MOZ_DIAGNOSTIC_ASSERT(aReg);
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
  mOuter = aReg;
  InitListener();
}

void ServiceWorkerRegistrationWorkerThread::ClearServiceWorkerRegistration(
    ServiceWorkerRegistration* aReg) {
  MOZ_ASSERT_IF(mOuter, mOuter == aReg);
  ReleaseListener();
  mOuter = nullptr;
}

void ServiceWorkerRegistrationWorkerThread::Update(
    const nsCString& aNewestWorkerScriptUrl,
    ServiceWorkerRegistrationCallback&& aSuccessCB,
    ServiceWorkerFailureCallback&& aFailureCB) {
  if (NS_WARN_IF(!mWorkerRef->GetPrivate())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
      mWorkerRef->GetPrivate(), "ServiceWorkerRegistration::Update");
  if (NS_WARN_IF(!workerRef)) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  nsIGlobalObject* global = workerRef->Private()->GlobalScope();
  if (NS_WARN_IF(!global)) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  // Eventually we need to support all workers, but for right now this
  // code assumes we're on a service worker global as self.registration.
  if (NS_WARN_IF(!workerRef->Private()->IsServiceWorker())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  // Avoid infinite update loops by ignoring update() calls during top
  // level script evaluation.  See:
  // https://github.com/slightlyoff/ServiceWorker/issues/800
  if (workerRef->Private()->IsLoadingWorkerScript()) {
    aSuccessCB(mDescriptor);
    return;
  }

  auto promise =
      MakeRefPtr<ServiceWorkerRegistrationPromise::Private>(__func__);
  auto holder =
      MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(
          global);

  promise
      ->Then(
          global->EventTargetFor(TaskCategory::Other), __func__,
          [successCB = std::move(aSuccessCB),
           holder](const ServiceWorkerRegistrationDescriptor& aDescriptor) {
            holder->Complete();
            successCB(aDescriptor);
          },
          [failureCB = std::move(aFailureCB),
           holder](const CopyableErrorResult& aRv) {
            holder->Complete();
            failureCB(CopyableErrorResult(aRv));
          })
      ->Track(*holder);

  RefPtr<SWRUpdateRunnable> r = new SWRUpdateRunnable(
      workerRef, promise, workerRef->Private()->GetServiceWorkerDescriptor(),
      aNewestWorkerScriptUrl);

  nsresult rv = workerRef->Private()->DispatchToMainThread(r.forget());
  if (NS_FAILED(rv)) {
    promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
    return;
  }
}

void ServiceWorkerRegistrationWorkerThread::Unregister(
    ServiceWorkerBoolCallback&& aSuccessCB,
    ServiceWorkerFailureCallback&& aFailureCB) {
  if (NS_WARN_IF(!mWorkerRef->GetPrivate())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  RefPtr<StrongWorkerRef> workerRef =
      StrongWorkerRef::Create(mWorkerRef->GetPrivate(), __func__);
  if (NS_WARN_IF(!workerRef)) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  // Eventually we need to support all workers, but for right now this
  // code assumes we're on a service worker global as self.registration.
  if (NS_WARN_IF(!workerRef->Private()->IsServiceWorker())) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  nsIGlobalObject* global = workerRef->Private()->GlobalScope();
  if (!global) {
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
    return;
  }

  auto promise = MakeRefPtr<GenericPromise::Private>(__func__);
  auto holder = MakeRefPtr<DOMMozPromiseRequestHolder<GenericPromise>>(global);

  promise
      ->Then(
          global->EventTargetFor(TaskCategory::Other), __func__,
          [successCB = std::move(aSuccessCB), holder](bool aResult) {
            holder->Complete();
            successCB(aResult);
          },
          [failureCB = std::move(aFailureCB), holder](nsresult aRv) {
            holder->Complete();
            failureCB(CopyableErrorResult(aRv));
          })
      ->Track(*holder);

  RefPtr<StartUnregisterRunnable> r =
      new StartUnregisterRunnable(workerRef, promise, mDescriptor);

  nsresult rv = workerRef->Private()->DispatchToMainThread(r);
  if (NS_FAILED(rv)) {
    promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
    return;
  }
}

void ServiceWorkerRegistrationWorkerThread::InitListener() {
  MOZ_ASSERT(!mListener);
  WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
  MOZ_ASSERT(worker);
  worker->AssertIsOnWorkerThread();

  RefPtr<ServiceWorkerRegistrationWorkerThread> self = this;
  mWorkerRef = WeakWorkerRef::Create(worker, [self]() {
    self->ReleaseListener();

    // Break the ref-cycle immediately when the worker thread starts to
    // teardown.  We must make sure its GC'd before the worker RuntimeService is
    // destroyed.  The WorkerListener may not be able to post a runnable
    // clearing this value after shutdown begins and thus delaying cleanup too
    // late.
    self->mOuter = nullptr;
  });

  if (NS_WARN_IF(!mWorkerRef)) {
    return;
  }

  mListener =
      new WorkerListener(this, mDescriptor, worker->HybridEventTarget());

  nsCOMPtr<nsIRunnable> r =
      NewRunnableMethod("dom::WorkerListener::StartListeningForEvents",
                        mListener, &WorkerListener::StartListeningForEvents);
  MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget()));
}

void ServiceWorkerRegistrationWorkerThread::ReleaseListener() {
  if (!mListener) {
    return;
  }

  MOZ_ASSERT(IsCurrentThreadRunningWorker());

  mListener->ClearRegistration();

  nsCOMPtr<nsIRunnable> r = NewCancelableRunnableMethod(
      "dom::WorkerListener::StopListeningForEvents", mListener,
      &WorkerListener::StopListeningForEvents);
  // Calling GetPrivate() is safe because this method is called when the
  // WorkerRef is notified.
  MOZ_ALWAYS_SUCCEEDS(
      mWorkerRef->GetPrivate()->DispatchToMainThread(r.forget()));

  mListener = nullptr;
  mWorkerRef = nullptr;
}

void ServiceWorkerRegistrationWorkerThread::UpdateState(
    const ServiceWorkerRegistrationDescriptor& aDescriptor) {
  if (mOuter) {
    mOuter->UpdateState(aDescriptor);
  }
}

void ServiceWorkerRegistrationWorkerThread::FireUpdateFound() {
  if (mOuter) {
    mOuter->MaybeDispatchUpdateFoundRunnable();
  }
}

class RegistrationClearedWorkerRunnable final : public WorkerRunnable {
  RefPtr<WorkerListener> mListener;

 public:
  RegistrationClearedWorkerRunnable(WorkerPrivate* aWorkerPrivate,
                                    WorkerListener* aListener)
      : WorkerRunnable(aWorkerPrivate), mListener(aListener) {
    // Need this assertion for now since runnables which modify busy count can
    // only be dispatched from parent thread to worker thread and we don't deal
    // with nested workers. SW threads can't be nested.
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
  }

  bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
    MOZ_ASSERT(aWorkerPrivate);
    aWorkerPrivate->AssertIsOnWorkerThread();
    mListener->RegistrationCleared();
    return true;
  }
};

void WorkerListener::RegistrationCleared() {
  MutexAutoLock lock(mMutex);
  if (!mRegistration) {
    return;
  }

  if (NS_IsMainThread()) {
    RefPtr<WorkerRunnable> r = new RegistrationClearedWorkerRunnable(
        mRegistration->GetWorkerPrivate(lock), this);
    Unused << r->Dispatch();

    StopListeningForEvents();
    return;
  }

  mRegistration->RegistrationCleared();
}

WorkerPrivate* ServiceWorkerRegistrationWorkerThread::GetWorkerPrivate(
    const MutexAutoLock& aProofOfLock) {
  // In this case, calling GetUnsafePrivate() is ok because we have a proof of
  // mutex lock.
  MOZ_ASSERT(mWorkerRef && mWorkerRef->GetUnsafePrivate());
  return mWorkerRef->GetUnsafePrivate();
}

}  // namespace dom
}  // namespace mozilla