1 // Copyright (c) 2011 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.
5 // Defining IPC Messages
7 // Your IPC messages will be defined by macros inside of an XXX_messages.h
8 // header file. Most of the time, the system can automatically generate all
9 // of messaging mechanism from these definitions, but sometimes some manual
10 // coding is required. In these cases, you will also have an XXX_messages.cc
11 // implemation file as well.
13 // The senders of your messages will include your XXX_messages.h file to
14 // get the full set of definitions they need to send your messages.
16 // Each XXX_messages.h file must be registered with the IPC system. This
17 // requires adding two things:
18 // - An XXXMsgStart value to the IPCMessageStart enum in ipc_message_utils.h
19 // - An inclusion of XXX_messages.h file in a message generator .h file
21 // The XXXMsgStart value is an enumeration that ensures uniqueness for
22 // each different message file. Later, you will use this inside your
23 // XXX_messages.h file before invoking message declatation macros:
24 // #define IPC_MESSAGE_START XXXMsgStart
25 // ( ... your macro invocations go here ... )
27 // Message Generator Files
29 // A message generator .h header file pulls in all other message-declaring
30 // headers for a given component. It is included by a message generator
31 // .cc file, which is where all the generated code will wind up. Typically,
32 // you will use an existing generator (e.g. common_message_generator.cc
33 // in /chrome/common), but there are circumstances where you may add a
36 // In the rare cicrucmstances where you can't re-use an existing file,
37 // your YYY_message_generator.cc file for a component YYY would contain
38 // the following code:
39 // // Get basic type definitions.
40 // #define IPC_MESSAGE_IMPL
41 // #include "path/to/YYY_message_generator.h"
42 // // Generate constructors.
43 // #include "ipc/struct_constructor_macros.h"
44 // #include "path/to/YYY_message_generator.h"
45 // // Generate destructors.
46 // #include "ipc/struct_destructor_macros.h"
47 // #include "path/to/YYY_message_generator.h"
48 // // Generate param traits write methods.
49 // #include "ipc/param_traits_write_macros.h"
51 // #include "path/to/YYY_message_generator.h"
53 // // Generate param traits read methods.
54 // #include "ipc/param_traits_read_macros.h"
56 // #include "path/to/YYY_message_generator.h"
58 // // Generate param traits log methods.
59 // #include "ipc/param_traits_log_macros.h"
61 // #include "path/to/YYY_message_generator.h"
64 // In cases where manual generation is required, in your XXX_messages.cc
65 // file, put the following after all the includes for param types:
66 // #define IPC_MESSAGE_IMPL
67 // #include "XXX_messages.h"
68 // (... implementation of traits not auto-generated ...)
72 // The XXX_messages.h file will be multiply-included by the
73 // YYY_message_generator.cc file, so your XXX_messages file can't be
74 // guarded in the usual manner. Ideally, there will be no need for any
75 // inclusion guard, since the XXX_messages.h file should consist soley
76 // of inclusions of other headers (which are self-guarding) and IPC
77 // macros (which are multiply evaluating).
79 // Note that there is no #pragma once either; doing so would mark the whole
80 // file as being singly-included. Since your XXX_messages.h file is only
81 // partially-guarded, care must be taken to ensure that it is only included
82 // by other .cc files (and the YYY_message_generator.h file). Including an
83 // XXX_messages.h file in some other .h file may result in duplicate
84 // declarations and a compilation failure.
88 // It is generally a bad idea to have type definitions in a XXX_messages.h
89 // file; most likely the typedef will then be used in the message, as opposed
90 // to the struct iself. Later, an IPC message dispatcher wil need to call
91 // a function taking that type, and that function is declared in some other
92 // header. Thus, in order to get the type definition, the other header
93 // would have to include the XXX_messages.h file, violating the rule above
94 // about not including XXX_messages.h file in other .h files.
96 // One approach here is to move these type definitions to another (guarded)
97 // .h file and include this second .h in your XXX_messages.h file. This
98 // is still less than ideal, because the dispatched function would have to
99 // redeclare the typedef or include this second header. This may be
100 // reasonable in a few cases.
102 // Failing all of the above, then you will want to bracket the smallest
103 // possible section of your XXX_messages.h file containing these types
104 // with an include guard macro. Be aware that providing an incomplete
105 // class type declaration to avoid pulling in a long chain of headers is
106 // acceptable when your XXX_messages.h header is being included by the
107 // message sending caller's code, but not when the YYY_message_generator.c
108 // is building the messages. In addtion, due to the multiple inclusion
109 // restriction, these type ought to be guarded. Follow a convention like:
110 // #ifndef SOME_GUARD_MACRO
111 // #define SOME_GUARD_MACRO
112 // class some_class; // One incomplete class declaration
113 // class_some_other_class; // Another incomplete class declaration
114 // #endif // SOME_GUARD_MACRO
115 // #ifdef IPC_MESSAGE_IMPL
116 // #inlcude "path/to/some_class.h" // Full class declaration
117 // #inlcude "path/to/some_other_class.h" // Full class declaration
118 // #endif // IPC_MESSAGE_IMPL
119 // (.. IPC macros using some_class and some_other_class ...)
123 // You will use IPC message macro invocations for three things:
124 // - New struct definitions for IPC
125 // - Registering existing struct and enum definitions with IPC
126 // - Defining the messages themselves
128 // New structs are defined with IPC_STRUCT_BEGIN(), IPC_STRUCT_MEMBER(),
129 // IPC_STRUCT_END() family of macros. These cause the XXX_messages.h
130 // to proclaim equivalent struct declarations for use by callers, as well
131 // as later registering the type with the message generation. Note that
132 // IPC_STRUCT_MEMBER() is only permitted inside matching calls to
133 // IPC_STRUCT_BEGIN() / IPC_STRUCT_END().
135 // Externally-defined structs are registered with IPC_STRUCT_TRAITS_BEGIN(),
136 // IPC_STRUCT_TRAITS_MEMBER(), and IPC_STRUCT_TRAITS_END() macros. These
137 // cause registration of the types with message generation only.
138 // There's also IPC_STRUCT_TRAITS_PARENT, which is used to register a parent
139 // class (whose own traits are already defined). Note that
140 // IPC_STRUCT_TRAITS_MEMBER() and IPC_STRUCT_TRAITS_PARENT are only permitted
141 // inside matching calls to IPC_STRUCT_TRAITS_BEGIN() /
142 // IPC_STRUCT_TRAITS_END().
144 // Enum types are registered with a single IPC_ENUM_TRAITS() macro. There
145 // is no need to enumerate each value to the IPC mechanism.
147 // Do not place semicolons following these IPC_ macro invocations. There
148 // is no reason to expect that their expansion corresponds one-to-one with
151 // Once the types have been declared / registered, message definitions follow.
152 // "Sync" messages are just synchronous calls, the Send() call doesn't return
153 // until a reply comes back. Input parameters are first (const TYPE&), and
154 // To declare a sync message, use the IPC_SYNC_ macros. The numbers at the
155 // end show how many input/output parameters there are (i.e. 1_2 is 1 in, 2
156 // out). The caller does a Send([route id, ], in1, &out1, &out2).
157 // The receiver's handler function will be
158 // void OnSyncMessageName(const type1& in1, type2* out1, type3* out2)
160 // A caller can also send a synchronous message, while the receiver can respond
161 // at a later time. This is transparent from the sender's side. The receiver
162 // needs to use a different handler that takes in a IPC::Message* as the output
163 // type, stash the message, and when it has the data it can Send the message.
165 // Use the IPC_MESSAGE_HANDLER_DELAY_REPLY macro instead of IPC_MESSAGE_HANDLER
166 // IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_SyncMessageName,
167 // OnSyncMessageName)
169 // The handler function will look like:
170 // void OnSyncMessageName(const type1& in1, IPC::Message* reply_msg);
172 // Receiver stashes the IPC::Message* pointer, and when it's ready, it does:
173 // ViewHostMsg_SyncMessageName::WriteReplyParams(reply_msg, out1, out2);
176 #ifndef IPC_IPC_MESSAGE_MACROS_H_
177 #define IPC_IPC_MESSAGE_MACROS_H_
179 #include "ipc/ipc_message_utils.h"
180 #include "ipc/param_traits_macros.h"
182 #if defined(IPC_MESSAGE_IMPL)
183 #include "ipc/ipc_message_utils_impl.h"
186 // Macros for defining structs. May be subsequently redefined.
187 #define IPC_STRUCT_BEGIN(struct_name) \
188 struct struct_name; \
189 IPC_STRUCT_TRAITS_BEGIN(struct_name) \
190 IPC_STRUCT_TRAITS_END() \
191 struct struct_name : IPC::NoParams { \
194 #define IPC_STRUCT_MEMBER(type, name) type name;
195 #define IPC_STRUCT_END() };
197 // Message macros collect specific numbers of arguments and funnel them into
198 // the common message generation macro. These should never be redefined.
199 #define IPC_MESSAGE_CONTROL0(msg_class) \
200 IPC_MESSAGE_DECL(EMPTY, CONTROL, msg_class, 0, 0, (), ())
202 #define IPC_MESSAGE_CONTROL1(msg_class, type1) \
203 IPC_MESSAGE_DECL(ASYNC, CONTROL, msg_class, 1, 0, (type1), ())
205 #define IPC_MESSAGE_CONTROL2(msg_class, type1, type2) \
206 IPC_MESSAGE_DECL(ASYNC, CONTROL, msg_class, 2, 0, (type1, type2), ())
208 #define IPC_MESSAGE_CONTROL3(msg_class, type1, type2, type3) \
209 IPC_MESSAGE_DECL(ASYNC, CONTROL, msg_class, 3, 0, (type1, type2, type3), ())
211 #define IPC_MESSAGE_CONTROL4(msg_class, type1, type2, type3, type4) \
212 IPC_MESSAGE_DECL(ASYNC, CONTROL, msg_class, 4, 0, (type1, type2, type3, type4), ())
214 #define IPC_MESSAGE_CONTROL5(msg_class, type1, type2, type3, type4, type5) \
215 IPC_MESSAGE_DECL(ASYNC, CONTROL, msg_class, 5, 0, (type1, type2, type3, type4, type5), ())
217 #define IPC_MESSAGE_ROUTED0(msg_class) \
218 IPC_MESSAGE_DECL(EMPTY, ROUTED, msg_class, 0, 0, (), ())
220 #define IPC_MESSAGE_ROUTED1(msg_class, type1) \
221 IPC_MESSAGE_DECL(ASYNC, ROUTED, msg_class, 1, 0, (type1), ())
223 #define IPC_MESSAGE_ROUTED2(msg_class, type1, type2) \
224 IPC_MESSAGE_DECL(ASYNC, ROUTED, msg_class, 2, 0, (type1, type2), ())
226 #define IPC_MESSAGE_ROUTED3(msg_class, type1, type2, type3) \
227 IPC_MESSAGE_DECL(ASYNC, ROUTED, msg_class, 3, 0, (type1, type2, type3), ())
229 #define IPC_MESSAGE_ROUTED4(msg_class, type1, type2, type3, type4) \
230 IPC_MESSAGE_DECL(ASYNC, ROUTED, msg_class, 4, 0, (type1, type2, type3, type4), ())
232 #define IPC_MESSAGE_ROUTED5(msg_class, type1, type2, type3, type4, type5) \
233 IPC_MESSAGE_DECL(ASYNC, ROUTED, msg_class, 5, 0, (type1, type2, type3, type4, type5), ())
235 #define IPC_SYNC_MESSAGE_CONTROL0_0(msg_class) \
236 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 0, 0, (), ())
238 #define IPC_SYNC_MESSAGE_CONTROL0_1(msg_class, type1_out) \
239 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 0, 1, (), (type1_out))
241 #define IPC_SYNC_MESSAGE_CONTROL0_2(msg_class, type1_out, type2_out) \
242 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 0, 2, (), (type1_out, type2_out))
244 #define IPC_SYNC_MESSAGE_CONTROL0_3(msg_class, type1_out, type2_out, type3_out) \
245 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 0, 3, (), (type1_out, type2_out, type3_out))
247 #define IPC_SYNC_MESSAGE_CONTROL0_4(msg_class, type1_out, type2_out, type3_out, type4_out) \
248 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 0, 4, (), (type1_out, type2_out, type3_out, type4_out))
250 #define IPC_SYNC_MESSAGE_CONTROL1_0(msg_class, type1_in) \
251 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 1, 0, (type1_in), ())
253 #define IPC_SYNC_MESSAGE_CONTROL1_1(msg_class, type1_in, type1_out) \
254 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 1, 1, (type1_in), (type1_out))
256 #define IPC_SYNC_MESSAGE_CONTROL1_2(msg_class, type1_in, type1_out, type2_out) \
257 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 1, 2, (type1_in), (type1_out, type2_out))
259 #define IPC_SYNC_MESSAGE_CONTROL1_3(msg_class, type1_in, type1_out, type2_out, type3_out) \
260 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 1, 3, (type1_in), (type1_out, type2_out, type3_out))
262 #define IPC_SYNC_MESSAGE_CONTROL1_4(msg_class, type1_in, type1_out, type2_out, type3_out, type4_out) \
263 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 1, 4, (type1_in), (type1_out, type2_out, type3_out, type4_out))
265 #define IPC_SYNC_MESSAGE_CONTROL2_0(msg_class, type1_in, type2_in) \
266 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 2, 0, (type1_in, type2_in), ())
268 #define IPC_SYNC_MESSAGE_CONTROL2_1(msg_class, type1_in, type2_in, type1_out) \
269 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 2, 1, (type1_in, type2_in), (type1_out))
271 #define IPC_SYNC_MESSAGE_CONTROL2_2(msg_class, type1_in, type2_in, type1_out, type2_out) \
272 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 2, 2, (type1_in, type2_in), (type1_out, type2_out))
274 #define IPC_SYNC_MESSAGE_CONTROL2_3(msg_class, type1_in, type2_in, type1_out, type2_out, type3_out) \
275 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 2, 3, (type1_in, type2_in), (type1_out, type2_out, type3_out))
277 #define IPC_SYNC_MESSAGE_CONTROL2_4(msg_class, type1_in, type2_in, type1_out, type2_out, type3_out, type4_out) \
278 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 2, 4, (type1_in, type2_in), (type1_out, type2_out, type3_out, type4_out))
280 #define IPC_SYNC_MESSAGE_CONTROL3_0(msg_class, type1_in, type2_in, type3_in) \
281 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 3, 0, (type1_in, type2_in, type3_in), ())
283 #define IPC_SYNC_MESSAGE_CONTROL3_1(msg_class, type1_in, type2_in, type3_in, type1_out) \
284 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 3, 1, (type1_in, type2_in, type3_in), (type1_out))
286 #define IPC_SYNC_MESSAGE_CONTROL3_2(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out) \
287 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 3, 2, (type1_in, type2_in, type3_in), (type1_out, type2_out))
289 #define IPC_SYNC_MESSAGE_CONTROL3_3(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out, type3_out) \
290 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 3, 3, (type1_in, type2_in, type3_in), (type1_out, type2_out, type3_out))
292 #define IPC_SYNC_MESSAGE_CONTROL3_4(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out, type3_out, type4_out) \
293 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 3, 4, (type1_in, type2_in, type3_in), (type1_out, type2_out, type3_out, type4_out))
295 #define IPC_SYNC_MESSAGE_CONTROL4_0(msg_class, type1_in, type2_in, type3_in, type4_in) \
296 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 4, 0, (type1_in, type2_in, type3_in, type4_in), ())
298 #define IPC_SYNC_MESSAGE_CONTROL4_1(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out) \
299 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 4, 1, (type1_in, type2_in, type3_in, type4_in), (type1_out))
301 #define IPC_SYNC_MESSAGE_CONTROL4_2(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out) \
302 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 4, 2, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out))
304 #define IPC_SYNC_MESSAGE_CONTROL4_3(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out, type3_out) \
305 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 4, 3, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out, type3_out))
307 #define IPC_SYNC_MESSAGE_CONTROL4_4(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out, type3_out, type4_out) \
308 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 4, 4, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out, type3_out, type4_out))
310 #define IPC_SYNC_MESSAGE_CONTROL5_0(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in) \
311 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 5, 0, (type1_in, type2_in, type3_in, type4_in, type5_in), ())
313 #define IPC_SYNC_MESSAGE_CONTROL5_1(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out) \
314 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 5, 1, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out))
316 #define IPC_SYNC_MESSAGE_CONTROL5_2(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out) \
317 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 5, 2, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out))
319 #define IPC_SYNC_MESSAGE_CONTROL5_3(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out, type3_out) \
320 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 5, 3, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out, type3_out))
322 #define IPC_SYNC_MESSAGE_CONTROL5_4(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out, type3_out, type4_out) \
323 IPC_MESSAGE_DECL(SYNC, CONTROL, msg_class, 5, 4, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out, type3_out, type4_out))
325 #define IPC_SYNC_MESSAGE_ROUTED0_0(msg_class) \
326 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 0, 0, (), ())
328 #define IPC_SYNC_MESSAGE_ROUTED0_1(msg_class, type1_out) \
329 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 0, 1, (), (type1_out))
331 #define IPC_SYNC_MESSAGE_ROUTED0_2(msg_class, type1_out, type2_out) \
332 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 0, 2, (), (type1_out, type2_out))
334 #define IPC_SYNC_MESSAGE_ROUTED0_3(msg_class, type1_out, type2_out, type3_out) \
335 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 0, 3, (), (type1_out, type2_out, type3_out))
337 #define IPC_SYNC_MESSAGE_ROUTED0_4(msg_class, type1_out, type2_out, type3_out, type4_out) \
338 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 0, 4, (), (type1_out, type2_out, type3_out, type4_out))
340 #define IPC_SYNC_MESSAGE_ROUTED1_0(msg_class, type1_in) \
341 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 1, 0, (type1_in), ())
343 #define IPC_SYNC_MESSAGE_ROUTED1_1(msg_class, type1_in, type1_out) \
344 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 1, 1, (type1_in), (type1_out))
346 #define IPC_SYNC_MESSAGE_ROUTED1_2(msg_class, type1_in, type1_out, type2_out) \
347 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 1, 2, (type1_in), (type1_out, type2_out))
349 #define IPC_SYNC_MESSAGE_ROUTED1_3(msg_class, type1_in, type1_out, type2_out, type3_out) \
350 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 1, 3, (type1_in), (type1_out, type2_out, type3_out))
352 #define IPC_SYNC_MESSAGE_ROUTED1_4(msg_class, type1_in, type1_out, type2_out, type3_out, type4_out) \
353 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 1, 4, (type1_in), (type1_out, type2_out, type3_out, type4_out))
355 #define IPC_SYNC_MESSAGE_ROUTED2_0(msg_class, type1_in, type2_in) \
356 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 2, 0, (type1_in, type2_in), ())
358 #define IPC_SYNC_MESSAGE_ROUTED2_1(msg_class, type1_in, type2_in, type1_out) \
359 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 2, 1, (type1_in, type2_in), (type1_out))
361 #define IPC_SYNC_MESSAGE_ROUTED2_2(msg_class, type1_in, type2_in, type1_out, type2_out) \
362 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 2, 2, (type1_in, type2_in), (type1_out, type2_out))
364 #define IPC_SYNC_MESSAGE_ROUTED2_3(msg_class, type1_in, type2_in, type1_out, type2_out, type3_out) \
365 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 2, 3, (type1_in, type2_in), (type1_out, type2_out, type3_out))
367 #define IPC_SYNC_MESSAGE_ROUTED2_4(msg_class, type1_in, type2_in, type1_out, type2_out, type3_out, type4_out) \
368 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 2, 4, (type1_in, type2_in), (type1_out, type2_out, type3_out, type4_out))
370 #define IPC_SYNC_MESSAGE_ROUTED3_0(msg_class, type1_in, type2_in, type3_in) \
371 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 3, 0, (type1_in, type2_in, type3_in), ())
373 #define IPC_SYNC_MESSAGE_ROUTED3_1(msg_class, type1_in, type2_in, type3_in, type1_out) \
374 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 3, 1, (type1_in, type2_in, type3_in), (type1_out))
376 #define IPC_SYNC_MESSAGE_ROUTED3_2(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out) \
377 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 3, 2, (type1_in, type2_in, type3_in), (type1_out, type2_out))
379 #define IPC_SYNC_MESSAGE_ROUTED3_3(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out, type3_out) \
380 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 3, 3, (type1_in, type2_in, type3_in), (type1_out, type2_out, type3_out))
382 #define IPC_SYNC_MESSAGE_ROUTED3_4(msg_class, type1_in, type2_in, type3_in, type1_out, type2_out, type3_out, type4_out) \
383 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 3, 4, (type1_in, type2_in, type3_in), (type1_out, type2_out, type3_out, type4_out))
385 #define IPC_SYNC_MESSAGE_ROUTED4_0(msg_class, type1_in, type2_in, type3_in, type4_in) \
386 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 4, 1, (type1_in, type2_in, type3_in, type4_in), ())
388 #define IPC_SYNC_MESSAGE_ROUTED4_1(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out) \
389 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 4, 1, (type1_in, type2_in, type3_in, type4_in), (type1_out))
391 #define IPC_SYNC_MESSAGE_ROUTED4_2(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out) \
392 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 4, 2, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out))
394 #define IPC_SYNC_MESSAGE_ROUTED4_3(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out, type3_out) \
395 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 4, 3, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out, type3_out))
397 #define IPC_SYNC_MESSAGE_ROUTED4_4(msg_class, type1_in, type2_in, type3_in, type4_in, type1_out, type2_out, type3_out, type4_out) \
398 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 4, 4, (type1_in, type2_in, type3_in, type4_in), (type1_out, type2_out, type3_out, type4_out))
400 #define IPC_SYNC_MESSAGE_ROUTED5_0(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in) \
401 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 5, 1, (type1_in, type2_in, type3_in, type4_in, type5_in), ())
403 #define IPC_SYNC_MESSAGE_ROUTED5_1(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out) \
404 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 5, 1, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out))
406 #define IPC_SYNC_MESSAGE_ROUTED5_2(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out) \
407 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 5, 2, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out))
409 #define IPC_SYNC_MESSAGE_ROUTED5_3(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out, type3_out) \
410 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 5, 3, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out, type3_out))
412 #define IPC_SYNC_MESSAGE_ROUTED5_4(msg_class, type1_in, type2_in, type3_in, type4_in, type5_in, type1_out, type2_out, type3_out, type4_out) \
413 IPC_MESSAGE_DECL(SYNC, ROUTED, msg_class, 5, 4, (type1_in, type2_in, type3_in, type4_in, type5_in), (type1_out, type2_out, type3_out, type4_out))
415 // Common message macro which dispatches into one of the 6 (sync x kind)
416 // routines. There is a way that these 6 cases can be lumped together,
417 // but the macros get very complicated in that case.
418 // Note: intended be redefined to generate other information.
419 #define IPC_MESSAGE_DECL(sync, kind, msg_class, \
420 in_cnt, out_cnt, in_list, out_list) \
421 IPC_##sync##_##kind##_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
422 IPC_MESSAGE_EXTRA(sync, kind, msg_class, in_cnt, out_cnt, in_list, out_list)
424 #define IPC_EMPTY_CONTROL_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
425 class msg_class : public IPC::Message { \
427 enum { ID = IPC_MESSAGE_ID() }; \
428 msg_class() : IPC::Message(MSG_ROUTING_CONTROL, ID, PRIORITY_NORMAL) {} \
431 #define IPC_EMPTY_ROUTED_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
432 class msg_class : public IPC::Message { \
434 enum { ID = IPC_MESSAGE_ID() }; \
435 msg_class(int32 routing_id) \
436 : IPC::Message(routing_id, ID, PRIORITY_NORMAL) {} \
439 #define IPC_ASYNC_CONTROL_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
441 public IPC::MessageWithTuple<IPC_TUPLE_IN_##in_cnt in_list> { \
443 enum { ID = IPC_MESSAGE_ID() }; \
444 msg_class(IPC_TYPE_IN_##in_cnt in_list); \
445 virtual ~msg_class(); \
446 static void Log(std::string* name, const Message* msg, std::string* l); \
449 #define IPC_ASYNC_ROUTED_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
451 public IPC::MessageWithTuple<IPC_TUPLE_IN_##in_cnt in_list> { \
453 enum { ID = IPC_MESSAGE_ID() }; \
454 msg_class(int32 routing_id IPC_COMMA_##in_cnt \
455 IPC_TYPE_IN_##in_cnt in_list); \
456 virtual ~msg_class(); \
457 static void Log(std::string* name, const Message* msg, std::string* l); \
460 #define IPC_SYNC_CONTROL_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
462 public IPC::MessageWithReply<IPC_TUPLE_IN_##in_cnt in_list, \
463 IPC_TUPLE_OUT_##out_cnt out_list> { \
465 enum { ID = IPC_MESSAGE_ID() }; \
466 msg_class(IPC_TYPE_IN_##in_cnt in_list \
467 IPC_COMMA_AND_##in_cnt(IPC_COMMA_##out_cnt) \
468 IPC_TYPE_OUT_##out_cnt out_list); \
469 virtual ~msg_class(); \
470 static void Log(std::string* name, const Message* msg, std::string* l); \
473 #define IPC_SYNC_ROUTED_DECL(msg_class, in_cnt, out_cnt, in_list, out_list) \
475 public IPC::MessageWithReply<IPC_TUPLE_IN_##in_cnt in_list, \
476 IPC_TUPLE_OUT_##out_cnt out_list> { \
478 enum { ID = IPC_MESSAGE_ID() }; \
479 msg_class(int32 routing_id \
480 IPC_COMMA_OR_##in_cnt(IPC_COMMA_##out_cnt) \
481 IPC_TYPE_IN_##in_cnt in_list \
482 IPC_COMMA_AND_##in_cnt(IPC_COMMA_##out_cnt) \
483 IPC_TYPE_OUT_##out_cnt out_list); \
484 virtual ~msg_class(); \
485 static void Log(std::string* name, const Message* msg, std::string* l); \
488 #if defined(IPC_MESSAGE_IMPL)
490 // "Implementation" inclusion produces constructors, destructors, and
491 // logging functions, except for the no-arg special cases, where the
492 // implementation occurs in the declaration, and there is no special
494 #define IPC_MESSAGE_EXTRA(sync, kind, msg_class, \
495 in_cnt, out_cnt, in_list, out_list) \
496 IPC_##sync##_##kind##_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list) \
497 IPC_##sync##_MESSAGE_LOG(msg_class)
499 #define IPC_EMPTY_CONTROL_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list)
500 #define IPC_EMPTY_ROUTED_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list)
502 #define IPC_ASYNC_CONTROL_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list) \
503 msg_class::msg_class(IPC_TYPE_IN_##in_cnt in_list) : \
504 IPC::MessageWithTuple<IPC_TUPLE_IN_##in_cnt in_list> \
505 (MSG_ROUTING_CONTROL, ID, IPC_NAME_IN_##in_cnt in_list) \
507 msg_class::~msg_class() {}
509 #define IPC_ASYNC_ROUTED_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list) \
510 msg_class::msg_class(int32 routing_id IPC_COMMA_##in_cnt \
511 IPC_TYPE_IN_##in_cnt in_list) : \
512 IPC::MessageWithTuple<IPC_TUPLE_IN_##in_cnt in_list> \
513 (routing_id, ID, IPC_NAME_IN_##in_cnt in_list) \
515 msg_class::~msg_class() {}
517 #define IPC_SYNC_CONTROL_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list) \
518 msg_class::msg_class(IPC_TYPE_IN_##in_cnt in_list \
519 IPC_COMMA_AND_##in_cnt(IPC_COMMA_##out_cnt) \
520 IPC_TYPE_OUT_##out_cnt out_list) : \
521 IPC::MessageWithReply<IPC_TUPLE_IN_##in_cnt in_list, \
522 IPC_TUPLE_OUT_##out_cnt out_list> \
523 (MSG_ROUTING_CONTROL, ID, \
524 IPC_NAME_IN_##in_cnt in_list, \
525 IPC_NAME_OUT_##out_cnt out_list) \
527 msg_class::~msg_class() {}
529 #define IPC_SYNC_ROUTED_IMPL(msg_class, in_cnt, out_cnt, in_list, out_list) \
530 msg_class::msg_class(int32 routing_id \
531 IPC_COMMA_OR_##in_cnt(IPC_COMMA_##out_cnt) \
532 IPC_TYPE_IN_##in_cnt in_list \
533 IPC_COMMA_AND_##in_cnt(IPC_COMMA_##out_cnt) \
534 IPC_TYPE_OUT_##out_cnt out_list) : \
535 IPC::MessageWithReply<IPC_TUPLE_IN_##in_cnt in_list, \
536 IPC_TUPLE_OUT_##out_cnt out_list> \
538 IPC_NAME_IN_##in_cnt in_list, \
539 IPC_NAME_OUT_##out_cnt out_list) \
541 msg_class::~msg_class() {}
543 #define IPC_EMPTY_MESSAGE_LOG(msg_class)
545 #define IPC_ASYNC_MESSAGE_LOG(msg_class) \
546 void msg_class::Log(std::string* name, \
547 const Message* msg, \
550 *name = #msg_class; \
555 IPC::LogParam(p, l); \
558 #define IPC_SYNC_MESSAGE_LOG(msg_class) \
559 void msg_class::Log(std::string* name, \
560 const Message* msg, \
563 *name = #msg_class; \
566 if (msg->is_sync()) { \
567 TupleTypes<SendParam>::ValueTuple p; \
568 if (ReadSendParam(msg, &p)) \
569 IPC::LogParam(p, l); \
570 AddOutputParamsToLog(msg, l); \
572 TupleTypes<ReplyParam>::ValueTuple p; \
573 if (ReadReplyParam(msg, &p)) \
574 IPC::LogParam(p, l); \
578 #elif defined(IPC_MESSAGE_MACROS_LOG_ENABLED)
580 #ifndef IPC_LOG_TABLE_CREATED
581 #define IPC_LOG_TABLE_CREATED
583 #include "base/hash_tables.h"
585 typedef void (*LogFunction
)(std::string
* name
,
586 const IPC::Message
* msg
,
587 std::string
* params
);
589 typedef base::hash_map
<uint32
, LogFunction
> LogFunctionMap
;
590 LogFunctionMap g_log_function_mapping
;
592 #endif // IPC_LOG_TABLE_CREATED
594 // "Log table" inclusion produces extra logging registration code.
595 #define IPC_MESSAGE_EXTRA(sync, kind, msg_class, \
596 in_cnt, out_cnt, in_list, out_list) \
597 class LoggerRegisterHelper##msg_class { \
599 LoggerRegisterHelper##msg_class() { \
600 g_log_function_mapping[msg_class::ID] = msg_class::Log; \
603 LoggerRegisterHelper##msg_class g_LoggerRegisterHelper##msg_class;
607 // Normal inclusion produces nothing extra.
608 #define IPC_MESSAGE_EXTRA(sync, kind, msg_class, \
609 in_cnt, out_cnt, in_list, out_list)
611 #endif // defined(IPC_MESSAGE_IMPL)
613 // Handle variable sized argument lists. These are usually invoked by token
614 // pasting against the argument counts.
615 #define IPC_TYPE_IN_0()
616 #define IPC_TYPE_IN_1(t1) const t1& arg1
617 #define IPC_TYPE_IN_2(t1, t2) const t1& arg1, const t2& arg2
618 #define IPC_TYPE_IN_3(t1, t2, t3) const t1& arg1, const t2& arg2, const t3& arg3
619 #define IPC_TYPE_IN_4(t1, t2, t3, t4) const t1& arg1, const t2& arg2, const t3& arg3, const t4& arg4
620 #define IPC_TYPE_IN_5(t1, t2, t3, t4, t5) const t1& arg1, const t2& arg2, const t3& arg3, const t4& arg4, const t5& arg5
622 #define IPC_TYPE_OUT_0()
623 #define IPC_TYPE_OUT_1(t1) t1* arg6
624 #define IPC_TYPE_OUT_2(t1, t2) t1* arg6, t2* arg7
625 #define IPC_TYPE_OUT_3(t1, t2, t3) t1* arg6, t2* arg7, t3* arg8
626 #define IPC_TYPE_OUT_4(t1, t2, t3, t4) t1* arg6, t2* arg7, t3* arg8, t4* arg9
628 #define IPC_TUPLE_IN_0() Tuple0
629 #define IPC_TUPLE_IN_1(t1) Tuple1<t1>
630 #define IPC_TUPLE_IN_2(t1, t2) Tuple2<t1, t2>
631 #define IPC_TUPLE_IN_3(t1, t2, t3) Tuple3<t1, t2, t3>
632 #define IPC_TUPLE_IN_4(t1, t2, t3, t4) Tuple4<t1, t2, t3, t4>
633 #define IPC_TUPLE_IN_5(t1, t2, t3, t4, t5) Tuple5<t1, t2, t3, t4, t5>
635 #define IPC_TUPLE_OUT_0() Tuple0
636 #define IPC_TUPLE_OUT_1(t1) Tuple1<t1&>
637 #define IPC_TUPLE_OUT_2(t1, t2) Tuple2<t1&, t2&>
638 #define IPC_TUPLE_OUT_3(t1, t2, t3) Tuple3<t1&, t2&, t3&>
639 #define IPC_TUPLE_OUT_4(t1, t2, t3, t4) Tuple4<t1&, t2&, t3&, t4&>
641 #define IPC_NAME_IN_0() MakeTuple()
642 #define IPC_NAME_IN_1(t1) MakeRefTuple(arg1)
643 #define IPC_NAME_IN_2(t1, t2) MakeRefTuple(arg1, arg2)
644 #define IPC_NAME_IN_3(t1, t2, t3) MakeRefTuple(arg1, arg2, arg3)
645 #define IPC_NAME_IN_4(t1, t2, t3, t4) MakeRefTuple(arg1, arg2, arg3, arg4)
646 #define IPC_NAME_IN_5(t1, t2, t3, t4, t5) MakeRefTuple(arg1, arg2, arg3, arg4, arg5)
648 #define IPC_NAME_OUT_0() MakeTuple()
649 #define IPC_NAME_OUT_1(t1) MakeRefTuple(*arg6)
650 #define IPC_NAME_OUT_2(t1, t2) MakeRefTuple(*arg6, *arg7)
651 #define IPC_NAME_OUT_3(t1, t2, t3) MakeRefTuple(*arg6, *arg7, *arg8)
652 #define IPC_NAME_OUT_4(t1, t2, t3, t4) MakeRefTuple(*arg6, *arg7, *arg8, *arg9)
654 // There are places where the syntax requires a comma if there are input args,
655 // if there are input args and output args, or if there are input args or
656 // output args. These macros allow generation of the comma as needed; invoke
657 // by token pasting against the argument counts.
659 #define IPC_COMMA_1 ,
660 #define IPC_COMMA_2 ,
661 #define IPC_COMMA_3 ,
662 #define IPC_COMMA_4 ,
663 #define IPC_COMMA_5 ,
665 #define IPC_COMMA_AND_0(x)
666 #define IPC_COMMA_AND_1(x) x
667 #define IPC_COMMA_AND_2(x) x
668 #define IPC_COMMA_AND_3(x) x
669 #define IPC_COMMA_AND_4(x) x
670 #define IPC_COMMA_AND_5(x) x
672 #define IPC_COMMA_OR_0(x) x
673 #define IPC_COMMA_OR_1(x) ,
674 #define IPC_COMMA_OR_2(x) ,
675 #define IPC_COMMA_OR_3(x) ,
676 #define IPC_COMMA_OR_4(x) ,
677 #define IPC_COMMA_OR_5(x) ,
680 // Note: we currently use __LINE__ to give unique IDs to messages within
681 // a file. They're globally unique since each file defines its own
682 // IPC_MESSAGE_START. Ideally, we wouldn't use line numbers (a possibility
683 // is to instead use the __COUNTER__ macro, but it needs gcc 4.3 and xcode
684 // doesn't use it yet).
685 #define IPC_MESSAGE_ID() ((IPC_MESSAGE_START << 16) + __LINE__)
686 #define IPC_MESSAGE_ID_CLASS(id) ((id) >> 16)
687 #define IPC_MESSAGE_ID_LINE(id) ((id) & 0xffff)
689 // Message crackers and handlers.
690 // Prefer to use the IPC_BEGIN_MESSAGE_MAP_EX to the older macros since they
691 // allow you to detect when a message could not be de-serialized. Usage:
693 // bool MyClass::OnMessageReceived(const IPC::Message& msg) {
694 // bool handled = true;
695 // bool msg_is_good = false;
696 // IPC_BEGIN_MESSAGE_MAP_EX(MyClass, msg, msg_is_good)
697 // IPC_MESSAGE_HANDLER(MsgClassOne, OnMsgClassOne)
698 // ...more handlers here ...
699 // IPC_MESSAGE_HANDLER(MsgClassTen, OnMsgClassTen)
700 // IPC_MESSAGE_UNHANDLED(handled = false)
701 // IPC_END_MESSAGE_MAP_EX()
702 // if (!msg_is_good) {
703 // // Signal error here or terminate offending process.
709 #define IPC_BEGIN_MESSAGE_MAP_EX(class_name, msg, msg_is_ok) \
711 typedef class_name _IpcMessageHandlerClass; \
712 const IPC::Message& ipc_message__ = msg; \
713 bool& msg_is_ok__ = msg_is_ok; \
714 switch (ipc_message__.type()) { \
716 #define IPC_BEGIN_MESSAGE_MAP(class_name, msg) \
718 typedef class_name _IpcMessageHandlerClass; \
719 const IPC::Message& ipc_message__ = msg; \
720 bool msg_is_ok__ = true; \
721 switch (ipc_message__.type()) { \
723 #define IPC_MESSAGE_FORWARD(msg_class, obj, member_func) \
724 case msg_class::ID: \
725 msg_is_ok__ = msg_class::Dispatch(&ipc_message__, obj, this, &member_func); \
728 #define IPC_MESSAGE_HANDLER(msg_class, member_func) \
729 IPC_MESSAGE_FORWARD(msg_class, this, _IpcMessageHandlerClass::member_func)
731 #define IPC_MESSAGE_FORWARD_DELAY_REPLY(msg_class, obj, member_func) \
732 case msg_class::ID: \
733 msg_is_ok__ = msg_class::DispatchDelayReply(&ipc_message__, obj, &member_func); \
736 #define IPC_MESSAGE_HANDLER_DELAY_REPLY(msg_class, member_func) \
737 IPC_MESSAGE_FORWARD_DELAY_REPLY(msg_class, this, \
738 _IpcMessageHandlerClass::member_func)
740 #define IPC_MESSAGE_HANDLER_GENERIC(msg_class, code) \
741 case msg_class::ID: \
745 #define IPC_REPLY_HANDLER(func) \
747 func(ipc_message__); \
751 #define IPC_MESSAGE_UNHANDLED(code) \
756 #define IPC_MESSAGE_UNHANDLED_ERROR() \
757 IPC_MESSAGE_UNHANDLED(NOTREACHED() << \
758 "Invalid message with type = " << \
759 ipc_message__.type())
761 #define IPC_END_MESSAGE_MAP() \
762 DCHECK(msg_is_ok__); \
766 #define IPC_END_MESSAGE_MAP_EX() \
770 // This corresponds to an enum value from IPCMessageStart.
771 #define IPC_MESSAGE_CLASS(message) \
772 IPC_MESSAGE_ID_CLASS(message.type())
774 #endif // IPC_IPC_MESSAGE_MACROS_H_
776 // Clean up IPC_MESSAGE_START in this unguarded section so that the
777 // XXX_messages.h files need not do so themselves. This makes the
778 // XXX_messages.h files easier to write.
779 #undef IPC_MESSAGE_START