Index: ipc/mojo/ipc_channel_mojo.cc |
diff --git a/ipc/mojo/ipc_channel_mojo.cc b/ipc/mojo/ipc_channel_mojo.cc |
index d2d7fe5b7bd84089bd821bd3b030ac282c88f6a1..c3c6e0c446f91719cdbf5ea529c7e86ad8824af1 100644 |
--- a/ipc/mojo/ipc_channel_mojo.cc |
+++ b/ipc/mojo/ipc_channel_mojo.cc |
@@ -13,6 +13,7 @@ |
#include "ipc/ipc_message_macros.h" |
#include "ipc/mojo/client_channel.mojom.h" |
#include "ipc/mojo/ipc_mojo_bootstrap.h" |
+#include "ipc/mojo/ipc_mojo_handle_attachment.h" |
#include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" |
@@ -341,29 +342,7 @@ int ChannelMojo::GetClientFileDescriptor() const { |
base::ScopedFD ChannelMojo::TakeClientFileDescriptor() { |
return bootstrap_->TakeClientFileDescriptor(); |
} |
- |
-// static |
-MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
- const std::vector<MojoHandle>& handle_buffer, |
- Message* message) { |
- for (size_t i = 0; i < handle_buffer.size(); ++i) { |
- mojo::embedder::ScopedPlatformHandle platform_handle; |
- MojoResult unwrap_result = mojo::embedder::PassWrappedPlatformHandle( |
- handle_buffer[i], &platform_handle); |
- if (unwrap_result != MOJO_RESULT_OK) { |
- DLOG(WARNING) << "Pipe failed to covert handles. Closing: " |
- << unwrap_result; |
- return unwrap_result; |
- } |
- |
- bool ok = message->attachment_set()->AddAttachment( |
- new internal::PlatformFileAttachment( |
- base::ScopedFD(platform_handle.release().fd))); |
- DCHECK(ok); |
- } |
- |
- return MOJO_RESULT_OK; |
-} |
+#endif // defined(OS_POSIX) && !defined(OS_NACL) |
// static |
MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
@@ -373,38 +352,71 @@ MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
// IPC::MessageAttachmentSet has intricate lifecycle semantics |
// of FDs, so just to dup()-and-own them is the safest option. |
if (message->HasAttachments()) { |
- MessageAttachmentSet* fdset = message->attachment_set(); |
- std::vector<base::PlatformFile> fds_to_send(fdset->size()); |
- fdset->PeekDescriptors(&fds_to_send[0]); |
- for (size_t i = 0; i < fds_to_send.size(); ++i) { |
- int fd_to_send = dup(fds_to_send[i]); |
- if (-1 == fd_to_send) { |
- DPLOG(WARNING) << "Failed to dup FD to transmit."; |
- fdset->CommitAll(); |
- return MOJO_RESULT_UNKNOWN; |
- } |
- |
- MojoHandle wrapped_handle; |
- MojoResult wrap_result = CreatePlatformHandleWrapper( |
- mojo::embedder::ScopedPlatformHandle( |
- mojo::embedder::PlatformHandle(fd_to_send)), |
- &wrapped_handle); |
- if (MOJO_RESULT_OK != wrap_result) { |
- DLOG(WARNING) << "Pipe failed to wrap handles. Closing: " |
- << wrap_result; |
- fdset->CommitAll(); |
- return wrap_result; |
+ MessageAttachmentSet* set = message->attachment_set(); |
+ for (unsigned i = 0; i < set->size(); ++i) { |
+ scoped_refptr<MessageAttachment> attachment = set->GetAttachmentAt(i); |
+ switch (attachment->GetType()) { |
+ case MessageAttachment::TYPE_PLATFORM_FILE: |
+#if defined(OS_POSIX) && !defined(OS_NACL) |
+ { |
+ base::PlatformFile file = |
+ dup(static_cast<IPC::internal::PlatformFileAttachment*>( |
+ attachment.get())->file()); |
+ if (file == -1) { |
+ DPLOG(WARNING) << "Failed to dup FD to transmit."; |
+ set->CommitAll(); |
+ return MOJO_RESULT_UNKNOWN; |
+ } |
+ |
+ MojoHandle wrapped_handle; |
+ MojoResult wrap_result = CreatePlatformHandleWrapper( |
+ mojo::embedder::ScopedPlatformHandle( |
+ mojo::embedder::PlatformHandle(file)), |
+ &wrapped_handle); |
+ if (MOJO_RESULT_OK != wrap_result) { |
+ DLOG(WARNING) << "Pipe failed to wrap handles. Closing: " |
+ << wrap_result; |
+ set->CommitAll(); |
+ return wrap_result; |
+ } |
+ |
+ handles->push_back(wrapped_handle); |
+ } |
+#else |
+ NOTREACHED(); |
+#endif // defined(OS_POSIX) && !defined(OS_NACL) |
+ break; |
+ case MessageAttachment::TYPE_MOJO_HANDLE: { |
+ mojo::ScopedHandle handle = |
+ static_cast<IPC::internal::MojoHandleAttachment*>( |
+ attachment.get())->TakeHandle(); |
+ handles->push_back(handle.release().value()); |
+ } break; |
} |
- |
- handles->push_back(wrapped_handle); |
} |
- fdset->CommitAll(); |
+ set->CommitAll(); |
} |
return MOJO_RESULT_OK; |
} |
-#endif // defined(OS_POSIX) && !defined(OS_NACL) |
+// static |
+MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
+ const std::vector<MojoHandle>& handle_buffer, |
+ Message* message) { |
+ for (size_t i = 0; i < handle_buffer.size(); ++i) { |
+ bool ok = message->attachment_set()->AddAttachment( |
+ new IPC::internal::MojoHandleAttachment( |
+ mojo::MakeScopedHandle(mojo::Handle(handle_buffer[i])))); |
+ DCHECK(ok); |
+ if (!ok) { |
+ DLOG(ERROR) << "Failed to add new Mojo handle."; |
+ return MOJO_RESULT_UNKNOWN; |
+ } |
+ } |
+ |
+ return MOJO_RESULT_OK; |
+} |
} // namespace IPC |