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> |
11 #include <vector> | 11 #include <vector> |
12 #include <map> | 12 #include <map> |
13 #include <set> | 13 #include <set> |
14 | 14 |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
17 #include "base/nullable_string16.h" | 17 #include "base/nullable_string16.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/time.h" |
22 #include "base/tuple.h" | 22 #include "base/tuple.h" |
23 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
24 #include "base/values.h" | 24 #include "base/values.h" |
25 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
26 #include "ipc/file_descriptor_set_posix.h" | 26 #include "ipc/file_descriptor_set_posix.h" |
27 #endif | 27 #endif |
28 #include "ipc/ipc_channel_handle.h" | 28 #include "ipc/ipc_channel_handle.h" |
29 #include "ipc/ipc_sync_message.h" | 29 #include "ipc/ipc_sync_message.h" |
30 | 30 |
31 // Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique | 31 // Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique |
(...skipping 28 matching lines...) Expand all Loading... |
60 GpuChannelMsgStart, | 60 GpuChannelMsgStart, |
61 GpuVideoDecoderHostMsgStart, | 61 GpuVideoDecoderHostMsgStart, |
62 GpuVideoDecoderMsgStart, | 62 GpuVideoDecoderMsgStart, |
63 ServiceMsgStart, | 63 ServiceMsgStart, |
64 ServiceHostMsgStart, | 64 ServiceHostMsgStart, |
65 // NOTE: When you add a new message class, also update | 65 // NOTE: When you add a new message class, also update |
66 // IPCStatusView::IPCStatusView to ensure logging works. | 66 // IPCStatusView::IPCStatusView to ensure logging works. |
67 LastMsgIndex | 67 LastMsgIndex |
68 }; | 68 }; |
69 | 69 |
70 class DictionaryValue; | |
71 class ListValue; | |
72 | |
73 namespace base { | |
74 class Time; | |
75 } | |
76 | |
77 namespace IPC { | 70 namespace IPC { |
78 | 71 |
79 //----------------------------------------------------------------------------- | 72 //----------------------------------------------------------------------------- |
80 // An iterator class for reading the fields contained within a Message. | 73 // An iterator class for reading the fields contained within a Message. |
81 | 74 |
82 class MessageIterator { | 75 class MessageIterator { |
83 public: | 76 public: |
84 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { | 77 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { |
85 } | 78 } |
86 int NextInt() const { | 79 int NextInt() const { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 return result; | 299 return result; |
307 } | 300 } |
308 static void Log(const param_type& p, std::wstring* l) { | 301 static void Log(const param_type& p, std::wstring* l) { |
309 l->append(StringPrintf(L"%lc", p)); | 302 l->append(StringPrintf(L"%lc", p)); |
310 } | 303 } |
311 }; | 304 }; |
312 | 305 |
313 template <> | 306 template <> |
314 struct ParamTraits<base::Time> { | 307 struct ParamTraits<base::Time> { |
315 typedef base::Time param_type; | 308 typedef base::Time param_type; |
316 static void Write(Message* m, const param_type& p); | 309 static void Write(Message* m, const param_type& p) { |
317 static bool Read(const Message* m, void** iter, param_type* r); | 310 ParamTraits<int64>::Write(m, p.ToInternalValue()); |
318 static void Log(const param_type& p, std::wstring* l); | 311 } |
| 312 static bool Read(const Message* m, void** iter, param_type* r) { |
| 313 int64 value; |
| 314 if (!ParamTraits<int64>::Read(m, iter, &value)) |
| 315 return false; |
| 316 *r = base::Time::FromInternalValue(value); |
| 317 return true; |
| 318 } |
| 319 static void Log(const param_type& p, std::wstring* l) { |
| 320 ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 321 } |
319 }; | 322 }; |
320 | 323 |
321 #if defined(OS_WIN) | 324 #if defined(OS_WIN) |
322 template <> | 325 template <> |
323 struct ParamTraits<LOGFONT> { | 326 struct ParamTraits<LOGFONT> { |
324 typedef LOGFONT param_type; | 327 typedef LOGFONT param_type; |
325 static void Write(Message* m, const param_type& p) { | 328 static void Write(Message* m, const param_type& p) { |
326 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); | 329 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
327 } | 330 } |
328 static bool Read(const Message* m, void** iter, param_type* r) { | 331 static bool Read(const Message* m, void** iter, param_type* r) { |
(...skipping 26 matching lines...) Expand all Loading... |
355 bool result = m->ReadData(iter, &data, &data_size); | 358 bool result = m->ReadData(iter, &data, &data_size); |
356 if (result && data_size == sizeof(MSG)) { | 359 if (result && data_size == sizeof(MSG)) { |
357 memcpy(r, data, sizeof(MSG)); | 360 memcpy(r, data, sizeof(MSG)); |
358 } else { | 361 } else { |
359 result = false; | 362 result = false; |
360 NOTREACHED(); | 363 NOTREACHED(); |
361 } | 364 } |
362 | 365 |
363 return result; | 366 return result; |
364 } | 367 } |
365 static void Log(const param_type& p, std::wstring* l) { | |
366 l->append(L"<MSG>"); | |
367 } | |
368 }; | 368 }; |
369 #endif // defined(OS_WIN) | 369 #endif // defined(OS_WIN) |
370 | 370 |
371 template <> | 371 template <> |
372 struct ParamTraits<DictionaryValue> { | 372 struct ParamTraits<DictionaryValue> { |
373 typedef DictionaryValue param_type; | 373 typedef DictionaryValue param_type; |
374 static void Write(Message* m, const param_type& p); | 374 static void Write(Message* m, const param_type& p); |
375 static bool Read(const Message* m, void** iter, param_type* r); | 375 static bool Read(const Message* m, void** iter, param_type* r); |
376 static void Log(const param_type& p, std::wstring* l); | 376 static void Log(const param_type& p, std::wstring* l); |
377 }; | 377 }; |
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1019 }; | 1019 }; |
1020 | 1020 |
1021 //----------------------------------------------------------------------------- | 1021 //----------------------------------------------------------------------------- |
1022 // Generic message subclasses | 1022 // Generic message subclasses |
1023 | 1023 |
1024 // Used for asynchronous messages. | 1024 // Used for asynchronous messages. |
1025 template <class ParamType> | 1025 template <class ParamType> |
1026 class MessageWithTuple : public Message { | 1026 class MessageWithTuple : public Message { |
1027 public: | 1027 public: |
1028 typedef ParamType Param; | 1028 typedef ParamType Param; |
1029 typedef typename TupleTypes<ParamType>::ParamTuple RefParam; | 1029 typedef typename ParamType::ParamTuple RefParam; |
1030 | 1030 |
1031 // The constructor and the Read() method's templated implementations are in | 1031 MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p) |
1032 // ipc_message_utils_impl.h. The subclass constructor and Log() methods call | 1032 : Message(routing_id, type, PRIORITY_NORMAL) { |
1033 // the templated versions of these and make sure there are instantiations in | 1033 WriteParam(this, p); |
1034 // those translation units. | 1034 } |
1035 MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p); | 1035 |
1036 static bool Read(const Message* msg, Param* p); | 1036 static bool Read(const Message* msg, Param* p) { |
| 1037 void* iter = NULL; |
| 1038 if (ReadParam(msg, &iter, p)) |
| 1039 return true; |
| 1040 NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1041 return false; |
| 1042 } |
1037 | 1043 |
1038 // Generic dispatcher. Should cover most cases. | 1044 // Generic dispatcher. Should cover most cases. |
1039 template<class T, class Method> | 1045 template<class T, class Method> |
1040 static bool Dispatch(const Message* msg, T* obj, Method func) { | 1046 static bool Dispatch(const Message* msg, T* obj, Method func) { |
1041 Param p; | 1047 Param p; |
1042 if (Read(msg, &p)) { | 1048 if (Read(msg, &p)) { |
1043 DispatchToMethod(obj, func, p); | 1049 DispatchToMethod(obj, func, p); |
1044 return true; | 1050 return true; |
1045 } | 1051 } |
1046 return false; | 1052 return false; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 static bool Dispatch(const Message* msg, T* obj, | 1104 static bool Dispatch(const Message* msg, T* obj, |
1099 void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { | 1105 void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { |
1100 Param p; | 1106 Param p; |
1101 if (Read(msg, &p)) { | 1107 if (Read(msg, &p)) { |
1102 (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); | 1108 (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); |
1103 return true; | 1109 return true; |
1104 } | 1110 } |
1105 return false; | 1111 return false; |
1106 } | 1112 } |
1107 | 1113 |
| 1114 static void Log(const Message* msg, std::wstring* l) { |
| 1115 Param p; |
| 1116 if (Read(msg, &p)) |
| 1117 LogParam(p, l); |
| 1118 } |
| 1119 |
1108 // Functions used to do manual unpacking. Only used by the automation code, | 1120 // Functions used to do manual unpacking. Only used by the automation code, |
1109 // these should go away once that code uses SyncChannel. | 1121 // these should go away once that code uses SyncChannel. |
1110 template<typename TA, typename TB> | 1122 template<typename TA, typename TB> |
1111 static bool Read(const IPC::Message* msg, TA* a, TB* b) { | 1123 static bool Read(const IPC::Message* msg, TA* a, TB* b) { |
1112 ParamType params; | 1124 ParamType params; |
1113 if (!Read(msg, ¶ms)) | 1125 if (!Read(msg, ¶ms)) |
1114 return false; | 1126 return false; |
1115 *a = params.a; | 1127 *a = params.a; |
1116 *b = params.b; | 1128 *b = params.b; |
1117 return true; | 1129 return true; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1170 | 1182 |
1171 // defined in ipc_logging.cc | 1183 // defined in ipc_logging.cc |
1172 void GenerateLogData(const std::string& channel, const Message& message, | 1184 void GenerateLogData(const std::string& channel, const Message& message, |
1173 LogData* data); | 1185 LogData* data); |
1174 | 1186 |
1175 // Used for synchronous messages. | 1187 // Used for synchronous messages. |
1176 template <class SendParamType, class ReplyParamType> | 1188 template <class SendParamType, class ReplyParamType> |
1177 class MessageWithReply : public SyncMessage { | 1189 class MessageWithReply : public SyncMessage { |
1178 public: | 1190 public: |
1179 typedef SendParamType SendParam; | 1191 typedef SendParamType SendParam; |
1180 typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; | 1192 typedef typename SendParam::ParamTuple RefSendParam; |
1181 typedef ReplyParamType ReplyParam; | 1193 typedef ReplyParamType ReplyParam; |
1182 | 1194 |
1183 MessageWithReply(int32 routing_id, uint32 type, | 1195 MessageWithReply(int32 routing_id, uint32 type, |
1184 const RefSendParam& send, const ReplyParam& reply) | 1196 const RefSendParam& send, const ReplyParam& reply) |
1185 : SyncMessage(routing_id, type, PRIORITY_NORMAL, | 1197 : SyncMessage(routing_id, type, PRIORITY_NORMAL, |
1186 new ParamDeserializer<ReplyParam>(reply)) { | 1198 new ParamDeserializer<ReplyParam>(reply)) { |
1187 WriteParam(this, send); | 1199 WriteParam(this, send); |
1188 } | 1200 } |
1189 | 1201 |
1190 static void Log(const Message* msg, std::wstring* l) { | 1202 static void Log(const Message* msg, std::wstring* l) { |
1191 if (msg->is_sync()) { | 1203 if (msg->is_sync()) { |
1192 SendParam p; | 1204 SendParam p; |
1193 void* iter = SyncMessage::GetDataIterator(msg); | 1205 void* iter = SyncMessage::GetDataIterator(msg); |
1194 if (ReadParam(msg, &iter, &p)) | 1206 if (ReadParam(msg, &iter, &p)) |
1195 LogParam(p, l); | 1207 LogParam(p, l); |
1196 | 1208 |
1197 #if defined(IPC_MESSAGE_LOG_ENABLED) | 1209 #if defined(IPC_MESSAGE_LOG_ENABLED) |
1198 const std::wstring& output_params = msg->output_params(); | 1210 const std::wstring& output_params = msg->output_params(); |
1199 if (!l->empty() && !output_params.empty()) | 1211 if (!l->empty() && !output_params.empty()) |
1200 l->append(L", "); | 1212 l->append(L", "); |
1201 | 1213 |
1202 l->append(output_params); | 1214 l->append(output_params); |
1203 #endif | 1215 #endif |
1204 } else { | 1216 } else { |
1205 // This is an outgoing reply. Now that we have the output parameters, we | 1217 // This is an outgoing reply. Now that we have the output parameters, we |
1206 // can finally log the message. | 1218 // can finally log the message. |
1207 typename TupleTypes<ReplyParam>::ValueTuple p; | 1219 typename ReplyParam::ValueTuple p; |
1208 void* iter = SyncMessage::GetDataIterator(msg); | 1220 void* iter = SyncMessage::GetDataIterator(msg); |
1209 if (ReadParam(msg, &iter, &p)) | 1221 if (ReadParam(msg, &iter, &p)) |
1210 LogParam(p, l); | 1222 LogParam(p, l); |
1211 } | 1223 } |
1212 } | 1224 } |
1213 | 1225 |
1214 template<class T, class Method> | 1226 template<class T, class Method> |
1215 static bool Dispatch(const Message* msg, T* obj, Method func) { | 1227 static bool Dispatch(const Message* msg, T* obj, Method func) { |
1216 SendParam send_params; | 1228 SendParam send_params; |
1217 void* iter = GetDataIterator(msg); | 1229 void* iter = GetDataIterator(msg); |
1218 Message* reply = GenerateReply(msg); | 1230 Message* reply = GenerateReply(msg); |
1219 bool error; | 1231 bool error; |
1220 if (ReadParam(msg, &iter, &send_params)) { | 1232 if (ReadParam(msg, &iter, &send_params)) { |
1221 typename TupleTypes<ReplyParam>::ValueTuple reply_params; | 1233 typename ReplyParam::ValueTuple reply_params; |
1222 DispatchToMethod(obj, func, send_params, &reply_params); | 1234 DispatchToMethod(obj, func, send_params, &reply_params); |
1223 WriteParam(reply, reply_params); | 1235 WriteParam(reply, reply_params); |
1224 error = false; | 1236 error = false; |
1225 #ifdef IPC_MESSAGE_LOG_ENABLED | 1237 #ifdef IPC_MESSAGE_LOG_ENABLED |
1226 if (msg->received_time() != 0) { | 1238 if (msg->received_time() != 0) { |
1227 std::wstring output_params; | 1239 std::wstring output_params; |
1228 LogParam(reply_params, &output_params); | 1240 LogParam(reply_params, &output_params); |
1229 msg->set_output_params(output_params); | 1241 msg->set_output_params(output_params); |
1230 } | 1242 } |
1231 #endif | 1243 #endif |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1299 ReplyParam p(a, b, c, d, e); | 1311 ReplyParam p(a, b, c, d, e); |
1300 WriteParam(reply, p); | 1312 WriteParam(reply, p); |
1301 } | 1313 } |
1302 }; | 1314 }; |
1303 | 1315 |
1304 //----------------------------------------------------------------------------- | 1316 //----------------------------------------------------------------------------- |
1305 | 1317 |
1306 } // namespace IPC | 1318 } // namespace IPC |
1307 | 1319 |
1308 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1320 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
OLD | NEW |