[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 | #define IPC_IPC_MESSAGE_UTILS_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 8 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | #include <map> |
| 12 | |
[email protected] | 690a99c | 2009-01-06 16:48:45 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 14 | #include "base/format_macros.h" |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 15 | #include "base/nullable_string16.h" |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 16 | #include "base/string16.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 18 | #include "base/utf_string_conversions.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 19 | #include "base/time.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "base/tuple.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 21 | #include "base/values.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 22 | #if defined(OS_POSIX) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 23 | #include "ipc/file_descriptor_set_posix.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 24 | #endif |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 25 | #include "ipc/ipc_channel_handle.h" |
| 26 | #include "ipc/ipc_sync_message.h" |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 27 | |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 28 | // Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique |
| 29 | // base. Messages have unique IDs across channels in order for the IPC logging |
| 30 | // code to figure out the message class from its ID. |
| 31 | enum IPCMessageStart { |
| 32 | // By using a start value of 0 for automation messages, we keep backward |
| 33 | // compatibility with old builds. |
| 34 | AutomationMsgStart = 0, |
| 35 | ViewMsgStart, |
| 36 | ViewHostMsgStart, |
| 37 | PluginProcessMsgStart, |
| 38 | PluginProcessHostMsgStart, |
| 39 | PluginMsgStart, |
| 40 | PluginHostMsgStart, |
| 41 | NPObjectMsgStart, |
| 42 | TestMsgStart, |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 43 | DevToolsAgentMsgStart, |
| 44 | DevToolsClientMsgStart, |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 45 | WorkerProcessMsgStart, |
| 46 | WorkerProcessHostMsgStart, |
| 47 | WorkerMsgStart, |
| 48 | WorkerHostMsgStart, |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 49 | NaClProcessMsgStart, |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 50 | GpuCommandBufferMsgStart, |
[email protected] | 60f1439 | 2009-12-15 20:46:32 | [diff] [blame] | 51 | UtilityMsgStart, |
| 52 | UtilityHostMsgStart, |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 53 | GpuMsgStart, |
| 54 | GpuHostMsgStart, |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 55 | GpuChannelMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 56 | // NOTE: When you add a new message class, also update |
| 57 | // IPCStatusView::IPCStatusView to ensure logging works. |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 58 | LastMsgIndex |
| 59 | }; |
| 60 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | namespace IPC { |
| 62 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | //----------------------------------------------------------------------------- |
| 64 | // An iterator class for reading the fields contained within a Message. |
| 65 | |
| 66 | class MessageIterator { |
| 67 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 68 | explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | } |
| 70 | int NextInt() const { |
[email protected] | 9422b7dc | 2009-12-30 20:09:02 | [diff] [blame] | 71 | int val = -1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | if (!msg_.ReadInt(&iter_, &val)) |
| 73 | NOTREACHED(); |
| 74 | return val; |
| 75 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | const std::string NextString() const { |
| 77 | std::string val; |
| 78 | if (!msg_.ReadString(&iter_, &val)) |
| 79 | NOTREACHED(); |
| 80 | return val; |
| 81 | } |
| 82 | const std::wstring NextWString() const { |
| 83 | std::wstring val; |
| 84 | if (!msg_.ReadWString(&iter_, &val)) |
| 85 | NOTREACHED(); |
| 86 | return val; |
| 87 | } |
[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 88 | void NextData(const char** data, int* length) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | if (!msg_.ReadData(&iter_, data, length)) { |
| 90 | NOTREACHED(); |
| 91 | } |
| 92 | } |
| 93 | private: |
| 94 | const Message& msg_; |
| 95 | mutable void* iter_; |
| 96 | }; |
| 97 | |
| 98 | //----------------------------------------------------------------------------- |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 99 | // ParamTraits specializations, etc. |
| 100 | |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 101 | template <class P> struct ParamTraits { |
| 102 | }; |
| 103 | |
| 104 | template <class P> |
| 105 | struct SimilarTypeTraits { |
| 106 | typedef P Type; |
| 107 | }; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 108 | |
| 109 | template <class P> |
| 110 | static inline void WriteParam(Message* m, const P& p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 111 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 112 | ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | template <class P> |
[email protected] | 1e86aa6 | 2009-04-24 21:22:33 | [diff] [blame] | 116 | static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, void** iter, |
| 117 | P* p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 118 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 119 | return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | template <class P> |
| 123 | static inline void LogParam(const P& p, std::wstring* l) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 124 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 125 | ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | template <> |
| 129 | struct ParamTraits<bool> { |
| 130 | typedef bool param_type; |
| 131 | static void Write(Message* m, const param_type& p) { |
| 132 | m->WriteBool(p); |
| 133 | } |
| 134 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 135 | return m->ReadBool(iter, r); |
| 136 | } |
| 137 | static void Log(const param_type& p, std::wstring* l) { |
| 138 | l->append(p ? L"true" : L"false"); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | template <> |
| 143 | struct ParamTraits<int> { |
| 144 | typedef int param_type; |
| 145 | static void Write(Message* m, const param_type& p) { |
| 146 | m->WriteInt(p); |
| 147 | } |
| 148 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 149 | return m->ReadInt(iter, r); |
| 150 | } |
| 151 | static void Log(const param_type& p, std::wstring* l) { |
| 152 | l->append(StringPrintf(L"%d", p)); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 157 | struct ParamTraits<unsigned int> { |
| 158 | typedef unsigned int param_type; |
| 159 | static void Write(Message* m, const param_type& p) { |
| 160 | m->WriteInt(p); |
| 161 | } |
| 162 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 163 | return m->ReadInt(iter, reinterpret_cast<int*>(r)); |
| 164 | } |
| 165 | static void Log(const param_type& p, std::wstring* l) { |
| 166 | l->append(StringPrintf(L"%d", p)); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 171 | struct ParamTraits<long> { |
| 172 | typedef long param_type; |
| 173 | static void Write(Message* m, const param_type& p) { |
| 174 | m->WriteLong(p); |
| 175 | } |
| 176 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 177 | return m->ReadLong(iter, r); |
| 178 | } |
| 179 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 180 | l->append(StringPrintf(L"%ld", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 181 | } |
| 182 | }; |
| 183 | |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 184 | template <> |
| 185 | struct ParamTraits<unsigned long> { |
| 186 | typedef unsigned long param_type; |
| 187 | static void Write(Message* m, const param_type& p) { |
| 188 | m->WriteLong(p); |
| 189 | } |
| 190 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 191 | return m->ReadLong(iter, reinterpret_cast<long*>(r)); |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 192 | } |
| 193 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 194 | l->append(StringPrintf(L"%lu", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 195 | } |
| 196 | }; |
| 197 | |
| 198 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 199 | struct ParamTraits<long long> { |
| 200 | typedef long long param_type; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 201 | static void Write(Message* m, const param_type& p) { |
| 202 | m->WriteInt64(static_cast<int64>(p)); |
| 203 | } |
| 204 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 205 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 206 | } |
| 207 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 208 | l->append(Int64ToWString(static_cast<int64>(p))); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | template <> |
| 213 | struct ParamTraits<unsigned long long> { |
| 214 | typedef unsigned long long param_type; |
| 215 | static void Write(Message* m, const param_type& p) { |
| 216 | m->WriteInt64(p); |
| 217 | } |
| 218 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 219 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 220 | } |
| 221 | static void Log(const param_type& p, std::wstring* l) { |
| 222 | l->append(Uint64ToWString(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 223 | } |
| 224 | }; |
| 225 | |
| 226 | template <> |
| 227 | struct ParamTraits<double> { |
| 228 | typedef double param_type; |
| 229 | static void Write(Message* m, const param_type& p) { |
| 230 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 231 | } |
| 232 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 233 | const char *data; |
| 234 | int data_size = 0; |
| 235 | bool result = m->ReadData(iter, &data, &data_size); |
| 236 | if (result && data_size == sizeof(param_type)) { |
| 237 | memcpy(r, data, sizeof(param_type)); |
| 238 | } else { |
| 239 | result = false; |
| 240 | NOTREACHED(); |
| 241 | } |
| 242 | |
| 243 | return result; |
| 244 | } |
| 245 | static void Log(const param_type& p, std::wstring* l) { |
| 246 | l->append(StringPrintf(L"e", p)); |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | template <> |
| 251 | struct ParamTraits<wchar_t> { |
| 252 | typedef wchar_t param_type; |
| 253 | static void Write(Message* m, const param_type& p) { |
| 254 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 255 | } |
| 256 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 257 | const char *data; |
| 258 | int data_size = 0; |
| 259 | bool result = m->ReadData(iter, &data, &data_size); |
| 260 | if (result && data_size == sizeof(param_type)) { |
| 261 | memcpy(r, data, sizeof(param_type)); |
| 262 | } else { |
| 263 | result = false; |
| 264 | NOTREACHED(); |
| 265 | } |
| 266 | |
| 267 | return result; |
| 268 | } |
| 269 | static void Log(const param_type& p, std::wstring* l) { |
| 270 | l->append(StringPrintf(L"%lc", p)); |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | template <> |
| 275 | struct ParamTraits<base::Time> { |
| 276 | typedef base::Time param_type; |
| 277 | static void Write(Message* m, const param_type& p) { |
| 278 | ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 279 | } |
| 280 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 281 | int64 value; |
| 282 | if (!ParamTraits<int64>::Read(m, iter, &value)) |
| 283 | return false; |
| 284 | *r = base::Time::FromInternalValue(value); |
| 285 | return true; |
| 286 | } |
| 287 | static void Log(const param_type& p, std::wstring* l) { |
| 288 | ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | #if defined(OS_WIN) |
| 293 | template <> |
| 294 | struct ParamTraits<LOGFONT> { |
| 295 | typedef LOGFONT param_type; |
| 296 | static void Write(Message* m, const param_type& p) { |
| 297 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 298 | } |
| 299 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 300 | const char *data; |
| 301 | int data_size = 0; |
| 302 | bool result = m->ReadData(iter, &data, &data_size); |
| 303 | if (result && data_size == sizeof(LOGFONT)) { |
| 304 | memcpy(r, data, sizeof(LOGFONT)); |
| 305 | } else { |
| 306 | result = false; |
| 307 | NOTREACHED(); |
| 308 | } |
| 309 | |
| 310 | return result; |
| 311 | } |
| 312 | static void Log(const param_type& p, std::wstring* l) { |
| 313 | l->append(StringPrintf(L"<LOGFONT>")); |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | template <> |
| 318 | struct ParamTraits<MSG> { |
| 319 | typedef MSG param_type; |
| 320 | static void Write(Message* m, const param_type& p) { |
| 321 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 322 | } |
| 323 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 324 | const char *data; |
| 325 | int data_size = 0; |
| 326 | bool result = m->ReadData(iter, &data, &data_size); |
| 327 | if (result && data_size == sizeof(MSG)) { |
| 328 | memcpy(r, data, sizeof(MSG)); |
| 329 | } else { |
| 330 | result = false; |
| 331 | NOTREACHED(); |
| 332 | } |
| 333 | |
| 334 | return result; |
| 335 | } |
| 336 | }; |
| 337 | #endif // defined(OS_WIN) |
| 338 | |
| 339 | template <> |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 340 | struct ParamTraits<DictionaryValue> { |
| 341 | typedef DictionaryValue param_type; |
| 342 | static void Write(Message* m, const param_type& p); |
| 343 | static bool Read(const Message* m, void** iter, param_type* r); |
| 344 | static void Log(const param_type& p, std::wstring* l); |
| 345 | }; |
| 346 | |
| 347 | template <> |
| 348 | struct ParamTraits<ListValue> { |
| 349 | typedef ListValue param_type; |
| 350 | static void Write(Message* m, const param_type& p); |
| 351 | static bool Read(const Message* m, void** iter, param_type* r); |
| 352 | static void Log(const param_type& p, std::wstring* l); |
| 353 | }; |
| 354 | |
| 355 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 356 | struct ParamTraits<std::string> { |
| 357 | typedef std::string param_type; |
| 358 | static void Write(Message* m, const param_type& p) { |
| 359 | m->WriteString(p); |
| 360 | } |
| 361 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 362 | return m->ReadString(iter, r); |
| 363 | } |
| 364 | static void Log(const param_type& p, std::wstring* l) { |
| 365 | l->append(UTF8ToWide(p)); |
| 366 | } |
| 367 | }; |
| 368 | |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 369 | template<typename CharType> |
| 370 | static void LogBytes(const std::vector<CharType>& data, std::wstring* out) { |
| 371 | #if defined(OS_WIN) |
| 372 | // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 373 | for (size_t i = 0; i < data.size(); ++i) |
| 374 | out->push_back(data[i]); |
| 375 | #else |
| 376 | // On POSIX, we log to stdout, which we assume can display ASCII. |
| 377 | static const size_t kMaxBytesToLog = 100; |
| 378 | for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
| 379 | if (isprint(data[i])) |
| 380 | out->push_back(data[i]); |
| 381 | else |
| 382 | out->append(StringPrintf(L"[%02X]", static_cast<unsigned char>(data[i]))); |
| 383 | } |
| 384 | if (data.size() > kMaxBytesToLog) { |
| 385 | out->append( |
| 386 | StringPrintf(L" and %u more bytes", |
| 387 | static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
| 388 | } |
| 389 | #endif |
| 390 | } |
| 391 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 392 | template <> |
| 393 | struct ParamTraits<std::vector<unsigned char> > { |
| 394 | typedef std::vector<unsigned char> param_type; |
| 395 | static void Write(Message* m, const param_type& p) { |
| 396 | if (p.size() == 0) { |
| 397 | m->WriteData(NULL, 0); |
| 398 | } else { |
| 399 | m->WriteData(reinterpret_cast<const char*>(&p.front()), |
| 400 | static_cast<int>(p.size())); |
| 401 | } |
| 402 | } |
| 403 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 404 | const char *data; |
| 405 | int data_size = 0; |
| 406 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 407 | return false; |
| 408 | r->resize(data_size); |
| 409 | if (data_size) |
| 410 | memcpy(&r->front(), data, data_size); |
| 411 | return true; |
| 412 | } |
| 413 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 414 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 415 | } |
| 416 | }; |
| 417 | |
| 418 | template <> |
| 419 | struct ParamTraits<std::vector<char> > { |
| 420 | typedef std::vector<char> param_type; |
| 421 | static void Write(Message* m, const param_type& p) { |
| 422 | if (p.size() == 0) { |
| 423 | m->WriteData(NULL, 0); |
| 424 | } else { |
| 425 | m->WriteData(&p.front(), static_cast<int>(p.size())); |
| 426 | } |
| 427 | } |
| 428 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 429 | const char *data; |
| 430 | int data_size = 0; |
| 431 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 432 | return false; |
| 433 | r->resize(data_size); |
| 434 | if (data_size) |
| 435 | memcpy(&r->front(), data, data_size); |
| 436 | return true; |
| 437 | } |
| 438 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 439 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 440 | } |
| 441 | }; |
| 442 | |
| 443 | template <class P> |
| 444 | struct ParamTraits<std::vector<P> > { |
| 445 | typedef std::vector<P> param_type; |
| 446 | static void Write(Message* m, const param_type& p) { |
| 447 | WriteParam(m, static_cast<int>(p.size())); |
| 448 | for (size_t i = 0; i < p.size(); i++) |
| 449 | WriteParam(m, p[i]); |
| 450 | } |
| 451 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 452 | int size; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 453 | // ReadLength() checks for < 0 itself. |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 454 | if (!m->ReadLength(iter, &size)) |
| 455 | return false; |
| 456 | // Resizing beforehand is not safe, see BUG 1006367 for details. |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 457 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 458 | return false; |
| 459 | r->resize(size); |
| 460 | for (int i = 0; i < size; i++) { |
| 461 | if (!ReadParam(m, iter, &(*r)[i])) |
| 462 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 463 | } |
| 464 | return true; |
| 465 | } |
| 466 | static void Log(const param_type& p, std::wstring* l) { |
| 467 | for (size_t i = 0; i < p.size(); ++i) { |
| 468 | if (i != 0) |
| 469 | l->append(L" "); |
| 470 | |
| 471 | LogParam((p[i]), l); |
| 472 | } |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | template <class K, class V> |
| 477 | struct ParamTraits<std::map<K, V> > { |
| 478 | typedef std::map<K, V> param_type; |
| 479 | static void Write(Message* m, const param_type& p) { |
| 480 | WriteParam(m, static_cast<int>(p.size())); |
| 481 | typename param_type::const_iterator iter; |
| 482 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 483 | WriteParam(m, iter->first); |
| 484 | WriteParam(m, iter->second); |
| 485 | } |
| 486 | } |
| 487 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 488 | int size; |
| 489 | if (!ReadParam(m, iter, &size) || size < 0) |
| 490 | return false; |
| 491 | for (int i = 0; i < size; ++i) { |
| 492 | K k; |
| 493 | if (!ReadParam(m, iter, &k)) |
| 494 | return false; |
| 495 | V& value = (*r)[k]; |
| 496 | if (!ReadParam(m, iter, &value)) |
| 497 | return false; |
| 498 | } |
| 499 | return true; |
| 500 | } |
| 501 | static void Log(const param_type& p, std::wstring* l) { |
| 502 | l->append(L"<std::map>"); |
| 503 | } |
| 504 | }; |
| 505 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 506 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 507 | template <> |
| 508 | struct ParamTraits<std::wstring> { |
| 509 | typedef std::wstring param_type; |
| 510 | static void Write(Message* m, const param_type& p) { |
| 511 | m->WriteWString(p); |
| 512 | } |
| 513 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 514 | return m->ReadWString(iter, r); |
| 515 | } |
| 516 | static void Log(const param_type& p, std::wstring* l) { |
| 517 | l->append(p); |
| 518 | } |
| 519 | }; |
| 520 | |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 521 | template <class A, class B> |
| 522 | struct ParamTraits<std::pair<A, B> > { |
| 523 | typedef std::pair<A, B> param_type; |
| 524 | static void Write(Message* m, const param_type& p) { |
| 525 | WriteParam(m, p.first); |
| 526 | WriteParam(m, p.second); |
| 527 | } |
| 528 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 529 | return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 530 | } |
| 531 | static void Log(const param_type& p, std::wstring* l) { |
| 532 | l->append(L"("); |
| 533 | LogParam(p.first, l); |
| 534 | l->append(L", "); |
| 535 | LogParam(p.second, l); |
| 536 | l->append(L")"); |
| 537 | } |
| 538 | }; |
| 539 | |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 540 | template <> |
| 541 | struct ParamTraits<NullableString16> { |
| 542 | typedef NullableString16 param_type; |
| 543 | static void Write(Message* m, const param_type& p) { |
| 544 | WriteParam(m, p.string()); |
| 545 | WriteParam(m, p.is_null()); |
| 546 | } |
| 547 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 548 | string16 string; |
| 549 | if (!ReadParam(m, iter, &string)) |
| 550 | return false; |
| 551 | bool is_null; |
| 552 | if (!ReadParam(m, iter, &is_null)) |
| 553 | return false; |
| 554 | *r = NullableString16(string, is_null); |
| 555 | return true; |
| 556 | } |
| 557 | static void Log(const param_type& p, std::wstring* l) { |
| 558 | l->append(L"("); |
| 559 | LogParam(p.string(), l); |
| 560 | l->append(L", "); |
| 561 | LogParam(p.is_null(), l); |
| 562 | l->append(L")"); |
| 563 | } |
| 564 | }; |
| 565 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 566 | // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't |
| 567 | // need this trait. |
| 568 | #if !defined(WCHAR_T_IS_UTF16) |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 569 | template <> |
| 570 | struct ParamTraits<string16> { |
| 571 | typedef string16 param_type; |
| 572 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 573 | m->WriteString16(p); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 574 | } |
| 575 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 576 | return m->ReadString16(iter, r); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 577 | } |
| 578 | static void Log(const param_type& p, std::wstring* l) { |
| 579 | l->append(UTF16ToWide(p)); |
| 580 | } |
| 581 | }; |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 582 | #endif |
| 583 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 584 | // and, a few more useful types... |
| 585 | #if defined(OS_WIN) |
| 586 | template <> |
| 587 | struct ParamTraits<HANDLE> { |
| 588 | typedef HANDLE param_type; |
| 589 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 590 | // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 |
| 591 | // bit systems. |
| 592 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 593 | } |
| 594 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 595 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 596 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 597 | } |
| 598 | static void Log(const param_type& p, std::wstring* l) { |
| 599 | l->append(StringPrintf(L"0x%X", p)); |
| 600 | } |
| 601 | }; |
| 602 | |
| 603 | template <> |
| 604 | struct ParamTraits<HCURSOR> { |
| 605 | typedef HCURSOR param_type; |
| 606 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 607 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 608 | } |
| 609 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 610 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 611 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 612 | } |
| 613 | static void Log(const param_type& p, std::wstring* l) { |
| 614 | l->append(StringPrintf(L"0x%X", p)); |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 619 | struct ParamTraits<HACCEL> { |
| 620 | typedef HACCEL param_type; |
| 621 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 622 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 623 | } |
| 624 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 625 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 626 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 627 | } |
| 628 | }; |
| 629 | |
| 630 | template <> |
| 631 | struct ParamTraits<POINT> { |
| 632 | typedef POINT param_type; |
| 633 | static void Write(Message* m, const param_type& p) { |
| 634 | m->WriteInt(p.x); |
| 635 | m->WriteInt(p.y); |
| 636 | } |
| 637 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 638 | int x, y; |
| 639 | if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) |
| 640 | return false; |
| 641 | r->x = x; |
| 642 | r->y = y; |
| 643 | return true; |
| 644 | } |
| 645 | static void Log(const param_type& p, std::wstring* l) { |
| 646 | l->append(StringPrintf(L"(%d, %d)", p.x, p.y)); |
| 647 | } |
| 648 | }; |
| 649 | #endif // defined(OS_WIN) |
| 650 | |
| 651 | template <> |
| 652 | struct ParamTraits<FilePath> { |
| 653 | typedef FilePath param_type; |
| 654 | static void Write(Message* m, const param_type& p) { |
| 655 | ParamTraits<FilePath::StringType>::Write(m, p.value()); |
| 656 | } |
| 657 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 658 | FilePath::StringType value; |
| 659 | if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) |
| 660 | return false; |
| 661 | *r = FilePath(value); |
| 662 | return true; |
| 663 | } |
| 664 | static void Log(const param_type& p, std::wstring* l) { |
| 665 | ParamTraits<FilePath::StringType>::Log(p.value(), l); |
| 666 | } |
| 667 | }; |
| 668 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 669 | #if defined(OS_POSIX) |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 670 | // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 671 | // receiving side, the FileDescriptor is a valid duplicate of the file |
| 672 | // descriptor which was transmitted: *it is not just a copy of the integer like |
| 673 | // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 674 | // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 675 | // descriptor*. |
| 676 | // |
| 677 | // The received file descriptor will have the |auto_close| flag set to true. The |
| 678 | // code which handles the message is responsible for taking ownership of it. |
| 679 | // File descriptors are OS resources and must be closed when no longer needed. |
| 680 | // |
| 681 | // When sending a file descriptor, the file descriptor must be valid at the time |
| 682 | // of transmission. Since transmission is not synchronous, one should consider |
| 683 | // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 684 | // flag, which causes the file descriptor to be closed after writing. |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 685 | template<> |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 686 | struct ParamTraits<base::FileDescriptor> { |
| 687 | typedef base::FileDescriptor param_type; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 688 | static void Write(Message* m, const param_type& p) { |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 689 | const bool valid = p.fd >= 0; |
| 690 | WriteParam(m, valid); |
| 691 | |
| 692 | if (valid) { |
| 693 | if (!m->WriteFileDescriptor(p)) |
| 694 | NOTREACHED(); |
| 695 | } |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 696 | } |
| 697 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 698 | bool valid; |
| 699 | if (!ReadParam(m, iter, &valid)) |
| 700 | return false; |
| 701 | |
| 702 | if (!valid) { |
| 703 | r->fd = -1; |
| 704 | r->auto_close = false; |
| 705 | return true; |
| 706 | } |
| 707 | |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 708 | return m->ReadFileDescriptor(iter, r); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 709 | } |
| 710 | static void Log(const param_type& p, std::wstring* l) { |
| 711 | if (p.auto_close) { |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 712 | l->append(StringPrintf(L"FD(%d auto-close)", p.fd)); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 713 | } else { |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 714 | l->append(StringPrintf(L"FD(%d)", p.fd)); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | }; |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 718 | #endif // defined(OS_POSIX) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 719 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 720 | // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 721 | // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 722 | // on FileDescriptor for more background. |
| 723 | template<> |
| 724 | struct ParamTraits<IPC::ChannelHandle> { |
| 725 | typedef ChannelHandle param_type; |
| 726 | static void Write(Message* m, const param_type& p) { |
| 727 | WriteParam(m, p.name); |
| 728 | #if defined(OS_POSIX) |
| 729 | WriteParam(m, p.socket); |
| 730 | #endif |
| 731 | } |
| 732 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 733 | return ReadParam(m, iter, &r->name) |
| 734 | #if defined(OS_POSIX) |
| 735 | && ReadParam(m, iter, &r->socket) |
| 736 | #endif |
| 737 | ; |
| 738 | } |
| 739 | static void Log(const param_type& p, std::wstring* l) { |
[email protected] | e9abf8d | 2009-07-10 22:41:00 | [diff] [blame] | 740 | l->append(ASCIIToWide(StringPrintf("ChannelHandle(%s", p.name.c_str()))); |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 741 | #if defined(OS_POSIX) |
| 742 | ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
| 743 | #endif |
| 744 | l->append(L")"); |
| 745 | } |
| 746 | }; |
| 747 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 748 | #if defined(OS_WIN) |
| 749 | template <> |
| 750 | struct ParamTraits<XFORM> { |
| 751 | typedef XFORM param_type; |
| 752 | static void Write(Message* m, const param_type& p) { |
| 753 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); |
| 754 | } |
| 755 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 756 | const char *data; |
| 757 | int data_size = 0; |
| 758 | bool result = m->ReadData(iter, &data, &data_size); |
| 759 | if (result && data_size == sizeof(XFORM)) { |
| 760 | memcpy(r, data, sizeof(XFORM)); |
| 761 | } else { |
| 762 | result = false; |
| 763 | NOTREACHED(); |
| 764 | } |
| 765 | |
| 766 | return result; |
| 767 | } |
| 768 | static void Log(const param_type& p, std::wstring* l) { |
| 769 | l->append(L"<XFORM>"); |
| 770 | } |
| 771 | }; |
| 772 | #endif // defined(OS_WIN) |
| 773 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 774 | struct LogData { |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 775 | std::string channel; |
[email protected] | 8bef70e | 2009-04-14 19:11:24 | [diff] [blame] | 776 | int32 routing_id; |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 777 | uint32 type; // "User-defined" message type, from ipc_message.h. |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 778 | std::wstring flags; |
| 779 | int64 sent; // Time that the message was sent (i.e. at Send()). |
| 780 | int64 receive; // Time before it was dispatched (i.e. before calling |
| 781 | // OnMessageReceived). |
| 782 | int64 dispatch; // Time after it was dispatched (i.e. after calling |
| 783 | // OnMessageReceived). |
[email protected] | e707d5e6 | 2009-02-12 04:00:08 | [diff] [blame] | 784 | std::wstring message_name; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 785 | std::wstring params; |
| 786 | }; |
| 787 | |
| 788 | template <> |
| 789 | struct ParamTraits<LogData> { |
| 790 | typedef LogData param_type; |
| 791 | static void Write(Message* m, const param_type& p) { |
| 792 | WriteParam(m, p.channel); |
[email protected] | 8bef70e | 2009-04-14 19:11:24 | [diff] [blame] | 793 | WriteParam(m, p.routing_id); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 794 | WriteParam(m, static_cast<int>(p.type)); |
| 795 | WriteParam(m, p.flags); |
| 796 | WriteParam(m, p.sent); |
| 797 | WriteParam(m, p.receive); |
| 798 | WriteParam(m, p.dispatch); |
| 799 | WriteParam(m, p.params); |
| 800 | } |
| 801 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 802 | int type; |
| 803 | bool result = |
| 804 | ReadParam(m, iter, &r->channel) && |
[email protected] | 25af9205 | 2010-03-26 22:21:17 | [diff] [blame] | 805 | ReadParam(m, iter, &r->routing_id) && |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 806 | ReadParam(m, iter, &type) && |
| 807 | ReadParam(m, iter, &r->flags) && |
| 808 | ReadParam(m, iter, &r->sent) && |
| 809 | ReadParam(m, iter, &r->receive) && |
| 810 | ReadParam(m, iter, &r->dispatch) && |
| 811 | ReadParam(m, iter, &r->params); |
| 812 | r->type = static_cast<uint16>(type); |
| 813 | return result; |
| 814 | } |
| 815 | static void Log(const param_type& p, std::wstring* l) { |
| 816 | // Doesn't make sense to implement this! |
| 817 | } |
| 818 | }; |
| 819 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 820 | template <> |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 821 | struct ParamTraits<Message> { |
| 822 | static void Write(Message* m, const Message& p) { |
| 823 | m->WriteInt(p.size()); |
| 824 | m->WriteData(reinterpret_cast<const char*>(p.data()), p.size()); |
| 825 | } |
| 826 | static bool Read(const Message* m, void** iter, Message* r) { |
| 827 | int size; |
| 828 | if (!m->ReadInt(iter, &size)) |
| 829 | return false; |
| 830 | const char* data; |
| 831 | if (!m->ReadData(iter, &data, &size)) |
| 832 | return false; |
| 833 | *r = Message(data, size); |
| 834 | return true; |
| 835 | } |
| 836 | static void Log(const Message& p, std::wstring* l) { |
| 837 | l->append(L"<IPC::Message>"); |
| 838 | } |
| 839 | }; |
| 840 | |
| 841 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 842 | struct ParamTraits<Tuple0> { |
| 843 | typedef Tuple0 param_type; |
| 844 | static void Write(Message* m, const param_type& p) { |
| 845 | } |
| 846 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 847 | return true; |
| 848 | } |
| 849 | static void Log(const param_type& p, std::wstring* l) { |
| 850 | } |
| 851 | }; |
| 852 | |
| 853 | template <class A> |
| 854 | struct ParamTraits< Tuple1<A> > { |
| 855 | typedef Tuple1<A> param_type; |
| 856 | static void Write(Message* m, const param_type& p) { |
| 857 | WriteParam(m, p.a); |
| 858 | } |
| 859 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 860 | return ReadParam(m, iter, &r->a); |
| 861 | } |
| 862 | static void Log(const param_type& p, std::wstring* l) { |
| 863 | LogParam(p.a, l); |
| 864 | } |
| 865 | }; |
| 866 | |
| 867 | template <class A, class B> |
| 868 | struct ParamTraits< Tuple2<A, B> > { |
| 869 | typedef Tuple2<A, B> param_type; |
| 870 | static void Write(Message* m, const param_type& p) { |
| 871 | WriteParam(m, p.a); |
| 872 | WriteParam(m, p.b); |
| 873 | } |
| 874 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 875 | return (ReadParam(m, iter, &r->a) && |
| 876 | ReadParam(m, iter, &r->b)); |
| 877 | } |
| 878 | static void Log(const param_type& p, std::wstring* l) { |
| 879 | LogParam(p.a, l); |
| 880 | l->append(L", "); |
| 881 | LogParam(p.b, l); |
| 882 | } |
| 883 | }; |
| 884 | |
| 885 | template <class A, class B, class C> |
| 886 | struct ParamTraits< Tuple3<A, B, C> > { |
| 887 | typedef Tuple3<A, B, C> param_type; |
| 888 | static void Write(Message* m, const param_type& p) { |
| 889 | WriteParam(m, p.a); |
| 890 | WriteParam(m, p.b); |
| 891 | WriteParam(m, p.c); |
| 892 | } |
| 893 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 894 | return (ReadParam(m, iter, &r->a) && |
| 895 | ReadParam(m, iter, &r->b) && |
| 896 | ReadParam(m, iter, &r->c)); |
| 897 | } |
| 898 | static void Log(const param_type& p, std::wstring* l) { |
| 899 | LogParam(p.a, l); |
| 900 | l->append(L", "); |
| 901 | LogParam(p.b, l); |
| 902 | l->append(L", "); |
| 903 | LogParam(p.c, l); |
| 904 | } |
| 905 | }; |
| 906 | |
| 907 | template <class A, class B, class C, class D> |
| 908 | struct ParamTraits< Tuple4<A, B, C, D> > { |
| 909 | typedef Tuple4<A, B, C, D> param_type; |
| 910 | static void Write(Message* m, const param_type& p) { |
| 911 | WriteParam(m, p.a); |
| 912 | WriteParam(m, p.b); |
| 913 | WriteParam(m, p.c); |
| 914 | WriteParam(m, p.d); |
| 915 | } |
| 916 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 917 | return (ReadParam(m, iter, &r->a) && |
| 918 | ReadParam(m, iter, &r->b) && |
| 919 | ReadParam(m, iter, &r->c) && |
| 920 | ReadParam(m, iter, &r->d)); |
| 921 | } |
| 922 | static void Log(const param_type& p, std::wstring* l) { |
| 923 | LogParam(p.a, l); |
| 924 | l->append(L", "); |
| 925 | LogParam(p.b, l); |
| 926 | l->append(L", "); |
| 927 | LogParam(p.c, l); |
| 928 | l->append(L", "); |
| 929 | LogParam(p.d, l); |
| 930 | } |
| 931 | }; |
| 932 | |
| 933 | template <class A, class B, class C, class D, class E> |
| 934 | struct ParamTraits< Tuple5<A, B, C, D, E> > { |
| 935 | typedef Tuple5<A, B, C, D, E> param_type; |
| 936 | static void Write(Message* m, const param_type& p) { |
| 937 | WriteParam(m, p.a); |
| 938 | WriteParam(m, p.b); |
| 939 | WriteParam(m, p.c); |
| 940 | WriteParam(m, p.d); |
| 941 | WriteParam(m, p.e); |
| 942 | } |
| 943 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 944 | return (ReadParam(m, iter, &r->a) && |
| 945 | ReadParam(m, iter, &r->b) && |
| 946 | ReadParam(m, iter, &r->c) && |
| 947 | ReadParam(m, iter, &r->d) && |
| 948 | ReadParam(m, iter, &r->e)); |
| 949 | } |
| 950 | static void Log(const param_type& p, std::wstring* l) { |
| 951 | LogParam(p.a, l); |
| 952 | l->append(L", "); |
| 953 | LogParam(p.b, l); |
| 954 | l->append(L", "); |
| 955 | LogParam(p.c, l); |
| 956 | l->append(L", "); |
| 957 | LogParam(p.d, l); |
| 958 | l->append(L", "); |
| 959 | LogParam(p.e, l); |
| 960 | } |
| 961 | }; |
| 962 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 963 | //----------------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 964 | // Generic message subclasses |
| 965 | |
| 966 | // Used for asynchronous messages. |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 967 | template <class ParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 968 | class MessageWithTuple : public Message { |
| 969 | public: |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 970 | typedef ParamType Param; |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 971 | typedef typename ParamType::ParamTuple RefParam; |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 972 | |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 973 | MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 974 | : Message(routing_id, type, PRIORITY_NORMAL) { |
| 975 | WriteParam(this, p); |
| 976 | } |
| 977 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 978 | static bool Read(const Message* msg, Param* p) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 979 | void* iter = NULL; |
[email protected] | 3b846396 | 2009-07-16 06:27:38 | [diff] [blame] | 980 | if (ReadParam(msg, &iter, p)) |
| 981 | return true; |
| 982 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 983 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | // Generic dispatcher. Should cover most cases. |
| 987 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 988 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 989 | Param p; |
| 990 | if (Read(msg, &p)) { |
| 991 | DispatchToMethod(obj, func, p); |
| 992 | return true; |
| 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | // The following dispatchers exist for the case where the callback function |
| 998 | // needs the message as well. They assume that "Param" is a type of Tuple |
| 999 | // (except the one arg case, as there is no Tuple1). |
| 1000 | template<class T, typename TA> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1001 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1002 | void (T::*func)(const Message&, TA)) { |
| 1003 | Param p; |
| 1004 | if (Read(msg, &p)) { |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 1005 | (obj->*func)(*msg, p.a); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1006 | return true; |
| 1007 | } |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | template<class T, typename TA, typename TB> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1012 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1013 | void (T::*func)(const Message&, TA, TB)) { |
| 1014 | Param p; |
| 1015 | if (Read(msg, &p)) { |
| 1016 | (obj->*func)(*msg, p.a, p.b); |
| 1017 | return true; |
| 1018 | } |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
| 1022 | template<class T, typename TA, typename TB, typename TC> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1023 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1024 | void (T::*func)(const Message&, TA, TB, TC)) { |
| 1025 | Param p; |
| 1026 | if (Read(msg, &p)) { |
| 1027 | (obj->*func)(*msg, p.a, p.b, p.c); |
| 1028 | return true; |
| 1029 | } |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | template<class T, typename TA, typename TB, typename TC, typename TD> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1034 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1035 | void (T::*func)(const Message&, TA, TB, TC, TD)) { |
| 1036 | Param p; |
| 1037 | if (Read(msg, &p)) { |
| 1038 | (obj->*func)(*msg, p.a, p.b, p.c, p.d); |
| 1039 | return true; |
| 1040 | } |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | template<class T, typename TA, typename TB, typename TC, typename TD, |
| 1045 | typename TE> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1046 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1047 | void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { |
| 1048 | Param p; |
| 1049 | if (Read(msg, &p)) { |
| 1050 | (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); |
| 1051 | return true; |
| 1052 | } |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1056 | static void Log(const Message* msg, std::wstring* l) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1057 | Param p; |
| 1058 | if (Read(msg, &p)) |
| 1059 | LogParam(p, l); |
| 1060 | } |
[email protected] | deb5740 | 2009-02-06 01:35:30 | [diff] [blame] | 1061 | |
| 1062 | // Functions used to do manual unpacking. Only used by the automation code, |
| 1063 | // these should go away once that code uses SyncChannel. |
| 1064 | template<typename TA, typename TB> |
| 1065 | static bool Read(const IPC::Message* msg, TA* a, TB* b) { |
| 1066 | ParamType params; |
| 1067 | if (!Read(msg, ¶ms)) |
| 1068 | return false; |
| 1069 | *a = params.a; |
| 1070 | *b = params.b; |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | template<typename TA, typename TB, typename TC> |
| 1075 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c) { |
| 1076 | ParamType params; |
| 1077 | if (!Read(msg, ¶ms)) |
| 1078 | return false; |
| 1079 | *a = params.a; |
| 1080 | *b = params.b; |
| 1081 | *c = params.c; |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | template<typename TA, typename TB, typename TC, typename TD> |
| 1086 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d) { |
| 1087 | ParamType params; |
| 1088 | if (!Read(msg, ¶ms)) |
| 1089 | return false; |
| 1090 | *a = params.a; |
| 1091 | *b = params.b; |
| 1092 | *c = params.c; |
| 1093 | *d = params.d; |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1098 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d, TE* e) { |
| 1099 | ParamType params; |
| 1100 | if (!Read(msg, ¶ms)) |
| 1101 | return false; |
| 1102 | *a = params.a; |
| 1103 | *b = params.b; |
| 1104 | *c = params.c; |
| 1105 | *d = params.d; |
| 1106 | *e = params.e; |
| 1107 | return true; |
| 1108 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1109 | }; |
| 1110 | |
| 1111 | // This class assumes that its template argument is a RefTuple (a Tuple with |
| 1112 | // reference elements). |
| 1113 | template <class RefTuple> |
| 1114 | class ParamDeserializer : public MessageReplyDeserializer { |
| 1115 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 1116 | explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1117 | |
| 1118 | bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { |
| 1119 | return ReadParam(&msg, &iter, &out_); |
| 1120 | } |
| 1121 | |
| 1122 | RefTuple out_; |
| 1123 | }; |
| 1124 | |
| 1125 | // defined in ipc_logging.cc |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 1126 | void GenerateLogData(const std::string& channel, const Message& message, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1127 | LogData* data); |
| 1128 | |
| 1129 | // Used for synchronous messages. |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1130 | template <class SendParamType, class ReplyParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1131 | class MessageWithReply : public SyncMessage { |
| 1132 | public: |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1133 | typedef SendParamType SendParam; |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 1134 | typedef typename SendParam::ParamTuple RefSendParam; |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1135 | typedef ReplyParamType ReplyParam; |
| 1136 | |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 1137 | MessageWithReply(int32 routing_id, uint32 type, |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 1138 | const RefSendParam& send, const ReplyParam& reply) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1139 | : SyncMessage(routing_id, type, PRIORITY_NORMAL, |
| 1140 | new ParamDeserializer<ReplyParam>(reply)) { |
| 1141 | WriteParam(this, send); |
| 1142 | } |
| 1143 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1144 | static void Log(const Message* msg, std::wstring* l) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1145 | if (msg->is_sync()) { |
| 1146 | SendParam p; |
| 1147 | void* iter = SyncMessage::GetDataIterator(msg); |
[email protected] | 1e86aa6 | 2009-04-24 21:22:33 | [diff] [blame] | 1148 | if (ReadParam(msg, &iter, &p)) |
| 1149 | LogParam(p, l); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1150 | |
[email protected] | 1156f7b | 2008-12-04 19:13:59 | [diff] [blame] | 1151 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1152 | const std::wstring& output_params = msg->output_params(); |
| 1153 | if (!l->empty() && !output_params.empty()) |
| 1154 | l->append(L", "); |
| 1155 | |
| 1156 | l->append(output_params); |
[email protected] | 2a34f9c | 2008-12-04 19:12:04 | [diff] [blame] | 1157 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1158 | } else { |
| 1159 | // This is an outgoing reply. Now that we have the output parameters, we |
| 1160 | // can finally log the message. |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 1161 | typename ReplyParam::ValueTuple p; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1162 | void* iter = SyncMessage::GetDataIterator(msg); |
[email protected] | 1e86aa6 | 2009-04-24 21:22:33 | [diff] [blame] | 1163 | if (ReadParam(msg, &iter, &p)) |
| 1164 | LogParam(p, l); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1169 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1170 | SendParam send_params; |
| 1171 | void* iter = GetDataIterator(msg); |
| 1172 | Message* reply = GenerateReply(msg); |
| 1173 | bool error; |
| 1174 | if (ReadParam(msg, &iter, &send_params)) { |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 1175 | typename ReplyParam::ValueTuple reply_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1176 | DispatchToMethod(obj, func, send_params, &reply_params); |
| 1177 | WriteParam(reply, reply_params); |
| 1178 | error = false; |
| 1179 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 1180 | if (msg->received_time() != 0) { |
| 1181 | std::wstring output_params; |
| 1182 | LogParam(reply_params, &output_params); |
| 1183 | msg->set_output_params(output_params); |
| 1184 | } |
| 1185 | #endif |
| 1186 | } else { |
| 1187 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1188 | reply->set_reply_error(); |
| 1189 | error = true; |
| 1190 | } |
| 1191 | |
| 1192 | obj->Send(reply); |
| 1193 | return !error; |
| 1194 | } |
| 1195 | |
| 1196 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1197 | static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1198 | SendParam send_params; |
| 1199 | void* iter = GetDataIterator(msg); |
| 1200 | Message* reply = GenerateReply(msg); |
| 1201 | bool error; |
| 1202 | if (ReadParam(msg, &iter, &send_params)) { |
| 1203 | Tuple1<Message&> t = MakeRefTuple(*reply); |
| 1204 | |
| 1205 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 1206 | if (msg->sent_time()) { |
| 1207 | // Don't log the sync message after dispatch, as we don't have the |
| 1208 | // output parameters at that point. Instead, save its data and log it |
| 1209 | // with the outgoing reply message when it's sent. |
| 1210 | LogData* data = new LogData; |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 1211 | GenerateLogData("", *msg, data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1212 | msg->set_dont_log(); |
| 1213 | reply->set_sync_log_data(data); |
| 1214 | } |
| 1215 | #endif |
| 1216 | DispatchToMethod(obj, func, send_params, &t); |
| 1217 | error = false; |
| 1218 | } else { |
| 1219 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1220 | reply->set_reply_error(); |
| 1221 | obj->Send(reply); |
| 1222 | error = true; |
| 1223 | } |
| 1224 | return !error; |
| 1225 | } |
| 1226 | |
| 1227 | template<typename TA> |
| 1228 | static void WriteReplyParams(Message* reply, TA a) { |
| 1229 | ReplyParam p(a); |
| 1230 | WriteParam(reply, p); |
| 1231 | } |
| 1232 | |
| 1233 | template<typename TA, typename TB> |
| 1234 | static void WriteReplyParams(Message* reply, TA a, TB b) { |
| 1235 | ReplyParam p(a, b); |
| 1236 | WriteParam(reply, p); |
| 1237 | } |
| 1238 | |
| 1239 | template<typename TA, typename TB, typename TC> |
| 1240 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c) { |
| 1241 | ReplyParam p(a, b, c); |
| 1242 | WriteParam(reply, p); |
| 1243 | } |
| 1244 | |
| 1245 | template<typename TA, typename TB, typename TC, typename TD> |
| 1246 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d) { |
| 1247 | ReplyParam p(a, b, c, d); |
| 1248 | WriteParam(reply, p); |
| 1249 | } |
| 1250 | |
| 1251 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1252 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) { |
| 1253 | ReplyParam p(a, b, c, d, e); |
| 1254 | WriteParam(reply, p); |
| 1255 | } |
| 1256 | }; |
| 1257 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1258 | //----------------------------------------------------------------------------- |
| 1259 | |
[email protected] | 3178f4e2 | 2008-08-05 21:20:41 | [diff] [blame] | 1260 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1261 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1262 | #endif // IPC_IPC_MESSAGE_UTILS_H_ |