• 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 #include "mojo/public/cpp/system/system_export.h"
26 
27 #if defined(OS_WIN)
28 #include <windows.h>
29 #endif
30 
31 namespace mojo {
32 
33 #if defined(OS_POSIX)
34 const MojoPlatformHandleType kPlatformFileHandleType =
35     MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
36 
37 #if defined(OS_MACOSX) && !defined(OS_IOS)
38 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
39     MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
40 #else
41 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
42     MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR;
43 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
44 
45 #elif defined(OS_WIN)
46 const MojoPlatformHandleType kPlatformFileHandleType =
47     MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
48 
49 const MojoPlatformHandleType kPlatformSharedBufferHandleType =
50     MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE;
51 #endif  // defined(OS_POSIX)
52 
53 // Wraps a PlatformFile as a Mojo handle. Takes ownership of the file object.
54 MOJO_CPP_SYSTEM_EXPORT
55 ScopedHandle WrapPlatformFile(base::PlatformFile platform_file);
56 
57 // Unwraps a PlatformFile from a Mojo handle.
58 MOJO_CPP_SYSTEM_EXPORT
59 MojoResult UnwrapPlatformFile(ScopedHandle handle, base::PlatformFile* file);
60 
61 // Wraps a base::SharedMemoryHandle as a Mojo handle. Takes ownership of the
62 // SharedMemoryHandle. Note that |read_only| is only an indicator of whether
63 // |memory_handle| only supports read-only mapping. It does NOT have any
64 // influence on the access control of the shared buffer object.
65 MOJO_CPP_SYSTEM_EXPORT
66 ScopedSharedBufferHandle WrapSharedMemoryHandle(
67     const base::SharedMemoryHandle& memory_handle,
68     size_t size,
69     bool read_only);
70 
71 // Unwraps a base::SharedMemoryHandle from a Mojo handle. The caller assumes
72 // responsibility for the lifetime of the SharedMemoryHandle.
73 MOJO_CPP_SYSTEM_EXPORT MojoResult
74 UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
75                          base::SharedMemoryHandle* memory_handle,
76                          size_t* size,
77                          bool* read_only);
78 
79 #if defined(OS_MACOSX) && !defined(OS_IOS)
80 // Wraps a mach_port_t as a Mojo handle. This takes a reference to the
81 // Mach port.
82 MOJO_CPP_SYSTEM_EXPORT ScopedHandle WrapMachPort(mach_port_t port);
83 
84 // Unwraps a mach_port_t from a Mojo handle. The caller gets ownership of the
85 // Mach port.
86 MOJO_CPP_SYSTEM_EXPORT MojoResult UnwrapMachPort(ScopedHandle handle,
87                                                  mach_port_t* port);
88 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
89 
90 }  // namespace mojo
91 
92 #endif  // MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_
93