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 "mojo/edk/js/core.h" | 5 #include "mojo/edk/js/core.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 gin::Handle<HandleWrapper> handle) { | 282 gin::Handle<HandleWrapper> handle) { |
283 return (new DrainData(args->isolate(), handle->release()))->GetPromise(); | 283 return (new DrainData(args->isolate(), handle->release()))->GetPromise(); |
284 } | 284 } |
285 | 285 |
286 bool IsHandle(gin::Arguments* args, v8::Handle<v8::Value> val) { | 286 bool IsHandle(gin::Arguments* args, v8::Handle<v8::Value> val) { |
287 gin::Handle<mojo::edk::js::HandleWrapper> ignore_handle; | 287 gin::Handle<mojo::edk::js::HandleWrapper> ignore_handle; |
288 return gin::Converter<gin::Handle<mojo::edk::js::HandleWrapper>>::FromV8( | 288 return gin::Converter<gin::Handle<mojo::edk::js::HandleWrapper>>::FromV8( |
289 args->isolate(), val, &ignore_handle); | 289 args->isolate(), val, &ignore_handle); |
290 } | 290 } |
291 | 291 |
| 292 gin::Dictionary CreateSharedBuffer(const gin::Arguments& args, |
| 293 MojoCreateSharedBufferOptionsFlags flags, |
| 294 uint64_t num_bytes) { |
| 295 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 296 dictionary.Set("result", MOJO_RESULT_INVALID_ARGUMENT); |
| 297 |
| 298 MojoHandle handle = MOJO_HANDLE_INVALID; |
| 299 MojoCreateSharedBufferOptions options; |
| 300 options.flags = flags; |
| 301 MojoResult result = MojoCreateSharedBuffer(&options, num_bytes, &handle); |
| 302 if (result != MOJO_RESULT_OK) { |
| 303 return dictionary; |
| 304 } |
| 305 |
| 306 dictionary.Set("result", result); |
| 307 dictionary.Set("handle", mojo::Handle(handle)); |
| 308 |
| 309 return dictionary; |
| 310 } |
| 311 |
| 312 gin::Dictionary DuplicateSharedBufferHandle(const gin::Arguments& args, |
| 313 mojo::Handle handle, |
| 314 MojoDuplicateBufferHandleOptionsFlags flags) { |
| 315 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 316 dictionary.Set("result", MOJO_RESULT_INVALID_ARGUMENT); |
| 317 |
| 318 MojoHandle duped = MOJO_HANDLE_INVALID; |
| 319 MojoDuplicateBufferHandleOptions options; |
| 320 options.flags = flags; |
| 321 MojoResult result = |
| 322 MojoDuplicateBufferHandle(handle.value(), &options, &duped); |
| 323 if (result != MOJO_RESULT_OK) { |
| 324 return dictionary; |
| 325 } |
| 326 |
| 327 dictionary.Set("result", result); |
| 328 dictionary.Set("handle", mojo::Handle(duped)); |
| 329 |
| 330 return dictionary; |
| 331 } |
| 332 |
| 333 gin::Dictionary MapBuffer(const gin::Arguments& args, |
| 334 mojo::Handle handle, |
| 335 uint64_t offset, |
| 336 uint64_t num_bytes, |
| 337 MojoMapBufferFlags flags) { |
| 338 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 339 MojoResult result = MOJO_RESULT_INVALID_ARGUMENT; |
| 340 void* data = nullptr; |
| 341 result = MojoMapBuffer(handle.value(), offset, num_bytes, &data, flags); |
| 342 if (result != MOJO_RESULT_OK || !data) { |
| 343 dictionary.Set("result", result); |
| 344 return dictionary; |
| 345 } |
| 346 |
| 347 v8::Handle<v8::ArrayBuffer> array_buffer = |
| 348 v8::ArrayBuffer::New(args.isolate(), data, num_bytes); |
| 349 |
| 350 dictionary.Set("result", result); |
| 351 dictionary.Set("buffer", array_buffer); |
| 352 |
| 353 return dictionary; |
| 354 } |
| 355 |
| 356 MojoResult UnmapBuffer(const gin::Arguments& args, |
| 357 const gin::ArrayBuffer& buffer) { |
| 358 return MojoUnmapBuffer(buffer.bytes()); |
| 359 } |
292 | 360 |
293 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | 361 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
294 | 362 |
295 } // namespace | 363 } // namespace |
296 | 364 |
297 const char Core::kModuleName[] = "mojo/public/js/core"; | 365 const char Core::kModuleName[] = "mojo/public/js/core"; |
298 | 366 |
299 v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) { | 367 v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) { |
300 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 368 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
301 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | 369 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( |
302 &g_wrapper_info); | 370 &g_wrapper_info); |
303 | 371 |
304 if (templ.IsEmpty()) { | 372 if (templ.IsEmpty()) { |
305 templ = | 373 templ = |
306 gin::ObjectTemplateBuilder(isolate) | 374 gin::ObjectTemplateBuilder(isolate) |
307 // TODO(mpcomplete): Should these just be methods on the JS Handle | 375 // TODO(mpcomplete): Should these just be methods on the JS Handle |
308 // object? | 376 // object? |
309 .SetMethod("close", CloseHandle) | 377 .SetMethod("close", CloseHandle) |
310 .SetMethod("wait", WaitHandle) | 378 .SetMethod("wait", WaitHandle) |
311 .SetMethod("waitMany", WaitMany) | 379 .SetMethod("waitMany", WaitMany) |
312 .SetMethod("createMessagePipe", CreateMessagePipe) | 380 .SetMethod("createMessagePipe", CreateMessagePipe) |
313 .SetMethod("writeMessage", WriteMessage) | 381 .SetMethod("writeMessage", WriteMessage) |
314 .SetMethod("readMessage", ReadMessage) | 382 .SetMethod("readMessage", ReadMessage) |
315 .SetMethod("createDataPipe", CreateDataPipe) | 383 .SetMethod("createDataPipe", CreateDataPipe) |
316 .SetMethod("writeData", WriteData) | 384 .SetMethod("writeData", WriteData) |
317 .SetMethod("readData", ReadData) | 385 .SetMethod("readData", ReadData) |
318 .SetMethod("drainData", DoDrainData) | 386 .SetMethod("drainData", DoDrainData) |
319 .SetMethod("isHandle", IsHandle) | 387 .SetMethod("isHandle", IsHandle) |
| 388 .SetMethod("createSharedBuffer", CreateSharedBuffer) |
| 389 .SetMethod("duplicateSharedBufferHandle", |
| 390 DuplicateSharedBufferHandle) |
| 391 .SetMethod("mapBuffer", MapBuffer) |
| 392 .SetMethod("unmapBuffer", UnmapBuffer) |
320 | 393 |
321 .SetValue("RESULT_OK", MOJO_RESULT_OK) | 394 .SetValue("RESULT_OK", MOJO_RESULT_OK) |
322 .SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED) | 395 .SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED) |
323 .SetValue("RESULT_UNKNOWN", MOJO_RESULT_UNKNOWN) | 396 .SetValue("RESULT_UNKNOWN", MOJO_RESULT_UNKNOWN) |
324 .SetValue("RESULT_INVALID_ARGUMENT", MOJO_RESULT_INVALID_ARGUMENT) | 397 .SetValue("RESULT_INVALID_ARGUMENT", MOJO_RESULT_INVALID_ARGUMENT) |
325 .SetValue("RESULT_DEADLINE_EXCEEDED", MOJO_RESULT_DEADLINE_EXCEEDED) | 398 .SetValue("RESULT_DEADLINE_EXCEEDED", MOJO_RESULT_DEADLINE_EXCEEDED) |
326 .SetValue("RESULT_NOT_FOUND", MOJO_RESULT_NOT_FOUND) | 399 .SetValue("RESULT_NOT_FOUND", MOJO_RESULT_NOT_FOUND) |
327 .SetValue("RESULT_ALREADY_EXISTS", MOJO_RESULT_ALREADY_EXISTS) | 400 .SetValue("RESULT_ALREADY_EXISTS", MOJO_RESULT_ALREADY_EXISTS) |
328 .SetValue("RESULT_PERMISSION_DENIED", MOJO_RESULT_PERMISSION_DENIED) | 401 .SetValue("RESULT_PERMISSION_DENIED", MOJO_RESULT_PERMISSION_DENIED) |
329 .SetValue("RESULT_RESOURCE_EXHAUSTED", | 402 .SetValue("RESULT_RESOURCE_EXHAUSTED", |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 .SetValue("WRITE_DATA_FLAG_NONE", MOJO_WRITE_DATA_FLAG_NONE) | 435 .SetValue("WRITE_DATA_FLAG_NONE", MOJO_WRITE_DATA_FLAG_NONE) |
363 .SetValue("WRITE_DATA_FLAG_ALL_OR_NONE", | 436 .SetValue("WRITE_DATA_FLAG_ALL_OR_NONE", |
364 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE) | 437 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE) |
365 | 438 |
366 .SetValue("READ_DATA_FLAG_NONE", MOJO_READ_DATA_FLAG_NONE) | 439 .SetValue("READ_DATA_FLAG_NONE", MOJO_READ_DATA_FLAG_NONE) |
367 .SetValue("READ_DATA_FLAG_ALL_OR_NONE", | 440 .SetValue("READ_DATA_FLAG_ALL_OR_NONE", |
368 MOJO_READ_DATA_FLAG_ALL_OR_NONE) | 441 MOJO_READ_DATA_FLAG_ALL_OR_NONE) |
369 .SetValue("READ_DATA_FLAG_DISCARD", MOJO_READ_DATA_FLAG_DISCARD) | 442 .SetValue("READ_DATA_FLAG_DISCARD", MOJO_READ_DATA_FLAG_DISCARD) |
370 .SetValue("READ_DATA_FLAG_QUERY", MOJO_READ_DATA_FLAG_QUERY) | 443 .SetValue("READ_DATA_FLAG_QUERY", MOJO_READ_DATA_FLAG_QUERY) |
371 .SetValue("READ_DATA_FLAG_PEEK", MOJO_READ_DATA_FLAG_PEEK) | 444 .SetValue("READ_DATA_FLAG_PEEK", MOJO_READ_DATA_FLAG_PEEK) |
| 445 .SetValue("CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE", |
| 446 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE) |
| 447 |
| 448 .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE", |
| 449 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE) |
| 450 |
| 451 .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY", |
| 452 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY) |
| 453 |
| 454 .SetValue("MAP_BUFFER_FLAG_NONE", MOJO_MAP_BUFFER_FLAG_NONE) |
372 .Build(); | 455 .Build(); |
373 | 456 |
374 data->SetObjectTemplate(&g_wrapper_info, templ); | 457 data->SetObjectTemplate(&g_wrapper_info, templ); |
375 } | 458 } |
376 | 459 |
377 return templ->NewInstance(); | 460 return templ->NewInstance(); |
378 } | 461 } |
379 | 462 |
380 } // namespace js | 463 } // namespace js |
381 } // namespace edk | 464 } // namespace edk |
382 } // namespace mojo | 465 } // namespace mojo |
OLD | NEW |