blob: 7d8cf4f3ab13662801d906b7882ba19415e0305b [file] [log] [blame]
[email protected]225c8f52010-02-05 22:23:201// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#ifndef IPC_IPC_MESSAGE_UTILS_H_
6#define IPC_IPC_MESSAGE_UTILS_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
[email protected]379e7a52010-03-09 00:38:419#include <algorithm>
initial.commit09911bf2008-07-26 23:55:2910#include <string>
11#include <vector>
12#include <map>
[email protected]96da6962010-05-13 19:10:3413#include <set>
initial.commit09911bf2008-07-26 23:55:2914
[email protected]dce5df52009-06-29 17:58:2515#include "base/format_macros.h"
[email protected]eb47a132009-03-04 00:39:5616#include "base/string16.h"
[email protected]dce5df52009-06-29 17:58:2517#include "base/string_util.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/tuple.h"
[email protected]939856a2010-08-24 20:29:0219#include "ipc/ipc_param_traits.h"
[email protected]0cfe5dae2010-08-17 00:24:5420#include "ipc/ipc_sync_message.h"
[email protected]55b4e212010-08-13 20:43:5821
[email protected]7a4de7a62010-08-17 18:38:2422#if defined(COMPILER_GCC)
23// GCC "helpfully" tries to inline template methods in release mode. Except we
24// want the majority of the template junk being expanded once in the
25// implementation file (and only provide the definitions in
26// ipc_message_utils_impl.h in those files) and exported, instead of expanded
27// at every call site. Special note: GCC happily accepts the attribute before
28// the method declaration, but only acts on it if it is after.
[email protected]efafbbd2010-08-19 20:23:2529#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40500
30// Starting in gcc 4.5, the noinline no longer implies the concept covered by
31// the introduced noclone attribute, which will create specialized versions of
32// functions/methods when certain types are constant.
33// www.gnu.org/software/gcc/gcc-4.5/changes.html
34#define IPC_MSG_NOINLINE __attribute__((noinline, noclone));
35#else
[email protected]7a4de7a62010-08-17 18:38:2436#define IPC_MSG_NOINLINE __attribute__((noinline));
[email protected]efafbbd2010-08-19 20:23:2537#endif
[email protected]7a4de7a62010-08-17 18:38:2438#elif defined(COMPILER_MSVC)
39// MSVC++ doesn't do this.
40#define IPC_MSG_NOINLINE
41#else
42#error "Please add the noinline property for your new compiler here."
43#endif
44
[email protected]f91cb992009-02-04 20:10:1245// Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique
46// base. Messages have unique IDs across channels in order for the IPC logging
47// code to figure out the message class from its ID.
48enum IPCMessageStart {
49 // By using a start value of 0 for automation messages, we keep backward
50 // compatibility with old builds.
51 AutomationMsgStart = 0,
52 ViewMsgStart,
53 ViewHostMsgStart,
54 PluginProcessMsgStart,
55 PluginProcessHostMsgStart,
56 PluginMsgStart,
57 PluginHostMsgStart,
[email protected]96da6962010-05-13 19:10:3458 ProfileImportProcessMsgStart,
59 ProfileImportProcessHostMsgStart,
[email protected]f91cb992009-02-04 20:10:1260 NPObjectMsgStart,
61 TestMsgStart,
[email protected]503683f2009-02-26 09:13:0162 DevToolsAgentMsgStart,
63 DevToolsClientMsgStart,
[email protected]eb47a132009-03-04 00:39:5664 WorkerProcessMsgStart,
65 WorkerProcessHostMsgStart,
66 WorkerMsgStart,
67 WorkerHostMsgStart,
[email protected]d032f492009-09-29 00:33:4668 NaClProcessMsgStart,
[email protected]246a70452010-03-05 21:53:5069 GpuCommandBufferMsgStart,
[email protected]60f14392009-12-15 20:46:3270 UtilityMsgStart,
71 UtilityHostMsgStart,
[email protected]c0fc0942010-01-13 00:55:3772 GpuMsgStart,
73 GpuHostMsgStart,
[email protected]246a70452010-03-05 21:53:5074 GpuChannelMsgStart,
[email protected]ee68378a2010-08-10 01:05:4175 GpuVideoDecoderHostMsgStart,
76 GpuVideoDecoderMsgStart,
[email protected]38fe1962010-07-31 07:57:0077 ServiceMsgStart,
78 ServiceHostMsgStart,
[email protected]709a847e2010-11-10 01:16:1179 PpapiMsgStart,
80 PpapiHostMsgStart,
[email protected]f91cb992009-02-04 20:10:1281 // NOTE: When you add a new message class, also update
82 // IPCStatusView::IPCStatusView to ensure logging works.
[email protected]f91cb992009-02-04 20:10:1283 LastMsgIndex
84};
85
[email protected]7a4de7a62010-08-17 18:38:2486class DictionaryValue;
87class FilePath;
88class ListValue;
89class NullableString16;
90
91namespace base {
92class Time;
[email protected]d84e48b2010-10-21 22:04:5293class TimeDelta;
[email protected]7a4de7a62010-08-17 18:38:2494struct FileDescriptor;
95}
96
initial.commit09911bf2008-07-26 23:55:2997namespace IPC {
98
[email protected]7a4de7a62010-08-17 18:38:2499struct ChannelHandle;
100
initial.commit09911bf2008-07-26 23:55:29101//-----------------------------------------------------------------------------
102// An iterator class for reading the fields contained within a Message.
103
104class MessageIterator {
105 public:
[email protected]e1981f432008-08-12 15:22:13106 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29107 }
108 int NextInt() const {
[email protected]9422b7dc2009-12-30 20:09:02109 int val = -1;
initial.commit09911bf2008-07-26 23:55:29110 if (!msg_.ReadInt(&iter_, &val))
111 NOTREACHED();
112 return val;
113 }
initial.commit09911bf2008-07-26 23:55:29114 const std::string NextString() const {
115 std::string val;
116 if (!msg_.ReadString(&iter_, &val))
117 NOTREACHED();
118 return val;
119 }
120 const std::wstring NextWString() const {
121 std::wstring val;
122 if (!msg_.ReadWString(&iter_, &val))
123 NOTREACHED();
124 return val;
125 }
[email protected]225c8f52010-02-05 22:23:20126 void NextData(const char** data, int* length) const {
initial.commit09911bf2008-07-26 23:55:29127 if (!msg_.ReadData(&iter_, data, length)) {
128 NOTREACHED();
129 }
130 }
131 private:
132 const Message& msg_;
133 mutable void* iter_;
134};
135
136//-----------------------------------------------------------------------------
[email protected]7d5c3ac2009-02-04 08:58:19137// ParamTraits specializations, etc.
138
[email protected]7d5c3ac2009-02-04 08:58:19139template <class P>
140static inline void WriteParam(Message* m, const P& p) {
[email protected]7b291f92009-08-14 05:43:53141 typedef typename SimilarTypeTraits<P>::Type Type;
[email protected]46ce5b562010-06-16 18:39:53142 ParamTraits<Type>::Write(m, static_cast<const Type& >(p));
[email protected]7d5c3ac2009-02-04 08:58:19143}
144
145template <class P>
[email protected]1e86aa62009-04-24 21:22:33146static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, void** iter,
147 P* p) {
[email protected]7b291f92009-08-14 05:43:53148 typedef typename SimilarTypeTraits<P>::Type Type;
149 return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p));
[email protected]7d5c3ac2009-02-04 08:58:19150}
151
152template <class P>
[email protected]252cad62010-08-18 18:33:57153static inline void LogParam(const P& p, std::string* l) {
[email protected]7b291f92009-08-14 05:43:53154 typedef typename SimilarTypeTraits<P>::Type Type;
[email protected]46ce5b562010-06-16 18:39:53155 ParamTraits<Type>::Log(static_cast<const Type& >(p), l);
[email protected]7d5c3ac2009-02-04 08:58:19156}
157
158template <>
159struct ParamTraits<bool> {
160 typedef bool param_type;
161 static void Write(Message* m, const param_type& p) {
162 m->WriteBool(p);
163 }
164 static bool Read(const Message* m, void** iter, param_type* r) {
165 return m->ReadBool(iter, r);
166 }
[email protected]252cad62010-08-18 18:33:57167 static void Log(const param_type& p, std::string* l) {
168 l->append(p ? "true" : "false");
[email protected]7d5c3ac2009-02-04 08:58:19169 }
170};
171
172template <>
173struct ParamTraits<int> {
174 typedef int param_type;
175 static void Write(Message* m, const param_type& p) {
176 m->WriteInt(p);
177 }
178 static bool Read(const Message* m, void** iter, param_type* r) {
179 return m->ReadInt(iter, r);
180 }
[email protected]252cad62010-08-18 18:33:57181 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19182};
183
184template <>
[email protected]63263f92009-07-28 19:35:08185struct ParamTraits<unsigned int> {
186 typedef unsigned int param_type;
187 static void Write(Message* m, const param_type& p) {
188 m->WriteInt(p);
189 }
190 static bool Read(const Message* m, void** iter, param_type* r) {
191 return m->ReadInt(iter, reinterpret_cast<int*>(r));
192 }
[email protected]252cad62010-08-18 18:33:57193 static void Log(const param_type& p, std::string* l);
[email protected]63263f92009-07-28 19:35:08194};
195
196template <>
[email protected]7d5c3ac2009-02-04 08:58:19197struct ParamTraits<long> {
198 typedef long param_type;
199 static void Write(Message* m, const param_type& p) {
200 m->WriteLong(p);
201 }
202 static bool Read(const Message* m, void** iter, param_type* r) {
203 return m->ReadLong(iter, r);
204 }
[email protected]252cad62010-08-18 18:33:57205 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19206};
207
[email protected]140c3032009-06-26 18:22:54208template <>
209struct ParamTraits<unsigned long> {
210 typedef unsigned long param_type;
211 static void Write(Message* m, const param_type& p) {
212 m->WriteLong(p);
213 }
214 static bool Read(const Message* m, void** iter, param_type* r) {
[email protected]63263f92009-07-28 19:35:08215 return m->ReadLong(iter, reinterpret_cast<long*>(r));
[email protected]140c3032009-06-26 18:22:54216 }
[email protected]252cad62010-08-18 18:33:57217 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19218};
219
220template <>
[email protected]63263f92009-07-28 19:35:08221struct ParamTraits<long long> {
222 typedef long long param_type;
[email protected]7d5c3ac2009-02-04 08:58:19223 static void Write(Message* m, const param_type& p) {
224 m->WriteInt64(static_cast<int64>(p));
225 }
226 static bool Read(const Message* m, void** iter, param_type* r) {
227 return m->ReadInt64(iter, reinterpret_cast<int64*>(r));
228 }
[email protected]252cad62010-08-18 18:33:57229 static void Log(const param_type& p, std::string* l);
[email protected]63263f92009-07-28 19:35:08230};
231
232template <>
233struct ParamTraits<unsigned long long> {
234 typedef unsigned long long param_type;
235 static void Write(Message* m, const param_type& p) {
236 m->WriteInt64(p);
237 }
238 static bool Read(const Message* m, void** iter, param_type* r) {
239 return m->ReadInt64(iter, reinterpret_cast<int64*>(r));
240 }
[email protected]252cad62010-08-18 18:33:57241 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19242};
243
[email protected]43a40202010-11-12 16:25:01244template <>
245struct ParamTraits<unsigned short> {
246 typedef unsigned short param_type;
247 static void Write(Message* m, const param_type& p);
248 static bool Read(const Message* m, void** iter, param_type* r);
249 static void Log(const param_type& p, std::string* l);
250};
251
[email protected]20199662010-06-17 03:29:26252// Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients
253// should be sure to check the sanity of these values after receiving them over
254// IPC.
255template <>
256struct ParamTraits<float> {
257 typedef float param_type;
258 static void Write(Message* m, const param_type& p) {
259 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
260 }
261 static bool Read(const Message* m, void** iter, param_type* r) {
262 const char *data;
263 int data_size;
264 if (!m->ReadData(iter, &data, &data_size) ||
265 data_size != sizeof(param_type)) {
266 NOTREACHED();
267 return false;
268 }
269 memcpy(r, data, sizeof(param_type));
270 return true;
271 }
[email protected]252cad62010-08-18 18:33:57272 static void Log(const param_type& p, std::string* l) {
273 l->append(StringPrintf("%e", p));
[email protected]20199662010-06-17 03:29:26274 }
275};
276
[email protected]7d5c3ac2009-02-04 08:58:19277template <>
278struct ParamTraits<double> {
279 typedef double param_type;
280 static void Write(Message* m, const param_type& p) {
281 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
282 }
283 static bool Read(const Message* m, void** iter, param_type* r) {
284 const char *data;
[email protected]20199662010-06-17 03:29:26285 int data_size;
286 if (!m->ReadData(iter, &data, &data_size) ||
287 data_size != sizeof(param_type)) {
[email protected]7d5c3ac2009-02-04 08:58:19288 NOTREACHED();
[email protected]20199662010-06-17 03:29:26289 return false;
[email protected]7d5c3ac2009-02-04 08:58:19290 }
[email protected]20199662010-06-17 03:29:26291 memcpy(r, data, sizeof(param_type));
292 return true;
[email protected]7d5c3ac2009-02-04 08:58:19293 }
[email protected]252cad62010-08-18 18:33:57294 static void Log(const param_type& p, std::string* l) {
295 l->append(StringPrintf("%e", p));
[email protected]7d5c3ac2009-02-04 08:58:19296 }
297};
298
299template <>
[email protected]7d5c3ac2009-02-04 08:58:19300struct ParamTraits<base::Time> {
301 typedef base::Time param_type;
[email protected]7a4de7a62010-08-17 18:38:24302 static void Write(Message* m, const param_type& p);
303 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57304 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19305};
306
[email protected]d84e48b2010-10-21 22:04:52307template <>
308struct ParamTraits<base::TimeDelta> {
309 typedef base::TimeDelta param_type;
310 static void Write(Message* m, const param_type& p);
311 static bool Read(const Message* m, void** iter, param_type* r);
312 static void Log(const param_type& p, std::string* l);
313};
314
[email protected]7d5c3ac2009-02-04 08:58:19315#if defined(OS_WIN)
316template <>
317struct ParamTraits<LOGFONT> {
318 typedef LOGFONT param_type;
319 static void Write(Message* m, const param_type& p) {
320 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
321 }
322 static bool Read(const Message* m, void** iter, param_type* r) {
323 const char *data;
324 int data_size = 0;
325 bool result = m->ReadData(iter, &data, &data_size);
326 if (result && data_size == sizeof(LOGFONT)) {
327 memcpy(r, data, sizeof(LOGFONT));
328 } else {
329 result = false;
330 NOTREACHED();
331 }
332
333 return result;
334 }
[email protected]252cad62010-08-18 18:33:57335 static void Log(const param_type& p, std::string* l) {
336 l->append(StringPrintf("<LOGFONT>"));
[email protected]7d5c3ac2009-02-04 08:58:19337 }
338};
339
340template <>
341struct ParamTraits<MSG> {
342 typedef MSG param_type;
343 static void Write(Message* m, const param_type& p) {
344 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
345 }
346 static bool Read(const Message* m, void** iter, param_type* r) {
347 const char *data;
348 int data_size = 0;
349 bool result = m->ReadData(iter, &data, &data_size);
350 if (result && data_size == sizeof(MSG)) {
351 memcpy(r, data, sizeof(MSG));
352 } else {
353 result = false;
354 NOTREACHED();
355 }
356
357 return result;
358 }
[email protected]252cad62010-08-18 18:33:57359 static void Log(const param_type& p, std::string* l) {
360 l->append("<MSG>");
[email protected]7a4de7a62010-08-17 18:38:24361 }
[email protected]7d5c3ac2009-02-04 08:58:19362};
363#endif // defined(OS_WIN)
364
365template <>
[email protected]584f2b22009-05-21 01:01:59366struct ParamTraits<DictionaryValue> {
367 typedef DictionaryValue param_type;
368 static void Write(Message* m, const param_type& p);
369 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57370 static void Log(const param_type& p, std::string* l);
[email protected]584f2b22009-05-21 01:01:59371};
372
373template <>
374struct ParamTraits<ListValue> {
375 typedef ListValue param_type;
376 static void Write(Message* m, const param_type& p);
377 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57378 static void Log(const param_type& p, std::string* l);
[email protected]584f2b22009-05-21 01:01:59379};
380
381template <>
[email protected]7d5c3ac2009-02-04 08:58:19382struct ParamTraits<std::string> {
383 typedef std::string param_type;
384 static void Write(Message* m, const param_type& p) {
385 m->WriteString(p);
386 }
387 static bool Read(const Message* m, void** iter, param_type* r) {
388 return m->ReadString(iter, r);
389 }
[email protected]252cad62010-08-18 18:33:57390 static void Log(const param_type& p, std::string* l) {
391 l->append(p);
[email protected]7d5c3ac2009-02-04 08:58:19392 }
393};
394
[email protected]3dd7a7a2009-07-27 21:09:07395template<typename CharType>
[email protected]252cad62010-08-18 18:33:57396static void LogBytes(const std::vector<CharType>& data, std::string* out) {
[email protected]3dd7a7a2009-07-27 21:09:07397#if defined(OS_WIN)
398 // Windows has a GUI for logging, which can handle arbitrary binary data.
399 for (size_t i = 0; i < data.size(); ++i)
400 out->push_back(data[i]);
401#else
402 // On POSIX, we log to stdout, which we assume can display ASCII.
403 static const size_t kMaxBytesToLog = 100;
404 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
405 if (isprint(data[i]))
406 out->push_back(data[i]);
407 else
[email protected]252cad62010-08-18 18:33:57408 out->append(StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
[email protected]3dd7a7a2009-07-27 21:09:07409 }
410 if (data.size() > kMaxBytesToLog) {
411 out->append(
[email protected]252cad62010-08-18 18:33:57412 StringPrintf(" and %u more bytes",
[email protected]3dd7a7a2009-07-27 21:09:07413 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
414 }
415#endif
416}
417
[email protected]7d5c3ac2009-02-04 08:58:19418template <>
419struct ParamTraits<std::vector<unsigned char> > {
420 typedef std::vector<unsigned 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(reinterpret_cast<const char*>(&p.front()),
426 static_cast<int>(p.size()));
427 }
428 }
429 static bool Read(const Message* m, void** iter, param_type* r) {
430 const char *data;
431 int data_size = 0;
432 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
433 return false;
434 r->resize(data_size);
435 if (data_size)
436 memcpy(&r->front(), data, data_size);
437 return true;
438 }
[email protected]252cad62010-08-18 18:33:57439 static void Log(const param_type& p, std::string* l) {
[email protected]3dd7a7a2009-07-27 21:09:07440 LogBytes(p, l);
[email protected]7d5c3ac2009-02-04 08:58:19441 }
442};
443
444template <>
445struct ParamTraits<std::vector<char> > {
446 typedef std::vector<char> param_type;
447 static void Write(Message* m, const param_type& p) {
448 if (p.size() == 0) {
449 m->WriteData(NULL, 0);
450 } else {
451 m->WriteData(&p.front(), static_cast<int>(p.size()));
452 }
453 }
454 static bool Read(const Message* m, void** iter, param_type* r) {
455 const char *data;
456 int data_size = 0;
457 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
458 return false;
459 r->resize(data_size);
460 if (data_size)
461 memcpy(&r->front(), data, data_size);
462 return true;
463 }
[email protected]252cad62010-08-18 18:33:57464 static void Log(const param_type& p, std::string* l) {
[email protected]3dd7a7a2009-07-27 21:09:07465 LogBytes(p, l);
[email protected]7d5c3ac2009-02-04 08:58:19466 }
467};
468
469template <class P>
470struct ParamTraits<std::vector<P> > {
471 typedef std::vector<P> param_type;
472 static void Write(Message* m, const param_type& p) {
473 WriteParam(m, static_cast<int>(p.size()));
474 for (size_t i = 0; i < p.size(); i++)
475 WriteParam(m, p[i]);
476 }
477 static bool Read(const Message* m, void** iter, param_type* r) {
478 int size;
[email protected]86440f52009-12-31 05:17:23479 // ReadLength() checks for < 0 itself.
[email protected]7d5c3ac2009-02-04 08:58:19480 if (!m->ReadLength(iter, &size))
481 return false;
482 // Resizing beforehand is not safe, see BUG 1006367 for details.
[email protected]86440f52009-12-31 05:17:23483 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size))
484 return false;
485 r->resize(size);
486 for (int i = 0; i < size; i++) {
487 if (!ReadParam(m, iter, &(*r)[i]))
488 return false;
[email protected]7d5c3ac2009-02-04 08:58:19489 }
490 return true;
491 }
[email protected]252cad62010-08-18 18:33:57492 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19493 for (size_t i = 0; i < p.size(); ++i) {
494 if (i != 0)
[email protected]252cad62010-08-18 18:33:57495 l->append(" ");
[email protected]7d5c3ac2009-02-04 08:58:19496 LogParam((p[i]), l);
497 }
498 }
499};
500
[email protected]96da6962010-05-13 19:10:34501template <class P>
502struct ParamTraits<std::set<P> > {
503 typedef std::set<P> param_type;
504 static void Write(Message* m, const param_type& p) {
505 WriteParam(m, static_cast<int>(p.size()));
506 typename param_type::const_iterator iter;
507 for (iter = p.begin(); iter != p.end(); ++iter)
508 WriteParam(m, *iter);
509 }
510 static bool Read(const Message* m, void** iter, param_type* r) {
511 int size;
512 if (!m->ReadLength(iter, &size))
513 return false;
514 for (int i = 0; i < size; ++i) {
515 P item;
516 if (!ReadParam(m, iter, &item))
517 return false;
518 r->insert(item);
519 }
520 return true;
521 }
[email protected]252cad62010-08-18 18:33:57522 static void Log(const param_type& p, std::string* l) {
523 l->append("<std::set>");
[email protected]96da6962010-05-13 19:10:34524 }
525};
526
527
[email protected]7d5c3ac2009-02-04 08:58:19528template <class K, class V>
529struct ParamTraits<std::map<K, V> > {
530 typedef std::map<K, V> param_type;
531 static void Write(Message* m, const param_type& p) {
532 WriteParam(m, static_cast<int>(p.size()));
533 typename param_type::const_iterator iter;
534 for (iter = p.begin(); iter != p.end(); ++iter) {
535 WriteParam(m, iter->first);
536 WriteParam(m, iter->second);
537 }
538 }
539 static bool Read(const Message* m, void** iter, param_type* r) {
540 int size;
541 if (!ReadParam(m, iter, &size) || size < 0)
542 return false;
543 for (int i = 0; i < size; ++i) {
544 K k;
545 if (!ReadParam(m, iter, &k))
546 return false;
547 V& value = (*r)[k];
548 if (!ReadParam(m, iter, &value))
549 return false;
550 }
551 return true;
552 }
[email protected]252cad62010-08-18 18:33:57553 static void Log(const param_type& p, std::string* l) {
554 l->append("<std::map>");
[email protected]7d5c3ac2009-02-04 08:58:19555 }
556};
557
[email protected]eb47a132009-03-04 00:39:56558
[email protected]7d5c3ac2009-02-04 08:58:19559template <>
560struct ParamTraits<std::wstring> {
561 typedef std::wstring param_type;
562 static void Write(Message* m, const param_type& p) {
563 m->WriteWString(p);
564 }
565 static bool Read(const Message* m, void** iter, param_type* r) {
566 return m->ReadWString(iter, r);
567 }
[email protected]252cad62010-08-18 18:33:57568 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19569};
570
[email protected]a5da6d612009-08-04 02:00:56571template <class A, class B>
572struct ParamTraits<std::pair<A, B> > {
573 typedef std::pair<A, B> param_type;
574 static void Write(Message* m, const param_type& p) {
575 WriteParam(m, p.first);
576 WriteParam(m, p.second);
577 }
578 static bool Read(const Message* m, void** iter, param_type* r) {
579 return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second);
580 }
[email protected]252cad62010-08-18 18:33:57581 static void Log(const param_type& p, std::string* l) {
582 l->append("(");
[email protected]a5da6d612009-08-04 02:00:56583 LogParam(p.first, l);
[email protected]252cad62010-08-18 18:33:57584 l->append(", ");
[email protected]a5da6d612009-08-04 02:00:56585 LogParam(p.second, l);
[email protected]252cad62010-08-18 18:33:57586 l->append(")");
[email protected]a5da6d612009-08-04 02:00:56587 }
588};
589
[email protected]15bf8712009-08-27 00:55:02590template <>
591struct ParamTraits<NullableString16> {
592 typedef NullableString16 param_type;
[email protected]7a4de7a62010-08-17 18:38:24593 static void Write(Message* m, const param_type& p);
594 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57595 static void Log(const param_type& p, std::string* l);
[email protected]15bf8712009-08-27 00:55:02596};
597
[email protected]eb47a132009-03-04 00:39:56598// If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't
599// need this trait.
600#if !defined(WCHAR_T_IS_UTF16)
[email protected]eb47a132009-03-04 00:39:56601template <>
602struct ParamTraits<string16> {
603 typedef string16 param_type;
604 static void Write(Message* m, const param_type& p) {
[email protected]3a2a5d22009-03-04 03:36:36605 m->WriteString16(p);
[email protected]eb47a132009-03-04 00:39:56606 }
607 static bool Read(const Message* m, void** iter, param_type* r) {
[email protected]3a2a5d22009-03-04 03:36:36608 return m->ReadString16(iter, r);
[email protected]eb47a132009-03-04 00:39:56609 }
[email protected]252cad62010-08-18 18:33:57610 static void Log(const param_type& p, std::string* l);
[email protected]eb47a132009-03-04 00:39:56611};
[email protected]eb47a132009-03-04 00:39:56612#endif
613
[email protected]7d5c3ac2009-02-04 08:58:19614// and, a few more useful types...
615#if defined(OS_WIN)
616template <>
617struct ParamTraits<HANDLE> {
618 typedef HANDLE param_type;
619 static void Write(Message* m, const param_type& p) {
[email protected]37477912010-02-09 08:06:35620 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
621 // bit systems.
622 m->WriteUInt32(reinterpret_cast<uint32>(p));
[email protected]7d5c3ac2009-02-04 08:58:19623 }
624 static bool Read(const Message* m, void** iter, param_type* r) {
[email protected]37477912010-02-09 08:06:35625 DCHECK_EQ(sizeof(param_type), sizeof(uint32));
626 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r));
[email protected]7d5c3ac2009-02-04 08:58:19627 }
[email protected]252cad62010-08-18 18:33:57628 static void Log(const param_type& p, std::string* l) {
629 l->append(StringPrintf("0x%X", p));
[email protected]7d5c3ac2009-02-04 08:58:19630 }
631};
632
633template <>
634struct ParamTraits<HCURSOR> {
635 typedef HCURSOR param_type;
636 static void Write(Message* m, const param_type& p) {
[email protected]37477912010-02-09 08:06:35637 m->WriteUInt32(reinterpret_cast<uint32>(p));
[email protected]7d5c3ac2009-02-04 08:58:19638 }
639 static bool Read(const Message* m, void** iter, param_type* r) {
[email protected]37477912010-02-09 08:06:35640 DCHECK_EQ(sizeof(param_type), sizeof(uint32));
641 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r));
[email protected]7d5c3ac2009-02-04 08:58:19642 }
[email protected]252cad62010-08-18 18:33:57643 static void Log(const param_type& p, std::string* l) {
644 l->append(StringPrintf("0x%X", p));
[email protected]7d5c3ac2009-02-04 08:58:19645 }
646};
647
648template <>
[email protected]7d5c3ac2009-02-04 08:58:19649struct ParamTraits<HACCEL> {
650 typedef HACCEL param_type;
651 static void Write(Message* m, const param_type& p) {
[email protected]37477912010-02-09 08:06:35652 m->WriteUInt32(reinterpret_cast<uint32>(p));
[email protected]7d5c3ac2009-02-04 08:58:19653 }
654 static bool Read(const Message* m, void** iter, param_type* r) {
[email protected]37477912010-02-09 08:06:35655 DCHECK_EQ(sizeof(param_type), sizeof(uint32));
656 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r));
[email protected]7d5c3ac2009-02-04 08:58:19657 }
658};
659
660template <>
661struct ParamTraits<POINT> {
662 typedef POINT param_type;
663 static void Write(Message* m, const param_type& p) {
664 m->WriteInt(p.x);
665 m->WriteInt(p.y);
666 }
667 static bool Read(const Message* m, void** iter, param_type* r) {
668 int x, y;
669 if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y))
670 return false;
671 r->x = x;
672 r->y = y;
673 return true;
674 }
[email protected]252cad62010-08-18 18:33:57675 static void Log(const param_type& p, std::string* l) {
676 l->append(StringPrintf("(%d, %d)", p.x, p.y));
[email protected]7d5c3ac2009-02-04 08:58:19677 }
678};
679#endif // defined(OS_WIN)
680
681template <>
682struct ParamTraits<FilePath> {
683 typedef FilePath param_type;
[email protected]7a4de7a62010-08-17 18:38:24684 static void Write(Message* m, const param_type& p);
685 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57686 static void Log(const param_type& p, std::string* l);
[email protected]7d5c3ac2009-02-04 08:58:19687};
688
[email protected]526776c2009-02-07 00:39:26689#if defined(OS_POSIX)
[email protected]2749885f2009-03-05 21:40:11690// FileDescriptors may be serialised over IPC channels on POSIX. On the
691// receiving side, the FileDescriptor is a valid duplicate of the file
692// descriptor which was transmitted: *it is not just a copy of the integer like
693// HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In
694// this case, the receiving end will see a value of -1. *Zero is a valid file
695// descriptor*.
696//
697// The received file descriptor will have the |auto_close| flag set to true. The
698// code which handles the message is responsible for taking ownership of it.
699// File descriptors are OS resources and must be closed when no longer needed.
700//
701// When sending a file descriptor, the file descriptor must be valid at the time
702// of transmission. Since transmission is not synchronous, one should consider
703// dup()ing any file descriptors to be transmitted and setting the |auto_close|
704// flag, which causes the file descriptor to be closed after writing.
[email protected]526776c2009-02-07 00:39:26705template<>
[email protected]5fe733de2009-02-11 18:59:20706struct ParamTraits<base::FileDescriptor> {
707 typedef base::FileDescriptor param_type;
[email protected]7a4de7a62010-08-17 18:38:24708 static void Write(Message* m, const param_type& p);
709 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57710 static void Log(const param_type& p, std::string* l);
[email protected]526776c2009-02-07 00:39:26711};
[email protected]379e7a52010-03-09 00:38:41712#endif // defined(OS_POSIX)
[email protected]526776c2009-02-07 00:39:26713
[email protected]d2e884d2009-06-22 20:37:52714// A ChannelHandle is basically a platform-inspecific wrapper around the
715// fact that IPC endpoints are handled specially on POSIX. See above comments
716// on FileDescriptor for more background.
717template<>
718struct ParamTraits<IPC::ChannelHandle> {
719 typedef ChannelHandle param_type;
[email protected]7a4de7a62010-08-17 18:38:24720 static void Write(Message* m, const param_type& p);
721 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57722 static void Log(const param_type& p, std::string* l);
[email protected]d2e884d2009-06-22 20:37:52723};
724
[email protected]7d5c3ac2009-02-04 08:58:19725#if defined(OS_WIN)
726template <>
727struct ParamTraits<XFORM> {
728 typedef XFORM param_type;
729 static void Write(Message* m, const param_type& p) {
730 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM));
731 }
732 static bool Read(const Message* m, void** iter, param_type* r) {
733 const char *data;
734 int data_size = 0;
735 bool result = m->ReadData(iter, &data, &data_size);
736 if (result && data_size == sizeof(XFORM)) {
737 memcpy(r, data, sizeof(XFORM));
738 } else {
739 result = false;
740 NOTREACHED();
741 }
742
743 return result;
744 }
[email protected]252cad62010-08-18 18:33:57745 static void Log(const param_type& p, std::string* l) {
746 l->append("<XFORM>");
[email protected]7d5c3ac2009-02-04 08:58:19747 }
748};
749#endif // defined(OS_WIN)
750
[email protected]7d5c3ac2009-02-04 08:58:19751struct LogData {
[email protected]20f0487a2010-09-30 20:06:30752 LogData();
753 ~LogData();
754
[email protected]9a3a293b2009-06-04 22:28:16755 std::string channel;
[email protected]8bef70e2009-04-14 19:11:24756 int32 routing_id;
[email protected]168ae922009-12-04 18:08:45757 uint32 type; // "User-defined" message type, from ipc_message.h.
[email protected]252cad62010-08-18 18:33:57758 std::string flags;
[email protected]7d5c3ac2009-02-04 08:58:19759 int64 sent; // Time that the message was sent (i.e. at Send()).
760 int64 receive; // Time before it was dispatched (i.e. before calling
761 // OnMessageReceived).
762 int64 dispatch; // Time after it was dispatched (i.e. after calling
763 // OnMessageReceived).
[email protected]252cad62010-08-18 18:33:57764 std::string message_name;
765 std::string params;
[email protected]7d5c3ac2009-02-04 08:58:19766};
767
768template <>
769struct ParamTraits<LogData> {
770 typedef LogData param_type;
[email protected]20f0487a2010-09-30 20:06:30771 static void Write(Message* m, const param_type& p);
772 static bool Read(const Message* m, void** iter, param_type* r);
[email protected]252cad62010-08-18 18:33:57773 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19774 // Doesn't make sense to implement this!
775 }
776};
777
[email protected]eb47a132009-03-04 00:39:56778template <>
[email protected]503683f2009-02-26 09:13:01779struct ParamTraits<Message> {
780 static void Write(Message* m, const Message& p) {
781 m->WriteInt(p.size());
782 m->WriteData(reinterpret_cast<const char*>(p.data()), p.size());
783 }
784 static bool Read(const Message* m, void** iter, Message* r) {
785 int size;
786 if (!m->ReadInt(iter, &size))
787 return false;
788 const char* data;
789 if (!m->ReadData(iter, &data, &size))
790 return false;
791 *r = Message(data, size);
792 return true;
793 }
[email protected]252cad62010-08-18 18:33:57794 static void Log(const Message& p, std::string* l) {
795 l->append("<IPC::Message>");
[email protected]503683f2009-02-26 09:13:01796 }
797};
798
799template <>
[email protected]7d5c3ac2009-02-04 08:58:19800struct ParamTraits<Tuple0> {
801 typedef Tuple0 param_type;
802 static void Write(Message* m, const param_type& p) {
803 }
804 static bool Read(const Message* m, void** iter, param_type* r) {
805 return true;
806 }
[email protected]252cad62010-08-18 18:33:57807 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19808 }
809};
810
811template <class A>
812struct ParamTraits< Tuple1<A> > {
813 typedef Tuple1<A> param_type;
814 static void Write(Message* m, const param_type& p) {
815 WriteParam(m, p.a);
816 }
817 static bool Read(const Message* m, void** iter, param_type* r) {
818 return ReadParam(m, iter, &r->a);
819 }
[email protected]252cad62010-08-18 18:33:57820 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19821 LogParam(p.a, l);
822 }
823};
824
825template <class A, class B>
826struct ParamTraits< Tuple2<A, B> > {
827 typedef Tuple2<A, B> param_type;
828 static void Write(Message* m, const param_type& p) {
829 WriteParam(m, p.a);
830 WriteParam(m, p.b);
831 }
832 static bool Read(const Message* m, void** iter, param_type* r) {
833 return (ReadParam(m, iter, &r->a) &&
834 ReadParam(m, iter, &r->b));
835 }
[email protected]252cad62010-08-18 18:33:57836 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19837 LogParam(p.a, l);
[email protected]252cad62010-08-18 18:33:57838 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19839 LogParam(p.b, l);
840 }
841};
842
843template <class A, class B, class C>
844struct ParamTraits< Tuple3<A, B, C> > {
845 typedef Tuple3<A, B, C> param_type;
846 static void Write(Message* m, const param_type& p) {
847 WriteParam(m, p.a);
848 WriteParam(m, p.b);
849 WriteParam(m, p.c);
850 }
851 static bool Read(const Message* m, void** iter, param_type* r) {
852 return (ReadParam(m, iter, &r->a) &&
853 ReadParam(m, iter, &r->b) &&
854 ReadParam(m, iter, &r->c));
855 }
[email protected]252cad62010-08-18 18:33:57856 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19857 LogParam(p.a, l);
[email protected]252cad62010-08-18 18:33:57858 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19859 LogParam(p.b, l);
[email protected]252cad62010-08-18 18:33:57860 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19861 LogParam(p.c, l);
862 }
863};
864
865template <class A, class B, class C, class D>
866struct ParamTraits< Tuple4<A, B, C, D> > {
867 typedef Tuple4<A, B, C, D> param_type;
868 static void Write(Message* m, const param_type& p) {
869 WriteParam(m, p.a);
870 WriteParam(m, p.b);
871 WriteParam(m, p.c);
872 WriteParam(m, p.d);
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 ReadParam(m, iter, &r->c) &&
878 ReadParam(m, iter, &r->d));
879 }
[email protected]252cad62010-08-18 18:33:57880 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19881 LogParam(p.a, l);
[email protected]252cad62010-08-18 18:33:57882 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19883 LogParam(p.b, l);
[email protected]252cad62010-08-18 18:33:57884 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19885 LogParam(p.c, l);
[email protected]252cad62010-08-18 18:33:57886 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19887 LogParam(p.d, l);
888 }
889};
890
891template <class A, class B, class C, class D, class E>
892struct ParamTraits< Tuple5<A, B, C, D, E> > {
893 typedef Tuple5<A, B, C, D, E> param_type;
894 static void Write(Message* m, const param_type& p) {
895 WriteParam(m, p.a);
896 WriteParam(m, p.b);
897 WriteParam(m, p.c);
898 WriteParam(m, p.d);
899 WriteParam(m, p.e);
900 }
901 static bool Read(const Message* m, void** iter, param_type* r) {
902 return (ReadParam(m, iter, &r->a) &&
903 ReadParam(m, iter, &r->b) &&
904 ReadParam(m, iter, &r->c) &&
905 ReadParam(m, iter, &r->d) &&
906 ReadParam(m, iter, &r->e));
907 }
[email protected]252cad62010-08-18 18:33:57908 static void Log(const param_type& p, std::string* l) {
[email protected]7d5c3ac2009-02-04 08:58:19909 LogParam(p.a, l);
[email protected]252cad62010-08-18 18:33:57910 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19911 LogParam(p.b, l);
[email protected]252cad62010-08-18 18:33:57912 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19913 LogParam(p.c, l);
[email protected]252cad62010-08-18 18:33:57914 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19915 LogParam(p.d, l);
[email protected]252cad62010-08-18 18:33:57916 l->append(", ");
[email protected]7d5c3ac2009-02-04 08:58:19917 LogParam(p.e, l);
918 }
919};
920
[email protected]7d5c3ac2009-02-04 08:58:19921//-----------------------------------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29922// Generic message subclasses
923
924// Used for asynchronous messages.
[email protected]81a34412009-01-05 19:17:24925template <class ParamType>
initial.commit09911bf2008-07-26 23:55:29926class MessageWithTuple : public Message {
927 public:
[email protected]81a34412009-01-05 19:17:24928 typedef ParamType Param;
[email protected]7a4de7a62010-08-17 18:38:24929 typedef typename TupleTypes<ParamType>::ParamTuple RefParam;
[email protected]81a34412009-01-05 19:17:24930
[email protected]7a4de7a62010-08-17 18:38:24931 // The constructor and the Read() method's templated implementations are in
932 // ipc_message_utils_impl.h. The subclass constructor and Log() methods call
933 // the templated versions of these and make sure there are instantiations in
934 // those translation units.
935 MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p);
[email protected]071b4b92010-08-09 16:06:58936
[email protected]7a4de7a62010-08-17 18:38:24937 static bool Read(const Message* msg, Param* p) IPC_MSG_NOINLINE;
initial.commit09911bf2008-07-26 23:55:29938
939 // Generic dispatcher. Should cover most cases.
940 template<class T, class Method>
[email protected]7d5c3ac2009-02-04 08:58:19941 static bool Dispatch(const Message* msg, T* obj, Method func) {
initial.commit09911bf2008-07-26 23:55:29942 Param p;
943 if (Read(msg, &p)) {
944 DispatchToMethod(obj, func, p);
945 return true;
946 }
947 return false;
948 }
949
950 // The following dispatchers exist for the case where the callback function
951 // needs the message as well. They assume that "Param" is a type of Tuple
952 // (except the one arg case, as there is no Tuple1).
953 template<class T, typename TA>
[email protected]7d5c3ac2009-02-04 08:58:19954 static bool Dispatch(const Message* msg, T* obj,
initial.commit09911bf2008-07-26 23:55:29955 void (T::*func)(const Message&, TA)) {
956 Param p;
957 if (Read(msg, &p)) {
[email protected]c2fe31542009-05-20 18:24:14958 (obj->*func)(*msg, p.a);
initial.commit09911bf2008-07-26 23:55:29959 return true;
960 }
961 return false;
962 }
963
964 template<class T, typename TA, typename TB>
[email protected]7d5c3ac2009-02-04 08:58:19965 static bool Dispatch(const Message* msg, T* obj,
initial.commit09911bf2008-07-26 23:55:29966 void (T::*func)(const Message&, TA, TB)) {
967 Param p;
968 if (Read(msg, &p)) {
969 (obj->*func)(*msg, p.a, p.b);
970 return true;
971 }
972 return false;
973 }
974
975 template<class T, typename TA, typename TB, typename TC>
[email protected]7d5c3ac2009-02-04 08:58:19976 static bool Dispatch(const Message* msg, T* obj,
initial.commit09911bf2008-07-26 23:55:29977 void (T::*func)(const Message&, TA, TB, TC)) {
978 Param p;
979 if (Read(msg, &p)) {
980 (obj->*func)(*msg, p.a, p.b, p.c);
981 return true;
982 }
983 return false;
984 }
985
986 template<class T, typename TA, typename TB, typename TC, typename TD>
[email protected]7d5c3ac2009-02-04 08:58:19987 static bool Dispatch(const Message* msg, T* obj,
initial.commit09911bf2008-07-26 23:55:29988 void (T::*func)(const Message&, TA, TB, TC, TD)) {
989 Param p;
990 if (Read(msg, &p)) {
991 (obj->*func)(*msg, p.a, p.b, p.c, p.d);
992 return true;
993 }
994 return false;
995 }
996
997 template<class T, typename TA, typename TB, typename TC, typename TD,
998 typename TE>
[email protected]7d5c3ac2009-02-04 08:58:19999 static bool Dispatch(const Message* msg, T* obj,
initial.commit09911bf2008-07-26 23:55:291000 void (T::*func)(const Message&, TA, TB, TC, TD, TE)) {
1001 Param p;
1002 if (Read(msg, &p)) {
1003 (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e);
1004 return true;
1005 }
1006 return false;
1007 }
1008
[email protected]deb57402009-02-06 01:35:301009 // Functions used to do manual unpacking. Only used by the automation code,
1010 // these should go away once that code uses SyncChannel.
1011 template<typename TA, typename TB>
1012 static bool Read(const IPC::Message* msg, TA* a, TB* b) {
1013 ParamType params;
1014 if (!Read(msg, &params))
1015 return false;
1016 *a = params.a;
1017 *b = params.b;
1018 return true;
1019 }
1020
1021 template<typename TA, typename TB, typename TC>
1022 static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c) {
1023 ParamType params;
1024 if (!Read(msg, &params))
1025 return false;
1026 *a = params.a;
1027 *b = params.b;
1028 *c = params.c;
1029 return true;
1030 }
1031
1032 template<typename TA, typename TB, typename TC, typename TD>
1033 static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d) {
1034 ParamType params;
1035 if (!Read(msg, &params))
1036 return false;
1037 *a = params.a;
1038 *b = params.b;
1039 *c = params.c;
1040 *d = params.d;
1041 return true;
1042 }
1043
1044 template<typename TA, typename TB, typename TC, typename TD, typename TE>
1045 static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d, TE* e) {
1046 ParamType params;
1047 if (!Read(msg, &params))
1048 return false;
1049 *a = params.a;
1050 *b = params.b;
1051 *c = params.c;
1052 *d = params.d;
1053 *e = params.e;
1054 return true;
1055 }
initial.commit09911bf2008-07-26 23:55:291056};
1057
[email protected]7a4de7a62010-08-17 18:38:241058// defined in ipc_logging.cc
1059void GenerateLogData(const std::string& channel, const Message& message,
1060 LogData* data);
1061
1062
1063#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]252cad62010-08-18 18:33:571064inline void AddOutputParamsToLog(const Message* msg, std::string* l) {
1065 const std::string& output_params = msg->output_params();
[email protected]7a4de7a62010-08-17 18:38:241066 if (!l->empty() && !output_params.empty())
[email protected]252cad62010-08-18 18:33:571067 l->append(", ");
[email protected]7a4de7a62010-08-17 18:38:241068
1069 l->append(output_params);
1070}
1071
1072template <class ReplyParamType>
1073inline void LogReplyParamsToMessage(const ReplyParamType& reply_params,
1074 const Message* msg) {
1075 if (msg->received_time() != 0) {
[email protected]252cad62010-08-18 18:33:571076 std::string output_params;
[email protected]7a4de7a62010-08-17 18:38:241077 LogParam(reply_params, &output_params);
1078 msg->set_output_params(output_params);
1079 }
1080}
1081
1082inline void ConnectMessageAndReply(const Message* msg, Message* reply) {
1083 if (msg->sent_time()) {
1084 // Don't log the sync message after dispatch, as we don't have the
1085 // output parameters at that point. Instead, save its data and log it
1086 // with the outgoing reply message when it's sent.
1087 LogData* data = new LogData;
1088 GenerateLogData("", *msg, data);
1089 msg->set_dont_log();
1090 reply->set_sync_log_data(data);
1091 }
1092}
1093#else
[email protected]252cad62010-08-18 18:33:571094inline void AddOutputParamsToLog(const Message* msg, std::string* l) {}
[email protected]7a4de7a62010-08-17 18:38:241095
1096template <class ReplyParamType>
1097inline void LogReplyParamsToMessage(const ReplyParamType& reply_params,
1098 const Message* msg) {}
1099
1100inline void ConnectMessageAndReply(const Message* msg, Message* reply) {}
1101#endif
1102
initial.commit09911bf2008-07-26 23:55:291103// This class assumes that its template argument is a RefTuple (a Tuple with
[email protected]7a4de7a62010-08-17 18:38:241104// reference elements). This would go into ipc_message_utils_impl.h, but it is
1105// also used by chrome_frame.
initial.commit09911bf2008-07-26 23:55:291106template <class RefTuple>
1107class ParamDeserializer : public MessageReplyDeserializer {
1108 public:
[email protected]e1981f432008-08-12 15:22:131109 explicit ParamDeserializer(const RefTuple& out) : out_(out) { }
initial.commit09911bf2008-07-26 23:55:291110
1111 bool SerializeOutputParameters(const IPC::Message& msg, void* iter) {
1112 return ReadParam(&msg, &iter, &out_);
1113 }
1114
1115 RefTuple out_;
1116};
1117
initial.commit09911bf2008-07-26 23:55:291118// Used for synchronous messages.
[email protected]75e5a872009-04-02 23:56:111119template <class SendParamType, class ReplyParamType>
initial.commit09911bf2008-07-26 23:55:291120class MessageWithReply : public SyncMessage {
1121 public:
[email protected]75e5a872009-04-02 23:56:111122 typedef SendParamType SendParam;
[email protected]7a4de7a62010-08-17 18:38:241123 typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam;
[email protected]75e5a872009-04-02 23:56:111124 typedef ReplyParamType ReplyParam;
1125
[email protected]168ae922009-12-04 18:08:451126 MessageWithReply(int32 routing_id, uint32 type,
[email protected]7a4de7a62010-08-17 18:38:241127 const RefSendParam& send, const ReplyParam& reply);
1128 static bool ReadSendParam(const Message* msg, SendParam* p) IPC_MSG_NOINLINE;
1129 static bool ReadReplyParam(
1130 const Message* msg,
1131 typename TupleTypes<ReplyParam>::ValueTuple* p) IPC_MSG_NOINLINE;
initial.commit09911bf2008-07-26 23:55:291132
1133 template<class T, class Method>
[email protected]7d5c3ac2009-02-04 08:58:191134 static bool Dispatch(const Message* msg, T* obj, Method func) {
initial.commit09911bf2008-07-26 23:55:291135 SendParam send_params;
initial.commit09911bf2008-07-26 23:55:291136 Message* reply = GenerateReply(msg);
1137 bool error;
[email protected]7a4de7a62010-08-17 18:38:241138 if (ReadSendParam(msg, &send_params)) {
1139 typename TupleTypes<ReplyParam>::ValueTuple reply_params;
initial.commit09911bf2008-07-26 23:55:291140 DispatchToMethod(obj, func, send_params, &reply_params);
1141 WriteParam(reply, reply_params);
1142 error = false;
[email protected]7a4de7a62010-08-17 18:38:241143 LogReplyParamsToMessage(reply_params, msg);
initial.commit09911bf2008-07-26 23:55:291144 } else {
1145 NOTREACHED() << "Error deserializing message " << msg->type();
1146 reply->set_reply_error();
1147 error = true;
1148 }
1149
1150 obj->Send(reply);
1151 return !error;
1152 }
1153
1154 template<class T, class Method>
[email protected]7d5c3ac2009-02-04 08:58:191155 static bool DispatchDelayReply(const Message* msg, T* obj, Method func) {
initial.commit09911bf2008-07-26 23:55:291156 SendParam send_params;
initial.commit09911bf2008-07-26 23:55:291157 Message* reply = GenerateReply(msg);
1158 bool error;
[email protected]7a4de7a62010-08-17 18:38:241159 if (ReadSendParam(msg, &send_params)) {
initial.commit09911bf2008-07-26 23:55:291160 Tuple1<Message&> t = MakeRefTuple(*reply);
[email protected]7a4de7a62010-08-17 18:38:241161 ConnectMessageAndReply(msg, reply);
initial.commit09911bf2008-07-26 23:55:291162 DispatchToMethod(obj, func, send_params, &t);
1163 error = false;
1164 } else {
1165 NOTREACHED() << "Error deserializing message " << msg->type();
1166 reply->set_reply_error();
1167 obj->Send(reply);
1168 error = true;
1169 }
1170 return !error;
1171 }
1172
1173 template<typename TA>
1174 static void WriteReplyParams(Message* reply, TA a) {
1175 ReplyParam p(a);
1176 WriteParam(reply, p);
1177 }
1178
1179 template<typename TA, typename TB>
1180 static void WriteReplyParams(Message* reply, TA a, TB b) {
1181 ReplyParam p(a, b);
1182 WriteParam(reply, p);
1183 }
1184
1185 template<typename TA, typename TB, typename TC>
1186 static void WriteReplyParams(Message* reply, TA a, TB b, TC c) {
1187 ReplyParam p(a, b, c);
1188 WriteParam(reply, p);
1189 }
1190
1191 template<typename TA, typename TB, typename TC, typename TD>
1192 static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d) {
1193 ReplyParam p(a, b, c, d);
1194 WriteParam(reply, p);
1195 }
1196
1197 template<typename TA, typename TB, typename TC, typename TD, typename TE>
1198 static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) {
1199 ReplyParam p(a, b, c, d, e);
1200 WriteParam(reply, p);
1201 }
1202};
1203
[email protected]7d5c3ac2009-02-04 08:58:191204//-----------------------------------------------------------------------------
1205
[email protected]3178f4e22008-08-05 21:20:411206} // namespace IPC
initial.commit09911bf2008-07-26 23:55:291207
[email protected]946d1b22009-07-22 23:57:211208#endif // IPC_IPC_MESSAGE_UTILS_H_