forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPCStreamParent.cpp
158 lines (119 loc) · 4.14 KB
/
IPCStreamParent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IPCStreamDestination.h"
#include "mozilla/ipc/IPCStreamSource.h"
#include "mozilla/ipc/InputStreamUtils.h"
#include "mozilla/ipc/PChildToParentStreamParent.h"
#include "mozilla/ipc/PParentToChildStreamParent.h"
#include "mozilla/Unused.h"
namespace mozilla {
namespace ipc {
// Child to Parent implementation
// ----------------------------------------------------------------------------
namespace {
class IPCStreamSourceParent final : public PParentToChildStreamParent,
public IPCStreamSource {
public:
static IPCStreamSourceParent* Create(nsIAsyncInputStream* aInputStream) {
MOZ_ASSERT(aInputStream);
IPCStreamSourceParent* source = new IPCStreamSourceParent(aInputStream);
if (!source->Initialize()) {
delete source;
return nullptr;
}
return source;
}
// PParentToChildStreamParent methods
void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
IPCResult RecvStartReading() override {
Start();
return IPC_OK();
}
IPCResult RecvRequestClose(const nsresult& aRv) override {
OnEnd(aRv);
return IPC_OK();
}
void Close(nsresult aRv) override {
MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
Unused << SendClose(aRv);
}
void SendData(const wr::ByteBuffer& aBuffer) override {
Unused << SendBuffer(aBuffer);
}
private:
explicit IPCStreamSourceParent(nsIAsyncInputStream* aInputStream)
: IPCStreamSource(aInputStream) {}
};
} // anonymous namespace
/* static */
PParentToChildStreamParent* IPCStreamSource::Create(
nsIAsyncInputStream* aInputStream,
ParentToChildStreamActorManager* aManager) {
MOZ_ASSERT(aInputStream);
MOZ_ASSERT(aManager);
IPCStreamSourceParent* source = IPCStreamSourceParent::Create(aInputStream);
if (!source) {
return nullptr;
}
if (!aManager->SendPParentToChildStreamConstructor(source)) {
// no delete here, the manager will delete the actor for us.
return nullptr;
}
source->ActorConstructed();
return source;
}
/* static */
IPCStreamSource* IPCStreamSource::Cast(PParentToChildStreamParent* aActor) {
MOZ_ASSERT(aActor);
return static_cast<IPCStreamSourceParent*>(aActor);
}
// Child to Parent implementation
// ----------------------------------------------------------------------------
namespace {
class IPCStreamDestinationParent final : public PChildToParentStreamParent,
public IPCStreamDestination {
public:
nsresult Initialize() { return IPCStreamDestination::Initialize(); }
~IPCStreamDestinationParent() = default;
private:
// PChildToParentStreamParent methods
void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
IPCResult RecvBuffer(const wr::ByteBuffer& aBuffer) override {
BufferReceived(aBuffer);
return IPC_OK();
}
IPCResult RecvClose(const nsresult& aRv) override {
CloseReceived(aRv);
return IPC_OK();
}
// IPCStreamDestination methods
void StartReading() override {
MOZ_ASSERT(HasDelayedStart());
Unused << SendStartReading();
}
void RequestClose(nsresult aRv) override { Unused << SendRequestClose(aRv); }
void TerminateDestination() override { Unused << Send__delete__(this); }
};
} // anonymous namespace
PChildToParentStreamParent* AllocPChildToParentStreamParent() {
IPCStreamDestinationParent* actor = new IPCStreamDestinationParent();
if (NS_WARN_IF(NS_FAILED(actor->Initialize()))) {
delete actor;
actor = nullptr;
}
return actor;
}
void DeallocPChildToParentStreamParent(PChildToParentStreamParent* aActor) {
delete aActor;
}
/* static */
IPCStreamDestination* IPCStreamDestination::Cast(
PChildToParentStreamParent* aActor) {
MOZ_ASSERT(aActor);
return static_cast<IPCStreamDestinationParent*>(aActor);
}
} // namespace ipc
} // namespace mozilla