Chromium Code Reviews| Index: mojo/public/cpp/system/platform_handle.h |
| diff --git a/mojo/public/cpp/system/platform_handle.h b/mojo/public/cpp/system/platform_handle.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..602f3d8030d0e57e40882e6a4127da4ab41120b2 |
| --- /dev/null |
| +++ b/mojo/public/cpp/system/platform_handle.h |
| @@ -0,0 +1,189 @@ |
| +// 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. |
| + |
| +// This file provides a C++ wrapping around the Mojo C API for platform handles, |
| +// replacing the prefix of "Mojo" with a "mojo" namespace. |
| +// |
| +// Please see "mojo/public/c/system/buffer.h" for complete documentation of the |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:53
s/buffer/platform_handle
Ken Rockot(use gerrit already)
2016/05/23 17:17:54
done
|
| +// API. |
| + |
| +#ifndef MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_ |
| +#define MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/files/file.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/memory/shared_memory_handle.h" |
| +#include "base/process/process_handle.h" |
| +#include "mojo/public/c/system/platform_handle.h" |
| +#include "mojo/public/cpp/system/buffer.h" |
| +#include "mojo/public/cpp/system/handle.h" |
| + |
| +#if defined(OS_WIN) |
| +#include <windows.h> |
| +#endif |
| + |
| +namespace mojo { |
| + |
| +#if defined(OS_POSIX) |
| + |
| +const MojoPlatformHandleType kPlatformFileHandleType = |
| + MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR; |
| +const base::PlatformFile kInvalidPlatformFile = -1; |
| + |
| +#if defined(OS_MACOSX) && !defined(OS_IOS) |
| +const MojoPlatformHandleType kPlatformSharedBufferHandleType = |
| + MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT; |
| +#else |
| +const MojoPlatformHandleType kPlatformSharedBufferHandleType = |
| + MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR; |
| +#endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| + |
| +#elif defined(OS_WIN) |
| +const MojoPlatformHandleType kPlatformFileHandleType = |
| + MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE; |
| +const base::PlatformFile kInvalidPlatformFile = INVALID_HANDLE_VALUE; |
| + |
| +const MojoPlatformHandleType kPlatformSharedBufferHandleType = |
| + MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE; |
| +#else |
| +#error Unsupported platform. |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:53
Hmmm. We don't support platforms that are non-posi
Ken Rockot(use gerrit already)
2016/05/23 17:17:54
Fair point - removed all these
|
| +#endif |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:53
#endif // defined(OS_POSIX)
Ken Rockot(use gerrit already)
2016/05/23 17:17:54
done
|
| + |
| +inline uint64_t PlatformHandleValueFromPlatformFile(base::PlatformFile file) { |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:53
Shouldn't these two be in an anonymous or internal
Ken Rockot(use gerrit already)
2016/05/23 17:17:54
Done
|
| +#if defined(OS_WIN) |
| + return reinterpret_cast<uint64_t>(file); |
| +#else |
| + return static_cast<uint64_t>(file); |
| +#endif |
| +} |
| + |
| +inline base::PlatformFile PlatformFileFromPlatformHandleValue(uint64_t value) { |
| +#if defined(OS_WIN) |
| + return reinterpret_cast<base::PlatformFile>(value); |
| +#else |
| + return static_cast<base::PlatformFile>(value); |
| +#endif |
| +} |
| + |
| +// Wraps a PlatformFile as a Mojo handle. Takes ownership of the file object. |
| +inline ScopedHandle WrapPlatformFile(base::PlatformFile platform_file) { |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:53
These implementations are non-trivial and not perf
Ken Rockot(use gerrit already)
2016/05/23 17:17:54
Done
|
| + 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)); |
| +} |
| + |
| +// Unwraps a PlatformFile from a Mojo handle. |
| +inline 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; |
| +} |
| + |
| +// Wraps a base::SharedMemoryHandle as a Mojo handle. Takes ownership of the |
| +// SharedMemoryHandle. Note that |read_only| is only an indicator of whether |
| +// |memory_handle| only supports read-only mapping. It does NOT have any |
| +// influence on the access control of the shared buffer object. |
| +inline 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()); |
| +#else |
| +#error Unsupported platform. |
| +#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)); |
| +} |
| + |
| +// Unwraps a base::SharedMemoryHandle from a Mojo handle. The caller assumes |
| +// responsibility for the lifetime of the SharedMemoryHandle. |
| +inline 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()); |
| +#else |
| +#error Unsupported platform. |
| +#endif |
| + |
| + return MOJO_RESULT_OK; |
| +} |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_ |