| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 void ParamTraits<base::FileDescriptor>::GetSize(base::PickleSizer* sizer, | 614 void ParamTraits<base::FileDescriptor>::GetSize(base::PickleSizer* sizer, |
| 615 const param_type& p) { | 615 const param_type& p) { |
| 616 GetParamSize(sizer, p.fd >= 0); | 616 GetParamSize(sizer, p.fd >= 0); |
| 617 if (p.fd >= 0) | 617 if (p.fd >= 0) |
| 618 sizer->AddAttachment(); | 618 sizer->AddAttachment(); |
| 619 } | 619 } |
| 620 | 620 |
| 621 void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m, | 621 void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m, |
| 622 const param_type& p) { | 622 const param_type& p) { |
| 623 // This serialization must be kept in sync with | 623 // This serialization must be kept in sync with |
| 624 // nacl_ipc_adapater.cc:WriteHandle(). | 624 // nacl_message_scanner.cc:WriteHandle(). |
| 625 const bool valid = p.fd >= 0; | 625 const bool valid = p.fd >= 0; |
| 626 WriteParam(m, valid); | 626 WriteParam(m, valid); |
| 627 | 627 |
| 628 if (!valid) | 628 if (!valid) |
| 629 return; | 629 return; |
| 630 | 630 |
| 631 if (p.auto_close) { | 631 if (p.auto_close) { |
| 632 if (!m->WriteAttachment( | 632 if (!m->WriteAttachment( |
| 633 new internal::PlatformFileAttachment(base::ScopedFD(p.fd)))) | 633 new internal::PlatformFileAttachment(base::ScopedFD(p.fd)))) |
| 634 NOTREACHED(); | 634 NOTREACHED(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, | 669 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
| 670 std::string* l) { | 670 std::string* l) { |
| 671 if (p.auto_close) { | 671 if (p.auto_close) { |
| 672 l->append(base::StringPrintf("FD(%d auto-close)", p.fd)); | 672 l->append(base::StringPrintf("FD(%d auto-close)", p.fd)); |
| 673 } else { | 673 } else { |
| 674 l->append(base::StringPrintf("FD(%d)", p.fd)); | 674 l->append(base::StringPrintf("FD(%d)", p.fd)); |
| 675 } | 675 } |
| 676 } | 676 } |
| 677 #endif // defined(OS_POSIX) | 677 #endif // defined(OS_POSIX) |
| 678 | 678 |
| 679 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 680 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, | 679 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, |
| 681 const param_type& p) { | 680 const param_type& p) { |
| 682 GetParamSize(sizer, p.IsValid()); | 681 GetParamSize(sizer, p.IsValid()); |
| 683 if (p.IsValid()) { | 682 if (!p.IsValid()) |
| 684 GetParamSize(sizer, p.GetMemoryObject()); | 683 return; |
| 685 GetParamSize(sizer, p.GetGUID()); | 684 |
| 686 uint32_t dummy = 0; | 685 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 687 GetParamSize(sizer, dummy); | 686 GetParamSize(sizer, p.GetMemoryObject()); |
| 688 } | 687 #elif defined(OS_WIN) |
| 688 GetParamSize(sizer, p.GetHandle()); |
| 689 #else |
| 690 sizer->AddAttachment(); |
| 691 #endif |
| 692 |
| 693 GetParamSize(sizer, p.GetGUID()); |
| 694 GetParamSize(sizer, static_cast<uint64_t>(p.GetSize())); |
| 689 } | 695 } |
| 690 | 696 |
| 691 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | 697 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, |
| 692 const param_type& p) { | 698 const param_type& p) { |
| 699 // This serialization must be kept in sync with |
| 700 // nacl_message_scanner.cc:WriteHandle(). |
| 693 const bool valid = p.IsValid(); | 701 const bool valid = p.IsValid(); |
| 694 WriteParam(m, valid); | 702 WriteParam(m, valid); |
| 695 | 703 |
| 696 if (!valid) | 704 if (!valid) |
| 697 return; | 705 return; |
| 698 | 706 |
| 707 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 699 MachPortMac mach_port_mac(p.GetMemoryObject()); | 708 MachPortMac mach_port_mac(p.GetMemoryObject()); |
| 700 WriteParam(m, mach_port_mac); | 709 WriteParam(m, mach_port_mac); |
| 701 DCHECK(!p.GetGUID().is_empty()); | |
| 702 WriteParam(m, p.GetGUID()); | |
| 703 | |
| 704 size_t size = 0; | |
| 705 bool result = p.GetSize(&size); | |
| 706 DCHECK(result); | |
| 707 ParamTraits<uint32_t>::Write(m, static_cast<uint32_t>(size)); | |
| 708 | |
| 709 // If the caller intended to pass ownership to the IPC stack, release a | |
| 710 // reference. | |
| 711 if (p.OwnershipPassesToIPC()) | |
| 712 p.Close(); | |
| 713 } | |
| 714 | |
| 715 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | |
| 716 base::PickleIterator* iter, | |
| 717 param_type* r) { | |
| 718 *r = base::SharedMemoryHandle(); | |
| 719 | |
| 720 bool valid; | |
| 721 if (!ReadParam(m, iter, &valid)) | |
| 722 return false; | |
| 723 if (!valid) | |
| 724 return true; | |
| 725 | |
| 726 MachPortMac mach_port_mac; | |
| 727 if (!ReadParam(m, iter, &mach_port_mac)) | |
| 728 return false; | |
| 729 | |
| 730 base::UnguessableToken guid; | |
| 731 if (!ReadParam(m, iter, &guid)) | |
| 732 return false; | |
| 733 | |
| 734 uint32_t size; | |
| 735 if (!ParamTraits<uint32_t>::Read(m, iter, &size)) | |
| 736 return false; | |
| 737 | |
| 738 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(), | |
| 739 static_cast<size_t>(size), guid); | |
| 740 return true; | |
| 741 } | |
| 742 | |
| 743 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | |
| 744 std::string* l) { | |
| 745 l->append("Mach port: "); | |
| 746 LogParam(p.GetMemoryObject(), l); | |
| 747 l->append("GUID: "); | |
| 748 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | |
| 749 } | |
| 750 | |
| 751 #elif defined(OS_WIN) | 710 #elif defined(OS_WIN) |
| 752 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* s, | |
| 753 const param_type& p) { | |
| 754 GetParamSize(s, p.IsValid()); | |
| 755 if (p.IsValid()) { | |
| 756 GetParamSize(s, p.GetHandle()); | |
| 757 GetParamSize(s, p.GetGUID()); | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | |
| 762 const param_type& p) { | |
| 763 const bool valid = p.IsValid(); | |
| 764 WriteParam(m, valid); | |
| 765 | |
| 766 if (!valid) | |
| 767 return; | |
| 768 | |
| 769 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE); | 711 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE); |
| 770 WriteParam(m, handle_win); | 712 WriteParam(m, handle_win); |
| 771 | 713 #else |
| 772 // If the caller intended to pass ownership to the IPC stack, release a | |
| 773 // reference. | |
| 774 if (p.OwnershipPassesToIPC()) | |
| 775 p.Close(); | |
| 776 | |
| 777 DCHECK(!p.GetGUID().is_empty()); | |
| 778 WriteParam(m, p.GetGUID()); | |
| 779 } | |
| 780 | |
| 781 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | |
| 782 base::PickleIterator* iter, | |
| 783 param_type* r) { | |
| 784 *r = base::SharedMemoryHandle(); | |
| 785 | |
| 786 bool valid; | |
| 787 if (!ReadParam(m, iter, &valid)) | |
| 788 return false; | |
| 789 if (!valid) | |
| 790 return true; | |
| 791 | |
| 792 HandleWin handle_win; | |
| 793 if (!ReadParam(m, iter, &handle_win)) | |
| 794 return false; | |
| 795 | |
| 796 base::UnguessableToken guid; | |
| 797 if (!ReadParam(m, iter, &guid)) | |
| 798 return false; | |
| 799 | |
| 800 *r = base::SharedMemoryHandle(handle_win.get_handle(), guid); | |
| 801 return true; | |
| 802 } | |
| 803 | |
| 804 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | |
| 805 std::string* l) { | |
| 806 l->append("HANDLE: "); | |
| 807 LogParam(p.GetHandle(), l); | |
| 808 l->append("GUID: "); | |
| 809 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | |
| 810 } | |
| 811 #elif defined(OS_POSIX) | |
| 812 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, | |
| 813 const param_type& p) { | |
| 814 GetParamSize(sizer, p.IsValid()); | |
| 815 if (p.IsValid()) { | |
| 816 sizer->AddAttachment(); | |
| 817 GetParamSize(sizer, p.GetGUID()); | |
| 818 } | |
| 819 } | |
| 820 | |
| 821 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | |
| 822 const param_type& p) { | |
| 823 // This serialization must be kept in sync with | |
| 824 // nacl_ipc_adapater.cc:WriteHandle(). | |
| 825 const bool valid = p.IsValid(); | |
| 826 WriteParam(m, valid); | |
| 827 | |
| 828 if (!valid) | |
| 829 return; | |
| 830 | |
| 831 if (p.OwnershipPassesToIPC()) { | 714 if (p.OwnershipPassesToIPC()) { |
| 832 if (!m->WriteAttachment(new internal::PlatformFileAttachment( | 715 if (!m->WriteAttachment(new internal::PlatformFileAttachment( |
| 833 base::ScopedFD(p.GetHandle())))) | 716 base::ScopedFD(p.GetHandle())))) |
| 834 NOTREACHED(); | 717 NOTREACHED(); |
| 835 } else { | 718 } else { |
| 836 if (!m->WriteAttachment( | 719 if (!m->WriteAttachment( |
| 837 new internal::PlatformFileAttachment(p.GetHandle()))) | 720 new internal::PlatformFileAttachment(p.GetHandle()))) |
| 838 NOTREACHED(); | 721 NOTREACHED(); |
| 839 } | 722 } |
| 723 #endif |
| 724 |
| 725 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
| 726 // If the caller intended to pass ownership to the IPC stack, release a |
| 727 // reference. |
| 728 if (p.OwnershipPassesToIPC()) |
| 729 p.Close(); |
| 730 #endif |
| 731 |
| 840 DCHECK(!p.GetGUID().is_empty()); | 732 DCHECK(!p.GetGUID().is_empty()); |
| 841 WriteParam(m, p.GetGUID()); | 733 WriteParam(m, p.GetGUID()); |
| 734 WriteParam(m, static_cast<uint64_t>(p.GetSize())); |
| 842 } | 735 } |
| 843 | 736 |
| 844 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | 737 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, |
| 845 base::PickleIterator* iter, | 738 base::PickleIterator* iter, |
| 846 param_type* r) { | 739 param_type* r) { |
| 847 *r = base::SharedMemoryHandle(); | 740 *r = base::SharedMemoryHandle(); |
| 848 | 741 |
| 849 bool valid; | 742 bool valid; |
| 850 if (!ReadParam(m, iter, &valid)) | 743 if (!ReadParam(m, iter, &valid)) |
| 851 return false; | 744 return false; |
| 852 | |
| 853 if (!valid) | 745 if (!valid) |
| 854 return true; | 746 return true; |
| 855 | 747 |
| 748 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 749 MachPortMac mach_port_mac; |
| 750 if (!ReadParam(m, iter, &mach_port_mac)) |
| 751 return false; |
| 752 #elif defined(OS_WIN) |
| 753 HandleWin handle_win; |
| 754 if (!ReadParam(m, iter, &handle_win)) |
| 755 return false; |
| 756 #else |
| 856 scoped_refptr<base::Pickle::Attachment> attachment; | 757 scoped_refptr<base::Pickle::Attachment> attachment; |
| 857 if (!m->ReadAttachment(iter, &attachment)) | 758 if (!m->ReadAttachment(iter, &attachment)) |
| 858 return false; | 759 return false; |
| 859 | 760 |
| 860 if (static_cast<MessageAttachment*>(attachment.get())->GetType() != | 761 if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 861 MessageAttachment::Type::PLATFORM_FILE) { | 762 MessageAttachment::Type::PLATFORM_FILE) { |
| 862 return false; | 763 return false; |
| 863 } | 764 } |
| 765 #endif |
| 864 | 766 |
| 865 base::UnguessableToken guid; | 767 base::UnguessableToken guid; |
| 866 if (!ReadParam(m, iter, &guid)) | 768 uint64_t size; |
| 769 if (!ReadParam(m, iter, &guid) || !ReadParam(m, iter, &size)) { |
| 867 return false; | 770 return false; |
| 771 } |
| 868 | 772 |
| 773 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 774 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(), |
| 775 static_cast<size_t>(size), guid); |
| 776 #elif defined(OS_WIN) |
| 777 *r = base::SharedMemoryHandle(handle_win.get_handle(), |
| 778 static_cast<size_t>(size), guid); |
| 779 #else |
| 869 *r = base::SharedMemoryHandle( | 780 *r = base::SharedMemoryHandle( |
| 870 base::FileDescriptor( | 781 base::FileDescriptor( |
| 871 static_cast<internal::PlatformFileAttachment*>(attachment.get()) | 782 static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 872 ->TakePlatformFile(), | 783 ->TakePlatformFile(), |
| 873 true), | 784 true), |
| 874 guid); | 785 static_cast<size_t>(size), guid); |
| 786 #endif |
| 787 |
| 875 return true; | 788 return true; |
| 876 } | 789 } |
| 877 | 790 |
| 878 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | 791 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, |
| 879 std::string* l) { | 792 std::string* l) { |
| 880 if (p.OwnershipPassesToIPC()) { | 793 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 881 l->append(base::StringPrintf("FD(%d auto-close)", p.GetHandle())); | 794 l->append("Mach port: "); |
| 882 } else { | 795 LogParam(p.GetMemoryObject(), l); |
| 883 l->append(base::StringPrintf("FD(%d)", p.GetHandle())); | 796 #elif defined(OS_WIN) |
| 884 } | 797 l->append("HANDLE: "); |
| 798 LogParam(p.GetHandle(), l); |
| 799 #else |
| 800 l->append("FD: "); |
| 801 LogParam(p.GetHandle(), l); |
| 802 #endif |
| 803 |
| 885 l->append("GUID: "); | 804 l->append("GUID: "); |
| 886 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | 805 LogParam(p.GetGUID(), l); |
| 806 l->append("size: "); |
| 807 LogParam(static_cast<uint64_t>(p.GetSize()), l); |
| 887 } | 808 } |
| 888 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
| 889 | 809 |
| 890 #if defined(OS_WIN) | 810 #if defined(OS_WIN) |
| 891 void ParamTraits<PlatformFileForTransit>::GetSize(base::PickleSizer* s, | 811 void ParamTraits<PlatformFileForTransit>::GetSize(base::PickleSizer* s, |
| 892 const param_type& p) { | 812 const param_type& p) { |
| 893 GetParamSize(s, p.IsValid()); | 813 GetParamSize(s, p.IsValid()); |
| 894 if (p.IsValid()) | 814 if (p.IsValid()) |
| 895 GetParamSize(s, p.GetHandle()); | 815 GetParamSize(s, p.GetHandle()); |
| 896 } | 816 } |
| 897 | 817 |
| 898 void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m, | 818 void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m, |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1136 void ParamTraits<base::UnguessableToken>::GetSize(base::PickleSizer* sizer, | 1056 void ParamTraits<base::UnguessableToken>::GetSize(base::PickleSizer* sizer, |
| 1137 const param_type& p) { | 1057 const param_type& p) { |
| 1138 sizer->AddBytes(2 * sizeof(uint64_t)); | 1058 sizer->AddBytes(2 * sizeof(uint64_t)); |
| 1139 } | 1059 } |
| 1140 | 1060 |
| 1141 void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m, | 1061 void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m, |
| 1142 const param_type& p) { | 1062 const param_type& p) { |
| 1143 DCHECK(!p.is_empty()); | 1063 DCHECK(!p.is_empty()); |
| 1144 | 1064 |
| 1145 // This serialization must be kept in sync with | 1065 // This serialization must be kept in sync with |
| 1146 // nacl_ipc_adapater.cc:WriteHandle(). | 1066 // nacl_message_scanner.cc:WriteHandle(). |
| 1147 ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization()); | 1067 ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization()); |
| 1148 ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization()); | 1068 ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization()); |
| 1149 } | 1069 } |
| 1150 | 1070 |
| 1151 bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m, | 1071 bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m, |
| 1152 base::PickleIterator* iter, | 1072 base::PickleIterator* iter, |
| 1153 param_type* r) { | 1073 param_type* r) { |
| 1154 uint64_t high, low; | 1074 uint64_t high, low; |
| 1155 if (!ParamTraits<uint64_t>::Read(m, iter, &high) || | 1075 if (!ParamTraits<uint64_t>::Read(m, iter, &high) || |
| 1156 !ParamTraits<uint64_t>::Read(m, iter, &low)) | 1076 !ParamTraits<uint64_t>::Read(m, iter, &low)) |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1377 return result; | 1297 return result; |
| 1378 } | 1298 } |
| 1379 | 1299 |
| 1380 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { | 1300 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 1381 l->append("<MSG>"); | 1301 l->append("<MSG>"); |
| 1382 } | 1302 } |
| 1383 | 1303 |
| 1384 #endif // OS_WIN | 1304 #endif // OS_WIN |
| 1385 | 1305 |
| 1386 } // namespace IPC | 1306 } // namespace IPC |
| OLD | NEW |