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"
19 // Child to Parent implementation
20 // ----------------------------------------------------------------------------
24 class IPCStreamSourceChild final
: public PChildToParentStreamChild
,
25 public IPCStreamSource
{
27 static IPCStreamSourceChild
* Create(nsIAsyncInputStream
* aInputStream
) {
28 MOZ_ASSERT(aInputStream
);
30 IPCStreamSourceChild
* source
= new IPCStreamSourceChild(aInputStream
);
31 if (!source
->Initialize()) {
39 // PChildToParentStreamChild methods
41 void ActorDestroy(ActorDestroyReason aReason
) override
{ ActorDestroyed(); }
43 IPCResult
RecvStartReading() override
{
48 IPCResult
RecvRequestClose(const nsresult
& aRv
) override
{
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
);
63 explicit IPCStreamSourceChild(nsIAsyncInputStream
* aInputStream
)
64 : IPCStreamSource(aInputStream
) {}
67 } // anonymous namespace
70 PChildToParentStreamChild
* IPCStreamSource::Create(
71 nsIAsyncInputStream
* aInputStream
, dom::ContentChild
* aManager
) {
72 MOZ_ASSERT(aInputStream
);
75 // PContent can only be used on the main thread
76 MOZ_ASSERT(NS_IsMainThread());
78 IPCStreamSourceChild
* source
= IPCStreamSourceChild::Create(aInputStream
);
83 if (!aManager
->SendPChildToParentStreamConstructor(source
)) {
87 source
->ActorConstructed();
92 PChildToParentStreamChild
* IPCStreamSource::Create(
93 nsIAsyncInputStream
* aInputStream
, PBackgroundChild
* aManager
) {
94 MOZ_ASSERT(aInputStream
);
97 IPCStreamSourceChild
* source
= IPCStreamSourceChild::Create(aInputStream
);
102 if (!aManager
->SendPChildToParentStreamConstructor(source
)) {
106 source
->ActorConstructed();
111 IPCStreamSource
* IPCStreamSource::Cast(PChildToParentStreamChild
* aActor
) {
113 return static_cast<IPCStreamSourceChild
*>(aActor
);
116 // Parent to Child implementation
117 // ----------------------------------------------------------------------------
121 class IPCStreamDestinationChild final
: public PParentToChildStreamChild
,
122 public IPCStreamDestination
{
124 nsresult
Initialize() { return IPCStreamDestination::Initialize(); }
126 ~IPCStreamDestinationChild() {}
129 // PParentToChildStreamChild methods
131 void ActorDestroy(ActorDestroyReason aReason
) override
{ ActorDestroyed(); }
133 IPCResult
RecvBuffer(const wr::ByteBuffer
& aBuffer
) override
{
134 BufferReceived(aBuffer
);
138 IPCResult
RecvClose(const nsresult
& aRv
) override
{
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()))) {
168 void DeallocPParentToChildStreamChild(PParentToChildStreamChild
* aActor
) {
173 IPCStreamDestination
* IPCStreamDestination::Cast(
174 PParentToChildStreamChild
* aActor
) {
176 return static_cast<IPCStreamDestinationChild
*>(aActor
);
180 } // namespace mozilla