| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sky/engine/config.h" | |
| 6 #include "sky/engine/bindings2/mojo_natives.h" | |
| 7 | |
| 8 #include <stdio.h> | |
| 9 #include <string.h> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "dart/runtime/include/dart_api.h" | |
| 16 #include "mojo/public/c/system/core.h" | |
| 17 #include "mojo/public/cpp/system/core.h" | |
| 18 #include "sky/engine/bindings2/builtin.h" | |
| 19 #include "sky/engine/tonic/dart_converter.h" | |
| 20 #include "sky/engine/tonic/dart_builtin.h" | |
| 21 | |
| 22 namespace blink { | |
| 23 | |
| 24 #define REGISTER_FUNCTION(name, count) \ | |
| 25 { "" #name, name, count }, | |
| 26 #define DECLARE_FUNCTION(name, count) \ | |
| 27 extern void name(Dart_NativeArguments args); | |
| 28 | |
| 29 #define MOJO_NATIVE_LIST(V) \ | |
| 30 V(MojoSharedBuffer_Create, 2) \ | |
| 31 V(MojoSharedBuffer_Duplicate, 2) \ | |
| 32 V(MojoSharedBuffer_Map, 5) \ | |
| 33 V(MojoSharedBuffer_Unmap, 1) \ | |
| 34 V(MojoDataPipe_Create, 3) \ | |
| 35 V(MojoDataPipe_WriteData, 4) \ | |
| 36 V(MojoDataPipe_BeginWriteData, 3) \ | |
| 37 V(MojoDataPipe_EndWriteData, 2) \ | |
| 38 V(MojoDataPipe_ReadData, 4) \ | |
| 39 V(MojoDataPipe_BeginReadData, 3) \ | |
| 40 V(MojoDataPipe_EndReadData, 2) \ | |
| 41 V(MojoMessagePipe_Create, 1) \ | |
| 42 V(MojoMessagePipe_Write, 5) \ | |
| 43 V(MojoMessagePipe_Read, 5) \ | |
| 44 V(MojoHandle_Close, 1) \ | |
| 45 V(MojoHandle_Wait, 3) \ | |
| 46 V(MojoHandle_Register, 1) \ | |
| 47 V(MojoHandle_WaitMany, 3) \ | |
| 48 V(MojoHandleWatcher_SendControlData, 4) \ | |
| 49 V(MojoHandleWatcher_RecvControlData, 1) \ | |
| 50 V(MojoHandleWatcher_SetControlHandle, 1) \ | |
| 51 V(MojoHandleWatcher_GetControlHandle, 0) | |
| 52 | |
| 53 MOJO_NATIVE_LIST(DECLARE_FUNCTION); | |
| 54 | |
| 55 static struct NativeEntries { | |
| 56 const char* name; | |
| 57 Dart_NativeFunction function; | |
| 58 int argument_count; | |
| 59 } MojoEntries[] = {MOJO_NATIVE_LIST(REGISTER_FUNCTION)}; | |
| 60 | |
| 61 Dart_NativeFunction MojoNativeLookup(Dart_Handle name, | |
| 62 int argument_count, | |
| 63 bool* auto_setup_scope) { | |
| 64 const char* function_name = nullptr; | |
| 65 Dart_Handle result = Dart_StringToCString(name, &function_name); | |
| 66 DART_CHECK_VALID(result); | |
| 67 DCHECK(function_name != nullptr); | |
| 68 DCHECK(auto_setup_scope != nullptr); | |
| 69 *auto_setup_scope = true; | |
| 70 size_t num_entries = arraysize(MojoEntries); | |
| 71 for (size_t i = 0; i < num_entries; ++i) { | |
| 72 const struct NativeEntries& entry = MojoEntries[i]; | |
| 73 if (!strcmp(function_name, entry.name) && | |
| 74 (entry.argument_count == argument_count)) { | |
| 75 return entry.function; | |
| 76 } | |
| 77 } | |
| 78 return nullptr; | |
| 79 } | |
| 80 | |
| 81 const uint8_t* MojoNativeSymbol(Dart_NativeFunction nf) { | |
| 82 size_t num_entries = arraysize(MojoEntries); | |
| 83 for (size_t i = 0; i < num_entries; ++i) { | |
| 84 const struct NativeEntries& entry = MojoEntries[i]; | |
| 85 if (entry.function == nf) { | |
| 86 return reinterpret_cast<const uint8_t*>(entry.name); | |
| 87 } | |
| 88 } | |
| 89 return nullptr; | |
| 90 } | |
| 91 | |
| 92 static void SetNullReturn(Dart_NativeArguments arguments) { | |
| 93 Dart_SetReturnValue(arguments, Dart_Null()); | |
| 94 } | |
| 95 | |
| 96 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { | |
| 97 Dart_SetIntegerReturnValue( | |
| 98 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); | |
| 99 } | |
| 100 | |
| 101 static Dart_Handle MojoLib() { | |
| 102 return DartBuiltin::LookupLibrary("dart:mojo_core"); | |
| 103 } | |
| 104 | |
| 105 static Dart_Handle SignalsStateToDart(Dart_Handle klass, | |
| 106 const MojoHandleSignalsState& state) { | |
| 107 Dart_Handle arg1 = Dart_NewInteger(state.satisfied_signals); | |
| 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 } | |
| 112 | |
| 113 #define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \ | |
| 114 { \ | |
| 115 Dart_Handle __status; \ | |
| 116 __status = Dart_GetNativeIntegerArgument(args, num, result); \ | |
| 117 if (Dart_IsError(__status)) { \ | |
| 118 Set##failure##Return(arguments); \ | |
| 119 return; \ | |
| 120 } \ | |
| 121 } \ | |
| 122 | |
| 123 struct CloserCallbackPeer { | |
| 124 MojoHandle handle; | |
| 125 }; | |
| 126 | |
| 127 static void MojoHandleCloserCallback(void* isolate_data, | |
| 128 Dart_WeakPersistentHandle handle, | |
| 129 void* peer) { | |
| 130 CloserCallbackPeer* callback_peer = | |
| 131 reinterpret_cast<CloserCallbackPeer*>(peer); | |
| 132 if (callback_peer->handle != MOJO_HANDLE_INVALID) { | |
| 133 MojoClose(callback_peer->handle); | |
| 134 } | |
| 135 delete callback_peer; | |
| 136 } | |
| 137 | |
| 138 // Setup a weak persistent handle for a Dart MojoHandle that calls MojoClose | |
| 139 // on the handle when the MojoHandle is GC'd or the VM is going down. | |
| 140 void MojoHandle_Register(Dart_NativeArguments arguments) { | |
| 141 // An instance of Dart class MojoHandle. | |
| 142 Dart_Handle mojo_handle_instance = Dart_GetNativeArgument(arguments, 0); | |
| 143 if (!Dart_IsInstance(mojo_handle_instance)) { | |
| 144 SetInvalidArgumentReturn(arguments); | |
| 145 return; | |
| 146 } | |
| 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 | |
| 166 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | |
| 167 Dart_Handle result = Dart_IntegerToInt64(mojo_handle, &raw_handle); | |
| 168 if (Dart_IsError(result)) { | |
| 169 SetInvalidArgumentReturn(arguments); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 if (raw_handle == static_cast<int64_t>(MOJO_HANDLE_INVALID)) { | |
| 174 SetInvalidArgumentReturn(arguments); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 CloserCallbackPeer* callback_peer = new CloserCallbackPeer(); | |
| 179 callback_peer->handle = static_cast<MojoHandle>(raw_handle); | |
| 180 Dart_NewWeakPersistentHandle(mojo_handle_instance, | |
| 181 reinterpret_cast<void*>(callback_peer), | |
| 182 sizeof(CloserCallbackPeer), | |
| 183 MojoHandleCloserCallback); | |
| 184 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | |
| 185 } | |
| 186 | |
| 187 void MojoHandle_Close(Dart_NativeArguments arguments) { | |
| 188 int64_t handle; | |
| 189 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 190 | |
| 191 MojoResult res = MojoClose(static_cast<MojoHandle>(handle)); | |
| 192 | |
| 193 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 194 } | |
| 195 | |
| 196 void MojoHandle_Wait(Dart_NativeArguments arguments) { | |
| 197 int64_t handle = 0; | |
| 198 int64_t signals = 0; | |
| 199 int64_t deadline = 0; | |
| 200 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 201 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | |
| 202 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | |
| 203 | |
| 204 MojoHandleSignalsState state; | |
| 205 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), | |
| 206 static_cast<MojoHandleSignals>(signals), | |
| 207 static_cast<MojoDeadline>(deadline), &state); | |
| 208 | |
| 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: | |
| 214 // [0] MojoResult | |
| 215 // [1] MojoHandleSignalsState. (may be null) | |
| 216 Dart_Handle list = Dart_NewList(2); | |
| 217 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); | |
| 218 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { | |
| 219 Dart_ListSetAt(list, 1, SignalsStateToDart(klass, state)); | |
| 220 } else { | |
| 221 Dart_ListSetAt(list, 1, Dart_Null()); | |
| 222 } | |
| 223 Dart_SetReturnValue(arguments, list); | |
| 224 } | |
| 225 | |
| 226 void MojoHandle_WaitMany(Dart_NativeArguments arguments) { | |
| 227 int64_t deadline = 0; | |
| 228 Dart_Handle handles = Dart_GetNativeArgument(arguments, 0); | |
| 229 Dart_Handle signals = Dart_GetNativeArgument(arguments, 1); | |
| 230 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | |
| 231 | |
| 232 if (!Dart_IsList(handles) || !Dart_IsList(signals)) { | |
| 233 SetInvalidArgumentReturn(arguments); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 intptr_t handles_len = 0; | |
| 238 intptr_t signals_len = 0; | |
| 239 Dart_ListLength(handles, &handles_len); | |
| 240 Dart_ListLength(signals, &signals_len); | |
| 241 if (handles_len != signals_len) { | |
| 242 SetInvalidArgumentReturn(arguments); | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 std::vector<mojo::Handle> mojo_handles(handles_len); | |
| 247 std::vector<MojoHandleSignals> mojo_signals(handles_len); | |
| 248 | |
| 249 for (int i = 0; i < handles_len; i++) { | |
| 250 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); | |
| 251 Dart_Handle dart_signal = Dart_ListGetAt(signals, i); | |
| 252 if (!Dart_IsInteger(dart_handle) || !Dart_IsInteger(dart_signal)) { | |
| 253 SetInvalidArgumentReturn(arguments); | |
| 254 return; | |
| 255 } | |
| 256 int64_t mojo_handle = 0; | |
| 257 int64_t mojo_signal = 0; | |
| 258 Dart_IntegerToInt64(dart_handle, &mojo_handle); | |
| 259 Dart_IntegerToInt64(dart_signal, &mojo_signal); | |
| 260 mojo_handles[i] = mojo::Handle(mojo_handle); | |
| 261 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); | |
| 262 } | |
| 263 | |
| 264 std::vector<MojoHandleSignalsState> states(handles_len); | |
| 265 mojo::WaitManyResult wmr = mojo::WaitMany( | |
| 266 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); | |
| 267 | |
| 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: | |
| 273 // [0] MojoResult | |
| 274 // [1] index of handle that caused a return (may be null) | |
| 275 // [2] list of MojoHandleSignalsState. (may be null) | |
| 276 Dart_Handle list = Dart_NewList(3); | |
| 277 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); | |
| 278 if (wmr.IsIndexValid()) | |
| 279 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); | |
| 280 else | |
| 281 Dart_ListSetAt(list, 1, Dart_Null()); | |
| 282 if (wmr.AreSignalsStatesValid()) { | |
| 283 Dart_Handle stateList = Dart_NewList(handles_len); | |
| 284 for (int i = 0; i < handles_len; i++) { | |
| 285 Dart_ListSetAt(stateList, i, SignalsStateToDart(klass, states[i])); | |
| 286 } | |
| 287 Dart_ListSetAt(list, 2, stateList); | |
| 288 } else { | |
| 289 Dart_ListSetAt(list, 2, Dart_Null()); | |
| 290 } | |
| 291 Dart_SetReturnValue(arguments, list); | |
| 292 } | |
| 293 | |
| 294 void MojoSharedBuffer_Create(Dart_NativeArguments arguments) { | |
| 295 int64_t num_bytes = 0; | |
| 296 int64_t flags = 0; | |
| 297 CHECK_INTEGER_ARGUMENT(arguments, 0, &num_bytes, Null); | |
| 298 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 299 | |
| 300 MojoCreateSharedBufferOptions options; | |
| 301 options.struct_size = sizeof(MojoCreateSharedBufferOptions); | |
| 302 options.flags = static_cast<MojoCreateSharedBufferOptionsFlags>(flags); | |
| 303 | |
| 304 MojoHandle out = MOJO_HANDLE_INVALID;; | |
| 305 MojoResult res = MojoCreateSharedBuffer( | |
| 306 &options, static_cast<int32_t>(num_bytes), &out); | |
| 307 | |
| 308 Dart_Handle list = Dart_NewList(2); | |
| 309 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 310 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); | |
| 311 Dart_SetReturnValue(arguments, list); | |
| 312 } | |
| 313 | |
| 314 void MojoSharedBuffer_Duplicate(Dart_NativeArguments arguments) { | |
| 315 int64_t handle = 0; | |
| 316 int64_t flags = 0; | |
| 317 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 318 CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null); | |
| 319 | |
| 320 MojoDuplicateBufferHandleOptions options; | |
| 321 options.struct_size = sizeof(MojoDuplicateBufferHandleOptions); | |
| 322 options.flags = static_cast<MojoDuplicateBufferHandleOptionsFlags>(flags); | |
| 323 | |
| 324 MojoHandle out = MOJO_HANDLE_INVALID;; | |
| 325 MojoResult res = MojoDuplicateBufferHandle( | |
| 326 static_cast<MojoHandle>(handle), &options, &out); | |
| 327 | |
| 328 Dart_Handle list = Dart_NewList(2); | |
| 329 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 330 Dart_ListSetAt(list, 1, Dart_NewInteger(out)); | |
| 331 Dart_SetReturnValue(arguments, list); | |
| 332 } | |
| 333 | |
| 334 static void MojoBufferUnmapCallback(void* isolate_data, | |
| 335 Dart_WeakPersistentHandle handle, | |
| 336 void* peer) { | |
| 337 MojoUnmapBuffer(peer); | |
| 338 } | |
| 339 | |
| 340 void MojoSharedBuffer_Map(Dart_NativeArguments arguments) { | |
| 341 int64_t handle = 0; | |
| 342 int64_t offset = 0; | |
| 343 int64_t num_bytes = 0; | |
| 344 int64_t flags = 0; | |
| 345 Dart_Handle mojo_buffer = Dart_GetNativeArgument(arguments, 0); | |
| 346 CHECK_INTEGER_ARGUMENT(arguments, 1, &handle, Null); | |
| 347 CHECK_INTEGER_ARGUMENT(arguments, 2, &offset, Null); | |
| 348 CHECK_INTEGER_ARGUMENT(arguments, 3, &num_bytes, Null); | |
| 349 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, Null); | |
| 350 | |
| 351 void* out; | |
| 352 MojoResult res = MojoMapBuffer(static_cast<MojoHandle>(handle), | |
| 353 offset, | |
| 354 num_bytes, | |
| 355 &out, | |
| 356 static_cast<MojoMapBufferFlags>(flags)); | |
| 357 | |
| 358 Dart_Handle list = Dart_NewList(2); | |
| 359 Dart_Handle typed_data; | |
| 360 if (res == MOJO_RESULT_OK) { | |
| 361 typed_data = Dart_NewExternalTypedData( | |
| 362 Dart_TypedData_kByteData, out, num_bytes); | |
| 363 Dart_NewWeakPersistentHandle( | |
| 364 mojo_buffer, out, num_bytes, MojoBufferUnmapCallback); | |
| 365 } else { | |
| 366 typed_data = Dart_Null(); | |
| 367 } | |
| 368 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 369 Dart_ListSetAt(list, 1, typed_data); | |
| 370 Dart_SetReturnValue(arguments, list); | |
| 371 } | |
| 372 | |
| 373 void MojoSharedBuffer_Unmap(Dart_NativeArguments arguments) { | |
| 374 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 0); | |
| 375 if (Dart_GetTypeOfExternalTypedData(typed_data) == Dart_TypedData_kInvalid) { | |
| 376 SetInvalidArgumentReturn(arguments); | |
| 377 return; | |
| 378 } | |
| 379 | |
| 380 Dart_TypedData_Type typ; | |
| 381 void *data; | |
| 382 intptr_t len; | |
| 383 Dart_TypedDataAcquireData(typed_data, &typ, &data, &len); | |
| 384 MojoResult res = MojoUnmapBuffer(data); | |
| 385 Dart_TypedDataReleaseData(typed_data); | |
| 386 | |
| 387 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 388 } | |
| 389 | |
| 390 void MojoDataPipe_Create(Dart_NativeArguments arguments) { | |
| 391 int64_t element_bytes = 0; | |
| 392 int64_t capacity_bytes = 0; | |
| 393 int64_t flags = 0; | |
| 394 CHECK_INTEGER_ARGUMENT(arguments, 0, &element_bytes, Null); | |
| 395 CHECK_INTEGER_ARGUMENT(arguments, 1, &capacity_bytes, Null); | |
| 396 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); | |
| 397 | |
| 398 MojoCreateDataPipeOptions options; | |
| 399 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 400 options.flags = static_cast<MojoCreateDataPipeOptionsFlags>(flags); | |
| 401 options.element_num_bytes = static_cast<uint32_t>(element_bytes); | |
| 402 options.capacity_num_bytes = static_cast<uint32_t>(capacity_bytes); | |
| 403 | |
| 404 MojoHandle producer = MOJO_HANDLE_INVALID; | |
| 405 MojoHandle consumer = MOJO_HANDLE_INVALID; | |
| 406 MojoResult res = MojoCreateDataPipe(&options, &producer, &consumer); | |
| 407 | |
| 408 Dart_Handle list = Dart_NewList(3); | |
| 409 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 410 Dart_ListSetAt(list, 1, Dart_NewInteger(producer)); | |
| 411 Dart_ListSetAt(list, 2, Dart_NewInteger(consumer)); | |
| 412 Dart_SetReturnValue(arguments, list); | |
| 413 } | |
| 414 | |
| 415 void MojoDataPipe_WriteData(Dart_NativeArguments arguments) { | |
| 416 int64_t handle = 0; | |
| 417 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 418 | |
| 419 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 420 if (!Dart_IsTypedData(typed_data)) { | |
| 421 SetNullReturn(arguments); | |
| 422 return; | |
| 423 } | |
| 424 | |
| 425 int64_t num_bytes = 0; | |
| 426 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 427 if (num_bytes <= 0) { | |
| 428 SetNullReturn(arguments); | |
| 429 return; | |
| 430 } | |
| 431 | |
| 432 int64_t flags = 0; | |
| 433 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); | |
| 434 | |
| 435 Dart_TypedData_Type type; | |
| 436 void* data; | |
| 437 intptr_t data_length; | |
| 438 Dart_TypedDataAcquireData(typed_data, &type, &data, &data_length); | |
| 439 uint32_t length = static_cast<uint32_t>(num_bytes); | |
| 440 MojoResult res = MojoWriteData( | |
| 441 static_cast<MojoHandle>(handle), | |
| 442 data, | |
| 443 &length, | |
| 444 static_cast<MojoWriteDataFlags>(flags)); | |
| 445 Dart_TypedDataReleaseData(typed_data); | |
| 446 | |
| 447 Dart_Handle list = Dart_NewList(2); | |
| 448 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 449 Dart_ListSetAt(list, 1, Dart_NewInteger(length)); | |
| 450 Dart_SetReturnValue(arguments, list); | |
| 451 } | |
| 452 | |
| 453 void MojoDataPipe_BeginWriteData(Dart_NativeArguments arguments) { | |
| 454 int64_t handle = 0; | |
| 455 int64_t buffer_bytes = 0; | |
| 456 int64_t flags = 0; | |
| 457 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 458 CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null); | |
| 459 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); | |
| 460 | |
| 461 void* buffer; | |
| 462 uint32_t size = static_cast<uint32_t>(buffer_bytes); | |
| 463 MojoResult res = MojoBeginWriteData( | |
| 464 static_cast<MojoHandle>(handle), | |
| 465 &buffer, | |
| 466 &size, | |
| 467 static_cast<MojoWriteDataFlags>(flags)); | |
| 468 | |
| 469 Dart_Handle list = Dart_NewList(2); | |
| 470 Dart_Handle typed_data; | |
| 471 if (res == MOJO_RESULT_OK) { | |
| 472 typed_data = Dart_NewExternalTypedData( | |
| 473 Dart_TypedData_kByteData, buffer, size); | |
| 474 } else { | |
| 475 typed_data = Dart_Null(); | |
| 476 } | |
| 477 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 478 Dart_ListSetAt(list, 1, typed_data); | |
| 479 Dart_SetReturnValue(arguments, list); | |
| 480 } | |
| 481 | |
| 482 void MojoDataPipe_EndWriteData(Dart_NativeArguments arguments) { | |
| 483 int64_t handle = 0; | |
| 484 int64_t num_bytes_written = 0; | |
| 485 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 486 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_written, InvalidArgument); | |
| 487 | |
| 488 MojoResult res = MojoEndWriteData( | |
| 489 static_cast<MojoHandle>(handle), | |
| 490 static_cast<uint32_t>(num_bytes_written)); | |
| 491 | |
| 492 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 493 } | |
| 494 | |
| 495 void MojoDataPipe_ReadData(Dart_NativeArguments arguments) { | |
| 496 int64_t handle = 0; | |
| 497 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 498 | |
| 499 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 500 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 501 SetNullReturn(arguments); | |
| 502 return; | |
| 503 } | |
| 504 // When querying the amount of data available to read from the pipe, | |
| 505 // null is passed in for typed_data. | |
| 506 | |
| 507 int64_t num_bytes = 0; | |
| 508 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 509 | |
| 510 int64_t flags = 0; | |
| 511 CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null); | |
| 512 | |
| 513 Dart_TypedData_Type typ; | |
| 514 void* data = nullptr; | |
| 515 intptr_t bdlen = 0; | |
| 516 if (!Dart_IsNull(typed_data)) { | |
| 517 Dart_TypedDataAcquireData(typed_data, &typ, &data, &bdlen); | |
| 518 } | |
| 519 uint32_t len = static_cast<uint32_t>(num_bytes); | |
| 520 MojoResult res = MojoReadData( | |
| 521 static_cast<MojoHandle>(handle), | |
| 522 data, | |
| 523 &len, | |
| 524 static_cast<MojoReadDataFlags>(flags)); | |
| 525 if (!Dart_IsNull(typed_data)) { | |
| 526 Dart_TypedDataReleaseData(typed_data); | |
| 527 } | |
| 528 | |
| 529 Dart_Handle list = Dart_NewList(2); | |
| 530 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 531 Dart_ListSetAt(list, 1, Dart_NewInteger(len)); | |
| 532 Dart_SetReturnValue(arguments, list); | |
| 533 } | |
| 534 | |
| 535 void MojoDataPipe_BeginReadData(Dart_NativeArguments arguments) { | |
| 536 int64_t handle = 0; | |
| 537 int64_t buffer_bytes = 0; | |
| 538 int64_t flags = 0; | |
| 539 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 540 CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null); | |
| 541 CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null); | |
| 542 | |
| 543 void* buffer; | |
| 544 uint32_t size = static_cast<uint32_t>(buffer_bytes); | |
| 545 MojoResult res = MojoBeginReadData( | |
| 546 static_cast<MojoHandle>(handle), | |
| 547 const_cast<const void**>(&buffer), | |
| 548 &size, | |
| 549 static_cast<MojoWriteDataFlags>(flags)); | |
| 550 | |
| 551 Dart_Handle list = Dart_NewList(2); | |
| 552 Dart_Handle typed_data; | |
| 553 if (res == MOJO_RESULT_OK) { | |
| 554 typed_data = Dart_NewExternalTypedData( | |
| 555 Dart_TypedData_kByteData, buffer, size); | |
| 556 } else { | |
| 557 typed_data = Dart_Null(); | |
| 558 } | |
| 559 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 560 Dart_ListSetAt(list, 1, typed_data); | |
| 561 Dart_SetReturnValue(arguments, list); | |
| 562 } | |
| 563 | |
| 564 void MojoDataPipe_EndReadData(Dart_NativeArguments arguments) { | |
| 565 int64_t handle = 0; | |
| 566 int64_t num_bytes_read = 0; | |
| 567 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 568 CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_read, InvalidArgument); | |
| 569 | |
| 570 MojoResult res = MojoEndReadData( | |
| 571 static_cast<MojoHandle>(handle), | |
| 572 static_cast<uint32_t>(num_bytes_read)); | |
| 573 | |
| 574 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 575 } | |
| 576 | |
| 577 void MojoMessagePipe_Create(Dart_NativeArguments arguments) { | |
| 578 int64_t flags = 0; | |
| 579 CHECK_INTEGER_ARGUMENT(arguments, 0, &flags, Null); | |
| 580 | |
| 581 MojoCreateMessagePipeOptions options; | |
| 582 options.struct_size = sizeof(MojoCreateMessagePipeOptions); | |
| 583 options.flags = static_cast<MojoCreateMessagePipeOptionsFlags>(flags); | |
| 584 | |
| 585 MojoHandle end1 = MOJO_HANDLE_INVALID; | |
| 586 MojoHandle end2 = MOJO_HANDLE_INVALID; | |
| 587 MojoResult res = MojoCreateMessagePipe(&options, &end1, &end2); | |
| 588 | |
| 589 Dart_Handle list = Dart_NewList(3); | |
| 590 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 591 Dart_ListSetAt(list, 1, Dart_NewInteger(end1)); | |
| 592 Dart_ListSetAt(list, 2, Dart_NewInteger(end2)); | |
| 593 Dart_SetReturnValue(arguments, list); | |
| 594 } | |
| 595 | |
| 596 void MojoMessagePipe_Write(Dart_NativeArguments arguments) { | |
| 597 int64_t handle = 0; | |
| 598 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | |
| 599 | |
| 600 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 601 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 602 SetInvalidArgumentReturn(arguments); | |
| 603 return; | |
| 604 } | |
| 605 | |
| 606 int64_t num_bytes = 0; | |
| 607 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, InvalidArgument); | |
| 608 if ((Dart_IsNull(typed_data) && (num_bytes != 0)) || | |
| 609 (!Dart_IsNull(typed_data) && (num_bytes <= 0))) { | |
| 610 SetInvalidArgumentReturn(arguments); | |
| 611 return; | |
| 612 } | |
| 613 | |
| 614 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); | |
| 615 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { | |
| 616 SetInvalidArgumentReturn(arguments); | |
| 617 return; | |
| 618 } | |
| 619 | |
| 620 int64_t flags = 0; | |
| 621 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, InvalidArgument); | |
| 622 | |
| 623 // Grab the data if there is any. | |
| 624 Dart_TypedData_Type typ; | |
| 625 void* bytes = nullptr; | |
| 626 intptr_t bdlen = 0; | |
| 627 if (!Dart_IsNull(typed_data)) { | |
| 628 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &bdlen); | |
| 629 } | |
| 630 | |
| 631 // Grab the handles if there are any. | |
| 632 scoped_ptr<MojoHandle[]> mojo_handles; | |
| 633 intptr_t handles_len = 0; | |
| 634 if (!Dart_IsNull(handles)) { | |
| 635 Dart_ListLength(handles, &handles_len); | |
| 636 if (handles_len > 0) { | |
| 637 mojo_handles.reset(new MojoHandle[handles_len]); | |
| 638 } | |
| 639 for (int i = 0; i < handles_len; i++) { | |
| 640 Dart_Handle dart_handle = Dart_ListGetAt(handles, i); | |
| 641 if (!Dart_IsInteger(dart_handle)) { | |
| 642 SetInvalidArgumentReturn(arguments); | |
| 643 return; | |
| 644 } | |
| 645 int64_t mojo_handle = 0; | |
| 646 Dart_IntegerToInt64(dart_handle, &mojo_handle); | |
| 647 mojo_handles[i] = static_cast<MojoHandle>(mojo_handle); | |
| 648 } | |
| 649 } | |
| 650 | |
| 651 MojoResult res = MojoWriteMessage( | |
| 652 static_cast<MojoHandle>(handle), | |
| 653 const_cast<const void*>(bytes), | |
| 654 static_cast<uint32_t>(num_bytes), | |
| 655 mojo_handles.get(), | |
| 656 static_cast<uint32_t>(handles_len), | |
| 657 static_cast<MojoWriteMessageFlags>(flags)); | |
| 658 | |
| 659 // Release the data. | |
| 660 if (!Dart_IsNull(typed_data)) { | |
| 661 Dart_TypedDataReleaseData(typed_data); | |
| 662 } | |
| 663 | |
| 664 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 665 } | |
| 666 | |
| 667 void MojoMessagePipe_Read(Dart_NativeArguments arguments) { | |
| 668 int64_t handle = 0; | |
| 669 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null); | |
| 670 | |
| 671 Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1); | |
| 672 if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) { | |
| 673 SetNullReturn(arguments); | |
| 674 return; | |
| 675 } | |
| 676 // When querying the amount of data available to read from the pipe, | |
| 677 // null is passed in for typed_data. | |
| 678 | |
| 679 int64_t num_bytes = 0; | |
| 680 CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null); | |
| 681 if ((Dart_IsNull(typed_data) && (num_bytes != 0)) || | |
| 682 (!Dart_IsNull(typed_data) && (num_bytes <= 0))) { | |
| 683 SetNullReturn(arguments); | |
| 684 return; | |
| 685 } | |
| 686 | |
| 687 Dart_Handle handles = Dart_GetNativeArgument(arguments, 3); | |
| 688 if (!Dart_IsList(handles) && !Dart_IsNull(handles)) { | |
| 689 SetNullReturn(arguments); | |
| 690 return; | |
| 691 } | |
| 692 | |
| 693 int64_t flags = 0; | |
| 694 CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, Null); | |
| 695 | |
| 696 // Grab the data if there is any. | |
| 697 Dart_TypedData_Type typ; | |
| 698 void* bytes = nullptr; | |
| 699 intptr_t byte_data_len = 0; | |
| 700 if (!Dart_IsNull(typed_data)) { | |
| 701 Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &byte_data_len); | |
| 702 } | |
| 703 uint32_t blen = static_cast<uint32_t>(num_bytes); | |
| 704 | |
| 705 // Grab the handles if there are any. | |
| 706 scoped_ptr<MojoHandle[]> mojo_handles; | |
| 707 intptr_t handles_len = 0; | |
| 708 if (!Dart_IsNull(handles)) { | |
| 709 Dart_ListLength(handles, &handles_len); | |
| 710 mojo_handles.reset(new MojoHandle[handles_len]); | |
| 711 } | |
| 712 uint32_t hlen = static_cast<uint32_t>(handles_len); | |
| 713 | |
| 714 MojoResult res = MojoReadMessage( | |
| 715 static_cast<MojoHandle>(handle), | |
| 716 bytes, | |
| 717 &blen, | |
| 718 mojo_handles.get(), | |
| 719 &hlen, | |
| 720 static_cast<MojoReadMessageFlags>(flags)); | |
| 721 | |
| 722 // Release the data. | |
| 723 if (!Dart_IsNull(typed_data)) { | |
| 724 Dart_TypedDataReleaseData(typed_data); | |
| 725 } | |
| 726 | |
| 727 if (!Dart_IsNull(handles)) { | |
| 728 for (int i = 0; i < handles_len; i++) { | |
| 729 Dart_ListSetAt(handles, i, Dart_NewInteger(mojo_handles[i])); | |
| 730 } | |
| 731 } | |
| 732 | |
| 733 Dart_Handle list = Dart_NewList(3); | |
| 734 Dart_ListSetAt(list, 0, Dart_NewInteger(res)); | |
| 735 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); | |
| 736 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); | |
| 737 Dart_SetReturnValue(arguments, list); | |
| 738 } | |
| 739 | |
| 740 struct ControlData { | |
| 741 int64_t handle; | |
| 742 Dart_Port port; | |
| 743 int64_t data; | |
| 744 }; | |
| 745 | |
| 746 void MojoHandleWatcher_SendControlData(Dart_NativeArguments arguments) { | |
| 747 int64_t control_handle = 0; | |
| 748 int64_t client_handle = 0; | |
| 749 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); | |
| 750 CHECK_INTEGER_ARGUMENT(arguments, 1, &client_handle, InvalidArgument); | |
| 751 | |
| 752 Dart_Handle send_port_handle = Dart_GetNativeArgument(arguments, 2); | |
| 753 Dart_Port send_port_id = 0; | |
| 754 if (!Dart_IsNull(send_port_handle)) { | |
| 755 Dart_Handle result = Dart_SendPortGetId(send_port_handle, &send_port_id); | |
| 756 if (Dart_IsError(result)) { | |
| 757 SetInvalidArgumentReturn(arguments); | |
| 758 return; | |
| 759 } | |
| 760 } | |
| 761 | |
| 762 int64_t data = 0; | |
| 763 CHECK_INTEGER_ARGUMENT(arguments, 3, &data, InvalidArgument); | |
| 764 | |
| 765 ControlData cd; | |
| 766 cd.handle = client_handle; | |
| 767 cd.port = send_port_id; | |
| 768 cd.data = data; | |
| 769 const void* bytes = reinterpret_cast<const void*>(&cd); | |
| 770 MojoResult res = MojoWriteMessage( | |
| 771 control_handle, bytes, sizeof(cd), nullptr, 0, 0); | |
| 772 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res)); | |
| 773 } | |
| 774 | |
| 775 void MojoHandleWatcher_RecvControlData(Dart_NativeArguments arguments) { | |
| 776 int64_t control_handle = 0; | |
| 777 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, Null); | |
| 778 | |
| 779 ControlData cd; | |
| 780 void* bytes = reinterpret_cast<void*>(&cd); | |
| 781 uint32_t num_bytes = sizeof(cd); | |
| 782 uint32_t num_handles = 0; | |
| 783 MojoResult res = MojoReadMessage( | |
| 784 control_handle, bytes, &num_bytes, nullptr, &num_handles, 0); | |
| 785 if (res != MOJO_RESULT_OK) { | |
| 786 SetNullReturn(arguments); | |
| 787 return; | |
| 788 } | |
| 789 | |
| 790 Dart_Handle list = Dart_NewList(3); | |
| 791 Dart_ListSetAt(list, 0, Dart_NewInteger(cd.handle)); | |
| 792 Dart_ListSetAt(list, 1, Dart_NewSendPort(cd.port)); | |
| 793 Dart_ListSetAt(list, 2, Dart_NewInteger(cd.data)); | |
| 794 Dart_SetReturnValue(arguments, list); | |
| 795 } | |
| 796 | |
| 797 static int64_t mojo_control_handle = MOJO_HANDLE_INVALID; | |
| 798 void MojoHandleWatcher_SetControlHandle(Dart_NativeArguments arguments) { | |
| 799 int64_t control_handle; | |
| 800 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); | |
| 801 mojo_control_handle = control_handle; | |
| 802 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | |
| 803 } | |
| 804 | |
| 805 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | |
| 806 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | |
| 807 } | |
| 808 | |
| 809 } // namespace blink | |
| OLD | NEW |