Bug 1578501 [wpt PR 18803] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / ipc / glue / IPCStreamChild.cpp
blob3041cf12ea4ec4e18dea51c67585306ea66a8571
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/. */
7 #include "IPCStreamDestination.h"
8 #include "IPCStreamSource.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/dom/ContentChild.h"
12 #include "mozilla/ipc/PBackgroundChild.h"
13 #include "mozilla/ipc/PChildToParentStreamChild.h"
14 #include "mozilla/ipc/PParentToChildStreamChild.h"
16 namespace mozilla {
17 namespace ipc {
19 // Child to Parent implementation
20 // ----------------------------------------------------------------------------
22 namespace {
24 class IPCStreamSourceChild final : public PChildToParentStreamChild,
25 public IPCStreamSource {
26 public:
27 static IPCStreamSourceChild* Create(nsIAsyncInputStream* aInputStream) {
28 MOZ_ASSERT(aInputStream);
30 IPCStreamSourceChild* source = new IPCStreamSourceChild(aInputStream);
31 if (!source->Initialize()) {
32 delete source;
33 return nullptr;
36 return source;
39 // PChildToParentStreamChild methods
41 void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
43 IPCResult RecvStartReading() override {
44 Start();
45 return IPC_OK();
48 IPCResult RecvRequestClose(const nsresult& aRv) override {
49 OnEnd(aRv);
50 return IPC_OK();
53 void Close(nsresult aRv) override {
54 MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
55 Unused << SendClose(aRv);
58 void SendData(const wr::ByteBuffer& aBuffer) override {
59 Unused << SendBuffer(aBuffer);
62 private:
63 explicit IPCStreamSourceChild(nsIAsyncInputStream* aInputStream)
64 : IPCStreamSource(aInputStream) {}
67 } // anonymous namespace
69 /* static */
70 PChildToParentStreamChild* IPCStreamSource::Create(
71 nsIAsyncInputStream* aInputStream, dom::ContentChild* aManager) {
72 MOZ_ASSERT(aInputStream);
73 MOZ_ASSERT(aManager);
75 // PContent can only be used on the main thread
76 MOZ_ASSERT(NS_IsMainThread());
78 IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
79 if (!source) {
80 return nullptr;
83 if (!aManager->SendPChildToParentStreamConstructor(source)) {
84 return nullptr;
87 source->ActorConstructed();
88 return source;
91 /* static */
92 PChildToParentStreamChild* IPCStreamSource::Create(
93 nsIAsyncInputStream* aInputStream, PBackgroundChild* aManager) {
94 MOZ_ASSERT(aInputStream);
95 MOZ_ASSERT(aManager);
97 IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
98 if (!source) {
99 return nullptr;
102 if (!aManager->SendPChildToParentStreamConstructor(source)) {
103 return nullptr;
106 source->ActorConstructed();
107 return source;
110 /* static */
111 IPCStreamSource* IPCStreamSource::Cast(PChildToParentStreamChild* aActor) {
112 MOZ_ASSERT(aActor);
113 return static_cast<IPCStreamSourceChild*>(aActor);
116 // Parent to Child implementation
117 // ----------------------------------------------------------------------------
119 namespace {
121 class IPCStreamDestinationChild final : public PParentToChildStreamChild,
122 public IPCStreamDestination {
123 public:
124 nsresult Initialize() { return IPCStreamDestination::Initialize(); }
126 ~IPCStreamDestinationChild() {}
128 private:
129 // PParentToChildStreamChild methods
131 void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
133 IPCResult RecvBuffer(const wr::ByteBuffer& aBuffer) override {
134 BufferReceived(aBuffer);
135 return IPC_OK();
138 IPCResult RecvClose(const nsresult& aRv) override {
139 CloseReceived(aRv);
140 return IPC_OK();
143 // IPCStreamDestination methods
145 void StartReading() override {
146 MOZ_ASSERT(HasDelayedStart());
147 Unused << SendStartReading();
150 void RequestClose(nsresult aRv) override { Unused << SendRequestClose(aRv); }
152 void TerminateDestination() override { Unused << Send__delete__(this); }
155 } // anonymous namespace
157 PParentToChildStreamChild* AllocPParentToChildStreamChild() {
158 IPCStreamDestinationChild* actor = new IPCStreamDestinationChild();
160 if (NS_WARN_IF(NS_FAILED(actor->Initialize()))) {
161 delete actor;
162 actor = nullptr;
165 return actor;
168 void DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor) {
169 delete aActor;
172 /* static */
173 IPCStreamDestination* IPCStreamDestination::Cast(
174 PParentToChildStreamChild* aActor) {
175 MOZ_ASSERT(aActor);
176 return static_cast<IPCStreamDestinationChild*>(aActor);
179 } // namespace ipc
180 } // namespace mozilla