Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "base/process_util.h"
10 #include "mozilla/ipc/MessageChannel.h"
11 #include "mozilla/ipc/ProtocolUtils.h"
12 #include "mozilla/ipc/Transport.h"
14 using namespace base;
15 using namespace IPC;
17 namespace mozilla {
18 namespace ipc {
20 IToplevelProtocol::~IToplevelProtocol()
21 {
22 mOpenActors.clear();
23 }
25 void IToplevelProtocol::AddOpenedActor(IToplevelProtocol* aActor)
26 {
27 #ifdef DEBUG
28 for (const IToplevelProtocol* actor = mOpenActors.getFirst();
29 actor;
30 actor = actor->getNext()) {
31 NS_ASSERTION(actor != aActor,
32 "Open the same protocol for more than one time");
33 }
34 #endif
36 mOpenActors.insertBack(aActor);
37 }
39 IToplevelProtocol*
40 IToplevelProtocol::CloneToplevel(const InfallibleTArray<ProtocolFdMapping>& aFds,
41 base::ProcessHandle aPeerProcess,
42 ProtocolCloneContext* aCtx)
43 {
44 NS_NOTREACHED("Clone() for this protocol actor is not implemented");
45 return nullptr;
46 }
48 void
49 IToplevelProtocol::CloneOpenedToplevels(IToplevelProtocol* aTemplate,
50 const InfallibleTArray<ProtocolFdMapping>& aFds,
51 base::ProcessHandle aPeerProcess,
52 ProtocolCloneContext* aCtx)
53 {
54 for (IToplevelProtocol* actor = aTemplate->GetFirstOpenedActors();
55 actor;
56 actor = actor->getNext()) {
57 IToplevelProtocol* newactor = actor->CloneToplevel(aFds, aPeerProcess, aCtx);
58 AddOpenedActor(newactor);
59 }
60 }
62 class ChannelOpened : public IPC::Message
63 {
64 public:
65 ChannelOpened(TransportDescriptor aDescriptor,
66 ProcessId aOtherProcess,
67 ProtocolId aProtocol)
68 : IPC::Message(MSG_ROUTING_CONTROL, // these only go to top-level actors
69 CHANNEL_OPENED_MESSAGE_TYPE,
70 PRIORITY_NORMAL)
71 {
72 IPC::WriteParam(this, aDescriptor);
73 IPC::WriteParam(this, aOtherProcess);
74 IPC::WriteParam(this, static_cast<uint32_t>(aProtocol));
75 }
77 static bool Read(const IPC::Message& aMsg,
78 TransportDescriptor* aDescriptor,
79 ProcessId* aOtherProcess,
80 ProtocolId* aProtocol)
81 {
82 void* iter = nullptr;
83 if (!IPC::ReadParam(&aMsg, &iter, aDescriptor) ||
84 !IPC::ReadParam(&aMsg, &iter, aOtherProcess) ||
85 !IPC::ReadParam(&aMsg, &iter, reinterpret_cast<uint32_t*>(aProtocol))) {
86 return false;
87 }
88 aMsg.EndRead(iter);
89 return true;
90 }
91 };
93 bool
94 Bridge(const PrivateIPDLInterface&,
95 MessageChannel* aParentChannel, ProcessHandle aParentProcess,
96 MessageChannel* aChildChannel, ProcessHandle aChildProcess,
97 ProtocolId aProtocol, ProtocolId aChildProtocol)
98 {
99 ProcessId parentId = GetProcId(aParentProcess);
100 ProcessId childId = GetProcId(aChildProcess);
101 if (!parentId || !childId) {
102 return false;
103 }
105 TransportDescriptor parentSide, childSide;
106 if (!CreateTransport(aParentProcess, aChildProcess,
107 &parentSide, &childSide)) {
108 return false;
109 }
111 if (!aParentChannel->Send(new ChannelOpened(parentSide,
112 childId,
113 aProtocol)) ||
114 !aChildChannel->Send(new ChannelOpened(childSide,
115 parentId,
116 aChildProtocol))) {
117 CloseDescriptor(parentSide);
118 CloseDescriptor(childSide);
119 return false;
120 }
121 return true;
122 }
124 bool
125 Open(const PrivateIPDLInterface&,
126 MessageChannel* aOpenerChannel, ProcessHandle aOtherProcess,
127 Transport::Mode aOpenerMode,
128 ProtocolId aProtocol, ProtocolId aChildProtocol)
129 {
130 bool isParent = (Transport::MODE_SERVER == aOpenerMode);
131 ProcessHandle thisHandle = GetCurrentProcessHandle();
132 ProcessHandle parentHandle = isParent ? thisHandle : aOtherProcess;
133 ProcessHandle childHandle = !isParent ? thisHandle : aOtherProcess;
134 ProcessId parentId = GetProcId(parentHandle);
135 ProcessId childId = GetProcId(childHandle);
136 if (!parentId || !childId) {
137 return false;
138 }
140 TransportDescriptor parentSide, childSide;
141 if (!CreateTransport(parentHandle, childHandle,
142 &parentSide, &childSide)) {
143 return false;
144 }
146 Message* parentMsg = new ChannelOpened(parentSide, childId, aProtocol);
147 Message* childMsg = new ChannelOpened(childSide, parentId, aChildProtocol);
148 nsAutoPtr<Message> messageForUs(isParent ? parentMsg : childMsg);
149 nsAutoPtr<Message> messageForOtherSide(!isParent ? parentMsg : childMsg);
150 if (!aOpenerChannel->Echo(messageForUs.forget()) ||
151 !aOpenerChannel->Send(messageForOtherSide.forget())) {
152 CloseDescriptor(parentSide);
153 CloseDescriptor(childSide);
154 return false;
155 }
156 return true;
157 }
159 bool
160 UnpackChannelOpened(const PrivateIPDLInterface&,
161 const Message& aMsg,
162 TransportDescriptor* aTransport,
163 ProcessId* aOtherProcess,
164 ProtocolId* aProtocol)
165 {
166 return ChannelOpened::Read(aMsg, aTransport, aOtherProcess, aProtocol);
167 }
169 void
170 ProtocolErrorBreakpoint(const char* aMsg)
171 {
172 // Bugs that generate these error messages can be tough to
173 // reproduce. Log always in the hope that someone finds the error
174 // message.
175 printf_stderr("IPDL protocol error: %s\n", aMsg);
176 }
178 void
179 FatalError(const char* aProtocolName, const char* aMsg,
180 ProcessHandle aHandle, bool aIsParent)
181 {
182 ProtocolErrorBreakpoint(aMsg);
184 nsAutoCString formattedMessage("IPDL error [");
185 formattedMessage.AppendASCII(aProtocolName);
186 formattedMessage.AppendLiteral("]: \"");
187 formattedMessage.AppendASCII(aMsg);
188 if (aIsParent) {
189 formattedMessage.AppendLiteral("\". Killing child side as a result.");
190 NS_ERROR(formattedMessage.get());
192 if (aHandle != kInvalidProcessHandle &&
193 !base::KillProcess(aHandle, base::PROCESS_END_KILLED_BY_USER, false)) {
194 NS_ERROR("May have failed to kill child!");
195 }
196 } else {
197 formattedMessage.AppendLiteral("\". abort()ing as a result.");
198 NS_RUNTIMEABORT(formattedMessage.get());
199 }
200 }
202 } // namespace ipc
203 } // namespace mozilla