Clear preference when reset in gesture config UI
[chromium-blink-merge.git] / ipc / ipc_message_utils.cc
blob8bf7609f8b4214c79254d329ab64f050259ed1c2
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"
17 #if defined(OS_POSIX)
18 #include "ipc/file_descriptor_set_posix.h"
19 #elif defined(OS_WIN)
20 #include <tchar.h>
21 #endif
23 namespace IPC {
25 namespace {
27 const int kMaxRecursionDepth = 100;
29 template<typename CharType>
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])));
45 if (data.size() > kMaxBytesToLog) {
46 out->append(base::StringPrintf(
47 " and %u more bytes",
48 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
50 #endif
53 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
54 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;
63 m->WriteInt(value->GetType());
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;
75 case base::Value::TYPE_INTEGER: {
76 int val;
77 result = value->GetAsInteger(&val);
78 DCHECK(result);
79 WriteParam(m, val);
80 break;
82 case base::Value::TYPE_DOUBLE: {
83 double val;
84 result = value->GetAsDouble(&val);
85 DCHECK(result);
86 WriteParam(m, val);
87 break;
89 case base::Value::TYPE_STRING: {
90 std::string val;
91 result = value->GetAsString(&val);
92 DCHECK(result);
93 WriteParam(m, val);
94 break;
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;
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();
109 it.Advance()) {
110 WriteParam(m, it.key());
111 WriteValue(m, &it.value(), recursion + 1);
113 break;
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);
122 break;
127 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
128 // object.
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;
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);
144 return true;
147 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
148 // object.
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;
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);
162 return true;
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;
172 int type;
173 if (!ReadParam(m, iter, &type))
174 return false;
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;
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;
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;
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;
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;
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;
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;
230 default:
231 return false;
234 return true;
237 } // namespace
239 // -----------------------------------------------------------------------------
241 LogData::LogData()
242 : routing_id(0),
243 type(0),
244 sent(0),
245 receive(0),
246 dispatch(0) {
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,
285 param_type* r) {
286 const char* data;
287 if (!m->ReadBytes(iter, &data, sizeof(param_type)))
288 return false;
289 memcpy(r, data, sizeof(param_type));
290 return true;
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,
302 param_type* r) {
303 const char *data;
304 int data_size;
305 if (!m->ReadData(iter, &data, &data_size) ||
306 data_size != sizeof(param_type)) {
307 NOTREACHED();
308 return false;
310 memcpy(r, data, sizeof(param_type));
311 return true;
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,
323 param_type* r) {
324 const char *data;
325 int data_size;
326 if (!m->ReadData(iter, &data, &data_size) ||
327 data_size != sizeof(param_type)) {
328 NOTREACHED();
329 return false;
331 memcpy(r, data, sizeof(param_type));
332 return true;
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) {
341 l->append(p);
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));
352 #endif
354 void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
355 if (p.empty()) {
356 m->WriteData(NULL, 0);
357 } else {
358 m->WriteData(&p.front(), static_cast<int>(p.size()));
362 bool ParamTraits<std::vector<char> >::Read(const Message* m,
363 PickleIterator* iter,
364 param_type* r) {
365 const char *data;
366 int data_size = 0;
367 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
368 return false;
369 r->resize(data_size);
370 if (data_size)
371 memcpy(&r->front(), data, data_size);
372 return true;
375 void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
376 LogBytes(p, l);
379 void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
380 const param_type& p) {
381 if (p.empty()) {
382 m->WriteData(NULL, 0);
383 } else {
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,
391 param_type* r) {
392 const char *data;
393 int data_size = 0;
394 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
395 return false;
396 r->resize(data_size);
397 if (data_size)
398 memcpy(&r->front(), data, data_size);
399 return true;
402 void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
403 std::string* l) {
404 LogBytes(p, l);
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,
418 param_type* r) {
419 int size;
420 // ReadLength() checks for < 0 itself.
421 if (!m->ReadLength(iter, &size))
422 return false;
423 r->resize(size);
424 for (int i = 0; i < size; i++) {
425 bool value;
426 if (!ReadParam(m, iter, &value))
427 return false;
428 (*r)[i] = value;
430 return true;
433 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
434 for (size_t i = 0; i < p.size(); ++i) {
435 if (i != 0)
436 l->push_back(' ');
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) {
448 int type;
449 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
450 return false;
452 return ReadDictionaryValue(m, iter, r, 0);
455 void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
456 std::string* l) {
457 std::string json;
458 base::JSONWriter::Write(&p, &json);
459 l->append(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);
467 if (valid) {
468 if (!m->WriteFileDescriptor(p))
469 NOTREACHED();
473 bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
474 PickleIterator* iter,
475 param_type* r) {
476 bool valid;
477 if (!ReadParam(m, iter, &valid))
478 return false;
480 if (!valid) {
481 r->fd = -1;
482 r->auto_close = false;
483 return true;
486 return m->ReadFileDescriptor(iter, r);
489 void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
490 std::string* l) {
491 if (p.auto_close) {
492 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
493 } else {
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) {
500 p.WriteToPickle(m);
503 bool ParamTraits<base::FilePath>::Read(const Message* m,
504 PickleIterator* iter,
505 param_type* r) {
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) {
519 int type;
520 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
521 return false;
523 return ReadListValue(m, iter, r, 0);
526 void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
527 std::string json;
528 base::JSONWriter::Write(&p, &json);
529 l->append(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,
540 param_type* r) {
541 string16 string;
542 if (!ReadParam(m, iter, &string))
543 return false;
544 bool is_null;
545 if (!ReadParam(m, iter, &is_null))
546 return false;
547 *r = base::NullableString16(string, is_null);
548 return true;
551 void ParamTraits<base::NullableString16>::Log(const param_type& p,
552 std::string* l) {
553 l->append("(");
554 LogParam(p.string(), l);
555 l->append(", ");
556 LogParam(p.is_null(), l);
557 l->append(")");
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,
571 param_type* p) {
572 double last_modified;
573 double last_accessed;
574 double creation_time;
575 bool result =
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);
581 if (result) {
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);
586 return result;
589 void ParamTraits<base::PlatformFileInfo>::Log(const param_type& p,
590 std::string* l) {
591 l->append("(");
592 LogParam(p.size, l);
593 l->append(",");
594 LogParam(p.is_directory, l);
595 l->append(",");
596 LogParam(p.last_modified.ToDoubleT(), l);
597 l->append(",");
598 LogParam(p.last_accessed.ToDoubleT(), l);
599 l->append(",");
600 LogParam(p.creation_time.ToDoubleT(), l);
601 l->append(")");
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,
609 param_type* r) {
610 int64 value;
611 if (!ParamTraits<int64>::Read(m, iter, &value))
612 return false;
613 *r = base::Time::FromInternalValue(value);
614 return true;
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,
627 param_type* r) {
628 int64 value;
629 bool ret = ParamTraits<int64>::Read(m, iter, &value);
630 if (ret)
631 *r = base::TimeDelta::FromInternalValue(value);
633 return ret;
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,
646 param_type* r) {
647 int64 value;
648 bool ret = ParamTraits<int64>::Read(m, iter, &value);
649 if (ret)
650 *r = base::TimeTicks::FromInternalValue(value);
652 return ret;
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) {
660 #if defined(OS_WIN)
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);
667 #endif
670 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
671 PickleIterator* iter,
672 param_type* r) {
673 return ReadParam(m, iter, &r->name)
674 #if defined(OS_POSIX)
675 && ReadParam(m, iter, &r->socket)
676 #endif
680 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
681 std::string* l) {
682 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
683 #if defined(OS_POSIX)
684 l->append(", ");
685 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
686 #endif
687 l->append(")");
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,
704 param_type* r) {
705 return
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());
726 #endif
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,
745 Message* r) {
746 uint32 routing_id, type, flags;
747 if (!m->ReadUInt32(iter, &routing_id) ||
748 !m->ReadUInt32(iter, &type) ||
749 !m->ReadUInt32(iter, &flags))
750 return false;
752 int payload_size;
753 const char* payload;
754 if (!m->ReadData(iter, &payload, &payload_size))
755 return false;
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>");
765 #if defined(OS_WIN)
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,
773 param_type* r) {
774 int32 temp;
775 if (!m->ReadInt(iter, &temp))
776 return false;
777 *r = LongToHandle(temp);
778 return true;
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,
790 param_type* r) {
791 const char *data;
792 int data_size = 0;
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));
797 return true;
801 NOTREACHED();
802 return false;
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,
814 param_type* r) {
815 const char *data;
816 int data_size = 0;
817 bool result = m->ReadData(iter, &data, &data_size);
818 if (result && data_size == sizeof(MSG)) {
819 memcpy(r, data, sizeof(MSG));
820 } else {
821 result = false;
822 NOTREACHED();
825 return result;
828 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
829 l->append("<MSG>");
832 #endif // OS_WIN
834 } // namespace IPC