tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ServiceWorkerChild.cpp (1892B)


      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 "ServiceWorkerChild.h"
      8 
      9 #include "ServiceWorker.h"
     10 #include "mozilla/dom/WorkerCommon.h"
     11 #include "mozilla/dom/WorkerRef.h"
     12 
     13 namespace mozilla::dom {
     14 
     15 void ServiceWorkerChild::ActorDestroy(ActorDestroyReason aReason) {
     16  mIPCWorkerRef = nullptr;
     17 
     18  if (mOwner) {
     19    mOwner->RevokeActor(this);
     20    MOZ_DIAGNOSTIC_ASSERT(!mOwner);
     21  }
     22 }
     23 
     24 // static
     25 RefPtr<ServiceWorkerChild> ServiceWorkerChild::Create() {
     26  RefPtr<ServiceWorkerChild> actor = new ServiceWorkerChild();
     27 
     28  if (!NS_IsMainThread()) {
     29    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
     30    MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
     31 
     32    RefPtr<IPCWorkerRefHelper<ServiceWorkerChild>> helper =
     33        new IPCWorkerRefHelper<ServiceWorkerChild>(actor);
     34 
     35    actor->mIPCWorkerRef = IPCWorkerRef::Create(
     36        workerPrivate, "ServiceWorkerChild",
     37        [helper] { helper->Actor()->MaybeStartTeardown(); });
     38 
     39    if (NS_WARN_IF(!actor->mIPCWorkerRef)) {
     40      return nullptr;
     41    }
     42  }
     43 
     44  return actor;
     45 }
     46 
     47 ServiceWorkerChild::ServiceWorkerChild()
     48    : mOwner(nullptr), mTeardownStarted(false) {}
     49 
     50 void ServiceWorkerChild::SetOwner(ServiceWorker* aOwner) {
     51  MOZ_DIAGNOSTIC_ASSERT(!mOwner);
     52  MOZ_DIAGNOSTIC_ASSERT(aOwner);
     53  mOwner = aOwner;
     54 }
     55 
     56 void ServiceWorkerChild::RevokeOwner(ServiceWorker* aOwner) {
     57  MOZ_DIAGNOSTIC_ASSERT(mOwner);
     58  MOZ_DIAGNOSTIC_ASSERT(aOwner == mOwner);
     59  mOwner = nullptr;
     60 }
     61 
     62 void ServiceWorkerChild::MaybeStartTeardown() {
     63  if (mTeardownStarted) {
     64    return;
     65  }
     66  mTeardownStarted = true;
     67  (void)SendTeardown();
     68 }
     69 
     70 }  // namespace mozilla::dom