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

Simplify mojo_base shared memory region support

Changes the SharedMemoryRegion types in mojo_base mojom to use a
handle<shared_buffer> as their internal representation. This simplifies
the traits and makes these region types usable in Java and JS.

Bug: 826213
Change-Id: Id515f61f34b85c655b70d45d53302e873547a83a
Reviewed-on: https://chromium-review.googlesource.com/1059008
Commit-Queue: Ken Rockot <rockot@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#559207}
parent adbc5eb9
...@@ -5,77 +5,23 @@ ...@@ -5,77 +5,23 @@
#ifndef MOJO_PUBLIC_CPP_BASE_SHARED_MEMORY_MOJOM_TRAITS_H_ #ifndef MOJO_PUBLIC_CPP_BASE_SHARED_MEMORY_MOJOM_TRAITS_H_
#define MOJO_PUBLIC_CPP_BASE_SHARED_MEMORY_MOJOM_TRAITS_H_ #define MOJO_PUBLIC_CPP_BASE_SHARED_MEMORY_MOJOM_TRAITS_H_
#include <cstdint>
#include "base/component_export.h" #include "base/component_export.h"
#include "base/memory/platform_shared_memory_region.h"
#include "base/memory/read_only_shared_memory_region.h" #include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/unsafe_shared_memory_region.h" #include "base/memory/unsafe_shared_memory_region.h"
#include "base/memory/writable_shared_memory_region.h" #include "base/memory/writable_shared_memory_region.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "mojo/public/cpp/base/unguessable_token_mojom_traits.h"
#include "mojo/public/cpp/bindings/enum_traits.h"
#include "mojo/public/cpp/bindings/struct_traits.h" #include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/system/handle.h" #include "mojo/public/cpp/system/handle.h"
#include "mojo/public/mojom/base/shared_memory.mojom-shared.h" #include "mojo/public/mojom/base/shared_memory.mojom-shared.h"
namespace mojo { namespace mojo {
template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM) StructTraits<
mojo_base::mojom::PlatformSharedMemoryHandleDataView,
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle> {
static mojo::ScopedHandle handle_value(
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle& handle);
#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_FUCHSIA) && \
(!defined(OS_MACOSX) || defined(OS_IOS))
static mojo::ScopedHandle readonly_fd(
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle& handle);
#endif
static bool Read(
mojo_base::mojom::PlatformSharedMemoryHandleDataView data,
base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle* out);
};
template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
EnumTraits<mojo_base::mojom::PlatformSharedMemoryRegion_Mode,
base::subtle::PlatformSharedMemoryRegion::Mode> {
static mojo_base::mojom::PlatformSharedMemoryRegion_Mode ToMojom(
base::subtle::PlatformSharedMemoryRegion::Mode input);
static bool FromMojom(mojo_base::mojom::PlatformSharedMemoryRegion_Mode input,
base::subtle::PlatformSharedMemoryRegion::Mode* output);
};
template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
StructTraits<mojo_base::mojom::PlatformSharedMemoryRegionDataView,
base::subtle::PlatformSharedMemoryRegion> {
static base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
platform_handle(base::subtle::PlatformSharedMemoryRegion& region);
static base::subtle::PlatformSharedMemoryRegion::Mode mode(
const base::subtle::PlatformSharedMemoryRegion& region);
static uint64_t size(const base::subtle::PlatformSharedMemoryRegion& region);
static base::UnguessableToken guid(
const base::subtle::PlatformSharedMemoryRegion& region);
static bool Read(mojo_base::mojom::PlatformSharedMemoryRegionDataView data,
base::subtle::PlatformSharedMemoryRegion* out);
};
template <> template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM) struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
StructTraits<mojo_base::mojom::ReadOnlySharedMemoryRegionDataView, StructTraits<mojo_base::mojom::ReadOnlySharedMemoryRegionDataView,
base::ReadOnlySharedMemoryRegion> { base::ReadOnlySharedMemoryRegion> {
static bool IsNull(const base::ReadOnlySharedMemoryRegion& region); static bool IsNull(const base::ReadOnlySharedMemoryRegion& region);
static void SetToNull(base::ReadOnlySharedMemoryRegion* region); static void SetToNull(base::ReadOnlySharedMemoryRegion* region);
static base::subtle::PlatformSharedMemoryRegion region( static mojo::ScopedSharedBufferHandle buffer(
base::ReadOnlySharedMemoryRegion& in_region); base::ReadOnlySharedMemoryRegion& in_region);
static bool Read(mojo_base::mojom::ReadOnlySharedMemoryRegionDataView data, static bool Read(mojo_base::mojom::ReadOnlySharedMemoryRegionDataView data,
base::ReadOnlySharedMemoryRegion* out); base::ReadOnlySharedMemoryRegion* out);
...@@ -87,7 +33,7 @@ struct COMPONENT_EXPORT(MOJO_BASE_MOJOM) ...@@ -87,7 +33,7 @@ struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
base::UnsafeSharedMemoryRegion> { base::UnsafeSharedMemoryRegion> {
static bool IsNull(const base::UnsafeSharedMemoryRegion& region); static bool IsNull(const base::UnsafeSharedMemoryRegion& region);
static void SetToNull(base::UnsafeSharedMemoryRegion* region); static void SetToNull(base::UnsafeSharedMemoryRegion* region);
static base::subtle::PlatformSharedMemoryRegion region( static mojo::ScopedSharedBufferHandle buffer(
base::UnsafeSharedMemoryRegion& in_region); base::UnsafeSharedMemoryRegion& in_region);
static bool Read(mojo_base::mojom::UnsafeSharedMemoryRegionDataView data, static bool Read(mojo_base::mojom::UnsafeSharedMemoryRegionDataView data,
base::UnsafeSharedMemoryRegion* out); base::UnsafeSharedMemoryRegion* out);
...@@ -99,7 +45,7 @@ struct COMPONENT_EXPORT(MOJO_BASE_MOJOM) ...@@ -99,7 +45,7 @@ struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
base::WritableSharedMemoryRegion> { base::WritableSharedMemoryRegion> {
static bool IsNull(const base::WritableSharedMemoryRegion& region); static bool IsNull(const base::WritableSharedMemoryRegion& region);
static void SetToNull(base::WritableSharedMemoryRegion* region); static void SetToNull(base::WritableSharedMemoryRegion* region);
static base::subtle::PlatformSharedMemoryRegion region( static mojo::ScopedSharedBufferHandle buffer(
base::WritableSharedMemoryRegion& in_region); base::WritableSharedMemoryRegion& in_region);
static bool Read(mojo_base::mojom::WritableSharedMemoryRegionDataView data, static bool Read(mojo_base::mojom::WritableSharedMemoryRegionDataView data,
base::WritableSharedMemoryRegion* out); base::WritableSharedMemoryRegion* out);
......
...@@ -15,7 +15,7 @@ TEST(SharedMemoryMojomTest, ReadOnly) { ...@@ -15,7 +15,7 @@ TEST(SharedMemoryMojomTest, ReadOnly) {
memcpy(region.mapping.memory(), kTestData.data(), kTestData.size()); memcpy(region.mapping.memory(), kTestData.data(), kTestData.size());
base::ReadOnlySharedMemoryRegion read_only_out; base::ReadOnlySharedMemoryRegion read_only_out;
EXPECT_TRUE(mojo::test::SerializeAndDeserialize< ASSERT_TRUE(mojo::test::SerializeAndDeserialize<
mojo_base::mojom::ReadOnlySharedMemoryRegion>(&region.region, mojo_base::mojom::ReadOnlySharedMemoryRegion>(&region.region,
&read_only_out)); &read_only_out));
base::ReadOnlySharedMemoryMapping mapping = read_only_out.Map(); base::ReadOnlySharedMemoryMapping mapping = read_only_out.Map();
...@@ -29,7 +29,7 @@ TEST(SharedMemoryMojomTest, Writable) { ...@@ -29,7 +29,7 @@ TEST(SharedMemoryMojomTest, Writable) {
memcpy(mapping.memory(), kTestData.data(), kTestData.size()); memcpy(mapping.memory(), kTestData.data(), kTestData.size());
base::WritableSharedMemoryRegion writable_out; base::WritableSharedMemoryRegion writable_out;
EXPECT_TRUE(mojo::test::SerializeAndDeserialize< ASSERT_TRUE(mojo::test::SerializeAndDeserialize<
mojo_base::mojom::WritableSharedMemoryRegion>(&region, mojo_base::mojom::WritableSharedMemoryRegion>(&region,
&writable_out)); &writable_out));
...@@ -44,7 +44,7 @@ TEST(SharedMemoryMojomTest, Unsafe) { ...@@ -44,7 +44,7 @@ TEST(SharedMemoryMojomTest, Unsafe) {
memcpy(mapping.memory(), kTestData.data(), kTestData.size()); memcpy(mapping.memory(), kTestData.data(), kTestData.size());
base::UnsafeSharedMemoryRegion unsafe_out; base::UnsafeSharedMemoryRegion unsafe_out;
EXPECT_TRUE( ASSERT_TRUE(
mojo::test::SerializeAndDeserialize< mojo::test::SerializeAndDeserialize<
mojo_base::mojom::UnsafeSharedMemoryRegion>(&region, &unsafe_out)); mojo_base::mojom::UnsafeSharedMemoryRegion>(&region, &unsafe_out));
......
...@@ -4,56 +4,22 @@ ...@@ -4,56 +4,22 @@
module mojo_base.mojom; module mojo_base.mojom;
import "mojo/public/mojom/base/unguessable_token.mojom";
// Mirrors the base::subtle::PlatformSharedMemoryRegion::ScopedPlatformHandle
// type.
//
// TODO(https://crbug.com/826213): Remove this once Mojo shared buffers are
// properly backed by base::subtle::PlatformSharedMemoryRegion.
struct PlatformSharedMemoryHandle {
handle handle_value;
[EnableIf=shared_memory_region_uses_fd_pair]
handle? readonly_fd;
};
// Mirrors the base::subtle::PlatformSharedMemoryRegion type.
//
// This should not be used directly. Use one of the specific region types below
// instead.
//
// TODO(https://crbug.com/826213): Remove this once Mojo shared buffers are
// properly backed by base::subtle::PlatformSharedMemoryRegion.
struct PlatformSharedMemoryRegion {
enum Mode {
kReadOnly,
kWritable,
kUnsafe,
};
PlatformSharedMemoryHandle platform_handle;
Mode mode;
uint64 size;
UnguessableToken guid;
};
// Wraps a shared memory handle with additional type information to convey that // Wraps a shared memory handle with additional type information to convey that
// the handle is only mappable to read-only memory. // the handle is only mappable to read-only memory.
struct ReadOnlySharedMemoryRegion { struct ReadOnlySharedMemoryRegion {
PlatformSharedMemoryRegion region; handle<shared_buffer> buffer;
}; };
// Wraps a shared memory handle with additional type information to convey that // Wraps a shared memory handle with additional type information to convey that
// the handle is mappable to writable memory but can also be converted to // the handle is mappable to writable memory but can also be converted to
// a ReadOnlySharedMemoryRegion for sharing with other clients. // a ReadOnlySharedMemoryRegion for sharing with other clients.
struct WritableSharedMemoryRegion { struct WritableSharedMemoryRegion {
PlatformSharedMemoryRegion region; handle<shared_buffer> buffer;
}; };
// Wraps a shared memory handle with additional type information to convey that // Wraps a shared memory handle with additional type information to convey that
// the handle is always mappable to writable memory by any client which obtains // the handle is always mappable to writable memory by any client which obtains
// a handle duplicated from this one. // a handle duplicated from this one.
struct UnsafeSharedMemoryRegion { struct UnsafeSharedMemoryRegion {
PlatformSharedMemoryRegion region; handle<shared_buffer> buffer;
}; };
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