DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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
/* -*- 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 "IPCStreamDestination.h"
#include "IPCStreamSource.h"

#include "mozilla/Unused.h"
#include "mozilla/dom/nsIContentChild.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "mozilla/ipc/PChildToParentStreamChild.h"
#include "mozilla/ipc/PParentToChildStreamChild.h"

namespace mozilla {
namespace ipc {

// Child to Parent implementation
// ----------------------------------------------------------------------------

namespace {

class IPCStreamSourceChild final : public PChildToParentStreamChild,
                                   public IPCStreamSource {
 public:
  static IPCStreamSourceChild* Create(nsIAsyncInputStream* aInputStream) {
    MOZ_ASSERT(aInputStream);

    IPCStreamSourceChild* source = new IPCStreamSourceChild(aInputStream);
    if (!source->Initialize()) {
      delete source;
      return nullptr;
    }

    return source;
  }

  // PChildToParentStreamChild methods

  void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }

  IPCResult RecvStartReading() override {
    Start();
    return IPC_OK();
  }

  IPCResult RecvRequestClose(const nsresult& aRv) override {
    OnEnd(aRv);
    return IPC_OK();
  }

  void Close(nsresult aRv) override {
    MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
    Unused << SendClose(aRv);
  }

  void SendData(const wr::ByteBuffer& aBuffer) override {
    Unused << SendBuffer(aBuffer);
  }

 private:
  explicit IPCStreamSourceChild(nsIAsyncInputStream* aInputStream)
      : IPCStreamSource(aInputStream) {}
};

}  // anonymous namespace

/* static */ PChildToParentStreamChild* IPCStreamSource::Create(
    nsIAsyncInputStream* aInputStream, dom::nsIContentChild* aManager) {
  MOZ_ASSERT(aInputStream);
  MOZ_ASSERT(aManager);

  // PContent can only be used on the main thread
  MOZ_ASSERT(NS_IsMainThread());

  IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
  if (!source) {
    return nullptr;
  }

  if (!aManager->SendPChildToParentStreamConstructor(source)) {
    return nullptr;
  }

  source->ActorConstructed();
  return source;
}

/* static */ PChildToParentStreamChild* IPCStreamSource::Create(
    nsIAsyncInputStream* aInputStream, PBackgroundChild* aManager) {
  MOZ_ASSERT(aInputStream);
  MOZ_ASSERT(aManager);

  IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
  if (!source) {
    return nullptr;
  }

  if (!aManager->SendPChildToParentStreamConstructor(source)) {
    return nullptr;
  }

  source->ActorConstructed();
  return source;
}

/* static */ IPCStreamSource* IPCStreamSource::Cast(
    PChildToParentStreamChild* aActor) {
  MOZ_ASSERT(aActor);
  return static_cast<IPCStreamSourceChild*>(aActor);
}

// Parent to Child implementation
// ----------------------------------------------------------------------------

namespace {

class IPCStreamDestinationChild final : public PParentToChildStreamChild,
                                        public IPCStreamDestination {
 public:
  nsresult Initialize() { return IPCStreamDestination::Initialize(); }

  ~IPCStreamDestinationChild() {}

 private:
  // PParentToChildStreamChild methods

  void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }

  IPCResult RecvBuffer(const wr::ByteBuffer& aBuffer) override {
    BufferReceived(aBuffer);
    return IPC_OK();
  }

  IPCResult RecvClose(const nsresult& aRv) override {
    CloseReceived(aRv);
    return IPC_OK();
  }

  // IPCStreamDestination methods

  void StartReading() override {
    MOZ_ASSERT(HasDelayedStart());
    Unused << SendStartReading();
  }

  void RequestClose(nsresult aRv) override { Unused << SendRequestClose(aRv); }

  void TerminateDestination() override { Unused << Send__delete__(this); }
};

}  // anonymous namespace

PParentToChildStreamChild* AllocPParentToChildStreamChild() {
  IPCStreamDestinationChild* actor = new IPCStreamDestinationChild();

  if (NS_WARN_IF(NS_FAILED(actor->Initialize()))) {
    delete actor;
    actor = nullptr;
  }

  return actor;
}

void DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor) {
  delete aActor;
}

/* static */ IPCStreamDestination* IPCStreamDestination::Cast(
    PParentToChildStreamChild* aActor) {
  MOZ_ASSERT(aActor);
  return static_cast<IPCStreamDestinationChild*>(aActor);
}

}  // namespace ipc
}  // namespace mozilla