OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/mach_port_relay.h" | 5 #include "mojo/edk/system/mach_port_relay.h" |
6 | 6 |
7 #include <mach/mach.h> | 7 #include <mach/mach.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/mac/mach_port_util.h" | 12 #include "base/mac/mach_port_util.h" |
13 #include "base/mac/scoped_mach_port.h" | 13 #include "base/mac/scoped_mach_port.h" |
14 #include "base/process/process.h" | 14 #include "base/process/process.h" |
15 #include "mojo/edk/embedder/platform_handle_vector.h" | 15 #include "mojo/edk/embedder/platform_handle_vector.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace edk { | 18 namespace edk { |
19 | 19 |
20 // static | 20 // static |
21 bool MachPortRelay::ReceivePorts(PlatformHandleVector* handles) { | 21 bool MachPortRelay::ReceivePorts(PlatformHandleVector* handles) { |
22 DCHECK(handles); | 22 DCHECK(handles); |
23 | 23 |
24 for (size_t i = 0; i < handles->size(); i++) { | 24 for (size_t i = 0; i < handles->size(); i++) { |
25 PlatformHandle* handle = handles->data() + i; | 25 PlatformHandle* handle = handles->data() + i; |
26 DCHECK(handle->type != PlatformHandle::Type::MACH); | 26 DCHECK(handle->type != PlatformHandle::Type::MACH); |
27 if (handle->type != PlatformHandle::Type::MACH_NAME) | 27 if (handle->type != PlatformHandle::Type::MACH_NAME) |
28 continue; | 28 continue; |
29 | 29 |
| 30 if (handle->port == MACH_PORT_NULL) { |
| 31 handle->type = PlatformHandle::Type::MACH; |
| 32 continue; |
| 33 } |
| 34 |
30 base::mac::ScopedMachReceiveRight message_port(handle->port); | 35 base::mac::ScopedMachReceiveRight message_port(handle->port); |
31 base::mac::ScopedMachSendRight received_port( | 36 base::mac::ScopedMachSendRight received_port( |
32 base::ReceiveMachPort(message_port.get())); | 37 base::ReceiveMachPort(message_port.get())); |
33 if (received_port.get() == MACH_PORT_NULL) { | 38 if (received_port.get() == MACH_PORT_NULL) { |
34 handle->port = MACH_PORT_NULL; | 39 handle->port = MACH_PORT_NULL; |
35 LOG(ERROR) << "Error receiving mach port"; | 40 LOG(ERROR) << "Error receiving mach port"; |
36 return false; | 41 return false; |
37 } | 42 } |
38 | 43 |
39 handle->port = received_port.release(); | 44 handle->port = received_port.release(); |
(...skipping 25 matching lines...) Expand all Loading... |
65 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); | 70 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); |
66 // Message should have handles, otherwise there's no point in calling this | 71 // Message should have handles, otherwise there's no point in calling this |
67 // function. | 72 // function. |
68 DCHECK(handles); | 73 DCHECK(handles); |
69 for (size_t i = 0; i < handles->size(); i++) { | 74 for (size_t i = 0; i < handles->size(); i++) { |
70 PlatformHandle* handle = &(*handles)[i]; | 75 PlatformHandle* handle = &(*handles)[i]; |
71 DCHECK(handle->type != PlatformHandle::Type::MACH_NAME); | 76 DCHECK(handle->type != PlatformHandle::Type::MACH_NAME); |
72 if (handle->type != PlatformHandle::Type::MACH) | 77 if (handle->type != PlatformHandle::Type::MACH) |
73 continue; | 78 continue; |
74 | 79 |
| 80 if (handle->port == MACH_PORT_NULL) { |
| 81 handle->type = PlatformHandle::Type::MACH_NAME; |
| 82 num_sent++; |
| 83 continue; |
| 84 } |
| 85 |
75 mach_port_name_t intermediate_port; | 86 mach_port_name_t intermediate_port; |
76 DCHECK(handle->port != MACH_PORT_NULL); | |
77 intermediate_port = base::CreateIntermediateMachPort( | 87 intermediate_port = base::CreateIntermediateMachPort( |
78 task_port, base::mac::ScopedMachSendRight(handle->port), nullptr); | 88 task_port, base::mac::ScopedMachSendRight(handle->port), nullptr); |
79 if (intermediate_port == MACH_PORT_NULL) { | 89 if (intermediate_port == MACH_PORT_NULL) { |
80 handle->port = MACH_PORT_NULL; | 90 handle->port = MACH_PORT_NULL; |
81 error = true; | 91 error = true; |
82 break; | 92 break; |
83 } | 93 } |
84 handle->port = intermediate_port; | 94 handle->port = intermediate_port; |
85 handle->type = PlatformHandle::Type::MACH_NAME; | 95 handle->type = PlatformHandle::Type::MACH_NAME; |
86 num_sent++; | 96 num_sent++; |
(...skipping 17 matching lines...) Expand all Loading... |
104 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); | 114 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); |
105 // Message should have handles, otherwise there's no point in calling this | 115 // Message should have handles, otherwise there's no point in calling this |
106 // function. | 116 // function. |
107 DCHECK(handles); | 117 DCHECK(handles); |
108 for (size_t i = 0; i < handles->size(); i++) { | 118 for (size_t i = 0; i < handles->size(); i++) { |
109 PlatformHandle* handle = handles->data() + i; | 119 PlatformHandle* handle = handles->data() + i; |
110 DCHECK(handle->type != PlatformHandle::Type::MACH); | 120 DCHECK(handle->type != PlatformHandle::Type::MACH); |
111 if (handle->type != PlatformHandle::Type::MACH_NAME) | 121 if (handle->type != PlatformHandle::Type::MACH_NAME) |
112 continue; | 122 continue; |
113 | 123 |
| 124 if (handle->port == MACH_PORT_NULL) { |
| 125 handle->type = PlatformHandle::Type::MACH; |
| 126 num_received++; |
| 127 continue; |
| 128 } |
| 129 |
114 mach_port_t extracted_right = MACH_PORT_NULL; | 130 mach_port_t extracted_right = MACH_PORT_NULL; |
115 mach_msg_type_name_t extracted_right_type; | 131 mach_msg_type_name_t extracted_right_type; |
116 kern_return_t kr = | 132 kern_return_t kr = |
117 mach_port_extract_right(task_port, handle->port, | 133 mach_port_extract_right(task_port, handle->port, |
118 MACH_MSG_TYPE_MOVE_SEND, | 134 MACH_MSG_TYPE_MOVE_SEND, |
119 &extracted_right, &extracted_right_type); | 135 &extracted_right, &extracted_right_type); |
120 if (kr != KERN_SUCCESS) { | 136 if (kr != KERN_SUCCESS) { |
121 error = true; | 137 error = true; |
122 break; | 138 break; |
123 } | 139 } |
(...skipping 22 matching lines...) Expand all Loading... |
146 } | 162 } |
147 | 163 |
148 void MachPortRelay::OnReceivedTaskPort(base::ProcessHandle process) { | 164 void MachPortRelay::OnReceivedTaskPort(base::ProcessHandle process) { |
149 base::AutoLock locker(observers_lock_); | 165 base::AutoLock locker(observers_lock_); |
150 for (const auto observer : observers_) | 166 for (const auto observer : observers_) |
151 observer->OnProcessReady(process); | 167 observer->OnProcessReady(process); |
152 } | 168 } |
153 | 169 |
154 } // namespace edk | 170 } // namespace edk |
155 } // namespace mojo | 171 } // namespace mojo |
OLD | NEW |