forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocolUtils.cpp
203 lines (180 loc) · 6.32 KB
/
ProtocolUtils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=8 et :
*/
/* 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 "base/process_util.h"
#include "mozilla/ipc/MessageChannel.h"
#include "mozilla/ipc/ProtocolUtils.h"
#include "mozilla/ipc/Transport.h"
using namespace base;
using namespace IPC;
namespace mozilla {
namespace ipc {
IToplevelProtocol::~IToplevelProtocol()
{
mOpenActors.clear();
}
void IToplevelProtocol::AddOpenedActor(IToplevelProtocol* aActor)
{
#ifdef DEBUG
for (const IToplevelProtocol* actor = mOpenActors.getFirst();
actor;
actor = actor->getNext()) {
NS_ASSERTION(actor != aActor,
"Open the same protocol for more than one time");
}
#endif
mOpenActors.insertBack(aActor);
}
IToplevelProtocol*
IToplevelProtocol::CloneToplevel(const InfallibleTArray<ProtocolFdMapping>& aFds,
base::ProcessHandle aPeerProcess,
ProtocolCloneContext* aCtx)
{
NS_NOTREACHED("Clone() for this protocol actor is not implemented");
return nullptr;
}
void
IToplevelProtocol::CloneOpenedToplevels(IToplevelProtocol* aTemplate,
const InfallibleTArray<ProtocolFdMapping>& aFds,
base::ProcessHandle aPeerProcess,
ProtocolCloneContext* aCtx)
{
for (IToplevelProtocol* actor = aTemplate->GetFirstOpenedActors();
actor;
actor = actor->getNext()) {
IToplevelProtocol* newactor = actor->CloneToplevel(aFds, aPeerProcess, aCtx);
AddOpenedActor(newactor);
}
}
class ChannelOpened : public IPC::Message
{
public:
ChannelOpened(TransportDescriptor aDescriptor,
ProcessId aOtherProcess,
ProtocolId aProtocol)
: IPC::Message(MSG_ROUTING_CONTROL, // these only go to top-level actors
CHANNEL_OPENED_MESSAGE_TYPE,
PRIORITY_NORMAL)
{
IPC::WriteParam(this, aDescriptor);
IPC::WriteParam(this, aOtherProcess);
IPC::WriteParam(this, static_cast<uint32_t>(aProtocol));
}
static bool Read(const IPC::Message& aMsg,
TransportDescriptor* aDescriptor,
ProcessId* aOtherProcess,
ProtocolId* aProtocol)
{
void* iter = nullptr;
if (!IPC::ReadParam(&aMsg, &iter, aDescriptor) ||
!IPC::ReadParam(&aMsg, &iter, aOtherProcess) ||
!IPC::ReadParam(&aMsg, &iter, reinterpret_cast<uint32_t*>(aProtocol))) {
return false;
}
aMsg.EndRead(iter);
return true;
}
};
bool
Bridge(const PrivateIPDLInterface&,
MessageChannel* aParentChannel, ProcessHandle aParentProcess,
MessageChannel* aChildChannel, ProcessHandle aChildProcess,
ProtocolId aProtocol, ProtocolId aChildProtocol)
{
ProcessId parentId = GetProcId(aParentProcess);
ProcessId childId = GetProcId(aChildProcess);
if (!parentId || !childId) {
return false;
}
TransportDescriptor parentSide, childSide;
if (!CreateTransport(aParentProcess, aChildProcess,
&parentSide, &childSide)) {
return false;
}
if (!aParentChannel->Send(new ChannelOpened(parentSide,
childId,
aProtocol)) ||
!aChildChannel->Send(new ChannelOpened(childSide,
parentId,
aChildProtocol))) {
CloseDescriptor(parentSide);
CloseDescriptor(childSide);
return false;
}
return true;
}
bool
Open(const PrivateIPDLInterface&,
MessageChannel* aOpenerChannel, ProcessHandle aOtherProcess,
Transport::Mode aOpenerMode,
ProtocolId aProtocol, ProtocolId aChildProtocol)
{
bool isParent = (Transport::MODE_SERVER == aOpenerMode);
ProcessHandle thisHandle = GetCurrentProcessHandle();
ProcessHandle parentHandle = isParent ? thisHandle : aOtherProcess;
ProcessHandle childHandle = !isParent ? thisHandle : aOtherProcess;
ProcessId parentId = GetProcId(parentHandle);
ProcessId childId = GetProcId(childHandle);
if (!parentId || !childId) {
return false;
}
TransportDescriptor parentSide, childSide;
if (!CreateTransport(parentHandle, childHandle,
&parentSide, &childSide)) {
return false;
}
Message* parentMsg = new ChannelOpened(parentSide, childId, aProtocol);
Message* childMsg = new ChannelOpened(childSide, parentId, aChildProtocol);
nsAutoPtr<Message> messageForUs(isParent ? parentMsg : childMsg);
nsAutoPtr<Message> messageForOtherSide(!isParent ? parentMsg : childMsg);
if (!aOpenerChannel->Echo(messageForUs.forget()) ||
!aOpenerChannel->Send(messageForOtherSide.forget())) {
CloseDescriptor(parentSide);
CloseDescriptor(childSide);
return false;
}
return true;
}
bool
UnpackChannelOpened(const PrivateIPDLInterface&,
const Message& aMsg,
TransportDescriptor* aTransport,
ProcessId* aOtherProcess,
ProtocolId* aProtocol)
{
return ChannelOpened::Read(aMsg, aTransport, aOtherProcess, aProtocol);
}
void
ProtocolErrorBreakpoint(const char* aMsg)
{
// Bugs that generate these error messages can be tough to
// reproduce. Log always in the hope that someone finds the error
// message.
printf_stderr("IPDL protocol error: %s\n", aMsg);
}
void
FatalError(const char* aProtocolName, const char* aMsg,
ProcessHandle aHandle, bool aIsParent)
{
ProtocolErrorBreakpoint(aMsg);
nsAutoCString formattedMessage("IPDL error [");
formattedMessage.AppendASCII(aProtocolName);
formattedMessage.AppendLiteral("]: \"");
formattedMessage.AppendASCII(aMsg);
if (aIsParent) {
formattedMessage.AppendLiteral("\". Killing child side as a result.");
NS_ERROR(formattedMessage.get());
if (aHandle != kInvalidProcessHandle &&
!base::KillProcess(aHandle, base::PROCESS_END_KILLED_BY_USER, false)) {
NS_ERROR("May have failed to kill child!");
}
} else {
formattedMessage.AppendLiteral("\". abort()ing as a result.");
NS_RUNTIMEABORT(formattedMessage.get());
}
}
} // namespace ipc
} // namespace mozilla