Skip to content

Commit

Permalink
Add helper function to generate a pair of ChannelHandles for ChannelM…
Browse files Browse the repository at this point in the history
…ojo.

BUG=604282

Review-Url: https://codereview.chromium.org/2109213003
Cr-Commit-Position: refs/heads/master@{#403106}
  • Loading branch information
akmistry authored and Commit bot committed Jun 30, 2016
1 parent ab8c381 commit 9fdf320
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ipc/ipc_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ class IPC_EXPORT Channel : public Endpoint {
static std::string GenerateVerifiedChannelID(const std::string& prefix);
#endif

// Generates a pair of channel handles that can be used as the client and
// server ends of a ChannelMojo. |name_postfix| is appended to the end of the
// handle name to help identify the handles.
//
// Note, when using ChannelMojo, |ChannelHandle::name| serves no functional
// purpose other than to identify the channel in logging.
static void GenerateMojoChannelHandlePair(
const std::string& name_postfix,
IPC::ChannelHandle* handle0,
IPC::ChannelHandle* handle1);

#if defined(OS_LINUX)
// Sandboxed processes live in a PID namespace, so when sending the IPC hello
// message from client to server we need to send the PID from the global
Expand Down
25 changes: 25 additions & 0 deletions ipc/ipc_channel_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "build/build_config.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_channel_mojo.h"
#include "mojo/public/cpp/system/message_pipe.h"

namespace IPC {

Expand Down Expand Up @@ -56,6 +57,30 @@ std::unique_ptr<Channel> Channel::CreateServer(
return Channel::Create(channel_handle, Channel::MODE_SERVER, listener);
}

// static
void Channel::GenerateMojoChannelHandlePair(
const std::string& name_postfix,
IPC::ChannelHandle* handle0,
IPC::ChannelHandle* handle1) {
DCHECK_NE(handle0, handle1);
// |name| is only used for logging and to aid developers in debugging. It
// doesn't _need_ to be unique, but this string is probably more useful than a
// generic "ChannelMojo".
#if !defined(OS_NACL_SFI)
std::string name = "ChannelMojo-" + GenerateUniqueRandomChannelID();
#else
std::string name = "ChannelMojo";
#endif
if (!name_postfix.empty()) {
name += "-" + name_postfix;
}
mojo::MessagePipe message_pipe;
*handle0 = ChannelHandle(name);
handle0->mojo_handle = message_pipe.handle0.release();
*handle1 = ChannelHandle(name);
handle1->mojo_handle = message_pipe.handle1.release();
}

Channel::~Channel() {
}

Expand Down

0 comments on commit 9fdf320

Please sign in to comment.