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 <assert.h> |
| 8 |
7 #include "gin/arguments.h" | 9 #include "gin/arguments.h" |
8 #include "gin/array_buffer.h" | 10 #include "gin/array_buffer.h" |
9 #include "gin/converter.h" | 11 #include "gin/converter.h" |
10 #include "gin/dictionary.h" | 12 #include "gin/dictionary.h" |
11 #include "gin/per_isolate_data.h" | 13 #include "gin/per_isolate_data.h" |
12 #include "gin/public/wrapper_info.h" | 14 #include "gin/public/wrapper_info.h" |
13 #include "mojo/public/bindings/js/handle.h" | 15 #include "mojo/public/bindings/js/handle.h" |
14 | 16 |
15 namespace mojo { | 17 namespace mojo { |
16 namespace js { | 18 namespace js { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 !args.GetNext(&deadline)) { | 57 !args.GetNext(&deadline)) { |
56 return args.ThrowError(); | 58 return args.ThrowError(); |
57 } | 59 } |
58 | 60 |
59 args.Return(mojo::WaitMany(handles, flags, deadline)); | 61 args.Return(mojo::WaitMany(handles, flags, deadline)); |
60 } | 62 } |
61 | 63 |
62 void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) { | 64 void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) { |
63 gin::Arguments args(info); | 65 gin::Arguments args(info); |
64 | 66 |
65 mojo::ScopedMessagePipeHandle handle_0; | 67 MojoHandle handle_0 = MOJO_HANDLE_INVALID; |
66 mojo::ScopedMessagePipeHandle handle_1; | 68 MojoHandle handle_1 = MOJO_HANDLE_INVALID; |
67 mojo::CreateMessagePipe(&handle_0, &handle_1); | 69 MojoResult result = MojoCreateMessagePipe(&handle_0, &handle_1); |
| 70 assert(result == MOJO_RESULT_OK); |
68 | 71 |
69 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); | 72 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); |
70 dictionary.Set("handle0", static_cast<mojo::Handle>(handle_0.release())); | 73 dictionary.Set("handle0", handle_0); |
71 dictionary.Set("handle1", static_cast<mojo::Handle>(handle_1.release())); | 74 dictionary.Set("handle1", handle_1); |
72 args.Return(dictionary); | 75 args.Return(dictionary); |
73 } | 76 } |
74 | 77 |
75 void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { | 78 void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { |
76 gin::Arguments args(info); | 79 gin::Arguments args(info); |
77 | 80 |
78 mojo::Handle handle; | 81 MojoHandle handle = MOJO_HANDLE_INVALID; |
79 gin::ArrayBufferView buffer(args.isolate()); | 82 gin::ArrayBufferView buffer(args.isolate()); |
80 std::vector<mojo::Handle> handles; | 83 std::vector<MojoHandle> handles; |
81 MojoWriteMessageFlags flags = MOJO_WRITE_MESSAGE_FLAG_NONE; | 84 MojoWriteMessageFlags flags = MOJO_WRITE_MESSAGE_FLAG_NONE; |
82 | 85 |
83 if (!args.GetNext(&handle) || | 86 if (!args.GetNext(&handle) || |
84 !args.GetNext(&buffer) || | 87 !args.GetNext(&buffer) || |
85 !args.GetNext(&handles) || | 88 !args.GetNext(&handles) || |
86 !args.GetNext(&flags)) { | 89 !args.GetNext(&flags)) { |
87 return args.ThrowError(); | 90 return args.ThrowError(); |
88 } | 91 } |
89 | 92 |
90 args.Return(mojo::WriteMessageRaw( | 93 args.Return(MojoWriteMessage(handle, |
91 MessagePipeHandle(handle.value()), buffer.bytes(), | 94 buffer.bytes(), |
92 static_cast<uint32_t>(buffer.num_bytes()), | 95 static_cast<uint32_t>(buffer.num_bytes()), |
93 handles.empty() ? NULL : reinterpret_cast<const MojoHandle*>(&handles[0]), | 96 handles.empty() ? NULL : handles.data(), |
94 static_cast<uint32_t>(handles.size()), flags)); | 97 static_cast<uint32_t>(handles.size()), |
| 98 flags)); |
95 } | 99 } |
96 | 100 |
97 void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { | 101 void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) { |
98 gin::Arguments args(info); | 102 gin::Arguments args(info); |
99 | 103 |
100 mojo::Handle handle; | 104 MojoHandle handle = MOJO_HANDLE_INVALID; |
101 gin::ArrayBufferView buffer(args.isolate()); | |
102 uint32_t num_handles = 0; | |
103 MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE; | 105 MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE; |
104 | 106 |
105 if (!args.GetNext(&handle) || | 107 if (!args.GetNext(&handle) || |
106 !args.GetNext(&buffer) || | |
107 !args.GetNext(&num_handles) || | |
108 !args.GetNext(&flags)) { | 108 !args.GetNext(&flags)) { |
109 return args.ThrowError(); | 109 return args.ThrowError(); |
110 } | 110 } |
111 | 111 |
112 uint32_t num_bytes = static_cast<uint32_t>(buffer.num_bytes()); | 112 uint32_t num_bytes = 0; |
113 std::vector<mojo::Handle> handles(num_handles); | 113 uint32_t num_handles = 0; |
114 MojoResult result = mojo::ReadMessageRaw( | 114 MojoResult result = MojoReadMessage( |
115 MessagePipeHandle(handle.value()), buffer.bytes(), &num_bytes, | 115 handle, NULL, &num_bytes, NULL, &num_handles, flags); |
116 handles.empty() ? NULL : reinterpret_cast<MojoHandle*>(&handles[0]), | 116 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { |
117 &num_handles, flags); | 117 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty( |
118 handles.resize(num_handles); | 118 info.GetIsolate()); |
| 119 dictionary.Set("result", result); |
| 120 args.Return(dictionary); |
| 121 } |
119 | 122 |
120 // TODO(abarth): We should benchmark this codepath to make sure it's ok to | 123 v8::Handle<v8::ArrayBuffer> array_buffer = v8::ArrayBuffer::New(num_bytes); |
121 // allocate all this memory on each read. | 124 std::vector<MojoHandle> handles(num_handles); |
| 125 |
| 126 gin::ArrayBuffer buffer(args.isolate()); |
| 127 ConvertFromV8(array_buffer, &buffer); |
| 128 assert(buffer.num_bytes() == num_bytes); |
| 129 |
| 130 result = MojoReadMessage(handle, |
| 131 buffer.bytes(), |
| 132 &num_bytes, |
| 133 handles.empty() ? NULL : handles.data(), |
| 134 &num_handles, |
| 135 flags); |
| 136 |
| 137 assert(buffer.num_bytes() == num_bytes); |
| 138 assert(handles.size() == num_handles); |
| 139 |
122 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); | 140 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate()); |
123 dictionary.Set("result", result); | 141 dictionary.Set("result", result); |
124 dictionary.Set("bytesRead", num_bytes); | 142 dictionary.Set("buffer", array_buffer); |
125 dictionary.Set("handles", handles); | 143 dictionary.Set("handles", handles); |
126 args.Return(dictionary); | 144 args.Return(dictionary); |
127 } | 145 } |
128 | 146 |
129 gin::WrapperInfo g_core_wrapper_info = { gin::kEmbedderNativeGin }; | 147 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
130 | 148 |
131 } // namespace | 149 } // namespace |
132 | 150 |
133 const char Core::kModuleName[] = "mojo/public/bindings/js/core"; | 151 const char Core::kModuleName[] = "mojo/public/bindings/js/core"; |
134 | 152 |
135 v8::Local<v8::ObjectTemplate> Core::GetTemplate(v8::Isolate* isolate) { | 153 v8::Local<v8::ObjectTemplate> Core::GetTemplate(v8::Isolate* isolate) { |
136 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 154 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
137 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | 155 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( |
138 &g_core_wrapper_info); | 156 &g_wrapper_info); |
139 | 157 |
140 if (templ.IsEmpty()) { | 158 if (templ.IsEmpty()) { |
141 templ = v8::ObjectTemplate::New(); | 159 templ = v8::ObjectTemplate::New(); |
142 | 160 |
143 templ->Set(gin::StringToSymbol(isolate, "close"), | 161 templ->Set(gin::StringToSymbol(isolate, "close"), |
144 v8::FunctionTemplate::New(Close)); | 162 v8::FunctionTemplate::New(Close)); |
145 templ->Set(gin::StringToSymbol(isolate, "wait"), | 163 templ->Set(gin::StringToSymbol(isolate, "wait"), |
146 v8::FunctionTemplate::New(Wait)); | 164 v8::FunctionTemplate::New(Wait)); |
147 templ->Set(gin::StringToSymbol(isolate, "waitMany"), | 165 templ->Set(gin::StringToSymbol(isolate, "waitMany"), |
148 v8::FunctionTemplate::New(WaitMany)); | 166 v8::FunctionTemplate::New(WaitMany)); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 gin::ConvertToV8(isolate, MOJO_WAIT_FLAG_EVERYTHING)); | 222 gin::ConvertToV8(isolate, MOJO_WAIT_FLAG_EVERYTHING)); |
205 | 223 |
206 templ->Set(gin::StringToSymbol(isolate, "WRITE_MESSAGE_FLAG_NONE"), | 224 templ->Set(gin::StringToSymbol(isolate, "WRITE_MESSAGE_FLAG_NONE"), |
207 gin::ConvertToV8(isolate, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 225 gin::ConvertToV8(isolate, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
208 | 226 |
209 templ->Set(gin::StringToSymbol(isolate, "READ_MESSAGE_FLAG_NONE"), | 227 templ->Set(gin::StringToSymbol(isolate, "READ_MESSAGE_FLAG_NONE"), |
210 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_NONE)); | 228 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_NONE)); |
211 templ->Set(gin::StringToSymbol(isolate, "READ_MESSAGE_FLAG_MAY_DISCARD"), | 229 templ->Set(gin::StringToSymbol(isolate, "READ_MESSAGE_FLAG_MAY_DISCARD"), |
212 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)); | 230 gin::ConvertToV8(isolate, MOJO_READ_MESSAGE_FLAG_MAY_DISCARD)); |
213 | 231 |
214 data->SetObjectTemplate(&g_core_wrapper_info, templ); | 232 data->SetObjectTemplate(&g_wrapper_info, templ); |
215 } | 233 } |
216 | 234 |
217 return templ; | 235 return templ; |
218 } | 236 } |
219 | 237 |
220 } // namespace js | 238 } // namespace js |
221 } // namespace mojo | 239 } // namespace mojo |
OLD | NEW |