| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/rand_util.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/win/windows_version.h" | |
| 17 #include "mojo/edk/embedder/platform_handle.h" | |
| 18 | |
| 19 namespace mojo { | |
| 20 namespace embedder { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 std::wstring GeneratePipeName() { | |
| 25 return base::StringPrintf(L"\\\\.\\pipe\\mojo.%u.%u.%I64u", | |
| 26 GetCurrentProcessId(), GetCurrentThreadId(), | |
| 27 base::RandUint64()); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 PlatformChannelPair::PlatformChannelPair() { | |
| 33 std::wstring pipe_name = GeneratePipeName(); | |
| 34 | |
| 35 const DWORD kOpenMode = | |
| 36 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE; | |
| 37 const DWORD kPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE; | |
| 38 server_handle_.reset(PlatformHandle( | |
| 39 CreateNamedPipeW(pipe_name.c_str(), kOpenMode, kPipeMode, | |
| 40 1, // Max instances. | |
| 41 4096, // Out buffer size. | |
| 42 4096, // In buffer size. | |
| 43 5000, // Timeout in milliseconds. | |
| 44 nullptr))); // Default security descriptor. | |
| 45 PCHECK(server_handle_.is_valid()); | |
| 46 | |
| 47 const DWORD kDesiredAccess = GENERIC_READ | GENERIC_WRITE; | |
| 48 // The SECURITY_ANONYMOUS flag means that the server side cannot impersonate | |
| 49 // the client. | |
| 50 const DWORD kFlags = | |
| 51 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | FILE_FLAG_OVERLAPPED; | |
| 52 // Allow the handle to be inherited by child processes. | |
| 53 SECURITY_ATTRIBUTES security_attributes = { | |
| 54 sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE}; | |
| 55 client_handle_.reset( | |
| 56 PlatformHandle(CreateFileW(pipe_name.c_str(), kDesiredAccess, | |
| 57 0, // No sharing. | |
| 58 &security_attributes, OPEN_EXISTING, kFlags, | |
| 59 nullptr))); // No template file. | |
| 60 PCHECK(client_handle_.is_valid()); | |
| 61 | |
| 62 // Since a client has connected, ConnectNamedPipe() should return zero and | |
| 63 // GetLastError() should return ERROR_PIPE_CONNECTED. | |
| 64 CHECK(!ConnectNamedPipe(server_handle_.get().handle, nullptr)); | |
| 65 PCHECK(GetLastError() == ERROR_PIPE_CONNECTED); | |
| 66 } | |
| 67 | |
| 68 // static | |
| 69 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( | |
| 70 const base::CommandLine& command_line) { | |
| 71 std::string client_handle_string = | |
| 72 command_line.GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | |
| 73 | |
| 74 int client_handle_value = 0; | |
| 75 if (client_handle_string.empty() || | |
| 76 !base::StringToInt(client_handle_string, &client_handle_value)) { | |
| 77 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | |
| 78 return ScopedPlatformHandle(); | |
| 79 } | |
| 80 | |
| 81 return ScopedPlatformHandle( | |
| 82 PlatformHandle(LongToHandle(client_handle_value))); | |
| 83 } | |
| 84 | |
| 85 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | |
| 86 base::CommandLine* command_line, | |
| 87 base::HandlesToInheritVector* handle_passing_info) const { | |
| 88 DCHECK(command_line); | |
| 89 DCHECK(handle_passing_info); | |
| 90 DCHECK(client_handle_.is_valid()); | |
| 91 | |
| 92 CHECK_GE(base::win::GetVersion(), base::win::VERSION_VISTA); | |
| 93 | |
| 94 handle_passing_info->push_back(client_handle_.get().handle); | |
| 95 | |
| 96 // Log a warning if the command line already has the switch, but "clobber" it | |
| 97 // anyway, since it's reasonably likely that all the switches were just copied | |
| 98 // from the parent. | |
| 99 LOG_IF(WARNING, command_line->HasSwitch(kMojoPlatformChannelHandleSwitch)) | |
| 100 << "Child command line already has switch --" | |
| 101 << kMojoPlatformChannelHandleSwitch << "=" | |
| 102 << command_line->GetSwitchValueASCII(kMojoPlatformChannelHandleSwitch); | |
| 103 // (Any existing switch won't actually be removed from the command line, but | |
| 104 // the last one appended takes precedence.) | |
| 105 command_line->AppendSwitchASCII( | |
| 106 kMojoPlatformChannelHandleSwitch, | |
| 107 base::IntToString(HandleToLong(client_handle_.get().handle))); | |
| 108 } | |
| 109 | |
| 110 } // namespace embedder | |
| 111 } // namespace mojo | |
| OLD | NEW |