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

Side by Side Diff: third_party/WebKit/Source/core/mojo/MojoHandle.cpp

Issue 2732163002: Implements JS bindings for mojo data pipe. (Closed)
Patch Set: optional args Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "core/mojo/MojoHandle.h" 5 #include "core/mojo/MojoHandle.h"
6 6
7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" 7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h"
8 #include "bindings/core/v8/ScriptState.h" 8 #include "bindings/core/v8/ScriptState.h"
9 #include "core/dom/DOMArrayBuffer.h" 9 #include "core/dom/DOMArrayBuffer.h"
10 #include "core/dom/DOMArrayBufferView.h" 10 #include "core/dom/DOMArrayBufferView.h"
11 #include "core/mojo/MojoCreateSharedBufferResult.h" 11 #include "core/mojo/MojoCreateSharedBufferResult.h"
12 #include "core/mojo/MojoDuplicateBufferHandleOptions.h" 12 #include "core/mojo/MojoDuplicateBufferHandleOptions.h"
13 #include "core/mojo/MojoMapBufferResult.h" 13 #include "core/mojo/MojoMapBufferResult.h"
14 #include "core/mojo/MojoReadDataFlags.h"
15 #include "core/mojo/MojoReadDataResult.h"
14 #include "core/mojo/MojoReadMessageFlags.h" 16 #include "core/mojo/MojoReadMessageFlags.h"
15 #include "core/mojo/MojoReadMessageResult.h" 17 #include "core/mojo/MojoReadMessageResult.h"
16 #include "core/mojo/MojoWatcher.h" 18 #include "core/mojo/MojoWatcher.h"
19 #include "core/mojo/MojoWriteDataFlags.h"
20 #include "core/mojo/MojoWriteDataResult.h"
17 21
18 // Mojo messages typically do not contain many handles. In fact most 22 // Mojo messages typically do not contain many handles. In fact most
19 // messages do not contain any handle. An inline capacity of 4 should avoid 23 // messages do not contain any handle. An inline capacity of 4 should avoid
20 // heap allocation in vast majority of cases. 24 // heap allocation in vast majority of cases.
21 static const size_t kHandleVectorInlineCapacity = 4; 25 static const size_t kHandleVectorInlineCapacity = 4;
22 26
23 namespace blink { 27 namespace blink {
24 28
25 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) { 29 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) {
26 return new MojoHandle(std::move(handle)); 30 return new MojoHandle(std::move(handle));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 for (size_t i = 0; i < numHandles; ++i) { 92 for (size_t i = 0; i < numHandles; ++i) {
89 handles[i] = 93 handles[i] =
90 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); 94 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i])));
91 } 95 }
92 96
93 resultDict.setResult(result); 97 resultDict.setResult(result);
94 resultDict.setBuffer(buffer); 98 resultDict.setBuffer(buffer);
95 resultDict.setHandles(handles); 99 resultDict.setHandles(handles);
96 } 100 }
97 101
102 void MojoHandle::writeData(ArrayBufferOrArrayBufferView& buffer,
103 const MojoWriteDataFlags& flagsDict,
104 MojoWriteDataResult& resultDict) {
105 ::MojoWriteDataFlags flags = MOJO_WRITE_DATA_FLAG_NONE;
106 if (flagsDict.allOrNone())
107 flags |= MOJO_WRITE_DATA_FLAG_ALL_OR_NONE;
108
109 const void* elements = nullptr;
110 uint32_t numBytes = 0;
111 if (buffer.isArrayBuffer()) {
112 DOMArrayBuffer* array = buffer.getAsArrayBuffer();
113 elements = array->data();
114 numBytes = array->byteLength();
115 } else {
116 DOMArrayBufferView* view = buffer.getAsArrayBufferView();
117 elements = view->baseAddress();
118 numBytes = view->byteLength();
119 }
120
121 MojoResult result =
122 MojoWriteData(m_handle->value(), elements, &numBytes, flags);
123 resultDict.setResult(result);
124 resultDict.setNumBytes(numBytes);
125 }
126
127 void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
128 MojoReadDataResult& resultDict) {
129 // This version of readData can only be used for querying.
130 if (!flagsDict.query()) {
131 resultDict.setResult(MOJO_RESULT_INVALID_ARGUMENT);
132 return;
133 }
134
135 uint32_t numBytes = 0;
136 MojoResult result = MojoReadData(m_handle->value(), nullptr, &numBytes,
137 MOJO_READ_DATA_FLAG_QUERY);
138 resultDict.setResult(result);
139 resultDict.setNumBytes(numBytes);
140 }
141
142 void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
143 unsigned numBytes,
144 MojoReadDataResult& resultDict) {
145 // This version of readData can only be used for discarding.
146 if (!flagsDict.discard()) {
147 resultDict.setResult(MOJO_RESULT_INVALID_ARGUMENT);
148 return;
149 }
150
151 ::MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_DISCARD;
152 if (flagsDict.allOrNone())
153 flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE;
154
155 MojoResult result =
156 MojoReadData(m_handle->value(), nullptr, &numBytes, flags);
157 resultDict.setResult(result);
158 resultDict.setNumBytes(numBytes);
159 }
160
161 void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
162 unsigned numBytes,
jbroman 2017/03/07 16:01:44 Any reason for the disparity between read/write, w
alokp 2017/03/07 19:34:20 My reasoning for not having numBytes on write was
jbroman 2017/03/07 20:31:47 Mild preference for that (for simplicity, and cons
alokp 2017/03/07 20:49:31 Done.
163 ArrayBufferOrArrayBufferView& buffer,
yzshen1 2017/03/07 17:56:39 [just to double check] Is it because having |buffe
alokp 2017/03/07 19:34:20 Right. I think a common usage pattern would be to
164 MojoReadDataResult& resultDict) {
165 // TODO(alokp): Validate options.
166
167 // This version of readData can only be used for reading and peeking.
168 ::MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_NONE;
169 if (flagsDict.allOrNone())
170 flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE;
171 if (flagsDict.peek())
172 flags |= MOJO_READ_DATA_FLAG_PEEK;
173
174 void* elements = nullptr;
175 unsigned bufferLength = 0;
176 if (buffer.isArrayBuffer()) {
177 DOMArrayBuffer* array = buffer.getAsArrayBuffer();
178 elements = array->data();
179 bufferLength = array->byteLength();
180 } else {
181 DOMArrayBufferView* view = buffer.getAsArrayBufferView();
182 elements = view->baseAddress();
183 bufferLength = view->byteLength();
184 }
185 numBytes = std::min(bufferLength, numBytes);
jbroman 2017/03/07 16:01:44 Isn't it always an error on the part of the progra
yzshen1 2017/03/07 17:56:39 We could return INVALID_ARGUMENT in this case, con
alokp 2017/03/07 19:34:20 Done.
186
187 MojoResult result =
188 MojoReadData(m_handle->value(), elements, &numBytes, flags);
189 resultDict.setResult(result);
190 resultDict.setResult(numBytes);
191 }
192
98 void MojoHandle::mapBuffer(unsigned offset, 193 void MojoHandle::mapBuffer(unsigned offset,
99 unsigned numBytes, 194 unsigned numBytes,
100 MojoMapBufferResult& resultDict) { 195 MojoMapBufferResult& resultDict) {
101 void* data = nullptr; 196 void* data = nullptr;
102 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data, 197 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data,
103 MOJO_MAP_BUFFER_FLAG_NONE); 198 MOJO_MAP_BUFFER_FLAG_NONE);
104 resultDict.setResult(result); 199 resultDict.setResult(result);
105 if (result == MOJO_RESULT_OK) { 200 if (result == MOJO_RESULT_OK) {
106 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) { 201 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) {
107 MojoResult result = MojoUnmapBuffer(buffer); 202 MojoResult result = MojoUnmapBuffer(buffer);
(...skipping 16 matching lines...) Expand all
124 mojo::Handle handle; 219 mojo::Handle handle;
125 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options, 220 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options,
126 handle.mutable_value()); 221 handle.mutable_value());
127 resultDict.setResult(result); 222 resultDict.setResult(result);
128 if (result == MOJO_RESULT_OK) { 223 if (result == MOJO_RESULT_OK) {
129 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle))); 224 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle)));
130 } 225 }
131 } 226 }
132 227
133 } // namespace blink 228 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698