| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "mojo/apps/js/bindings/core.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "gin/arguments.h" | |
| 10 #include "gin/array_buffer.h" | |
| 11 #include "gin/converter.h" | |
| 12 #include "gin/dictionary.h" | |
| 13 #include "gin/function_template.h" | |
| 14 #include "gin/object_template_builder.h" | |
| 15 #include "gin/per_isolate_data.h" | |
| 16 #include "gin/public/wrapper_info.h" | |
| 17 #include "mojo/apps/js/bindings/handle.h" | |
| 18 | |
| 19 namespace mojo { | |
| 20 namespace js { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 gin::Dictionary CreateMessagePipe(const gin::Arguments& args) { | |
| 25 MojoHandle handle0 = MOJO_HANDLE_INVALID; | |
| 26 MojoHandle handle1 = MOJO_HANDLE_INVALID; | |
| 27 MojoResult result = MojoCreateMessagePipe(&handle0, &handle1); | |
| 28 CHECK(result == MOJO_RESULT_OK); | |
| 29 | |
| 30 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); | |
| 31 dictionary.Set("handle0", handle0); | |
| 32 dictionary.Set("handle1", handle1); | |
| 33 return dictionary; | |
| 34 } | |
| 35 | |
| 36 MojoResult WriteMessage(MojoHandle handle, | |
| 37 const gin::ArrayBufferView& buffer, | |
| 38 const std::vector<MojoHandle>& handles, | |
| 39 MojoWriteMessageFlags flags) { | |
| 40 return MojoWriteMessage(handle, | |
| 41 buffer.bytes(), | |
| 42 static_cast<uint32_t>(buffer.num_bytes()), | |
| 43 handles.empty() ? NULL : handles.data(), | |
| 44 static_cast<uint32_t>(handles.size()), | |
| 45 flags); | |
| 46 } | |
| 47 | |
| 48 gin::Dictionary ReadMessage(const gin::Arguments& args, MojoHandle handle, | |
| 49 MojoReadMessageFlags flags) { | |
| 50 uint32_t num_bytes = 0; | |
| 51 uint32_t num_handles = 0; | |
| 52 MojoResult result = MojoReadMessage( | |
| 53 handle, NULL, &num_bytes, NULL, &num_handles, flags); | |
| 54 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { | |
| 55 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); | |
| 56 dictionary.Set("result", result); | |
| 57 return dictionary; | |
| 58 } | |
| 59 | |
| 60 v8::Handle<v8::ArrayBuffer> array_buffer = | |
| 61 v8::ArrayBuffer::New(args.isolate(), num_bytes); | |
| 62 std::vector<MojoHandle> handles(num_handles); | |
| 63 | |
| 64 gin::ArrayBuffer buffer; | |
| 65 ConvertFromV8(args.isolate(), array_buffer, &buffer); | |
| 66 CHECK(buffer.num_bytes() == num_bytes); | |
| 67 | |
| 68 result = MojoReadMessage(handle, | |
| 69 buffer.bytes(), | |
| 70 &num_bytes, | |
| 71 handles.empty() ? NULL : handles.data(), | |
| 72 &num_handles, | |
| 73 flags); | |
| 74 | |
| 75 CHECK(buffer.num_bytes() == num_bytes); | |
| 76 CHECK(handles.size() == num_handles); | |
| 77 | |
| 78 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); | |
| 79 dictionary.Set("result", result); | |
| 80 dictionary.Set("buffer", array_buffer); | |
| 81 dictionary.Set("handles", handles); | |
| 82 return dictionary; | |
| 83 } | |
| 84 | |
| 85 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 const char Core::kModuleName[] = "mojo/apps/js/bindings/core"; | |
| 90 | |
| 91 v8::Local<v8::Value> Core::GetModule(v8::Isolate* isolate) { | |
| 92 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | |
| 93 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | |
| 94 &g_wrapper_info); | |
| 95 | |
| 96 if (templ.IsEmpty()) { | |
| 97 templ = gin::ObjectTemplateBuilder(isolate) | |
| 98 .SetMethod("close", mojo::CloseRaw) | |
| 99 .SetMethod("wait", mojo::Wait) | |
| 100 .SetMethod("waitMany", mojo::WaitMany<std::vector<mojo::Handle>, | |
| 101 std::vector<MojoWaitFlags> >) | |
| 102 .SetMethod("createMessagePipe", CreateMessagePipe) | |
| 103 .SetMethod("writeMessage", WriteMessage) | |
| 104 .SetMethod("readMessage", ReadMessage) | |
| 105 | |
| 106 // TODO(vtl): Change name of "kInvalidHandle", now that there's no such | |
| 107 // C++ constant? | |
| 108 .SetValue("kInvalidHandle", mojo::Handle()) | |
| 109 | |
| 110 .SetValue("RESULT_OK", MOJO_RESULT_OK) | |
| 111 .SetValue("RESULT_CANCELLED", MOJO_RESULT_CANCELLED) | |
| 112 .SetValue("RESULT_UNKNOWN", MOJO_RESULT_UNKNOWN) | |
| 113 .SetValue("RESULT_INVALID_ARGUMENT", MOJO_RESULT_INVALID_ARGUMENT) | |
| 114 .SetValue("RESULT_DEADLINE_EXCEEDED", MOJO_RESULT_DEADLINE_EXCEEDED) | |
| 115 .SetValue("RESULT_NOT_FOUND", MOJO_RESULT_NOT_FOUND) | |
| 116 .SetValue("RESULT_ALREADY_EXISTS", MOJO_RESULT_ALREADY_EXISTS) | |
| 117 .SetValue("RESULT_PERMISSION_DENIED", MOJO_RESULT_PERMISSION_DENIED) | |
| 118 .SetValue("RESULT_RESOURCE_EXHAUSTED", MOJO_RESULT_RESOURCE_EXHAUSTED) | |
| 119 .SetValue("RESULT_FAILED_PRECONDITION", MOJO_RESULT_FAILED_PRECONDITION) | |
| 120 .SetValue("RESULT_ABORTED", MOJO_RESULT_ABORTED) | |
| 121 .SetValue("RESULT_OUT_OF_RANGE", MOJO_RESULT_OUT_OF_RANGE) | |
| 122 .SetValue("RESULT_UNIMPLEMENTED", MOJO_RESULT_UNIMPLEMENTED) | |
| 123 .SetValue("RESULT_INTERNAL", MOJO_RESULT_INTERNAL) | |
| 124 .SetValue("RESULT_UNAVAILABLE", MOJO_RESULT_UNAVAILABLE) | |
| 125 .SetValue("RESULT_DATA_LOSS", MOJO_RESULT_DATA_LOSS) | |
| 126 .SetValue("RESULT_BUSY", MOJO_RESULT_BUSY) | |
| 127 .SetValue("RESULT_SHOULD_WAIT", MOJO_RESULT_SHOULD_WAIT) | |
| 128 | |
| 129 .SetValue("DEADLINE_INDEFINITE", MOJO_DEADLINE_INDEFINITE) | |
| 130 | |
| 131 .SetValue("WAIT_FLAG_NONE", MOJO_WAIT_FLAG_NONE) | |
| 132 .SetValue("WAIT_FLAG_READABLE", MOJO_WAIT_FLAG_READABLE) | |
| 133 .SetValue("WAIT_FLAG_READABLE", MOJO_WAIT_FLAG_READABLE) | |
| 134 .SetValue("WAIT_FLAG_EVERYTHING", MOJO_WAIT_FLAG_EVERYTHING) | |
| 135 | |
| 136 .SetValue("WRITE_MESSAGE_FLAG_NONE", MOJO_WRITE_MESSAGE_FLAG_NONE) | |
| 137 | |
| 138 .SetValue("READ_MESSAGE_FLAG_NONE", MOJO_READ_MESSAGE_FLAG_NONE) | |
| 139 .SetValue("READ_MESSAGE_FLAG_MAY_DISCARD", | |
| 140 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD) | |
| 141 .Build(); | |
| 142 | |
| 143 data->SetObjectTemplate(&g_wrapper_info, templ); | |
| 144 } | |
| 145 | |
| 146 return templ->NewInstance(); | |
| 147 } | |
| 148 | |
| 149 } // namespace js | |
| 150 } // namespace mojo | |
| OLD | NEW |