Commit d5c877ac authored by Robert Sesek's avatar Robert Sesek Committed by Commit Bot

Add new Mojo PlatformHandle type declarations for Mach receive rights.

The two types of Mach port rights, send and receive, require distinct
handling. This CL deprecates the TYPE_MACH_PORT name, since it does not
specify the underlying right's type.

Bug: 932175
Change-Id: I4d49eaba1566d8742b7e8235cacc54e0a0075202
Reviewed-on: https://chromium-review.googlesource.com/c/1489176
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#635804}
parent eb9d827d
......@@ -30,8 +30,7 @@ typedef uint32_t MojoPlatformHandleType;
// usable on POSIX host systems (e.g. Android, Linux, Chrome OS, Mac).
#define MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR ((MojoPlatformHandleType)1)
// The |MojoPlatformHandle| value represents a Mach port right (e.g. a value
// opaquely of type |mach_port_t|). Only usable on Mac OS X hosts.
// Deprecated. TYPE_MACH_PORT is equivalent to TYPE_MACH_SEND_RIGHT.
#define MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT ((MojoPlatformHandleType)2)
// The |MojoPlatformHandle| value represents a Windows HANDLE value. Only usable
......@@ -42,6 +41,15 @@ typedef uint32_t MojoPlatformHandleType;
// usable on Fuchsia hosts.
#define MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE ((MojoPlatformHandleType)4)
// The |MojoPlatformHandle| value represents a Mach send right (e.g. a value
// opaquely of type |mach_port_t|). Only usable on macOS hosts.
#define MOJO_PLATFORM_HANDLE_TYPE_MACH_SEND_RIGHT \
MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT
// The |MojoPlatformHandle| value represents a Mach receive right (e.g. a value
// opaquely of type |mach_port_t|). Only usable on macOS hosts.
#define MOJO_PLATFORM_HANDLE_TYPE_MACH_RECEIVE_RIGHT ((MojoPlatformHandleType)5)
// |MojoPlatformHandle|: A handle to a native platform object.
//
// |uint32_t struct_size|: The size of this structure. Used for versioning
......
......@@ -95,7 +95,9 @@ PlatformHandle::PlatformHandle(zx::handle handle)
: type_(Type::kHandle), handle_(std::move(handle)) {}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
PlatformHandle::PlatformHandle(base::mac::ScopedMachSendRight mach_port)
: type_(Type::kMachPort), mach_port_(std::move(mach_port)) {}
: type_(Type::kMachSend), mach_send_(std::move(mach_port)) {}
PlatformHandle::PlatformHandle(base::mac::ScopedMachReceiveRight mach_port)
: type_(Type::kMachReceive), mach_receive_(std::move(mach_port)) {}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......@@ -118,7 +120,8 @@ PlatformHandle& PlatformHandle::operator=(PlatformHandle&& other) {
#elif defined(OS_FUCHSIA)
handle_ = std::move(other.handle_);
#elif defined(OS_MACOSX) && !defined(OS_IOS)
mach_port_ = std::move(other.mach_port_);
mach_send_ = std::move(other.mach_send_);
mach_receive_ = std::move(other.mach_receive_);
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......@@ -152,10 +155,14 @@ void PlatformHandle::ToMojoPlatformHandle(PlatformHandle handle,
break;
}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.is_mach_port()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
if (handle.is_mach_send()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_SEND_RIGHT;
out_handle->value = static_cast<uint64_t>(handle.ReleaseMachSendRight());
break;
} else if (handle.is_mach_receive()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_RECEIVE_RIGHT;
out_handle->value =
static_cast<uint64_t>(handle.TakeMachPort().release());
static_cast<uint64_t>(handle.ReleaseMachReceiveRight());
break;
}
#endif
......@@ -188,9 +195,12 @@ PlatformHandle PlatformHandle::FromMojoPlatformHandle(
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE)
return PlatformHandle(zx::handle(handle->value));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT) {
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_MACH_SEND_RIGHT) {
return PlatformHandle(base::mac::ScopedMachSendRight(
static_cast<mach_port_t>(handle->value)));
} else if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_MACH_RECEIVE_RIGHT) {
return PlatformHandle(base::mac::ScopedMachReceiveRight(
static_cast<mach_port_t>(handle->value)));
}
#endif
......@@ -209,7 +219,8 @@ void PlatformHandle::reset() {
#elif defined(OS_FUCHSIA)
handle_.reset();
#elif defined(OS_MACOSX) && !defined(OS_IOS)
mach_port_.reset();
mach_send_.reset();
mach_receive_.reset();
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......@@ -225,7 +236,8 @@ void PlatformHandle::release() {
#elif defined(OS_FUCHSIA)
ignore_result(handle_.release());
#elif defined(OS_MACOSX) && !defined(OS_IOS)
ignore_result(mach_port_.release());
ignore_result(mach_send_.release());
ignore_result(mach_receive_.release());
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......@@ -241,8 +253,9 @@ PlatformHandle PlatformHandle::Clone() const {
return PlatformHandle(CloneHandle(handle_));
return PlatformHandle(CloneFD(fd_));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (is_valid_mach_port())
return PlatformHandle(CloneMachPort(mach_port_));
if (is_valid_mach_send())
return PlatformHandle(CloneMachPort(mach_send_));
CHECK(!is_valid_mach_receive()) << "Cannot clone Mach receive rights";
return PlatformHandle(CloneFD(fd_));
#elif defined(OS_POSIX)
return PlatformHandle(CloneFD(fd_));
......
......@@ -47,6 +47,8 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
kHandle,
#elif defined(OS_MACOSX) && !defined(OS_IOS)
kMachPort,
kMachSend = kMachPort,
kMachReceive,
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
kFd,
......@@ -62,6 +64,7 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
explicit PlatformHandle(zx::handle handle);
#elif defined(OS_MACOSX) && !defined(OS_IOS)
explicit PlatformHandle(base::mac::ScopedMachSendRight mach_port);
explicit PlatformHandle(base::mac::ScopedMachReceiveRight mach_port);
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......@@ -129,20 +132,47 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
bool is_valid() const { return is_valid_fd() || is_valid_mach_port(); }
bool is_valid_mach_port() const { return mach_port_.is_valid(); }
bool is_mach_port() const { return type_ == Type::kMachPort; }
const base::mac::ScopedMachSendRight& GetMachPort() const {
return mach_port_;
bool is_valid_mach_port() const {
return is_valid_mach_send() || is_valid_mach_receive();
}
bool is_valid_mach_send() const { return mach_send_.is_valid(); }
bool is_mach_send() const { return type_ == Type::kMachSend; }
const base::mac::ScopedMachSendRight& GetMachSendRight() const {
return mach_send_;
}
base::mac::ScopedMachSendRight TakeMachPort() {
if (type_ == Type::kMachPort)
base::mac::ScopedMachSendRight TakeMachSendRight() {
if (type_ == Type::kMachSend)
type_ = Type::kNone;
return std::move(mach_port_);
return std::move(mach_send_);
}
mach_port_t ReleaseMachPort() WARN_UNUSED_RESULT {
if (type_ == Type::kMachPort)
mach_port_t ReleaseMachSendRight() WARN_UNUSED_RESULT {
return TakeMachSendRight().release();
}
bool is_valid_mach_receive() const { return mach_receive_.is_valid(); }
bool is_mach_receive() const { return type_ == Type::kMachReceive; }
const base::mac::ScopedMachReceiveRight& GetMachReceiveRight() const {
return mach_receive_;
}
base::mac::ScopedMachReceiveRight TakeMachReceiveRight() {
if (type_ == Type::kMachReceive)
type_ = Type::kNone;
return mach_port_.release();
return std::move(mach_receive_);
}
mach_port_t ReleaseMachReceiveRight() WARN_UNUSED_RESULT {
return TakeMachReceiveRight().release();
}
// The following Mach port methods are deprecated. Use the ones above
// instead.
bool is_mach_port() const { return type_ == Type::kMachPort; }
const base::mac::ScopedMachSendRight& GetMachPort() const {
return GetMachSendRight();
}
base::mac::ScopedMachSendRight TakeMachPort() { return TakeMachSendRight(); }
mach_port_t ReleaseMachPort() WARN_UNUSED_RESULT {
return ReleaseMachSendRight();
}
#elif defined(OS_POSIX)
bool is_valid() const { return is_valid_fd(); }
......@@ -174,7 +204,8 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
#elif defined(OS_FUCHSIA)
zx::handle handle_;
#elif defined(OS_MACOSX) && !defined(OS_IOS)
base::mac::ScopedMachSendRight mach_port_;
base::mac::ScopedMachSendRight mach_send_;
base::mac::ScopedMachReceiveRight mach_receive_;
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment