Chromium Code Reviews| 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 contains types/functions and constants for platform handle wrapping | |
| 6 // and unwrapping APIs. | |
| 7 // | |
| 8 // Note: This header should be compilable as C. | |
| 9 | |
| 10 #ifndef MOJO_PUBLIC_C_SYSTEM_PLATFORM_HANDLE_H_ | |
| 11 #define MOJO_PUBLIC_C_SYSTEM_PLATFORM_HANDLE_H_ | |
| 12 | |
| 13 #include <stdint.h> | |
| 14 | |
| 15 #include "mojo/public/c/system/system_export.h" | |
| 16 #include "mojo/public/c/system/types.h" | |
| 17 | |
| 18 #ifdef __cplusplus | |
| 19 extern "C" { | |
| 20 #endif | |
| 21 | |
| 22 // |MojoPlatformHandleType|: A value indicating the specific type of platform | |
| 23 // handle encapsulated by a MojoPlatformHandle (see below.) This is stored | |
| 24 // in the MojoPlatformHandle's |type| field and determines how the |value| | |
| 25 // field is interpreted. | |
| 26 // | |
| 27 // |MOJO_PLATFORM_HANDLE_TYPE_INVALID| - An invalid platform handle. | |
| 28 // |MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR| - A file descriptor. Only valid | |
| 29 // on POSIX systems. | |
| 30 // |MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT| - A Mach port. Only valid on OS X. | |
| 31 // |MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE| - A Windows HANDLE value. Only | |
| 32 // valid on Windows. | |
| 33 | |
| 34 typedef uint32_t MojoPlatformHandleType; | |
| 35 | |
| 36 #ifdef __cplusplus | |
| 37 const MojoPlatformHandleType MOJO_PLATFORM_HANDLE_TYPE_INVALID = 0; | |
| 38 const MojoPlatformHandleType MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR = 1; | |
| 39 const MojoPlatformHandleType MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT = 2; | |
| 40 const MojoPlatformHandleType MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE = 3; | |
| 41 #else | |
| 42 #define MOJO_PLATFORM_HANDLE_TYPE_INVALID ((MojoPlatformHandleType)0) | |
| 43 #define MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR ((MojoPlatformHandleType)1) | |
| 44 #define MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT ((MojoPlatformHandleType)2) | |
| 45 #define MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE ((MojoPlatformHandleType)3) | |
| 46 #endif | |
| 47 | |
| 48 // |MojoPlatformHandle|: A handle to an OS object. | |
| 49 // |uint32_t struct_size|: The size of this structure. Used for versioning | |
| 50 // to allow for future extensions. | |
| 51 // |MojoPlatformHandleType type|: The type of handle stored in |value|. | |
| 52 // |uint64_t value|: The value of this handle. Ignored if |type| is | |
| 53 // MOJO_PLATFORM_HANDLE_TYPE_INVALID. | |
| 54 // | |
| 55 | |
| 56 struct MOJO_ALIGNAS(8) MojoPlatformHandle { | |
| 57 uint32_t struct_size; | |
| 58 MojoPlatformHandleType type; | |
| 59 uint64_t value; | |
| 60 }; | |
| 61 MOJO_STATIC_ASSERT(sizeof(MojoPlatformHandle) == 16, | |
| 62 "MojoPlatformHandle has wrong size"); | |
| 63 | |
| 64 // |MojoPlatformSharedBufferHandleFlags|: Flags relevant to wrapped platform | |
| 65 // shared buffers. | |
| 66 // | |
| 67 // |MOJO_PLATFORM_SHARED_BUFFER_HANDLE_NONE| - No flags. | |
| 68 // |MOJO_PLATFORM_SHARED_BUFFER_HANDLE_READ_ONLY| - Indicates that the wrapped | |
| 69 // buffer handle may only be mapped for reading. | |
| 70 | |
| 71 typedef uint32_t MojoPlatformSharedBufferHandleFlags; | |
| 72 | |
| 73 #ifdef __cplusplus | |
| 74 const MojoPlatformSharedBufferHandleFlags | |
| 75 MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE = 0; | |
| 76 | |
| 77 const MojoPlatformSharedBufferHandleFlags | |
| 78 MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY = 1 << 0; | |
| 79 #else | |
| 80 #define MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE \ | |
| 81 ((MojoPlatformSharedBufferHandleFlags)0) | |
| 82 | |
| 83 #define MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY \ | |
| 84 ((MojoPlatformSharedBufferHandleFlags)1 << 0) | |
| 85 #endif | |
| 86 | |
| 87 // Wraps a generic platform handle as a Mojo handle which can be transferred | |
| 88 // over a message pipe. Takes ownership of the underlying platform object. | |
| 89 // | |
| 90 // |platform_handle|: The platform handle to wrap. | |
| 91 // | |
| 92 // Returns: | |
| 93 // |MOJO_RESULT_OK| if the handle was successfully wrapped. In this case | |
| 94 // |*mojo_handle| contains the Mojo handle of the wrapped object. | |
| 95 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if the system is out of handles. | |
| 96 // |MOJO_RESULT_INVALID_ARGUMENT| if |platform_handle| was not a valid | |
| 97 // platform handle. | |
| 98 // | |
| 99 // NOTE: It is not alwawys possible to detect if |platform_handle| is valid, so | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
s/alwawys/always
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
done
| |
| 100 // this call may succeed even when |handle| invalid (e.g. a valid file | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
s/|handle|/|platform_handle|
Also, I think you re
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
I do - it used to be called |handle|. In any case,
| |
| 101 // descriptor value for a file descriptor that isn't actually open.) | |
| 102 MOJO_SYSTEM_EXPORT MojoResult | |
| 103 MojoWrapPlatformHandle(const struct MojoPlatformHandle* platform_handle, | |
| 104 MojoHandle* mojo_handle); // Out | |
| 105 | |
| 106 // Unwraps a generic platform handle from a Mojo handle. If this call succeeds, | |
| 107 // the Mojo handle is closed. If this call succeeds, ownership of the underlying | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
"the mojo handle is closed"
This sentence is redun
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
Fixed.
| |
| 108 // platform object is bound to the returned platform handle and becomes the | |
| 109 // caller's responsibility. The Mojo handle is always closed regardless of | |
| 110 // success or failure. | |
| 111 // | |
| 112 // |mojo_handle|: The Mojo handle from which to unwrap the platform handle. | |
| 113 // | |
| 114 // Returns: | |
| 115 // |MOJO_RESULT_OK| if the handle was successfully unwrapped. In this case | |
| 116 // |*platform_handle| contains the unwrapped platform handle. | |
| 117 // |MOJO_RESULT_INVALID_ARGUMENT| if |mojo_handle| was not a valid Mojo | |
| 118 // handle wrapping a platform handle. | |
| 119 MOJO_SYSTEM_EXPORT MojoResult | |
| 120 MojoUnwrapPlatformHandle(MojoHandle mojo_handle, | |
| 121 struct MojoPlatformHandle* platform_handle); // Out | |
| 122 | |
| 123 // Wraps a platform shared buffer handle as a Mojo shared buffer handle which | |
| 124 // can be transferred over a message pipe. Takes ownership of the platform | |
| 125 // shared buffer handle. | |
| 126 // | |
| 127 // |platform_handle|: The platform handle to wrap. Must be a handle to a | |
| 128 // shared buffer object. | |
| 129 // |num_bytes|: The size of the shared buffer in bytes. | |
| 130 // | |
| 131 // NOTE: Set |MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY| in |flags| if | |
| 132 // the buffer to be wrapped is already read-only. This flag does NOT change the | |
| 133 // access control of the buffer in any way. | |
| 134 // | |
| 135 // Returns: | |
| 136 // |MOJO_RESULT_OK| if the handle was successfully wrapped. In this case | |
| 137 // |*mojo_handle| contains a Mojo shared buffer handle. | |
| 138 // |MOJO_RESULT_INVALID_ARGUMENT| if |platform_handle| was not a valid | |
| 139 // platform shared buffer handle. | |
| 140 MOJO_SYSTEM_EXPORT MojoResult | |
| 141 MojoWrapPlatformSharedBufferHandle( | |
| 142 const struct MojoPlatformHandle* platform_handle, | |
| 143 size_t num_bytes, | |
| 144 MojoPlatformSharedBufferHandleFlags flags, | |
| 145 MojoHandle* mojo_handle); // Out | |
| 146 | |
| 147 // Unwraps a platform shared buffer handle from a Mojo shared buffer handle. | |
| 148 // If this call succeeds, ownership of the underlying shared buffer object is | |
| 149 // bound to the returned platform handle and becomes the caller's | |
| 150 // responsibility. The Mojo handle is always closed regardless of success or | |
| 151 // failure. | |
| 152 // | |
| 153 // |mojo_handle|: The Mojo shared buffer handle to unwrap. | |
| 154 // | |
| 155 // Returns: | |
| 156 // |MOJO_RESULT_OK| if the handle was successfully unwrapped. In this case | |
| 157 // |*platform_handle| contains a platform shared buffer handle, | |
| 158 // |*num_bytes| contains the size of the shared buffer object, and | |
| 159 // |*flags| indicates flags relevant to the wrapped buffer (see below). | |
| 160 // |MOJO_RESULT_INVALID_ARGUMENT| if |mojo_handle| is not a valid Mojo | |
| 161 // shared buffer handle. | |
| 162 // | |
| 163 // Flags which may be set in |*flags| upon success: | |
| 164 // |MOJO_PLATFORM_SHARED_BUFFER_FLAG_READ_ONLY| is set iff the unwrapped | |
| 165 // shared buffer handle may only be used for read-only mapping. | |
| 166 MOJO_SYSTEM_EXPORT MojoResult | |
| 167 MojoUnwrapPlatformSharedBufferHandle( | |
| 168 MojoHandle mojo_handle, | |
| 169 struct MojoPlatformHandle* platform_handle, | |
| 170 size_t* num_bytes, | |
| 171 MojoPlatformSharedBufferHandleFlags* flags); | |
| 172 | |
| 173 #ifdef __cplusplus | |
| 174 } // extern "C" | |
| 175 #endif | |
| 176 | |
| 177 #endif // MOJO_PUBLIC_C_SYSTEM_PLATFORM_HANDLE_H_ | |
| OLD | NEW |