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 #ifndef MOJO_EDK_SYSTEM_BROKER_MESSAGES_H_
6 #define MOJO_EDK_SYSTEM_BROKER_MESSAGES_H_
7
8 #include "mojo/edk/system/channel.h"
9
10 namespace mojo {
11 namespace edk {
12
13 #pragma pack(push, 1)
14
15 enum BrokerMessageType : uint32_t {
16 INIT,
17 BUFFER_REQUEST,
18 BUFFER_RESPONSE,
19 };
20
21 struct BrokerMessageHeader {
22 BrokerMessageType type;
23 uint32_t padding;
24 };
25
26 static_assert(sizeof(BrokerMessageHeader) % kChannelMessageAlignment == 0,
27 "Invalid header size.");
28
29 struct BufferRequestData {
30 uint32_t size;
31 };
32
33 #pragma pack(pop)
34
35 template <typename T>
CreateBrokerMessage(BrokerMessageType type,size_t num_handles,T ** out_message_data)36 inline Channel::MessagePtr CreateBrokerMessage(BrokerMessageType type,
37 size_t num_handles,
38 T** out_message_data) {
39 const size_t message_size = sizeof(BrokerMessageHeader) + sizeof(T);
40 Channel::MessagePtr message(new Channel::Message(message_size, num_handles));
41 BrokerMessageHeader* header =
42 reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
43 header->type = type;
44 header->padding = 0;
45 *out_message_data = reinterpret_cast<T*>(header + 1);
46 return message;
47 }
48
CreateBrokerMessage(BrokerMessageType type,size_t num_handles,std::nullptr_t ** dummy_out_data)49 inline Channel::MessagePtr CreateBrokerMessage(
50 BrokerMessageType type,
51 size_t num_handles,
52 std::nullptr_t** dummy_out_data) {
53 Channel::MessagePtr message(
54 new Channel::Message(sizeof(BrokerMessageHeader), num_handles));
55 BrokerMessageHeader* header =
56 reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
57 header->type = type;
58 header->padding = 0;
59 return message;
60 }
61
62 } // namespace edk
63 } // namespace mojo
64
65 #endif // MOJO_EDK_SYSTEM_BROKER_MESSAGES_H_
66