Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "core/mojo/Mojo.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" | |
| 8 #include "core/dom/DOMArrayBuffer.h" | |
| 9 #include "core/dom/DOMArrayBufferView.h" | |
| 10 #include "core/mojo/MojoCreateMessagePipeOptions.h" | |
| 11 #include "core/mojo/MojoCreateMessagePipeResult.h" | |
| 12 #include "core/mojo/MojoHandle.h" | |
| 13 #include "core/mojo/MojoReadMessageResult.h" | |
| 14 #include "core/mojo/MojoWatcher.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 Mojo* Mojo::create() { | |
| 19 return new Mojo(); | |
| 20 } | |
| 21 | |
| 22 MojoWatcher* Mojo::watch(ScriptState* scriptState, | |
| 23 MojoHandle* handle, | |
| 24 MojoHandleSignals signals, | |
| 25 MojoWatchCallback* callback) { | |
| 26 return MojoWatcher::create(handle->get(), signals, scriptState, callback); | |
| 27 } | |
| 28 | |
| 29 void Mojo::createMessagePipe(const MojoCreateMessagePipeOptions& optionsDict, | |
| 30 MojoCreateMessagePipeResult& resultDict) { | |
| 31 ::MojoCreateMessagePipeOptions options = {0}; | |
| 32 options.struct_size = sizeof(::MojoCreateMessagePipeOptions); | |
| 33 options.flags = optionsDict.flags(); | |
| 34 | |
| 35 mojo::ScopedMessagePipeHandle handle0, handle1; | |
| 36 MojoResult result = mojo::CreateMessagePipe(&options, &handle0, &handle1); | |
| 37 | |
| 38 resultDict.setResult(result); | |
| 39 if (result == MOJO_RESULT_OK) { | |
| 40 resultDict.setHandle0( | |
| 41 MojoHandle::create(mojo::ScopedHandle::From(std::move(handle0)))); | |
| 42 resultDict.setHandle1( | |
| 43 MojoHandle::create(mojo::ScopedHandle::From(std::move(handle1)))); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 MojoResult Mojo::writeMessage(MojoHandle* pipe, | |
| 48 ArrayBufferOrArrayBufferView& buffer, | |
| 49 const HeapVector<Member<MojoHandle>>& handles, | |
| 50 MojoWriteMessageFlags flags) { | |
| 51 // MojoWriteMessage takes ownership of the handles, so release them here. | |
| 52 std::vector<::MojoHandle> rawHandles(handles.size()); | |
|
esprehn
2016/10/25 03:34:06
Use Vector, I'd also suggest using inline capacity
alokp
2016/10/25 05:01:37
Used WTF::Vector. The number of handle really depe
Ken Rockot(use gerrit already)
2016/10/25 18:11:11
I don't think there's any harm in having some non-
alokp
2016/10/25 20:33:36
OK. Used an inline capacity of 4.
| |
| 53 for (size_t i = 0; i < handles.size(); ++i) | |
|
esprehn
2016/10/25 03:34:06
for (auto& handle : handles)
rawHandles.append(h
alokp
2016/10/25 05:01:37
Done.
| |
| 54 rawHandles[i] = handles[i]->release().value(); | |
| 55 | |
| 56 const void* bytes = nullptr; | |
| 57 uint32_t numBytes = 0; | |
| 58 if (buffer.isArrayBuffer()) { | |
| 59 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); | |
| 60 bytes = array->data(); | |
| 61 numBytes = array->byteLength(); | |
| 62 } else { | |
|
esprehn
2016/10/25 03:34:06
We should really add an abstraction over this for
| |
| 63 DCHECK(buffer.isArrayBufferView()); | |
| 64 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); | |
| 65 bytes = view->baseAddress(); | |
| 66 numBytes = view->byteLength(); | |
| 67 } | |
| 68 | |
| 69 return MojoWriteMessage(pipe->value(), bytes, numBytes, rawHandles.data(), | |
| 70 rawHandles.size(), flags); | |
| 71 } | |
| 72 | |
| 73 void Mojo::readMessage(MojoHandle* pipe, | |
| 74 MojoReadMessageFlags flags, | |
| 75 MojoReadMessageResult& resultDict) { | |
| 76 uint32_t numBytes = 0, numHandles = 0; | |
| 77 MojoResult result = MojoReadMessage(pipe->value(), nullptr, &numBytes, | |
| 78 nullptr, &numHandles, flags); | |
| 79 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { | |
| 80 resultDict.setResult(result); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 DCHECK_GT(numBytes, 0u); | |
| 85 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1); | |
| 86 std::vector<::MojoHandle> rawHandles(numHandles); | |
|
esprehn
2016/10/25 03:34:06
Vector<> again, you probably also want inline capa
alokp
2016/10/25 05:01:37
Used Vector. Since most messages would not have an
Ken Rockot(use gerrit already)
2016/10/25 18:11:11
Only marginally, just consumes a little extra stac
| |
| 87 result = MojoReadMessage(pipe->value(), buffer->data(), &numBytes, | |
| 88 rawHandles.data(), &numHandles, flags); | |
| 89 | |
| 90 HeapVector<Member<MojoHandle>> handles(numHandles); | |
| 91 for (size_t i = 0; i < numHandles; ++i) { | |
| 92 handles[i] = | |
| 93 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); | |
| 94 } | |
| 95 | |
| 96 resultDict.setResult(result); | |
| 97 resultDict.setBuffer(buffer); | |
| 98 resultDict.setHandles(handles); | |
| 99 } | |
| 100 | |
| 101 } // namespace blink | |
| OLD | NEW |