tor-browser

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

ServiceWorkerRegistrationChild.cpp (2538B)


      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 "ServiceWorkerRegistrationChild.h"
      8 
      9 #include "ServiceWorkerRegistration.h"
     10 #include "mozilla/dom/WorkerCommon.h"
     11 #include "mozilla/dom/WorkerRef.h"
     12 
     13 namespace mozilla::dom {
     14 
     15 using mozilla::ipc::IPCResult;
     16 
     17 void ServiceWorkerRegistrationChild::ActorDestroy(ActorDestroyReason aReason) {
     18  mIPCWorkerRef = nullptr;
     19 
     20  if (mOwner) {
     21    mOwner->RevokeActor(this);
     22    MOZ_DIAGNOSTIC_ASSERT(!mOwner);
     23  }
     24 }
     25 
     26 IPCResult ServiceWorkerRegistrationChild::RecvUpdateState(
     27    const IPCServiceWorkerRegistrationDescriptor& aDescriptor) {
     28  if (mOwner) {
     29    RefPtr<ServiceWorkerRegistration> owner = mOwner;
     30    owner->UpdateState(ServiceWorkerRegistrationDescriptor(aDescriptor));
     31  }
     32  return IPC_OK();
     33 }
     34 
     35 IPCResult ServiceWorkerRegistrationChild::RecvFireUpdateFound() {
     36  if (mOwner) {
     37    RefPtr<ServiceWorkerRegistration> owner = mOwner;
     38    owner->FireUpdateFound();
     39  }
     40  return IPC_OK();
     41 }
     42 
     43 // static
     44 RefPtr<ServiceWorkerRegistrationChild>
     45 ServiceWorkerRegistrationChild::Create() {
     46  RefPtr actor = new ServiceWorkerRegistrationChild;
     47 
     48  if (!NS_IsMainThread()) {
     49    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
     50    MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
     51 
     52    RefPtr<IPCWorkerRefHelper<ServiceWorkerRegistrationChild>> helper =
     53        new IPCWorkerRefHelper<ServiceWorkerRegistrationChild>(actor);
     54 
     55    actor->mIPCWorkerRef =
     56        IPCWorkerRef::Create(workerPrivate, "ServiceWorkerRegistrationChild",
     57                             [helper] { helper->Actor()->Shutdown(); });
     58 
     59    if (NS_WARN_IF(!actor->mIPCWorkerRef)) {
     60      return nullptr;
     61    }
     62  }
     63 
     64  return actor;
     65 }
     66 
     67 ServiceWorkerRegistrationChild::ServiceWorkerRegistrationChild()
     68    : mOwner(nullptr) {}
     69 
     70 void ServiceWorkerRegistrationChild::SetOwner(
     71    ServiceWorkerRegistration* aOwner) {
     72  MOZ_DIAGNOSTIC_ASSERT(!mOwner);
     73  MOZ_DIAGNOSTIC_ASSERT(aOwner);
     74  mOwner = aOwner;
     75 }
     76 
     77 void ServiceWorkerRegistrationChild::RevokeOwner(
     78    ServiceWorkerRegistration* aOwner) {
     79  MOZ_DIAGNOSTIC_ASSERT(mOwner);
     80  MOZ_DIAGNOSTIC_ASSERT(aOwner == mOwner);
     81  mOwner = nullptr;
     82 }
     83 
     84 void ServiceWorkerRegistrationChild::Shutdown() {
     85  if (!CanSend()) {
     86    return;
     87  }
     88  (void)Send__delete__(this);
     89 }
     90 
     91 }  // namespace mozilla::dom