tor-browser

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

SocketProcessBridgeParent.cpp (3361B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "SocketProcessBridgeParent.h"
      7 #include "SocketProcessLogging.h"
      8 
      9 #ifdef MOZ_WEBRTC
     10 #  include "mozilla/dom/MediaTransportParent.h"
     11 #endif
     12 #include "mozilla/ipc/BackgroundParent.h"
     13 #include "mozilla/ipc/Endpoint.h"
     14 #include "SocketProcessChild.h"
     15 #include "mozilla/net/BackgroundDataBridgeParent.h"
     16 #include "nsThreadUtils.h"
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 SocketProcessBridgeParent::SocketProcessBridgeParent(ProcessId aId) : mId(aId) {
     22  LOG(
     23      ("CONSTRUCT SocketProcessBridgeParent::SocketProcessBridgeParent "
     24       "mId=%" PRIPID "\n",
     25       mId));
     26 }
     27 
     28 SocketProcessBridgeParent::~SocketProcessBridgeParent() {
     29  LOG(("DESTRUCT SocketProcessBridgeParent::SocketProcessBridgeParent\n"));
     30 }
     31 
     32 mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvInitBackgroundDataBridge(
     33    mozilla::ipc::Endpoint<PBackgroundDataBridgeParent>&& aEndpoint,
     34    uint64_t aChannelID) {
     35  LOG(("SocketProcessBridgeParent::RecvInitBackgroundDataBridge\n"));
     36 
     37  nsCOMPtr<nsISerialEventTarget> transportQueue;
     38  if (NS_FAILED(NS_CreateBackgroundTaskQueue("BackgroundDataBridge",
     39                                             getter_AddRefs(transportQueue)))) {
     40    return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed");
     41  }
     42 
     43  if (!aEndpoint.IsValid()) {
     44    return IPC_FAIL(this, "Invalid endpoint");
     45  }
     46 
     47  transportQueue->Dispatch(NS_NewRunnableFunction(
     48      "BackgroundDataBridgeParent::Bind",
     49      [endpoint = std::move(aEndpoint), aChannelID]() mutable {
     50        RefPtr<net::BackgroundDataBridgeParent> actor =
     51            new net::BackgroundDataBridgeParent(aChannelID);
     52        endpoint.Bind(actor);
     53      }));
     54  return IPC_OK();
     55 }
     56 
     57 #ifdef MOZ_WEBRTC
     58 mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvInitMediaTransport(
     59    mozilla::ipc::Endpoint<mozilla::dom::PMediaTransportParent>&& aEndpoint) {
     60  LOG(("SocketProcessBridgeParent::RecvInitMediaTransport\n"));
     61 
     62  if (!aEndpoint.IsValid()) {
     63    return IPC_FAIL(this, "Invalid endpoint");
     64  }
     65 
     66  if (!mMediaTransportTaskQueue) {
     67    nsCOMPtr<nsISerialEventTarget> transportQueue;
     68    if (NS_FAILED(NS_CreateBackgroundTaskQueue(
     69            "MediaTransport", getter_AddRefs(transportQueue)))) {
     70      return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed");
     71    }
     72 
     73    mMediaTransportTaskQueue = std::move(transportQueue);
     74  }
     75 
     76  mMediaTransportTaskQueue->Dispatch(NS_NewRunnableFunction(
     77      "BackgroundDataBridgeParent::Bind",
     78      [endpoint = std::move(aEndpoint)]() mutable {
     79        RefPtr<MediaTransportParent> actor = new MediaTransportParent();
     80        endpoint.Bind(actor);
     81      }));
     82  return IPC_OK();
     83 }
     84 #endif
     85 
     86 void SocketProcessBridgeParent::ActorDestroy(ActorDestroyReason aReason) {
     87  // See bug 1846478. We might be able to remove this dispatch.
     88  GetCurrentSerialEventTarget()->Dispatch(NS_NewRunnableFunction(
     89      "SocketProcessBridgeParent::ActorDestroy", [id = mId] {
     90        if (SocketProcessChild* child = SocketProcessChild::GetSingleton()) {
     91          child->DestroySocketProcessBridgeParent(id);
     92        }
     93      }));
     94 }
     95 
     96 }  // namespace net
     97 }  // namespace mozilla