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