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 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
671 } else { | 671 } else { |
672 l->append(base::StringPrintf("FD(%d)", p.fd)); | 672 l->append(base::StringPrintf("FD(%d)", p.fd)); |
673 } | 673 } |
674 } | 674 } |
675 #endif // defined(OS_POSIX) | 675 #endif // defined(OS_POSIX) |
676 | 676 |
677 #if defined(OS_MACOSX) && !defined(OS_IOS) | 677 #if defined(OS_MACOSX) && !defined(OS_IOS) |
678 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, | 678 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, |
679 const param_type& p) { | 679 const param_type& p) { |
680 GetParamSize(sizer, p.GetMemoryObject()); | 680 GetParamSize(sizer, p.GetMemoryObject()); |
681 uint32_t dummy = 0; | 681 ParamTraits<base::UnguessableToken>::GetSize(sizer, p.GetGUID()); |
682 | |
683 // Size of p.GetSize(). | |
684 uint64_t dummy = 0; | |
685 static_assert(sizeof(dummy) == sizeof(size_t), | |
686 "The dummy size must match the size written in ::Write"); | |
682 GetParamSize(sizer, dummy); | 687 GetParamSize(sizer, dummy); |
683 } | 688 } |
684 | 689 |
685 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | 690 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, |
686 const param_type& p) { | 691 const param_type& p) { |
687 MachPortMac mach_port_mac(p.GetMemoryObject()); | 692 MachPortMac mach_port_mac(p.GetMemoryObject()); |
688 ParamTraits<MachPortMac>::Write(m, mach_port_mac); | 693 ParamTraits<MachPortMac>::Write(m, mach_port_mac); |
694 ParamTraits<base::UnguessableToken>::Write(m, p.GetGUID()); | |
Will Harris
2017/05/04 17:50:51
should there be a DCHECK here that guid is not emp
erikchen
2017/05/04 19:21:13
Yes, done.
| |
695 | |
689 size_t size = 0; | 696 size_t size = 0; |
690 bool result = p.GetSize(&size); | 697 bool result = p.GetSize(&size); |
691 DCHECK(result); | 698 DCHECK(result); |
692 ParamTraits<uint32_t>::Write(m, static_cast<uint32_t>(size)); | 699 ParamTraits<uint32_t>::Write(m, static_cast<uint32_t>(size)); |
693 | 700 |
694 // If the caller intended to pass ownership to the IPC stack, release a | 701 // If the caller intended to pass ownership to the IPC stack, release a |
695 // reference. | 702 // reference. |
696 if (p.OwnershipPassesToIPC()) | 703 if (p.OwnershipPassesToIPC()) |
697 p.Close(); | 704 p.Close(); |
698 } | 705 } |
699 | 706 |
700 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | 707 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, |
701 base::PickleIterator* iter, | 708 base::PickleIterator* iter, |
702 param_type* r) { | 709 param_type* r) { |
703 MachPortMac mach_port_mac; | 710 MachPortMac mach_port_mac; |
704 if (!ParamTraits<MachPortMac>::Read(m, iter, &mach_port_mac)) | 711 if (!ParamTraits<MachPortMac>::Read(m, iter, &mach_port_mac)) |
705 return false; | 712 return false; |
706 | 713 |
714 base::UnguessableToken guid; | |
715 if (!ParamTraits<base::UnguessableToken>::Read(m, iter, &guid)) | |
Ken Rockot(use gerrit already)
2017/05/04 17:54:50
We have templated helper functions for this in ipc
erikchen
2017/05/04 19:21:13
Done.
| |
716 return false; | |
717 | |
707 uint32_t size; | 718 uint32_t size; |
708 if (!ParamTraits<uint32_t>::Read(m, iter, &size)) | 719 if (!ParamTraits<uint32_t>::Read(m, iter, &size)) |
709 return false; | 720 return false; |
710 | 721 |
711 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(), | 722 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(), |
712 static_cast<size_t>(size)); | 723 static_cast<size_t>(size), guid); |
713 return true; | 724 return true; |
714 } | 725 } |
715 | 726 |
716 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | 727 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, |
717 std::string* l) { | 728 std::string* l) { |
718 l->append("Mach port: "); | 729 l->append("Mach port: "); |
719 LogParam(p.GetMemoryObject(), l); | 730 LogParam(p.GetMemoryObject(), l); |
731 l->append("GUID: "); | |
732 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | |
720 } | 733 } |
721 | 734 |
722 #elif defined(OS_WIN) | 735 #elif defined(OS_WIN) |
723 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* s, | 736 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* s, |
724 const param_type& p) { | 737 const param_type& p) { |
725 GetParamSize(s, p.IsValid()); | 738 GetParamSize(s, p.IsValid()); |
726 if (p.IsValid()) | 739 if (p.IsValid()) { |
727 GetParamSize(s, p.GetHandle()); | 740 GetParamSize(s, p.GetHandle()); |
741 ParamTraits<base::UnguessableToken>::GetSize(sizer, p.GetGUID()); | |
742 } | |
728 } | 743 } |
729 | 744 |
730 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | 745 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, |
731 const param_type& p) { | 746 const param_type& p) { |
732 const bool valid = p.IsValid(); | 747 const bool valid = p.IsValid(); |
733 WriteParam(m, valid); | 748 WriteParam(m, valid); |
734 | 749 |
735 if (!valid) | 750 if (!valid) |
736 return; | 751 return; |
737 | 752 |
738 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE); | 753 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE); |
739 ParamTraits<HandleWin>::Write(m, handle_win); | 754 ParamTraits<HandleWin>::Write(m, handle_win); |
740 | 755 |
741 // If the caller intended to pass ownership to the IPC stack, release a | 756 // If the caller intended to pass ownership to the IPC stack, release a |
742 // reference. | 757 // reference. |
743 if (p.OwnershipPassesToIPC()) | 758 if (p.OwnershipPassesToIPC()) |
744 p.Close(); | 759 p.Close(); |
760 | |
761 ParamTraits<base::UnguessableToken>::Write(m, p.GetGUID()); | |
745 } | 762 } |
746 | 763 |
747 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | 764 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, |
748 base::PickleIterator* iter, | 765 base::PickleIterator* iter, |
749 param_type* r) { | 766 param_type* r) { |
750 *r = base::SharedMemoryHandle(); | 767 *r = base::SharedMemoryHandle(); |
751 | 768 |
752 bool valid; | 769 bool valid; |
753 if (!ReadParam(m, iter, &valid)) | 770 if (!ReadParam(m, iter, &valid)) |
754 return false; | 771 return false; |
755 if (!valid) | 772 if (!valid) |
756 return true; | 773 return true; |
757 | 774 |
758 HandleWin handle_win; | 775 HandleWin handle_win; |
759 if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win)) | 776 if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win)) |
760 return false; | 777 return false; |
761 *r = base::SharedMemoryHandle(handle_win.get_handle()); | 778 |
779 base::UnguessableToken guid; | |
780 if (!ParamTraits<base::UnguessableToken>::Read(m, iter, &guid)) | |
781 return false; | |
782 | |
783 *r = base::SharedMemoryHandle(handle_win.get_handle(), guid); | |
762 return true; | 784 return true; |
763 } | 785 } |
764 | 786 |
765 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | 787 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, |
766 std::string* l) { | 788 std::string* l) { |
789 l->append("HANDLE: "); | |
767 LogParam(p.GetHandle(), l); | 790 LogParam(p.GetHandle(), l); |
791 l->append("GUID: "); | |
792 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | |
768 } | 793 } |
769 #elif defined(OS_POSIX) | 794 #elif defined(OS_POSIX) |
770 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, | 795 void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer, |
771 const param_type& p) { | 796 const param_type& p) { |
772 GetParamSize(sizer, p.IsValid()); | 797 GetParamSize(sizer, p.IsValid()); |
773 if (p.IsValid()) | 798 if (p.IsValid()) { |
774 sizer->AddAttachment(); | 799 sizer->AddAttachment(); |
800 ParamTraits<base::UnguessableToken>::GetSize(sizer, p.GetGUID()); | |
801 } | |
775 } | 802 } |
776 | 803 |
777 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, | 804 void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, |
778 const param_type& p) { | 805 const param_type& p) { |
779 const bool valid = p.IsValid(); | 806 const bool valid = p.IsValid(); |
780 WriteParam(m, valid); | 807 WriteParam(m, valid); |
781 | 808 |
782 if (!valid) | 809 if (!valid) |
783 return; | 810 return; |
784 | 811 |
785 if (p.OwnershipPassesToIPC()) { | 812 if (p.OwnershipPassesToIPC()) { |
786 if (!m->WriteAttachment(new internal::PlatformFileAttachment( | 813 if (!m->WriteAttachment(new internal::PlatformFileAttachment( |
787 base::ScopedFD(p.GetHandle())))) | 814 base::ScopedFD(p.GetHandle())))) |
788 NOTREACHED(); | 815 NOTREACHED(); |
789 } else { | 816 } else { |
790 if (!m->WriteAttachment( | 817 if (!m->WriteAttachment( |
791 new internal::PlatformFileAttachment(p.GetHandle()))) | 818 new internal::PlatformFileAttachment(p.GetHandle()))) |
792 NOTREACHED(); | 819 NOTREACHED(); |
793 } | 820 } |
821 ParamTraits<base::UnguessableToken>::Write(m, p.GetGUID()); | |
794 } | 822 } |
795 | 823 |
796 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, | 824 bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, |
797 base::PickleIterator* iter, | 825 base::PickleIterator* iter, |
798 param_type* r) { | 826 param_type* r) { |
799 *r = base::SharedMemoryHandle(); | 827 *r = base::SharedMemoryHandle(); |
800 | 828 |
801 bool valid; | 829 bool valid; |
802 if (!ReadParam(m, iter, &valid)) | 830 if (!ReadParam(m, iter, &valid)) |
803 return false; | 831 return false; |
804 | 832 |
805 if (!valid) | 833 if (!valid) |
806 return true; | 834 return true; |
807 | 835 |
808 scoped_refptr<base::Pickle::Attachment> attachment; | 836 scoped_refptr<base::Pickle::Attachment> attachment; |
809 if (!m->ReadAttachment(iter, &attachment)) | 837 if (!m->ReadAttachment(iter, &attachment)) |
810 return false; | 838 return false; |
811 | 839 |
812 if (static_cast<MessageAttachment*>(attachment.get())->GetType() != | 840 if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
813 MessageAttachment::Type::PLATFORM_FILE) { | 841 MessageAttachment::Type::PLATFORM_FILE) { |
814 return false; | 842 return false; |
815 } | 843 } |
816 | 844 |
817 *r = base::SharedMemoryHandle(base::FileDescriptor( | 845 base::UnguessableToken guid; |
818 static_cast<internal::PlatformFileAttachment*>(attachment.get()) | 846 if (!ParamTraits<base::UnguessableToken>::Read(m, iter, &guid)) |
819 ->TakePlatformFile(), | 847 return false; |
820 true)); | 848 |
849 *r = base::SharedMemoryHandle( | |
850 base::FileDescriptor( | |
851 static_cast<internal::PlatformFileAttachment*>(attachment.get()) | |
852 ->TakePlatformFile(), | |
853 true), | |
854 guid); | |
821 return true; | 855 return true; |
822 } | 856 } |
823 | 857 |
824 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, | 858 void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, |
825 std::string* l) { | 859 std::string* l) { |
826 if (p.OwnershipPassesToIPC()) { | 860 if (p.OwnershipPassesToIPC()) { |
827 l->append(base::StringPrintf("FD(%d auto-close)", p.GetHandle())); | 861 l->append(base::StringPrintf("FD(%d auto-close)", p.GetHandle())); |
828 } else { | 862 } else { |
829 l->append(base::StringPrintf("FD(%d)", p.GetHandle())); | 863 l->append(base::StringPrintf("FD(%d)", p.GetHandle())); |
830 } | 864 } |
865 l->append("GUID: "); | |
866 ParamTraits<base::UnguessableToken>::Log(p.GetGUID(), l); | |
831 } | 867 } |
832 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 868 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
833 | 869 |
834 #if defined(OS_WIN) | 870 #if defined(OS_WIN) |
835 void ParamTraits<PlatformFileForTransit>::GetSize(base::PickleSizer* s, | 871 void ParamTraits<PlatformFileForTransit>::GetSize(base::PickleSizer* s, |
836 const param_type& p) { | 872 const param_type& p) { |
837 GetParamSize(s, p.IsValid()); | 873 GetParamSize(s, p.IsValid()); |
838 if (p.IsValid()) | 874 if (p.IsValid()) |
839 GetParamSize(s, p.GetHandle()); | 875 GetParamSize(s, p.GetHandle()); |
840 } | 876 } |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1319 return result; | 1355 return result; |
1320 } | 1356 } |
1321 | 1357 |
1322 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { | 1358 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
1323 l->append("<MSG>"); | 1359 l->append("<MSG>"); |
1324 } | 1360 } |
1325 | 1361 |
1326 #endif // OS_WIN | 1362 #endif // OS_WIN |
1327 | 1363 |
1328 } // namespace IPC | 1364 } // namespace IPC |
OLD | NEW |