1 // Copyright 2014 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/constants and functions specific to message pipes. 6 // 7 // Note: This header should be compilable as C. 8 9 #ifndef MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ 10 #define MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ 11 12 #include <stdint.h> 13 14 #include "mojo/public/c/system/macros.h" 15 #include "mojo/public/c/system/system_export.h" 16 #include "mojo/public/c/system/types.h" 17 18 // Used to refer to message objects created by |MojoCreateMessage()|. 19 typedef uintptr_t MojoMessageHandle; 20 21 #define MOJO_MESSAGE_HANDLE_INVALID ((MojoMessageHandle)0) 22 23 // Flags passed to |MojoCreateMessagePipe()| via |MojoCreateMessagePipeOptions|. 24 // See values defined below. 25 typedef uint32_t MojoCreateMessagePipeFlags; 26 27 // No flags. Default behavior. 28 #define MOJO_CREATE_MESSAGE_PIPE_FLAG_NONE ((uint32_t)0) 29 30 // Options passed to |MojoCreateMessagePipe()|. 31 struct MOJO_ALIGNAS(8) MojoCreateMessagePipeOptions { 32 // The size of this structure, used for versioning. 33 uint32_t struct_size; 34 35 // See |MojoCreateMessagePipeFlags|. 36 MojoCreateMessagePipeFlags flags; 37 }; 38 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) <= 8, "int64_t has weird alignment"); 39 MOJO_STATIC_ASSERT(sizeof(MojoCreateMessagePipeOptions) == 8, 40 "MojoCreateMessagePipeOptions has wrong size"); 41 42 // Flags passed to |MojoWriteMessage()| via |MojoWriteMessageOptions|. See 43 // values defined below. 44 typedef uint32_t MojoWriteMessageFlags; 45 46 // No flags. Default behavior. 47 #define MOJO_WRITE_MESSAGE_FLAG_NONE ((uint32_t)0) 48 49 // Options passed to |MojoWriteMessage()|. 50 struct MOJO_ALIGNAS(8) MojoWriteMessageOptions { 51 // The size of this structure, used for versioning. 52 uint32_t struct_size; 53 54 // See |MojoWriteMessageFlags|. 55 MojoWriteMessageFlags flags; 56 }; 57 MOJO_STATIC_ASSERT(sizeof(MojoWriteMessageOptions) == 8, 58 "MojoWriteMessageOptions has wrong size"); 59 60 // Flags passed to |MojoReadMessage()| via |MojoReadMessageOptions|. See values 61 // defined below. 62 typedef uint32_t MojoReadMessageFlags; 63 64 // No flags. Default behavior. 65 #define MOJO_READ_MESSAGE_FLAG_NONE ((uint32_t)0) 66 67 // Options passed to |MojoReadMessage()|. 68 struct MOJO_ALIGNAS(8) MojoReadMessageOptions { 69 // The size of this structure, used for versioning. 70 uint32_t struct_size; 71 72 // See |MojoReadMessageFlags|. 73 MojoReadMessageFlags flags; 74 }; 75 MOJO_STATIC_ASSERT(sizeof(MojoReadMessageOptions) == 8, 76 "MojoReadMessageOptions has wrong size"); 77 78 // Flags passed to |MojoFuseMessagePipes()| via |MojoFuseMessagePipeOptions|. 79 // See values defined below. 80 typedef uint32_t MojoFuseMessagePipesFlags; 81 82 // No flags. Default behavior. 83 #define MOJO_FUSE_MESSAGE_PIPES_FLAG_NONE ((uint32_t)0) 84 85 // Options passed to |MojoFuseMessagePipes()|. 86 struct MOJO_ALIGNAS(8) MojoFuseMessagePipesOptions { 87 // The size of this structure, used for versioning. 88 uint32_t struct_size; 89 90 // See |MojoFuseMessagePipesFlags|. 91 MojoFuseMessagePipesFlags flags; 92 }; 93 MOJO_STATIC_ASSERT(sizeof(MojoFuseMessagePipesOptions) == 8, 94 "MojoFuseMessagePipesOptions has wrong size"); 95 96 // Flags passed to |MojoCreateMessage()| via |MojoCreateMessageOptions|. 97 typedef uint32_t MojoCreateMessageFlags; 98 99 // No flags. Default behavior. 100 #define MOJO_CREATE_MESSAGE_FLAG_NONE ((uint32_t)0) 101 102 // Options passed to |MojoCreateMessage()|. 103 struct MOJO_ALIGNAS(8) MojoCreateMessageOptions { 104 // The size of this structure, used for versioning. 105 uint32_t struct_size; 106 107 // See |MojoCreateMessageFlags|. 108 MojoCreateMessageFlags flags; 109 }; 110 MOJO_STATIC_ASSERT(sizeof(MojoCreateMessageOptions) == 8, 111 "MojoCreateMessageOptions has wrong size"); 112 113 // Flags passed to |MojoSerializeMessage()| via |MojoSerializeMessageOptions|. 114 typedef uint32_t MojoSerializeMessageFlags; 115 116 // No flags. Default behavior. 117 #define MOJO_SERIALIZE_MESSAGE_FLAG_NONE ((uint32_t)0) 118 119 // Options passed to |MojoSerializeMessage()|. 120 struct MOJO_ALIGNAS(8) MojoSerializeMessageOptions { 121 // The size of this structure, used for versioning. 122 uint32_t struct_size; 123 124 // See |MojoSerializeMessageFlags|. 125 MojoSerializeMessageFlags flags; 126 }; 127 MOJO_STATIC_ASSERT(sizeof(MojoSerializeMessageOptions) == 8, 128 "MojoSerializeMessageOptions has wrong size"); 129 130 // Flags passed to |MojoAppendMessageData()| via |MojoAppendMessageDataOptions|. 131 typedef uint32_t MojoAppendMessageDataFlags; 132 133 // No flags. Default behavior. 134 #define MOJO_APPEND_MESSAGE_DATA_FLAG_NONE ((uint32_t)0) 135 136 // If set, this comments the resulting (post-append) message size as the final 137 // size of the message payload, in terms of both bytes and attached handles. 138 #define MOJO_APPEND_MESSAGE_DATA_FLAG_COMMIT_SIZE \ 139 ((MojoAppendMessageDataFlags)1) 140 141 // Options passed to |MojoAppendMessageData()|. 142 struct MOJO_ALIGNAS(8) MojoAppendMessageDataOptions { 143 // The size of this structure, used for versioning. 144 uint32_t struct_size; 145 146 // See |MojoAppendMessageDataFlags|. 147 MojoAppendMessageDataFlags flags; 148 }; 149 MOJO_STATIC_ASSERT(sizeof(MojoAppendMessageDataOptions) == 8, 150 "MojoAppendMessageDataOptions has wrong size"); 151 152 // Flags passed to |MojoGetMessageData()| via |MojoGetMessageDataOptions|. 153 typedef uint32_t MojoGetMessageDataFlags; 154 155 // No flags. Default behavior. 156 #define MOJO_GET_MESSAGE_DATA_FLAG_NONE ((uint32_t)0) 157 158 // Ignores attached handles when retrieving message data. This leaves any 159 // attached handles intact and owned by the message object. 160 #define MOJO_GET_MESSAGE_DATA_FLAG_IGNORE_HANDLES ((uint32_t)1) 161 162 // Options passed to |MojoGetMessageData()|. 163 struct MOJO_ALIGNAS(8) MojoGetMessageDataOptions { 164 // The size of this structure, used for versioning. 165 uint32_t struct_size; 166 167 // See |MojoGetMessageDataFlags|. 168 MojoGetMessageDataFlags flags; 169 }; 170 MOJO_STATIC_ASSERT(sizeof(MojoGetMessageDataOptions) == 8, 171 "MojoGetMessageDataOptions has wrong size"); 172 173 // Flags passed to |MojoSetMessageContext()| via |MojoSetMessageContextOptions|. 174 typedef uint32_t MojoSetMessageContextFlags; 175 176 // No flags. Default behavior. 177 #define MOJO_SET_MESSAGE_CONTEXT_FLAG_NONE ((uint32_t)0) 178 179 // Options passed to |MojoSetMessageContext()|. 180 struct MOJO_ALIGNAS(8) MojoSetMessageContextOptions { 181 // The size of this structure, used for versioning. 182 uint32_t struct_size; 183 184 // See |MojoSetMessageContextFlags|. 185 MojoSetMessageContextFlags flags; 186 }; 187 MOJO_STATIC_ASSERT(sizeof(MojoSetMessageContextOptions) == 8, 188 "MojoSetMessageContextOptions has wrong size"); 189 190 // Flags passed to |MojoGetMessageContext()| via |MojoGetMessageContextOptions|. 191 typedef uint32_t MojoGetMessageContextFlags; 192 193 // No flags. Default behavior. 194 #define MOJO_GET_MESSAGE_CONTEXT_FLAG_NONE ((uint32_t)0) 195 196 // Options passed to |MojoGetMessageContext()|. 197 struct MOJO_ALIGNAS(8) MojoGetMessageContextOptions { 198 // The size of this structure, used for versioning. 199 uint32_t struct_size; 200 201 // See |MojoGetMessageContextFlags|. 202 MojoGetMessageContextFlags flags; 203 }; 204 MOJO_STATIC_ASSERT(sizeof(MojoGetMessageContextOptions) == 8, 205 "MojoGetMessageContextOptions has wrong size"); 206 207 // Flags passed to |MojoNotifyBadMessage()| via |MojoNotifyBadMessageOptions|. 208 typedef uint32_t MojoNotifyBadMessageFlags; 209 210 // No flags. Default behavior. 211 #define MOJO_NOTIFY_BAD_MESSAGE_FLAG_NONE ((uint32_t)0) 212 213 // Options passed to |MojoNotifyBadMessage()|. 214 struct MOJO_ALIGNAS(8) MojoNotifyBadMessageOptions { 215 // The size of this structure, used for versioning. 216 uint32_t struct_size; 217 218 // See |MojoNotifyBadMessageFlags|. 219 MojoNotifyBadMessageFlags flags; 220 }; 221 MOJO_STATIC_ASSERT(sizeof(MojoNotifyBadMessageOptions) == 8, 222 "MojoNotifyBadMessageOptions has wrong size"); 223 224 #ifdef __cplusplus 225 extern "C" { 226 #endif 227 228 // A callback which can serialize a message given some context. Passed to 229 // |MojoSetMessageContext()| along with a context it knows how to serialize. 230 // See |MojoSetMessageContext()| for more details. 231 // 232 // |message| is a message object which had |context| attached. 233 // |context| the context which was attached to |message|. 234 // 235 // Note that the context is always detached from the message immediately before 236 // this callback is invoked, and that the associated destructor (if any) is also 237 // invoked on |context| immediately after the serializer returns. 238 typedef void (*MojoMessageContextSerializer)(MojoMessageHandle message, 239 uintptr_t context); 240 241 // A callback which can be used to destroy a message context after serialization 242 // or in the event that the message to which it's attached is destroyed without 243 // ever being serialized. 244 typedef void (*MojoMessageContextDestructor)(uintptr_t context); 245 246 // Note: See the comment in functions.h about the meaning of the "optional" 247 // label for pointer parameters. 248 249 // Creates a message pipe, which is a bidirectional communication channel for 250 // framed data (i.e., messages). Messages can contain plain data and/or Mojo 251 // handles. 252 // 253 // |options| may be set to null for a message pipe with the default options. 254 // 255 // On success, |*message_pipe_handle0| and |*message_pipe_handle1| are set to 256 // handles for the two endpoints (ports) for the message pipe. 257 // 258 // Returns: 259 // |MOJO_RESULT_OK| on success. 260 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., 261 // |*options| is invalid). 262 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has 263 // been reached. 264 MOJO_SYSTEM_EXPORT MojoResult MojoCreateMessagePipe( 265 const struct MojoCreateMessagePipeOptions* options, // Optional. 266 MojoHandle* message_pipe_handle0, // Out. 267 MojoHandle* message_pipe_handle1); // Out. 268 269 // Writes a message to the message pipe endpoint given by |message_pipe_handle|. 270 // 271 // Note that regardless of success or failure, |message| is destroyed by this 272 // call and therefore invalidated. 273 // 274 // |options| may be null. 275 // 276 // Returns: 277 // |MOJO_RESULT_OK| on success (i.e., the message was enqueued). 278 // |MOJO_RESULT_INVALID_ARGUMENT| if |message_pipe_handle| or |message| is 279 // invalid. 280 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. 281 // Note that closing an endpoint is not necessarily synchronous (e.g., 282 // across processes), so this function may succeed even if the other 283 // endpoint has been closed (in which case the message would be dropped). 284 // |MOJO_RESULT_NOT_FOUND| if |message| has neither a context nor serialized 285 // buffer attached and therefore has nothing to be written. 286 MOJO_SYSTEM_EXPORT MojoResult 287 MojoWriteMessage(MojoHandle message_pipe_handle, 288 MojoMessageHandle message, 289 const struct MojoWriteMessageOptions* options); 290 291 // Reads the next message from a message pipe and returns a message as an opaque 292 // message handle. The returned message must eventually be destroyed using 293 // |MojoDestroyMessage()|. 294 // 295 // Message payload and handles can be accessed using |MojoGetMessageData()|. For 296 // Unserialized messages, context may be accessed using 297 // |MojoGetMessageContext()|. 298 // 299 // |options| may be null. |message| must be non-null. 300 // 301 // Returns: 302 // |MOJO_RESULT_OK| on success (i.e., a message was actually read). 303 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid. 304 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed 305 // and there are no more messages to read. 306 // |MOJO_RESULT_SHOULD_WAIT| if no message was available to be read. 307 MOJO_SYSTEM_EXPORT MojoResult 308 MojoReadMessage(MojoHandle message_pipe_handle, 309 const struct MojoReadMessageOptions* options, 310 MojoMessageHandle* message); 311 312 // Fuses two message pipe endpoints together. Given two pipes: 313 // 314 // A <-> B and C <-> D 315 // 316 // Fusing handle B and handle C results in a single pipe: 317 // 318 // A <-> D 319 // 320 // Handles B and C are ALWAYS closed. Any unread messages at C will eventually 321 // be delivered to A, and any unread messages at B will eventually be delivered 322 // to D. 323 // 324 // NOTE: A handle may only be fused if it is an open message pipe handle which 325 // has not been written to. 326 // 327 // |options| may be null. 328 // 329 // Returns: 330 // |MOJO_RESULT_OK| on success. 331 // |MOJO_RESULT_FAILED_PRECONDITION| if both handles were valid message pipe 332 // handles but could not be merged (e.g. one of them has been written to). 333 // |MOJO_INVALID_ARGUMENT| if either handle is not a fusable message pipe 334 // handle. 335 MOJO_SYSTEM_EXPORT MojoResult 336 MojoFuseMessagePipes(MojoHandle handle0, 337 MojoHandle handle1, 338 const struct MojoFuseMessagePipesOptions* options); 339 340 // Creates a new message object which may be sent over a message pipe via 341 // |MojoWriteMessage()|. Returns a handle to the new message object in 342 // |*message|. 343 // 344 // In its initial state the message object cannot be successfully written to a 345 // message pipe, but must first have either an opaque context or some serialized 346 // data attached (see |MojoSetMessageContext()| and 347 // |MojoAppendMessageData()|). 348 // 349 // NOTE: Unlike other types of Mojo API objects, messages are NOT thread-safe 350 // and thus callers of message-related APIs must be careful to restrict usage of 351 // any given |MojoMessageHandle| to a single thread at a time. 352 // 353 // Returns: 354 // |MOJO_RESULT_OK| if a new message was created. |*message| contains a handle 355 // to the new message object upon return. 356 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is null. 357 MOJO_SYSTEM_EXPORT MojoResult 358 MojoCreateMessage(const struct MojoCreateMessageOptions* options, 359 MojoMessageHandle* message); 360 361 // Destroys a message object created by |MojoCreateMessage()| or 362 // |MojoReadMessage()|. 363 // 364 // |message|: The message to destroy. Note that if a message has been written 365 // to a message pipe using |MojoWriteMessage()|, it is neither necessary nor 366 // valid to attempt to destroy it. 367 // 368 // Returns: 369 // |MOJO_RESULT_OK| if |message| was valid and has been freed. 370 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| was not a valid message. 371 MOJO_SYSTEM_EXPORT MojoResult MojoDestroyMessage(MojoMessageHandle message); 372 373 // Forces a message to be serialized in-place if not already serialized. 374 // 375 // Returns: 376 // |MOJO_RESULT_OK| if |message| was not serialized and is now serialized. 377 // In this case its thunks were invoked to perform serialization and 378 // ultimately destroy its associated context. The message may still be 379 // written to a pipe or decomposed by |MojoGetMessageData()|. 380 // |MOJO_RESULT_FAILED_PRECONDITION| if |message| was already serialized. 381 // |MOJO_RESULT_NOT_FOUND| if |message| cannot be serialized (i.e. it was 382 // created with null |MojoMessageContextSerializer|.) 383 // |MOJO_RESULT_BUSY| if one or more handles provided by the user context 384 // reported itself as busy during the serialization attempt. In this case 385 // all serialized handles are closed automatically. 386 // |MOJO_RESULT_ABORTED| if some other unspecified error occurred during 387 // handle serialization. In this case all serialized handles are closed 388 // automatically. 389 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message handle. 390 // 391 // Note that unserialized messages may be successfully transferred from one 392 // message pipe endpoint to another without ever being serialized. This function 393 // allows callers to coerce eager serialization. 394 MOJO_SYSTEM_EXPORT MojoResult 395 MojoSerializeMessage(MojoMessageHandle message, 396 const struct MojoSerializeMessageOptions* options); 397 398 // Appends data to a message object. 399 // 400 // |message|: The message. 401 // |additional_payload_size|: The number of bytes by which to extend the payload 402 // of the message. 403 // |handles|: Handles to be appended to the message. May be null iff 404 // |num_handles| is 0. 405 // |num_handles|: The number of handles to be appended to the message. 406 // 407 // |options| may be null. 408 // 409 // If this call succeeds, |*buffer| will contain the address of the data's 410 // storage if |buffer| was non-null, and if |buffer_size| was non-null then 411 // |*buffer_size| will contain the storage capacity. The caller may write 412 // message contents here. 413 // 414 // Note that while the size of the returned buffer may exceed the total 415 // requested size accumulated over one or more calls to 416 // |MojoAppendMessageData()|, only the extent of caller's requested capacity is 417 // considered to be part of the message. 418 // 419 // A message with attached data must have its capacity finalized before it can 420 // be transmitted by calling this function with 421 // |MOJO_APPEND_MESSAGE_DATA_FLAG_COMMIT_SIZE| set in |options->flags|. Note 422 // that even after this happens, the returned |*buffer| remains valid and 423 // writable until the message is passed to either |MojoWriteMessage()| or 424 // |MojoDestroyMessage()|. 425 // 426 // Ownership of all handles in |handles| is transferred to the message object if 427 // and ONLY if this operation succeeds and returns |MOJO_RESULT_OK|. Otherwise 428 // the caller retains ownership. 429 // 430 // Returns: 431 // |MOJO_RESULT_OK| upon success. The message's data buffer and size are 432 // stored to |*buffer| and |*buffer_size| respectively. Any previously 433 // appended data remains intact but may be moved in the event that 434 // |*buffer| itself is moved. If 435 // |MOJO_APPEND_MESSAGE_DATA_FLAG_COMMIT_SIZE| was set in 436 // |options->flags|, the message is ready for transmission. 437 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message object; 438 // if |num_handles| is non-zero but |handles| is null; or if any handle in 439 // |handles| is invalid. 440 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if |additional_payload_size| or 441 // |num_handles| exceeds some implementation- or embedder-defined maximum. 442 // |MOJO_RESULT_FAILED_PRECONDITION| if |message| has a context attached. 443 // |MOJO_RESULT_BUSY| if one or more handles in |handles| is currently busy 444 // and unable to be serialized. 445 MOJO_SYSTEM_EXPORT MojoResult 446 MojoAppendMessageData(MojoMessageHandle message, 447 uint32_t payload_size, 448 const MojoHandle* handles, 449 uint32_t num_handles, 450 const struct MojoAppendMessageDataOptions* options, 451 void** buffer, 452 uint32_t* buffer_size); 453 454 // Retrieves data attached to a message object. 455 // 456 // |message|: The message. 457 // |num_bytes|: An output parameter which will receive the total size in bytes 458 // of the message's payload. 459 // |buffer|: An output parameter which will receive the address of a buffer 460 // containing exactly |*num_bytes| bytes of payload data. This buffer 461 // address is not owned by the caller and is only valid as long as the 462 // message handle in |message| is valid. 463 // |num_handles|: An input/output parameter. On input, if not null, this points 464 // to value specifying the available capacity (in number of handles) of 465 // |handles|. On output, if not null, this will point to a value specifying 466 // the actual number of handles available in the serialized message. 467 // |handles|: A buffer to contain up to (input) |*num_handles| handles. May be 468 // null if |num_handles| is null or |*num_handles| is 0. 469 // 470 // |options| may be null. 471 // 472 // Returns: 473 // |MOJO_RESULT_OK| if |message| is a message with data attached and the 474 // provided handle storage is sufficient to contain all handles attached 475 // to the message. If |MOJO_GET_MESSAGE_DATA_FLAG_IGNORE_HANDLES| was set 476 // in |options->flags|, any attached handles in the message are left 477 // intact and both |handles| and |num_handles| are ignored. Otherwise 478 // ownership of any attached handles is transferred to the caller, and 479 // |MojoGetMessageData()| may no longer be called on |message| unless 480 // |MOJO_GET_MESSAGE_DATA_FLAG_IGNORE_HANDLES| is used. 481 // |MOJO_RESULT_INVALID_ARGUMENT| if |num_handles| is non-null and 482 // |*num_handles| is non-zero, but |handles| is null; or if |message| is 483 // not a valid message handle. 484 // |MOJO_RESULT_FAILED_PRECONDITION| if |message| is not a fully serialized 485 // message. The caller may use |MojoSerializeMessage()| and try again, 486 // or |MojoAppendMessageData()| with the 487 // |MOJO_APPEND_MESSAGE_DATA_FLAG_COMMIT_SIZE| flag set to complete a 488 // partially serialized |message|. 489 // |MOJO_RESULT_NOT_FOUND| if the message's handles (if any) contents have 490 // already been extracted (or have failed to be extracted) by a previous 491 // call to |MojoGetMessageData()|. 492 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if |num_handles| is null and there are 493 // handles attached to the message, or if |*num_handles| on input is less 494 // than the number of handles attached to the message. Also may be 495 // returned if |num_bytes| or |buffer| is null and the message has a non- 496 // empty payload. Note that if |MOJO_GET_MESSAGE_DATA_FLAG_IGNORE_HANDLES| 497 // is set in |options->flags| any current or previously attached handles 498 // are ignored. 499 // |MOJO_RESULT_ARBORTED| if the message is in an invalid state and its data 500 // and/or handles are unrecoverable. The message is left in this state but 501 // future calls to this API will yield the same result. 502 MOJO_SYSTEM_EXPORT MojoResult 503 MojoGetMessageData(MojoMessageHandle message, 504 const struct MojoGetMessageDataOptions* options, 505 void** buffer, 506 uint32_t* num_bytes, 507 MojoHandle* handles, 508 uint32_t* num_handles); 509 510 // Sets an opaque context value on a message object. The presence of an opaque 511 // context is mutually exclusive to the presence of message data. 512 // 513 // |context| is the context value to associate with this message, and 514 // |serializer| is a function which may be called at some later time to convert 515 // |message| to a serialized message object using |context| as input. See 516 // |MojoMessageContextSerializer| for more details. |destructor| is a function 517 // which may be called to allow the user to cleanup state associated with 518 // |context| after serialization or in the event that the message is destroyed 519 // without ever being serialized. 520 // 521 // Typically a caller will use |context| as an opaque pointer to some heap 522 // object which is effectively owned by the message once this returns. In this 523 // way, messages can be sent over a message pipe to a peer endpoint in the same 524 // process as the sender without performing a serialization step. 525 // 526 // If the message does need to cross a process boundary or is otherwise 527 // forced to serialize (see |MojoSerializeMessage()| below), it will be 528 // serialized by invoking |serializer|. 529 // 530 // If |serializer| is null, the created message cannot be serialized. Subsequent 531 // calls to |MojoSerializeMessage()| on the created message, or any attempt to 532 // transmit the message across a process boundary, will fail. 533 // 534 // If |destructor| is null, it is assumed that no cleanup is required after 535 // serializing or destroying a message with |context| attached. 536 // 537 // A |context| value of zero is invalid, and setting this on a message 538 // effectively removes its context. This is necessary if the caller wishes to 539 // attach data to the message or if the caller wants to destroy the message 540 // object without triggering |destructor|. 541 // 542 // |options| may be null. 543 // 544 // Returns: 545 // |MOJO_RESULT_OK| if the opaque context value was successfully set. 546 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message object; 547 // |options| is non-null and |*options| contains one or more malformed 548 // fields; or |context| is zero but either |serializer| or |destructor| is 549 // non-null. 550 // |MOJO_RESULT_ALREADY_EXISTS| if |message| already has a non-zero context. 551 // |MOJO_RESULT_FAILED_PRECONDITION| if |message| has data attached. 552 MOJO_SYSTEM_EXPORT MojoResult 553 MojoSetMessageContext(MojoMessageHandle message, 554 uintptr_t context, 555 MojoMessageContextSerializer serializer, 556 MojoMessageContextDestructor destructor, 557 const struct MojoSetMessageContextOptions* options); 558 559 // Retrieves a message object's opaque context value. 560 // 561 // |options| may be null. 562 // 563 // Returns: 564 // |MOJO_RESULT_OK| if |message| is a valid message object. |*context| 565 // contains its opaque context value, which may be zero. 566 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message object 567 // or |options| is non-null and |*options| contains one or more malformed 568 // fields. 569 MOJO_SYSTEM_EXPORT MojoResult 570 MojoGetMessageContext(MojoMessageHandle message, 571 const struct MojoGetMessageContextOptions* options, 572 uintptr_t* context); 573 574 // Notifies the system that a bad message was received on a message pipe, 575 // according to whatever criteria the caller chooses. This ultimately tries to 576 // notify the embedder about the bad message, and the embedder may enforce some 577 // policy for dealing with the source of the message (e.g. close the pipe, 578 // terminate, a process, etc.) The embedder may not be notified if the calling 579 // process has lost its connection to the source process. 580 // 581 // |message|: The message to report as bad. 582 // |error|: An error string which may provide the embedder with context when 583 // notified of this error. 584 // |error_num_bytes|: The length of |error| in bytes. 585 // 586 // |options| may be null. 587 // 588 // Returns: 589 // |MOJO_RESULT_OK| if successful. 590 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message. 591 MOJO_SYSTEM_EXPORT MojoResult 592 MojoNotifyBadMessage(MojoMessageHandle message, 593 const char* error, 594 uint32_t error_num_bytes, 595 const struct MojoNotifyBadMessageOptions* options); 596 597 #ifdef __cplusplus 598 } // extern "C" 599 #endif 600 601 #endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ 602