2011-06-03 11:33:56 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-06-03 11:33:56 -07:00
|
|
|
|
|
|
|
#include "base/process_util.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/AsyncChannel.h"
|
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
|
|
|
#include "mozilla/ipc/Transport.h"
|
|
|
|
|
|
|
|
using namespace base;
|
2011-06-03 11:33:56 -07:00
|
|
|
using namespace IPC;
|
2011-06-03 11:33:56 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
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);
|
2012-09-17 01:37:20 -07:00
|
|
|
IPC::WriteParam(this, static_cast<uint32_t>(aProtocol));
|
2011-06-03 11:33:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool Read(const IPC::Message& aMsg,
|
|
|
|
TransportDescriptor* aDescriptor,
|
|
|
|
ProcessId* aOtherProcess,
|
|
|
|
ProtocolId* aProtocol)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
void* iter = nullptr;
|
2011-06-03 11:33:56 -07:00
|
|
|
if (!IPC::ReadParam(&aMsg, &iter, aDescriptor) ||
|
|
|
|
!IPC::ReadParam(&aMsg, &iter, aOtherProcess) ||
|
2012-09-17 01:37:20 -07:00
|
|
|
!IPC::ReadParam(&aMsg, &iter, reinterpret_cast<uint32_t*>(aProtocol))) {
|
2011-06-03 11:33:56 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aMsg.EndRead(iter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Bridge(const PrivateIPDLInterface&,
|
|
|
|
AsyncChannel* aParentChannel, ProcessHandle aParentProcess,
|
|
|
|
AsyncChannel* aChildChannel, ProcessHandle aChildProcess,
|
|
|
|
ProtocolId aProtocol)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
aProtocol))) {
|
|
|
|
CloseDescriptor(parentSide);
|
|
|
|
CloseDescriptor(childSide);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-03 11:33:56 -07:00
|
|
|
bool
|
|
|
|
Open(const PrivateIPDLInterface&,
|
|
|
|
AsyncChannel* aOpenerChannel, ProcessHandle aOtherProcess,
|
|
|
|
Transport::Mode aOpenerMode,
|
|
|
|
ProtocolId aProtocol)
|
|
|
|
{
|
|
|
|
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, aProtocol);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-03 11:33:56 -07:00
|
|
|
bool
|
|
|
|
UnpackChannelOpened(const PrivateIPDLInterface&,
|
2011-06-03 11:33:56 -07:00
|
|
|
const Message& aMsg,
|
2011-06-03 11:33:56 -07:00
|
|
|
TransportDescriptor* aTransport,
|
|
|
|
ProcessId* aOtherProcess,
|
|
|
|
ProtocolId* aProtocol)
|
|
|
|
{
|
|
|
|
return ChannelOpened::Read(aMsg, aTransport, aOtherProcess, aProtocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|