OLD | NEW |
---|---|
(Empty) | |
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/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
| |
9 // 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 | |
34 const MojoPlatformHandleType kPlatformFileHandleType = | |
35 MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR; | |
36 const base::PlatformFile kInvalidPlatformFile = -1; | |
37 | |
38 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
39 const MojoPlatformHandleType kPlatformSharedBufferHandleType = | |
40 MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT; | |
41 #else | |
42 const MojoPlatformHandleType kPlatformSharedBufferHandleType = | |
43 MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR; | |
44 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
45 | |
46 #elif defined(OS_WIN) | |
47 const MojoPlatformHandleType kPlatformFileHandleType = | |
48 MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE; | |
49 const base::PlatformFile kInvalidPlatformFile = INVALID_HANDLE_VALUE; | |
50 | |
51 const MojoPlatformHandleType kPlatformSharedBufferHandleType = | |
52 MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE; | |
53 #else | |
54 #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
| |
55 #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
| |
56 | |
57 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
| |
58 #if defined(OS_WIN) | |
59 return reinterpret_cast<uint64_t>(file); | |
60 #else | |
61 return static_cast<uint64_t>(file); | |
62 #endif | |
63 } | |
64 | |
65 inline base::PlatformFile PlatformFileFromPlatformHandleValue(uint64_t value) { | |
66 #if defined(OS_WIN) | |
67 return reinterpret_cast<base::PlatformFile>(value); | |
68 #else | |
69 return static_cast<base::PlatformFile>(value); | |
70 #endif | |
71 } | |
72 | |
73 // Wraps a PlatformFile as a Mojo handle. Takes ownership of the file object. | |
74 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
| |
75 MojoPlatformHandle platform_handle; | |
76 platform_handle.struct_size = sizeof(MojoPlatformHandle); | |
77 platform_handle.type = kPlatformFileHandleType; | |
78 platform_handle.value = PlatformHandleValueFromPlatformFile(platform_file); | |
79 | |
80 MojoHandle mojo_handle; | |
81 MojoResult result = MojoWrapPlatformHandle(&platform_handle, &mojo_handle); | |
82 CHECK_EQ(result, MOJO_RESULT_OK); | |
83 | |
84 return ScopedHandle(Handle(mojo_handle)); | |
85 } | |
86 | |
87 // Unwraps a PlatformFile from a Mojo handle. | |
88 inline MojoResult UnwrapPlatformFile(ScopedHandle handle, | |
89 base::PlatformFile* file) { | |
90 MojoPlatformHandle platform_handle; | |
91 platform_handle.struct_size = sizeof(MojoPlatformHandle); | |
92 MojoResult result = MojoUnwrapPlatformHandle(handle.release().value(), | |
93 &platform_handle); | |
94 if (result != MOJO_RESULT_OK) | |
95 return result; | |
96 | |
97 if (platform_handle.type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) { | |
98 *file = kInvalidPlatformFile; | |
99 } else { | |
100 CHECK_EQ(platform_handle.type, kPlatformFileHandleType); | |
101 *file = PlatformFileFromPlatformHandleValue(platform_handle.value); | |
102 } | |
103 | |
104 return MOJO_RESULT_OK; | |
105 } | |
106 | |
107 // Wraps a base::SharedMemoryHandle as a Mojo handle. Takes ownership of the | |
108 // SharedMemoryHandle. Note that |read_only| is only an indicator of whether | |
109 // |memory_handle| only supports read-only mapping. It does NOT have any | |
110 // influence on the access control of the shared buffer object. | |
111 inline ScopedSharedBufferHandle WrapSharedMemoryHandle( | |
112 const base::SharedMemoryHandle& memory_handle, | |
113 size_t size, | |
114 bool read_only) { | |
115 MojoPlatformHandle platform_handle; | |
116 platform_handle.struct_size = sizeof(MojoPlatformHandle); | |
117 platform_handle.type = kPlatformSharedBufferHandleType; | |
118 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
119 platform_handle.value = | |
120 static_cast<uint64_t>(memory_handle.GetMemoryObject()); | |
121 #elif defined(OS_POSIX) | |
122 platform_handle.value = PlatformHandleValueFromPlatformFile(memory_handle.fd); | |
123 #elif defined(OS_WIN) | |
124 platform_handle.value = | |
125 PlatformHandleValueFromPlatformFile(memory_handle.GetHandle()); | |
126 #else | |
127 #error Unsupported platform. | |
128 #endif | |
129 | |
130 MojoPlatformSharedBufferHandleFlags flags = | |
131 MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE; | |
132 if (read_only) | |
133 flags |= MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY; | |
134 | |
135 MojoHandle mojo_handle; | |
136 MojoResult result = MojoWrapPlatformSharedBufferHandle( | |
137 &platform_handle, size, flags, &mojo_handle); | |
138 CHECK_EQ(result, MOJO_RESULT_OK); | |
139 | |
140 return ScopedSharedBufferHandle(SharedBufferHandle(mojo_handle)); | |
141 } | |
142 | |
143 // Unwraps a base::SharedMemoryHandle from a Mojo handle. The caller assumes | |
144 // responsibility for the lifetime of the SharedMemoryHandle. | |
145 inline MojoResult UnwrapSharedMemoryHandle( | |
146 ScopedSharedBufferHandle handle, | |
147 base::SharedMemoryHandle* memory_handle, | |
148 size_t* size, | |
149 bool* read_only) { | |
150 MojoPlatformHandle platform_handle; | |
151 platform_handle.struct_size = sizeof(MojoPlatformHandle); | |
152 | |
153 MojoPlatformSharedBufferHandleFlags flags; | |
154 size_t num_bytes; | |
155 MojoResult result = MojoUnwrapPlatformSharedBufferHandle( | |
156 handle.release().value(), &platform_handle, &num_bytes, &flags); | |
157 if (result != MOJO_RESULT_OK) | |
158 return result; | |
159 | |
160 if (size) | |
161 *size = num_bytes; | |
162 | |
163 if (read_only) | |
164 *read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY; | |
165 | |
166 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
167 CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT); | |
168 *memory_handle = base::SharedMemoryHandle( | |
169 static_cast<mach_port_t>(platform_handle.value), num_bytes, | |
170 base::GetCurrentProcId()); | |
171 #elif defined(OS_POSIX) | |
172 CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR); | |
173 *memory_handle = base::SharedMemoryHandle( | |
174 static_cast<int>(platform_handle.value), false); | |
175 #elif defined(OS_WIN) | |
176 CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE); | |
177 *memory_handle = base::SharedMemoryHandle( | |
178 reinterpret_cast<HANDLE>(platform_handle.value), | |
179 base::GetCurrentProcId()); | |
180 #else | |
181 #error Unsupported platform. | |
182 #endif | |
183 | |
184 return MOJO_RESULT_OK; | |
185 } | |
186 | |
187 } // namespace mojo | |
188 | |
189 #endif // MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_ | |
OLD | NEW |