Chromium Code Reviews| 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/time.h" | 21 #include "base/utf_string_conversions.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 26 matching lines...) Expand all Loading... | |
| 58 GpuMsgStart, | 58 GpuMsgStart, |
| 59 GpuHostMsgStart, | 59 GpuHostMsgStart, |
| 60 GpuChannelMsgStart, | 60 GpuChannelMsgStart, |
| 61 ServiceMsgStart, | 61 ServiceMsgStart, |
| 62 ServiceHostMsgStart, | 62 ServiceHostMsgStart, |
| 63 // NOTE: When you add a new message class, also update | 63 // NOTE: When you add a new message class, also update |
| 64 // IPCStatusView::IPCStatusView to ensure logging works. | 64 // IPCStatusView::IPCStatusView to ensure logging works. |
| 65 LastMsgIndex | 65 LastMsgIndex |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 class DictionaryValue; | |
| 69 class ListValue; | |
| 70 | |
| 71 namespace base { | |
| 72 class Time; | |
| 73 } | |
| 74 | |
| 68 namespace IPC { | 75 namespace IPC { |
| 69 | 76 |
| 70 //----------------------------------------------------------------------------- | 77 //----------------------------------------------------------------------------- |
| 71 // An iterator class for reading the fields contained within a Message. | 78 // An iterator class for reading the fields contained within a Message. |
| 72 | 79 |
| 73 class MessageIterator { | 80 class MessageIterator { |
| 74 public: | 81 public: |
| 75 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { | 82 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { |
| 76 } | 83 } |
| 77 int NextInt() const { | 84 int NextInt() const { |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 return result; | 304 return result; |
| 298 } | 305 } |
| 299 static void Log(const param_type& p, std::wstring* l) { | 306 static void Log(const param_type& p, std::wstring* l) { |
| 300 l->append(StringPrintf(L"%lc", p)); | 307 l->append(StringPrintf(L"%lc", p)); |
| 301 } | 308 } |
| 302 }; | 309 }; |
| 303 | 310 |
| 304 template <> | 311 template <> |
| 305 struct ParamTraits<base::Time> { | 312 struct ParamTraits<base::Time> { |
| 306 typedef base::Time param_type; | 313 typedef base::Time param_type; |
| 307 static void Write(Message* m, const param_type& p) { | 314 static void Write(Message* m, const param_type& p); |
| 308 ParamTraits<int64>::Write(m, p.ToInternalValue()); | 315 static bool Read(const Message* m, void** iter, param_type* r); |
| 309 } | 316 static void Log(const param_type& p, std::wstring* l); |
| 310 static bool Read(const Message* m, void** iter, param_type* r) { | |
| 311 int64 value; | |
| 312 if (!ParamTraits<int64>::Read(m, iter, &value)) | |
| 313 return false; | |
| 314 *r = base::Time::FromInternalValue(value); | |
| 315 return true; | |
| 316 } | |
| 317 static void Log(const param_type& p, std::wstring* l) { | |
| 318 ParamTraits<int64>::Log(p.ToInternalValue(), l); | |
| 319 } | |
| 320 }; | 317 }; |
| 321 | 318 |
| 322 #if defined(OS_WIN) | 319 #if defined(OS_WIN) |
| 323 template <> | 320 template <> |
| 324 struct ParamTraits<LOGFONT> { | 321 struct ParamTraits<LOGFONT> { |
| 325 typedef LOGFONT param_type; | 322 typedef LOGFONT param_type; |
| 326 static void Write(Message* m, const param_type& p) { | 323 static void Write(Message* m, const param_type& p) { |
| 327 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); | 324 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 328 } | 325 } |
| 329 static bool Read(const Message* m, void** iter, param_type* r) { | 326 static bool Read(const Message* m, void** iter, param_type* r) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 356 bool result = m->ReadData(iter, &data, &data_size); | 353 bool result = m->ReadData(iter, &data, &data_size); |
| 357 if (result && data_size == sizeof(MSG)) { | 354 if (result && data_size == sizeof(MSG)) { |
| 358 memcpy(r, data, sizeof(MSG)); | 355 memcpy(r, data, sizeof(MSG)); |
| 359 } else { | 356 } else { |
| 360 result = false; | 357 result = false; |
| 361 NOTREACHED(); | 358 NOTREACHED(); |
| 362 } | 359 } |
| 363 | 360 |
| 364 return result; | 361 return result; |
| 365 } | 362 } |
| 363 static void Log(const param_type& p, std::wstring* l) { | |
| 364 l->append(L"<MSG>"); | |
| 365 } | |
| 366 }; | 366 }; |
| 367 #endif // defined(OS_WIN) | 367 #endif // defined(OS_WIN) |
| 368 | 368 |
| 369 template <> | 369 template <> |
| 370 struct ParamTraits<DictionaryValue> { | 370 struct ParamTraits<DictionaryValue> { |
| 371 typedef DictionaryValue param_type; | 371 typedef DictionaryValue param_type; |
| 372 static void Write(Message* m, const param_type& p); | 372 static void Write(Message* m, const param_type& p); |
| 373 static bool Read(const Message* m, void** iter, param_type* r); | 373 static bool Read(const Message* m, void** iter, param_type* r); |
| 374 static void Log(const param_type& p, std::wstring* l); | 374 static void Log(const param_type& p, std::wstring* l); |
| 375 }; | 375 }; |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1017 }; | 1017 }; |
| 1018 | 1018 |
| 1019 //----------------------------------------------------------------------------- | 1019 //----------------------------------------------------------------------------- |
| 1020 // Generic message subclasses | 1020 // Generic message subclasses |
| 1021 | 1021 |
| 1022 // Used for asynchronous messages. | 1022 // Used for asynchronous messages. |
| 1023 template <class ParamType> | 1023 template <class ParamType> |
| 1024 class MessageWithTuple : public Message { | 1024 class MessageWithTuple : public Message { |
| 1025 public: | 1025 public: |
| 1026 typedef ParamType Param; | 1026 typedef ParamType Param; |
| 1027 typedef typename ParamType::ParamTuple RefParam; | 1027 typedef typename TupleTypes<ParamType>::ParamTuple RefParam; |
| 1028 | 1028 |
| 1029 MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p) | 1029 // The constructor and the Read() method's templated implementations are in |
| 1030 : Message(routing_id, type, PRIORITY_NORMAL) { | 1030 // ipc_message_utils_impl.h. The subclass constructor and Log() methods call |
| 1031 WriteParam(this, p); | 1031 // the templated versions of these and make sure there are instantiations in |
| 1032 } | 1032 // those translation units. |
| 1033 MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p); | |
| 1033 | 1034 |
| 1035 // TODO(erg): Migrate this method into ipc_message_utils_impl.h once I figure | |
| 1036 // out why just having the template in that file and the forward declaration | |
| 1037 // here breaks the release build. | |
| 1034 static bool Read(const Message* msg, Param* p) { | 1038 static bool Read(const Message* msg, Param* p) { |
|
Matt Perry
2010/08/09 20:56:28
Interesting, it still compiles with the forward de
Elliot Glaysher
2010/08/09 21:04:04
Correct. Since it is a reference type (and we don'
| |
| 1035 void* iter = NULL; | 1039 void* iter = NULL; |
| 1036 if (ReadParam(msg, &iter, p)) | 1040 if (ReadParam(msg, &iter, p)) |
| 1037 return true; | 1041 return true; |
| 1038 NOTREACHED() << "Error deserializing message " << msg->type(); | 1042 NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1039 return false; | 1043 return false; |
| 1040 } | 1044 } |
| 1041 | 1045 |
| 1042 // Generic dispatcher. Should cover most cases. | 1046 // Generic dispatcher. Should cover most cases. |
| 1043 template<class T, class Method> | 1047 template<class T, class Method> |
| 1044 static bool Dispatch(const Message* msg, T* obj, Method func) { | 1048 static bool Dispatch(const Message* msg, T* obj, Method func) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1102 static bool Dispatch(const Message* msg, T* obj, | 1106 static bool Dispatch(const Message* msg, T* obj, |
| 1103 void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { | 1107 void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { |
| 1104 Param p; | 1108 Param p; |
| 1105 if (Read(msg, &p)) { | 1109 if (Read(msg, &p)) { |
| 1106 (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); | 1110 (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); |
| 1107 return true; | 1111 return true; |
| 1108 } | 1112 } |
| 1109 return false; | 1113 return false; |
| 1110 } | 1114 } |
| 1111 | 1115 |
| 1112 static void Log(const Message* msg, std::wstring* l) { | |
| 1113 Param p; | |
| 1114 if (Read(msg, &p)) | |
| 1115 LogParam(p, l); | |
| 1116 } | |
| 1117 | |
| 1118 // Functions used to do manual unpacking. Only used by the automation code, | 1116 // Functions used to do manual unpacking. Only used by the automation code, |
| 1119 // these should go away once that code uses SyncChannel. | 1117 // these should go away once that code uses SyncChannel. |
| 1120 template<typename TA, typename TB> | 1118 template<typename TA, typename TB> |
| 1121 static bool Read(const IPC::Message* msg, TA* a, TB* b) { | 1119 static bool Read(const IPC::Message* msg, TA* a, TB* b) { |
| 1122 ParamType params; | 1120 ParamType params; |
| 1123 if (!Read(msg, ¶ms)) | 1121 if (!Read(msg, ¶ms)) |
| 1124 return false; | 1122 return false; |
| 1125 *a = params.a; | 1123 *a = params.a; |
| 1126 *b = params.b; | 1124 *b = params.b; |
| 1127 return true; | 1125 return true; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1180 | 1178 |
| 1181 // defined in ipc_logging.cc | 1179 // defined in ipc_logging.cc |
| 1182 void GenerateLogData(const std::string& channel, const Message& message, | 1180 void GenerateLogData(const std::string& channel, const Message& message, |
| 1183 LogData* data); | 1181 LogData* data); |
| 1184 | 1182 |
| 1185 // Used for synchronous messages. | 1183 // Used for synchronous messages. |
| 1186 template <class SendParamType, class ReplyParamType> | 1184 template <class SendParamType, class ReplyParamType> |
| 1187 class MessageWithReply : public SyncMessage { | 1185 class MessageWithReply : public SyncMessage { |
| 1188 public: | 1186 public: |
| 1189 typedef SendParamType SendParam; | 1187 typedef SendParamType SendParam; |
| 1190 typedef typename SendParam::ParamTuple RefSendParam; | 1188 typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; |
| 1191 typedef ReplyParamType ReplyParam; | 1189 typedef ReplyParamType ReplyParam; |
| 1192 | 1190 |
| 1193 MessageWithReply(int32 routing_id, uint32 type, | 1191 MessageWithReply(int32 routing_id, uint32 type, |
| 1194 const RefSendParam& send, const ReplyParam& reply) | 1192 const RefSendParam& send, const ReplyParam& reply) |
| 1195 : SyncMessage(routing_id, type, PRIORITY_NORMAL, | 1193 : SyncMessage(routing_id, type, PRIORITY_NORMAL, |
| 1196 new ParamDeserializer<ReplyParam>(reply)) { | 1194 new ParamDeserializer<ReplyParam>(reply)) { |
| 1197 WriteParam(this, send); | 1195 WriteParam(this, send); |
| 1198 } | 1196 } |
| 1199 | 1197 |
| 1200 static void Log(const Message* msg, std::wstring* l) { | 1198 static void Log(const Message* msg, std::wstring* l) { |
| 1201 if (msg->is_sync()) { | 1199 if (msg->is_sync()) { |
| 1202 SendParam p; | 1200 SendParam p; |
| 1203 void* iter = SyncMessage::GetDataIterator(msg); | 1201 void* iter = SyncMessage::GetDataIterator(msg); |
| 1204 if (ReadParam(msg, &iter, &p)) | 1202 if (ReadParam(msg, &iter, &p)) |
| 1205 LogParam(p, l); | 1203 LogParam(p, l); |
| 1206 | 1204 |
| 1207 #if defined(IPC_MESSAGE_LOG_ENABLED) | 1205 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 1208 const std::wstring& output_params = msg->output_params(); | 1206 const std::wstring& output_params = msg->output_params(); |
| 1209 if (!l->empty() && !output_params.empty()) | 1207 if (!l->empty() && !output_params.empty()) |
| 1210 l->append(L", "); | 1208 l->append(L", "); |
| 1211 | 1209 |
| 1212 l->append(output_params); | 1210 l->append(output_params); |
| 1213 #endif | 1211 #endif |
| 1214 } else { | 1212 } else { |
| 1215 // This is an outgoing reply. Now that we have the output parameters, we | 1213 // This is an outgoing reply. Now that we have the output parameters, we |
| 1216 // can finally log the message. | 1214 // can finally log the message. |
| 1217 typename ReplyParam::ValueTuple p; | 1215 typename TupleTypes<ReplyParam>::ValueTuple p; |
| 1218 void* iter = SyncMessage::GetDataIterator(msg); | 1216 void* iter = SyncMessage::GetDataIterator(msg); |
| 1219 if (ReadParam(msg, &iter, &p)) | 1217 if (ReadParam(msg, &iter, &p)) |
| 1220 LogParam(p, l); | 1218 LogParam(p, l); |
| 1221 } | 1219 } |
| 1222 } | 1220 } |
| 1223 | 1221 |
| 1224 template<class T, class Method> | 1222 template<class T, class Method> |
| 1225 static bool Dispatch(const Message* msg, T* obj, Method func) { | 1223 static bool Dispatch(const Message* msg, T* obj, Method func) { |
| 1226 SendParam send_params; | 1224 SendParam send_params; |
| 1227 void* iter = GetDataIterator(msg); | 1225 void* iter = GetDataIterator(msg); |
| 1228 Message* reply = GenerateReply(msg); | 1226 Message* reply = GenerateReply(msg); |
| 1229 bool error; | 1227 bool error; |
| 1230 if (ReadParam(msg, &iter, &send_params)) { | 1228 if (ReadParam(msg, &iter, &send_params)) { |
| 1231 typename ReplyParam::ValueTuple reply_params; | 1229 typename TupleTypes<ReplyParam>::ValueTuple reply_params; |
| 1232 DispatchToMethod(obj, func, send_params, &reply_params); | 1230 DispatchToMethod(obj, func, send_params, &reply_params); |
| 1233 WriteParam(reply, reply_params); | 1231 WriteParam(reply, reply_params); |
| 1234 error = false; | 1232 error = false; |
| 1235 #ifdef IPC_MESSAGE_LOG_ENABLED | 1233 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 1236 if (msg->received_time() != 0) { | 1234 if (msg->received_time() != 0) { |
| 1237 std::wstring output_params; | 1235 std::wstring output_params; |
| 1238 LogParam(reply_params, &output_params); | 1236 LogParam(reply_params, &output_params); |
| 1239 msg->set_output_params(output_params); | 1237 msg->set_output_params(output_params); |
| 1240 } | 1238 } |
| 1241 #endif | 1239 #endif |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1309 ReplyParam p(a, b, c, d, e); | 1307 ReplyParam p(a, b, c, d, e); |
| 1310 WriteParam(reply, p); | 1308 WriteParam(reply, p); |
| 1311 } | 1309 } |
| 1312 }; | 1310 }; |
| 1313 | 1311 |
| 1314 //----------------------------------------------------------------------------- | 1312 //----------------------------------------------------------------------------- |
| 1315 | 1313 |
| 1316 } // namespace IPC | 1314 } // namespace IPC |
| 1317 | 1315 |
| 1318 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1316 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
| OLD | NEW |