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.
5 #include "ipc/ipc_message_utils.h"
7 #include "base/files/file_path.h"
8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/nullable_string16.h"
11 #include "base/string_number_conversions.h"
12 #include "base/time.h"
13 #include "base/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "ipc/ipc_channel_handle.h"
18 #include "ipc/file_descriptor_set_posix.h"
27 const int kMaxRecursionDepth
= 100;
29 template<typename CharType
>
30 void LogBytes(const std::vector
<CharType
>& data
, std::string
* out
) {
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
]);
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
) {
40 out
->push_back(data
[i
]);
43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data
[i
])));
45 if (data
.size() > kMaxBytesToLog
) {
46 out
->append(base::StringPrintf(
48 static_cast<unsigned>(data
.size() - kMaxBytesToLog
)));
53 bool ReadValue(const Message
* m
, PickleIterator
* iter
, Value
** value
,
56 void WriteValue(Message
* m
, const Value
* value
, int recursion
) {
58 if (recursion
> kMaxRecursionDepth
) {
59 LOG(WARNING
) << "Max recursion depth hit in WriteValue.";
63 m
->WriteInt(value
->GetType());
65 switch (value
->GetType()) {
66 case Value::TYPE_NULL
:
68 case Value::TYPE_BOOLEAN
: {
70 result
= value
->GetAsBoolean(&val
);
75 case Value::TYPE_INTEGER
: {
77 result
= value
->GetAsInteger(&val
);
82 case Value::TYPE_DOUBLE
: {
84 result
= value
->GetAsDouble(&val
);
89 case Value::TYPE_STRING
: {
91 result
= value
->GetAsString(&val
);
96 case 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()));
102 case Value::TYPE_DICTIONARY
: {
103 const DictionaryValue
* dict
= static_cast<const DictionaryValue
*>(value
);
105 WriteParam(m
, static_cast<int>(dict
->size()));
107 for (DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd(); it
.Advance()) {
108 WriteParam(m
, it
.key());
109 WriteValue(m
, &it
.value(), recursion
+ 1);
113 case Value::TYPE_LIST
: {
114 const ListValue
* list
= static_cast<const ListValue
*>(value
);
115 WriteParam(m
, static_cast<int>(list
->GetSize()));
116 for (ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
118 WriteValue(m
, *it
, recursion
+ 1);
125 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
127 bool ReadDictionaryValue(const Message
* m
, PickleIterator
* iter
,
128 DictionaryValue
* value
, int recursion
) {
130 if (!ReadParam(m
, iter
, &size
))
133 for (int i
= 0; i
< size
; ++i
) {
136 if (!ReadParam(m
, iter
, &key
) ||
137 !ReadValue(m
, iter
, &subval
, recursion
+ 1))
139 value
->SetWithoutPathExpansion(key
, subval
);
145 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
147 bool ReadListValue(const Message
* m
, PickleIterator
* iter
,
148 ListValue
* value
, int recursion
) {
150 if (!ReadParam(m
, iter
, &size
))
153 for (int i
= 0; i
< size
; ++i
) {
155 if (!ReadValue(m
, iter
, &subval
, recursion
+ 1))
157 value
->Set(i
, subval
);
163 bool ReadValue(const Message
* m
, PickleIterator
* iter
, Value
** value
,
165 if (recursion
> kMaxRecursionDepth
) {
166 LOG(WARNING
) << "Max recursion depth hit in ReadValue.";
171 if (!ReadParam(m
, iter
, &type
))
175 case Value::TYPE_NULL
:
176 *value
= Value::CreateNullValue();
178 case Value::TYPE_BOOLEAN
: {
180 if (!ReadParam(m
, iter
, &val
))
182 *value
= new base::FundamentalValue(val
);
185 case Value::TYPE_INTEGER
: {
187 if (!ReadParam(m
, iter
, &val
))
189 *value
= new base::FundamentalValue(val
);
192 case Value::TYPE_DOUBLE
: {
194 if (!ReadParam(m
, iter
, &val
))
196 *value
= new base::FundamentalValue(val
);
199 case Value::TYPE_STRING
: {
201 if (!ReadParam(m
, iter
, &val
))
203 *value
= new base::StringValue(val
);
206 case Value::TYPE_BINARY
: {
209 if (!m
->ReadData(iter
, &data
, &length
))
211 *value
= base::BinaryValue::CreateWithCopiedBuffer(data
, length
);
214 case Value::TYPE_DICTIONARY
: {
215 scoped_ptr
<DictionaryValue
> val(new DictionaryValue());
216 if (!ReadDictionaryValue(m
, iter
, val
.get(), recursion
))
218 *value
= val
.release();
221 case Value::TYPE_LIST
: {
222 scoped_ptr
<ListValue
> val(new ListValue());
223 if (!ReadListValue(m
, iter
, val
.get(), recursion
))
225 *value
= val
.release();
237 // -----------------------------------------------------------------------------
247 LogData::~LogData() {
250 void ParamTraits
<bool>::Log(const param_type
& p
, std::string
* l
) {
251 l
->append(p
? "true" : "false");
254 void ParamTraits
<int>::Log(const param_type
& p
, std::string
* l
) {
255 l
->append(base::IntToString(p
));
258 void ParamTraits
<unsigned int>::Log(const param_type
& p
, std::string
* l
) {
259 l
->append(base::UintToString(p
));
262 void ParamTraits
<long>::Log(const param_type
& p
, std::string
* l
) {
263 l
->append(base::Int64ToString(static_cast<int64
>(p
)));
266 void ParamTraits
<unsigned long>::Log(const param_type
& p
, std::string
* l
) {
267 l
->append(base::Uint64ToString(static_cast<uint64
>(p
)));
270 void ParamTraits
<long long>::Log(const param_type
& p
, std::string
* l
) {
271 l
->append(base::Int64ToString(static_cast<int64
>(p
)));
274 void ParamTraits
<unsigned long long>::Log(const param_type
& p
, std::string
* l
) {
275 l
->append(base::Uint64ToString(p
));
278 void ParamTraits
<unsigned short>::Write(Message
* m
, const param_type
& p
) {
279 m
->WriteBytes(&p
, sizeof(param_type
));
282 bool ParamTraits
<unsigned short>::Read(const Message
* m
, PickleIterator
* iter
,
285 if (!m
->ReadBytes(iter
, &data
, sizeof(param_type
)))
287 memcpy(r
, data
, sizeof(param_type
));
291 void ParamTraits
<unsigned short>::Log(const param_type
& p
, std::string
* l
) {
292 l
->append(base::UintToString(p
));
295 void ParamTraits
<float>::Write(Message
* m
, const param_type
& p
) {
296 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(param_type
));
299 bool ParamTraits
<float>::Read(const Message
* m
, PickleIterator
* iter
,
303 if (!m
->ReadData(iter
, &data
, &data_size
) ||
304 data_size
!= sizeof(param_type
)) {
308 memcpy(r
, data
, sizeof(param_type
));
312 void ParamTraits
<float>::Log(const param_type
& p
, std::string
* l
) {
313 l
->append(base::StringPrintf("%e", p
));
316 void ParamTraits
<double>::Write(Message
* m
, const param_type
& p
) {
317 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(param_type
));
320 bool ParamTraits
<double>::Read(const Message
* m
, PickleIterator
* iter
,
324 if (!m
->ReadData(iter
, &data
, &data_size
) ||
325 data_size
!= sizeof(param_type
)) {
329 memcpy(r
, data
, sizeof(param_type
));
333 void ParamTraits
<double>::Log(const param_type
& p
, std::string
* l
) {
334 l
->append(base::StringPrintf("%e", p
));
338 void ParamTraits
<std::string
>::Log(const param_type
& p
, std::string
* l
) {
342 void ParamTraits
<std::wstring
>::Log(const param_type
& p
, std::string
* l
) {
343 l
->append(WideToUTF8(p
));
346 #if !defined(WCHAR_T_IS_UTF16)
347 void ParamTraits
<string16
>::Log(const param_type
& p
, std::string
* l
) {
348 l
->append(UTF16ToUTF8(p
));
352 void ParamTraits
<std::vector
<char> >::Write(Message
* m
, const param_type
& p
) {
354 m
->WriteData(NULL
, 0);
356 m
->WriteData(&p
.front(), static_cast<int>(p
.size()));
360 bool ParamTraits
<std::vector
<char> >::Read(const Message
* m
,
361 PickleIterator
* iter
,
365 if (!m
->ReadData(iter
, &data
, &data_size
) || data_size
< 0)
367 r
->resize(data_size
);
369 memcpy(&r
->front(), data
, data_size
);
373 void ParamTraits
<std::vector
<char> >::Log(const param_type
& p
, std::string
* l
) {
377 void ParamTraits
<std::vector
<unsigned char> >::Write(Message
* m
,
378 const param_type
& p
) {
380 m
->WriteData(NULL
, 0);
382 m
->WriteData(reinterpret_cast<const char*>(&p
.front()),
383 static_cast<int>(p
.size()));
387 bool ParamTraits
<std::vector
<unsigned char> >::Read(const Message
* m
,
388 PickleIterator
* iter
,
392 if (!m
->ReadData(iter
, &data
, &data_size
) || data_size
< 0)
394 r
->resize(data_size
);
396 memcpy(&r
->front(), data
, data_size
);
400 void ParamTraits
<std::vector
<unsigned char> >::Log(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
]));
414 bool ParamTraits
<std::vector
<bool> >::Read(const Message
* m
,
415 PickleIterator
* iter
,
418 // ReadLength() checks for < 0 itself.
419 if (!m
->ReadLength(iter
, &size
))
422 for (int i
= 0; i
< size
; i
++) {
424 if (!ReadParam(m
, iter
, &value
))
431 void ParamTraits
<std::vector
<bool> >::Log(const param_type
& p
, std::string
* l
) {
432 for (size_t i
= 0; i
< p
.size(); ++i
) {
435 LogParam(static_cast<bool>(p
[i
]), l
);
439 void ParamTraits
<DictionaryValue
>::Write(Message
* m
, const param_type
& p
) {
440 WriteValue(m
, &p
, 0);
443 bool ParamTraits
<DictionaryValue
>::Read(
444 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
446 if (!ReadParam(m
, iter
, &type
) || type
!= Value::TYPE_DICTIONARY
)
449 return ReadDictionaryValue(m
, iter
, r
, 0);
452 void ParamTraits
<DictionaryValue
>::Log(const param_type
& p
, std::string
* l
) {
454 base::JSONWriter::Write(&p
, &json
);
458 #if defined(OS_POSIX)
459 void ParamTraits
<base::FileDescriptor
>::Write(Message
* m
, const param_type
& p
) {
460 const bool valid
= p
.fd
>= 0;
461 WriteParam(m
, valid
);
464 if (!m
->WriteFileDescriptor(p
))
469 bool ParamTraits
<base::FileDescriptor
>::Read(const Message
* m
,
470 PickleIterator
* iter
,
473 if (!ReadParam(m
, iter
, &valid
))
478 r
->auto_close
= false;
482 return m
->ReadFileDescriptor(iter
, r
);
485 void ParamTraits
<base::FileDescriptor
>::Log(const param_type
& p
,
488 l
->append(base::StringPrintf("FD(%d auto-close)", p
.fd
));
490 l
->append(base::StringPrintf("FD(%d)", p
.fd
));
493 #endif // defined(OS_POSIX)
495 void ParamTraits
<base::FilePath
>::Write(Message
* m
, const param_type
& p
) {
499 bool ParamTraits
<base::FilePath
>::Read(const Message
* m
,
500 PickleIterator
* iter
,
502 return r
->ReadFromPickle(iter
);
505 void ParamTraits
<base::FilePath
>::Log(const param_type
& p
, std::string
* l
) {
506 ParamTraits
<base::FilePath::StringType
>::Log(p
.value(), l
);
509 void ParamTraits
<ListValue
>::Write(Message
* m
, const param_type
& p
) {
510 WriteValue(m
, &p
, 0);
513 bool ParamTraits
<ListValue
>::Read(
514 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
516 if (!ReadParam(m
, iter
, &type
) || type
!= Value::TYPE_LIST
)
519 return ReadListValue(m
, iter
, r
, 0);
522 void ParamTraits
<ListValue
>::Log(const param_type
& p
, std::string
* l
) {
524 base::JSONWriter::Write(&p
, &json
);
528 void ParamTraits
<NullableString16
>::Write(Message
* m
, const param_type
& p
) {
529 WriteParam(m
, p
.string());
530 WriteParam(m
, p
.is_null());
533 bool ParamTraits
<NullableString16
>::Read(const Message
* m
, PickleIterator
* iter
,
536 if (!ReadParam(m
, iter
, &string
))
539 if (!ReadParam(m
, iter
, &is_null
))
541 *r
= NullableString16(string
, is_null
);
545 void ParamTraits
<NullableString16
>::Log(const param_type
& p
, std::string
* l
) {
547 LogParam(p
.string(), l
);
549 LogParam(p
.is_null(), l
);
553 void ParamTraits
<base::PlatformFileInfo
>::Write(Message
* m
,
554 const param_type
& p
) {
555 WriteParam(m
, p
.size
);
556 WriteParam(m
, p
.is_directory
);
557 WriteParam(m
, p
.last_modified
.ToDoubleT());
558 WriteParam(m
, p
.last_accessed
.ToDoubleT());
559 WriteParam(m
, p
.creation_time
.ToDoubleT());
562 bool ParamTraits
<base::PlatformFileInfo
>::Read(const Message
* m
,
563 PickleIterator
* iter
,
565 double last_modified
;
566 double last_accessed
;
567 double creation_time
;
569 ReadParam(m
, iter
, &p
->size
) &&
570 ReadParam(m
, iter
, &p
->is_directory
) &&
571 ReadParam(m
, iter
, &last_modified
) &&
572 ReadParam(m
, iter
, &last_accessed
) &&
573 ReadParam(m
, iter
, &creation_time
);
575 p
->last_modified
= base::Time::FromDoubleT(last_modified
);
576 p
->last_accessed
= base::Time::FromDoubleT(last_accessed
);
577 p
->creation_time
= base::Time::FromDoubleT(creation_time
);
582 void ParamTraits
<base::PlatformFileInfo
>::Log(const param_type
& p
,
587 LogParam(p
.is_directory
, l
);
589 LogParam(p
.last_modified
.ToDoubleT(), l
);
591 LogParam(p
.last_accessed
.ToDoubleT(), l
);
593 LogParam(p
.creation_time
.ToDoubleT(), l
);
597 void ParamTraits
<base::Time
>::Write(Message
* m
, const param_type
& p
) {
598 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
601 bool ParamTraits
<base::Time
>::Read(const Message
* m
, PickleIterator
* iter
,
604 if (!ParamTraits
<int64
>::Read(m
, iter
, &value
))
606 *r
= base::Time::FromInternalValue(value
);
610 void ParamTraits
<base::Time
>::Log(const param_type
& p
, std::string
* l
) {
611 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
614 void ParamTraits
<base::TimeDelta
>::Write(Message
* m
, const param_type
& p
) {
615 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
618 bool ParamTraits
<base::TimeDelta
>::Read(const Message
* m
,
619 PickleIterator
* iter
,
622 bool ret
= ParamTraits
<int64
>::Read(m
, iter
, &value
);
624 *r
= base::TimeDelta::FromInternalValue(value
);
629 void ParamTraits
<base::TimeDelta
>::Log(const param_type
& p
, std::string
* l
) {
630 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
633 void ParamTraits
<base::TimeTicks
>::Write(Message
* m
, const param_type
& p
) {
634 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
637 bool ParamTraits
<base::TimeTicks
>::Read(const Message
* m
,
638 PickleIterator
* iter
,
641 bool ret
= ParamTraits
<int64
>::Read(m
, iter
, &value
);
643 *r
= base::TimeTicks::FromInternalValue(value
);
648 void ParamTraits
<base::TimeTicks
>::Log(const param_type
& p
, std::string
* l
) {
649 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
652 void ParamTraits
<IPC::ChannelHandle
>::Write(Message
* m
, const param_type
& p
) {
654 // On Windows marshalling pipe handle is not supported.
655 DCHECK(p
.pipe
.handle
== NULL
);
656 #endif // defined (OS_WIN)
657 WriteParam(m
, p
.name
);
658 #if defined(OS_POSIX)
659 WriteParam(m
, p
.socket
);
663 bool ParamTraits
<IPC::ChannelHandle
>::Read(const Message
* m
,
664 PickleIterator
* iter
,
666 return ReadParam(m
, iter
, &r
->name
)
667 #if defined(OS_POSIX)
668 && ReadParam(m
, iter
, &r
->socket
)
673 void ParamTraits
<IPC::ChannelHandle
>::Log(const param_type
& p
,
675 l
->append(base::StringPrintf("ChannelHandle(%s", p
.name
.c_str()));
676 #if defined(OS_POSIX)
678 ParamTraits
<base::FileDescriptor
>::Log(p
.socket
, l
);
683 void ParamTraits
<LogData
>::Write(Message
* m
, const param_type
& p
) {
684 WriteParam(m
, p
.channel
);
685 WriteParam(m
, p
.routing_id
);
686 WriteParam(m
, p
.type
);
687 WriteParam(m
, p
.flags
);
688 WriteParam(m
, p
.sent
);
689 WriteParam(m
, p
.receive
);
690 WriteParam(m
, p
.dispatch
);
691 WriteParam(m
, p
.message_name
);
692 WriteParam(m
, p
.params
);
695 bool ParamTraits
<LogData
>::Read(const Message
* m
,
696 PickleIterator
* iter
,
699 ReadParam(m
, iter
, &r
->channel
) &&
700 ReadParam(m
, iter
, &r
->routing_id
) &&
701 ReadParam(m
, iter
, &r
->type
) &&
702 ReadParam(m
, iter
, &r
->flags
) &&
703 ReadParam(m
, iter
, &r
->sent
) &&
704 ReadParam(m
, iter
, &r
->receive
) &&
705 ReadParam(m
, iter
, &r
->dispatch
) &&
706 ReadParam(m
, iter
, &r
->message_name
) &&
707 ReadParam(m
, iter
, &r
->params
);
710 void ParamTraits
<LogData
>::Log(const param_type
& p
, std::string
* l
) {
711 // Doesn't make sense to implement this!
714 void ParamTraits
<Message
>::Write(Message
* m
, const Message
& p
) {
715 #if defined(OS_POSIX)
716 // We don't serialize the file descriptors in the nested message, so there
717 // better not be any.
718 DCHECK(!p
.HasFileDescriptors());
721 // Don't just write out the message. This is used to send messages between
722 // NaCl (Posix environment) and the browser (could be on Windows). The message
723 // header formats differ between these systems (so does handle sharing, but
724 // we already asserted we don't have any handles). So just write out the
725 // parts of the header we use.
727 // Be careful also to use only explicitly-sized types. The NaCl environment
728 // could be 64-bit and the host browser could be 32-bits. The nested message
729 // may or may not be safe to send between 32-bit and 64-bit systems, but we
730 // leave that up to the code sending the message to ensure.
731 m
->WriteUInt32(static_cast<uint32
>(p
.routing_id()));
732 m
->WriteUInt32(p
.type());
733 m
->WriteUInt32(p
.flags());
734 m
->WriteData(p
.payload(), static_cast<uint32
>(p
.payload_size()));
737 bool ParamTraits
<Message
>::Read(const Message
* m
, PickleIterator
* iter
,
739 uint32 routing_id
, type
, flags
;
740 if (!m
->ReadUInt32(iter
, &routing_id
) ||
741 !m
->ReadUInt32(iter
, &type
) ||
742 !m
->ReadUInt32(iter
, &flags
))
747 if (!m
->ReadData(iter
, &payload
, &payload_size
))
750 r
->SetHeaderValues(static_cast<int32
>(routing_id
), type
, flags
);
751 return r
->WriteBytes(payload
, payload_size
);
754 void ParamTraits
<Message
>::Log(const Message
& p
, std::string
* l
) {
755 l
->append("<IPC::Message>");
759 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
760 // bit systems. That's why we use the Windows macros to convert to 32 bits.
761 void ParamTraits
<HANDLE
>::Write(Message
* m
, const param_type
& p
) {
762 m
->WriteInt(HandleToLong(p
));
765 bool ParamTraits
<HANDLE
>::Read(const Message
* m
, PickleIterator
* iter
,
768 if (!m
->ReadInt(iter
, &temp
))
770 *r
= LongToHandle(temp
);
774 void ParamTraits
<HANDLE
>::Log(const param_type
& p
, std::string
* l
) {
775 l
->append(base::StringPrintf("0x%X", p
));
778 void ParamTraits
<LOGFONT
>::Write(Message
* m
, const param_type
& p
) {
779 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(LOGFONT
));
782 bool ParamTraits
<LOGFONT
>::Read(const Message
* m
, PickleIterator
* iter
,
786 if (m
->ReadData(iter
, &data
, &data_size
) && data_size
== sizeof(LOGFONT
)) {
787 const LOGFONT
*font
= reinterpret_cast<LOGFONT
*>(const_cast<char*>(data
));
788 if (_tcsnlen(font
->lfFaceName
, LF_FACESIZE
) < LF_FACESIZE
) {
789 memcpy(r
, data
, sizeof(LOGFONT
));
798 void ParamTraits
<LOGFONT
>::Log(const param_type
& p
, std::string
* l
) {
799 l
->append(base::StringPrintf("<LOGFONT>"));
802 void ParamTraits
<MSG
>::Write(Message
* m
, const param_type
& p
) {
803 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(MSG
));
806 bool ParamTraits
<MSG
>::Read(const Message
* m
, PickleIterator
* iter
,
810 bool result
= m
->ReadData(iter
, &data
, &data_size
);
811 if (result
&& data_size
== sizeof(MSG
)) {
812 memcpy(r
, data
, sizeof(MSG
));
821 void ParamTraits
<MSG
>::Log(const param_type
& p
, std::string
* l
) {