blob: 9c647889cfc9de41c2449fbbecea6b7fc2fa8bd2 [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_PERCEPTION_SHARED_MEMORY_PROVIDER_H_
#define MEDIA_PERCEPTION_SHARED_MEMORY_PROVIDER_H_
#include <base/memory/shared_memory.h>
#include <mojo/edk/embedder/embedder.h>
#include <mojo/edk/embedder/platform_channel_pair.h>
#include <mojo/edk/embedder/platform_channel_utils_posix.h>
#include <mojo/edk/embedder/platform_handle_vector.h>
#include <mojo/edk/embedder/scoped_platform_handle.h>
#include <mojo/public/cpp/bindings/binding.h>
#include <memory>
// NOLINTNEXTLINE
#include "base/logging.h"
namespace mri {
// Wrapper on base::SharedMemory to provide init from file descriptor
// functionality and mapping and unmapping for in-process access.
class SharedMemoryProvider {
public:
~SharedMemoryProvider();
// Returns a new SharedMemoryProvider unique_ptr, which is a nullptr if the
// initialization failed and sets up |shared_memory_| from a file descriptor.
static std::unique_ptr<SharedMemoryProvider> CreateFromRawFileDescriptor(
bool read_only, mojo::ScopedHandle fd_handle,
uint32_t memory_size_in_bytes);
// Shared memory is owned by the provider.
base::SharedMemory* GetSharedMemoryForInProcessAccess();
uint32_t GetMemorySizeInBytes();
private:
// Initializes the |shared_memory_| member.
// scoped_handle is owned by the caller.
SharedMemoryProvider(bool read_only,
mojo::edk::ScopedPlatformHandle* scoped_handle,
uint32_t memory_size_in_bytes);
std::unique_ptr<base::SharedMemory> shared_memory_;
size_t mapped_size_;
};
} // namespace mri
#endif // MEDIA_PERCEPTION_SHARED_MEMORY_PROVIDER_H_