OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef IPC_IPC_MESSAGE_UTILS_H_ | 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
6 #define IPC_IPC_MESSAGE_UTILS_H_ | 6 #define IPC_IPC_MESSAGE_UTILS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
(...skipping 1146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1157 return false; | 1157 return false; |
1158 *a = params.a; | 1158 *a = params.a; |
1159 *b = params.b; | 1159 *b = params.b; |
1160 *c = params.c; | 1160 *c = params.c; |
1161 *d = params.d; | 1161 *d = params.d; |
1162 *e = params.e; | 1162 *e = params.e; |
1163 return true; | 1163 return true; |
1164 } | 1164 } |
1165 }; | 1165 }; |
1166 | 1166 |
1167 // defined in ipc_logging.cc | |
1168 void GenerateLogData(const std::string& channel, const Message& message, | |
1169 LogData* data); | |
1170 | |
1171 | |
1172 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
1173 inline void AddOutputParamsToLog(const Message* msg, std::wstring* l) { | |
1174 const std::wstring& output_params = msg->output_params(); | |
1175 if (!l->empty() && !output_params.empty()) | |
1176 l->append(L", "); | |
1177 | |
1178 l->append(output_params); | |
1179 } | |
1180 | |
1181 template <class ReplyParamType> | |
1182 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, | |
1183 const Message* msg) { | |
1184 if (msg->received_time() != 0) { | |
1185 std::wstring output_params; | |
1186 LogParam(reply_params, &output_params); | |
1187 msg->set_output_params(output_params); | |
1188 } | |
1189 } | |
1190 | |
1191 inline void ConnectMessageAndReply(const Message* msg, Message* reply) { | |
1192 if (msg->sent_time()) { | |
1193 // Don't log the sync message after dispatch, as we don't have the | |
1194 // output parameters at that point. Instead, save its data and log it | |
1195 // with the outgoing reply message when it's sent. | |
1196 LogData* data = new LogData; | |
1197 GenerateLogData("", *msg, data); | |
1198 msg->set_dont_log(); | |
1199 reply->set_sync_log_data(data); | |
1200 } | |
1201 } | |
1202 #else | |
1203 inline void AddOutputParamsToLog(const Message* msg, std::wstring* l) {} | |
1204 | |
1205 template <class ReplyParamType> | |
1206 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, | |
1207 const Message* msg) {} | |
1208 | |
1209 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} | |
1210 #endif | |
1211 | |
1212 // This class assumes that its template argument is a RefTuple (a Tuple with | 1167 // This class assumes that its template argument is a RefTuple (a Tuple with |
1213 // reference elements). This would go into ipc_message_utils_impl.h, but it is | 1168 // reference elements). |
1214 // also used by chrome_frame. | |
1215 template <class RefTuple> | 1169 template <class RefTuple> |
1216 class ParamDeserializer : public MessageReplyDeserializer { | 1170 class ParamDeserializer : public MessageReplyDeserializer { |
1217 public: | 1171 public: |
1218 explicit ParamDeserializer(const RefTuple& out) : out_(out) { } | 1172 explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
1219 | 1173 |
1220 bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { | 1174 bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { |
1221 return ReadParam(&msg, &iter, &out_); | 1175 return ReadParam(&msg, &iter, &out_); |
1222 } | 1176 } |
1223 | 1177 |
1224 RefTuple out_; | 1178 RefTuple out_; |
1225 }; | 1179 }; |
1226 | 1180 |
| 1181 // defined in ipc_logging.cc |
| 1182 void GenerateLogData(const std::string& channel, const Message& message, |
| 1183 LogData* data); |
| 1184 |
1227 // Used for synchronous messages. | 1185 // Used for synchronous messages. |
1228 template <class SendParamType, class ReplyParamType> | 1186 template <class SendParamType, class ReplyParamType> |
1229 class MessageWithReply : public SyncMessage { | 1187 class MessageWithReply : public SyncMessage { |
1230 public: | 1188 public: |
1231 typedef SendParamType SendParam; | 1189 typedef SendParamType SendParam; |
1232 typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; | 1190 typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; |
1233 typedef ReplyParamType ReplyParam; | 1191 typedef ReplyParamType ReplyParam; |
1234 | 1192 |
1235 MessageWithReply(int32 routing_id, uint32 type, | 1193 MessageWithReply(int32 routing_id, uint32 type, |
1236 const RefSendParam& send, const ReplyParam& reply); | 1194 const RefSendParam& send, const ReplyParam& reply) |
1237 | 1195 : SyncMessage(routing_id, type, PRIORITY_NORMAL, |
1238 // TODO(erg): Migrate these ReadSendParam/ReadReplyParam() methods to | 1196 new ParamDeserializer<ReplyParam>(reply)) { |
1239 // ipc_message_utils_impl.h once I figure out how to get the linkage correct | 1197 WriteParam(this, send); |
1240 // in the release builds. | |
1241 static bool ReadSendParam(const Message* msg, SendParam* p) { | |
1242 void* iter = SyncMessage::GetDataIterator(msg); | |
1243 return ReadParam(msg, &iter, p); | |
1244 } | 1198 } |
1245 | 1199 |
1246 static bool ReadReplyParam(const Message* msg, | 1200 static void Log(const Message* msg, std::wstring* l) { |
1247 typename TupleTypes<ReplyParam>::ValueTuple* p) { | 1201 if (msg->is_sync()) { |
1248 void* iter = SyncMessage::GetDataIterator(msg); | 1202 SendParam p; |
1249 return ReadParam(msg, &iter, p); | 1203 void* iter = SyncMessage::GetDataIterator(msg); |
| 1204 if (ReadParam(msg, &iter, &p)) |
| 1205 LogParam(p, l); |
| 1206 |
| 1207 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 1208 const std::wstring& output_params = msg->output_params(); |
| 1209 if (!l->empty() && !output_params.empty()) |
| 1210 l->append(L", "); |
| 1211 |
| 1212 l->append(output_params); |
| 1213 #endif |
| 1214 } else { |
| 1215 // This is an outgoing reply. Now that we have the output parameters, we |
| 1216 // can finally log the message. |
| 1217 typename TupleTypes<ReplyParam>::ValueTuple p; |
| 1218 void* iter = SyncMessage::GetDataIterator(msg); |
| 1219 if (ReadParam(msg, &iter, &p)) |
| 1220 LogParam(p, l); |
| 1221 } |
1250 } | 1222 } |
1251 | 1223 |
1252 template<class T, class Method> | 1224 template<class T, class Method> |
1253 static bool Dispatch(const Message* msg, T* obj, Method func) { | 1225 static bool Dispatch(const Message* msg, T* obj, Method func) { |
1254 SendParam send_params; | 1226 SendParam send_params; |
| 1227 void* iter = GetDataIterator(msg); |
1255 Message* reply = GenerateReply(msg); | 1228 Message* reply = GenerateReply(msg); |
1256 bool error; | 1229 bool error; |
1257 if (ReadSendParam(msg, &send_params)) { | 1230 if (ReadParam(msg, &iter, &send_params)) { |
1258 typename TupleTypes<ReplyParam>::ValueTuple reply_params; | 1231 typename TupleTypes<ReplyParam>::ValueTuple reply_params; |
1259 DispatchToMethod(obj, func, send_params, &reply_params); | 1232 DispatchToMethod(obj, func, send_params, &reply_params); |
1260 WriteParam(reply, reply_params); | 1233 WriteParam(reply, reply_params); |
1261 error = false; | 1234 error = false; |
1262 LogReplyParamsToMessage(reply_params, msg); | 1235 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 1236 if (msg->received_time() != 0) { |
| 1237 std::wstring output_params; |
| 1238 LogParam(reply_params, &output_params); |
| 1239 msg->set_output_params(output_params); |
| 1240 } |
| 1241 #endif |
1263 } else { | 1242 } else { |
1264 NOTREACHED() << "Error deserializing message " << msg->type(); | 1243 NOTREACHED() << "Error deserializing message " << msg->type(); |
1265 reply->set_reply_error(); | 1244 reply->set_reply_error(); |
1266 error = true; | 1245 error = true; |
1267 } | 1246 } |
1268 | 1247 |
1269 obj->Send(reply); | 1248 obj->Send(reply); |
1270 return !error; | 1249 return !error; |
1271 } | 1250 } |
1272 | 1251 |
1273 template<class T, class Method> | 1252 template<class T, class Method> |
1274 static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { | 1253 static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { |
1275 SendParam send_params; | 1254 SendParam send_params; |
| 1255 void* iter = GetDataIterator(msg); |
1276 Message* reply = GenerateReply(msg); | 1256 Message* reply = GenerateReply(msg); |
1277 bool error; | 1257 bool error; |
1278 if (ReadSendParam(msg, &send_params)) { | 1258 if (ReadParam(msg, &iter, &send_params)) { |
1279 Tuple1<Message&> t = MakeRefTuple(*reply); | 1259 Tuple1<Message&> t = MakeRefTuple(*reply); |
1280 ConnectMessageAndReply(msg, reply); | 1260 |
| 1261 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 1262 if (msg->sent_time()) { |
| 1263 // Don't log the sync message after dispatch, as we don't have the |
| 1264 // output parameters at that point. Instead, save its data and log it |
| 1265 // with the outgoing reply message when it's sent. |
| 1266 LogData* data = new LogData; |
| 1267 GenerateLogData("", *msg, data); |
| 1268 msg->set_dont_log(); |
| 1269 reply->set_sync_log_data(data); |
| 1270 } |
| 1271 #endif |
1281 DispatchToMethod(obj, func, send_params, &t); | 1272 DispatchToMethod(obj, func, send_params, &t); |
1282 error = false; | 1273 error = false; |
1283 } else { | 1274 } else { |
1284 NOTREACHED() << "Error deserializing message " << msg->type(); | 1275 NOTREACHED() << "Error deserializing message " << msg->type(); |
1285 reply->set_reply_error(); | 1276 reply->set_reply_error(); |
1286 obj->Send(reply); | 1277 obj->Send(reply); |
1287 error = true; | 1278 error = true; |
1288 } | 1279 } |
1289 return !error; | 1280 return !error; |
1290 } | 1281 } |
(...skipping 27 matching lines...) Expand all Loading... |
1318 ReplyParam p(a, b, c, d, e); | 1309 ReplyParam p(a, b, c, d, e); |
1319 WriteParam(reply, p); | 1310 WriteParam(reply, p); |
1320 } | 1311 } |
1321 }; | 1312 }; |
1322 | 1313 |
1323 //----------------------------------------------------------------------------- | 1314 //----------------------------------------------------------------------------- |
1324 | 1315 |
1325 } // namespace IPC | 1316 } // namespace IPC |
1326 | 1317 |
1327 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1318 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
OLD | NEW |