Line data Source code
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/. */
6 :
7 : #include "IPCStreamDestination.h"
8 : #include "IPCStreamSource.h"
9 :
10 : #include "mozilla/Unused.h"
11 : #include "mozilla/dom/nsIContentChild.h"
12 : #include "mozilla/ipc/PBackgroundChild.h"
13 : #include "mozilla/ipc/PChildToParentStreamChild.h"
14 : #include "mozilla/ipc/PParentToChildStreamChild.h"
15 :
16 : namespace mozilla {
17 : namespace ipc {
18 :
19 : // Child to Parent implementation
20 : // ----------------------------------------------------------------------------
21 :
22 : namespace {
23 :
24 0 : class IPCStreamSourceChild final : public PChildToParentStreamChild
25 : , public IPCStreamSource
26 : {
27 : public:
28 : static IPCStreamSourceChild*
29 0 : Create(nsIAsyncInputStream* aInputStream)
30 : {
31 0 : MOZ_ASSERT(aInputStream);
32 :
33 0 : IPCStreamSourceChild* source = new IPCStreamSourceChild(aInputStream);
34 0 : if (!source->Initialize()) {
35 0 : delete source;
36 0 : return nullptr;
37 : }
38 :
39 0 : return source;
40 : }
41 :
42 : // PChildToParentStreamChild methods
43 :
44 : void
45 0 : ActorDestroy(ActorDestroyReason aReason) override
46 : {
47 0 : ActorDestroyed();
48 0 : }
49 :
50 : IPCResult
51 0 : RecvStartReading() override
52 : {
53 0 : Start();
54 0 : return IPC_OK();
55 : }
56 :
57 : IPCResult
58 0 : RecvRequestClose(const nsresult& aRv) override
59 : {
60 0 : OnEnd(aRv);
61 0 : return IPC_OK();
62 : }
63 :
64 : void
65 0 : Close(nsresult aRv) override
66 : {
67 0 : MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
68 0 : Unused << SendClose(aRv);
69 0 : }
70 :
71 : void
72 0 : SendData(const nsCString& aBuffer) override
73 : {
74 0 : Unused << SendBuffer(aBuffer);
75 0 : }
76 :
77 : private:
78 0 : explicit IPCStreamSourceChild(nsIAsyncInputStream* aInputStream)
79 0 : :IPCStreamSource(aInputStream)
80 0 : {}
81 : };
82 :
83 : } // anonymous namespace
84 :
85 : /* static */ PChildToParentStreamChild*
86 0 : IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
87 : dom::nsIContentChild* aManager)
88 : {
89 0 : MOZ_ASSERT(aInputStream);
90 0 : MOZ_ASSERT(aManager);
91 :
92 : // PContent can only be used on the main thread
93 0 : MOZ_ASSERT(NS_IsMainThread());
94 :
95 0 : IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
96 0 : if (!source) {
97 0 : return nullptr;
98 : }
99 :
100 0 : if (!aManager->SendPChildToParentStreamConstructor(source)) {
101 0 : return nullptr;
102 : }
103 :
104 0 : source->ActorConstructed();
105 0 : return source;
106 : }
107 :
108 : /* static */ PChildToParentStreamChild*
109 0 : IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
110 : PBackgroundChild* aManager)
111 : {
112 0 : MOZ_ASSERT(aInputStream);
113 0 : MOZ_ASSERT(aManager);
114 :
115 0 : IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
116 0 : if (!source) {
117 0 : return nullptr;
118 : }
119 :
120 0 : if (!aManager->SendPChildToParentStreamConstructor(source)) {
121 0 : return nullptr;
122 : }
123 :
124 0 : source->ActorConstructed();
125 0 : return source;
126 : }
127 :
128 : /* static */ IPCStreamSource*
129 0 : IPCStreamSource::Cast(PChildToParentStreamChild* aActor)
130 : {
131 0 : MOZ_ASSERT(aActor);
132 0 : return static_cast<IPCStreamSourceChild*>(aActor);
133 : }
134 :
135 : // Parent to Child implementation
136 : // ----------------------------------------------------------------------------
137 :
138 : namespace {
139 :
140 0 : class IPCStreamDestinationChild final : public PParentToChildStreamChild
141 : , public IPCStreamDestination
142 : {
143 : public:
144 0 : nsresult Initialize()
145 : {
146 0 : return IPCStreamDestination::Initialize();
147 : }
148 :
149 0 : ~IPCStreamDestinationChild()
150 0 : {}
151 :
152 : private:
153 : // PParentToChildStreamChild methods
154 :
155 : void
156 0 : ActorDestroy(ActorDestroyReason aReason) override
157 : {
158 0 : ActorDestroyed();
159 0 : }
160 :
161 : IPCResult
162 0 : RecvBuffer(const nsCString& aBuffer) override
163 : {
164 0 : BufferReceived(aBuffer);
165 0 : return IPC_OK();
166 : }
167 :
168 : IPCResult
169 0 : RecvClose(const nsresult& aRv) override
170 : {
171 0 : CloseReceived(aRv);
172 0 : return IPC_OK();
173 : }
174 :
175 : // IPCStreamDestination methods
176 :
177 : void
178 0 : StartReading() override
179 : {
180 0 : MOZ_ASSERT(HasDelayedStart());
181 0 : Unused << SendStartReading();
182 0 : }
183 :
184 : void
185 0 : RequestClose(nsresult aRv) override
186 : {
187 0 : Unused << SendRequestClose(aRv);
188 0 : }
189 :
190 : void
191 0 : TerminateDestination() override
192 : {
193 0 : Unused << Send__delete__(this);
194 0 : }
195 : };
196 :
197 : } // anonymous namespace
198 :
199 : PParentToChildStreamChild*
200 0 : AllocPParentToChildStreamChild()
201 : {
202 0 : IPCStreamDestinationChild* actor = new IPCStreamDestinationChild();
203 :
204 0 : if (NS_WARN_IF(NS_FAILED(actor->Initialize()))) {
205 0 : delete actor;
206 0 : actor = nullptr;
207 : }
208 :
209 0 : return actor;
210 : }
211 :
212 : void
213 0 : DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor)
214 : {
215 0 : delete aActor;
216 0 : }
217 :
218 : /* static */ IPCStreamDestination*
219 0 : IPCStreamDestination::Cast(PParentToChildStreamChild* aActor)
220 : {
221 0 : MOZ_ASSERT(aActor);
222 0 : return static_cast<IPCStreamDestinationChild*>(aActor);
223 : }
224 :
225 : } // namespace ipc
226 : } // namespace mozilla
|