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