Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ipc/ipc_message_utils.h" | 5 #include "ipc/ipc_message_utils.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/nullable_string16.h" | 10 #include "base/nullable_string16.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 // Windows has a GUI for logging, which can handle arbitrary binary data. | 32 // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 33 for (size_t i = 0; i < data.size(); ++i) | 33 for (size_t i = 0; i < data.size(); ++i) |
| 34 out->push_back(data[i]); | 34 out->push_back(data[i]); |
| 35 #else | 35 #else |
| 36 // On POSIX, we log to stdout, which we assume can display ASCII. | 36 // On POSIX, we log to stdout, which we assume can display ASCII. |
| 37 static const size_t kMaxBytesToLog = 100; | 37 static const size_t kMaxBytesToLog = 100; |
| 38 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { | 38 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
| 39 if (isprint(data[i])) | 39 if (isprint(data[i])) |
| 40 out->push_back(data[i]); | 40 out->push_back(data[i]); |
| 41 else | 41 else |
| 42 out->append(StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); | 42 out->append( |
| 43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); | |
| 43 } | 44 } |
| 44 if (data.size() > kMaxBytesToLog) { | 45 if (data.size() > kMaxBytesToLog) { |
| 45 out->append( | 46 out->append( |
| 46 StringPrintf(" and %u more bytes", | 47 base::StringPrintf(" and %u more bytes", |
| 47 static_cast<unsigned>(data.size() - kMaxBytesToLog))); | 48 static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
|
Tom Sepez
2013/03/27 19:14:51
nit: funny indent here.
| |
| 48 } | 49 } |
| 49 #endif | 50 #endif |
| 50 } | 51 } |
| 51 | 52 |
| 52 bool ReadValue(const Message* m, PickleIterator* iter, Value** value, | 53 bool ReadValue(const Message* m, PickleIterator* iter, Value** value, |
| 53 int recursion); | 54 int recursion); |
| 54 | 55 |
| 55 void WriteValue(Message* m, const Value* value, int recursion) { | 56 void WriteValue(Message* m, const Value* value, int recursion) { |
| 56 bool result; | 57 bool result; |
| 57 if (recursion > kMaxRecursionDepth) { | 58 if (recursion > kMaxRecursionDepth) { |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 if (!m->ReadData(iter, &data, &data_size) || | 303 if (!m->ReadData(iter, &data, &data_size) || |
| 303 data_size != sizeof(param_type)) { | 304 data_size != sizeof(param_type)) { |
| 304 NOTREACHED(); | 305 NOTREACHED(); |
| 305 return false; | 306 return false; |
| 306 } | 307 } |
| 307 memcpy(r, data, sizeof(param_type)); | 308 memcpy(r, data, sizeof(param_type)); |
| 308 return true; | 309 return true; |
| 309 } | 310 } |
| 310 | 311 |
| 311 void ParamTraits<float>::Log(const param_type& p, std::string* l) { | 312 void ParamTraits<float>::Log(const param_type& p, std::string* l) { |
| 312 l->append(StringPrintf("%e", p)); | 313 l->append(base::StringPrintf("%e", p)); |
| 313 } | 314 } |
| 314 | 315 |
| 315 void ParamTraits<double>::Write(Message* m, const param_type& p) { | 316 void ParamTraits<double>::Write(Message* m, const param_type& p) { |
| 316 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); | 317 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 317 } | 318 } |
| 318 | 319 |
| 319 bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter, | 320 bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter, |
| 320 param_type* r) { | 321 param_type* r) { |
| 321 const char *data; | 322 const char *data; |
| 322 int data_size; | 323 int data_size; |
| 323 if (!m->ReadData(iter, &data, &data_size) || | 324 if (!m->ReadData(iter, &data, &data_size) || |
| 324 data_size != sizeof(param_type)) { | 325 data_size != sizeof(param_type)) { |
| 325 NOTREACHED(); | 326 NOTREACHED(); |
| 326 return false; | 327 return false; |
| 327 } | 328 } |
| 328 memcpy(r, data, sizeof(param_type)); | 329 memcpy(r, data, sizeof(param_type)); |
| 329 return true; | 330 return true; |
| 330 } | 331 } |
| 331 | 332 |
| 332 void ParamTraits<double>::Log(const param_type& p, std::string* l) { | 333 void ParamTraits<double>::Log(const param_type& p, std::string* l) { |
| 333 l->append(StringPrintf("%e", p)); | 334 l->append(base::StringPrintf("%e", p)); |
| 334 } | 335 } |
| 335 | 336 |
| 336 | 337 |
| 337 void ParamTraits<std::string>::Log(const param_type& p, std::string* l) { | 338 void ParamTraits<std::string>::Log(const param_type& p, std::string* l) { |
| 338 l->append(p); | 339 l->append(p); |
| 339 } | 340 } |
| 340 | 341 |
| 341 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { | 342 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { |
| 342 l->append(WideToUTF8(p)); | 343 l->append(WideToUTF8(p)); |
| 343 } | 344 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 r->auto_close = false; | 475 r->auto_close = false; |
| 475 return true; | 476 return true; |
| 476 } | 477 } |
| 477 | 478 |
| 478 return m->ReadFileDescriptor(iter, r); | 479 return m->ReadFileDescriptor(iter, r); |
| 479 } | 480 } |
| 480 | 481 |
| 481 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, | 482 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
| 482 std::string* l) { | 483 std::string* l) { |
| 483 if (p.auto_close) { | 484 if (p.auto_close) { |
| 484 l->append(StringPrintf("FD(%d auto-close)", p.fd)); | 485 l->append(base::StringPrintf("FD(%d auto-close)", p.fd)); |
| 485 } else { | 486 } else { |
| 486 l->append(StringPrintf("FD(%d)", p.fd)); | 487 l->append(base::StringPrintf("FD(%d)", p.fd)); |
| 487 } | 488 } |
| 488 } | 489 } |
| 489 #endif // defined(OS_POSIX) | 490 #endif // defined(OS_POSIX) |
| 490 | 491 |
| 491 void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) { | 492 void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) { |
| 492 p.WriteToPickle(m); | 493 p.WriteToPickle(m); |
| 493 } | 494 } |
| 494 | 495 |
| 495 bool ParamTraits<base::FilePath>::Read(const Message* m, | 496 bool ParamTraits<base::FilePath>::Read(const Message* m, |
| 496 PickleIterator* iter, | 497 PickleIterator* iter, |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 661 param_type* r) { | 662 param_type* r) { |
| 662 return ReadParam(m, iter, &r->name) | 663 return ReadParam(m, iter, &r->name) |
| 663 #if defined(OS_POSIX) | 664 #if defined(OS_POSIX) |
| 664 && ReadParam(m, iter, &r->socket) | 665 && ReadParam(m, iter, &r->socket) |
| 665 #endif | 666 #endif |
| 666 ; | 667 ; |
| 667 } | 668 } |
| 668 | 669 |
| 669 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, | 670 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
| 670 std::string* l) { | 671 std::string* l) { |
| 671 l->append(StringPrintf("ChannelHandle(%s", p.name.c_str())); | 672 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str())); |
| 672 #if defined(OS_POSIX) | 673 #if defined(OS_POSIX) |
| 673 l->append(", "); | 674 l->append(", "); |
| 674 ParamTraits<base::FileDescriptor>::Log(p.socket, l); | 675 ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
| 675 #endif | 676 #endif |
| 676 l->append(")"); | 677 l->append(")"); |
| 677 } | 678 } |
| 678 | 679 |
| 679 void ParamTraits<LogData>::Write(Message* m, const param_type& p) { | 680 void ParamTraits<LogData>::Write(Message* m, const param_type& p) { |
| 680 WriteParam(m, p.channel); | 681 WriteParam(m, p.channel); |
| 681 WriteParam(m, p.routing_id); | 682 WriteParam(m, p.routing_id); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 761 bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter, | 762 bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter, |
| 762 param_type* r) { | 763 param_type* r) { |
| 763 int32 temp; | 764 int32 temp; |
| 764 if (!m->ReadInt(iter, &temp)) | 765 if (!m->ReadInt(iter, &temp)) |
| 765 return false; | 766 return false; |
| 766 *r = LongToHandle(temp); | 767 *r = LongToHandle(temp); |
| 767 return true; | 768 return true; |
| 768 } | 769 } |
| 769 | 770 |
| 770 void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) { | 771 void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) { |
| 771 l->append(StringPrintf("0x%X", p)); | 772 l->append(base::StringPrintf("0x%X", p)); |
| 772 } | 773 } |
| 773 | 774 |
| 774 void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) { | 775 void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) { |
| 775 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); | 776 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 776 } | 777 } |
| 777 | 778 |
| 778 bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter, | 779 bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter, |
| 779 param_type* r) { | 780 param_type* r) { |
| 780 const char *data; | 781 const char *data; |
| 781 int data_size = 0; | 782 int data_size = 0; |
| 782 if (m->ReadData(iter, &data, &data_size) && data_size == sizeof(LOGFONT)) { | 783 if (m->ReadData(iter, &data, &data_size) && data_size == sizeof(LOGFONT)) { |
| 783 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data)); | 784 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data)); |
| 784 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) { | 785 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) { |
| 785 memcpy(r, data, sizeof(LOGFONT)); | 786 memcpy(r, data, sizeof(LOGFONT)); |
| 786 return true; | 787 return true; |
| 787 } | 788 } |
| 788 } | 789 } |
| 789 | 790 |
| 790 NOTREACHED(); | 791 NOTREACHED(); |
| 791 return false; | 792 return false; |
| 792 } | 793 } |
| 793 | 794 |
| 794 void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) { | 795 void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) { |
| 795 l->append(StringPrintf("<LOGFONT>")); | 796 l->append(base::StringPrintf("<LOGFONT>")); |
| 796 } | 797 } |
| 797 | 798 |
| 798 void ParamTraits<MSG>::Write(Message* m, const param_type& p) { | 799 void ParamTraits<MSG>::Write(Message* m, const param_type& p) { |
| 799 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); | 800 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 800 } | 801 } |
| 801 | 802 |
| 802 bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter, | 803 bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter, |
| 803 param_type* r) { | 804 param_type* r) { |
| 804 const char *data; | 805 const char *data; |
| 805 int data_size = 0; | 806 int data_size = 0; |
| 806 bool result = m->ReadData(iter, &data, &data_size); | 807 bool result = m->ReadData(iter, &data, &data_size); |
| 807 if (result && data_size == sizeof(MSG)) { | 808 if (result && data_size == sizeof(MSG)) { |
| 808 memcpy(r, data, sizeof(MSG)); | 809 memcpy(r, data, sizeof(MSG)); |
| 809 } else { | 810 } else { |
| 810 result = false; | 811 result = false; |
| 811 NOTREACHED(); | 812 NOTREACHED(); |
| 812 } | 813 } |
| 813 | 814 |
| 814 return result; | 815 return result; |
| 815 } | 816 } |
| 816 | 817 |
| 817 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { | 818 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 818 l->append("<MSG>"); | 819 l->append("<MSG>"); |
| 819 } | 820 } |
| 820 | 821 |
| 821 #endif // OS_WIN | 822 #endif // OS_WIN |
| 822 | 823 |
| 823 } // namespace IPC | 824 } // namespace IPC |
| OLD | NEW |