• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CORE_BROKER_MESSAGES_H_
6 #define MOJO_CORE_BROKER_MESSAGES_H_
7 
8 #include "build/build_config.h"
9 #include "mojo/core/channel.h"
10 
11 namespace mojo {
12 namespace core {
13 
14 #pragma pack(push, 1)
15 
16 enum BrokerMessageType : uint32_t {
17   INIT,
18   BUFFER_REQUEST,
19   BUFFER_RESPONSE,
20 };
21 
22 struct BrokerMessageHeader {
23   BrokerMessageType type;
24   uint32_t padding;
25 };
26 
27 static_assert(IsAlignedForChannelMessage(sizeof(BrokerMessageHeader)),
28               "Invalid header size.");
29 
30 struct BufferRequestData {
31   uint32_t size;
32 };
33 
34 struct BufferResponseData {
35   uint64_t guid_high;
36   uint64_t guid_low;
37 };
38 
39 #if defined(OS_WIN)
40 struct InitData {
41   // NOTE: InitData in the payload is followed by string16 data with exactly
42   // |pipe_name_length| wide characters (i.e., |pipe_name_length|*2 bytes.)
43   // This applies to Windows only.
44   uint32_t pipe_name_length;
45 };
46 #endif
47 
48 #pragma pack(pop)
49 
50 template <typename T>
GetBrokerMessageData(Channel::Message * message,T ** out_data)51 inline bool GetBrokerMessageData(Channel::Message* message, T** out_data) {
52   const size_t required_size = sizeof(BrokerMessageHeader) + sizeof(T);
53   if (message->payload_size() < required_size)
54     return false;
55 
56   auto* header = static_cast<BrokerMessageHeader*>(message->mutable_payload());
57   *out_data = reinterpret_cast<T*>(header + 1);
58   return true;
59 }
60 
61 template <typename T>
62 inline Channel::MessagePtr CreateBrokerMessage(
63     BrokerMessageType type,
64     size_t num_handles,
65     size_t extra_data_size,
66     T** out_message_data,
67     void** out_extra_data = nullptr) {
68   const size_t message_size = sizeof(BrokerMessageHeader) +
69                               sizeof(**out_message_data) + extra_data_size;
70   Channel::MessagePtr message(new Channel::Message(message_size, num_handles));
71   BrokerMessageHeader* header =
72       reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
73   header->type = type;
74   header->padding = 0;
75   *out_message_data = reinterpret_cast<T*>(header + 1);
76   if (out_extra_data)
77     *out_extra_data = *out_message_data + 1;
78   return message;
79 }
80 
CreateBrokerMessage(BrokerMessageType type,size_t num_handles,std::nullptr_t ** dummy_out_data)81 inline Channel::MessagePtr CreateBrokerMessage(
82     BrokerMessageType type,
83     size_t num_handles,
84     std::nullptr_t** dummy_out_data) {
85   Channel::MessagePtr message(
86       new Channel::Message(sizeof(BrokerMessageHeader), num_handles));
87   BrokerMessageHeader* header =
88       reinterpret_cast<BrokerMessageHeader*>(message->mutable_payload());
89   header->type = type;
90   header->padding = 0;
91   return message;
92 }
93 
94 }  // namespace core
95 }  // namespace mojo
96 
97 #endif  // MOJO_CORE_BROKER_MESSAGES_H_
98