From 71a6a8e8bf0ffbf501d5726d04c8388736605d97 Mon Sep 17 00:00:00 2001 From: Wez Date: Mon, 28 Aug 2017 19:38:05 +0000 Subject: [PATCH] Fuchsia: Add platform implementation for Mojo WrapAttachmentImpl. * Fix buggy DLOG_IF() logging statement. Bug: 746674 Change-Id: I878dfaac8f574d9c6c6c97c0040077a7f59eec4d Reviewed-on: https://chromium-review.googlesource.com/636783 Reviewed-by: Ken Rockot Reviewed-by: Wez Commit-Queue: Wez Cr-Commit-Position: refs/heads/master@{#497843} --- ipc/handle_attachment_fuchsia.cc | 7 ++--- ipc/handle_attachment_fuchsia.h | 5 +--- ipc/ipc_channel_mojo.cc | 46 ++++++++++++++++++++++++----- mojo/edk/embedder/platform_handle.h | 4 +++ 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/ipc/handle_attachment_fuchsia.cc b/ipc/handle_attachment_fuchsia.cc index 9d4d13f0c81f27..88cf5184dd6bd4 100644 --- a/ipc/handle_attachment_fuchsia.cc +++ b/ipc/handle_attachment_fuchsia.cc @@ -13,13 +13,12 @@ namespace internal { HandleAttachmentFuchsia::HandleAttachmentFuchsia(const mx_handle_t& handle) { mx_status_t result = mx_handle_duplicate(handle, MX_RIGHT_SAME_RIGHTS, handle_.receive()); - DLOG_IF(ERROR, result == MX_OK) + DLOG_IF(ERROR, result != MX_OK) << "mx_handle_duplicate: " << mx_status_get_string(result); } -HandleAttachmentFuchsia::HandleAttachmentFuchsia(const mx_handle_t& handle, - FromWire from_wire) - : handle_(handle) {} +HandleAttachmentFuchsia::HandleAttachmentFuchsia(base::ScopedMxHandle handle) + : handle_(std::move(handle)) {} HandleAttachmentFuchsia::~HandleAttachmentFuchsia() {} diff --git a/ipc/handle_attachment_fuchsia.h b/ipc/handle_attachment_fuchsia.h index 072df0c3052ae3..915e41663b8f3e 100644 --- a/ipc/handle_attachment_fuchsia.h +++ b/ipc/handle_attachment_fuchsia.h @@ -22,12 +22,9 @@ class IPC_EXPORT HandleAttachmentFuchsia : public MessageAttachment { // result. Should only be called by the sender of a Chrome IPC message. explicit HandleAttachmentFuchsia(const mx_handle_t& handle); - enum FromWire { - FROM_WIRE, - }; // This constructor takes ownership of |handle|. Should only be called by the // receiver of a Chrome IPC message. - HandleAttachmentFuchsia(const mx_handle_t& handle, FromWire from_wire); + explicit HandleAttachmentFuchsia(base::ScopedMxHandle handle); Type GetType() const override; diff --git a/ipc/ipc_channel_mojo.cc b/ipc/ipc_channel_mojo.cc index 006a9ab65d076c..af5b86bc1d9a4f 100644 --- a/ipc/ipc_channel_mojo.cc +++ b/ipc/ipc_channel_mojo.cc @@ -40,6 +40,10 @@ #include "ipc/handle_attachment_win.h" #endif +#if defined(OS_FUCHSIA) +#include "ipc/handle_attachment_fuchsia.h" +#endif + namespace IPC { namespace { @@ -92,7 +96,6 @@ MojoResult WrapPlatformHandle(base::PlatformFile handle, } #if defined(OS_MACOSX) - MojoResult WrapMachPort(mach_port_t mach_port, mojom::SerializedHandlePtr* serialized) { MojoPlatformHandle platform_handle = { @@ -110,17 +113,31 @@ MojoResult WrapMachPort(mach_port_t mach_port, mojom::SerializedHandle::Type::MACH_PORT); return MOJO_RESULT_OK; } +#elif defined(OS_FUCHSIA) +MojoResult WrapMxHandle(mx_handle_t handle, + mojom::SerializedHandlePtr* serialized) { + MojoPlatformHandle platform_handle = { + sizeof(MojoPlatformHandle), MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE, + static_cast(handle)}; -#endif + MojoHandle wrapped_handle; + MojoResult result = MojoWrapPlatformHandle(&platform_handle, &wrapped_handle); + if (result != MOJO_RESULT_OK) + return result; -#if defined(OS_POSIX) + *serialized = CreateSerializedHandle( + mojo::MakeScopedHandle(mojo::Handle(wrapped_handle)), + mojom::SerializedHandle::Type::FUCHSIA_HANDLE); + return MOJO_RESULT_OK; +} +#endif // defined(OS_FUCHSIA) +#if defined(OS_POSIX) base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) { return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) : base::ScopedFD(dup(attachment->file())); } - -#endif +#endif // defined(OS_POSIX) MojoResult WrapAttachmentImpl(MessageAttachment* attachment, mojom::SerializedHandlePtr* serialized) { @@ -146,7 +163,7 @@ MojoResult WrapAttachmentImpl(MessageAttachment* attachment, mojom::SerializedHandle::Type::PLATFORM_FILE, serialized); } -#endif +#endif // defined(OS_POSIX) #if defined(OS_MACOSX) DCHECK_EQ(attachment->GetType(), MessageAttachment::Type::MACH_PORT); internal::MachPortAttachmentMac& mach_port_attachment = @@ -155,6 +172,12 @@ MojoResult WrapAttachmentImpl(MessageAttachment* attachment, serialized); mach_port_attachment.reset_mach_port_ownership(); return result; +#elif defined(OS_FUCHSIA) + DCHECK_EQ(attachment->GetType(), MessageAttachment::Type::FUCHSIA_HANDLE); + internal::HandleAttachmentFuchsia& handle_attachment = + static_cast(*attachment); + MojoResult result = WrapMxHandle(handle_attachment.Take(), serialized); + return result; #elif defined(OS_WIN) DCHECK_EQ(attachment->GetType(), MessageAttachment::Type::WIN_HANDLE); internal::HandleAttachmentWin& handle_attachment = @@ -211,8 +234,15 @@ MojoResult UnwrapAttachment(mojom::SerializedHandlePtr handle, mach_port, internal::MachPortAttachmentMac::FROM_WIRE); return MOJO_RESULT_OK; } -#endif // defined(OS_MACOSX) -#if defined(OS_WIN) +#elif defined(OS_FUCHSIA) + if (handle->type == mojom::SerializedHandle::Type::FUCHSIA_HANDLE) { + base::ScopedMxHandle handle; + if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE) + handle.reset(static_cast(platform_handle.value)); + *attachment = new internal::HandleAttachmentFuchsia(std::move(handle)); + return MOJO_RESULT_OK; + } +#elif defined(OS_WIN) if (handle->type == mojom::SerializedHandle::Type::WIN_HANDLE) { base::PlatformFile handle = base::kInvalidPlatformFile; if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE) diff --git a/mojo/edk/embedder/platform_handle.h b/mojo/edk/embedder/platform_handle.h index 2624a65c2d5bfc..aa866219c4dae8 100644 --- a/mojo/edk/embedder/platform_handle.h +++ b/mojo/edk/embedder/platform_handle.h @@ -16,8 +16,11 @@ #include #elif defined(OS_FUCHSIA) #include +#include #endif +#include "base/logging.h" + namespace mojo { namespace edk { @@ -34,6 +37,7 @@ struct MOJO_SYSTEM_IMPL_EXPORT PlatformHandle { } static PlatformHandle ForFd(int fd) { PlatformHandle platform_handle; + DCHECK_LT(fd, MAX_MXIO_FD); platform_handle.fd = fd; return platform_handle; }