Skip to content

Commit

Permalink
Fuchsia: Add platform implementation for Mojo WrapAttachmentImpl.
Browse files Browse the repository at this point in the history
* Fix buggy DLOG_IF() logging statement.


Bug: 746674
Change-Id: I878dfaac8f574d9c6c6c97c0040077a7f59eec4d
Reviewed-on: https://chromium-review.googlesource.com/636783
Reviewed-by: Ken Rockot <rockot@chromium.org>
Reviewed-by: Wez <wez@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#497843}
  • Loading branch information
Wez authored and Commit Bot committed Aug 28, 2017
1 parent 54d88c8 commit 71a6a8e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
7 changes: 3 additions & 4 deletions ipc/handle_attachment_fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down
5 changes: 1 addition & 4 deletions ipc/handle_attachment_fuchsia.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
46 changes: 38 additions & 8 deletions ipc/ipc_channel_mojo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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<uint64_t>(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) {
Expand All @@ -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 =
Expand All @@ -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<internal::HandleAttachmentFuchsia&>(*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 =
Expand Down Expand Up @@ -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<mx_handle_t>(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)
Expand Down
4 changes: 4 additions & 0 deletions mojo/edk/embedder/platform_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
#include <mach/mach.h>
#elif defined(OS_FUCHSIA)
#include <magenta/syscalls.h>
#include <mxio/limits.h>
#endif

#include "base/logging.h"

namespace mojo {
namespace edk {

Expand All @@ -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;
}
Expand Down

0 comments on commit 71a6a8e

Please sign in to comment.