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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/js/core.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/edk/js/core.cc
diff --git a/mojo/edk/js/core.cc b/mojo/edk/js/core.cc
index c8ccc37c536db220ef621fa5dc51299515236f24..46cfab6342afc78674fc9a93ed34b29636f00e37 100644
--- a/mojo/edk/js/core.cc
+++ b/mojo/edk/js/core.cc
@@ -289,6 +289,83 @@ bool IsHandle(gin::Arguments* args, v8::Handle<v8::Value> val) {
args->isolate(), val, &ignore_handle);
}
+gin::Dictionary CreateSharedBuffer(const gin::Arguments& args,
+ uint64_t num_bytes,
+ MojoCreateSharedBufferOptionsFlags flags) {
+ gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
+ MojoHandle handle = MOJO_HANDLE_INVALID;
+ MojoCreateSharedBufferOptions options;
+ options.flags = flags;
+ MojoResult result = MojoCreateSharedBuffer(&options, num_bytes, &handle);
+ if (result != MOJO_RESULT_OK) {
+ dictionary.Set("result", result);
+ return dictionary;
+ }
+
+ dictionary.Set("result", result);
+ dictionary.Set("handle", mojo::Handle(handle));
+
+ return dictionary;
+}
+
+gin::Dictionary DuplicateBufferHandle(
+ const gin::Arguments& args,
+ mojo::Handle handle,
+ MojoDuplicateBufferHandleOptionsFlags flags) {
+ gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
+ MojoHandle duped = MOJO_HANDLE_INVALID;
+ MojoDuplicateBufferHandleOptions options;
+ // The |flags| is mandatory parameter for DuplicateBufferHandle, and it will
+ // be always initialized in MojoDuplicateBufferHandleOptions struct. For now,
+ // since the struct has only one options field (flags), set struct_size to be
+ // equal to the size of MojoDuplicateBufferHandleOptions.
+ options.struct_size =
+ static_cast<uint32_t>(sizeof(MojoDuplicateBufferHandleOptions));
+ options.flags = flags;
+ MojoResult result =
+ MojoDuplicateBufferHandle(handle.value(), &options, &duped);
+ if (result != MOJO_RESULT_OK) {
+ dictionary.Set("result", result);
+ return dictionary;
+ }
+
+ dictionary.Set("result", result);
+ dictionary.Set("handle", mojo::Handle(duped));
+
+ return dictionary;
+}
+
+gin::Dictionary MapBuffer(const gin::Arguments& args,
+ mojo::Handle handle,
+ uint64_t offset,
+ uint64_t num_bytes,
+ MojoMapBufferFlags flags) {
+ gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate());
+ void* data = nullptr;
+ MojoResult result =
+ MojoMapBuffer(handle.value(), offset, num_bytes, &data, flags);
+ if (result != MOJO_RESULT_OK) {
+ dictionary.Set("result", result);
+ return dictionary;
+ }
+
+ v8::Handle<v8::ArrayBuffer> array_buffer =
+ v8::ArrayBuffer::New(args.isolate(), data, num_bytes);
+
+ dictionary.Set("result", result);
+ dictionary.Set("buffer", array_buffer);
+
+ return dictionary;
+}
+
+MojoResult UnmapBuffer(const gin::Arguments& args,
+ const v8::Handle<v8::ArrayBuffer>& buffer) {
+ // Buffer must be external, created by MapBuffer
+ if (!buffer->IsExternal())
+ return MOJO_RESULT_INVALID_ARGUMENT;
+
+ return MojoUnmapBuffer(buffer->GetContents().Data());
+}
gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
@@ -317,6 +394,10 @@ v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) {
.SetMethod("readData", ReadData)
.SetMethod("drainData", DoDrainData)
.SetMethod("isHandle", IsHandle)
+ .SetMethod("createSharedBuffer", CreateSharedBuffer)
+ .SetMethod("duplicateBufferHandle", DuplicateBufferHandle)
+ .SetMethod("mapBuffer", MapBuffer)
+ .SetMethod("unmapBuffer", UnmapBuffer)
.SetValue("RESULT_OK", MOJO_RESULT_OK)
.SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED)
@@ -369,6 +450,16 @@ v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) {
.SetValue("READ_DATA_FLAG_DISCARD", MOJO_READ_DATA_FLAG_DISCARD)
.SetValue("READ_DATA_FLAG_QUERY", MOJO_READ_DATA_FLAG_QUERY)
.SetValue("READ_DATA_FLAG_PEEK", MOJO_READ_DATA_FLAG_PEEK)
+ .SetValue("CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE",
+ MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE)
+
+ .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE",
+ MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE)
+
+ .SetValue("DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY",
+ MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY)
+
+ .SetValue("MAP_BUFFER_FLAG_NONE", MOJO_MAP_BUFFER_FLAG_NONE)
.Build();
data->SetObjectTemplate(&g_wrapper_info, templ);
« 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