1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // 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
7 #include "base/files/file_path.h"
8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/nullable_string16.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "ipc/ipc_channel_handle.h"
16
17 #if defined(OS_POSIX)
18 #include "ipc/file_descriptor_set_posix.h"
19 #elif defined(OS_WIN)
20 #include <tchar.h>
21 #endif
22
23 namespace IPC {
24
25 namespace {
26
27 const int kMaxRecursionDepth = 100;
28
29 template<typename CharType>
LogBytes(const std::vector<CharType> & data,std::string * out)30 void 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
42 out->append(
43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
44 }
45 if (data.size() > kMaxBytesToLog) {
46 out->append(base::StringPrintf(
47 " and %u more bytes",
48 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
49 }
50 #endif
51 }
52
53 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
54 int recursion);
55
WriteValue(Message * m,const base::Value * value,int recursion)56 void WriteValue(Message* m, const base::Value* value, int recursion) {
57 bool result;
58 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()) {
66 case base::Value::TYPE_NULL:
67 break;
68 case base::Value::TYPE_BOOLEAN: {
69 bool val;
70 result = value->GetAsBoolean(&val);
71 DCHECK(result);
72 WriteParam(m, val);
73 break;
74 }
75 case base::Value::TYPE_INTEGER: {
76 int val;
77 result = value->GetAsInteger(&val);
78 DCHECK(result);
79 WriteParam(m, val);
80 break;
81 }
82 case base::Value::TYPE_DOUBLE: {
83 double val;
84 result = value->GetAsDouble(&val);
85 DCHECK(result);
86 WriteParam(m, val);
87 break;
88 }
89 case base::Value::TYPE_STRING: {
90 std::string val;
91 result = value->GetAsString(&val);
92 DCHECK(result);
93 WriteParam(m, val);
94 break;
95 }
96 case base::Value::TYPE_BINARY: {
97 const base::BinaryValue* binary =
98 static_cast<const base::BinaryValue*>(value);
99 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
100 break;
101 }
102 case base::Value::TYPE_DICTIONARY: {
103 const base::DictionaryValue* dict =
104 static_cast<const base::DictionaryValue*>(value);
105
106 WriteParam(m, static_cast<int>(dict->size()));
107
108 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
109 it.Advance()) {
110 WriteParam(m, it.key());
111 WriteValue(m, &it.value(), recursion + 1);
112 }
113 break;
114 }
115 case base::Value::TYPE_LIST: {
116 const base::ListValue* list = static_cast<const base::ListValue*>(value);
117 WriteParam(m, static_cast<int>(list->GetSize()));
118 for (base::ListValue::const_iterator it = list->begin();
119 it != list->end(); ++it) {
120 WriteValue(m, *it, recursion + 1);
121 }
122 break;
123 }
124 }
125 }
126
127 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
128 // object.
ReadDictionaryValue(const Message * m,PickleIterator * iter,base::DictionaryValue * value,int recursion)129 bool ReadDictionaryValue(const Message* m, PickleIterator* iter,
130 base::DictionaryValue* value, int recursion) {
131 int size;
132 if (!ReadParam(m, iter, &size))
133 return false;
134
135 for (int i = 0; i < size; ++i) {
136 std::string key;
137 base::Value* subval;
138 if (!ReadParam(m, iter, &key) ||
139 !ReadValue(m, iter, &subval, recursion + 1))
140 return false;
141 value->SetWithoutPathExpansion(key, subval);
142 }
143
144 return true;
145 }
146
147 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
148 // object.
ReadListValue(const Message * m,PickleIterator * iter,base::ListValue * value,int recursion)149 bool ReadListValue(const Message* m, PickleIterator* iter,
150 base::ListValue* value, int recursion) {
151 int size;
152 if (!ReadParam(m, iter, &size))
153 return false;
154
155 for (int i = 0; i < size; ++i) {
156 base::Value* subval;
157 if (!ReadValue(m, iter, &subval, recursion + 1))
158 return false;
159 value->Set(i, subval);
160 }
161
162 return true;
163 }
164
ReadValue(const Message * m,PickleIterator * iter,base::Value ** value,int recursion)165 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
166 int recursion) {
167 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) {
177 case base::Value::TYPE_NULL:
178 *value = base::Value::CreateNullValue();
179 break;
180 case base::Value::TYPE_BOOLEAN: {
181 bool val;
182 if (!ReadParam(m, iter, &val))
183 return false;
184 *value = new base::FundamentalValue(val);
185 break;
186 }
187 case base::Value::TYPE_INTEGER: {
188 int val;
189 if (!ReadParam(m, iter, &val))
190 return false;
191 *value = new base::FundamentalValue(val);
192 break;
193 }
194 case base::Value::TYPE_DOUBLE: {
195 double val;
196 if (!ReadParam(m, iter, &val))
197 return false;
198 *value = new base::FundamentalValue(val);
199 break;
200 }
201 case base::Value::TYPE_STRING: {
202 std::string val;
203 if (!ReadParam(m, iter, &val))
204 return false;
205 *value = new base::StringValue(val);
206 break;
207 }
208 case base::Value::TYPE_BINARY: {
209 const char* data;
210 int length;
211 if (!m->ReadData(iter, &data, &length))
212 return false;
213 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length);
214 break;
215 }
216 case base::Value::TYPE_DICTIONARY: {
217 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue());
218 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
219 return false;
220 *value = val.release();
221 break;
222 }
223 case base::Value::TYPE_LIST: {
224 scoped_ptr<base::ListValue> val(new base::ListValue());
225 if (!ReadListValue(m, iter, val.get(), recursion))
226 return false;
227 *value = val.release();
228 break;
229 }
230 default:
231 return false;
232 }
233
234 return true;
235 }
236
237 } // namespace
238
239 // -----------------------------------------------------------------------------
240
LogData()241 LogData::LogData()
242 : routing_id(0),
243 type(0),
244 sent(0),
245 receive(0),
246 dispatch(0) {
247 }
248
~LogData()249 LogData::~LogData() {
250 }
251
Log(const param_type & p,std::string * l)252 void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
253 l->append(p ? "true" : "false");
254 }
255
Write(Message * m,const param_type & p)256 void ParamTraits<unsigned char>::Write(Message* m, const param_type& p) {
257 m->WriteBytes(&p, sizeof(param_type));
258 }
259
Read(const Message * m,PickleIterator * iter,param_type * r)260 bool 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
Log(const param_type & p,std::string * l)269 void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
270 l->append(base::UintToString(p));
271 }
272
Write(Message * m,const param_type & p)273 void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) {
274 m->WriteBytes(&p, sizeof(param_type));
275 }
276
Read(const Message * m,PickleIterator * iter,param_type * r)277 bool 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
Log(const param_type & p,std::string * l)286 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
287 l->append(base::UintToString(p));
288 }
289
Log(const param_type & p,std::string * l)290 void ParamTraits<int>::Log(const param_type& p, std::string* l) {
291 l->append(base::IntToString(p));
292 }
293
Log(const param_type & p,std::string * l)294 void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
295 l->append(base::UintToString(p));
296 }
297
Log(const param_type & p,std::string * l)298 void ParamTraits<long>::Log(const param_type& p, std::string* l) {
299 l->append(base::Int64ToString(static_cast<int64>(p)));
300 }
301
Log(const param_type & p,std::string * l)302 void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
303 l->append(base::Uint64ToString(static_cast<uint64>(p)));
304 }
305
Log(const param_type & p,std::string * l)306 void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
307 l->append(base::Int64ToString(static_cast<int64>(p)));
308 }
309
Log(const param_type & p,std::string * l)310 void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
311 l->append(base::Uint64ToString(p));
312 }
313
Log(const param_type & p,std::string * l)314 void ParamTraits<float>::Log(const param_type& p, std::string* l) {
315 l->append(base::StringPrintf("%e", p));
316 }
317
Write(Message * m,const param_type & p)318 void ParamTraits<double>::Write(Message* m, const param_type& p) {
319 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
320 }
321
Read(const Message * m,PickleIterator * iter,param_type * r)322 bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter,
323 param_type* r) {
324 const char *data;
325 if (!m->ReadBytes(iter, &data, sizeof(*r))) {
326 NOTREACHED();
327 return false;
328 }
329 memcpy(r, data, sizeof(param_type));
330 return true;
331 }
332
Log(const param_type & p,std::string * l)333 void ParamTraits<double>::Log(const param_type& p, std::string* l) {
334 l->append(base::StringPrintf("%e", p));
335 }
336
337
Log(const param_type & p,std::string * l)338 void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
339 l->append(p);
340 }
341
Log(const param_type & p,std::string * l)342 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
343 l->append(base::WideToUTF8(p));
344 }
345
346 #if !defined(WCHAR_T_IS_UTF16)
Log(const param_type & p,std::string * l)347 void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
348 l->append(base::UTF16ToUTF8(p));
349 }
350 #endif
351
Write(Message * m,const param_type & p)352 void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
353 if (p.empty()) {
354 m->WriteData(NULL, 0);
355 } else {
356 m->WriteData(&p.front(), static_cast<int>(p.size()));
357 }
358 }
359
Read(const Message * m,PickleIterator * iter,param_type * r)360 bool ParamTraits<std::vector<char> >::Read(const Message* m,
361 PickleIterator* iter,
362 param_type* r) {
363 const char *data;
364 int data_size = 0;
365 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
366 return false;
367 r->resize(data_size);
368 if (data_size)
369 memcpy(&r->front(), data, data_size);
370 return true;
371 }
372
Log(const param_type & p,std::string * l)373 void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
374 LogBytes(p, l);
375 }
376
Write(Message * m,const param_type & p)377 void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
378 const param_type& p) {
379 if (p.empty()) {
380 m->WriteData(NULL, 0);
381 } else {
382 m->WriteData(reinterpret_cast<const char*>(&p.front()),
383 static_cast<int>(p.size()));
384 }
385 }
386
Read(const Message * m,PickleIterator * iter,param_type * r)387 bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
388 PickleIterator* iter,
389 param_type* r) {
390 const char *data;
391 int data_size = 0;
392 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
393 return false;
394 r->resize(data_size);
395 if (data_size)
396 memcpy(&r->front(), data, data_size);
397 return true;
398 }
399
Log(const param_type & p,std::string * l)400 void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
401 std::string* l) {
402 LogBytes(p, l);
403 }
404
Write(Message * m,const param_type & p)405 void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
406 WriteParam(m, static_cast<int>(p.size()));
407 // Cast to bool below is required because libc++'s
408 // vector<bool>::const_reference is different from bool, and we want to avoid
409 // writing an extra specialization of ParamTraits for it.
410 for (size_t i = 0; i < p.size(); i++)
411 WriteParam(m, static_cast<bool>(p[i]));
412 }
413
Read(const Message * m,PickleIterator * iter,param_type * r)414 bool ParamTraits<std::vector<bool> >::Read(const Message* m,
415 PickleIterator* iter,
416 param_type* r) {
417 int size;
418 // ReadLength() checks for < 0 itself.
419 if (!m->ReadLength(iter, &size))
420 return false;
421 r->resize(size);
422 for (int i = 0; i < size; i++) {
423 bool value;
424 if (!ReadParam(m, iter, &value))
425 return false;
426 (*r)[i] = value;
427 }
428 return true;
429 }
430
Log(const param_type & p,std::string * l)431 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
432 for (size_t i = 0; i < p.size(); ++i) {
433 if (i != 0)
434 l->push_back(' ');
435 LogParam(static_cast<bool>(p[i]), l);
436 }
437 }
438
Write(Message * m,const param_type & p)439 void ParamTraits<base::DictionaryValue>::Write(Message* m,
440 const param_type& p) {
441 WriteValue(m, &p, 0);
442 }
443
Read(const Message * m,PickleIterator * iter,param_type * r)444 bool ParamTraits<base::DictionaryValue>::Read(
445 const Message* m, PickleIterator* iter, param_type* r) {
446 int type;
447 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
448 return false;
449
450 return ReadDictionaryValue(m, iter, r, 0);
451 }
452
Log(const param_type & p,std::string * l)453 void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
454 std::string* l) {
455 std::string json;
456 base::JSONWriter::Write(&p, &json);
457 l->append(json);
458 }
459
460 #if defined(OS_POSIX)
Write(Message * m,const param_type & p)461 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
462 const bool valid = p.fd >= 0;
463 WriteParam(m, valid);
464
465 if (!valid)
466 return;
467
468 if (p.auto_close) {
469 if (!m->WriteFile(base::ScopedFD(p.fd)))
470 NOTREACHED();
471 } else {
472 if (!m->WriteBorrowingFile(p.fd))
473 NOTREACHED();
474 }
475 }
476
Read(const Message * m,PickleIterator * iter,param_type * r)477 bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
478 PickleIterator* iter,
479 param_type* r) {
480 *r = base::FileDescriptor();
481
482 bool valid;
483 if (!ReadParam(m, iter, &valid))
484 return false;
485
486 // TODO(morrita): Seems like this should return false.
487 if (!valid)
488 return true;
489
490 base::ScopedFD fd;
491 if (!m->ReadFile(iter, &fd))
492 return false;
493
494 *r = base::FileDescriptor(fd.release(), true);
495 return true;
496 }
497
Log(const param_type & p,std::string * l)498 void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
499 std::string* l) {
500 if (p.auto_close) {
501 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
502 } else {
503 l->append(base::StringPrintf("FD(%d)", p.fd));
504 }
505 }
506 #endif // defined(OS_POSIX)
507
Write(Message * m,const param_type & p)508 void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) {
509 p.WriteToPickle(m);
510 }
511
Read(const Message * m,PickleIterator * iter,param_type * r)512 bool ParamTraits<base::FilePath>::Read(const Message* m,
513 PickleIterator* iter,
514 param_type* r) {
515 return r->ReadFromPickle(iter);
516 }
517
Log(const param_type & p,std::string * l)518 void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
519 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
520 }
521
Write(Message * m,const param_type & p)522 void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) {
523 WriteValue(m, &p, 0);
524 }
525
Read(const Message * m,PickleIterator * iter,param_type * r)526 bool ParamTraits<base::ListValue>::Read(
527 const Message* m, PickleIterator* iter, param_type* r) {
528 int type;
529 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
530 return false;
531
532 return ReadListValue(m, iter, r, 0);
533 }
534
Log(const param_type & p,std::string * l)535 void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
536 std::string json;
537 base::JSONWriter::Write(&p, &json);
538 l->append(json);
539 }
540
Write(Message * m,const param_type & p)541 void ParamTraits<base::NullableString16>::Write(Message* m,
542 const param_type& p) {
543 WriteParam(m, p.string());
544 WriteParam(m, p.is_null());
545 }
546
Read(const Message * m,PickleIterator * iter,param_type * r)547 bool ParamTraits<base::NullableString16>::Read(const Message* m,
548 PickleIterator* iter,
549 param_type* r) {
550 base::string16 string;
551 if (!ReadParam(m, iter, &string))
552 return false;
553 bool is_null;
554 if (!ReadParam(m, iter, &is_null))
555 return false;
556 *r = base::NullableString16(string, is_null);
557 return true;
558 }
559
Log(const param_type & p,std::string * l)560 void ParamTraits<base::NullableString16>::Log(const param_type& p,
561 std::string* l) {
562 l->append("(");
563 LogParam(p.string(), l);
564 l->append(", ");
565 LogParam(p.is_null(), l);
566 l->append(")");
567 }
568
Write(Message * m,const param_type & p)569 void ParamTraits<base::File::Info>::Write(Message* m,
570 const param_type& p) {
571 WriteParam(m, p.size);
572 WriteParam(m, p.is_directory);
573 WriteParam(m, p.last_modified.ToDoubleT());
574 WriteParam(m, p.last_accessed.ToDoubleT());
575 WriteParam(m, p.creation_time.ToDoubleT());
576 }
577
Read(const Message * m,PickleIterator * iter,param_type * p)578 bool ParamTraits<base::File::Info>::Read(const Message* m,
579 PickleIterator* iter,
580 param_type* p) {
581 double last_modified, last_accessed, creation_time;
582 if (!ReadParam(m, iter, &p->size) ||
583 !ReadParam(m, iter, &p->is_directory) ||
584 !ReadParam(m, iter, &last_modified) ||
585 !ReadParam(m, iter, &last_accessed) ||
586 !ReadParam(m, iter, &creation_time))
587 return false;
588 p->last_modified = base::Time::FromDoubleT(last_modified);
589 p->last_accessed = base::Time::FromDoubleT(last_accessed);
590 p->creation_time = base::Time::FromDoubleT(creation_time);
591 return true;
592 }
593
Log(const param_type & p,std::string * l)594 void ParamTraits<base::File::Info>::Log(const param_type& p,
595 std::string* l) {
596 l->append("(");
597 LogParam(p.size, l);
598 l->append(",");
599 LogParam(p.is_directory, l);
600 l->append(",");
601 LogParam(p.last_modified.ToDoubleT(), l);
602 l->append(",");
603 LogParam(p.last_accessed.ToDoubleT(), l);
604 l->append(",");
605 LogParam(p.creation_time.ToDoubleT(), l);
606 l->append(")");
607 }
608
Write(Message * m,const param_type & p)609 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
610 ParamTraits<int64>::Write(m, p.ToInternalValue());
611 }
612
Read(const Message * m,PickleIterator * iter,param_type * r)613 bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
614 param_type* r) {
615 int64 value;
616 if (!ParamTraits<int64>::Read(m, iter, &value))
617 return false;
618 *r = base::Time::FromInternalValue(value);
619 return true;
620 }
621
Log(const param_type & p,std::string * l)622 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
623 ParamTraits<int64>::Log(p.ToInternalValue(), l);
624 }
625
Write(Message * m,const param_type & p)626 void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
627 ParamTraits<int64>::Write(m, p.ToInternalValue());
628 }
629
Read(const Message * m,PickleIterator * iter,param_type * r)630 bool ParamTraits<base::TimeDelta>::Read(const Message* m,
631 PickleIterator* iter,
632 param_type* r) {
633 int64 value;
634 bool ret = ParamTraits<int64>::Read(m, iter, &value);
635 if (ret)
636 *r = base::TimeDelta::FromInternalValue(value);
637
638 return ret;
639 }
640
Log(const param_type & p,std::string * l)641 void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
642 ParamTraits<int64>::Log(p.ToInternalValue(), l);
643 }
644
Write(Message * m,const param_type & p)645 void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
646 ParamTraits<int64>::Write(m, p.ToInternalValue());
647 }
648
Read(const Message * m,PickleIterator * iter,param_type * r)649 bool ParamTraits<base::TimeTicks>::Read(const Message* m,
650 PickleIterator* iter,
651 param_type* r) {
652 int64 value;
653 bool ret = ParamTraits<int64>::Read(m, iter, &value);
654 if (ret)
655 *r = base::TimeTicks::FromInternalValue(value);
656
657 return ret;
658 }
659
Log(const param_type & p,std::string * l)660 void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
661 ParamTraits<int64>::Log(p.ToInternalValue(), l);
662 }
663
Write(Message * m,const param_type & p)664 void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
665 #if defined(OS_WIN)
666 // On Windows marshalling pipe handle is not supported.
667 DCHECK(p.pipe.handle == NULL);
668 #endif // defined (OS_WIN)
669 WriteParam(m, p.name);
670 #if defined(OS_POSIX)
671 WriteParam(m, p.socket);
672 #endif
673 }
674
Read(const Message * m,PickleIterator * iter,param_type * r)675 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
676 PickleIterator* iter,
677 param_type* r) {
678 return ReadParam(m, iter, &r->name)
679 #if defined(OS_POSIX)
680 && ReadParam(m, iter, &r->socket)
681 #endif
682 ;
683 }
684
Log(const param_type & p,std::string * l)685 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
686 std::string* l) {
687 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
688 #if defined(OS_POSIX)
689 l->append(", ");
690 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
691 #endif
692 l->append(")");
693 }
694
Write(Message * m,const param_type & p)695 void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
696 WriteParam(m, p.channel);
697 WriteParam(m, p.routing_id);
698 WriteParam(m, p.type);
699 WriteParam(m, p.flags);
700 WriteParam(m, p.sent);
701 WriteParam(m, p.receive);
702 WriteParam(m, p.dispatch);
703 WriteParam(m, p.message_name);
704 WriteParam(m, p.params);
705 }
706
Read(const Message * m,PickleIterator * iter,param_type * r)707 bool ParamTraits<LogData>::Read(const Message* m,
708 PickleIterator* iter,
709 param_type* r) {
710 return
711 ReadParam(m, iter, &r->channel) &&
712 ReadParam(m, iter, &r->routing_id) &&
713 ReadParam(m, iter, &r->type) &&
714 ReadParam(m, iter, &r->flags) &&
715 ReadParam(m, iter, &r->sent) &&
716 ReadParam(m, iter, &r->receive) &&
717 ReadParam(m, iter, &r->dispatch) &&
718 ReadParam(m, iter, &r->message_name) &&
719 ReadParam(m, iter, &r->params);
720 }
721
Log(const param_type & p,std::string * l)722 void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
723 // Doesn't make sense to implement this!
724 }
725
Write(Message * m,const Message & p)726 void ParamTraits<Message>::Write(Message* m, const Message& p) {
727 #if defined(OS_POSIX)
728 // We don't serialize the file descriptors in the nested message, so there
729 // better not be any.
730 DCHECK(!p.HasFileDescriptors());
731 #endif
732
733 // Don't just write out the message. This is used to send messages between
734 // NaCl (Posix environment) and the browser (could be on Windows). The message
735 // header formats differ between these systems (so does handle sharing, but
736 // we already asserted we don't have any handles). So just write out the
737 // parts of the header we use.
738 //
739 // Be careful also to use only explicitly-sized types. The NaCl environment
740 // could be 64-bit and the host browser could be 32-bits. The nested message
741 // may or may not be safe to send between 32-bit and 64-bit systems, but we
742 // leave that up to the code sending the message to ensure.
743 m->WriteUInt32(static_cast<uint32>(p.routing_id()));
744 m->WriteUInt32(p.type());
745 m->WriteUInt32(p.flags());
746 m->WriteData(p.payload(), static_cast<uint32>(p.payload_size()));
747 }
748
Read(const Message * m,PickleIterator * iter,Message * r)749 bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
750 Message* r) {
751 uint32 routing_id, type, flags;
752 if (!m->ReadUInt32(iter, &routing_id) ||
753 !m->ReadUInt32(iter, &type) ||
754 !m->ReadUInt32(iter, &flags))
755 return false;
756
757 int payload_size;
758 const char* payload;
759 if (!m->ReadData(iter, &payload, &payload_size))
760 return false;
761
762 r->SetHeaderValues(static_cast<int32>(routing_id), type, flags);
763 return r->WriteBytes(payload, payload_size);
764 }
765
Log(const Message & p,std::string * l)766 void ParamTraits<Message>::Log(const Message& p, std::string* l) {
767 l->append("<IPC::Message>");
768 }
769
770 #if defined(OS_WIN)
771 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
772 // bit systems. That's why we use the Windows macros to convert to 32 bits.
Write(Message * m,const param_type & p)773 void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
774 m->WriteInt(HandleToLong(p));
775 }
776
Read(const Message * m,PickleIterator * iter,param_type * r)777 bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
778 param_type* r) {
779 int32 temp;
780 if (!m->ReadInt(iter, &temp))
781 return false;
782 *r = LongToHandle(temp);
783 return true;
784 }
785
Log(const param_type & p,std::string * l)786 void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
787 l->append(base::StringPrintf("0x%X", p));
788 }
789
Write(Message * m,const param_type & p)790 void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
791 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
792 }
793
Read(const Message * m,PickleIterator * iter,param_type * r)794 bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
795 param_type* r) {
796 const char *data;
797 int data_size = 0;
798 if (m->ReadData(iter, &data, &data_size) && data_size == sizeof(LOGFONT)) {
799 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
800 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
801 memcpy(r, data, sizeof(LOGFONT));
802 return true;
803 }
804 }
805
806 NOTREACHED();
807 return false;
808 }
809
Log(const param_type & p,std::string * l)810 void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
811 l->append(base::StringPrintf("<LOGFONT>"));
812 }
813
Write(Message * m,const param_type & p)814 void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
815 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
816 }
817
Read(const Message * m,PickleIterator * iter,param_type * r)818 bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
819 param_type* r) {
820 const char *data;
821 int data_size = 0;
822 bool result = m->ReadData(iter, &data, &data_size);
823 if (result && data_size == sizeof(MSG)) {
824 memcpy(r, data, sizeof(MSG));
825 } else {
826 result = false;
827 NOTREACHED();
828 }
829
830 return result;
831 }
832
Log(const param_type & p,std::string * l)833 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
834 l->append("<MSG>");
835 }
836
837 #endif // OS_WIN
838
839 } // namespace IPC
840