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