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 "mozilla/dom/nsIContentParent.h"
9 : #include "mozilla/ipc/PBackgroundParent.h"
10 : #include "mozilla/ipc/PChildToParentStreamParent.h"
11 : #include "mozilla/ipc/PParentToChildStreamParent.h"
12 : #include "mozilla/Unused.h"
13 :
14 : namespace mozilla {
15 : namespace ipc {
16 :
17 : // Child to Parent implementation
18 : // ----------------------------------------------------------------------------
19 :
20 : namespace {
21 :
22 0 : class IPCStreamSourceParent final : public PParentToChildStreamParent
23 : , public IPCStreamSource
24 : {
25 : public:
26 : static IPCStreamSourceParent*
27 0 : Create(nsIAsyncInputStream* aInputStream)
28 : {
29 0 : MOZ_ASSERT(aInputStream);
30 :
31 0 : IPCStreamSourceParent* source = new IPCStreamSourceParent(aInputStream);
32 0 : if (!source->Initialize()) {
33 0 : delete source;
34 0 : return nullptr;
35 : }
36 :
37 0 : return source;
38 : }
39 :
40 : // PParentToChildStreamParent methods
41 :
42 : void
43 0 : ActorDestroy(ActorDestroyReason aReason) override
44 : {
45 0 : ActorDestroyed();
46 0 : }
47 :
48 : IPCResult
49 0 : RecvStartReading() override
50 : {
51 0 : Start();
52 0 : return IPC_OK();
53 : }
54 :
55 : IPCResult
56 0 : RecvRequestClose(const nsresult& aRv) override
57 : {
58 0 : OnEnd(aRv);
59 0 : return IPC_OK();
60 : }
61 :
62 : void
63 0 : Close(nsresult aRv) override
64 : {
65 0 : MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
66 0 : Unused << SendClose(aRv);
67 0 : }
68 :
69 : void
70 0 : SendData(const nsCString& aBuffer) override
71 : {
72 0 : Unused << SendBuffer(aBuffer);
73 0 : }
74 :
75 : private:
76 0 : explicit IPCStreamSourceParent(nsIAsyncInputStream* aInputStream)
77 0 : :IPCStreamSource(aInputStream)
78 0 : {}
79 : };
80 :
81 : } // anonymous namespace
82 :
83 : /* static */ PParentToChildStreamParent*
84 0 : IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
85 : dom::nsIContentParent* aManager)
86 : {
87 0 : MOZ_ASSERT(aInputStream);
88 0 : MOZ_ASSERT(aManager);
89 :
90 : // PContent can only be used on the main thread
91 0 : MOZ_ASSERT(NS_IsMainThread());
92 :
93 0 : IPCStreamSourceParent* source = IPCStreamSourceParent::Create(aInputStream);
94 0 : if (!source) {
95 0 : return nullptr;
96 : }
97 :
98 0 : if (!aManager->SendPParentToChildStreamConstructor(source)) {
99 : // no delete here, the manager will delete the actor for us.
100 0 : return nullptr;
101 : }
102 :
103 0 : source->ActorConstructed();
104 0 : return source;
105 : }
106 :
107 : /* static */ PParentToChildStreamParent*
108 0 : IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
109 : PBackgroundParent* aManager)
110 : {
111 0 : MOZ_ASSERT(aInputStream);
112 0 : MOZ_ASSERT(aManager);
113 :
114 0 : IPCStreamSourceParent* source = IPCStreamSourceParent::Create(aInputStream);
115 0 : if (!source) {
116 0 : return nullptr;
117 : }
118 :
119 0 : if (!aManager->SendPParentToChildStreamConstructor(source)) {
120 : // no delete here, the manager will delete the actor for us.
121 0 : return nullptr;
122 : }
123 :
124 0 : source->ActorConstructed();
125 0 : return source;
126 : }
127 :
128 : /* static */ IPCStreamSource*
129 0 : IPCStreamSource::Cast(PParentToChildStreamParent* aActor)
130 : {
131 0 : MOZ_ASSERT(aActor);
132 0 : return static_cast<IPCStreamSourceParent*>(aActor);
133 : }
134 :
135 : // Child to Parent implementation
136 : // ----------------------------------------------------------------------------
137 :
138 : namespace {
139 :
140 0 : class IPCStreamDestinationParent final : public PChildToParentStreamParent
141 : , public IPCStreamDestination
142 : {
143 : public:
144 0 : nsresult Initialize()
145 : {
146 0 : return IPCStreamDestination::Initialize();
147 : }
148 :
149 0 : ~IPCStreamDestinationParent()
150 0 : {}
151 :
152 : private:
153 : // PChildToParentStreamParent 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 : PChildToParentStreamParent*
200 0 : AllocPChildToParentStreamParent()
201 : {
202 0 : IPCStreamDestinationParent* actor = new IPCStreamDestinationParent();
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 : DeallocPChildToParentStreamParent(PChildToParentStreamParent* aActor)
214 : {
215 0 : delete aActor;
216 0 : }
217 :
218 : /* static */ IPCStreamDestination*
219 0 : IPCStreamDestination::Cast(PChildToParentStreamParent* aActor)
220 : {
221 0 : MOZ_ASSERT(aActor);
222 0 : return static_cast<IPCStreamDestinationParent*>(aActor);
223 : }
224 :
225 : } // namespace ipc
226 : } // namespace mozilla
|