Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/ipc/glue/IPCStreamChild.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 "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
class IPCStreamSourceChild final : public PChildToParentStreamChild
25
                                 , public IPCStreamSource
26
{
27
public:
28
  static IPCStreamSourceChild*
29
  Create(nsIAsyncInputStream* aInputStream)
30
0
  {
31
0
    MOZ_ASSERT(aInputStream);
32
0
33
0
    IPCStreamSourceChild* source = new IPCStreamSourceChild(aInputStream);
34
0
    if (!source->Initialize()) {
35
0
      delete source;
36
0
      return nullptr;
37
0
    }
38
0
39
0
    return source;
40
0
  }
41
42
  // PChildToParentStreamChild methods
43
44
  void
45
  ActorDestroy(ActorDestroyReason aReason) override
46
0
  {
47
0
    ActorDestroyed();
48
0
  }
49
50
  IPCResult
51
  RecvStartReading() override
52
0
  {
53
0
    Start();
54
0
    return IPC_OK();
55
0
  }
56
57
  IPCResult
58
  RecvRequestClose(const nsresult& aRv) override
59
0
  {
60
0
    OnEnd(aRv);
61
0
    return IPC_OK();
62
0
  }
63
64
  void
65
  Close(nsresult aRv) override
66
0
  {
67
0
    MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
68
0
    Unused << SendClose(aRv);
69
0
  }
70
71
  void
72
  SendData(const wr::ByteBuffer& aBuffer) override
73
0
  {
74
0
    Unused << SendBuffer(aBuffer);
75
0
  }
76
77
private:
78
  explicit IPCStreamSourceChild(nsIAsyncInputStream* aInputStream)
79
    :IPCStreamSource(aInputStream)
80
0
  {}
81
};
82
83
} // anonymous namespace
84
85
/* static */ PChildToParentStreamChild*
86
IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
87
                        dom::nsIContentChild* aManager)
88
0
{
89
0
  MOZ_ASSERT(aInputStream);
90
0
  MOZ_ASSERT(aManager);
91
0
92
0
  // PContent can only be used on the main thread
93
0
  MOZ_ASSERT(NS_IsMainThread());
94
0
95
0
  IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
96
0
  if (!source) {
97
0
    return nullptr;
98
0
  }
99
0
100
0
  if (!aManager->SendPChildToParentStreamConstructor(source)) {
101
0
    return nullptr;
102
0
  }
103
0
104
0
  source->ActorConstructed();
105
0
  return source;
106
0
}
107
108
/* static */ PChildToParentStreamChild*
109
IPCStreamSource::Create(nsIAsyncInputStream* aInputStream,
110
                        PBackgroundChild* aManager)
111
0
{
112
0
  MOZ_ASSERT(aInputStream);
113
0
  MOZ_ASSERT(aManager);
114
0
115
0
  IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
116
0
  if (!source) {
117
0
    return nullptr;
118
0
  }
119
0
120
0
  if (!aManager->SendPChildToParentStreamConstructor(source)) {
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(PChildToParentStreamChild* aActor)
130
0
{
131
0
  MOZ_ASSERT(aActor);
132
0
  return static_cast<IPCStreamSourceChild*>(aActor);
133
0
}
134
135
// Parent to Child implementation
136
// ----------------------------------------------------------------------------
137
138
namespace {
139
140
class IPCStreamDestinationChild final : public PParentToChildStreamChild
141
                                      , public IPCStreamDestination
142
{
143
public:
144
  nsresult Initialize()
145
0
  {
146
0
    return IPCStreamDestination::Initialize();
147
0
  }
148
149
  ~IPCStreamDestinationChild()
150
0
  {}
151
152
private:
153
  // PParentToChildStreamChild 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
PParentToChildStreamChild*
200
AllocPParentToChildStreamChild()
201
0
{
202
0
  IPCStreamDestinationChild* actor = new IPCStreamDestinationChild();
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
DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor)
214
0
{
215
0
  delete aActor;
216
0
}
217
218
/* static */ IPCStreamDestination*
219
IPCStreamDestination::Cast(PParentToChildStreamChild* aActor)
220
0
{
221
0
  MOZ_ASSERT(aActor);
222
0
  return static_cast<IPCStreamDestinationChild*>(aActor);
223
0
}
224
225
} // namespace ipc
226
} // namespace mozilla