OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdio.h> | 5 #include <stdio.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 V(MojoDataPipe_Create, 3) \ | 30 V(MojoDataPipe_Create, 3) \ |
31 V(MojoDataPipe_WriteData, 4) \ | 31 V(MojoDataPipe_WriteData, 4) \ |
32 V(MojoDataPipe_BeginWriteData, 3) \ | 32 V(MojoDataPipe_BeginWriteData, 3) \ |
33 V(MojoDataPipe_EndWriteData, 2) \ | 33 V(MojoDataPipe_EndWriteData, 2) \ |
34 V(MojoDataPipe_ReadData, 4) \ | 34 V(MojoDataPipe_ReadData, 4) \ |
35 V(MojoDataPipe_BeginReadData, 3) \ | 35 V(MojoDataPipe_BeginReadData, 3) \ |
36 V(MojoDataPipe_EndReadData, 2) \ | 36 V(MojoDataPipe_EndReadData, 2) \ |
37 V(MojoMessagePipe_Create, 1) \ | 37 V(MojoMessagePipe_Create, 1) \ |
38 V(MojoMessagePipe_Write, 5) \ | 38 V(MojoMessagePipe_Write, 5) \ |
39 V(MojoMessagePipe_Read, 5) \ | 39 V(MojoMessagePipe_Read, 5) \ |
| 40 V(MojoMessagePipe_QueryAndRead, 3) \ |
40 V(Mojo_GetTimeTicksNow, 0) \ | 41 V(Mojo_GetTimeTicksNow, 0) \ |
41 V(MojoHandle_Close, 1) \ | 42 V(MojoHandle_Close, 1) \ |
42 V(MojoHandle_Wait, 3) \ | 43 V(MojoHandle_Wait, 3) \ |
43 V(MojoHandle_RegisterFinalizer, 2) \ | 44 V(MojoHandle_RegisterFinalizer, 2) \ |
44 V(MojoHandle_WaitMany, 3) \ | 45 V(MojoHandle_WaitMany, 3) \ |
45 V(MojoHandleWatcher_GrowStateArrays, 1) \ | 46 V(MojoHandleWatcher_GrowStateArrays, 1) \ |
46 V(MojoHandleWatcher_WaitMany, 2) \ | 47 V(MojoHandleWatcher_WaitMany, 2) \ |
47 V(MojoHandleWatcher_SendControlData, 4) \ | 48 V(MojoHandleWatcher_SendControlData, 4) \ |
48 V(MojoHandleWatcher_RecvControlData, 1) \ | 49 V(MojoHandleWatcher_RecvControlData, 1) \ |
49 V(MojoHandleWatcher_SetControlHandle, 1) \ | 50 V(MojoHandleWatcher_SetControlHandle, 1) \ |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 } | 712 } |
712 } | 713 } |
713 | 714 |
714 Dart_Handle list = Dart_NewList(3); | 715 Dart_Handle list = Dart_NewList(3); |
715 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | 716 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); |
716 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); | 717 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); |
717 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); | 718 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); |
718 Dart_SetReturnValue(arguments, list); | 719 Dart_SetReturnValue(arguments, list); |
719 } | 720 } |
720 | 721 |
| 722 void ByteArrayFinalizer(void* isolate_callback_data, |
| 723 Dart_WeakPersistentHandle handle, |
| 724 void* peer) { |
| 725 uint8_t* byte_array = reinterpret_cast<uint8_t*>(peer); |
| 726 delete[] byte_array; |
| 727 } |
| 728 |
| 729 void HandleArrayFinalizer(void* isolate_callback_data, |
| 730 Dart_WeakPersistentHandle handle, |
| 731 void* peer) { |
| 732 uint32_t* handle_array = reinterpret_cast<uint32_t*>(peer); |
| 733 delete[] handle_array; |
| 734 } |
| 735 |
| 736 void MojoMessagePipe_QueryAndRead(Dart_NativeArguments arguments) { |
| 737 Dart_Handle err; |
| 738 int64_t dart_handle; |
| 739 int64_t flags = 0; |
| 740 CHECK_INTEGER_ARGUMENT(arguments, 0, &dart_handle, Null); |
| 741 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); |
| 742 Dart_Handle result = Dart_GetNativeArgument(arguments, 2); |
| 743 |
| 744 Dart_Handle data = Dart_ListGetAt(result, 1); |
| 745 Dart_Handle handles = Dart_ListGetAt(result, 2); |
| 746 |
| 747 // Query the number of bytes and handles available. |
| 748 uint32_t blen = 0; |
| 749 uint32_t hlen = 0; |
| 750 MojoResult res = |
| 751 MojoReadMessage(static_cast<MojoHandle>(dart_handle), nullptr, &blen, |
| 752 nullptr, &hlen, static_cast<MojoReadMessageFlags>(flags)); |
| 753 |
| 754 if ((res != MOJO_RESULT_OK) && (res != MOJO_RESULT_RESOURCE_EXHAUSTED)) { |
| 755 Dart_ListSetAt(result, 0, Dart_NewInteger(res)); |
| 756 Dart_ListSetAt(result, 1, data); |
| 757 Dart_ListSetAt(result, 2, handles); |
| 758 Dart_ListSetAt(result, 3, Dart_NewInteger(0)); |
| 759 Dart_ListSetAt(result, 4, Dart_NewInteger(0)); |
| 760 return; |
| 761 } |
| 762 |
| 763 Dart_TypedData_Type typ; |
| 764 void* bytes = nullptr; |
| 765 intptr_t bytes_len = 0; |
| 766 if ((blen > 0) && Dart_IsNull(data)) { |
| 767 uint8_t* new_byte_data = new uint8_t[blen]; |
| 768 data = Dart_NewExternalTypedData(Dart_TypedData_kByteData, new_byte_data, |
| 769 blen); |
| 770 MOJO_DCHECK(!Dart_IsError(data)); |
| 771 Dart_NewWeakPersistentHandle(data, new_byte_data, blen, ByteArrayFinalizer); |
| 772 } else if (blen > 0) { |
| 773 err = Dart_TypedDataAcquireData(data, &typ, &bytes, &bytes_len); |
| 774 MOJO_DCHECK(!Dart_IsError(err)); |
| 775 err = Dart_TypedDataReleaseData(data); |
| 776 MOJO_DCHECK(!Dart_IsError(err)); |
| 777 if (static_cast<uintptr_t>(bytes_len) < blen) { |
| 778 uint8_t* new_byte_data = new uint8_t[blen]; |
| 779 data = Dart_NewExternalTypedData(Dart_TypedData_kByteData, new_byte_data, |
| 780 blen); |
| 781 MOJO_DCHECK(!Dart_IsError(data)); |
| 782 Dart_NewWeakPersistentHandle(data, new_byte_data, blen, |
| 783 ByteArrayFinalizer); |
| 784 } |
| 785 } |
| 786 |
| 787 void* handle_bytes = nullptr; |
| 788 intptr_t handles_len = 0; |
| 789 if ((hlen > 0) && Dart_IsNull(handles)) { |
| 790 uint32_t* new_handle_data = new uint32_t[hlen]; |
| 791 handles = Dart_NewExternalTypedData(Dart_TypedData_kUint32, new_handle_data, |
| 792 hlen); |
| 793 MOJO_DCHECK(!Dart_IsError(handles)); |
| 794 Dart_NewWeakPersistentHandle(handles, new_handle_data, |
| 795 hlen * sizeof(uint32_t), HandleArrayFinalizer); |
| 796 } else if (hlen > 0) { |
| 797 err = Dart_TypedDataAcquireData(handles, &typ, &handle_bytes, &handles_len); |
| 798 MOJO_DCHECK(!Dart_IsError(err)); |
| 799 err = Dart_TypedDataReleaseData(handles); |
| 800 MOJO_DCHECK(!Dart_IsError(err)); |
| 801 if (static_cast<uintptr_t>(handles_len) < hlen) { |
| 802 uint32_t* new_handle_data = new uint32_t[hlen]; |
| 803 handles = Dart_NewExternalTypedData(Dart_TypedData_kUint32, |
| 804 new_handle_data, hlen); |
| 805 MOJO_DCHECK(!Dart_IsError(handles)); |
| 806 Dart_NewWeakPersistentHandle(handles, new_handle_data, |
| 807 hlen * sizeof(uint32_t), |
| 808 HandleArrayFinalizer); |
| 809 } |
| 810 } |
| 811 |
| 812 if (blen > 0) { |
| 813 err = Dart_TypedDataAcquireData(data, &typ, &bytes, &bytes_len); |
| 814 MOJO_DCHECK(!Dart_IsError(err)); |
| 815 } |
| 816 |
| 817 if (hlen > 0) { |
| 818 err = Dart_TypedDataAcquireData(handles, &typ, &handle_bytes, &handles_len); |
| 819 MOJO_DCHECK(!Dart_IsError(err)); |
| 820 } |
| 821 |
| 822 res = MojoReadMessage(static_cast<MojoHandle>(dart_handle), bytes, &blen, |
| 823 reinterpret_cast<MojoHandle*>(handle_bytes), &hlen, |
| 824 static_cast<MojoReadMessageFlags>(flags)); |
| 825 |
| 826 if (blen > 0) { |
| 827 err = Dart_TypedDataReleaseData(data); |
| 828 MOJO_DCHECK(!Dart_IsError(err)); |
| 829 } |
| 830 |
| 831 if (hlen > 0) { |
| 832 err = Dart_TypedDataReleaseData(handles); |
| 833 MOJO_DCHECK(!Dart_IsError(err)); |
| 834 } |
| 835 |
| 836 Dart_ListSetAt(result, 0, Dart_NewInteger(res)); |
| 837 Dart_ListSetAt(result, 1, data); |
| 838 Dart_ListSetAt(result, 2, handles); |
| 839 Dart_ListSetAt(result, 3, Dart_NewInteger(blen)); |
| 840 Dart_ListSetAt(result, 4, Dart_NewInteger(hlen)); |
| 841 } |
| 842 |
721 struct MojoWaitManyState { | 843 struct MojoWaitManyState { |
722 MojoWaitManyState() {} | 844 MojoWaitManyState() {} |
723 | 845 |
724 std::vector<uint32_t> handles; | 846 std::vector<uint32_t> handles; |
725 std::vector<uint32_t> signals; | 847 std::vector<uint32_t> signals; |
726 std::vector<uint32_t> out_index; | 848 std::vector<uint32_t> out_index; |
727 std::vector<MojoHandleSignalsState> out_signals; | 849 std::vector<MojoHandleSignalsState> out_signals; |
728 | 850 |
729 static MojoWaitManyState* GetInstance(); | 851 static MojoWaitManyState* GetInstance(); |
730 | 852 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
893 mojo_control_handle = control_handle; | 1015 mojo_control_handle = control_handle; |
894 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 1016 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
895 } | 1017 } |
896 | 1018 |
897 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 1019 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
898 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 1020 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
899 } | 1021 } |
900 | 1022 |
901 } // namespace dart | 1023 } // namespace dart |
902 } // namespace mojo | 1024 } // namespace mojo |
OLD | NEW |