1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_BINDINGS_LIB_MESSAGE_H_ 6 #define MOJO_PUBLIC_BINDINGS_LIB_MESSAGE_H_ 7 8 #include <vector> 9 10 #include "mojo/public/system/core_cpp.h" 11 12 namespace mojo { 13 14 #pragma pack(push, 1) 15 16 struct MessageHeader { 17 uint32_t num_bytes; 18 uint32_t name; 19 }; 20 MOJO_COMPILE_ASSERT(sizeof(MessageHeader) == 8, bad_sizeof_MessageHeader); 21 22 struct MessageData { 23 MessageHeader header; 24 uint8_t payload[1]; 25 }; 26 MOJO_COMPILE_ASSERT(sizeof(MessageData) == 9, bad_sizeof_MessageData); 27 28 #pragma pack(pop) 29 30 // Message is a holder for the data and handles to be sent over a MessagePipe. 31 // Message owns its data and handles, but a consumer of Message is free to 32 // manipulate the data and handles members. 33 class Message { 34 public: 35 Message(); 36 ~Message(); 37 38 void Swap(Message* other); 39 40 MessageData* data; // Heap-allocated using malloc. 41 // TODO(davemoore): Turn these into ScopedHandles and fix bindings generation. 42 std::vector<Handle> handles; 43 44 private: 45 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); 46 }; 47 48 class MessageReceiver { 49 public: 50 // The receiver may mutate the given message or take ownership of its 51 // |message->data| member by setting it to NULL. Returns true if the message 52 // was accepted and false otherwise, indicating that the message was invalid 53 // or malformed. 54 virtual bool Accept(Message* message) = 0; 55 }; 56 57 } // namespace mojo 58 59 #endif // MOJO_PUBLIC_BINDINGS_LIB_MESSAGE_H_ 60