tor-browser

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

ClientManagerChild.cpp (3289B)


      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 "ClientManagerChild.h"
      8 
      9 #include "ClientHandleChild.h"
     10 #include "ClientManagerOpChild.h"
     11 #include "ClientNavigateOpChild.h"
     12 #include "ClientSourceChild.h"
     13 #include "mozilla/dom/WorkerCommon.h"
     14 #include "mozilla/dom/WorkerRef.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 void ClientManagerChild::ActorDestroy(ActorDestroyReason aReason) {
     19  mIPCWorkerRef = nullptr;
     20 
     21  if (mManager) {
     22    mManager->RevokeActor(this);
     23 
     24    // Revoking the actor link should automatically cause the owner
     25    // to call RevokeOwner() as well.
     26    MOZ_DIAGNOSTIC_ASSERT(!mManager);
     27  }
     28 }
     29 
     30 PClientManagerOpChild* ClientManagerChild::AllocPClientManagerOpChild(
     31    const ClientOpConstructorArgs& aArgs) {
     32  MOZ_ASSERT_UNREACHABLE(
     33      "ClientManagerOpChild must be explicitly constructed.");
     34  return nullptr;
     35 }
     36 
     37 bool ClientManagerChild::DeallocPClientManagerOpChild(
     38    PClientManagerOpChild* aActor) {
     39  delete aActor;
     40  return true;
     41 }
     42 
     43 PClientNavigateOpChild* ClientManagerChild::AllocPClientNavigateOpChild(
     44    const ClientNavigateOpConstructorArgs& aArgs) {
     45  return new ClientNavigateOpChild();
     46 }
     47 
     48 bool ClientManagerChild::DeallocPClientNavigateOpChild(
     49    PClientNavigateOpChild* aActor) {
     50  delete aActor;
     51  return true;
     52 }
     53 
     54 mozilla::ipc::IPCResult ClientManagerChild::RecvPClientNavigateOpConstructor(
     55    PClientNavigateOpChild* aActor,
     56    const ClientNavigateOpConstructorArgs& aArgs) {
     57  auto actor = static_cast<ClientNavigateOpChild*>(aActor);
     58  actor->Init(aArgs);
     59  return IPC_OK();
     60 }
     61 
     62 // static
     63 already_AddRefed<ClientManagerChild> ClientManagerChild::Create() {
     64  RefPtr<ClientManagerChild> actor = new ClientManagerChild();
     65 
     66  if (!NS_IsMainThread()) {
     67    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
     68    MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
     69 
     70    RefPtr<IPCWorkerRefHelper<ClientManagerChild>> helper =
     71        new IPCWorkerRefHelper<ClientManagerChild>(actor);
     72 
     73    actor->mIPCWorkerRef = IPCWorkerRef::Create(
     74        workerPrivate, "ClientManagerChild",
     75        [helper] { helper->Actor()->MaybeStartTeardown(); });
     76 
     77    if (NS_WARN_IF(!actor->mIPCWorkerRef)) {
     78      return nullptr;
     79    }
     80  }
     81 
     82  return actor.forget();
     83 }
     84 
     85 ClientManagerChild::ClientManagerChild()
     86    : mManager(nullptr), mTeardownStarted(false) {}
     87 
     88 ClientManagerChild::~ClientManagerChild() = default;
     89 
     90 void ClientManagerChild::SetOwner(ClientThing<ClientManagerChild>* aThing) {
     91  MOZ_DIAGNOSTIC_ASSERT(aThing);
     92  MOZ_DIAGNOSTIC_ASSERT(!mManager);
     93  mManager = aThing;
     94 }
     95 
     96 void ClientManagerChild::RevokeOwner(ClientThing<ClientManagerChild>* aThing) {
     97  MOZ_DIAGNOSTIC_ASSERT(mManager);
     98  MOZ_DIAGNOSTIC_ASSERT(mManager == aThing);
     99  mManager = nullptr;
    100 }
    101 
    102 void ClientManagerChild::MaybeStartTeardown() {
    103  if (mTeardownStarted) {
    104    return;
    105  }
    106  mTeardownStarted = true;
    107  SendTeardown();
    108 }
    109 
    110 WorkerPrivate* ClientManagerChild::GetWorkerPrivate() const {
    111  if (!mIPCWorkerRef) {
    112    return nullptr;
    113  }
    114  return mIPCWorkerRef->Private();
    115 }
    116 
    117 }  // namespace mozilla::dom