Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Unified Diff: mojo/public/cpp/system/platform_handle.cc

Issue 1995753002: [mojo-edk] Expose portable API for platform handle wrapping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/cpp/system/platform_handle.h ('k') | mojo/public/platform/native/system_thunks.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/system/platform_handle.cc
diff --git a/mojo/public/cpp/system/platform_handle.cc b/mojo/public/cpp/system/platform_handle.cc
new file mode 100644
index 0000000000000000000000000000000000000000..840c1126053fa9aa246a7d5b35f16d0f7dcf1808
--- /dev/null
+++ b/mojo/public/cpp/system/platform_handle.cc
@@ -0,0 +1,129 @@
+// Copyright 2016 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/public/cpp/system/platform_handle.h"
+
+namespace mojo {
+
+namespace {
+
+uint64_t PlatformHandleValueFromPlatformFile(base::PlatformFile file) {
+#if defined(OS_WIN)
+ return reinterpret_cast<uint64_t>(file);
+#else
+ return static_cast<uint64_t>(file);
+#endif
+}
+
+base::PlatformFile PlatformFileFromPlatformHandleValue(uint64_t value) {
+#if defined(OS_WIN)
+ return reinterpret_cast<base::PlatformFile>(value);
+#else
+ return static_cast<base::PlatformFile>(value);
+#endif
+}
+
+} // namespace
+
+ScopedHandle WrapPlatformFile(base::PlatformFile platform_file) {
+ MojoPlatformHandle platform_handle;
+ platform_handle.struct_size = sizeof(MojoPlatformHandle);
+ platform_handle.type = kPlatformFileHandleType;
+ platform_handle.value = PlatformHandleValueFromPlatformFile(platform_file);
+
+ MojoHandle mojo_handle;
+ MojoResult result = MojoWrapPlatformHandle(&platform_handle, &mojo_handle);
+ CHECK_EQ(result, MOJO_RESULT_OK);
+
+ return ScopedHandle(Handle(mojo_handle));
+}
+
+MojoResult UnwrapPlatformFile(ScopedHandle handle, base::PlatformFile* file) {
+ MojoPlatformHandle platform_handle;
+ platform_handle.struct_size = sizeof(MojoPlatformHandle);
+ MojoResult result = MojoUnwrapPlatformHandle(handle.release().value(),
+ &platform_handle);
+ if (result != MOJO_RESULT_OK)
+ return result;
+
+ if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) {
+ *file = kInvalidPlatformFile;
+ } else {
+ CHECK_EQ(platform_handle.type, kPlatformFileHandleType);
+ *file = PlatformFileFromPlatformHandleValue(platform_handle.value);
+ }
+
+ return MOJO_RESULT_OK;
+}
+
+ScopedSharedBufferHandle WrapSharedMemoryHandle(
+ const base::SharedMemoryHandle& memory_handle,
+ size_t size,
+ bool read_only) {
+ MojoPlatformHandle platform_handle;
+ platform_handle.struct_size = sizeof(MojoPlatformHandle);
+ platform_handle.type = kPlatformSharedBufferHandleType;
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+ platform_handle.value =
+ static_cast<uint64_t>(memory_handle.GetMemoryObject());
+#elif defined(OS_POSIX)
+ platform_handle.value = PlatformHandleValueFromPlatformFile(memory_handle.fd);
+#elif defined(OS_WIN)
+ platform_handle.value =
+ PlatformHandleValueFromPlatformFile(memory_handle.GetHandle());
+#endif
+
+ MojoPlatformSharedBufferHandleFlags flags =
+ MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE;
+ if (read_only)
+ flags |= MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY;
+
+ MojoHandle mojo_handle;
+ MojoResult result = MojoWrapPlatformSharedBufferHandle(
+ &platform_handle, size, flags, &mojo_handle);
+ CHECK_EQ(result, MOJO_RESULT_OK);
+
+ return ScopedSharedBufferHandle(SharedBufferHandle(mojo_handle));
+}
+
+MojoResult UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
+ base::SharedMemoryHandle* memory_handle,
+ size_t* size,
+ bool* read_only) {
+ MojoPlatformHandle platform_handle;
+ platform_handle.struct_size = sizeof(MojoPlatformHandle);
+
+ MojoPlatformSharedBufferHandleFlags flags;
+ size_t num_bytes;
+ MojoResult result = MojoUnwrapPlatformSharedBufferHandle(
+ handle.release().value(), &platform_handle, &num_bytes, &flags);
+ if (result != MOJO_RESULT_OK)
+ return result;
+
+ if (size)
+ *size = num_bytes;
+
+ if (read_only)
+ *read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY;
+
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+ CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT);
+ *memory_handle = base::SharedMemoryHandle(
+ static_cast<mach_port_t>(platform_handle.value), num_bytes,
+ base::GetCurrentProcId());
+#elif defined(OS_POSIX)
+ CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR);
+ *memory_handle = base::SharedMemoryHandle(
+ static_cast<int>(platform_handle.value), false);
+#elif defined(OS_WIN)
+ CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE);
+ *memory_handle = base::SharedMemoryHandle(
+ reinterpret_cast<HANDLE>(platform_handle.value),
+ base::GetCurrentProcId());
+#endif
+
+ return MOJO_RESULT_OK;
+}
+
+} // namespace mojo
« no previous file with comments | « mojo/public/cpp/system/platform_handle.h ('k') | mojo/public/platform/native/system_thunks.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698