Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/system/child_broker.h" | 5 #include "mojo/edk/system/child_broker.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "mojo/edk/embedder/embedder_internal.h" | 14 #include "mojo/edk/embedder/embedder_internal.h" |
| 15 #include "mojo/edk/embedder/platform_channel_pair.h" | 15 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 16 #include "mojo/edk/embedder/platform_shared_buffer.h" | |
| 17 #include "mojo/edk/embedder/platform_support.h" | |
| 16 #include "mojo/edk/system/broker_messages.h" | 18 #include "mojo/edk/system/broker_messages.h" |
| 17 #include "mojo/edk/system/message_pipe_dispatcher.h" | 19 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 18 | 20 |
| 21 #if defined(OS_POSIX) | |
| 22 #include <fcntl.h> | |
| 23 | |
| 24 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | |
| 25 #endif | |
| 26 | |
| 19 namespace mojo { | 27 namespace mojo { |
| 20 namespace edk { | 28 namespace edk { |
| 21 | 29 |
| 22 ChildBroker* ChildBroker::GetInstance() { | 30 ChildBroker* ChildBroker::GetInstance() { |
| 23 return base::Singleton< | 31 return base::Singleton< |
| 24 ChildBroker, base::LeakySingletonTraits<ChildBroker>>::get(); | 32 ChildBroker, base::LeakySingletonTraits<ChildBroker>>::get(); |
| 25 } | 33 } |
| 26 | 34 |
| 27 void ChildBroker::SetChildBrokerHostHandle(ScopedPlatformHandle handle) { | 35 void ChildBroker::SetChildBrokerHostHandle(ScopedPlatformHandle handle) { |
| 28 ScopedPlatformHandle parent_async_channel_handle; | 36 ScopedPlatformHandle parent_async_channel_handle; |
| 37 parent_sync_channel_ = std::move(handle); | |
| 38 | |
| 39 // We have two pipes to the parent. The first is for the token | |
| 40 // exchange for creating and passing handles on Windows, and creating shared | |
| 41 // buffers on POSIX, since the child needs the parent's help if it is | |
| 42 // sandboxed. The second is used for multiplexing related messages. We | |
| 43 // send the second over the first. | |
| 29 #if defined(OS_POSIX) | 44 #if defined(OS_POSIX) |
| 30 parent_async_channel_handle = std::move(handle); | 45 // Make the synchronous channel blocking. |
| 46 int flags = fcntl(parent_sync_channel_.get().handle, F_GETFL, 0); | |
| 47 PCHECK(flags != -1); | |
| 48 PCHECK(fcntl(parent_sync_channel_.get().handle, F_SETFL, | |
| 49 flags & ~O_NONBLOCK) != -1); | |
| 50 | |
| 51 std::deque<PlatformHandle> received_handles; | |
| 52 char buf[1]; | |
| 53 ssize_t result = PlatformChannelRecvmsg(parent_sync_channel_.get(), buf, 1, | |
| 54 &received_handles); | |
| 55 CHECK_EQ(1, result); | |
| 56 CHECK_EQ(1u, received_handles.size()); | |
| 57 parent_async_channel_handle.reset(received_handles.front()); | |
| 31 #else | 58 #else |
| 32 // On Windows we have two pipes to the parent. The first is for the token | |
| 33 // exchange for creating and passing handles, since the child needs the | |
| 34 // parent's help if it is sandboxed. The second is the same as POSIX, which is | |
| 35 // used for multiplexing related messages. So on Windows, we send the second | |
| 36 // pipe as the first string over the first one. | |
| 37 parent_sync_channel_ = handle.Pass(); | |
| 38 | |
| 39 HANDLE parent_handle = INVALID_HANDLE_VALUE; | 59 HANDLE parent_handle = INVALID_HANDLE_VALUE; |
| 40 DWORD bytes_read = 0; | 60 DWORD bytes_read = 0; |
| 41 BOOL rv = ReadFile(parent_sync_channel_.get().handle, &parent_handle, | 61 BOOL rv = ReadFile(parent_sync_channel_.get().handle, &parent_handle, |
| 42 sizeof(parent_handle), &bytes_read, NULL); | 62 sizeof(parent_handle), &bytes_read, NULL); |
| 43 CHECK(rv); | 63 CHECK(rv); |
| 44 parent_async_channel_handle.reset(PlatformHandle(parent_handle)); | 64 parent_async_channel_handle.reset(PlatformHandle(parent_handle)); |
| 65 #endif | |
| 45 sync_channel_lock_.Unlock(); | 66 sync_channel_lock_.Unlock(); |
| 46 #endif | |
| 47 | 67 |
| 48 internal::g_io_thread_task_runner->PostTask( | 68 internal::g_io_thread_task_runner->PostTask( |
| 49 FROM_HERE, | 69 FROM_HERE, |
| 50 base::Bind(&ChildBroker::InitAsyncChannel, base::Unretained(this), | 70 base::Bind(&ChildBroker::InitAsyncChannel, base::Unretained(this), |
| 51 base::Passed(&parent_async_channel_handle))); | 71 base::Passed(&parent_async_channel_handle))); |
| 52 } | 72 } |
| 53 | 73 |
| 54 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
| 55 void ChildBroker::CreatePlatformChannelPair( | 75 void ChildBroker::CreatePlatformChannelPair( |
| 56 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { | 76 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 std::vector<HANDLE> handles_temp(count); | 112 std::vector<HANDLE> handles_temp(count); |
| 93 uint32_t response_size = | 113 uint32_t response_size = |
| 94 static_cast<uint32_t>(handles_temp.size()) * sizeof(HANDLE); | 114 static_cast<uint32_t>(handles_temp.size()) * sizeof(HANDLE); |
| 95 sync_channel_lock_.Lock(); | 115 sync_channel_lock_.Lock(); |
| 96 if (WriteAndReadResponse(message, &handles_temp[0], response_size)) { | 116 if (WriteAndReadResponse(message, &handles_temp[0], response_size)) { |
| 97 for (uint32_t i = 0; i < count; ++i) | 117 for (uint32_t i = 0; i < count; ++i) |
| 98 handles[i].handle = handles_temp[i]; | 118 handles[i].handle = handles_temp[i]; |
| 99 sync_channel_lock_.Unlock(); | 119 sync_channel_lock_.Unlock(); |
| 100 } | 120 } |
| 101 } | 121 } |
| 122 #else | |
| 123 scoped_refptr<PlatformSharedBuffer> ChildBroker::CreateSharedBuffer( | |
| 124 size_t num_bytes) { | |
| 125 sync_channel_lock_.Lock(); | |
| 126 scoped_refptr<PlatformSharedBuffer> shared_buffer; | |
| 127 | |
| 128 BrokerMessage message; | |
| 129 message.size = kBrokerMessageHeaderSize + sizeof(uint32_t); | |
| 130 message.id = CREATE_SHARED_BUFFER; | |
| 131 message.shared_buffer_size = num_bytes; | |
| 132 | |
| 133 std::deque<PlatformHandle> handles; | |
| 134 if (WriteAndReadHandles(&message, &handles)) { | |
| 135 DCHECK_EQ(1u, handles.size()); | |
| 136 PlatformHandle handle = handles.front(); | |
| 137 if (handle.is_valid()) | |
| 138 shared_buffer = | |
| 139 internal::g_platform_support->CreateSharedBufferFromHandle( | |
| 140 num_bytes, ScopedPlatformHandle(handles.front())); | |
| 141 } | |
| 142 | |
| 143 sync_channel_lock_.Unlock(); | |
| 144 return shared_buffer; | |
| 145 } | |
| 102 #endif | 146 #endif |
| 103 | 147 |
| 104 void ChildBroker::ConnectMessagePipe(uint64_t pipe_id, | 148 void ChildBroker::ConnectMessagePipe(uint64_t pipe_id, |
| 105 MessagePipeDispatcher* message_pipe) { | 149 MessagePipeDispatcher* message_pipe) { |
| 106 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); | 150 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); |
| 107 | 151 |
| 108 ConnectMessagePipeMessage data; | 152 ConnectMessagePipeMessage data; |
| 109 memset(&data, 0, sizeof(data)); | 153 memset(&data, 0, sizeof(data)); |
| 110 data.pipe_id = pipe_id; | 154 data.pipe_id = pipe_id; |
| 111 if (pending_connects_.find(pipe_id) != pending_connects_.end()) { | 155 if (pending_connects_.find(pipe_id) != pending_connects_.end()) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 connected_pipes_[message_pipe]->RemoveRoute(pipe_id); | 209 connected_pipes_[message_pipe]->RemoveRoute(pipe_id); |
| 166 connected_pipes_.erase(message_pipe); | 210 connected_pipes_.erase(message_pipe); |
| 167 } | 211 } |
| 168 | 212 |
| 169 ChildBroker::ChildBroker() | 213 ChildBroker::ChildBroker() |
| 170 : parent_async_channel_(nullptr), | 214 : parent_async_channel_(nullptr), |
| 171 in_process_pipes_channel1_(nullptr), | 215 in_process_pipes_channel1_(nullptr), |
| 172 in_process_pipes_channel2_(nullptr) { | 216 in_process_pipes_channel2_(nullptr) { |
| 173 DCHECK(!internal::g_broker); | 217 DCHECK(!internal::g_broker); |
| 174 internal::g_broker = this; | 218 internal::g_broker = this; |
| 175 #if defined(OS_WIN) | |
| 176 // Block any threads from calling this until we have a pipe to the parent. | 219 // Block any threads from calling this until we have a pipe to the parent. |
| 177 sync_channel_lock_.Lock(); | 220 sync_channel_lock_.Lock(); |
| 178 #endif | |
| 179 } | 221 } |
| 180 | 222 |
| 181 ChildBroker::~ChildBroker() { | 223 ChildBroker::~ChildBroker() { |
| 182 } | 224 } |
| 183 | 225 |
| 184 void ChildBroker::OnReadMessage( | 226 void ChildBroker::OnReadMessage( |
| 185 const MessageInTransit::View& message_view, | 227 const MessageInTransit::View& message_view, |
| 186 ScopedPlatformHandleVectorPtr platform_handles) { | 228 ScopedPlatformHandleVectorPtr platform_handles) { |
| 187 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); | 229 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); |
| 188 MultiplexMessages type = | 230 MultiplexMessages type = |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 // then when it's read it returns no messages because it doesn't have the | 320 // then when it's read it returns no messages because it doesn't have the |
| 279 // channel yet. | 321 // channel yet. |
| 280 message_pipe->GotNonTransferableChannel(raw_channel->channel()); | 322 message_pipe->GotNonTransferableChannel(raw_channel->channel()); |
| 281 // The above call could have caused |CloseMessagePipe| to be called. | 323 // The above call could have caused |CloseMessagePipe| to be called. |
| 282 if (connected_pipes_.find(message_pipe) != connected_pipes_.end()) | 324 if (connected_pipes_.find(message_pipe) != connected_pipes_.end()) |
| 283 raw_channel->AddRoute(pipe_id, message_pipe); | 325 raw_channel->AddRoute(pipe_id, message_pipe); |
| 284 } | 326 } |
| 285 | 327 |
| 286 #if defined(OS_WIN) | 328 #if defined(OS_WIN) |
| 287 | 329 |
| 330 void ChildBroker::CreatePlatformChannelPairNoLock( | |
| 331 ScopedPlatformHandle* server, | |
| 332 ScopedPlatformHandle* client) { | |
| 333 BrokerMessage message; | |
| 334 message.size = kBrokerMessageHeaderSize; | |
| 335 message.id = CREATE_PLATFORM_CHANNEL_PAIR; | |
| 336 | |
| 337 uint32_t response_size = 2 * sizeof(HANDLE); | |
| 338 HANDLE handles[2]; | |
| 339 if (WriteAndReadResponse(&message, handles, response_size)) { | |
| 340 server->reset(PlatformHandle(handles[0])); | |
| 341 client->reset(PlatformHandle(handles[1])); | |
| 342 } | |
| 343 } | |
| 344 | |
| 288 bool ChildBroker::WriteAndReadResponse(BrokerMessage* message, | 345 bool ChildBroker::WriteAndReadResponse(BrokerMessage* message, |
| 289 void* response, | 346 void* response, |
| 290 uint32_t response_size) { | 347 uint32_t response_size) { |
| 291 CHECK(parent_sync_channel_.is_valid()); | 348 CHECK(parent_sync_channel_.is_valid()); |
| 292 | 349 |
| 293 bool result = true; | 350 bool result = true; |
| 294 DWORD bytes_written = 0; | 351 DWORD bytes_written = 0; |
| 295 // This will always write in one chunk per | 352 // This will always write in one chunk per |
| 296 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365150.aspx. | 353 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365150.aspx. |
| 297 BOOL rv = WriteFile(parent_sync_channel_.get().handle, message, message->size, | 354 BOOL rv = WriteFile(parent_sync_channel_.get().handle, message, message->size, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 309 result = false; | 366 result = false; |
| 310 break; | 367 break; |
| 311 } | 368 } |
| 312 response_size -= bytes_read; | 369 response_size -= bytes_read; |
| 313 response = static_cast<char*>(response) + bytes_read; | 370 response = static_cast<char*>(response) + bytes_read; |
| 314 } | 371 } |
| 315 } | 372 } |
| 316 | 373 |
| 317 return result; | 374 return result; |
| 318 } | 375 } |
| 376 #else | |
| 377 bool ChildBroker::WriteAndReadHandles(BrokerMessage* message, | |
| 378 std::deque<PlatformHandle>* handles) { | |
| 379 DCHECK_EQ(0, | |
| 380 fcntl(parent_sync_channel_.get().handle, F_GETFL, 0) & O_NONBLOCK); | |
|
jam
2016/01/08 22:37:40
code inside DCHECK will get compiled out in releas
Eliot Courtney
2016/01/10 01:31:58
Sorry, this line shouldn't be here -- I was just v
| |
| 381 CHECK(parent_sync_channel_.is_valid()); | |
| 319 | 382 |
| 320 void ChildBroker::CreatePlatformChannelPairNoLock( | 383 uint32_t remaining_bytes = message->size; |
| 321 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { | 384 while (remaining_bytes > 0) { |
| 322 BrokerMessage message; | 385 ssize_t bytes_written = PlatformChannelWrite( |
| 323 message.size = kBrokerMessageHeaderSize; | 386 parent_sync_channel_.get(), |
| 324 message.id = CREATE_PLATFORM_CHANNEL_PAIR; | 387 reinterpret_cast<uint8_t*>(message) + (message->size - remaining_bytes), |
| 388 remaining_bytes); | |
| 325 | 389 |
| 326 uint32_t response_size = 2 * sizeof(HANDLE); | 390 if (bytes_written != -1) |
| 327 HANDLE handles[2]; | 391 remaining_bytes -= bytes_written; |
| 328 if (WriteAndReadResponse(&message, handles, response_size)) { | 392 else |
| 329 server->reset(PlatformHandle(handles[0])); | 393 return false; |
| 330 client->reset(PlatformHandle(handles[1])); | |
| 331 } | 394 } |
| 395 | |
| 396 // Perform a blocking read (we set this fd to be blocking in | |
| 397 // SetChildBrokerHostHandle). | |
| 398 char buf[1]; | |
| 399 ssize_t bytes_read = | |
| 400 PlatformChannelRecvmsg(parent_sync_channel_.get(), buf, 1, handles); | |
| 401 // If the other side shutdown, or there was an error, or we didn't get | |
| 402 // any handles. | |
| 403 if (bytes_read == 0 || bytes_read == -1 || handles->empty()) | |
| 404 return false; | |
| 405 | |
| 406 return true; | |
| 332 } | 407 } |
| 333 | |
| 334 #endif | 408 #endif |
| 335 | 409 |
| 336 } // namespace edk | 410 } // namespace edk |
| 337 } // namespace mojo | 411 } // namespace mojo |
| OLD | NEW |