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 "sky/engine/config.h" | 5 #include "sky/engine/config.h" |
6 #include "sky/engine/bindings/mojo_natives.h" | 6 #include "sky/engine/bindings/mojo_natives.h" |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <vector> | 10 #include <vector> |
(...skipping 25 matching lines...) Expand all Loading... |
36 V(MojoDataPipe_BeginWriteData, 3) \ | 36 V(MojoDataPipe_BeginWriteData, 3) \ |
37 V(MojoDataPipe_EndWriteData, 2) \ | 37 V(MojoDataPipe_EndWriteData, 2) \ |
38 V(MojoDataPipe_ReadData, 4) \ | 38 V(MojoDataPipe_ReadData, 4) \ |
39 V(MojoDataPipe_BeginReadData, 3) \ | 39 V(MojoDataPipe_BeginReadData, 3) \ |
40 V(MojoDataPipe_EndReadData, 2) \ | 40 V(MojoDataPipe_EndReadData, 2) \ |
41 V(MojoMessagePipe_Create, 1) \ | 41 V(MojoMessagePipe_Create, 1) \ |
42 V(MojoMessagePipe_Write, 5) \ | 42 V(MojoMessagePipe_Write, 5) \ |
43 V(MojoMessagePipe_Read, 5) \ | 43 V(MojoMessagePipe_Read, 5) \ |
44 V(MojoHandle_Close, 1) \ | 44 V(MojoHandle_Close, 1) \ |
45 V(MojoHandle_Wait, 3) \ | 45 V(MojoHandle_Wait, 3) \ |
46 V(MojoHandle_Register, 1) \ | 46 V(MojoHandle_Register, 2) \ |
47 V(MojoHandle_WaitMany, 3) \ | 47 V(MojoHandle_WaitMany, 3) \ |
48 V(MojoHandleWatcher_SendControlData, 4) \ | 48 V(MojoHandleWatcher_SendControlData, 4) \ |
49 V(MojoHandleWatcher_RecvControlData, 1) \ | 49 V(MojoHandleWatcher_RecvControlData, 1) \ |
50 V(MojoHandleWatcher_SetControlHandle, 1) \ | 50 V(MojoHandleWatcher_SetControlHandle, 1) \ |
51 V(MojoHandleWatcher_GetControlHandle, 0) | 51 V(MojoHandleWatcher_GetControlHandle, 0) |
52 | 52 |
53 MOJO_NATIVE_LIST(DECLARE_FUNCTION); | 53 MOJO_NATIVE_LIST(DECLARE_FUNCTION); |
54 | 54 |
55 static struct NativeEntries { | 55 static struct NativeEntries { |
56 const char* name; | 56 const char* name; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 static void SetNullReturn(Dart_NativeArguments arguments) { | 92 static void SetNullReturn(Dart_NativeArguments arguments) { |
93 Dart_SetReturnValue(arguments, Dart_Null()); | 93 Dart_SetReturnValue(arguments, Dart_Null()); |
94 } | 94 } |
95 | 95 |
96 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { | 96 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { |
97 Dart_SetIntegerReturnValue( | 97 Dart_SetIntegerReturnValue( |
98 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); | 98 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); |
99 } | 99 } |
100 | 100 |
101 static Dart_Handle MojoLib() { | 101 static Dart_Handle SignalsStateToDart(const MojoHandleSignalsState& state) { |
102 return DartBuiltin::LookupLibrary("dart:mojo.core"); | 102 Dart_Handle list = Dart_NewList(2); |
103 } | 103 Dart_Handle arg0 = Dart_NewInteger(state.satisfied_signals); |
104 | 104 Dart_Handle arg1 = Dart_NewInteger(state.satisfiable_signals); |
105 static Dart_Handle SignalsStateToDart(Dart_Handle klass, | 105 Dart_ListSetAt(list, 0, arg0); |
106 const MojoHandleSignalsState& state) { | 106 Dart_ListSetAt(list, 1, arg1); |
107 Dart_Handle arg1 = Dart_NewInteger(state.satisfied_signals); | 107 return list; |
108 Dart_Handle arg2 = Dart_NewInteger(state.satisfiable_signals); | |
109 Dart_Handle args[] = {arg1, arg2}; | |
110 return Dart_New(klass, Dart_Null(), 2, args); | |
111 } | 108 } |
112 | 109 |
113 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ | 110 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ |
114 { \ | 111 { \ |
115 Dart_Handle __status; \ | 112 Dart_Handle __status; \ |
116 __status = Dart_GetNativeIntegerArgument(args, num, result); \ | 113 __status = Dart_GetNativeIntegerArgument(args, num, result); \ |
117 if (Dart_IsError(__status)) { \ | 114 if (Dart_IsError(__status)) { \ |
118 Set##failure##Return(arguments); \ | 115 Set##failure##Return(arguments); \ |
119 return; \ | 116 return; \ |
120 } \ | 117 } \ |
121 } \ | 118 } \ |
122 | 119 |
123 struct CloserCallbackPeer { | 120 struct CloserCallbackPeer { |
124 MojoHandle handle; | 121 MojoHandle handle; |
125 }; | 122 }; |
126 | 123 |
127 static void MojoHandleCloserCallback(void* isolate_data, | 124 static void MojoHandleCloserCallback(void* isolate_data, |
128 Dart_WeakPersistentHandle handle, | 125 Dart_WeakPersistentHandle handle, |
129 void* peer) { | 126 void* peer) { |
130 CloserCallbackPeer* callback_peer = | 127 CloserCallbackPeer* callback_peer = |
131 reinterpret_cast<CloserCallbackPeer*>(peer); | 128 reinterpret_cast<CloserCallbackPeer*>(peer); |
132 if (callback_peer->handle != MOJO_HANDLE_INVALID) { | 129 if (callback_peer->handle != MOJO_HANDLE_INVALID) { |
133 MojoClose(callback_peer->handle); | 130 MojoClose(callback_peer->handle); |
134 } | 131 } |
135 delete callback_peer; | 132 delete callback_peer; |
136 } | 133 } |
137 | 134 |
138 // Setup a weak persistent handle for a Dart MojoHandle that calls MojoClose | 135 // Setup a weak persistent handle for a MojoHandle that calls MojoClose on the |
139 // on the handle when the MojoHandle is GC'd or the VM is going down. | 136 // handle when the MojoHandle is GC'd or the VM is going down. |
140 void MojoHandle_Register(Dart_NativeArguments arguments) { | 137 void MojoHandle_Register(Dart_NativeArguments arguments) { |
141 // An instance of Dart class MojoHandle. | |
142 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | 138 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); |
143 if (!Dart_IsInstance(mojo_handle_instance)) { | 139 if (!Dart_IsInstance(mojo_handle_instance)) { |
144 SetInvalidArgumentReturn(arguments); | 140 SetInvalidArgumentReturn(arguments); |
145 return; | 141 return; |
146 } | 142 } |
147 // TODO(zra): Here, we could check that mojo_handle_instance is really a | |
148 // MojoHandle instance, but with the Dart API it's not too easy to get a Type | |
149 // object from the class name outside of the root library. For now, we'll rely | |
150 // on the existence of the right fields to be sufficient. | |
151 | |
152 Dart_Handle raw_mojo_handle_instance = Dart_GetField( | |
153 mojo_handle_instance, ToDart("_handle")); | |
154 if (Dart_IsError(raw_mojo_handle_instance)) { | |
155 SetInvalidArgumentReturn(arguments); | |
156 return; | |
157 } | |
158 | |
159 Dart_Handle mojo_handle = Dart_GetField( | |
160 raw_mojo_handle_instance, ToDart("h")); | |
161 if (Dart_IsError(mojo_handle)) { | |
162 SetInvalidArgumentReturn(arguments); | |
163 return; | |
164 } | |
165 | 143 |
166 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | 144 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); |
167 Dart_Handle result = Dart_IntegerToInt64(mojo_handle, &raw_handle); | 145 CHECK_INTEGER_ARGUMENT(arguments, 1, &raw_handle, InvalidArgument); |
168 if (Dart_IsError(result)) { | |
169 SetInvalidArgumentReturn(arguments); | |
170 return; | |
171 } | |
172 | |
173 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | 146 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { |
174 SetInvalidArgumentReturn(arguments); | 147 SetInvalidArgumentReturn(arguments); |
175 return; | 148 return; |
176 } | 149 } |
177 | 150 |
| 151 // Set up a finalizer. |
178 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); | 152 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); |
179 callback_peer->handle = static_cast<MojoHandle>(raw_handle); | 153 callback_peer->handle = static_cast<MojoHandle>(raw_handle); |
180 Dart_NewWeakPersistentHandle(mojo_handle_instance, | 154 Dart_NewWeakPersistentHandle(mojo_handle_instance, |
181 reinterpret_cast<void*>(callback_peer), | 155 reinterpret_cast<void*>(callback_peer), |
182 sizeof(CloserCallbackPeer), | 156 sizeof(CloserCallbackPeer), |
183 MojoHandleCloserCallback); | 157 MojoHandleCloserCallback); |
184 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 158 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
185 } | 159 } |
186 | 160 |
187 void MojoHandle_Close(Dart_NativeArguments arguments) { | 161 void MojoHandle_Close(Dart_NativeArguments arguments) { |
(...skipping 11 matching lines...) Expand all Loading... |
199 int64_t deadline = 0; | 173 int64_t deadline = 0; |
200 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 174 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
201 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | 175 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); |
202 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | 176 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); |
203 | 177 |
204 MojoHandleSignalsState state; | 178 MojoHandleSignalsState state; |
205 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), | 179 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), |
206 static_cast<MojoHandleSignals>(signals), | 180 static_cast<MojoHandleSignals>(signals), |
207 static_cast<MojoDeadline>(deadline), &state); | 181 static_cast<MojoDeadline>(deadline), &state); |
208 | 182 |
209 Dart_Handle klass = Dart_GetClass( | |
210 MojoLib(), ToDart("MojoHandleSignalsState")); | |
211 DART_CHECK_VALID(klass); | |
212 | |
213 // The return value is structured as a list of length 2: | 183 // The return value is structured as a list of length 2: |
214 // [0] MojoResult | 184 // [0] MojoResult |
215 // [1] MojoHandleSignalsState. (may be null) | 185 // [1] MojoHandleSignalsState. (may be null) |
216 Dart_Handle list = Dart_NewList(2); | 186 Dart_Handle list = Dart_NewList(2); |
217 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); | 187 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); |
218 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { | 188 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { |
219 Dart_ListSetAt(list, 1, SignalsStateToDart(klass, state)); | 189 Dart_ListSetAt(list, 1, SignalsStateToDart(state)); |
220 } else { | 190 } else { |
221 Dart_ListSetAt(list, 1, Dart_Null()); | 191 Dart_ListSetAt(list, 1, Dart_Null()); |
222 } | 192 } |
223 Dart_SetReturnValue(arguments, list); | 193 Dart_SetReturnValue(arguments, list); |
224 } | 194 } |
225 | 195 |
226 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { | 196 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { |
227 int64_t deadline = 0; | 197 int64_t deadline = 0; |
228 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); | 198 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); |
229 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); | 199 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); |
(...skipping 28 matching lines...) Expand all Loading... |
258 Dart_IntegerToInt64(dart_handle, &mojo_handle); | 228 Dart_IntegerToInt64(dart_handle, &mojo_handle); |
259 Dart_IntegerToInt64(dart_signal, &mojo_signal); | 229 Dart_IntegerToInt64(dart_signal, &mojo_signal); |
260 mojo_handles[i] = mojo::Handle(mojo_handle); | 230 mojo_handles[i] = mojo::Handle(mojo_handle); |
261 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); | 231 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); |
262 } | 232 } |
263 | 233 |
264 std::vector<MojoHandleSignalsState> states(handles_len); | 234 std::vector<MojoHandleSignalsState> states(handles_len); |
265 mojo::WaitManyResult wmr = mojo::WaitMany( | 235 mojo::WaitManyResult wmr = mojo::WaitMany( |
266 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); | 236 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); |
267 | 237 |
268 Dart_Handle klass = Dart_GetClass( | |
269 MojoLib(), ToDart("MojoHandleSignalsState")); | |
270 DART_CHECK_VALID(klass); | |
271 | |
272 // The return value is structured as a list of length 3: | 238 // The return value is structured as a list of length 3: |
273 // [0] MojoResult | 239 // [0] MojoResult |
274 // [1] index of handle that caused a return (may be null) | 240 // [1] index of handle that caused a return (may be null) |
275 // [2] list of MojoHandleSignalsState. (may be null) | 241 // [2] list of MojoHandleSignalsState. (may be null) |
276 Dart_Handle list = Dart_NewList(3); | 242 Dart_Handle list = Dart_NewList(3); |
277 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); | 243 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); |
278 if (wmr.IsIndexValid()) | 244 if (wmr.IsIndexValid()) |
279 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); | 245 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); |
280 else | 246 else |
281 Dart_ListSetAt(list, 1, Dart_Null()); | 247 Dart_ListSetAt(list, 1, Dart_Null()); |
282 if (wmr.AreSignalsStatesValid()) { | 248 if (wmr.AreSignalsStatesValid()) { |
283 Dart_Handle stateList = Dart_NewList(handles_len); | 249 Dart_Handle stateList = Dart_NewList(handles_len); |
284 for (int i = 0; i < handles_len; i++) { | 250 for (int i = 0; i < handles_len; i++) { |
285 Dart_ListSetAt(stateList, i, SignalsStateToDart(klass, states[i])); | 251 Dart_ListSetAt(stateList, i, SignalsStateToDart(states[i])); |
286 } | 252 } |
287 Dart_ListSetAt(list, 2, stateList); | 253 Dart_ListSetAt(list, 2, stateList); |
288 } else { | 254 } else { |
289 Dart_ListSetAt(list, 2, Dart_Null()); | 255 Dart_ListSetAt(list, 2, Dart_Null()); |
290 } | 256 } |
291 Dart_SetReturnValue(arguments, list); | 257 Dart_SetReturnValue(arguments, list); |
292 } | 258 } |
293 | 259 |
294 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { | 260 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { |
295 int64_t num_bytes = 0; | 261 int64_t num_bytes = 0; |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); | 766 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); |
801 mojo_control_handle = control_handle; | 767 mojo_control_handle = control_handle; |
802 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 768 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
803 } | 769 } |
804 | 770 |
805 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 771 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
806 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 772 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
807 } | 773 } |
808 | 774 |
809 } // namespace blink | 775 } // namespace blink |
OLD | NEW |