Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This header is meant to be included in multiple passes, hence no traditional | 5 // Defining IPC Messages |
| 6 // header guard. | |
| 7 // | 6 // |
| 8 // In your XXX_messages_internal.h file, before defining any messages do: | 7 // Your IPC messages will be defined by macros inside of an XXX_messages.h |
| 9 // #define IPC_MESSAGE_START XMsgStart | 8 // header file. Most of the time, the system can automatically generate all |
| 10 // XMstStart value is from the IPCMessageStart enum in ipc_message_utils.h, and | 9 // of messaging mechanism from these definitions, but sometimes some manual |
| 11 // needs to be unique for each different file. | 10 // coding is required. In these cases, you will also have an XXX_messages.cc |
| 12 // In your XXX_messages.cc file, after all the includes for param types: | 11 // implemation file as well. |
| 12 // | |
| 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. | |
| 15 // | |
| 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 | |
| 20 // | |
| 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 ... ) | |
| 26 // | |
| 27 // A message generator .h header file pulls in all other message-declaring | |
| 28 // headers for a given component. It is included by a message generator | |
| 29 // .cc file, which is where all the generated code will wind up. Typically, | |
| 30 // you will use an existing generator (e.g. common_message_generator.cc and | |
| 31 // common_message_generator.h in /chrome/common), but there are circumstances | |
| 32 // where you may add a new one. | |
| 33 // | |
| 34 // In the rare cicrucmstances where you can't re-use an existing file, | |
| 35 // your YYY_message_generator.cc file for a component YYY would contain | |
| 36 // the following code: | |
| 37 // // Get basic type definitions. | |
| 13 // #define IPC_MESSAGE_IMPL | 38 // #define IPC_MESSAGE_IMPL |
| 14 // #include "X_messages.h" | 39 // #include "path/to/YYY_message_generator.h" |
| 40 // // Generate constructors. | |
| 41 // #include "ipc/struct_constructor_macros.h" | |
| 42 // #include "path/to/YYY_message_generator.h" | |
| 43 // // Generate destructors. | |
| 44 // #include "ipc/struct_destructor_macros.h" | |
| 45 // #include "path/to/YYY_message_generator.h" | |
| 46 // namespace IPC { | |
| 47 // // Generate param traits write methods. | |
| 48 // #include "ipc/param_traits_write_macros.h" | |
| 49 // #include "path/to/YYY_message_generator.h" | |
| 50 // // Generate param traits read methods. | |
| 51 // #include "ipc/param_traits_read_macros.h" | |
| 52 // #include "path/to/YYY_message_generator.h" | |
| 53 // // Generate param traits log methods. | |
| 54 // #include "ipc/param_traits_log_macros.h" | |
| 55 // #include "path/to/YYY_message_generator.h" | |
| 56 // } // namespace IPC | |
| 15 // | 57 // |
| 58 // In cases where manual generation is required, in your XXX_messages.cc | |
| 59 // file, put the following after all the includes for param types: | |
| 60 // #define IPC_MESSAGE_IMPL | |
| 61 // #include "XXX_messages.h" | |
| 62 // (... implementation of traits not auto-generated ...) | |
| 63 // | |
| 64 // The XXX_messages.h file will be multiply-included by the | |
| 65 // YYY_message_generator.cc file, so your XXX_messages file can't be | |
| 66 // guarded in the usual manner. Ideally, there will be no need for any | |
| 67 // inclusion guard, since the XXX_messages.h file should consist soley | |
| 68 // of inclusions of other headers (which are self-guarding) and IPC | |
| 69 // macros (which are multiply evaluating). | |
| 70 // | |
| 71 // Note that there is no #pragma once; doing so would mark the whole file | |
| 72 // as being singly-included. Since your XXX_messages.h file is only | |
| 73 // partially-guarded, care must be taken to ensure that it is only included | |
| 74 // by other .cc files (and the YYY_message_generator.h file). Including an | |
| 75 // XXX_messages.h file in some other .h file may result in duplicate | |
| 76 // declarations and a compilation failure. | |
| 77 // | |
| 78 // One exception to the no-include guard rule occurs if you have type | |
| 79 // definitions present in your XXX_messages.h file. Ideally, you'd move | |
|
jam
2011/02/10 18:43:33
btw thinking more about this, I think typedefs in
| |
| 80 // these to another (guarded) .h file and include this second .h in your | |
| 81 // XXX_messages.h file. But if for some reason this can't be done, then | |
| 82 // you will want to bracket the smallest possible section of your | |
| 83 // XXX_messages.h file with an include guard macro. | |
| 84 // | |
| 85 // Sometimes is it convenient to provide an incomplete class type declaration | |
| 86 // to avoid pulling in a long chain of headers. This is acceptable when | |
| 87 // your XXX_messages.h header is being included by the message sending code, | |
| 88 // but not when the YYY_message_generator.c is building the messages. In | |
| 89 // addtion, due to the multiple inclusion restriction, these type will need | |
| 90 // to be guarded. Follow a convention like: | |
| 91 // #ifndef SOME_GUARD_MACRO | |
|
jam
2011/02/10 18:43:33
nit: we don't really need an ifdef guard around fo
| |
| 92 // #define SOME_GUARD_MACRO | |
| 93 // class some_class; // One incomplete class declaration | |
| 94 // class_some_other_class; // Another incomplete class declaration | |
| 95 // #endif // SOME_GUARD_MACRO | |
| 96 // #ifdef IPC_MESSAGE_IMPL | |
| 97 // #inlcude "path/to/some_class.h" // Full class declaration | |
| 98 // #inlcude "path/to/some_other_class.h" // Full class declaration | |
| 99 // #endif // IPC_MESSAGE_IMPL | |
| 100 // (.. IPC macros using some_class and some_other_class ...) | |
| 101 // | |
| 102 // You will use IPC message macro invocations for three things: | |
| 103 // - New struct definitions for IPC | |
| 104 // - Registering existing struct and enum definitions with IPC | |
| 105 // - Defining the messages themselves | |
| 106 // | |
| 107 // New structs are defined with IPC_STRUCT_BEGIN(), IPC_STRUCT_MEMBER(), | |
| 108 // IPC_STRUCT_END() family of macros. These cause the XXX_messages.h | |
| 109 // to proclaim equivalent struct declarations for use by callers, as well | |
| 110 // as later registering the type with the message generation. Note that | |
| 111 // IPC_STRUCT_MEMBER() is only permitted inside matching calls to | |
| 112 // IPC_STRUCT_BEGIN() / IPC_STRUCT_END(). | |
| 113 // | |
| 114 // Externally-defined structs are registered with IPC_STRUCT_TRAITS_BEGIN(), | |
| 115 // IPC_STRUCT_TRAITS_MEMBER(), and IPC_STRUCT_TRAITS_END() macros. These | |
| 116 // cause registration of the types with message generation only. Note that | |
| 117 // IPC_STRUCT_TRAITS_MEMBER() is only permitted inside matching calls | |
| 118 // to IPC_STRUCT_TRAITS_BEGIN() / IPC_STRUCT_TRAITS_END(). | |
| 119 // | |
| 120 // Enum types are registered with a single IPC_ENUM_TRAITS() macro. There | |
| 121 // is no need to enumerate each value to the IPC mechanism. | |
| 122 // | |
| 123 // Once the types have been declared / registered, message definitions follow. | |
| 16 // "Sync" messages are just synchronous calls, the Send() call doesn't return | 124 // "Sync" messages are just synchronous calls, the Send() call doesn't return |
| 17 // until a reply comes back. Input parameters are first (const TYPE&), and | 125 // until a reply comes back. Input parameters are first (const TYPE&), and |
| 18 // To declare a sync message, use the IPC_SYNC_ macros. The numbers at the | 126 // To declare a sync message, use the IPC_SYNC_ macros. The numbers at the |
| 19 // end show how many input/output parameters there are (i.e. 1_2 is 1 in, 2 | 127 // end show how many input/output parameters there are (i.e. 1_2 is 1 in, 2 |
| 20 // out). The caller does a Send([route id, ], in1, &out1, &out2). | 128 // out). The caller does a Send([route id, ], in1, &out1, &out2). |
| 21 // The receiver's handler function will be | 129 // The receiver's handler function will be |
| 22 // void OnSyncMessageName(const type1& in1, type2* out1, type3* out2) | 130 // void OnSyncMessageName(const type1& in1, type2* out1, type3* out2) |
| 23 // | 131 // |
| 24 // | |
| 25 // A caller can also send a synchronous message, while the receiver can respond | 132 // A caller can also send a synchronous message, while the receiver can respond |
| 26 // at a later time. This is transparent from the sender's side. The receiver | 133 // at a later time. This is transparent from the sender's side. The receiver |
| 27 // needs to use a different handler that takes in a IPC::Message* as the output | 134 // needs to use a different handler that takes in a IPC::Message* as the output |
| 28 // type, stash the message, and when it has the data it can Send the message. | 135 // type, stash the message, and when it has the data it can Send the message. |
| 29 // | 136 // |
| 30 // Use the IPC_MESSAGE_HANDLER_DELAY_REPLY macro instead of IPC_MESSAGE_HANDLER | 137 // Use the IPC_MESSAGE_HANDLER_DELAY_REPLY macro instead of IPC_MESSAGE_HANDLER |
| 31 // IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_SyncMessageName, | 138 // IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_SyncMessageName, |
| 32 // OnSyncMessageName) | 139 // OnSyncMessageName) |
| 33 // | 140 // |
| 34 // The handler function will look like: | 141 // The handler function will look like: |
| 35 // void OnSyncMessageName(const type1& in1, IPC::Message* reply_msg); | 142 // void OnSyncMessageName(const type1& in1, IPC::Message* reply_msg); |
| 36 // | 143 // |
| 37 // Receiver stashes the IPC::Message* pointer, and when it's ready, it does: | 144 // Receiver stashes the IPC::Message* pointer, and when it's ready, it does: |
| 38 // ViewHostMsg_SyncMessageName::WriteReplyParams(reply_msg, out1, out2); | 145 // ViewHostMsg_SyncMessageName::WriteReplyParams(reply_msg, out1, out2); |
| 39 // Send(reply_msg); | 146 // Send(reply_msg); |
| 40 | 147 |
| 148 #ifndef IPC_IPC_MESSAGE_MACROS_H_ | |
| 149 #define IPC_IPC_MESSAGE_MACROS_H_ | |
| 150 // Can use #pragma once all XXX_messages.h files clean up IPC_MESSAGE_START | |
| 151 | |
| 41 #include "ipc/ipc_message_utils.h" | 152 #include "ipc/ipc_message_utils.h" |
| 42 | 153 #include "ipc/param_traits_macros.h" |
| 43 // In case a file includes several X_messages.h files, we don't want to get | |
| 44 // errors because each X_messages_internal.h file will define this. | |
| 45 #undef IPC_MESSAGE_START | |
| 46 | 154 |
| 47 #if defined(IPC_MESSAGE_IMPL) | 155 #if defined(IPC_MESSAGE_IMPL) |
| 48 #include "ipc/ipc_message_impl_macros.h" | 156 #include "ipc/ipc_message_impl_macros.h" |
| 49 #elif defined(IPC_MESSAGE_MACROS_LOG_ENABLED) | 157 #elif defined(IPC_MESSAGE_MACROS_LOG_ENABLED) |
| 50 | 158 |
| 51 #ifndef IPC_LOG_TABLE_CREATED | 159 #ifndef IPC_LOG_TABLE_CREATED |
| 52 #define IPC_LOG_TABLE_CREATED | 160 #define IPC_LOG_TABLE_CREATED |
| 53 | 161 |
| 54 #include "base/hash_tables.h" | 162 #include "base/hash_tables.h" |
| 55 | 163 |
| 56 typedef void (*LogFunction)(std::string* name, | 164 typedef void (*LogFunction)(std::string* name, |
| 57 const IPC::Message* msg, | 165 const IPC::Message* msg, |
| 58 std::string* params); | 166 std::string* params); |
| 59 | 167 |
| 60 typedef base::hash_map<uint32, LogFunction > LogFunctionMap; | 168 typedef base::hash_map<uint32, LogFunction > LogFunctionMap; |
| 61 LogFunctionMap g_log_function_mapping; | 169 LogFunctionMap g_log_function_mapping; |
| 62 | 170 |
| 63 #endif | 171 #endif // IPC_LOG_TABLE_CREATED |
| 64 | 172 |
| 65 | 173 |
| 66 #define IPC_MESSAGE_LOG(msg_class) \ | 174 #define IPC_MESSAGE_LOG(msg_class) \ |
| 67 class LoggerRegisterHelper##msg_class { \ | 175 class LoggerRegisterHelper##msg_class { \ |
| 68 public: \ | 176 public: \ |
| 69 LoggerRegisterHelper##msg_class() { \ | 177 LoggerRegisterHelper##msg_class() { \ |
| 70 g_log_function_mapping[msg_class::ID] = msg_class::Log; \ | 178 g_log_function_mapping[msg_class::ID] = msg_class::Log; \ |
| 71 } \ | 179 } \ |
| 72 }; \ | 180 }; \ |
| 73 LoggerRegisterHelper##msg_class g_LoggerRegisterHelper##msg_class; | 181 LoggerRegisterHelper##msg_class g_LoggerRegisterHelper##msg_class; |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 | 381 |
| 274 #define IPC_SYNC_MESSAGE_ROUTED5_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out) \ | 382 #define IPC_SYNC_MESSAGE_ROUTED5_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out) \ |
| 275 IPC_MESSAGE_LOG(msg_class) | 383 IPC_MESSAGE_LOG(msg_class) |
| 276 | 384 |
| 277 #define IPC_SYNC_MESSAGE_ROUTED5_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out) \ | 385 #define IPC_SYNC_MESSAGE_ROUTED5_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out) \ |
| 278 IPC_MESSAGE_LOG(msg_class) | 386 IPC_MESSAGE_LOG(msg_class) |
| 279 | 387 |
| 280 #define IPC_SYNC_MESSAGE_ROUTED5_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out, type4_out) \ | 388 #define IPC_SYNC_MESSAGE_ROUTED5_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out, type4_out) \ |
| 281 IPC_MESSAGE_LOG(msg_class) | 389 IPC_MESSAGE_LOG(msg_class) |
| 282 | 390 |
| 283 #else | 391 #else // defined(IPC_MESSAGE_MACROS_LOG_ENABLED) |
| 284 | 392 |
| 285 #define IPC_MESSAGE_CONTROL0_EXTRA(msg_class) | 393 #define IPC_MESSAGE_CONTROL0_EXTRA(msg_class) |
| 286 #define IPC_MESSAGE_CONTROL1_EXTRA(msg_class, type1) | 394 #define IPC_MESSAGE_CONTROL1_EXTRA(msg_class, type1) |
| 287 #define IPC_MESSAGE_CONTROL2_EXTRA(msg_class, type1, type2) | 395 #define IPC_MESSAGE_CONTROL2_EXTRA(msg_class, type1, type2) |
| 288 #define IPC_MESSAGE_CONTROL3_EXTRA(msg_class, type1, type2, type3) | 396 #define IPC_MESSAGE_CONTROL3_EXTRA(msg_class, type1, type2, type3) |
| 289 #define IPC_MESSAGE_CONTROL4_EXTRA(msg_class, type1, type2, type3, type4) | 397 #define IPC_MESSAGE_CONTROL4_EXTRA(msg_class, type1, type2, type3, type4) |
| 290 #define IPC_MESSAGE_CONTROL5_EXTRA(msg_class, type1, type2, type3, type4, type5) | 398 #define IPC_MESSAGE_CONTROL5_EXTRA(msg_class, type1, type2, type3, type4, type5) |
| 291 #define IPC_MESSAGE_ROUTED0_EXTRA(msg_class) | 399 #define IPC_MESSAGE_ROUTED0_EXTRA(msg_class) |
| 292 #define IPC_MESSAGE_ROUTED1_EXTRA(msg_class, type1) | 400 #define IPC_MESSAGE_ROUTED1_EXTRA(msg_class, type1) |
| 293 #define IPC_MESSAGE_ROUTED2_EXTRA(msg_class, type1, type2) | 401 #define IPC_MESSAGE_ROUTED2_EXTRA(msg_class, type1, type2) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 #define IPC_SYNC_MESSAGE_ROUTED4_1_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out) | 453 #define IPC_SYNC_MESSAGE_ROUTED4_1_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out) |
| 346 #define IPC_SYNC_MESSAGE_ROUTED4_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out) | 454 #define IPC_SYNC_MESSAGE_ROUTED4_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out) |
| 347 #define IPC_SYNC_MESSAGE_ROUTED4_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out, type3_out) | 455 #define IPC_SYNC_MESSAGE_ROUTED4_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out, type3_out) |
| 348 #define IPC_SYNC_MESSAGE_ROUTED4_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out, type3_out, type4_out) | 456 #define IPC_SYNC_MESSAGE_ROUTED4_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type1_out, type2_out, type3_out, type4_out) |
| 349 #define IPC_SYNC_MESSAGE_ROUTED5_0_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in) | 457 #define IPC_SYNC_MESSAGE_ROUTED5_0_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in) |
| 350 #define IPC_SYNC_MESSAGE_ROUTED5_1_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out) | 458 #define IPC_SYNC_MESSAGE_ROUTED5_1_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out) |
| 351 #define IPC_SYNC_MESSAGE_ROUTED5_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out) | 459 #define IPC_SYNC_MESSAGE_ROUTED5_2_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out) |
| 352 #define IPC_SYNC_MESSAGE_ROUTED5_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out) | 460 #define IPC_SYNC_MESSAGE_ROUTED5_3_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type3_out) |
| 353 #define IPC_SYNC_MESSAGE_ROUTED5_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type4_out) | 461 #define IPC_SYNC_MESSAGE_ROUTED5_4_EXTRA(msg_class, type1_in, type2_in, type3_in , type4_in, type5_in, type1_out, type2_out, type4_out) |
| 354 | 462 |
| 355 #endif | 463 #endif // defined(IPC_MESSAGE_MACROS_LOG_ENABLED) |
| 464 | |
| 465 // Macros for defining structs. May be subsequently redefined. | |
| 466 #define IPC_STRUCT_BEGIN(struct_name) \ | |
| 467 struct struct_name; \ | |
| 468 IPC_STRUCT_TRAITS_BEGIN(struct_name) \ | |
| 469 IPC_STRUCT_TRAITS_END() \ | |
| 470 struct struct_name : IPC::NoParams { \ | |
| 471 struct_name(); \ | |
| 472 ~struct_name(); | |
| 473 #define IPC_STRUCT_MEMBER(type, name) type name; | |
| 474 #define IPC_STRUCT_END() }; | |
| 356 | 475 |
| 357 // Note: we currently use __LINE__ to give unique IDs to messages within a file. | 476 // Note: we currently use __LINE__ to give unique IDs to messages within a file. |
| 358 // They're globally unique since each file defines its own IPC_MESSAGE_START. | 477 // They're globally unique since each file defines its own IPC_MESSAGE_START. |
| 359 // Ideally, we wouldn't use line numbers, but instead use the __COUNTER__ macro, | 478 // Ideally, we wouldn't use line numbers, but instead use the __COUNTER__ macro, |
| 360 // but it needs gcc 4.3 and xcode doesn't use it yet. When that happens, switch | 479 // but it needs gcc 4.3 and xcode doesn't use it yet. When that happens, switch |
| 361 // to it. | 480 // to it. |
| 362 | 481 |
| 363 #define IPC_MESSAGE_CONTROL0(msg_class) \ | 482 #define IPC_MESSAGE_CONTROL0(msg_class) \ |
| 364 class msg_class : public IPC::Message { \ | 483 class msg_class : public IPC::Message { \ |
| 365 public: \ | 484 public: \ |
| (...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1236 } \ | 1355 } \ |
| 1237 } | 1356 } |
| 1238 | 1357 |
| 1239 #define IPC_END_MESSAGE_MAP_EX() \ | 1358 #define IPC_END_MESSAGE_MAP_EX() \ |
| 1240 } \ | 1359 } \ |
| 1241 } | 1360 } |
| 1242 | 1361 |
| 1243 // This corresponds to an enum value from IPCMessageStart. | 1362 // This corresponds to an enum value from IPCMessageStart. |
| 1244 #define IPC_MESSAGE_CLASS(message) \ | 1363 #define IPC_MESSAGE_CLASS(message) \ |
| 1245 message.type() >> 16 | 1364 message.type() >> 16 |
| 1365 | |
| 1366 #endif // IPC_IPC_MESSAGE_MACROS_H_ | |
| 1367 | |
| 1368 // Clean up IPC_MESSAGE_START in this unguarded section so that the | |
| 1369 // XXX_messages.h files need not do so themselves. This makes the | |
| 1370 // XXX_messages.h files easier to write. | |
| 1371 #undef IPC_MESSAGE_START | |
| 1372 | |
| OLD | NEW |