Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/public/bindings/js/core.h" | 5 #include "mojo/public/bindings/js/core.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "gin/arguments.h" | 9 #include "gin/arguments.h" |
| 10 #include "gin/array_buffer.h" | 10 #include "gin/array_buffer.h" |
| 11 #include "gin/converter.h" | 11 #include "gin/converter.h" |
| 12 #include "gin/dictionary.h" | 12 #include "gin/dictionary.h" |
| 13 #include "gin/function_template.h" | 13 #include "gin/function_template.h" |
| 14 #include "gin/per_isolate_data.h" | 14 #include "gin/per_isolate_data.h" |
| 15 #include "gin/public/wrapper_info.h" | 15 #include "gin/public/wrapper_info.h" |
| 16 #include "mojo/public/bindings/js/handle.h" | 16 #include "mojo/public/bindings/js/handle.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace js { | 19 namespace js { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) { | 23 gin::Dictionary CreateMessagePipe(const gin::Arguments& args) { |
| 24 gin::Arguments args(info); | |
| 25 | |
| 26 MojoHandle handle_0 = MOJO_HANDLE_INVALID; | 24 MojoHandle handle_0 = MOJO_HANDLE_INVALID; |
| 27 MojoHandle handle_1 = MOJO_HANDLE_INVALID; | 25 MojoHandle handle_1 = MOJO_HANDLE_INVALID; |
| 28 MojoResult result = MojoCreateMessagePipe(&handle_0, &handle_1); | 26 MojoResult result = MojoCreateMessagePipe(&handle_0, &handle_1); |
| 29 CHECK(result == MOJO_RESULT_OK); | 27 CHECK(result == MOJO_RESULT_OK); |
| 30 | 28 |
| 31 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); | 29 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 32 dictionary.Set("handle0", handle_0); | 30 dictionary.Set("handle0", handle_0); |
| 33 dictionary.Set("handle1", handle_1); | 31 dictionary.Set("handle1", handle_1); |
| 34 args.Return(dictionary); | 32 return dictionary; |
| 35 } | 33 } |
| 36 | 34 |
| 37 void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { | 35 MojoResult WriteMessage(MojoHandle handle, |
| 38 gin::Arguments args(info); | 36 const gin::ArrayBufferView& buffer, |
| 39 | 37 const std::vector<MojoHandle>& handles, |
| 40 MojoHandle handle = MOJO_HANDLE_INVALID; | 38 MojoWriteMessageFlags flags) { |
| 41 gin::ArrayBufferView buffer(args.isolate()); | 39 return MojoWriteMessage(handle, |
| 42 std::vector<MojoHandle> handles; | 40 buffer.bytes(), |
| 43 MojoWriteMessageFlags flags = MOJO_WRITE_MESSAGE_FLAG_NONE; | 41 static_cast<uint32_t>(buffer.num_bytes()), |
| 44 | 42 handles.empty() ? NULL : handles.data(), |
| 45 if (!args.GetNext(&handle) || | 43 static_cast<uint32_t>(handles.size()), |
| 46 !args.GetNext(&buffer) || | 44 flags); |
| 47 !args.GetNext(&handles) || | |
| 48 !args.GetNext(&flags)) { | |
| 49 return args.ThrowError(); | |
| 50 } | |
| 51 | |
| 52 args.Return(MojoWriteMessage(handle, | |
| 53 buffer.bytes(), | |
| 54 static_cast<uint32_t>(buffer.num_bytes()), | |
| 55 handles.empty() ? NULL : handles.data(), | |
| 56 static_cast<uint32_t>(handles.size()), | |
| 57 flags)); | |
| 58 } | 45 } |
| 59 | 46 |
| 60 void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { | 47 gin::Dictionary ReadMessage(const gin::Arguments& args, MojoHandle handle, |
| 61 gin::Arguments args(info); | 48 MojoReadMessageFlags flags) { |
| 62 | |
| 63 MojoHandle handle = MOJO_HANDLE_INVALID; | |
| 64 MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE; | |
| 65 | |
| 66 if (!args.GetNext(&handle) || | |
| 67 !args.GetNext(&flags)) { | |
| 68 return args.ThrowError(); | |
| 69 } | |
| 70 | |
| 71 uint32_t num_bytes = 0; | 49 uint32_t num_bytes = 0; |
| 72 uint32_t num_handles = 0; | 50 uint32_t num_handles = 0; |
| 73 MojoResult result = MojoReadMessage( | 51 MojoResult result = MojoReadMessage( |
| 74 handle, NULL, &num_bytes, NULL, &num_handles, flags); | 52 handle, NULL, &num_bytes, NULL, &num_handles, flags); |
| 75 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { | 53 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 76 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty( | 54 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 77 info.GetIsolate()); | |
| 78 dictionary.Set("result", result); | 55 dictionary.Set("result", result); |
| 79 args.Return(dictionary); | 56 return dictionary; |
| 80 } | 57 } |
| 81 | 58 |
| 82 v8::Handle<v8::ArrayBuffer> array_buffer = v8::ArrayBuffer::New(num_bytes); | 59 v8::Handle<v8::ArrayBuffer> array_buffer = v8::ArrayBuffer::New(num_bytes); |
| 83 std::vector<MojoHandle> handles(num_handles); | 60 std::vector<MojoHandle> handles(num_handles); |
| 84 | 61 |
| 85 gin::ArrayBuffer buffer(args.isolate()); | 62 gin::ArrayBuffer buffer; |
| 86 ConvertFromV8(array_buffer, &buffer); | 63 ConvertFromV8(args.isolate(), array_buffer, &buffer); |
| 87 CHECK(buffer.num_bytes() == num_bytes); | 64 CHECK(buffer.num_bytes() == num_bytes); |
| 88 | 65 |
| 89 result = MojoReadMessage(handle, | 66 result = MojoReadMessage(handle, |
| 90 buffer.bytes(), | 67 buffer.bytes(), |
| 91 &num_bytes, | 68 &num_bytes, |
| 92 handles.empty() ? NULL : handles.data(), | 69 handles.empty() ? NULL : handles.data(), |
| 93 &num_handles, | 70 &num_handles, |
| 94 flags); | 71 flags); |
| 95 | 72 |
| 96 CHECK(buffer.num_bytes() == num_bytes); | 73 CHECK(buffer.num_bytes() == num_bytes); |
| 97 CHECK(handles.size() == num_handles); | 74 CHECK(handles.size() == num_handles); |
| 98 | 75 |
| 99 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); | 76 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(args.isolate()); |
| 100 dictionary.Set("result", result); | 77 dictionary.Set("result", result); |
| 101 dictionary.Set("buffer", array_buffer); | 78 dictionary.Set("buffer", array_buffer); |
| 102 dictionary.Set("handles", handles); | 79 dictionary.Set("handles", handles); |
| 103 args.Return(dictionary); | 80 return dictionary; |
| 104 } | 81 } |
| 105 | 82 |
| 106 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | 83 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
| 107 | 84 |
| 108 } // namespace | 85 } // namespace |
| 109 | 86 |
| 110 const char Core::kModuleName[] = "mojo/public/bindings/js/core"; | 87 const char Core::kModuleName[] = "mojo/public/bindings/js/core"; |
| 111 | 88 |
| 112 v8::Local<v8::ObjectTemplate> Core::GetTemplate(v8::Isolate* isolate) { | 89 v8::Local<v8::ObjectTemplate> Core::GetTemplate(v8::Isolate* isolate) { |
| 113 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 90 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
| 114 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | 91 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( |
| 115 &g_wrapper_info); | 92 &g_wrapper_info); |
| 116 | 93 |
| 117 if (templ.IsEmpty()) { | 94 if (templ.IsEmpty()) { |
| 118 templ = v8::ObjectTemplate::New(); | 95 templ = v8::ObjectTemplate::New(); |
| 119 | 96 |
| 120 templ->Set(gin::StringToSymbol(isolate, "close"), | 97 templ->Set(gin::StringToSymbol(isolate, "close"), |
| 121 gin::CreateFunctionTemplate(isolate, | 98 gin::CreateFunctionTemplate(isolate, |
| 122 base::Bind(mojo::CloseRaw))); | 99 base::Bind(mojo::CloseRaw))); |
| 123 templ->Set(gin::StringToSymbol(isolate, "wait"), | 100 templ->Set(gin::StringToSymbol(isolate, "wait"), |
| 124 gin::CreateFunctionTemplate(isolate, base::Bind(mojo::Wait))); | 101 gin::CreateFunctionTemplate(isolate, |
| 102 base::Bind(mojo::Wait))); | |
| 125 templ->Set(gin::StringToSymbol(isolate, "waitMany"), | 103 templ->Set(gin::StringToSymbol(isolate, "waitMany"), |
| 126 gin::CreateFunctionTemplate(isolate, | 104 gin::CreateFunctionTemplate(isolate, |
| 127 base::Bind(mojo::WaitMany<std::vector<mojo::Handle>, | 105 base::Bind(mojo::WaitMany<std::vector<mojo::Handle>, |
| 128 std::vector<MojoWaitFlags> >))); | 106 std::vector<MojoWaitFlags> >))); |
| 129 templ->Set(gin::StringToSymbol(isolate, "createMessagePipe"), | 107 templ->Set(gin::StringToSymbol(isolate, "createMessagePipe"), |
| 130 v8::FunctionTemplate::New(CreateMessagePipe)); | 108 gin::CreateFunctionTemplate(isolate, |
| 109 base::Bind(CreateMessagePipe))); | |
| 131 templ->Set(gin::StringToSymbol(isolate, "writeMessage"), | 110 templ->Set(gin::StringToSymbol(isolate, "writeMessage"), |
| 132 v8::FunctionTemplate::New(WriteMessage)); | 111 gin::CreateFunctionTemplate(isolate, |
| 112 base::Bind(WriteMessage))); | |
| 133 templ->Set(gin::StringToSymbol(isolate, "readMessage"), | 113 templ->Set(gin::StringToSymbol(isolate, "readMessage"), |
| 134 v8::FunctionTemplate::New(ReadMessage)); | 114 gin::CreateFunctionTemplate(isolate, |
| 115 base::Bind(ReadMessage))); | |
|
abarth-chromium
2013/11/27 00:20:10
Nice!
| |
| 135 | 116 |
| 136 // TODO(vtl): Change name of "kInvalidHandle", now that there's no such C++ | 117 // TODO(vtl): Change name of "kInvalidHandle", now that there's no such C++ |
| 137 // constant? | 118 // constant? |
| 138 templ->Set(gin::StringToSymbol(isolate, "kInvalidHandle"), | 119 templ->Set(gin::StringToSymbol(isolate, "kInvalidHandle"), |
| 139 gin::ConvertToV8(isolate, mojo::Handle())); | 120 gin::ConvertToV8(isolate, mojo::Handle())); |
| 140 | 121 |
| 141 templ->Set(gin::StringToSymbol(isolate, "RESULT_OK"), | 122 templ->Set(gin::StringToSymbol(isolate, "RESULT_OK"), |
| 142 gin::ConvertToV8(isolate, MOJO_RESULT_OK)); | 123 gin::ConvertToV8(isolate, MOJO_RESULT_OK)); |
| 143 templ->Set(gin::StringToSymbol(isolate, "RESULT_CANCELLED"), | 124 templ->Set(gin::StringToSymbol(isolate, "RESULT_CANCELLED"), |
| 144 gin::ConvertToV8(isolate, MOJO_RESULT_CANCELLED)); | 125 gin::ConvertToV8(isolate, MOJO_RESULT_CANCELLED)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)); | 173 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)); |
| 193 | 174 |
| 194 data->SetObjectTemplate(&g_wrapper_info, templ); | 175 data->SetObjectTemplate(&g_wrapper_info, templ); |
| 195 } | 176 } |
| 196 | 177 |
| 197 return templ; | 178 return templ; |
| 198 } | 179 } |
| 199 | 180 |
| 200 } // namespace js | 181 } // namespace js |
| 201 } // namespace mojo | 182 } // namespace mojo |
| OLD | NEW |