blob: 2acddceb51c870752c58138779f4a4e4b6b01b61 [file] [log] [blame]
[email protected]a7c03d4f32012-01-24 02:36:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]946d1b22009-07-22 23:57:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ipc/ipc_message_utils.h"
6
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]93d49d72009-10-23 20:00:208#include "base/json/json_writer.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]0238a162013-06-13 13:47:4610#include "base/strings/nullable_string16.h"
[email protected]4aa794a12013-06-11 06:32:1811#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4512#include "base/strings/utf_string_conversions.h"
[email protected]b43e5562013-06-28 15:20:0213#include "base/time/time.h"
[email protected]946d1b22009-07-22 23:57:2114#include "base/values.h"
[email protected]bf5aedf02012-06-04 21:18:2515#include "ipc/ipc_channel_handle.h"
16
[email protected]7a4de7a62010-08-17 18:38:2417#if defined(OS_POSIX)
18#include "ipc/file_descriptor_set_posix.h"
[email protected]2e02cfe82012-11-21 00:58:0019#elif defined(OS_WIN)
20#include <tchar.h>
[email protected]7a4de7a62010-08-17 18:38:2421#endif
[email protected]946d1b22009-07-22 23:57:2122
23namespace IPC {
24
[email protected]bf5aedf02012-06-04 21:18:2525namespace {
26
[email protected]946d1b22009-07-22 23:57:2127const int kMaxRecursionDepth = 100;
28
[email protected]bf5aedf02012-06-04 21:18:2529template<typename CharType>
30void LogBytes(const std::vector<CharType>& data, std::string* out) {
31#if defined(OS_WIN)
32 // Windows has a GUI for logging, which can handle arbitrary binary data.
33 for (size_t i = 0; i < data.size(); ++i)
34 out->push_back(data[i]);
35#else
36 // On POSIX, we log to stdout, which we assume can display ASCII.
37 static const size_t kMaxBytesToLog = 100;
38 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
39 if (isprint(data[i]))
40 out->push_back(data[i]);
41 else
[email protected]7d3cbc92013-03-18 22:33:0442 out->append(
43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
[email protected]bf5aedf02012-06-04 21:18:2544 }
45 if (data.size() > kMaxBytesToLog) {
[email protected]f8660f82013-03-30 17:29:2846 out->append(base::StringPrintf(
47 " and %u more bytes",
48 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
[email protected]bf5aedf02012-06-04 21:18:2549 }
50#endif
51}
[email protected]946d1b22009-07-22 23:57:2152
[email protected]ea5ef4c2013-06-13 22:50:2753bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:2554 int recursion);
[email protected]946d1b22009-07-22 23:57:2155
[email protected]ea5ef4c2013-06-13 22:50:2756void WriteValue(Message* m, const base::Value* value, int recursion) {
[email protected]dbc761a2012-07-26 01:29:2157 bool result;
[email protected]946d1b22009-07-22 23:57:2158 if (recursion > kMaxRecursionDepth) {
59 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
60 return;
61 }
62
63 m->WriteInt(value->GetType());
64
65 switch (value->GetType()) {
[email protected]ea5ef4c2013-06-13 22:50:2766 case base::Value::TYPE_NULL:
[email protected]946d1b22009-07-22 23:57:2167 break;
[email protected]ea5ef4c2013-06-13 22:50:2768 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:2169 bool val;
[email protected]dbc761a2012-07-26 01:29:2170 result = value->GetAsBoolean(&val);
71 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2172 WriteParam(m, val);
73 break;
74 }
[email protected]ea5ef4c2013-06-13 22:50:2775 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:2176 int val;
[email protected]dbc761a2012-07-26 01:29:2177 result = value->GetAsInteger(&val);
78 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2179 WriteParam(m, val);
80 break;
81 }
[email protected]ea5ef4c2013-06-13 22:50:2782 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:2183 double val;
[email protected]dbc761a2012-07-26 01:29:2184 result = value->GetAsDouble(&val);
85 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2186 WriteParam(m, val);
87 break;
88 }
[email protected]ea5ef4c2013-06-13 22:50:2789 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:2190 std::string val;
[email protected]dbc761a2012-07-26 01:29:2191 result = value->GetAsString(&val);
92 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2193 WriteParam(m, val);
94 break;
95 }
[email protected]ea5ef4c2013-06-13 22:50:2796 case base::Value::TYPE_BINARY: {
[email protected]b59ea312011-08-05 18:20:0597 const base::BinaryValue* binary =
98 static_cast<const base::BinaryValue*>(value);
[email protected]7ee1a44c2010-07-23 14:18:5999 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
[email protected]e4dad9fb2009-10-06 18:15:58100 break;
[email protected]946d1b22009-07-22 23:57:21101 }
[email protected]ea5ef4c2013-06-13 22:50:27102 case base::Value::TYPE_DICTIONARY: {
103 const base::DictionaryValue* dict =
104 static_cast<const base::DictionaryValue*>(value);
[email protected]946d1b22009-07-22 23:57:21105
[email protected]4dad9ad82009-11-25 20:47:52106 WriteParam(m, static_cast<int>(dict->size()));
[email protected]946d1b22009-07-22 23:57:21107
[email protected]ea5ef4c2013-06-13 22:50:27108 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
109 it.Advance()) {
[email protected]a899c0b02013-01-18 14:43:27110 WriteParam(m, it.key());
111 WriteValue(m, &it.value(), recursion + 1);
[email protected]946d1b22009-07-22 23:57:21112 }
113 break;
114 }
[email protected]ea5ef4c2013-06-13 22:50:27115 case base::Value::TYPE_LIST: {
116 const base::ListValue* list = static_cast<const base::ListValue*>(value);
[email protected]946d1b22009-07-22 23:57:21117 WriteParam(m, static_cast<int>(list->GetSize()));
[email protected]ea5ef4c2013-06-13 22:50:27118 for (base::ListValue::const_iterator it = list->begin();
119 it != list->end(); ++it) {
[email protected]a899c0b02013-01-18 14:43:27120 WriteValue(m, *it, recursion + 1);
[email protected]946d1b22009-07-22 23:57:21121 }
122 break;
123 }
124 }
125}
126
127// Helper for ReadValue that reads a DictionaryValue into a pre-allocated
128// object.
[email protected]bf5aedf02012-06-04 21:18:25129bool ReadDictionaryValue(const Message* m, PickleIterator* iter,
[email protected]ea5ef4c2013-06-13 22:50:27130 base::DictionaryValue* value, int recursion) {
[email protected]946d1b22009-07-22 23:57:21131 int size;
132 if (!ReadParam(m, iter, &size))
133 return false;
134
135 for (int i = 0; i < size; ++i) {
[email protected]e7b418b2010-07-30 19:47:47136 std::string key;
[email protected]0c6c1e42013-06-21 19:42:19137 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21138 if (!ReadParam(m, iter, &key) ||
139 !ReadValue(m, iter, &subval, recursion + 1))
140 return false;
[email protected]d8b4aa42011-08-19 05:59:57141 value->SetWithoutPathExpansion(key, subval);
[email protected]946d1b22009-07-22 23:57:21142 }
143
144 return true;
145}
146
147// Helper for ReadValue that reads a ReadListValue into a pre-allocated
148// object.
[email protected]bf5aedf02012-06-04 21:18:25149bool ReadListValue(const Message* m, PickleIterator* iter,
[email protected]ea5ef4c2013-06-13 22:50:27150 base::ListValue* value, int recursion) {
[email protected]946d1b22009-07-22 23:57:21151 int size;
152 if (!ReadParam(m, iter, &size))
153 return false;
154
155 for (int i = 0; i < size; ++i) {
[email protected]ea5ef4c2013-06-13 22:50:27156 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21157 if (!ReadValue(m, iter, &subval, recursion + 1))
158 return false;
159 value->Set(i, subval);
160 }
161
162 return true;
163}
164
[email protected]ea5ef4c2013-06-13 22:50:27165bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:25166 int recursion) {
[email protected]946d1b22009-07-22 23:57:21167 if (recursion > kMaxRecursionDepth) {
168 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
169 return false;
170 }
171
172 int type;
173 if (!ReadParam(m, iter, &type))
174 return false;
175
176 switch (type) {
[email protected]ea5ef4c2013-06-13 22:50:27177 case base::Value::TYPE_NULL:
178 *value = base::Value::CreateNullValue();
[email protected]946d1b22009-07-22 23:57:21179 break;
[email protected]ea5ef4c2013-06-13 22:50:27180 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:21181 bool val;
182 if (!ReadParam(m, iter, &val))
183 return false;
[email protected]4038a132013-01-30 05:24:07184 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21185 break;
186 }
[email protected]ea5ef4c2013-06-13 22:50:27187 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:21188 int val;
189 if (!ReadParam(m, iter, &val))
190 return false;
[email protected]4038a132013-01-30 05:24:07191 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21192 break;
193 }
[email protected]ea5ef4c2013-06-13 22:50:27194 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:21195 double val;
196 if (!ReadParam(m, iter, &val))
197 return false;
[email protected]4038a132013-01-30 05:24:07198 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21199 break;
200 }
[email protected]ea5ef4c2013-06-13 22:50:27201 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:21202 std::string val;
203 if (!ReadParam(m, iter, &val))
204 return false;
[email protected]4038a132013-01-30 05:24:07205 *value = new base::StringValue(val);
[email protected]946d1b22009-07-22 23:57:21206 break;
207 }
[email protected]ea5ef4c2013-06-13 22:50:27208 case base::Value::TYPE_BINARY: {
[email protected]e4dad9fb2009-10-06 18:15:58209 const char* data;
210 int length;
211 if (!m->ReadData(iter, &data, &length))
212 return false;
[email protected]b59ea312011-08-05 18:20:05213 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length);
[email protected]946d1b22009-07-22 23:57:21214 break;
215 }
[email protected]ea5ef4c2013-06-13 22:50:27216 case base::Value::TYPE_DICTIONARY: {
217 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue());
[email protected]946d1b22009-07-22 23:57:21218 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
219 return false;
220 *value = val.release();
221 break;
222 }
[email protected]ea5ef4c2013-06-13 22:50:27223 case base::Value::TYPE_LIST: {
224 scoped_ptr<base::ListValue> val(new base::ListValue());
[email protected]946d1b22009-07-22 23:57:21225 if (!ReadListValue(m, iter, val.get(), recursion))
226 return false;
227 *value = val.release();
228 break;
229 }
[email protected]e4dad9fb2009-10-06 18:15:58230 default:
[email protected]946d1b22009-07-22 23:57:21231 return false;
232 }
233
234 return true;
235}
236
[email protected]bf5aedf02012-06-04 21:18:25237} // namespace
238
239// -----------------------------------------------------------------------------
240
241LogData::LogData()
242 : routing_id(0),
243 type(0),
244 sent(0),
245 receive(0),
246 dispatch(0) {
247}
248
249LogData::~LogData() {
250}
251
[email protected]bf5aedf02012-06-04 21:18:25252void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
253 l->append(p ? "true" : "false");
254}
255
[email protected]c1ee48d2013-07-12 23:12:28256void ParamTraits<unsigned char>::Write(Message* m, const param_type& p) {
257 m->WriteBytes(&p, sizeof(param_type));
258}
259
260bool ParamTraits<unsigned char>::Read(const Message* m, PickleIterator* iter,
261 param_type* r) {
262 const char* data;
263 if (!m->ReadBytes(iter, &data, sizeof(param_type)))
264 return false;
265 memcpy(r, data, sizeof(param_type));
266 return true;
267}
268
269void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
270 l->append(base::UintToString(p));
271}
272
273void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) {
274 m->WriteBytes(&p, sizeof(param_type));
275}
276
277bool ParamTraits<unsigned short>::Read(const Message* m, PickleIterator* iter,
278 param_type* r) {
279 const char* data;
280 if (!m->ReadBytes(iter, &data, sizeof(param_type)))
281 return false;
282 memcpy(r, data, sizeof(param_type));
283 return true;
284}
285
286void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
287 l->append(base::UintToString(p));
288}
289
[email protected]252cad62010-08-18 18:33:57290void ParamTraits<int>::Log(const param_type& p, std::string* l) {
291 l->append(base::IntToString(p));
292}
293
294void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
295 l->append(base::UintToString(p));
296}
297
298void ParamTraits<long>::Log(const param_type& p, std::string* l) {
299 l->append(base::Int64ToString(static_cast<int64>(p)));
300}
301
302void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
303 l->append(base::Uint64ToString(static_cast<uint64>(p)));
304}
305
306void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
307 l->append(base::Int64ToString(static_cast<int64>(p)));
308}
309
310void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
311 l->append(base::Uint64ToString(p));
312}
[email protected]7a4de7a62010-08-17 18:38:24313
[email protected]bf5aedf02012-06-04 21:18:25314void ParamTraits<float>::Write(Message* m, const param_type& p) {
315 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
[email protected]c410e022012-05-30 21:15:57316}
317
[email protected]bf5aedf02012-06-04 21:18:25318bool ParamTraits<float>::Read(const Message* m, PickleIterator* iter,
319 param_type* r) {
320 const char *data;
321 int data_size;
322 if (!m->ReadData(iter, &data, &data_size) ||
323 data_size != sizeof(param_type)) {
324 NOTREACHED();
[email protected]7a4de7a62010-08-17 18:38:24325 return false;
[email protected]bf5aedf02012-06-04 21:18:25326 }
327 memcpy(r, data, sizeof(param_type));
[email protected]7a4de7a62010-08-17 18:38:24328 return true;
329}
330
[email protected]bf5aedf02012-06-04 21:18:25331void ParamTraits<float>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04332 l->append(base::StringPrintf("%e", p));
[email protected]7a4de7a62010-08-17 18:38:24333}
334
[email protected]bf5aedf02012-06-04 21:18:25335void ParamTraits<double>::Write(Message* m, const param_type& p) {
336 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
[email protected]d84e48b2010-10-21 22:04:52337}
338
[email protected]bf5aedf02012-06-04 21:18:25339bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter,
340 param_type* r) {
341 const char *data;
342 int data_size;
343 if (!m->ReadData(iter, &data, &data_size) ||
344 data_size != sizeof(param_type)) {
345 NOTREACHED();
346 return false;
347 }
348 memcpy(r, data, sizeof(param_type));
349 return true;
[email protected]d84e48b2010-10-21 22:04:52350}
351
[email protected]bf5aedf02012-06-04 21:18:25352void ParamTraits<double>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04353 l->append(base::StringPrintf("%e", p));
[email protected]1d14f582011-09-02 20:42:04354}
355
[email protected]bf5aedf02012-06-04 21:18:25356
357void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
358 l->append(p);
[email protected]1d14f582011-09-02 20:42:04359}
360
[email protected]bf5aedf02012-06-04 21:18:25361void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
362 l->append(WideToUTF8(p));
[email protected]1d14f582011-09-02 20:42:04363}
364
[email protected]bf5aedf02012-06-04 21:18:25365#if !defined(WCHAR_T_IS_UTF16)
366void ParamTraits<string16>::Log(const param_type& p, std::string* l) {
367 l->append(UTF16ToUTF8(p));
368}
369#endif
370
371void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
372 if (p.empty()) {
373 m->WriteData(NULL, 0);
374 } else {
375 m->WriteData(&p.front(), static_cast<int>(p.size()));
376 }
377}
378
379bool ParamTraits<std::vector<char> >::Read(const Message* m,
380 PickleIterator* iter,
381 param_type* r) {
382 const char *data;
383 int data_size = 0;
384 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
385 return false;
386 r->resize(data_size);
387 if (data_size)
388 memcpy(&r->front(), data, data_size);
389 return true;
390}
391
392void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
393 LogBytes(p, l);
394}
395
396void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
397 const param_type& p) {
398 if (p.empty()) {
399 m->WriteData(NULL, 0);
400 } else {
401 m->WriteData(reinterpret_cast<const char*>(&p.front()),
402 static_cast<int>(p.size()));
403 }
404}
405
406bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
407 PickleIterator* iter,
408 param_type* r) {
409 const char *data;
410 int data_size = 0;
411 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
412 return false;
413 r->resize(data_size);
414 if (data_size)
415 memcpy(&r->front(), data, data_size);
416 return true;
417}
418
419void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
420 std::string* l) {
421 LogBytes(p, l);
422}
423
424void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
425 WriteParam(m, static_cast<int>(p.size()));
[email protected]d4124852013-03-20 20:25:00426 // Cast to bool below is required because libc++'s
427 // vector<bool>::const_reference is different from bool, and we want to avoid
428 // writing an extra specialization of ParamTraits for it.
[email protected]bf5aedf02012-06-04 21:18:25429 for (size_t i = 0; i < p.size(); i++)
[email protected]d4124852013-03-20 20:25:00430 WriteParam(m, static_cast<bool>(p[i]));
[email protected]bf5aedf02012-06-04 21:18:25431}
432
433bool ParamTraits<std::vector<bool> >::Read(const Message* m,
434 PickleIterator* iter,
435 param_type* r) {
436 int size;
437 // ReadLength() checks for < 0 itself.
438 if (!m->ReadLength(iter, &size))
439 return false;
440 r->resize(size);
441 for (int i = 0; i < size; i++) {
442 bool value;
443 if (!ReadParam(m, iter, &value))
444 return false;
445 (*r)[i] = value;
446 }
447 return true;
448}
449
450void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
451 for (size_t i = 0; i < p.size(); ++i) {
452 if (i != 0)
453 l->push_back(' ');
[email protected]d4124852013-03-20 20:25:00454 LogParam(static_cast<bool>(p[i]), l);
[email protected]bf5aedf02012-06-04 21:18:25455 }
[email protected]d84e48b2010-10-21 22:04:52456}
457
[email protected]ea5ef4c2013-06-13 22:50:27458void ParamTraits<base::DictionaryValue>::Write(Message* m,
459 const param_type& p) {
[email protected]946d1b22009-07-22 23:57:21460 WriteValue(m, &p, 0);
461}
462
[email protected]ea5ef4c2013-06-13 22:50:27463bool ParamTraits<base::DictionaryValue>::Read(
[email protected]ce208f872012-03-07 20:42:56464 const Message* m, PickleIterator* iter, param_type* r) {
[email protected]946d1b22009-07-22 23:57:21465 int type;
[email protected]0c6c1e42013-06-21 19:42:19466 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
[email protected]946d1b22009-07-22 23:57:21467 return false;
468
469 return ReadDictionaryValue(m, iter, r, 0);
470}
471
[email protected]ea5ef4c2013-06-13 22:50:27472void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
473 std::string* l) {
[email protected]946d1b22009-07-22 23:57:21474 std::string json;
[email protected]4abb4602012-03-16 01:59:55475 base::JSONWriter::Write(&p, &json);
[email protected]252cad62010-08-18 18:33:57476 l->append(json);
[email protected]946d1b22009-07-22 23:57:21477}
478
[email protected]7a4de7a62010-08-17 18:38:24479#if defined(OS_POSIX)
480void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
481 const bool valid = p.fd >= 0;
482 WriteParam(m, valid);
483
484 if (valid) {
485 if (!m->WriteFileDescriptor(p))
486 NOTREACHED();
487 }
488}
489
[email protected]ce208f872012-03-07 20:42:56490bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
491 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24492 param_type* r) {
493 bool valid;
494 if (!ReadParam(m, iter, &valid))
495 return false;
496
497 if (!valid) {
498 r->fd = -1;
499 r->auto_close = false;
500 return true;
501 }
502
503 return m->ReadFileDescriptor(iter, r);
504}
505
506void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57507 std::string* l) {
[email protected]7a4de7a62010-08-17 18:38:24508 if (p.auto_close) {
[email protected]7d3cbc92013-03-18 22:33:04509 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24510 } else {
[email protected]7d3cbc92013-03-18 22:33:04511 l->append(base::StringPrintf("FD(%d)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24512 }
513}
514#endif // defined(OS_POSIX)
515
[email protected]6d4b67a2013-02-10 04:49:30516void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) {
[email protected]aeae59f2013-01-28 13:47:55517 p.WriteToPickle(m);
[email protected]bf5aedf02012-06-04 21:18:25518}
519
[email protected]6d4b67a2013-02-10 04:49:30520bool ParamTraits<base::FilePath>::Read(const Message* m,
521 PickleIterator* iter,
522 param_type* r) {
[email protected]aeae59f2013-01-28 13:47:55523 return r->ReadFromPickle(iter);
[email protected]bf5aedf02012-06-04 21:18:25524}
525
[email protected]6d4b67a2013-02-10 04:49:30526void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
527 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
[email protected]bf5aedf02012-06-04 21:18:25528}
529
[email protected]ea5ef4c2013-06-13 22:50:27530void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25531 WriteValue(m, &p, 0);
532}
533
[email protected]ea5ef4c2013-06-13 22:50:27534bool ParamTraits<base::ListValue>::Read(
[email protected]bf5aedf02012-06-04 21:18:25535 const Message* m, PickleIterator* iter, param_type* r) {
536 int type;
[email protected]0c6c1e42013-06-21 19:42:19537 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
[email protected]bf5aedf02012-06-04 21:18:25538 return false;
539
540 return ReadListValue(m, iter, r, 0);
541}
542
[email protected]ea5ef4c2013-06-13 22:50:27543void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25544 std::string json;
545 base::JSONWriter::Write(&p, &json);
546 l->append(json);
547}
548
[email protected]0238a162013-06-13 13:47:46549void ParamTraits<base::NullableString16>::Write(Message* m,
550 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25551 WriteParam(m, p.string());
552 WriteParam(m, p.is_null());
553}
554
[email protected]0238a162013-06-13 13:47:46555bool ParamTraits<base::NullableString16>::Read(const Message* m,
556 PickleIterator* iter,
557 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25558 string16 string;
559 if (!ReadParam(m, iter, &string))
560 return false;
561 bool is_null;
562 if (!ReadParam(m, iter, &is_null))
563 return false;
[email protected]0238a162013-06-13 13:47:46564 *r = base::NullableString16(string, is_null);
[email protected]bf5aedf02012-06-04 21:18:25565 return true;
566}
567
[email protected]0238a162013-06-13 13:47:46568void ParamTraits<base::NullableString16>::Log(const param_type& p,
569 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25570 l->append("(");
571 LogParam(p.string(), l);
572 l->append(", ");
573 LogParam(p.is_null(), l);
574 l->append(")");
575}
576
577void ParamTraits<base::PlatformFileInfo>::Write(Message* m,
578 const param_type& p) {
579 WriteParam(m, p.size);
580 WriteParam(m, p.is_directory);
581 WriteParam(m, p.last_modified.ToDoubleT());
582 WriteParam(m, p.last_accessed.ToDoubleT());
583 WriteParam(m, p.creation_time.ToDoubleT());
584}
585
586bool ParamTraits<base::PlatformFileInfo>::Read(const Message* m,
587 PickleIterator* iter,
588 param_type* p) {
589 double last_modified;
590 double last_accessed;
591 double creation_time;
592 bool result =
593 ReadParam(m, iter, &p->size) &&
594 ReadParam(m, iter, &p->is_directory) &&
595 ReadParam(m, iter, &last_modified) &&
596 ReadParam(m, iter, &last_accessed) &&
597 ReadParam(m, iter, &creation_time);
598 if (result) {
599 p->last_modified = base::Time::FromDoubleT(last_modified);
600 p->last_accessed = base::Time::FromDoubleT(last_accessed);
601 p->creation_time = base::Time::FromDoubleT(creation_time);
602 }
603 return result;
604}
605
606void ParamTraits<base::PlatformFileInfo>::Log(const param_type& p,
607 std::string* l) {
608 l->append("(");
609 LogParam(p.size, l);
610 l->append(",");
611 LogParam(p.is_directory, l);
612 l->append(",");
613 LogParam(p.last_modified.ToDoubleT(), l);
614 l->append(",");
615 LogParam(p.last_accessed.ToDoubleT(), l);
616 l->append(",");
617 LogParam(p.creation_time.ToDoubleT(), l);
618 l->append(")");
619}
620
621void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
622 ParamTraits<int64>::Write(m, p.ToInternalValue());
623}
624
625bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
626 param_type* r) {
627 int64 value;
628 if (!ParamTraits<int64>::Read(m, iter, &value))
629 return false;
630 *r = base::Time::FromInternalValue(value);
631 return true;
632}
633
634void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
635 ParamTraits<int64>::Log(p.ToInternalValue(), l);
636}
637
638void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
639 ParamTraits<int64>::Write(m, p.ToInternalValue());
640}
641
642bool ParamTraits<base::TimeDelta>::Read(const Message* m,
643 PickleIterator* iter,
644 param_type* r) {
645 int64 value;
646 bool ret = ParamTraits<int64>::Read(m, iter, &value);
647 if (ret)
648 *r = base::TimeDelta::FromInternalValue(value);
649
650 return ret;
651}
652
653void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
654 ParamTraits<int64>::Log(p.ToInternalValue(), l);
655}
656
657void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
658 ParamTraits<int64>::Write(m, p.ToInternalValue());
659}
660
661bool ParamTraits<base::TimeTicks>::Read(const Message* m,
662 PickleIterator* iter,
663 param_type* r) {
664 int64 value;
665 bool ret = ParamTraits<int64>::Read(m, iter, &value);
666 if (ret)
667 *r = base::TimeTicks::FromInternalValue(value);
668
669 return ret;
670}
671
672void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
673 ParamTraits<int64>::Log(p.ToInternalValue(), l);
674}
675
[email protected]7a4de7a62010-08-17 18:38:24676void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
[email protected]a7c03d4f32012-01-24 02:36:05677#if defined(OS_WIN)
678 // On Windows marshalling pipe handle is not supported.
679 DCHECK(p.pipe.handle == NULL);
680#endif // defined (OS_WIN)
[email protected]7a4de7a62010-08-17 18:38:24681 WriteParam(m, p.name);
682#if defined(OS_POSIX)
683 WriteParam(m, p.socket);
684#endif
685}
686
[email protected]ce208f872012-03-07 20:42:56687bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
688 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24689 param_type* r) {
690 return ReadParam(m, iter, &r->name)
691#if defined(OS_POSIX)
692 && ReadParam(m, iter, &r->socket)
693#endif
694 ;
695}
696
697void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57698 std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04699 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
[email protected]7a4de7a62010-08-17 18:38:24700#if defined(OS_POSIX)
[email protected]3cd3bce2011-09-23 10:32:19701 l->append(", ");
[email protected]7a4de7a62010-08-17 18:38:24702 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
703#endif
[email protected]252cad62010-08-18 18:33:57704 l->append(")");
[email protected]7a4de7a62010-08-17 18:38:24705}
706
[email protected]20f0487a2010-09-30 20:06:30707void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
708 WriteParam(m, p.channel);
709 WriteParam(m, p.routing_id);
[email protected]8bf55ca2011-10-17 22:15:27710 WriteParam(m, p.type);
[email protected]20f0487a2010-09-30 20:06:30711 WriteParam(m, p.flags);
712 WriteParam(m, p.sent);
713 WriteParam(m, p.receive);
714 WriteParam(m, p.dispatch);
[email protected]bae578e92012-11-15 03:17:45715 WriteParam(m, p.message_name);
[email protected]20f0487a2010-09-30 20:06:30716 WriteParam(m, p.params);
717}
718
[email protected]ce208f872012-03-07 20:42:56719bool ParamTraits<LogData>::Read(const Message* m,
720 PickleIterator* iter,
721 param_type* r) {
[email protected]8bf55ca2011-10-17 22:15:27722 return
[email protected]20f0487a2010-09-30 20:06:30723 ReadParam(m, iter, &r->channel) &&
724 ReadParam(m, iter, &r->routing_id) &&
[email protected]8bf55ca2011-10-17 22:15:27725 ReadParam(m, iter, &r->type) &&
[email protected]20f0487a2010-09-30 20:06:30726 ReadParam(m, iter, &r->flags) &&
727 ReadParam(m, iter, &r->sent) &&
728 ReadParam(m, iter, &r->receive) &&
729 ReadParam(m, iter, &r->dispatch) &&
[email protected]bae578e92012-11-15 03:17:45730 ReadParam(m, iter, &r->message_name) &&
[email protected]20f0487a2010-09-30 20:06:30731 ReadParam(m, iter, &r->params);
[email protected]20f0487a2010-09-30 20:06:30732}
733
[email protected]bf5aedf02012-06-04 21:18:25734void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
735 // Doesn't make sense to implement this!
736}
737
738void ParamTraits<Message>::Write(Message* m, const Message& p) {
[email protected]34d48612012-06-29 00:05:04739#if defined(OS_POSIX)
740 // We don't serialize the file descriptors in the nested message, so there
741 // better not be any.
742 DCHECK(!p.HasFileDescriptors());
743#endif
744
745 // Don't just write out the message. This is used to send messages between
746 // NaCl (Posix environment) and the browser (could be on Windows). The message
747 // header formats differ between these systems (so does handle sharing, but
748 // we already asserted we don't have any handles). So just write out the
749 // parts of the header we use.
750 //
751 // Be careful also to use only explicitly-sized types. The NaCl environment
752 // could be 64-bit and the host browser could be 32-bits. The nested message
753 // may or may not be safe to send between 32-bit and 64-bit systems, but we
754 // leave that up to the code sending the message to ensure.
755 m->WriteUInt32(static_cast<uint32>(p.routing_id()));
756 m->WriteUInt32(p.type());
757 m->WriteUInt32(p.flags());
758 m->WriteData(p.payload(), static_cast<uint32>(p.payload_size()));
[email protected]bf5aedf02012-06-04 21:18:25759}
760
761bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
762 Message* r) {
[email protected]34d48612012-06-29 00:05:04763 uint32 routing_id, type, flags;
764 if (!m->ReadUInt32(iter, &routing_id) ||
765 !m->ReadUInt32(iter, &type) ||
766 !m->ReadUInt32(iter, &flags))
[email protected]bf5aedf02012-06-04 21:18:25767 return false;
[email protected]34d48612012-06-29 00:05:04768
769 int payload_size;
770 const char* payload;
771 if (!m->ReadData(iter, &payload, &payload_size))
[email protected]bf5aedf02012-06-04 21:18:25772 return false;
[email protected]34d48612012-06-29 00:05:04773
774 r->SetHeaderValues(static_cast<int32>(routing_id), type, flags);
775 return r->WriteBytes(payload, payload_size);
[email protected]bf5aedf02012-06-04 21:18:25776}
777
778void ParamTraits<Message>::Log(const Message& p, std::string* l) {
779 l->append("<IPC::Message>");
780}
781
782#if defined(OS_WIN)
783// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
[email protected]4a635b72013-03-04 02:29:03784// bit systems. That's why we use the Windows macros to convert to 32 bits.
[email protected]bf5aedf02012-06-04 21:18:25785void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
[email protected]4a635b72013-03-04 02:29:03786 m->WriteInt(HandleToLong(p));
[email protected]bf5aedf02012-06-04 21:18:25787}
788
789bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
790 param_type* r) {
[email protected]4a635b72013-03-04 02:29:03791 int32 temp;
792 if (!m->ReadInt(iter, &temp))
[email protected]bf5aedf02012-06-04 21:18:25793 return false;
[email protected]4a635b72013-03-04 02:29:03794 *r = LongToHandle(temp);
[email protected]bf5aedf02012-06-04 21:18:25795 return true;
796}
797
798void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04799 l->append(base::StringPrintf("0x%X", p));
[email protected]bf5aedf02012-06-04 21:18:25800}
801
802void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
803 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
804}
805
806bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
807 param_type* r) {
808 const char *data;
809 int data_size = 0;
[email protected]2e02cfe82012-11-21 00:58:00810 if (m->ReadData(iter, &data, &data_size) && data_size == sizeof(LOGFONT)) {
811 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
812 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
813 memcpy(r, data, sizeof(LOGFONT));
814 return true;
815 }
[email protected]bf5aedf02012-06-04 21:18:25816 }
817
[email protected]2e02cfe82012-11-21 00:58:00818 NOTREACHED();
819 return false;
[email protected]bf5aedf02012-06-04 21:18:25820}
821
822void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04823 l->append(base::StringPrintf("<LOGFONT>"));
[email protected]bf5aedf02012-06-04 21:18:25824}
825
826void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
827 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
828}
829
830bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
831 param_type* r) {
832 const char *data;
833 int data_size = 0;
834 bool result = m->ReadData(iter, &data, &data_size);
835 if (result && data_size == sizeof(MSG)) {
836 memcpy(r, data, sizeof(MSG));
837 } else {
838 result = false;
839 NOTREACHED();
840 }
841
842 return result;
843}
844
845void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
846 l->append("<MSG>");
847}
848
849#endif // OS_WIN
850
[email protected]946d1b22009-07-22 23:57:21851} // namespace IPC