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(IsAlignedForChannelMessage(sizeof(BrokerMessageHeader)),
27 "Invalid header size.");
28
29 struct BufferRequestData {
30 uint32_t size;
31 };
32
33 #if defined(OS_WIN)
34 struct InitData {
35 // NOTE: InitData in the payload is followed by string16 data with exactly
36 // |pipe_name_length| wide characters (i.e., |pipe_name_length|*2 bytes.)
37 // This applies to Windows only.
38 uint32_t pipe_name_length;
39 };
40 #endif
41
42 #pragma pack(pop)
43
44 template <typename T>
45 inline Channel::MessagePtr CreateBrokerMessage(
46 BrokerMessageType type,
47 size_t num_handles,
48 size_t extra_data_size,
49 T** out_message_data,
50 void** out_extra_data = nullptr) {
51 const size_t message_size = sizeof(BrokerMessageHeader) +
52 sizeof(**out_message_data) + extra_data_size;
53 Channel::MessagePtr message(new Channel::Message(message_size, num_handles));
54 BrokerMessageHeader* header =
55 reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
56 header->type = type;
57 header->padding = 0;
58 *out_message_data = reinterpret_cast<T*>(header + 1);
59 if (out_extra_data)
60 *out_extra_data = *out_message_data + 1;
61 return message;
62 }
63
CreateBrokerMessage(BrokerMessageType type,size_t num_handles,std::nullptr_t ** dummy_out_data)64 inline Channel::MessagePtr CreateBrokerMessage(
65 BrokerMessageType type,
66 size_t num_handles,
67 std::nullptr_t** dummy_out_data) {
68 Channel::MessagePtr message(
69 new Channel::Message(sizeof(BrokerMessageHeader), num_handles));
70 BrokerMessageHeader* header =
71 reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
72 header->type = type;
73 header->padding = 0;
74 return message;
75 }
76
77 } // namespace edk
78 } // namespace mojo
79
80 #endif // MOJO_EDK_SYSTEM_BROKER_MESSAGES_H_
81