Commit a22cf312 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Begin migration of EDK away from InternalPlatformHandle

Migrates a few bits of the EDK from ScopedInternalPlatformHandle
and InternalPlatformHandle over to the public PlatformHandle type.

Adds an explicit Type enumeration to PlatformHandle since there
can be subtle semantic differences between an abstract invalid
handle, vs (e.g.) an invalid FD, vs (e.g.) a null Mach port.

Also moves some stuff from mojo/edk/embedder to mojo/edk/system
where it belongs.

Bug: 753541
Change-Id: I362757a27c3fba3794097b1d32341d1e703b90d0
Reviewed-on: https://chromium-review.googlesource.com/1107392Reviewed-by: default avatarJay Civelli <jcivelli@chromium.org>
Commit-Queue: Ken Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569361}
parent 3bf89d01
......@@ -66,13 +66,11 @@ template("core_impl_source_set") {
public = [
"embedder/configuration.h",
"embedder/connection_params.h",
"embedder/entrypoints.h",
"embedder/named_platform_channel_pair.h",
"embedder/named_platform_handle.h",
"embedder/named_platform_handle_utils.h",
"embedder/platform_channel_pair.h",
"embedder/platform_handle.h",
"embedder/platform_handle_utils.h",
"embedder/process_error_callback.h",
"embedder/scoped_platform_handle.h",
"embedder/transport_protocol.h",
......@@ -83,6 +81,7 @@ template("core_impl_source_set") {
"system/data_pipe_control_message.h",
"system/data_pipe_producer_dispatcher.h",
"system/dispatcher.h",
"system/entrypoints.h",
"system/handle_signals_state.h",
"system/handle_table.h",
"system/invitation_dispatcher.h",
......@@ -90,6 +89,7 @@ template("core_impl_source_set") {
"system/node_controller.h",
"system/options_validation.h",
"system/platform_handle_dispatcher.h",
"system/platform_handle_utils.h",
"system/platform_shared_memory_mapping.h",
"system/request_context.h",
"system/scoped_process_handle.h",
......@@ -105,13 +105,10 @@ template("core_impl_source_set") {
sources = [
"embedder/connection_params.cc",
"embedder/entrypoints.cc",
"embedder/named_platform_handle_utils_win.cc",
"embedder/platform_channel_pair.cc",
"embedder/platform_channel_pair_win.cc",
"embedder/platform_handle.cc",
"embedder/platform_handle_utils.cc",
"embedder/platform_handle_utils_win.cc",
"system/atomic_flag.h",
"system/broker.h",
"system/broker_win.cc",
......@@ -123,6 +120,7 @@ template("core_impl_source_set") {
"system/data_pipe_control_message.cc",
"system/data_pipe_producer_dispatcher.cc",
"system/dispatcher.cc",
"system/entrypoints.cc",
"system/handle_table.cc",
"system/invitation_dispatcher.cc",
"system/message_pipe_dispatcher.cc",
......@@ -130,6 +128,7 @@ template("core_impl_source_set") {
"system/node_channel.h",
"system/node_controller.cc",
"system/platform_handle_dispatcher.cc",
"system/platform_handle_utils.cc",
"system/platform_shared_memory_mapping.cc",
"system/request_context.cc",
"system/scoped_process_handle.cc",
......@@ -154,7 +153,6 @@ template("core_impl_source_set") {
sources += [
"embedder/named_platform_handle_utils_fuchsia.cc",
"embedder/platform_channel_pair_fuchsia.cc",
"embedder/platform_handle_utils_fuchsia.cc",
"system/channel_fuchsia.cc",
]
......@@ -162,10 +160,7 @@ template("core_impl_source_set") {
}
if (is_posix) {
sources += [
"embedder/platform_channel_pair_posix.cc",
"embedder/platform_handle_utils_posix.cc",
]
sources += [ "embedder/platform_channel_pair_posix.cc" ]
if (!is_nacl || is_nacl_nonsfi) {
public += [ "embedder/platform_channel_utils_posix.h" ]
......
......@@ -11,9 +11,9 @@
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/task_runner.h"
#include "mojo/edk/embedder/entrypoints.h"
#include "mojo/edk/system/configuration.h"
#include "mojo/edk/system/core.h"
#include "mojo/edk/system/entrypoints.h"
#include "mojo/edk/system/node_controller.h"
#include "mojo/public/c/system/thunks.h"
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "build/build_config.h"
namespace mojo {
namespace edk {
MojoResult MojoPlatformHandleToScopedInternalPlatformHandle(
const MojoPlatformHandle* platform_handle,
ScopedInternalPlatformHandle* out_handle) {
if (platform_handle->struct_size != sizeof(MojoPlatformHandle))
return MOJO_RESULT_INVALID_ARGUMENT;
if (platform_handle->type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) {
out_handle->reset();
return MOJO_RESULT_OK;
}
InternalPlatformHandle handle;
switch (platform_handle->type) {
#if defined(OS_FUCHSIA)
case MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE:
handle = InternalPlatformHandle::ForHandle(platform_handle->value);
break;
case MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR:
handle = InternalPlatformHandle::ForFd(platform_handle->value);
break;
#elif defined(OS_POSIX)
case MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR:
handle.handle = static_cast<int>(platform_handle->value);
break;
#endif
#if defined(OS_MACOSX) && !defined(OS_IOS)
case MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT:
handle.type = InternalPlatformHandle::Type::MACH;
handle.port = static_cast<mach_port_t>(platform_handle->value);
break;
#endif
#if defined(OS_WIN)
case MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE:
handle.handle = reinterpret_cast<HANDLE>(platform_handle->value);
break;
#endif
default:
return MOJO_RESULT_INVALID_ARGUMENT;
}
out_handle->reset(handle);
return MOJO_RESULT_OK;
}
MojoResult ScopedInternalPlatformHandleToMojoPlatformHandle(
ScopedInternalPlatformHandle handle,
MojoPlatformHandle* platform_handle) {
if (platform_handle->struct_size != sizeof(MojoPlatformHandle))
return MOJO_RESULT_INVALID_ARGUMENT;
if (!handle.is_valid()) {
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_INVALID;
return MOJO_RESULT_OK;
}
#if defined(OS_FUCHSIA)
if (handle.get().is_valid_fd()) {
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
platform_handle->value = handle.release().as_fd();
} else {
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE;
platform_handle->value = handle.release().as_handle();
}
#elif defined(OS_POSIX)
switch (handle.get().type) {
case InternalPlatformHandle::Type::POSIX:
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
platform_handle->value = static_cast<uint64_t>(handle.release().handle);
break;
#if defined(OS_MACOSX) && !defined(OS_IOS)
case InternalPlatformHandle::Type::MACH:
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
platform_handle->value = static_cast<uint64_t>(handle.release().port);
break;
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
default:
return MOJO_RESULT_INVALID_ARGUMENT;
}
#elif defined(OS_WIN)
platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
platform_handle->value = reinterpret_cast<uint64_t>(handle.release().handle);
#endif // defined(OS_WIN)
return MOJO_RESULT_OK;
}
void ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle handle,
ScopedInternalPlatformHandle* extracted_handle,
ScopedInternalPlatformHandle* extracted_readonly_handle) {
#if defined(OS_WIN)
extracted_handle->reset(InternalPlatformHandle(handle.Take()));
#elif defined(OS_FUCHSIA)
extracted_handle->reset(InternalPlatformHandle::ForHandle(handle.release()));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
// This is a Mach port. Same code as below, but separated for clarity.
extracted_handle->reset(InternalPlatformHandle(handle.release()));
#elif defined(OS_ANDROID)
// This is a file descriptor. Same code as above, but separated for clarity.
extracted_handle->reset(InternalPlatformHandle(handle.release()));
#else
extracted_handle->reset(InternalPlatformHandle(handle.fd.release()));
extracted_readonly_handle->reset(
InternalPlatformHandle(handle.readonly_fd.release()));
#endif
}
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
ScopedInternalPlatformHandle handle,
ScopedInternalPlatformHandle readonly_handle) {
#if defined(OS_WIN)
DCHECK(!readonly_handle.is_valid());
return base::win::ScopedHandle(handle.release().handle);
#elif defined(OS_FUCHSIA)
DCHECK(!readonly_handle.is_valid());
return base::ScopedZxHandle(handle.release().as_handle());
#elif defined(OS_MACOSX) && !defined(OS_IOS)
DCHECK(!readonly_handle.is_valid());
return base::mac::ScopedMachSendRight(handle.release().port);
#elif defined(OS_ANDROID)
DCHECK(!readonly_handle.is_valid());
return base::ScopedFD(handle.release().handle);
#else
return base::subtle::ScopedFDPair(
base::ScopedFD(handle.release().handle),
base::ScopedFD(readonly_handle.release().handle));
#endif
}
} // namespace edk
} // namespace mojo
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "base/logging.h"
namespace mojo {
namespace edk {
ScopedInternalPlatformHandle DuplicatePlatformHandle(
InternalPlatformHandle platform_handle) {
DCHECK(platform_handle.is_valid());
zx_handle_t duped;
// zx_handle_duplicate won't touch |duped| in case of failure.
zx_status_t result = zx_handle_duplicate(platform_handle.as_handle(),
ZX_RIGHT_SAME_RIGHTS, &duped);
DLOG_IF(ERROR, result != ZX_OK) << "zx_duplicate_handle failed: " << result;
return ScopedInternalPlatformHandle(InternalPlatformHandle::ForHandle(duped));
}
} // namespace edk
} // namespace mojo
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/platform_handle_utils.h"
#include <unistd.h>
#include "base/logging.h"
namespace mojo {
namespace edk {
ScopedInternalPlatformHandle DuplicatePlatformHandle(
InternalPlatformHandle platform_handle) {
DCHECK(platform_handle.is_valid());
// Note that |dup()| returns -1 on error (which is exactly the value we use
// for invalid |InternalPlatformHandle| FDs).
InternalPlatformHandle duped(dup(platform_handle.handle));
duped.needs_connection = platform_handle.needs_connection;
return ScopedInternalPlatformHandle(duped);
}
} // namespace edk
} // namespace mojo
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/platform_handle_utils.h"
#include <windows.h>
#include "base/logging.h"
namespace mojo {
namespace edk {
ScopedInternalPlatformHandle DuplicatePlatformHandle(
InternalPlatformHandle platform_handle) {
DCHECK(platform_handle.is_valid());
HANDLE new_handle;
CHECK_NE(platform_handle.handle, INVALID_HANDLE_VALUE);
if (!DuplicateHandle(GetCurrentProcess(), platform_handle.handle,
GetCurrentProcess(), &new_handle, 0, TRUE,
DUPLICATE_SAME_ACCESS))
return ScopedInternalPlatformHandle();
DCHECK_NE(new_handle, INVALID_HANDLE_VALUE);
return ScopedInternalPlatformHandle(InternalPlatformHandle(new_handle));
}
} // namespace edk
} // namespace mojo
......@@ -3,8 +3,8 @@
// found in the LICENSE file.
#include "base/time/time.h"
#include "mojo/edk/embedder/entrypoints.h"
#include "mojo/edk/system/core.h"
#include "mojo/edk/system/entrypoints.h"
#include "mojo/public/c/system/core.h"
#include "mojo/public/c/system/thunks.h"
......
......@@ -13,9 +13,9 @@
#include "build/build_config.h"
#include "mojo/edk/embedder/named_platform_channel_pair.h"
#include "mojo/edk/embedder/named_platform_handle.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/broker_messages.h"
#include "mojo/edk/system/platform_handle_utils.h"
namespace mojo {
namespace edk {
......@@ -110,8 +110,11 @@ void BrokerHost::OnBufferRequest(uint32_t num_bytes) {
std::vector<ScopedInternalPlatformHandle> handles(2);
if (region.IsValid()) {
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &handles[0], &handles[1]);
PlatformHandle h[2];
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &h[0], &h[1]);
handles[0] = PlatformHandleToScopedInternalPlatformHandle(std::move(h[0]));
handles[1] = PlatformHandleToScopedInternalPlatformHandle(std::move(h[1]));
#if !defined(OS_POSIX) || defined(OS_ANDROID) || defined(OS_FUCHSIA) || \
(defined(OS_MACOSX) && !defined(OS_IOS))
// Non-POSIX systems, as well as Android, Fuchsia, and non-iOS Mac, only use
......
......@@ -13,10 +13,10 @@
#include "base/memory/platform_shared_memory_region.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/platform_channel_utils_posix.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/broker_messages.h"
#include "mojo/edk/system/channel.h"
#include "mojo/edk/system/platform_handle_utils.h"
namespace mojo {
namespace edk {
......@@ -127,13 +127,18 @@ base::WritableSharedMemoryRegion Broker::GetWritableSharedMemoryRegion(
if (!GetBrokerMessageData(message.get(), &data))
return base::WritableSharedMemoryRegion();
if (incoming_platform_handles.size() == 1)
incoming_platform_handles.emplace_back();
PlatformHandle handles[2];
handles[0] = ScopedInternalPlatformHandleToPlatformHandle(
std::move(incoming_platform_handles[0]));
if (incoming_platform_handles.size() > 1) {
handles[1] = ScopedInternalPlatformHandleToPlatformHandle(
std::move(incoming_platform_handles[1]));
}
return base::WritableSharedMemoryRegion::Deserialize(
base::subtle::PlatformSharedMemoryRegion::Take(
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
std::move(incoming_platform_handles[0]),
std::move(incoming_platform_handles[1])),
CreateSharedMemoryRegionHandleFromPlatformHandles(
std::move(handles[0]), std::move(handles[1])),
base::subtle::PlatformSharedMemoryRegion::Mode::kWritable,
num_bytes,
base::UnguessableToken::Deserialize(data->guid_high,
......
......@@ -14,11 +14,11 @@
#include "mojo/edk/embedder/named_platform_handle.h"
#include "mojo/edk/embedder/named_platform_handle_utils.h"
#include "mojo/edk/embedder/platform_handle.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/broker.h"
#include "mojo/edk/system/broker_messages.h"
#include "mojo/edk/system/channel.h"
#include "mojo/edk/system/platform_handle_utils.h"
namespace mojo {
namespace edk {
......@@ -147,8 +147,9 @@ base::WritableSharedMemoryRegion Broker::GetWritableSharedMemoryRegion(
return base::WritableSharedMemoryRegion();
return base::WritableSharedMemoryRegion::Deserialize(
base::subtle::PlatformSharedMemoryRegion::Take(
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
std::move(handle), ScopedInternalPlatformHandle()),
CreateSharedMemoryRegionHandleFromPlatformHandles(
ScopedInternalPlatformHandleToPlatformHandle(std::move(handle)),
PlatformHandle()),
base::subtle::PlatformSharedMemoryRegion::Mode::kWritable,
num_bytes,
base::UnguessableToken::Deserialize(data->guid_high,
......
......@@ -23,7 +23,6 @@
#include "base/time/time.h"
#include "base/trace_event/memory_dump_manager.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/embedder/process_error_callback.h"
#include "mojo/edk/system/channel.h"
#include "mojo/edk/system/configuration.h"
......@@ -33,6 +32,7 @@
#include "mojo/edk/system/invitation_dispatcher.h"
#include "mojo/edk/system/message_pipe_dispatcher.h"
#include "mojo/edk/system/platform_handle_dispatcher.h"
#include "mojo/edk/system/platform_handle_utils.h"
#include "mojo/edk/system/platform_shared_memory_mapping.h"
#include "mojo/edk/system/ports/event.h"
#include "mojo/edk/system/ports/name.h"
......@@ -982,16 +982,16 @@ MojoResult Core::GetBufferInfo(MojoHandle buffer_handle,
return dispatcher->GetBufferInfo(info);
}
MojoResult Core::WrapInternalPlatformHandle(
MojoResult Core::WrapPlatformHandle(
const MojoPlatformHandle* platform_handle,
const MojoWrapPlatformHandleOptions* options,
MojoHandle* mojo_handle) {
ScopedInternalPlatformHandle handle;
MojoResult result = MojoPlatformHandleToScopedInternalPlatformHandle(
platform_handle, &handle);
if (result != MOJO_RESULT_OK)
return result;
if (!platform_handle ||
platform_handle->struct_size < sizeof(*platform_handle)) {
return MOJO_RESULT_INVALID_ARGUMENT;
}
auto handle = PlatformHandle::FromMojoPlatformHandle(platform_handle);
MojoHandle h =
AddDispatcher(PlatformHandleDispatcher::Create(std::move(handle)));
if (h == MOJO_HANDLE_INVALID)
......@@ -1001,10 +1001,15 @@ MojoResult Core::WrapInternalPlatformHandle(
return MOJO_RESULT_OK;
}
MojoResult Core::UnwrapInternalPlatformHandle(
MojoResult Core::UnwrapPlatformHandle(
MojoHandle mojo_handle,
const MojoUnwrapPlatformHandleOptions* options,
MojoPlatformHandle* platform_handle) {
if (!platform_handle ||
platform_handle->struct_size < sizeof(*platform_handle)) {
return MOJO_RESULT_INVALID_ARGUMENT;
}
scoped_refptr<Dispatcher> dispatcher;
{
base::AutoLock lock(handles_->GetLock());
......@@ -1020,11 +1025,11 @@ MojoResult Core::UnwrapInternalPlatformHandle(
PlatformHandleDispatcher* phd =
static_cast<PlatformHandleDispatcher*>(dispatcher.get());
ScopedInternalPlatformHandle handle = phd->PassInternalPlatformHandle();
PlatformHandle handle = phd->TakePlatformHandle();
phd->Close();
return ScopedInternalPlatformHandleToMojoPlatformHandle(std::move(handle),
platform_handle);
PlatformHandle::ToMojoPlatformHandle(std::move(handle), platform_handle);
return MOJO_RESULT_OK;
}
MojoResult Core::WrapPlatformSharedMemoryRegion(
......@@ -1048,12 +1053,11 @@ MojoResult Core::WrapPlatformSharedMemoryRegion(
return MOJO_RESULT_INVALID_ARGUMENT;
#endif
ScopedInternalPlatformHandle handles[2];
PlatformHandle handles[2];
bool handles_ok = true;
for (size_t i = 0; i < num_platform_handles; ++i) {
MojoResult result = MojoPlatformHandleToScopedInternalPlatformHandle(
&platform_handles[i], &handles[i]);
if (result != MOJO_RESULT_OK)
handles[i] = PlatformHandle::FromMojoPlatformHandle(&platform_handles[i]);
if (!handles[i].is_valid())
handles_ok = false;
}
if (!handles_ok)
......@@ -1079,7 +1083,7 @@ MojoResult Core::WrapPlatformSharedMemoryRegion(
base::subtle::PlatformSharedMemoryRegion region =
base::subtle::PlatformSharedMemoryRegion::Take(
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
CreateSharedMemoryRegionHandleFromPlatformHandles(
std::move(handles[0]), std::move(handles[1])),
mode, size, token);
if (!region.IsValid())
......@@ -1151,9 +1155,9 @@ MojoResult Core::UnwrapPlatformSharedMemoryRegion(
return MOJO_RESULT_INVALID_ARGUMENT;
}
ScopedInternalPlatformHandle handle;
ScopedInternalPlatformHandle read_only_handle;
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
PlatformHandle handle;
PlatformHandle read_only_handle;
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &handle, &read_only_handle);
const uint32_t available_handle_storage_slots = *num_platform_handles;
......@@ -1166,19 +1170,17 @@ MojoResult Core::UnwrapPlatformSharedMemoryRegion(
base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
if (available_handle_storage_slots < 2)
return MOJO_RESULT_INVALID_ARGUMENT;
if (ScopedInternalPlatformHandleToMojoPlatformHandle(
std::move(read_only_handle), &platform_handles[1]) !=
MOJO_RESULT_OK) {
PlatformHandle::ToMojoPlatformHandle(std::move(read_only_handle),
&platform_handles[1]);
if (platform_handles[1].type == MOJO_PLATFORM_HANDLE_TYPE_INVALID)
return MOJO_RESULT_INVALID_ARGUMENT;
}
*num_platform_handles = 2;
}
#endif
if (ScopedInternalPlatformHandleToMojoPlatformHandle(
std::move(handle), &platform_handles[0]) != MOJO_RESULT_OK) {
PlatformHandle::ToMojoPlatformHandle(std::move(handle), &platform_handles[0]);
if (platform_handles[0].type == MOJO_PLATFORM_HANDLE_TYPE_INVALID)
return MOJO_RESULT_INVALID_ARGUMENT;
}
return MOJO_RESULT_OK;
}
......@@ -1327,12 +1329,14 @@ MojoResult Core::SendInvitation(
auto* invitation_dispatcher =
static_cast<InvitationDispatcher*>(dispatcher.get());
ScopedInternalPlatformHandle endpoint_handle;
MojoResult result = MojoPlatformHandleToScopedInternalPlatformHandle(
&transport_endpoint->platform_handles[0], &endpoint_handle);
if (result != MOJO_RESULT_OK || !endpoint_handle.is_valid())
auto endpoint = PlatformHandle::FromMojoPlatformHandle(
&transport_endpoint->platform_handles[0]);
if (!endpoint.is_valid())
return MOJO_RESULT_INVALID_ARGUMENT;
auto endpoint_handle =
PlatformHandleToScopedInternalPlatformHandle(std::move(endpoint));
#if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_FUCHSIA))
if (transport_endpoint->type == MOJO_INVITATION_TRANSPORT_TYPE_CHANNEL_SERVER)
endpoint_handle.get().needs_connection = true;
......@@ -1412,15 +1416,17 @@ MojoResult Core::AcceptInvitation(
if (*invitation_handle == MOJO_HANDLE_INVALID)
return MOJO_RESULT_RESOURCE_EXHAUSTED;
ScopedInternalPlatformHandle endpoint_handle;
MojoResult result = MojoPlatformHandleToScopedInternalPlatformHandle(
&transport_endpoint->platform_handles[0], &endpoint_handle);
if (result != MOJO_RESULT_OK) {
auto endpoint = PlatformHandle::FromMojoPlatformHandle(
&transport_endpoint->platform_handles[0]);
if (!endpoint.is_valid()) {
Close(*invitation_handle);
*invitation_handle = MOJO_HANDLE_INVALID;
return MOJO_RESULT_INVALID_ARGUMENT;
}
auto endpoint_handle =
PlatformHandleToScopedInternalPlatformHandle(std::move(endpoint));
#if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_FUCHSIA))
if (transport_endpoint->type == MOJO_INVITATION_TRANSPORT_TYPE_CHANNEL_SERVER)
endpoint_handle.get().needs_connection = true;
......
......@@ -276,11 +276,10 @@ class MOJO_SYSTEM_IMPL_EXPORT Core {
// These methods correspond to the API functions defined in
// "mojo/public/c/system/platform_handle.h".
MojoResult WrapInternalPlatformHandle(
const MojoPlatformHandle* platform_handle,
const MojoWrapPlatformHandleOptions* options,
MojoHandle* mojo_handle);
MojoResult UnwrapInternalPlatformHandle(
MojoResult WrapPlatformHandle(const MojoPlatformHandle* platform_handle,
const MojoWrapPlatformHandleOptions* options,
MojoHandle* mojo_handle);
MojoResult UnwrapPlatformHandle(
MojoHandle mojo_handle,
const MojoUnwrapPlatformHandleOptions* options,
MojoPlatformHandle* platform_handle);
......
......@@ -14,10 +14,10 @@
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/system/core.h"
#include "mojo/edk/system/data_pipe_control_message.h"
#include "mojo/edk/system/node_controller.h"
#include "mojo/edk/system/platform_handle_utils.h"
#include "mojo/edk/system/request_context.h"
#include "mojo/edk/system/user_message_impl.h"
#include "mojo/public/c/system/data_pipe.h"
......@@ -320,13 +320,16 @@ bool DataPipeConsumerDispatcher::EndSerialize(
ports[0] = control_port_.name();
ScopedInternalPlatformHandle ignored_handle;
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
region_handle.PassPlatformHandle(), &platform_handles[0],
&ignored_handle);
if (!platform_handles[0].is_valid() || ignored_handle.is_valid())
PlatformHandle handle;
PlatformHandle ignored_handle;
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region_handle.PassPlatformHandle(), &handle, &ignored_handle);
if (!handle.is_valid() || ignored_handle.is_valid())
return false;
platform_handles[0] =
PlatformHandleToScopedInternalPlatformHandle(std::move(handle));
return true;
}
......@@ -379,11 +382,10 @@ DataPipeConsumerDispatcher::Deserialize(const void* data,
if (node_controller->node()->GetPort(ports[0], &port) != ports::OK)
return nullptr;
ScopedInternalPlatformHandle buffer_handle;
std::swap(buffer_handle, handles[0]);
auto region_handle =
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
std::move(buffer_handle), ScopedInternalPlatformHandle());
auto buffer_handle =
ScopedInternalPlatformHandleToPlatformHandle(std::move(handles[0]));
auto region_handle = CreateSharedMemoryRegionHandleFromPlatformHandles(
std::move(buffer_handle), PlatformHandle());
auto region = base::subtle::PlatformSharedMemoryRegion::Take(
std::move(region_handle),
base::subtle::PlatformSharedMemoryRegion::Mode::kUnsafe,
......
......@@ -12,11 +12,11 @@
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/system/configuration.h"
#include "mojo/edk/system/core.h"
#include "mojo/edk/system/data_pipe_control_message.h"
#include "mojo/edk/system/node_controller.h"
#include "mojo/edk/system/platform_handle_utils.h"
#include "mojo/edk/system/request_context.h"
#include "mojo/edk/system/user_message_impl.h"
#include "mojo/public/c/system/data_pipe.h"
......@@ -279,13 +279,15 @@ bool DataPipeProducerDispatcher::EndSerialize(
ports[0] = control_port_.name();
ScopedInternalPlatformHandle ignored_handle;
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
region_handle.PassPlatformHandle(), &platform_handles[0],
&ignored_handle);
if (!platform_handles[0].is_valid() || ignored_handle.is_valid())
PlatformHandle handle;
PlatformHandle ignored_handle;
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region_handle.PassPlatformHandle(), &handle, &ignored_handle);
if (!handle.is_valid() || ignored_handle.is_valid())
return false;
platform_handles[0] =
PlatformHandleToScopedInternalPlatformHandle(std::move(handle));
return true;
}
......@@ -340,11 +342,10 @@ DataPipeProducerDispatcher::Deserialize(const void* data,
if (node_controller->node()->GetPort(ports[0], &port) != ports::OK)
return nullptr;
ScopedInternalPlatformHandle buffer_handle;
std::swap(buffer_handle, handles[0]);
auto region_handle =
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
std::move(buffer_handle), ScopedInternalPlatformHandle());
auto buffer_handle =
ScopedInternalPlatformHandleToPlatformHandle(std::move(handles[0]));
auto region_handle = CreateSharedMemoryRegionHandleFromPlatformHandles(
std::move(buffer_handle), PlatformHandle());
auto region = base::subtle::PlatformSharedMemoryRegion::Take(
std::move(region_handle),
base::subtle::PlatformSharedMemoryRegion::Mode::kUnsafe,
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/entrypoints.h"
#include "mojo/edk/system/entrypoints.h"
#include <stdint.h>
......@@ -249,16 +249,14 @@ MojoResult MojoWrapPlatformHandleImpl(
const MojoPlatformHandle* platform_handle,
const MojoWrapPlatformHandleOptions* options,
MojoHandle* mojo_handle) {
return g_core->WrapInternalPlatformHandle(platform_handle, options,
mojo_handle);
return g_core->WrapPlatformHandle(platform_handle, options, mojo_handle);
}
MojoResult MojoUnwrapPlatformHandleImpl(
MojoHandle mojo_handle,
const MojoUnwrapPlatformHandleOptions* options,
MojoPlatformHandle* platform_handle) {
return g_core->UnwrapInternalPlatformHandle(mojo_handle, options,
platform_handle);
return g_core->UnwrapPlatformHandle(mojo_handle, options, platform_handle);
}
MojoResult MojoWrapPlatformSharedMemoryRegionImpl(
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_EDK_EMBEDDER_ENTRYPOINTS_H_
#define MOJO_EDK_EMBEDDER_ENTRYPOINTS_H_
#ifndef MOJO_EDK_SYSTEM_ENTRYPOINTS_H_
#define MOJO_EDK_SYSTEM_ENTRYPOINTS_H_
#include "mojo/edk/system/system_impl_export.h"
#include "mojo/public/c/system/thunks.h"
......@@ -22,4 +22,4 @@ MOJO_SYSTEM_IMPL_EXPORT const MojoSystemThunks& GetSystemThunks();
} // namespace edk
} // namespace mojo
#endif // MOJO_EDK_EMBEDDER_ENTRYPOINTS_H_
#endif // MOJO_EDK_SYSTEM_ENTRYPOINTS_H_
......@@ -144,7 +144,7 @@ TEST_F(InvitationTest, InvalidArguments) {
PlatformChannel channel;
MojoPlatformHandle endpoint_handle;
endpoint_handle.struct_size = sizeof(endpoint_handle);
PlatformHandleToMojoPlatformHandle(
PlatformHandle::ToMojoPlatformHandle(
channel.TakeLocalEndpoint().TakePlatformHandle(), &endpoint_handle);
ASSERT_NE(endpoint_handle.type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
......@@ -348,7 +348,7 @@ void InvitationTest::SendInvitationToClient(
uintptr_t error_handler_context,
base::StringPiece isolated_invitation_name) {
MojoPlatformHandle handle;
PlatformHandleToMojoPlatformHandle(std::move(endpoint_handle), &handle);
PlatformHandle::ToMojoPlatformHandle(std::move(endpoint_handle), &handle);
CHECK_NE(handle.type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
MojoHandle invitation;
......@@ -407,8 +407,8 @@ class TestClientBase : public InvitationTest {
}
}
MojoPlatformHandle endpoint_handle;
PlatformHandleToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
PlatformHandle::ToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
CHECK_NE(endpoint_handle.type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
MojoInvitationTransportEndpoint transport_endpoint;
......
......@@ -5,19 +5,18 @@
#include "mojo/edk/system/platform_handle_dispatcher.h"
#include "base/synchronization/lock.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/platform_handle_utils.h"
namespace mojo {
namespace edk {
// static
scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Create(
ScopedInternalPlatformHandle platform_handle) {
PlatformHandle platform_handle) {
return new PlatformHandleDispatcher(std::move(platform_handle));
}
ScopedInternalPlatformHandle
PlatformHandleDispatcher::PassInternalPlatformHandle() {
PlatformHandle PlatformHandleDispatcher::TakePlatformHandle() {
return std::move(platform_handle_);
}
......@@ -49,7 +48,8 @@ bool PlatformHandleDispatcher::EndSerialize(
base::AutoLock lock(lock_);
if (is_closed_)
return false;
handles[0] = ScopedInternalPlatformHandle(platform_handle_.get());
handles[0] =
PlatformHandleToScopedInternalPlatformHandle(std::move(platform_handle_));
return true;
}
......@@ -63,12 +63,8 @@ bool PlatformHandleDispatcher::BeginTransit() {
void PlatformHandleDispatcher::CompleteTransitAndClose() {
base::AutoLock lock(lock_);
in_transit_ = false;
is_closed_ = true;
// The system has taken ownership of our handle.
ignore_result(platform_handle_.release());
}
void PlatformHandleDispatcher::CancelTransit() {
......@@ -87,11 +83,12 @@ scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize(
if (num_bytes || num_ports || num_handles != 1)
return nullptr;
return PlatformHandleDispatcher::Create(std::move(handles[0]));
return PlatformHandleDispatcher::Create(
ScopedInternalPlatformHandleToPlatformHandle(std::move(handles[0])));
}
PlatformHandleDispatcher::PlatformHandleDispatcher(
ScopedInternalPlatformHandle platform_handle)
PlatformHandle platform_handle)
: platform_handle_(std::move(platform_handle)) {}
PlatformHandleDispatcher::~PlatformHandleDispatcher() {
......
......@@ -8,9 +8,9 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/dispatcher.h"
#include "mojo/edk/system/system_impl_export.h"
#include "mojo/public/cpp/platform/platform_handle.h"
namespace mojo {
namespace edk {
......@@ -18,9 +18,9 @@ namespace edk {
class MOJO_SYSTEM_IMPL_EXPORT PlatformHandleDispatcher : public Dispatcher {
public:
static scoped_refptr<PlatformHandleDispatcher> Create(
ScopedInternalPlatformHandle platform_handle);
PlatformHandle platform_handle);
ScopedInternalPlatformHandle PassInternalPlatformHandle();
PlatformHandle TakePlatformHandle();
// Dispatcher:
Type GetType() const override;
......@@ -44,13 +44,13 @@ class MOJO_SYSTEM_IMPL_EXPORT PlatformHandleDispatcher : public Dispatcher {
size_t num_handles);
private:
PlatformHandleDispatcher(ScopedInternalPlatformHandle platform_handle);
PlatformHandleDispatcher(PlatformHandle platform_handle);
~PlatformHandleDispatcher() override;
base::Lock lock_;
bool in_transit_ = false;
bool is_closed_ = false;
ScopedInternalPlatformHandle platform_handle_;
PlatformHandle platform_handle_;
DISALLOW_COPY_AND_ASSIGN(PlatformHandleDispatcher);
};
......
......@@ -12,8 +12,8 @@
#include "base/files/scoped_file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/platform_handle_utils.h"
#include "mojo/edk/test/test_utils.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -22,29 +22,6 @@ namespace mojo {
namespace edk {
namespace {
ScopedInternalPlatformHandle PlatformHandleToScopedInternalPlatformHandle(
PlatformHandle platform_handle) {
MojoPlatformHandle handle;
PlatformHandleToMojoPlatformHandle(std::move(platform_handle), &handle);
ScopedInternalPlatformHandle internal_handle;
MojoResult result = MojoPlatformHandleToScopedInternalPlatformHandle(
&handle, &internal_handle);
if (result != MOJO_RESULT_OK)
return ScopedInternalPlatformHandle();
return internal_handle;
}
PlatformHandle ScopedInternalPlatformHandleToPlatformHandle(
ScopedInternalPlatformHandle internal_handle) {
MojoPlatformHandle handle;
handle.struct_size = sizeof(handle);
MojoResult result = ScopedInternalPlatformHandleToMojoPlatformHandle(
std::move(internal_handle), &handle);
if (result != MOJO_RESULT_OK)
return PlatformHandle();
return MojoPlatformHandleToPlatformHandle(&handle);
}
TEST(PlatformHandleDispatcherTest, Basic) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
......@@ -63,13 +40,11 @@ TEST(PlatformHandleDispatcherTest, Basic) {
ASSERT_TRUE(h.is_valid());
scoped_refptr<PlatformHandleDispatcher> dispatcher =
PlatformHandleDispatcher::Create(
PlatformHandleToScopedInternalPlatformHandle(std::move(h)));
PlatformHandleDispatcher::Create(std::move(h));
EXPECT_FALSE(h.is_valid());
EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, dispatcher->GetType());
h = ScopedInternalPlatformHandleToPlatformHandle(
dispatcher->PassInternalPlatformHandle());
h = dispatcher->TakePlatformHandle();
EXPECT_TRUE(h.is_valid());
fp = test::FILEFromPlatformHandle(std::move(h), "rb");
......@@ -83,7 +58,7 @@ TEST(PlatformHandleDispatcherTest, Basic) {
EXPECT_STREQ(kHelloWorld, read_buffer);
// Try getting the handle again. (It should fail cleanly.)
auto internal_handle = dispatcher->PassInternalPlatformHandle();
auto internal_handle = dispatcher->TakePlatformHandle();
EXPECT_FALSE(internal_handle.is_valid());
EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
......@@ -102,8 +77,7 @@ TEST(PlatformHandleDispatcherTest, Serialization) {
scoped_refptr<PlatformHandleDispatcher> dispatcher =
PlatformHandleDispatcher::Create(
PlatformHandleToScopedInternalPlatformHandle(
test::PlatformHandleFromFILE(std::move(fp))));
test::PlatformHandleFromFILE(std::move(fp)));
uint32_t num_bytes = 0;
uint32_t num_ports = 0;
......@@ -122,8 +96,7 @@ TEST(PlatformHandleDispatcherTest, Serialization) {
EXPECT_TRUE(received_handle.is_valid());
ScopedInternalPlatformHandle handle =
dispatcher->PassInternalPlatformHandle();
PlatformHandle handle = dispatcher->TakePlatformHandle();
EXPECT_FALSE(handle.is_valid());
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dispatcher->Close());
......@@ -137,10 +110,7 @@ TEST(PlatformHandleDispatcherTest, Serialization) {
EXPECT_FALSE(received_handle.is_valid());
EXPECT_TRUE(dispatcher->GetType() == Dispatcher::Type::PLATFORM_HANDLE);
fp = test::FILEFromPlatformHandle(
ScopedInternalPlatformHandleToPlatformHandle(
dispatcher->PassInternalPlatformHandle()),
"rb");
fp = test::FILEFromPlatformHandle(dispatcher->TakePlatformHandle(), "rb");
EXPECT_TRUE(fp);
rewind(fp.get());
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/system/platform_handle_utils.h"
#include "build/build_config.h"
#if defined(OS_FUCHSIA)
#include "base/fuchsia/scoped_zx_handle.h"
#elif defined(OS_POSIX)
#include "base/files/scoped_file.h"
#elif defined(OS_WIN)
#include "base/win/scoped_handle.h"
#endif
#if defined(OS_MACOSX) && !defined(OS_IOS)
#include "base/mac/scoped_mach_port.h"
#endif
namespace mojo {
namespace edk {
PlatformHandle ScopedInternalPlatformHandleToPlatformHandle(
ScopedInternalPlatformHandle handle) {
#if defined(OS_FUCHSIA)
if (handle.get().is_valid_fd())
return PlatformHandle(base::ScopedFD(handle.release().as_fd()));
else
return PlatformHandle(base::ScopedZxHandle(handle.release().as_handle()));
#elif defined(OS_POSIX)
if (handle.get().type == InternalPlatformHandle::Type::POSIX) {
return PlatformHandle(base::ScopedFD(handle.release().handle));
}
#elif defined(OS_WIN)
return PlatformHandle(base::win::ScopedHandle(handle.release().handle));
#endif
#if defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.get().type == InternalPlatformHandle::Type::MACH) {
return PlatformHandle(
base::mac::ScopedMachSendRight(handle.release().port));
}
#endif
return PlatformHandle();
}
ScopedInternalPlatformHandle PlatformHandleToScopedInternalPlatformHandle(
PlatformHandle handle) {
#if defined(OS_FUCHSIA)
if (handle.is_fd()) {
return ScopedInternalPlatformHandle(
InternalPlatformHandle::ForFd(handle.ReleaseFD()));
} else if (handle.is_handle()) {
return ScopedInternalPlatformHandle(
InternalPlatformHandle::ForHandle(handle.ReleaseHandle()));
}
#elif defined(OS_POSIX)
if (handle.is_fd()) {
return ScopedInternalPlatformHandle(
InternalPlatformHandle(handle.ReleaseFD()));
}
#elif defined(OS_WIN)
if (handle.is_handle()) {
return ScopedInternalPlatformHandle(
InternalPlatformHandle(handle.ReleaseHandle()));
}
#endif
#if defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.is_mach_port()) {
return ScopedInternalPlatformHandle(
InternalPlatformHandle(handle.ReleaseMachPort()));
}
#endif
return ScopedInternalPlatformHandle();
}
void ExtractPlatformHandlesFromSharedMemoryRegionHandle(
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle handle,
PlatformHandle* extracted_handle,
PlatformHandle* extracted_readonly_handle) {
#if defined(OS_WIN)
*extracted_handle = PlatformHandle(base::win::ScopedHandle(handle.Take()));
#elif defined(OS_FUCHSIA)
*extracted_handle = PlatformHandle(std::move(handle));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
// This is a Mach port. Same code as above and below, but separated for
// clarity.
*extracted_handle = PlatformHandle(std::move(handle));
#elif defined(OS_ANDROID)
// This is a file descriptor. Same code as above, but separated for clarity.
*extracted_handle = PlatformHandle(std::move(handle));
#else
*extracted_handle = PlatformHandle(std::move(handle.fd));
*extracted_readonly_handle = PlatformHandle(std::move(handle.readonly_fd));
#endif
}
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
CreateSharedMemoryRegionHandleFromPlatformHandles(
PlatformHandle handle,
PlatformHandle readonly_handle) {
#if defined(OS_WIN)
DCHECK(!readonly_handle.is_valid());
return handle.TakeHandle();
#elif defined(OS_FUCHSIA)
DCHECK(!readonly_handle.is_valid());
return handle.TakeHandle();
#elif defined(OS_MACOSX) && !defined(OS_IOS)
DCHECK(!readonly_handle.is_valid());
return handle.TakeMachPort();
#elif defined(OS_ANDROID)
DCHECK(!readonly_handle.is_valid());
return handle.TakeFD();
#else
return base::subtle::ScopedFDPair(handle.TakeFD(), readonly_handle.TakeFD());
#endif
}
} // namespace edk
} // namespace mojo
......@@ -2,11 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_UTILS_H_
#define MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_UTILS_H_
#ifndef MOJO_EDK_SYSTEM_PLATFORM_HANDLE_UTILS_H_
#define MOJO_EDK_SYSTEM_PLATFORM_HANDLE_UTILS_H_
#include "base/memory/platform_shared_memory_region.h"
#include "mojo/edk/embedder/platform_handle.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/edk/system/system_impl_export.h"
#include "mojo/public/c/system/platform_handle.h"
......@@ -16,48 +15,29 @@
namespace mojo {
namespace edk {
// Closes all the |InternalPlatformHandle|s in the given container.
template <typename InternalPlatformHandleContainer>
MOJO_SYSTEM_IMPL_EXPORT inline void CloseAllInternalPlatformHandles(
InternalPlatformHandleContainer* platform_handles) {
for (typename InternalPlatformHandleContainer::iterator it =
platform_handles->begin();
it != platform_handles->end(); ++it)
it->CloseIfNecessary();
}
MOJO_SYSTEM_IMPL_EXPORT MojoResult
MojoPlatformHandleToScopedInternalPlatformHandle(
const MojoPlatformHandle* platform_handle,
ScopedInternalPlatformHandle* out_handle);
MOJO_SYSTEM_IMPL_EXPORT MojoResult
ScopedInternalPlatformHandleToMojoPlatformHandle(
ScopedInternalPlatformHandle handle,
MojoPlatformHandle* platform_handle);
// Duplicates the given |InternalPlatformHandle| (which must be valid). (Returns
// an invalid |ScopedInternalPlatformHandle| on failure.)
// Conversion between an internal handle type and the public PlatformHandle.
MOJO_SYSTEM_IMPL_EXPORT PlatformHandle
ScopedInternalPlatformHandleToPlatformHandle(
ScopedInternalPlatformHandle handle);
MOJO_SYSTEM_IMPL_EXPORT ScopedInternalPlatformHandle
DuplicatePlatformHandle(InternalPlatformHandle platform_handle);
PlatformHandleToScopedInternalPlatformHandle(PlatformHandle handle);
// Converts a base shared memory platform handle into one (maybe two on POSIX)
// EDK ScopedInternalPlatformHandles.
MOJO_SYSTEM_IMPL_EXPORT void
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
// PlatformHandle(s).
MOJO_SYSTEM_IMPL_EXPORT void ExtractPlatformHandlesFromSharedMemoryRegionHandle(
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle handle,
ScopedInternalPlatformHandle* extracted_handle,
ScopedInternalPlatformHandle* extracted_readonly_handle);
PlatformHandle* extracted_handle,
PlatformHandle* extracted_readonly_handle);
// Converts one (maybe two on POSIX) EDK ScopedInternalPlatformHandles to a base
// shared memory platform handle.
// Converts one (maybe two on POSIX) PlatformHandle(s) to a base shared memory
// platform handle.
MOJO_SYSTEM_IMPL_EXPORT
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
ScopedInternalPlatformHandle handle,
ScopedInternalPlatformHandle readonly_handle);
CreateSharedMemoryRegionHandleFromPlatformHandles(
PlatformHandle handle,
PlatformHandle readonly_handle);
} // namespace edk
} // namespace mojo
#endif // MOJO_EDK_EMBEDDER_PLATFORM_HANDLE_UTILS_H_
#endif // MOJO_EDK_SYSTEM_PLATFORM_HANDLE_UTILS_H_
......@@ -14,10 +14,10 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/platform_handle_utils.h"
#include "mojo/edk/system/configuration.h"
#include "mojo/edk/system/node_controller.h"
#include "mojo/edk/system/options_validation.h"
#include "mojo/edk/system/platform_handle_utils.h"
#include "mojo/edk/system/platform_shared_memory_mapping.h"
#include "mojo/public/c/system/platform_handle.h"
......@@ -140,14 +140,15 @@ scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize(
if (num_ports)
return nullptr;
ScopedInternalPlatformHandle handles[2];
PlatformHandle handles[2];
#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_FUCHSIA) && \
(!defined(OS_MACOSX) || defined(OS_IOS))
if (serialized_state->access_mode ==
MOJO_PLATFORM_SHARED_MEMORY_REGION_ACCESS_MODE_WRITABLE) {
if (num_platform_handles != 2)
return nullptr;
handles[1] = std::move(platform_handles[1]);
handles[1] = ScopedInternalPlatformHandleToPlatformHandle(
std::move(platform_handles[1]));
} else {
if (num_platform_handles != 1)
return nullptr;
......@@ -156,7 +157,8 @@ scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize(
if (num_platform_handles != 1)
return nullptr;
#endif
handles[0] = std::move(platform_handles[0]);
handles[0] = ScopedInternalPlatformHandleToPlatformHandle(
std::move(platform_handles[0]));
base::UnguessableToken guid = base::UnguessableToken::Deserialize(
serialized_state->guid_high, serialized_state->guid_low);
......@@ -176,9 +178,10 @@ scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize(
LOG(ERROR) << "Invalid serialized shared buffer access mode.";
return nullptr;
}
auto region = base::subtle::PlatformSharedMemoryRegion::Take(
CreateSharedMemoryRegionHandleFromInternalPlatformHandles(
std::move(handles[0]), std::move(handles[1])),
CreateSharedMemoryRegionHandleFromPlatformHandles(std::move(handles[0]),
std::move(handles[1])),
mode, static_cast<size_t>(serialized_state->num_bytes), guid);
if (!region.IsValid()) {
LOG(ERROR)
......@@ -356,15 +359,24 @@ bool SharedBufferDispatcher::EndSerialize(
(!defined(OS_MACOSX) || defined(OS_IOS))
if (region.GetMode() ==
base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &handles[0], &handles[1]);
PlatformHandle platform_handles[2];
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &platform_handles[0],
&platform_handles[1]);
handles[0] = PlatformHandleToScopedInternalPlatformHandle(
std::move(platform_handles[0]));
handles[1] = PlatformHandleToScopedInternalPlatformHandle(
std::move(platform_handles[1]));
return true;
}
#endif
ScopedInternalPlatformHandle ignored_handle;
ExtractInternalPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &handles[0], &ignored_handle);
PlatformHandle platform_handle;
PlatformHandle ignored_handle;
ExtractPlatformHandlesFromSharedMemoryRegionHandle(
region.PassPlatformHandle(), &platform_handle, &ignored_handle);
handles[0] =
PlatformHandleToScopedInternalPlatformHandle(std::move(platform_handle));
return true;
}
......
......@@ -31,6 +31,7 @@ component("platform") {
public_deps = [
"//base",
"//mojo/public/c/system:headers",
]
if (is_posix && (!is_nacl && !is_fuchsia)) {
......
......@@ -2,6 +2,7 @@ include_rules = [
# Mojo platform support must not depend on any other part of the Mojo public
# library.
"-mojo",
"+mojo/public/c/system",
"+mojo/public/cpp/platform",
# For some syscalls when building in NaCl toolchains.
......
......@@ -9,19 +9,26 @@
#if defined(OS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#elif defined(OS_FUCHSIA)
#include <lib/fdio/limits.h>
#include <unistd.h>
#include <zircon/status.h>
#include <zircon/syscalls.h>
#include "base/fuchsia/scoped_zx_handle.h"
#elif defined(OS_MACOSX) && !defined(OS_IOS)
#include <mach/mach_vm.h>
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h"
#endif
#if defined(OS_POSIX)
#include <unistd.h>
#include "base/files/scoped_file.h"
#endif
namespace mojo {
......@@ -81,17 +88,18 @@ PlatformHandle::PlatformHandle(PlatformHandle&& other) = default;
#if defined(OS_WIN)
PlatformHandle::PlatformHandle(base::win::ScopedHandle handle)
: handle_(std::move(handle)) {}
: type_(Type::kHandle), handle_(std::move(handle)) {}
#elif defined(OS_FUCHSIA)
PlatformHandle::PlatformHandle(base::ScopedZxHandle handle)
: handle_(std::move(handle)) {}
: type_(Type::kHandle), handle_(std::move(handle)) {}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
PlatformHandle::PlatformHandle(base::mac::ScopedMachSendRight mach_port)
: mach_port_(std::move(mach_port)) {}
: type_(Type::kMachPort), mach_port_(std::move(mach_port)) {}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
PlatformHandle::PlatformHandle(base::ScopedFD fd) : fd_(std::move(fd)) {
PlatformHandle::PlatformHandle(base::ScopedFD fd)
: type_(Type::kFd), fd_(std::move(fd)) {
#if defined(OS_FUCHSIA)
DCHECK_LT(fd_.get(), FDIO_MAX_FD);
#endif
......@@ -102,7 +110,82 @@ PlatformHandle::~PlatformHandle() = default;
PlatformHandle& PlatformHandle::operator=(PlatformHandle&& other) = default;
// static
void PlatformHandle::ToMojoPlatformHandle(PlatformHandle handle,
MojoPlatformHandle* out_handle) {
DCHECK(out_handle);
out_handle->struct_size = sizeof(MojoPlatformHandle);
if (handle.type_ == Type::kNone) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_INVALID;
out_handle->value = 0;
return;
}
do {
#if defined(OS_WIN)
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
out_handle->value =
static_cast<uint64_t>(HandleToLong(handle.TakeHandle().Take()));
break;
#elif defined(OS_FUCHSIA)
if (handle.is_handle()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE;
out_handle->value = handle.TakeHandle().release();
break;
}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.is_mach_port()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
out_handle->value =
static_cast<uint64_t>(handle.TakeMachPort().release());
break;
}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
DCHECK(handle.is_fd());
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
out_handle->value = static_cast<uint64_t>(handle.TakeFD().release());
#endif
} while (false);
// One of the above cases must take ownership of |handle|.
DCHECK(!handle.is_valid());
}
// static
PlatformHandle PlatformHandle::FromMojoPlatformHandle(
const MojoPlatformHandle* handle) {
if (handle->struct_size < sizeof(*handle) ||
handle->type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) {
return PlatformHandle();
}
#if defined(OS_WIN)
if (handle->type != MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE)
return PlatformHandle();
return PlatformHandle(
base::win::ScopedHandle(LongToHandle(static_cast<long>(handle->value))));
#elif defined(OS_FUCHSIA)
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE)
return PlatformHandle(base::ScopedZxHandle(handle->value));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT) {
return PlatformHandle(base::mac::ScopedMachSendRight(
static_cast<mach_port_t>(handle->value)));
}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
if (handle->type != MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR)
return PlatformHandle();
return PlatformHandle(base::ScopedFD(static_cast<int>(handle->value)));
#endif
}
void PlatformHandle::reset() {
type_ = Type::kNone;
#if defined(OS_WIN)
handle_.Close();
#elif defined(OS_FUCHSIA)
......
......@@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "mojo/public/c/system/platform_handle.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
......@@ -40,6 +41,18 @@ namespace mojo {
// NOTE: This assumes ownership if the handle it represents.
class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
public:
enum class Type {
kNone,
#if defined(OS_WIN) || defined(OS_FUCHSIA)
kHandle,
#elif defined(OS_MACOSX) && !defined(OS_IOS)
kMachPort,
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
kFd,
#endif
};
PlatformHandle();
PlatformHandle(PlatformHandle&& other);
......@@ -59,7 +72,20 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
PlatformHandle& operator=(PlatformHandle&& other);
// Takes ownership of |handle|'s underlying platform handle and fills in
// |mojo_handle| with a representation of it. The caller assumes ownership of
// the platform handle.
static void ToMojoPlatformHandle(PlatformHandle handle,
MojoPlatformHandle* mojo_handle);
// Closes the underlying platform handle.
// Assumes ownership of the platform handle described by |handle|, and returns
// it as a new PlatformHandle.
static PlatformHandle FromMojoPlatformHandle(
const MojoPlatformHandle* handle);
Type type() const { return type_; }
void reset();
// Duplicates the underlying platform handle, returning a new PlatformHandle
......@@ -67,21 +93,23 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
PlatformHandle Clone() const;
#if defined(OS_WIN)
bool is_valid() const { return handle_.IsValid(); }
bool is_valid() const { return is_valid_handle(); }
bool is_valid_handle() const { return handle_.IsValid(); }
bool is_handle() const { return type_ == Type::kHandle; }
const base::win::ScopedHandle& GetHandle() const { return handle_; }
base::win::ScopedHandle TakeHandle() { return std::move(handle_); }
HANDLE ReleaseHandle() WARN_UNUSED_RESULT { return handle_.Take(); }
#elif defined(OS_FUCHSIA)
bool is_valid() const { return is_valid_fd() || is_valid_handle(); }
bool is_valid_handle() const { return handle_.is_valid(); }
bool is_handle() const { return type_ == Type::kHandle; }
const base::ScopedZxHandle& GetHandle() const { return handle_; }
base::ScopedZxHandle TakeHandle() { return std::move(handle_); }
zx_handle_t ReleaseHandle() WARN_UNUSED_RESULT { return handle_.release(); }
#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_;
}
......@@ -99,12 +127,15 @@ class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
bool is_valid_fd() const { return fd_.is_valid(); }
bool is_fd() const { return type_ == Type::kFd; }
const base::ScopedFD& GetFD() const { return fd_; }
base::ScopedFD TakeFD() { return std::move(fd_); }
int ReleaseFD() WARN_UNUSED_RESULT { return fd_.release(); }
#endif
private:
Type type_ = Type::kNone;
#if defined(OS_WIN)
base::win::ScopedHandle handle_;
#elif defined(OS_FUCHSIA)
......
......@@ -237,9 +237,9 @@ TEST_P(PlatformHandleTest, CStructConversion) {
EXPECT_EQ(kTestData, GetObjectContents(test_handle()));
MojoPlatformHandle c_handle;
PlatformHandleToMojoPlatformHandle(std::move(test_handle()), &c_handle);
PlatformHandle::ToMojoPlatformHandle(std::move(test_handle()), &c_handle);
PlatformHandle handle = MojoPlatformHandleToPlatformHandle(&c_handle);
PlatformHandle handle = PlatformHandle::FromMojoPlatformHandle(&c_handle);
EXPECT_EQ(kTestData, GetObjectContents(handle));
}
......
......@@ -31,8 +31,8 @@ void PlatformHandleToTransportEndpoint(
PlatformHandle platform_handle,
MojoPlatformHandle* endpoint_handle,
MojoInvitationTransportEndpoint* endpoint) {
PlatformHandleToMojoPlatformHandle(std::move(platform_handle),
endpoint_handle);
PlatformHandle::ToMojoPlatformHandle(std::move(platform_handle),
endpoint_handle);
CHECK_NE(endpoint_handle->type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
endpoint->struct_size = sizeof(*endpoint);
......@@ -216,8 +216,8 @@ IncomingInvitation& IncomingInvitation::operator=(IncomingInvitation&& other) =
IncomingInvitation IncomingInvitation::Accept(
PlatformChannelEndpoint channel_endpoint) {
MojoPlatformHandle endpoint_handle;
PlatformHandleToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
PlatformHandle::ToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
CHECK_NE(endpoint_handle.type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
MojoInvitationTransportEndpoint transport_endpoint;
......@@ -240,8 +240,8 @@ IncomingInvitation IncomingInvitation::Accept(
ScopedMessagePipeHandle IncomingInvitation::AcceptIsolated(
PlatformChannelEndpoint channel_endpoint) {
MojoPlatformHandle endpoint_handle;
PlatformHandleToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
PlatformHandle::ToMojoPlatformHandle(channel_endpoint.TakePlatformHandle(),
&endpoint_handle);
CHECK_NE(endpoint_handle.type, MOJO_PLATFORM_HANDLE_TYPE_INVALID);
MojoInvitationTransportEndpoint transport_endpoint;
......
......@@ -181,7 +181,7 @@ base::subtle::PlatformSharedMemoryRegion UnwrapPlatformSharedMemoryRegion(
ScopedHandle WrapPlatformHandle(PlatformHandle handle) {
MojoPlatformHandle platform_handle;
PlatformHandleToMojoPlatformHandle(std::move(handle), &platform_handle);
PlatformHandle::ToMojoPlatformHandle(std::move(handle), &platform_handle);
MojoHandle wrapped_handle;
MojoResult result =
......@@ -198,78 +198,7 @@ PlatformHandle UnwrapPlatformHandle(ScopedHandle handle) {
nullptr, &platform_handle);
if (result != MOJO_RESULT_OK)
return PlatformHandle();
return MojoPlatformHandleToPlatformHandle(&platform_handle);
}
void PlatformHandleToMojoPlatformHandle(PlatformHandle handle,
MojoPlatformHandle* out_handle) {
DCHECK(out_handle);
out_handle->struct_size = sizeof(MojoPlatformHandle);
if (!handle.is_valid()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_INVALID;
out_handle->value = 0;
return;
}
do {
#if defined(OS_WIN)
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
out_handle->value =
static_cast<uint64_t>(HandleToLong(handle.TakeHandle().Take()));
break;
#elif defined(OS_FUCHSIA)
if (handle.is_valid_handle()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE;
out_handle->value = handle.TakeHandle().release();
break;
}
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.is_valid_mach_port()) {
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
out_handle->value =
static_cast<uint64_t>(handle.TakeMachPort().release());
break;
}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
DCHECK(handle.is_valid_fd());
out_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
out_handle->value = static_cast<uint64_t>(handle.TakeFD().release());
#endif
} while (false);
// One of the above cases must take ownership of |handle|.
DCHECK(!handle.is_valid());
}
PlatformHandle MojoPlatformHandleToPlatformHandle(
const MojoPlatformHandle* handle) {
if (handle->struct_size < sizeof(*handle) ||
handle->type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) {
return PlatformHandle();
}
#if defined(OS_WIN)
if (handle->type != MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE)
return PlatformHandle();
return PlatformHandle(
base::win::ScopedHandle(LongToHandle(static_cast<long>(handle->value))));
#elif defined(OS_FUCHSIA)
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_FUCHSIA_HANDLE)
return PlatformHandle(base::ScopedZxHandle(handle->value));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (handle->type == MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT) {
return PlatformHandle(base::mac::ScopedMachSendRight(
static_cast<mach_port_t>(handle->value)));
}
#endif
#if defined(OS_POSIX) || defined(OS_FUCHSIA)
if (handle->type != MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR)
return PlatformHandle();
return PlatformHandle(base::ScopedFD(static_cast<int>(handle->value)));
#endif
return PlatformHandle::FromMojoPlatformHandle(&platform_handle);
}
// Wraps a PlatformFile as a Mojo handle. Takes ownership of the file object.
......
......@@ -76,19 +76,6 @@ enum class UnwrappedSharedMemoryHandleProtection {
kReadOnly,
};
// Converts a PlatformHandle to the MojoPlatformHandle C struct for use with
// Mojo APIs. Note that although MojoPlatformHandle has weak ownership, this
// relinquishes ownership from |handle|.
void MOJO_CPP_SYSTEM_EXPORT
PlatformHandleToMojoPlatformHandle(PlatformHandle handle,
MojoPlatformHandle* out_handle);
// Converts a MojoPlatformHandle C struct to a PlatformHandle for use with
// various C++ APIs. Note that although MojoPlatformHandle has weak ownership,
// the new handle assumes ownership of the represented platform handle.
PlatformHandle MOJO_CPP_SYSTEM_EXPORT
MojoPlatformHandleToPlatformHandle(const MojoPlatformHandle* handle);
// Wraps a PlatformHandle from the C++ platform support library as a Mojo
// handle.
MOJO_CPP_SYSTEM_EXPORT ScopedHandle WrapPlatformHandle(PlatformHandle handle);
......
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