Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(871)

Side by Side Diff: mojo/edk/system/channel.cc

Issue 1890043002: [mojo-edk] Support transferring null Mach ports. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/channel.h ('k') | mojo/edk/system/channel_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/channel.h" 5 #include "mojo/edk/system/channel.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 size_t extra_header_size = 0; 47 size_t extra_header_size = 0;
48 #if defined(OS_WIN) 48 #if defined(OS_WIN)
49 // On Windows we serialize platform handles into the extra header space. 49 // On Windows we serialize platform handles into the extra header space.
50 extra_header_size = max_handles_ * sizeof(PlatformHandle); 50 extra_header_size = max_handles_ * sizeof(PlatformHandle);
51 #elif defined(OS_MACOSX) && !defined(OS_IOS) 51 #elif defined(OS_MACOSX) && !defined(OS_IOS)
52 // On OSX, some of the platform handles may be mach ports, which are 52 // On OSX, some of the platform handles may be mach ports, which are
53 // serialised into the message buffer. Since there could be a mix of fds and 53 // serialised into the message buffer. Since there could be a mix of fds and
54 // mach ports, we store the mach ports as an <index, port> pair (of uint32_t), 54 // mach ports, we store the mach ports as an <index, port> pair (of uint32_t),
55 // so that the original ordering of handles can be re-created. 55 // so that the original ordering of handles can be re-created.
56 extra_header_size = max_handles * sizeof(MachPortsEntry); 56 if (max_handles) {
57 extra_header_size =
58 sizeof(MachPortsExtraHeader) + (max_handles * sizeof(MachPortsEntry));
59 }
57 #endif 60 #endif
58 // Pad extra header data to be aliged to |kChannelMessageAlignment| bytes. 61 // Pad extra header data to be aliged to |kChannelMessageAlignment| bytes.
59 if (extra_header_size % kChannelMessageAlignment) { 62 if (extra_header_size % kChannelMessageAlignment) {
60 extra_header_size += kChannelMessageAlignment - 63 extra_header_size += kChannelMessageAlignment -
61 (extra_header_size % kChannelMessageAlignment); 64 (extra_header_size % kChannelMessageAlignment);
62 } 65 }
63 DCHECK_EQ(0u, extra_header_size % kChannelMessageAlignment); 66 DCHECK_EQ(0u, extra_header_size % kChannelMessageAlignment);
64 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 67 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
65 DCHECK_EQ(0u, extra_header_size); 68 DCHECK_EQ(0u, extra_header_size);
66 #endif 69 #endif
(...skipping 22 matching lines...) Expand all
89 static_cast<uint16_t>(sizeof(Header) + extra_header_size); 92 static_cast<uint16_t>(sizeof(Header) + extra_header_size);
90 #endif 93 #endif
91 94
92 if (max_handles_ > 0) { 95 if (max_handles_ > 0) {
93 #if defined(OS_WIN) 96 #if defined(OS_WIN)
94 handles_ = reinterpret_cast<PlatformHandle*>(mutable_extra_header()); 97 handles_ = reinterpret_cast<PlatformHandle*>(mutable_extra_header());
95 // Initialize all handles to invalid values. 98 // Initialize all handles to invalid values.
96 for (size_t i = 0; i < max_handles_; ++i) 99 for (size_t i = 0; i < max_handles_; ++i)
97 handles()[i] = PlatformHandle(); 100 handles()[i] = PlatformHandle();
98 #elif defined(OS_MACOSX) && !defined(OS_IOS) 101 #elif defined(OS_MACOSX) && !defined(OS_IOS)
99 mach_ports_ = reinterpret_cast<MachPortsEntry*>(mutable_extra_header()); 102 mach_ports_header_ =
103 reinterpret_cast<MachPortsExtraHeader*>(mutable_extra_header());
104 mach_ports_header_->num_ports = 0;
erikchen 2016/04/15 15:53:49 why don't you set this straight to max_handles_?
Anand Mistry (off Chromium) 2016/04/15 23:29:07 Because this is the number of encoded Mach ports,
100 // Initialize all handles to invalid values. 105 // Initialize all handles to invalid values.
101 for (size_t i = 0; i < max_handles_; ++i) 106 for (size_t i = 0; i < max_handles_; ++i) {
102 mach_ports_[i] = {0, static_cast<uint32_t>(MACH_PORT_NULL)}; 107 mach_ports_header_->entries[i] =
108 {0, static_cast<uint32_t>(MACH_PORT_NULL)};
109 }
103 #endif 110 #endif
104 } 111 }
105 } 112 }
106 113
107 Channel::Message::~Message() { 114 Channel::Message::~Message() {
108 #if defined(OS_WIN) 115 #if defined(OS_WIN)
109 // On POSIX the ScopedPlatformHandleVectorPtr will do this for us. 116 // On POSIX the ScopedPlatformHandleVectorPtr will do this for us.
110 for (size_t i = 0; i < header_->num_handles; ++i) 117 for (size_t i = 0; i < header_->num_handles; ++i)
111 handles()[i].CloseIfNecessary(); 118 handles()[i].CloseIfNecessary();
112 #endif 119 #endif
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 memcpy(handles(), new_handles->data(), 241 memcpy(handles(), new_handles->data(),
235 sizeof(PlatformHandle) * new_handles->size()); 242 sizeof(PlatformHandle) * new_handles->size());
236 new_handles->clear(); 243 new_handles->clear();
237 #else 244 #else
238 std::swap(handle_vector_, new_handles); 245 std::swap(handle_vector_, new_handles);
239 #endif // defined(OS_WIN) 246 #endif // defined(OS_WIN)
240 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) 247 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID)
241 248
242 #if defined(OS_MACOSX) && !defined(OS_IOS) 249 #if defined(OS_MACOSX) && !defined(OS_IOS)
243 size_t mach_port_index = 0; 250 size_t mach_port_index = 0;
244 for (size_t i = 0; i < max_handles_; ++i) 251 if (mach_ports_header_) {
245 mach_ports_[i] = {0, static_cast<uint32_t>(MACH_PORT_NULL)}; 252 for (size_t i = 0; i < max_handles_; ++i) {
246 for (size_t i = 0; i < handle_vector_->size(); i++) { 253 mach_ports_header_->entries[i] =
247 if ((*handle_vector_)[i].type == PlatformHandle::Type::MACH || 254 {0, static_cast<uint32_t>(MACH_PORT_NULL)};
248 (*handle_vector_)[i].type == PlatformHandle::Type::MACH_NAME) {
249 mach_port_t port = (*handle_vector_)[i].port;
250 mach_ports_[mach_port_index].index = i;
251 mach_ports_[mach_port_index].mach_port = port;
252 mach_port_index++;
253 } 255 }
256 for (size_t i = 0; i < handle_vector_->size(); i++) {
257 if ((*handle_vector_)[i].type == PlatformHandle::Type::MACH ||
258 (*handle_vector_)[i].type == PlatformHandle::Type::MACH_NAME) {
259 mach_port_t port = (*handle_vector_)[i].port;
260 mach_ports_header_->entries[mach_port_index].index = i;
261 mach_ports_header_->entries[mach_port_index].mach_port = port;
262 mach_port_index++;
263 }
264 }
265 mach_ports_header_->num_ports = static_cast<uint16_t>(mach_port_index);
254 } 266 }
255 #endif 267 #endif
256 } 268 }
257 269
258 ScopedPlatformHandleVectorPtr Channel::Message::TakeHandles() { 270 ScopedPlatformHandleVectorPtr Channel::Message::TakeHandles() {
259 #if defined(OS_WIN) 271 #if defined(OS_WIN)
260 if (header_->num_handles == 0) 272 if (header_->num_handles == 0)
261 return ScopedPlatformHandleVectorPtr(); 273 return ScopedPlatformHandleVectorPtr();
262 ScopedPlatformHandleVectorPtr moved_handles( 274 ScopedPlatformHandleVectorPtr moved_handles(
263 new PlatformHandleVector(header_->num_handles)); 275 new PlatformHandleVector(header_->num_handles));
264 for (size_t i = 0; i < header_->num_handles; ++i) 276 for (size_t i = 0; i < header_->num_handles; ++i)
265 std::swap(moved_handles->at(i), handles()[i]); 277 std::swap(moved_handles->at(i), handles()[i]);
266 header_->num_handles = 0; 278 header_->num_handles = 0;
267 return moved_handles; 279 return moved_handles;
268 #elif defined(OS_MACOSX) && !defined(OS_IOS) 280 #elif defined(OS_MACOSX) && !defined(OS_IOS)
269 for (size_t i = 0; i < max_handles_; ++i) 281 if (mach_ports_header_) {
270 mach_ports_[i] = {0, static_cast<uint32_t>(MACH_PORT_NULL)}; 282 for (size_t i = 0; i < max_handles_; ++i) {
283 mach_ports_header_->entries[i] =
284 {0, static_cast<uint32_t>(MACH_PORT_NULL)};
285 }
286 mach_ports_header_->num_ports = 0;
287 }
271 header_->num_handles = 0; 288 header_->num_handles = 0;
272 return std::move(handle_vector_); 289 return std::move(handle_vector_);
273 #else 290 #else
274 header_->num_handles = 0; 291 header_->num_handles = 0;
275 return std::move(handle_vector_); 292 return std::move(handle_vector_);
276 #endif 293 #endif
277 } 294 }
278 295
279 ScopedPlatformHandleVectorPtr Channel::Message::TakeHandlesForTransport() { 296 ScopedPlatformHandleVectorPtr Channel::Message::TakeHandlesForTransport() {
280 #if defined(OS_WIN) 297 #if defined(OS_WIN)
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 return true; 540 return true;
524 } 541 }
525 542
526 void Channel::OnError() { 543 void Channel::OnError() {
527 if (delegate_) 544 if (delegate_)
528 delegate_->OnChannelError(); 545 delegate_->OnChannelError();
529 } 546 }
530 547
531 } // namespace edk 548 } // namespace edk
532 } // namespace mojo 549 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/channel.h ('k') | mojo/edk/system/channel_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698