Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: mojo/edk/js/core.cc

Issue 2131163002: [mojo] Add shared buffer support to mojo JavaScript bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initialize options.struct_size Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/js/core.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 uint64_t num_bytes,
294 MojoCreateSharedBufferOptionsFlags flags) {
295 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
296 MojoHandle handle = MOJO_HANDLE_INVALID;
297 MojoCreateSharedBufferOptions options;
298 options.flags = flags;
299 MojoResult result = MojoCreateSharedBuffer(&options, num_bytes, &handle);
300 if (result != MOJO_RESULT_OK) {
301 dictionary.Set("result", result);
302 return dictionary;
303 }
304
305 dictionary.Set("result", result);
306 dictionary.Set("handle", mojo::Handle(handle));
307
308 return dictionary;
309 }
310
311 gin::Dictionary DuplicateBufferHandle(
312 const gin::Arguments& args,
313 mojo::Handle handle,
314 MojoDuplicateBufferHandleOptionsFlags flags) {
315 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
316 MojoHandle duped = MOJO_HANDLE_INVALID;
317 MojoDuplicateBufferHandleOptions options;
318 // The |flags| is mandatory parameter for DuplicateBufferHandle, and it will
319 // be always initialized in MojoDuplicateBufferHandleOptions struct. For now,
320 // since the struct has only one options field (flags), set struct_size to be
321 // equal to the size of MojoDuplicateBufferHandleOptions.
322 options.struct_size =
323 static_cast<uint32_t>(sizeof(MojoDuplicateBufferHandleOptions));
324 options.flags = flags;
325 MojoResult result =
326 MojoDuplicateBufferHandle(handle.value(), &options, &duped);
327 if (result != MOJO_RESULT_OK) {
328 dictionary.Set("result", result);
329 return dictionary;
330 }
331
332 dictionary.Set("result", result);
333 dictionary.Set("handle", mojo::Handle(duped));
334
335 return dictionary;
336 }
337
338 gin::Dictionary MapBuffer(const gin::Arguments& args,
339 mojo::Handle handle,
340 uint64_t offset,
341 uint64_t num_bytes,
342 MojoMapBufferFlags flags) {
343 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
344 void* data = nullptr;
345 MojoResult result =
346 MojoMapBuffer(handle.value(), offset, num_bytes, &data, flags);
347 if (result != MOJO_RESULT_OK) {
348 dictionary.Set("result", result);
349 return dictionary;
350 }
351
352 v8::Handle<v8::ArrayBuffer> array_buffer =
353 v8::ArrayBuffer::New(args.isolate(), data, num_bytes);
354
355 dictionary.Set("result", result);
356 dictionary.Set("buffer", array_buffer);
357
358 return dictionary;
359 }
360
361 MojoResult UnmapBuffer(const gin::Arguments& args,
362 const v8::Handle<v8::ArrayBuffer>& buffer) {
363 // Buffer must be external, created by MapBuffer
364 if (!buffer->IsExternal())
365 return MOJO_RESULT_INVALID_ARGUMENT;
366
367 return MojoUnmapBuffer(buffer->GetContents().Data());
368 }
292 369
293 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; 370 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
294 371
295 } // namespace 372 } // namespace
296 373
297 const char Core::kModuleName[] = "mojo/public/js/core"; 374 const char Core::kModuleName[] = "mojo/public/js/core";
298 375
299 v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) { 376 v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) {
300 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); 377 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
301 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( 378 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
302 &g_wrapper_info); 379 &g_wrapper_info);
303 380
304 if (templ.IsEmpty()) { 381 if (templ.IsEmpty()) {
305 templ = 382 templ =
306 gin::ObjectTemplateBuilder(isolate) 383 gin::ObjectTemplateBuilder(isolate)
307 // TODO(mpcomplete): Should these just be methods on the JS Handle 384 // TODO(mpcomplete): Should these just be methods on the JS Handle
308 // object? 385 // object?
309 .SetMethod("close", CloseHandle) 386 .SetMethod("close", CloseHandle)
310 .SetMethod("wait", WaitHandle) 387 .SetMethod("wait", WaitHandle)
311 .SetMethod("waitMany", WaitMany) 388 .SetMethod("waitMany", WaitMany)
312 .SetMethod("createMessagePipe", CreateMessagePipe) 389 .SetMethod("createMessagePipe", CreateMessagePipe)
313 .SetMethod("writeMessage", WriteMessage) 390 .SetMethod("writeMessage", WriteMessage)
314 .SetMethod("readMessage", ReadMessage) 391 .SetMethod("readMessage", ReadMessage)
315 .SetMethod("createDataPipe", CreateDataPipe) 392 .SetMethod("createDataPipe", CreateDataPipe)
316 .SetMethod("writeData", WriteData) 393 .SetMethod("writeData", WriteData)
317 .SetMethod("readData", ReadData) 394 .SetMethod("readData", ReadData)
318 .SetMethod("drainData", DoDrainData) 395 .SetMethod("drainData", DoDrainData)
319 .SetMethod("isHandle", IsHandle) 396 .SetMethod("isHandle", IsHandle)
397 .SetMethod("createSharedBuffer", CreateSharedBuffer)
398 .SetMethod("duplicateBufferHandle", DuplicateBufferHandle)
399 .SetMethod("mapBuffer", MapBuffer)
400 .SetMethod("unmapBuffer", UnmapBuffer)
320 401
321 .SetValue("RESULT_OK", MOJO_RESULT_OK) 402 .SetValue("RESULT_OK", MOJO_RESULT_OK)
322 .SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED) 403 .SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED)
323 .SetValue("RESULT_UNKNOWN", MOJO_RESULT_UNKNOWN) 404 .SetValue("RESULT_UNKNOWN", MOJO_RESULT_UNKNOWN)
324 .SetValue("RESULT_INVALID_ARGUMENT", MOJO_RESULT_INVALID_ARGUMENT) 405 .SetValue("RESULT_INVALID_ARGUMENT", MOJO_RESULT_INVALID_ARGUMENT)
325 .SetValue("RESULT_DEADLINE_EXCEEDED", MOJO_RESULT_DEADLINE_EXCEEDED) 406 .SetValue("RESULT_DEADLINE_EXCEEDED", MOJO_RESULT_DEADLINE_EXCEEDED)
326 .SetValue("RESULT_NOT_FOUND", MOJO_RESULT_NOT_FOUND) 407 .SetValue("RESULT_NOT_FOUND", MOJO_RESULT_NOT_FOUND)
327 .SetValue("RESULT_ALREADY_EXISTS", MOJO_RESULT_ALREADY_EXISTS) 408 .SetValue("RESULT_ALREADY_EXISTS", MOJO_RESULT_ALREADY_EXISTS)
328 .SetValue("RESULT_PERMISSION_DENIED", MOJO_RESULT_PERMISSION_DENIED) 409 .SetValue("RESULT_PERMISSION_DENIED", MOJO_RESULT_PERMISSION_DENIED)
329 .SetValue("RESULT_RESOURCE_EXHAUSTED", 410 .SetValue("RESULT_RESOURCE_EXHAUSTED",
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 .SetValue("WRITE_DATA_FLAG_NONE", MOJO_WRITE_DATA_FLAG_NONE) 443 .SetValue("WRITE_DATA_FLAG_NONE", MOJO_WRITE_DATA_FLAG_NONE)
363 .SetValue("WRITE_DATA_FLAG_ALL_OR_NONE", 444 .SetValue("WRITE_DATA_FLAG_ALL_OR_NONE",
364 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE) 445 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)
365 446
366 .SetValue("READ_DATA_FLAG_NONE", MOJO_READ_DATA_FLAG_NONE) 447 .SetValue("READ_DATA_FLAG_NONE", MOJO_READ_DATA_FLAG_NONE)
367 .SetValue("READ_DATA_FLAG_ALL_OR_NONE", 448 .SetValue("READ_DATA_FLAG_ALL_OR_NONE",
368 MOJO_READ_DATA_FLAG_ALL_OR_NONE) 449 MOJO_READ_DATA_FLAG_ALL_OR_NONE)
369 .SetValue("READ_DATA_FLAG_DISCARD", MOJO_READ_DATA_FLAG_DISCARD) 450 .SetValue("READ_DATA_FLAG_DISCARD", MOJO_READ_DATA_FLAG_DISCARD)
370 .SetValue("READ_DATA_FLAG_QUERY", MOJO_READ_DATA_FLAG_QUERY) 451 .SetValue("READ_DATA_FLAG_QUERY", MOJO_READ_DATA_FLAG_QUERY)
371 .SetValue("READ_DATA_FLAG_PEEK", MOJO_READ_DATA_FLAG_PEEK) 452 .SetValue("READ_DATA_FLAG_PEEK", MOJO_READ_DATA_FLAG_PEEK)
453 .SetValue("CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE",
454 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE)
455
456 .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE",
457 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE)
458
459 .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY",
460 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY)
461
462 .SetValue("MAP_BUFFER_FLAG_NONE", MOJO_MAP_BUFFER_FLAG_NONE)
372 .Build(); 463 .Build();
373 464
374 data->SetObjectTemplate(&g_wrapper_info, templ); 465 data->SetObjectTemplate(&g_wrapper_info, templ);
375 } 466 }
376 467
377 return templ->NewInstance(); 468 return templ->NewInstance();
378 } 469 }
379 470
380 } // namespace js 471 } // namespace js
381 } // namespace edk 472 } // namespace edk
382 } // namespace mojo 473 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/public/js/core.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698