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 <set> | |
10 #include <vector> | 9 #include <vector> |
11 | 10 |
12 #include "dart/runtime/include/dart_api.h" | 11 #include "dart/runtime/include/dart_api.h" |
13 #include "mojo/dart/embedder/builtin.h" | 12 #include "mojo/dart/embedder/builtin.h" |
14 #include "mojo/dart/embedder/mojo_dart_state.h" | |
15 #include "mojo/public/c/system/core.h" | 13 #include "mojo/public/c/system/core.h" |
16 #include "mojo/public/cpp/environment/logging.h" | 14 #include "mojo/public/cpp/environment/logging.h" |
17 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
18 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
19 | 17 |
20 namespace mojo { | 18 namespace mojo { |
21 namespace dart { | 19 namespace dart { |
22 | 20 |
23 #define MOJO_NATIVE_LIST(V) \ | 21 #define MOJO_NATIVE_LIST(V) \ |
24 V(MojoSharedBuffer_Create, 2) \ | 22 V(MojoSharedBuffer_Create, 2) \ |
25 V(MojoSharedBuffer_Duplicate, 2) \ | 23 V(MojoSharedBuffer_Duplicate, 2) \ |
26 V(MojoSharedBuffer_Map, 5) \ | 24 V(MojoSharedBuffer_Map, 5) \ |
27 V(MojoSharedBuffer_Unmap, 1) \ | 25 V(MojoSharedBuffer_Unmap, 1) \ |
28 V(MojoDataPipe_Create, 3) \ | 26 V(MojoDataPipe_Create, 3) \ |
29 V(MojoDataPipe_WriteData, 4) \ | 27 V(MojoDataPipe_WriteData, 4) \ |
30 V(MojoDataPipe_BeginWriteData, 3) \ | 28 V(MojoDataPipe_BeginWriteData, 3) \ |
31 V(MojoDataPipe_EndWriteData, 2) \ | 29 V(MojoDataPipe_EndWriteData, 2) \ |
32 V(MojoDataPipe_ReadData, 4) \ | 30 V(MojoDataPipe_ReadData, 4) \ |
33 V(MojoDataPipe_BeginReadData, 3) \ | 31 V(MojoDataPipe_BeginReadData, 3) \ |
34 V(MojoDataPipe_EndReadData, 2) \ | 32 V(MojoDataPipe_EndReadData, 2) \ |
35 V(MojoMessagePipe_Create, 1) \ | 33 V(MojoMessagePipe_Create, 1) \ |
36 V(MojoMessagePipe_Write, 5) \ | 34 V(MojoMessagePipe_Write, 5) \ |
37 V(MojoMessagePipe_Read, 5) \ | 35 V(MojoMessagePipe_Read, 5) \ |
38 V(Mojo_GetTimeTicksNow, 0) \ | 36 V(Mojo_GetTimeTicksNow, 0) \ |
39 V(MojoHandle_Close, 1) \ | 37 V(MojoHandle_Close, 1) \ |
40 V(MojoHandle_Wait, 3) \ | 38 V(MojoHandle_Wait, 3) \ |
41 V(MojoHandle_Register, 2) \ | 39 V(MojoHandle_RegisterFinalizer, 2) \ |
42 V(MojoHandle_WaitMany, 3) \ | 40 V(MojoHandle_WaitMany, 3) \ |
43 V(MojoHandleWatcher_GrowStateArrays, 1) \ | 41 V(MojoHandleWatcher_GrowStateArrays, 1) \ |
44 V(MojoHandleWatcher_WaitMany, 2) \ | 42 V(MojoHandleWatcher_WaitMany, 2) \ |
45 V(MojoHandleWatcher_SendControlData, 4) \ | 43 V(MojoHandleWatcher_SendControlData, 4) \ |
46 V(MojoHandleWatcher_RecvControlData, 1) \ | 44 V(MojoHandleWatcher_RecvControlData, 1) \ |
47 V(MojoHandleWatcher_SetControlHandle, 1) \ | 45 V(MojoHandleWatcher_SetControlHandle, 1) \ |
48 V(MojoHandleWatcher_GetControlHandle, 0) | 46 V(MojoHandleWatcher_GetControlHandle, 0) |
49 | 47 |
50 MOJO_NATIVE_LIST(DECLARE_FUNCTION); | 48 MOJO_NATIVE_LIST(DECLARE_FUNCTION); |
51 | 49 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 // the handle has leaked from the Dart code, which is an error. | 128 // the handle has leaked from the Dart code, which is an error. |
131 MOJO_LOG(ERROR) << "Handle Finalizer closing handle:\n\tisolate: " | 129 MOJO_LOG(ERROR) << "Handle Finalizer closing handle:\n\tisolate: " |
132 << "\n\thandle: " << callback_peer->handle; | 130 << "\n\thandle: " << callback_peer->handle; |
133 } | 131 } |
134 } | 132 } |
135 delete callback_peer; | 133 delete callback_peer; |
136 } | 134 } |
137 | 135 |
138 // Setup a weak persistent handle for a MojoHandle that calls MojoClose on the | 136 // Setup a weak persistent handle for a MojoHandle that calls MojoClose on the |
139 // handle when the MojoHandle is GC'd or the VM is going down. | 137 // handle when the MojoHandle is GC'd or the VM is going down. |
140 void MojoHandle_Register(Dart_NativeArguments arguments) { | 138 void MojoHandle_RegisterFinalizer(Dart_NativeArguments arguments) { |
141 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | 139 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); |
142 if (!Dart_IsInstance(mojo_handle_instance)) { | 140 if (!Dart_IsInstance(mojo_handle_instance)) { |
143 SetInvalidArgumentReturn(arguments); | 141 SetInvalidArgumentReturn(arguments); |
144 return; | 142 return; |
145 } | 143 } |
146 | 144 |
147 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | 145 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); |
148 CHECK_INTEGER_ARGUMENT(arguments, 1, &raw_handle, InvalidArgument); | 146 CHECK_INTEGER_ARGUMENT(arguments, 1, &raw_handle, InvalidArgument); |
149 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | 147 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { |
150 SetInvalidArgumentReturn(arguments); | 148 SetInvalidArgumentReturn(arguments); |
151 return; | 149 return; |
152 } | 150 } |
153 | 151 |
154 // Add the handle to this isolate's set. | 152 // Add the handle to this isolate's set. |
155 MojoHandle handle = static_cast<MojoHandle>(raw_handle); | 153 MojoHandle handle = static_cast<MojoHandle>(raw_handle); |
156 auto isolate_data = MojoDartState::Current(); | |
157 assert(isolate_data != nullptr); | |
158 isolate_data->unclosed_handles().insert(handle); | |
159 | 154 |
160 // Set up a finalizer. | 155 // Set up a finalizer. |
161 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); | 156 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); |
162 callback_peer->handle = handle; | 157 callback_peer->handle = handle; |
163 Dart_NewWeakPersistentHandle(mojo_handle_instance, | 158 Dart_NewWeakPersistentHandle(mojo_handle_instance, |
164 reinterpret_cast<void*>(callback_peer), | 159 reinterpret_cast<void*>(callback_peer), |
165 sizeof(CloserCallbackPeer), | 160 sizeof(CloserCallbackPeer), |
166 MojoHandleCloserCallback); | 161 MojoHandleCloserCallback); |
167 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 162 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
168 } | 163 } |
169 | 164 |
170 void Mojo_GetTimeTicksNow(Dart_NativeArguments arguments) { | 165 void Mojo_GetTimeTicksNow(Dart_NativeArguments arguments) { |
171 MojoTimeTicks ticks = MojoGetTimeTicksNow(); | 166 MojoTimeTicks ticks = MojoGetTimeTicksNow(); |
172 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(ticks)); | 167 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(ticks)); |
173 } | 168 } |
174 | 169 |
175 void MojoHandle_Close(Dart_NativeArguments arguments) { | 170 void MojoHandle_Close(Dart_NativeArguments arguments) { |
176 int64_t raw_handle; | 171 int64_t raw_handle; |
177 CHECK_INTEGER_ARGUMENT(arguments, 0, &raw_handle, InvalidArgument); | 172 CHECK_INTEGER_ARGUMENT(arguments, 0, &raw_handle, InvalidArgument); |
178 | 173 |
179 // Remove the handle from this isolate's set. | 174 // Remove the handle from this isolate's set. |
180 MojoHandle handle = static_cast<MojoHandle>(raw_handle); | 175 MojoHandle handle = static_cast<MojoHandle>(raw_handle); |
181 auto isolate_data = MojoDartState::Current(); | |
182 assert(isolate_data != nullptr); | |
183 isolate_data->unclosed_handles().erase(handle); | |
184 | |
185 MojoResult res = MojoClose(handle); | 176 MojoResult res = MojoClose(handle); |
186 | 177 |
187 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | 178 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); |
188 } | 179 } |
189 | 180 |
190 void MojoHandle_Wait(Dart_NativeArguments arguments) { | 181 void MojoHandle_Wait(Dart_NativeArguments arguments) { |
191 int64_t handle = 0; | 182 int64_t handle = 0; |
192 int64_t signals = 0; | 183 int64_t signals = 0; |
193 int64_t deadline = 0; | 184 int64_t deadline = 0; |
194 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 185 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 MojoWaitManyState() {} | 718 MojoWaitManyState() {} |
728 | 719 |
729 std::vector<uint32_t> handles; | 720 std::vector<uint32_t> handles; |
730 std::vector<uint32_t> signals; | 721 std::vector<uint32_t> signals; |
731 std::vector<uint32_t> out_index; | 722 std::vector<uint32_t> out_index; |
732 std::vector<MojoHandleSignalsState> out_signals; | 723 std::vector<MojoHandleSignalsState> out_signals; |
733 | 724 |
734 static MojoWaitManyState* GetInstance(); | 725 static MojoWaitManyState* GetInstance(); |
735 | 726 |
736 private: | 727 private: |
737 DISALLOW_COPY_AND_ASSIGN(MojoWaitManyState); | 728 MOJO_DISALLOW_COPY_AND_ASSIGN(MojoWaitManyState); |
738 }; | 729 }; |
739 | 730 |
740 // This global is safe because it is only accessed by the single handle watcher | 731 // This global is safe because it is only accessed by the single handle watcher |
741 // isolate. If multiple handle watcher isolates are ever needed, it will need | 732 // isolate. If multiple handle watcher isolates are ever needed, it will need |
742 // to be replicated. | 733 // to be replicated. |
743 MojoWaitManyState* MojoWaitManyState::GetInstance() { | 734 MojoWaitManyState* MojoWaitManyState::GetInstance() { |
744 static MojoWaitManyState* state = new MojoWaitManyState; | 735 static MojoWaitManyState* state = new MojoWaitManyState; |
745 return state; | 736 return state; |
746 } | 737 } |
747 | 738 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 mojo_control_handle = control_handle; | 889 mojo_control_handle = control_handle; |
899 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 890 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
900 } | 891 } |
901 | 892 |
902 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 893 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
903 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 894 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
904 } | 895 } |
905 | 896 |
906 } // namespace dart | 897 } // namespace dart |
907 } // namespace mojo | 898 } // namespace mojo |
OLD | NEW |