Update existing uses of /wd4267 to use the GN config
[chromium-blink-merge.git] / ipc / ipc_message_utils.cc
blob7368a0db22101222f067a6ef5350c27fe6238caf
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"
16 #include "ipc/ipc_message_attachment.h"
17 #include "ipc/ipc_message_attachment_set.h"
19 #if defined(OS_POSIX)
20 #include "ipc/ipc_platform_file_attachment_posix.h"
21 #endif
23 #if defined(OS_WIN)
24 #include <tchar.h>
25 #endif
27 namespace IPC {
29 namespace {
31 const int kMaxRecursionDepth = 100;
33 template<typename CharType>
34 void LogBytes(const std::vector<CharType>& data, std::string* out) {
35 #if defined(OS_WIN)
36 // Windows has a GUI for logging, which can handle arbitrary binary data.
37 for (size_t i = 0; i < data.size(); ++i)
38 out->push_back(data[i]);
39 #else
40 // On POSIX, we log to stdout, which we assume can display ASCII.
41 static const size_t kMaxBytesToLog = 100;
42 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
43 if (isprint(data[i]))
44 out->push_back(data[i]);
45 else
46 out->append(
47 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
49 if (data.size() > kMaxBytesToLog) {
50 out->append(base::StringPrintf(
51 " and %u more bytes",
52 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
54 #endif
57 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
58 int recursion);
60 void WriteValue(Message* m, const base::Value* value, int recursion) {
61 bool result;
62 if (recursion > kMaxRecursionDepth) {
63 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
64 return;
67 m->WriteInt(value->GetType());
69 switch (value->GetType()) {
70 case base::Value::TYPE_NULL:
71 break;
72 case base::Value::TYPE_BOOLEAN: {
73 bool val;
74 result = value->GetAsBoolean(&val);
75 DCHECK(result);
76 WriteParam(m, val);
77 break;
79 case base::Value::TYPE_INTEGER: {
80 int val;
81 result = value->GetAsInteger(&val);
82 DCHECK(result);
83 WriteParam(m, val);
84 break;
86 case base::Value::TYPE_DOUBLE: {
87 double val;
88 result = value->GetAsDouble(&val);
89 DCHECK(result);
90 WriteParam(m, val);
91 break;
93 case base::Value::TYPE_STRING: {
94 std::string val;
95 result = value->GetAsString(&val);
96 DCHECK(result);
97 WriteParam(m, val);
98 break;
100 case base::Value::TYPE_BINARY: {
101 const base::BinaryValue* binary =
102 static_cast<const base::BinaryValue*>(value);
103 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
104 break;
106 case base::Value::TYPE_DICTIONARY: {
107 const base::DictionaryValue* dict =
108 static_cast<const base::DictionaryValue*>(value);
110 WriteParam(m, static_cast<int>(dict->size()));
112 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
113 it.Advance()) {
114 WriteParam(m, it.key());
115 WriteValue(m, &it.value(), recursion + 1);
117 break;
119 case base::Value::TYPE_LIST: {
120 const base::ListValue* list = static_cast<const base::ListValue*>(value);
121 WriteParam(m, static_cast<int>(list->GetSize()));
122 for (base::ListValue::const_iterator it = list->begin();
123 it != list->end(); ++it) {
124 WriteValue(m, *it, recursion + 1);
126 break;
131 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
132 // object.
133 bool ReadDictionaryValue(const Message* m, PickleIterator* iter,
134 base::DictionaryValue* value, int recursion) {
135 int size;
136 if (!ReadParam(m, iter, &size))
137 return false;
139 for (int i = 0; i < size; ++i) {
140 std::string key;
141 base::Value* subval;
142 if (!ReadParam(m, iter, &key) ||
143 !ReadValue(m, iter, &subval, recursion + 1))
144 return false;
145 value->SetWithoutPathExpansion(key, subval);
148 return true;
151 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
152 // object.
153 bool ReadListValue(const Message* m, PickleIterator* iter,
154 base::ListValue* value, int recursion) {
155 int size;
156 if (!ReadParam(m, iter, &size))
157 return false;
159 for (int i = 0; i < size; ++i) {
160 base::Value* subval;
161 if (!ReadValue(m, iter, &subval, recursion + 1))
162 return false;
163 value->Set(i, subval);
166 return true;
169 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
170 int recursion) {
171 if (recursion > kMaxRecursionDepth) {
172 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
173 return false;
176 int type;
177 if (!ReadParam(m, iter, &type))
178 return false;
180 switch (type) {
181 case base::Value::TYPE_NULL:
182 *value = base::Value::CreateNullValue();
183 break;
184 case base::Value::TYPE_BOOLEAN: {
185 bool val;
186 if (!ReadParam(m, iter, &val))
187 return false;
188 *value = new base::FundamentalValue(val);
189 break;
191 case base::Value::TYPE_INTEGER: {
192 int val;
193 if (!ReadParam(m, iter, &val))
194 return false;
195 *value = new base::FundamentalValue(val);
196 break;
198 case base::Value::TYPE_DOUBLE: {
199 double val;
200 if (!ReadParam(m, iter, &val))
201 return false;
202 *value = new base::FundamentalValue(val);
203 break;
205 case base::Value::TYPE_STRING: {
206 std::string val;
207 if (!ReadParam(m, iter, &val))
208 return false;
209 *value = new base::StringValue(val);
210 break;
212 case base::Value::TYPE_BINARY: {
213 const char* data;
214 int length;
215 if (!iter->ReadData(&data, &length))
216 return false;
217 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length);
218 break;
220 case base::Value::TYPE_DICTIONARY: {
221 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue());
222 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
223 return false;
224 *value = val.release();
225 break;
227 case base::Value::TYPE_LIST: {
228 scoped_ptr<base::ListValue> val(new base::ListValue());
229 if (!ReadListValue(m, iter, val.get(), recursion))
230 return false;
231 *value = val.release();
232 break;
234 default:
235 return false;
238 return true;
241 } // namespace
243 // -----------------------------------------------------------------------------
245 LogData::LogData()
246 : routing_id(0),
247 type(0),
248 sent(0),
249 receive(0),
250 dispatch(0) {
253 LogData::~LogData() {
256 void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
257 l->append(p ? "true" : "false");
260 void ParamTraits<unsigned char>::Write(Message* m, const param_type& p) {
261 m->WriteBytes(&p, sizeof(param_type));
264 bool ParamTraits<unsigned char>::Read(const Message* m, PickleIterator* iter,
265 param_type* r) {
266 const char* data;
267 if (!iter->ReadBytes(&data, sizeof(param_type)))
268 return false;
269 memcpy(r, data, sizeof(param_type));
270 return true;
273 void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
274 l->append(base::UintToString(p));
277 void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) {
278 m->WriteBytes(&p, sizeof(param_type));
281 bool ParamTraits<unsigned short>::Read(const Message* m, PickleIterator* iter,
282 param_type* r) {
283 const char* data;
284 if (!iter->ReadBytes(&data, sizeof(param_type)))
285 return false;
286 memcpy(r, data, sizeof(param_type));
287 return true;
290 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
291 l->append(base::UintToString(p));
294 void ParamTraits<int>::Log(const param_type& p, std::string* l) {
295 l->append(base::IntToString(p));
298 void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
299 l->append(base::UintToString(p));
302 void ParamTraits<long>::Log(const param_type& p, std::string* l) {
303 l->append(base::Int64ToString(static_cast<int64>(p)));
306 void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
307 l->append(base::Uint64ToString(static_cast<uint64>(p)));
310 void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
311 l->append(base::Int64ToString(static_cast<int64>(p)));
314 void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
315 l->append(base::Uint64ToString(p));
318 void ParamTraits<float>::Log(const param_type& p, std::string* l) {
319 l->append(base::StringPrintf("%e", p));
322 void ParamTraits<double>::Write(Message* m, const param_type& p) {
323 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
326 bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter,
327 param_type* r) {
328 const char *data;
329 if (!iter->ReadBytes(&data, sizeof(*r))) {
330 NOTREACHED();
331 return false;
333 memcpy(r, data, sizeof(param_type));
334 return true;
337 void ParamTraits<double>::Log(const param_type& p, std::string* l) {
338 l->append(base::StringPrintf("%e", p));
342 void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
343 l->append(p);
346 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
347 l->append(base::WideToUTF8(p));
350 #if !defined(WCHAR_T_IS_UTF16)
351 void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
352 l->append(base::UTF16ToUTF8(p));
354 #endif
356 void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
357 if (p.empty()) {
358 m->WriteData(NULL, 0);
359 } else {
360 m->WriteData(&p.front(), static_cast<int>(p.size()));
364 bool ParamTraits<std::vector<char> >::Read(const Message* m,
365 PickleIterator* iter,
366 param_type* r) {
367 const char *data;
368 int data_size = 0;
369 if (!iter->ReadData(&data, &data_size) || data_size < 0)
370 return false;
371 r->resize(data_size);
372 if (data_size)
373 memcpy(&r->front(), data, data_size);
374 return true;
377 void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
378 LogBytes(p, l);
381 void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
382 const param_type& p) {
383 if (p.empty()) {
384 m->WriteData(NULL, 0);
385 } else {
386 m->WriteData(reinterpret_cast<const char*>(&p.front()),
387 static_cast<int>(p.size()));
391 bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
392 PickleIterator* iter,
393 param_type* r) {
394 const char *data;
395 int data_size = 0;
396 if (!iter->ReadData(&data, &data_size) || data_size < 0)
397 return false;
398 r->resize(data_size);
399 if (data_size)
400 memcpy(&r->front(), data, data_size);
401 return true;
404 void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
405 std::string* l) {
406 LogBytes(p, l);
409 void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
410 WriteParam(m, static_cast<int>(p.size()));
411 // Cast to bool below is required because libc++'s
412 // vector<bool>::const_reference is different from bool, and we want to avoid
413 // writing an extra specialization of ParamTraits for it.
414 for (size_t i = 0; i < p.size(); i++)
415 WriteParam(m, static_cast<bool>(p[i]));
418 bool ParamTraits<std::vector<bool> >::Read(const Message* m,
419 PickleIterator* iter,
420 param_type* r) {
421 int size;
422 // ReadLength() checks for < 0 itself.
423 if (!iter->ReadLength(&size))
424 return false;
425 r->resize(size);
426 for (int i = 0; i < size; i++) {
427 bool value;
428 if (!ReadParam(m, iter, &value))
429 return false;
430 (*r)[i] = value;
432 return true;
435 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
436 for (size_t i = 0; i < p.size(); ++i) {
437 if (i != 0)
438 l->push_back(' ');
439 LogParam(static_cast<bool>(p[i]), l);
443 void ParamTraits<base::DictionaryValue>::Write(Message* m,
444 const param_type& p) {
445 WriteValue(m, &p, 0);
448 bool ParamTraits<base::DictionaryValue>::Read(
449 const Message* m, PickleIterator* iter, param_type* r) {
450 int type;
451 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
452 return false;
454 return ReadDictionaryValue(m, iter, r, 0);
457 void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
458 std::string* l) {
459 std::string json;
460 base::JSONWriter::Write(&p, &json);
461 l->append(json);
464 #if defined(OS_POSIX)
465 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
466 const bool valid = p.fd >= 0;
467 WriteParam(m, valid);
469 if (!valid)
470 return;
472 if (p.auto_close) {
473 if (!m->WriteAttachment(
474 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
475 NOTREACHED();
476 } else {
477 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
478 NOTREACHED();
482 bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
483 PickleIterator* iter,
484 param_type* r) {
485 *r = base::FileDescriptor();
487 bool valid;
488 if (!ReadParam(m, iter, &valid))
489 return false;
491 // TODO(morrita): Seems like this should return false.
492 if (!valid)
493 return true;
495 scoped_refptr<MessageAttachment> attachment;
496 if (!m->ReadAttachment(iter, &attachment))
497 return false;
499 *r = base::FileDescriptor(attachment->TakePlatformFile(), true);
500 return true;
503 void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
504 std::string* l) {
505 if (p.auto_close) {
506 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
507 } else {
508 l->append(base::StringPrintf("FD(%d)", p.fd));
511 #endif // defined(OS_POSIX)
513 void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) {
514 p.WriteToPickle(m);
517 bool ParamTraits<base::FilePath>::Read(const Message* m,
518 PickleIterator* iter,
519 param_type* r) {
520 return r->ReadFromPickle(iter);
523 void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
524 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
527 void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) {
528 WriteValue(m, &p, 0);
531 bool ParamTraits<base::ListValue>::Read(
532 const Message* m, PickleIterator* iter, param_type* r) {
533 int type;
534 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
535 return false;
537 return ReadListValue(m, iter, r, 0);
540 void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
541 std::string json;
542 base::JSONWriter::Write(&p, &json);
543 l->append(json);
546 void ParamTraits<base::NullableString16>::Write(Message* m,
547 const param_type& p) {
548 WriteParam(m, p.string());
549 WriteParam(m, p.is_null());
552 bool ParamTraits<base::NullableString16>::Read(const Message* m,
553 PickleIterator* iter,
554 param_type* r) {
555 base::string16 string;
556 if (!ReadParam(m, iter, &string))
557 return false;
558 bool is_null;
559 if (!ReadParam(m, iter, &is_null))
560 return false;
561 *r = base::NullableString16(string, is_null);
562 return true;
565 void ParamTraits<base::NullableString16>::Log(const param_type& p,
566 std::string* l) {
567 l->append("(");
568 LogParam(p.string(), l);
569 l->append(", ");
570 LogParam(p.is_null(), l);
571 l->append(")");
574 void ParamTraits<base::File::Info>::Write(Message* m,
575 const param_type& p) {
576 WriteParam(m, p.size);
577 WriteParam(m, p.is_directory);
578 WriteParam(m, p.last_modified.ToDoubleT());
579 WriteParam(m, p.last_accessed.ToDoubleT());
580 WriteParam(m, p.creation_time.ToDoubleT());
583 bool ParamTraits<base::File::Info>::Read(const Message* m,
584 PickleIterator* iter,
585 param_type* p) {
586 double last_modified, last_accessed, creation_time;
587 if (!ReadParam(m, iter, &p->size) ||
588 !ReadParam(m, iter, &p->is_directory) ||
589 !ReadParam(m, iter, &last_modified) ||
590 !ReadParam(m, iter, &last_accessed) ||
591 !ReadParam(m, iter, &creation_time))
592 return false;
593 p->last_modified = base::Time::FromDoubleT(last_modified);
594 p->last_accessed = base::Time::FromDoubleT(last_accessed);
595 p->creation_time = base::Time::FromDoubleT(creation_time);
596 return true;
599 void ParamTraits<base::File::Info>::Log(const param_type& p,
600 std::string* l) {
601 l->append("(");
602 LogParam(p.size, l);
603 l->append(",");
604 LogParam(p.is_directory, l);
605 l->append(",");
606 LogParam(p.last_modified.ToDoubleT(), l);
607 l->append(",");
608 LogParam(p.last_accessed.ToDoubleT(), l);
609 l->append(",");
610 LogParam(p.creation_time.ToDoubleT(), l);
611 l->append(")");
614 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
615 ParamTraits<int64>::Write(m, p.ToInternalValue());
618 bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
619 param_type* r) {
620 int64 value;
621 if (!ParamTraits<int64>::Read(m, iter, &value))
622 return false;
623 *r = base::Time::FromInternalValue(value);
624 return true;
627 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
628 ParamTraits<int64>::Log(p.ToInternalValue(), l);
631 void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
632 ParamTraits<int64>::Write(m, p.ToInternalValue());
635 bool ParamTraits<base::TimeDelta>::Read(const Message* m,
636 PickleIterator* iter,
637 param_type* r) {
638 int64 value;
639 bool ret = ParamTraits<int64>::Read(m, iter, &value);
640 if (ret)
641 *r = base::TimeDelta::FromInternalValue(value);
643 return ret;
646 void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
647 ParamTraits<int64>::Log(p.ToInternalValue(), l);
650 void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
651 ParamTraits<int64>::Write(m, p.ToInternalValue());
654 bool ParamTraits<base::TimeTicks>::Read(const Message* m,
655 PickleIterator* iter,
656 param_type* r) {
657 int64 value;
658 bool ret = ParamTraits<int64>::Read(m, iter, &value);
659 if (ret)
660 *r = base::TimeTicks::FromInternalValue(value);
662 return ret;
665 void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
666 ParamTraits<int64>::Log(p.ToInternalValue(), l);
669 void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
670 #if defined(OS_WIN)
671 // On Windows marshalling pipe handle is not supported.
672 DCHECK(p.pipe.handle == NULL);
673 #endif // defined (OS_WIN)
674 WriteParam(m, p.name);
675 #if defined(OS_POSIX)
676 WriteParam(m, p.socket);
677 #endif
680 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
681 PickleIterator* iter,
682 param_type* r) {
683 return ReadParam(m, iter, &r->name)
684 #if defined(OS_POSIX)
685 && ReadParam(m, iter, &r->socket)
686 #endif
690 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
691 std::string* l) {
692 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
693 #if defined(OS_POSIX)
694 l->append(", ");
695 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
696 #endif
697 l->append(")");
700 void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
701 WriteParam(m, p.channel);
702 WriteParam(m, p.routing_id);
703 WriteParam(m, p.type);
704 WriteParam(m, p.flags);
705 WriteParam(m, p.sent);
706 WriteParam(m, p.receive);
707 WriteParam(m, p.dispatch);
708 WriteParam(m, p.message_name);
709 WriteParam(m, p.params);
712 bool ParamTraits<LogData>::Read(const Message* m,
713 PickleIterator* iter,
714 param_type* r) {
715 return
716 ReadParam(m, iter, &r->channel) &&
717 ReadParam(m, iter, &r->routing_id) &&
718 ReadParam(m, iter, &r->type) &&
719 ReadParam(m, iter, &r->flags) &&
720 ReadParam(m, iter, &r->sent) &&
721 ReadParam(m, iter, &r->receive) &&
722 ReadParam(m, iter, &r->dispatch) &&
723 ReadParam(m, iter, &r->message_name) &&
724 ReadParam(m, iter, &r->params);
727 void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
728 // Doesn't make sense to implement this!
731 void ParamTraits<Message>::Write(Message* m, const Message& p) {
732 #if defined(OS_POSIX)
733 // We don't serialize the file descriptors in the nested message, so there
734 // better not be any.
735 DCHECK(!p.HasAttachments());
736 #endif
738 // Don't just write out the message. This is used to send messages between
739 // NaCl (Posix environment) and the browser (could be on Windows). The message
740 // header formats differ between these systems (so does handle sharing, but
741 // we already asserted we don't have any handles). So just write out the
742 // parts of the header we use.
744 // Be careful also to use only explicitly-sized types. The NaCl environment
745 // could be 64-bit and the host browser could be 32-bits. The nested message
746 // may or may not be safe to send between 32-bit and 64-bit systems, but we
747 // leave that up to the code sending the message to ensure.
748 m->WriteUInt32(static_cast<uint32>(p.routing_id()));
749 m->WriteUInt32(p.type());
750 m->WriteUInt32(p.flags());
751 m->WriteData(p.payload(), static_cast<uint32>(p.payload_size()));
754 bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
755 Message* r) {
756 uint32 routing_id, type, flags;
757 if (!iter->ReadUInt32(&routing_id) ||
758 !iter->ReadUInt32(&type) ||
759 !iter->ReadUInt32(&flags))
760 return false;
762 int payload_size;
763 const char* payload;
764 if (!iter->ReadData(&payload, &payload_size))
765 return false;
767 r->SetHeaderValues(static_cast<int32>(routing_id), type, flags);
768 return r->WriteBytes(payload, payload_size);
771 void ParamTraits<Message>::Log(const Message& p, std::string* l) {
772 l->append("<IPC::Message>");
775 #if defined(OS_WIN)
776 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
777 // bit systems. That's why we use the Windows macros to convert to 32 bits.
778 void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
779 m->WriteInt(HandleToLong(p));
782 bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
783 param_type* r) {
784 int32 temp;
785 if (!iter->ReadInt(&temp))
786 return false;
787 *r = LongToHandle(temp);
788 return true;
791 void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
792 l->append(base::StringPrintf("0x%X", p));
795 void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
796 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
799 bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
800 param_type* r) {
801 const char *data;
802 int data_size = 0;
803 if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) {
804 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
805 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
806 memcpy(r, data, sizeof(LOGFONT));
807 return true;
811 NOTREACHED();
812 return false;
815 void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
816 l->append(base::StringPrintf("<LOGFONT>"));
819 void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
820 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
823 bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
824 param_type* r) {
825 const char *data;
826 int data_size = 0;
827 bool result = iter->ReadData(&data, &data_size);
828 if (result && data_size == sizeof(MSG)) {
829 memcpy(r, data, sizeof(MSG));
830 } else {
831 result = false;
832 NOTREACHED();
835 return result;
838 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
839 l->append("<MSG>");
842 #endif // OS_WIN
844 } // namespace IPC