Coverage Report

Created: 2018-09-25 14:53

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