• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This file provides a C++ wrapping around the Mojo C API for platform handles,
6 // replacing the prefix of "Mojo" with a "mojo" namespace.
7 //
8 // Please see "mojo/public/c/system/platform_handle.h" for complete
9 // documentation of the API.
10 
11 #ifndef MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_
12 #define MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_
13 
14 #include <stdint.h>
15 
16 #include "base/compiler_specific.h"
17 #include "base/files/file.h"
18 #include "base/logging.h"
19 #include "base/macros.h"
20 #include "base/memory/shared_memory_handle.h"
21 #include "base/process/process_handle.h"
22 #include "mojo/public/c/system/platform_handle.h"
23 #include "mojo/public/cpp/system/buffer.h"
24 #include "mojo/public/cpp/system/handle.h"
25 
26 #if defined(OS_WIN)
27 #include <windows.h>
28 #endif
29 
30 namespace mojo {
31 
32 #if defined(OS_POSIX)
33 const MojoPlatformHandleType kPlatformFileHandleType =
34     MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
35 
36 #if defined(OS_MACOSX) && !defined(OS_IOS)
37 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
38     MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
39 #else
40 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
41     MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
42 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
43 
44 #elif defined(OS_WIN)
45 const MojoPlatformHandleType kPlatformFileHandleType =
46     MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
47 
48 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
49     MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
50 #endif  // defined(OS_POSIX)
51 
52 // Wraps a PlatformFile as a Mojo handle. Takes ownership of the file object.
53 ScopedHandle WrapPlatformFile(base::PlatformFile platform_file);
54 
55 // Unwraps a PlatformFile from a Mojo handle.
56 MojoResult UnwrapPlatformFile(ScopedHandle handle, base::PlatformFile* file);
57 
58 // Wraps a base::SharedMemoryHandle as a Mojo handle. Takes ownership of the
59 // SharedMemoryHandle. Note that |read_only| is only an indicator of whether
60 // |memory_handle| only supports read-only mapping. It does NOT have any
61 // influence on the access control of the shared buffer object.
62 ScopedSharedBufferHandle WrapSharedMemoryHandle(
63     const base::SharedMemoryHandle& memory_handle,
64     size_t size,
65     bool read_only);
66 
67 // Unwraps a base::SharedMemoryHandle from a Mojo handle. The caller assumes
68 // responsibility for the lifetime of the SharedMemoryHandle.
69 MojoResult UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
70                                     base::SharedMemoryHandle* memory_handle,
71                                     size_t* size,
72                                     bool* read_only);
73 
74 }  // namespace mojo
75 
76 #endif  // MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_
77