This source file includes following definitions.
- GeneratePipeName
- PassClientHandleFromParentProcess
- PrepareToPassClientHandleToChildProcess
#include "mojo/embedder/platform_channel_pair.h"
#include <windows.h>
#include <string>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/win/windows_version.h"
#include "mojo/embedder/platform_handle.h"
namespace mojo {
namespace embedder {
namespace {
std::wstring GeneratePipeName() {
return base::StringPrintf(
L"\\\\.\\pipe\\mojo.%u.%u.%I64u",
GetCurrentProcessId(), GetCurrentThreadId(), base::RandUint64());
}
}
PlatformChannelPair::PlatformChannelPair() {
std::wstring pipe_name = GeneratePipeName();
const DWORD kOpenMode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
FILE_FLAG_FIRST_PIPE_INSTANCE;
const DWORD kPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE;
server_handle_.reset(PlatformHandle(
CreateNamedPipeW(pipe_name.c_str(),
kOpenMode,
kPipeMode,
1,
4096,
4096,
5000,
NULL)));
PCHECK(server_handle_.is_valid());
const DWORD kDesiredAccess = GENERIC_READ | GENERIC_WRITE;
const DWORD kFlags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS |
FILE_FLAG_OVERLAPPED;
SECURITY_ATTRIBUTES security_attributes = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE
};
client_handle_.reset(PlatformHandle(
CreateFileW(pipe_name.c_str(),
kDesiredAccess,
0,
&security_attributes,
OPEN_EXISTING,
kFlags,
NULL)));
PCHECK(client_handle_.is_valid());
CHECK(!ConnectNamedPipe(server_handle_.get().handle, NULL));
PCHECK(GetLastError() == ERROR_PIPE_CONNECTED);
}
ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess(
const CommandLine& command_line) {
std::string client_handle_string =
command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
int client_handle_value = 0;
if (client_handle_string.empty() ||
!base::StringToInt(client_handle_string, &client_handle_value)) {
LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch;
return ScopedPlatformHandle();
}
return ScopedPlatformHandle(
PlatformHandle(LongToHandle(client_handle_value)));
}
void PlatformChannelPair::PrepareToPassClientHandleToChildProcess(
CommandLine* command_line,
base::HandlesToInheritVector* handle_passing_info) const {
DCHECK(command_line);
DCHECK(handle_passing_info);
DCHECK(client_handle_.is_valid());
CHECK_GE(base::win::GetVersion(), base::win::VERSION_VISTA);
handle_passing_info->push_back(client_handle_.get().handle);
LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch))
<< "Child command line already has switch --"
<< kMojoPlatformChannelHandleSwitch << "="
<< command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch);
command_line->AppendSwitchASCII(
kMojoPlatformChannelHandleSwitch,
base::IntToString(HandleToLong(client_handle_.get().handle)));
}
}
}