OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/mojo/MojoHandle.h" |
| 6 |
| 7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" |
| 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/dom/DOMArrayBuffer.h" |
| 10 #include "core/dom/DOMArrayBufferView.h" |
| 11 #include "core/mojo/MojoReadMessageFlags.h" |
| 12 #include "core/mojo/MojoReadMessageResult.h" |
| 13 #include "core/mojo/MojoWatcher.h" |
| 14 #include "core/mojo/MojoWriteMessageOptions.h" |
| 15 |
| 16 // Mojo messages typically do not contain many handles. In fact most |
| 17 // messages do not contain any handle. An inline capacity of 4 should avoid |
| 18 // heap allocation in vast majority of cases. |
| 19 static const size_t kHandleVectorInlineCapacity = 4; |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) { |
| 24 return new MojoHandle(std::move(handle)); |
| 25 } |
| 26 |
| 27 MojoHandle::MojoHandle(mojo::ScopedHandle handle) |
| 28 : m_handle(std::move(handle)) {} |
| 29 |
| 30 void MojoHandle::close() { |
| 31 m_handle.reset(); |
| 32 } |
| 33 |
| 34 MojoWatcher* MojoHandle::watch(ScriptState* scriptState, |
| 35 const MojoHandleSignals& signals, |
| 36 MojoWatchCallback* callback) { |
| 37 return MojoWatcher::create(m_handle.get(), signals, callback, |
| 38 scriptState->getExecutionContext()); |
| 39 } |
| 40 |
| 41 MojoResult MojoHandle::writeMessage( |
| 42 ArrayBufferOrArrayBufferView& buffer, |
| 43 const HeapVector<Member<MojoHandle>>& handles) { |
| 44 // MojoWriteMessage takes ownership of the handles, so release them here. |
| 45 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(handles.size()); |
| 46 std::transform( |
| 47 handles.begin(), handles.end(), rawHandles.begin(), |
| 48 [](MojoHandle* handle) { return handle->m_handle.release().value(); }); |
| 49 |
| 50 const void* bytes = nullptr; |
| 51 uint32_t numBytes = 0; |
| 52 if (buffer.isArrayBuffer()) { |
| 53 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); |
| 54 bytes = array->data(); |
| 55 numBytes = array->byteLength(); |
| 56 } else { |
| 57 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); |
| 58 bytes = view->baseAddress(); |
| 59 numBytes = view->byteLength(); |
| 60 } |
| 61 |
| 62 return MojoWriteMessage(m_handle->value(), bytes, numBytes, rawHandles.data(), |
| 63 rawHandles.size(), MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 64 } |
| 65 |
| 66 void MojoHandle::readMessage(const MojoReadMessageFlags& flagsDict, |
| 67 MojoReadMessageResult& resultDict) { |
| 68 ::MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE; |
| 69 if (flagsDict.mayDiscard()) |
| 70 flags |= MOJO_READ_MESSAGE_FLAG_MAY_DISCARD; |
| 71 |
| 72 uint32_t numBytes = 0, numHandles = 0; |
| 73 MojoResult result = MojoReadMessage(m_handle->value(), nullptr, &numBytes, |
| 74 nullptr, &numHandles, flags); |
| 75 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 76 resultDict.setResult(result); |
| 77 return; |
| 78 } |
| 79 |
| 80 DCHECK_GT(numBytes, 0u); |
| 81 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1); |
| 82 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(numHandles); |
| 83 result = MojoReadMessage(m_handle->value(), buffer->data(), &numBytes, |
| 84 rawHandles.data(), &numHandles, flags); |
| 85 |
| 86 HeapVector<Member<MojoHandle>> handles(numHandles); |
| 87 for (size_t i = 0; i < numHandles; ++i) { |
| 88 handles[i] = |
| 89 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); |
| 90 } |
| 91 |
| 92 resultDict.setResult(result); |
| 93 resultDict.setBuffer(buffer); |
| 94 resultDict.setHandles(handles); |
| 95 } |
| 96 |
| 97 } // namespace blink |
OLD | NEW |