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/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"
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
, base::Value
** value
,
56 void WriteValue(Message
* m
, const base::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 base::Value::TYPE_NULL
:
68 case base::Value::TYPE_BOOLEAN
: {
70 result
= value
->GetAsBoolean(&val
);
75 case base::Value::TYPE_INTEGER
: {
77 result
= value
->GetAsInteger(&val
);
82 case base::Value::TYPE_DOUBLE
: {
84 result
= value
->GetAsDouble(&val
);
89 case base::Value::TYPE_STRING
: {
91 result
= value
->GetAsString(&val
);
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()));
102 case base::Value::TYPE_DICTIONARY
: {
103 const base::DictionaryValue
* dict
=
104 static_cast<const base::DictionaryValue
*>(value
);
106 WriteParam(m
, static_cast<int>(dict
->size()));
108 for (base::DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd();
110 WriteParam(m
, it
.key());
111 WriteValue(m
, &it
.value(), recursion
+ 1);
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);
127 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
129 bool ReadDictionaryValue(const Message
* m
, PickleIterator
* iter
,
130 base::DictionaryValue
* value
, int recursion
) {
132 if (!ReadParam(m
, iter
, &size
))
135 for (int i
= 0; i
< size
; ++i
) {
138 if (!ReadParam(m
, iter
, &key
) ||
139 !ReadValue(m
, iter
, &subval
, recursion
+ 1))
141 value
->SetWithoutPathExpansion(key
, subval
);
147 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
149 bool ReadListValue(const Message
* m
, PickleIterator
* iter
,
150 base::ListValue
* value
, int recursion
) {
152 if (!ReadParam(m
, iter
, &size
))
155 for (int i
= 0; i
< size
; ++i
) {
157 if (!ReadValue(m
, iter
, &subval
, recursion
+ 1))
159 value
->Set(i
, subval
);
165 bool ReadValue(const Message
* m
, PickleIterator
* iter
, base::Value
** value
,
167 if (recursion
> kMaxRecursionDepth
) {
168 LOG(WARNING
) << "Max recursion depth hit in ReadValue.";
173 if (!ReadParam(m
, iter
, &type
))
177 case base::Value::TYPE_NULL
:
178 *value
= base::Value::CreateNullValue();
180 case base::Value::TYPE_BOOLEAN
: {
182 if (!ReadParam(m
, iter
, &val
))
184 *value
= new base::FundamentalValue(val
);
187 case base::Value::TYPE_INTEGER
: {
189 if (!ReadParam(m
, iter
, &val
))
191 *value
= new base::FundamentalValue(val
);
194 case base::Value::TYPE_DOUBLE
: {
196 if (!ReadParam(m
, iter
, &val
))
198 *value
= new base::FundamentalValue(val
);
201 case base::Value::TYPE_STRING
: {
203 if (!ReadParam(m
, iter
, &val
))
205 *value
= new base::StringValue(val
);
208 case base::Value::TYPE_BINARY
: {
211 if (!m
->ReadData(iter
, &data
, &length
))
213 *value
= base::BinaryValue::CreateWithCopiedBuffer(data
, length
);
216 case base::Value::TYPE_DICTIONARY
: {
217 scoped_ptr
<base::DictionaryValue
> val(new base::DictionaryValue());
218 if (!ReadDictionaryValue(m
, iter
, val
.get(), recursion
))
220 *value
= val
.release();
223 case base::Value::TYPE_LIST
: {
224 scoped_ptr
<base::ListValue
> val(new base::ListValue());
225 if (!ReadListValue(m
, iter
, val
.get(), recursion
))
227 *value
= val
.release();
239 // -----------------------------------------------------------------------------
249 LogData::~LogData() {
252 void ParamTraits
<bool>::Log(const param_type
& p
, std::string
* l
) {
253 l
->append(p
? "true" : "false");
256 void ParamTraits
<int>::Log(const param_type
& p
, std::string
* l
) {
257 l
->append(base::IntToString(p
));
260 void ParamTraits
<unsigned int>::Log(const param_type
& p
, std::string
* l
) {
261 l
->append(base::UintToString(p
));
264 void ParamTraits
<long>::Log(const param_type
& p
, std::string
* l
) {
265 l
->append(base::Int64ToString(static_cast<int64
>(p
)));
268 void ParamTraits
<unsigned long>::Log(const param_type
& p
, std::string
* l
) {
269 l
->append(base::Uint64ToString(static_cast<uint64
>(p
)));
272 void ParamTraits
<long long>::Log(const param_type
& p
, std::string
* l
) {
273 l
->append(base::Int64ToString(static_cast<int64
>(p
)));
276 void ParamTraits
<unsigned long long>::Log(const param_type
& p
, std::string
* l
) {
277 l
->append(base::Uint64ToString(p
));
280 void ParamTraits
<unsigned short>::Write(Message
* m
, const param_type
& p
) {
281 m
->WriteBytes(&p
, sizeof(param_type
));
284 bool ParamTraits
<unsigned short>::Read(const Message
* m
, PickleIterator
* iter
,
287 if (!m
->ReadBytes(iter
, &data
, sizeof(param_type
)))
289 memcpy(r
, data
, sizeof(param_type
));
293 void ParamTraits
<unsigned short>::Log(const param_type
& p
, std::string
* l
) {
294 l
->append(base::UintToString(p
));
297 void ParamTraits
<float>::Write(Message
* m
, const param_type
& p
) {
298 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(param_type
));
301 bool ParamTraits
<float>::Read(const Message
* m
, PickleIterator
* iter
,
305 if (!m
->ReadData(iter
, &data
, &data_size
) ||
306 data_size
!= sizeof(param_type
)) {
310 memcpy(r
, data
, sizeof(param_type
));
314 void ParamTraits
<float>::Log(const param_type
& p
, std::string
* l
) {
315 l
->append(base::StringPrintf("%e", p
));
318 void ParamTraits
<double>::Write(Message
* m
, const param_type
& p
) {
319 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(param_type
));
322 bool ParamTraits
<double>::Read(const Message
* m
, PickleIterator
* iter
,
326 if (!m
->ReadData(iter
, &data
, &data_size
) ||
327 data_size
!= sizeof(param_type
)) {
331 memcpy(r
, data
, sizeof(param_type
));
335 void ParamTraits
<double>::Log(const param_type
& p
, std::string
* l
) {
336 l
->append(base::StringPrintf("%e", p
));
340 void ParamTraits
<std::string
>::Log(const param_type
& p
, std::string
* l
) {
344 void ParamTraits
<std::wstring
>::Log(const param_type
& p
, std::string
* l
) {
345 l
->append(WideToUTF8(p
));
348 #if !defined(WCHAR_T_IS_UTF16)
349 void ParamTraits
<string16
>::Log(const param_type
& p
, std::string
* l
) {
350 l
->append(UTF16ToUTF8(p
));
354 void ParamTraits
<std::vector
<char> >::Write(Message
* m
, const param_type
& p
) {
356 m
->WriteData(NULL
, 0);
358 m
->WriteData(&p
.front(), static_cast<int>(p
.size()));
362 bool ParamTraits
<std::vector
<char> >::Read(const Message
* m
,
363 PickleIterator
* iter
,
367 if (!m
->ReadData(iter
, &data
, &data_size
) || data_size
< 0)
369 r
->resize(data_size
);
371 memcpy(&r
->front(), data
, data_size
);
375 void ParamTraits
<std::vector
<char> >::Log(const param_type
& p
, std::string
* l
) {
379 void ParamTraits
<std::vector
<unsigned char> >::Write(Message
* m
,
380 const param_type
& p
) {
382 m
->WriteData(NULL
, 0);
384 m
->WriteData(reinterpret_cast<const char*>(&p
.front()),
385 static_cast<int>(p
.size()));
389 bool ParamTraits
<std::vector
<unsigned char> >::Read(const Message
* m
,
390 PickleIterator
* iter
,
394 if (!m
->ReadData(iter
, &data
, &data_size
) || data_size
< 0)
396 r
->resize(data_size
);
398 memcpy(&r
->front(), data
, data_size
);
402 void ParamTraits
<std::vector
<unsigned char> >::Log(const param_type
& p
,
407 void ParamTraits
<std::vector
<bool> >::Write(Message
* m
, const param_type
& p
) {
408 WriteParam(m
, static_cast<int>(p
.size()));
409 // Cast to bool below is required because libc++'s
410 // vector<bool>::const_reference is different from bool, and we want to avoid
411 // writing an extra specialization of ParamTraits for it.
412 for (size_t i
= 0; i
< p
.size(); i
++)
413 WriteParam(m
, static_cast<bool>(p
[i
]));
416 bool ParamTraits
<std::vector
<bool> >::Read(const Message
* m
,
417 PickleIterator
* iter
,
420 // ReadLength() checks for < 0 itself.
421 if (!m
->ReadLength(iter
, &size
))
424 for (int i
= 0; i
< size
; i
++) {
426 if (!ReadParam(m
, iter
, &value
))
433 void ParamTraits
<std::vector
<bool> >::Log(const param_type
& p
, std::string
* l
) {
434 for (size_t i
= 0; i
< p
.size(); ++i
) {
437 LogParam(static_cast<bool>(p
[i
]), l
);
441 void ParamTraits
<base::DictionaryValue
>::Write(Message
* m
,
442 const param_type
& p
) {
443 WriteValue(m
, &p
, 0);
446 bool ParamTraits
<base::DictionaryValue
>::Read(
447 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
449 if (!ReadParam(m
, iter
, &type
) || type
!= base::Value::TYPE_DICTIONARY
)
452 return ReadDictionaryValue(m
, iter
, r
, 0);
455 void ParamTraits
<base::DictionaryValue
>::Log(const param_type
& p
,
458 base::JSONWriter::Write(&p
, &json
);
462 #if defined(OS_POSIX)
463 void ParamTraits
<base::FileDescriptor
>::Write(Message
* m
, const param_type
& p
) {
464 const bool valid
= p
.fd
>= 0;
465 WriteParam(m
, valid
);
468 if (!m
->WriteFileDescriptor(p
))
473 bool ParamTraits
<base::FileDescriptor
>::Read(const Message
* m
,
474 PickleIterator
* iter
,
477 if (!ReadParam(m
, iter
, &valid
))
482 r
->auto_close
= false;
486 return m
->ReadFileDescriptor(iter
, r
);
489 void ParamTraits
<base::FileDescriptor
>::Log(const param_type
& p
,
492 l
->append(base::StringPrintf("FD(%d auto-close)", p
.fd
));
494 l
->append(base::StringPrintf("FD(%d)", p
.fd
));
497 #endif // defined(OS_POSIX)
499 void ParamTraits
<base::FilePath
>::Write(Message
* m
, const param_type
& p
) {
503 bool ParamTraits
<base::FilePath
>::Read(const Message
* m
,
504 PickleIterator
* iter
,
506 return r
->ReadFromPickle(iter
);
509 void ParamTraits
<base::FilePath
>::Log(const param_type
& p
, std::string
* l
) {
510 ParamTraits
<base::FilePath::StringType
>::Log(p
.value(), l
);
513 void ParamTraits
<base::ListValue
>::Write(Message
* m
, const param_type
& p
) {
514 WriteValue(m
, &p
, 0);
517 bool ParamTraits
<base::ListValue
>::Read(
518 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
520 if (!ReadParam(m
, iter
, &type
) || type
!= base::Value::TYPE_LIST
)
523 return ReadListValue(m
, iter
, r
, 0);
526 void ParamTraits
<base::ListValue
>::Log(const param_type
& p
, std::string
* l
) {
528 base::JSONWriter::Write(&p
, &json
);
532 void ParamTraits
<base::NullableString16
>::Write(Message
* m
,
533 const param_type
& p
) {
534 WriteParam(m
, p
.string());
535 WriteParam(m
, p
.is_null());
538 bool ParamTraits
<base::NullableString16
>::Read(const Message
* m
,
539 PickleIterator
* iter
,
542 if (!ReadParam(m
, iter
, &string
))
545 if (!ReadParam(m
, iter
, &is_null
))
547 *r
= base::NullableString16(string
, is_null
);
551 void ParamTraits
<base::NullableString16
>::Log(const param_type
& p
,
554 LogParam(p
.string(), l
);
556 LogParam(p
.is_null(), l
);
560 void ParamTraits
<base::PlatformFileInfo
>::Write(Message
* m
,
561 const param_type
& p
) {
562 WriteParam(m
, p
.size
);
563 WriteParam(m
, p
.is_directory
);
564 WriteParam(m
, p
.last_modified
.ToDoubleT());
565 WriteParam(m
, p
.last_accessed
.ToDoubleT());
566 WriteParam(m
, p
.creation_time
.ToDoubleT());
569 bool ParamTraits
<base::PlatformFileInfo
>::Read(const Message
* m
,
570 PickleIterator
* iter
,
572 double last_modified
;
573 double last_accessed
;
574 double creation_time
;
576 ReadParam(m
, iter
, &p
->size
) &&
577 ReadParam(m
, iter
, &p
->is_directory
) &&
578 ReadParam(m
, iter
, &last_modified
) &&
579 ReadParam(m
, iter
, &last_accessed
) &&
580 ReadParam(m
, iter
, &creation_time
);
582 p
->last_modified
= base::Time::FromDoubleT(last_modified
);
583 p
->last_accessed
= base::Time::FromDoubleT(last_accessed
);
584 p
->creation_time
= base::Time::FromDoubleT(creation_time
);
589 void ParamTraits
<base::PlatformFileInfo
>::Log(const param_type
& p
,
594 LogParam(p
.is_directory
, l
);
596 LogParam(p
.last_modified
.ToDoubleT(), l
);
598 LogParam(p
.last_accessed
.ToDoubleT(), l
);
600 LogParam(p
.creation_time
.ToDoubleT(), l
);
604 void ParamTraits
<base::Time
>::Write(Message
* m
, const param_type
& p
) {
605 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
608 bool ParamTraits
<base::Time
>::Read(const Message
* m
, PickleIterator
* iter
,
611 if (!ParamTraits
<int64
>::Read(m
, iter
, &value
))
613 *r
= base::Time::FromInternalValue(value
);
617 void ParamTraits
<base::Time
>::Log(const param_type
& p
, std::string
* l
) {
618 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
621 void ParamTraits
<base::TimeDelta
>::Write(Message
* m
, const param_type
& p
) {
622 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
625 bool ParamTraits
<base::TimeDelta
>::Read(const Message
* m
,
626 PickleIterator
* iter
,
629 bool ret
= ParamTraits
<int64
>::Read(m
, iter
, &value
);
631 *r
= base::TimeDelta::FromInternalValue(value
);
636 void ParamTraits
<base::TimeDelta
>::Log(const param_type
& p
, std::string
* l
) {
637 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
640 void ParamTraits
<base::TimeTicks
>::Write(Message
* m
, const param_type
& p
) {
641 ParamTraits
<int64
>::Write(m
, p
.ToInternalValue());
644 bool ParamTraits
<base::TimeTicks
>::Read(const Message
* m
,
645 PickleIterator
* iter
,
648 bool ret
= ParamTraits
<int64
>::Read(m
, iter
, &value
);
650 *r
= base::TimeTicks::FromInternalValue(value
);
655 void ParamTraits
<base::TimeTicks
>::Log(const param_type
& p
, std::string
* l
) {
656 ParamTraits
<int64
>::Log(p
.ToInternalValue(), l
);
659 void ParamTraits
<IPC::ChannelHandle
>::Write(Message
* m
, const param_type
& p
) {
661 // On Windows marshalling pipe handle is not supported.
662 DCHECK(p
.pipe
.handle
== NULL
);
663 #endif // defined (OS_WIN)
664 WriteParam(m
, p
.name
);
665 #if defined(OS_POSIX)
666 WriteParam(m
, p
.socket
);
670 bool ParamTraits
<IPC::ChannelHandle
>::Read(const Message
* m
,
671 PickleIterator
* iter
,
673 return ReadParam(m
, iter
, &r
->name
)
674 #if defined(OS_POSIX)
675 && ReadParam(m
, iter
, &r
->socket
)
680 void ParamTraits
<IPC::ChannelHandle
>::Log(const param_type
& p
,
682 l
->append(base::StringPrintf("ChannelHandle(%s", p
.name
.c_str()));
683 #if defined(OS_POSIX)
685 ParamTraits
<base::FileDescriptor
>::Log(p
.socket
, l
);
690 void ParamTraits
<LogData
>::Write(Message
* m
, const param_type
& p
) {
691 WriteParam(m
, p
.channel
);
692 WriteParam(m
, p
.routing_id
);
693 WriteParam(m
, p
.type
);
694 WriteParam(m
, p
.flags
);
695 WriteParam(m
, p
.sent
);
696 WriteParam(m
, p
.receive
);
697 WriteParam(m
, p
.dispatch
);
698 WriteParam(m
, p
.message_name
);
699 WriteParam(m
, p
.params
);
702 bool ParamTraits
<LogData
>::Read(const Message
* m
,
703 PickleIterator
* iter
,
706 ReadParam(m
, iter
, &r
->channel
) &&
707 ReadParam(m
, iter
, &r
->routing_id
) &&
708 ReadParam(m
, iter
, &r
->type
) &&
709 ReadParam(m
, iter
, &r
->flags
) &&
710 ReadParam(m
, iter
, &r
->sent
) &&
711 ReadParam(m
, iter
, &r
->receive
) &&
712 ReadParam(m
, iter
, &r
->dispatch
) &&
713 ReadParam(m
, iter
, &r
->message_name
) &&
714 ReadParam(m
, iter
, &r
->params
);
717 void ParamTraits
<LogData
>::Log(const param_type
& p
, std::string
* l
) {
718 // Doesn't make sense to implement this!
721 void ParamTraits
<Message
>::Write(Message
* m
, const Message
& p
) {
722 #if defined(OS_POSIX)
723 // We don't serialize the file descriptors in the nested message, so there
724 // better not be any.
725 DCHECK(!p
.HasFileDescriptors());
728 // Don't just write out the message. This is used to send messages between
729 // NaCl (Posix environment) and the browser (could be on Windows). The message
730 // header formats differ between these systems (so does handle sharing, but
731 // we already asserted we don't have any handles). So just write out the
732 // parts of the header we use.
734 // Be careful also to use only explicitly-sized types. The NaCl environment
735 // could be 64-bit and the host browser could be 32-bits. The nested message
736 // may or may not be safe to send between 32-bit and 64-bit systems, but we
737 // leave that up to the code sending the message to ensure.
738 m
->WriteUInt32(static_cast<uint32
>(p
.routing_id()));
739 m
->WriteUInt32(p
.type());
740 m
->WriteUInt32(p
.flags());
741 m
->WriteData(p
.payload(), static_cast<uint32
>(p
.payload_size()));
744 bool ParamTraits
<Message
>::Read(const Message
* m
, PickleIterator
* iter
,
746 uint32 routing_id
, type
, flags
;
747 if (!m
->ReadUInt32(iter
, &routing_id
) ||
748 !m
->ReadUInt32(iter
, &type
) ||
749 !m
->ReadUInt32(iter
, &flags
))
754 if (!m
->ReadData(iter
, &payload
, &payload_size
))
757 r
->SetHeaderValues(static_cast<int32
>(routing_id
), type
, flags
);
758 return r
->WriteBytes(payload
, payload_size
);
761 void ParamTraits
<Message
>::Log(const Message
& p
, std::string
* l
) {
762 l
->append("<IPC::Message>");
766 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
767 // bit systems. That's why we use the Windows macros to convert to 32 bits.
768 void ParamTraits
<HANDLE
>::Write(Message
* m
, const param_type
& p
) {
769 m
->WriteInt(HandleToLong(p
));
772 bool ParamTraits
<HANDLE
>::Read(const Message
* m
, PickleIterator
* iter
,
775 if (!m
->ReadInt(iter
, &temp
))
777 *r
= LongToHandle(temp
);
781 void ParamTraits
<HANDLE
>::Log(const param_type
& p
, std::string
* l
) {
782 l
->append(base::StringPrintf("0x%X", p
));
785 void ParamTraits
<LOGFONT
>::Write(Message
* m
, const param_type
& p
) {
786 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(LOGFONT
));
789 bool ParamTraits
<LOGFONT
>::Read(const Message
* m
, PickleIterator
* iter
,
793 if (m
->ReadData(iter
, &data
, &data_size
) && data_size
== sizeof(LOGFONT
)) {
794 const LOGFONT
*font
= reinterpret_cast<LOGFONT
*>(const_cast<char*>(data
));
795 if (_tcsnlen(font
->lfFaceName
, LF_FACESIZE
) < LF_FACESIZE
) {
796 memcpy(r
, data
, sizeof(LOGFONT
));
805 void ParamTraits
<LOGFONT
>::Log(const param_type
& p
, std::string
* l
) {
806 l
->append(base::StringPrintf("<LOGFONT>"));
809 void ParamTraits
<MSG
>::Write(Message
* m
, const param_type
& p
) {
810 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(MSG
));
813 bool ParamTraits
<MSG
>::Read(const Message
* m
, PickleIterator
* iter
,
817 bool result
= m
->ReadData(iter
, &data
, &data_size
);
818 if (result
&& data_size
== sizeof(MSG
)) {
819 memcpy(r
, data
, sizeof(MSG
));
828 void ParamTraits
<MSG
>::Log(const param_type
& p
, std::string
* l
) {