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/ipc/PChildToParentStreamChild.h"
12 #include "mozilla/ipc/PParentToChildStreamChild.h"
17 // Child to Parent implementation
18 // ----------------------------------------------------------------------------
22 class IPCStreamSourceChild final
: public PChildToParentStreamChild
,
23 public IPCStreamSource
{
25 static IPCStreamSourceChild
* Create(nsIAsyncInputStream
* aInputStream
) {
26 MOZ_ASSERT(aInputStream
);
28 IPCStreamSourceChild
* source
= new IPCStreamSourceChild(aInputStream
);
29 if (!source
->Initialize()) {
37 // PChildToParentStreamChild methods
39 void ActorDestroy(ActorDestroyReason aReason
) override
{ ActorDestroyed(); }
41 IPCResult
RecvStartReading() override
{
46 IPCResult
RecvRequestClose(const nsresult
& aRv
) override
{
51 void Close(nsresult aRv
) override
{
52 MOZ_ASSERT(IPCStreamSource::mState
== IPCStreamSource::eClosed
);
53 Unused
<< SendClose(aRv
);
56 void SendData(const wr::ByteBuffer
& aBuffer
) override
{
57 Unused
<< SendBuffer(aBuffer
);
61 explicit IPCStreamSourceChild(nsIAsyncInputStream
* aInputStream
)
62 : IPCStreamSource(aInputStream
) {}
65 } // anonymous namespace
68 PChildToParentStreamChild
* IPCStreamSource::Create(
69 nsIAsyncInputStream
* aInputStream
,
70 ChildToParentStreamActorManager
* aManager
) {
71 MOZ_ASSERT(aInputStream
);
74 IPCStreamSourceChild
* source
= IPCStreamSourceChild::Create(aInputStream
);
79 if (!aManager
->SendPChildToParentStreamConstructor(source
)) {
83 source
->ActorConstructed();
88 IPCStreamSource
* IPCStreamSource::Cast(PChildToParentStreamChild
* aActor
) {
90 return static_cast<IPCStreamSourceChild
*>(aActor
);
93 // Parent to Child implementation
94 // ----------------------------------------------------------------------------
98 class IPCStreamDestinationChild final
: public PParentToChildStreamChild
,
99 public IPCStreamDestination
{
101 nsresult
Initialize() { return IPCStreamDestination::Initialize(); }
103 ~IPCStreamDestinationChild() = default;
106 // PParentToChildStreamChild methods
108 void ActorDestroy(ActorDestroyReason aReason
) override
{ ActorDestroyed(); }
110 IPCResult
RecvBuffer(const wr::ByteBuffer
& aBuffer
) override
{
111 BufferReceived(aBuffer
);
115 IPCResult
RecvClose(const nsresult
& aRv
) override
{
120 // IPCStreamDestination methods
122 void StartReading() override
{
123 MOZ_ASSERT(HasDelayedStart());
124 Unused
<< SendStartReading();
127 void RequestClose(nsresult aRv
) override
{ Unused
<< SendRequestClose(aRv
); }
129 void TerminateDestination() override
{ Unused
<< Send__delete__(this); }
132 } // anonymous namespace
134 PParentToChildStreamChild
* AllocPParentToChildStreamChild() {
135 IPCStreamDestinationChild
* actor
= new IPCStreamDestinationChild();
137 if (NS_WARN_IF(NS_FAILED(actor
->Initialize()))) {
145 void DeallocPParentToChildStreamChild(PParentToChildStreamChild
* aActor
) {
150 IPCStreamDestination
* IPCStreamDestination::Cast(
151 PParentToChildStreamChild
* aActor
) {
153 return static_cast<IPCStreamDestinationChild
*>(aActor
);
157 } // namespace mozilla