Chromium Code Reviews| Index: third_party/WebKit/Source/core/mojo/MojoHandle.cpp |
| diff --git a/third_party/WebKit/Source/core/mojo/MojoHandle.cpp b/third_party/WebKit/Source/core/mojo/MojoHandle.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f3abddca293d751665bafc603282bfafb6a58fa3 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/mojo/MojoHandle.cpp |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/mojo/MojoHandle.h" |
| + |
| +#include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" |
| +#include "bindings/core/v8/ScriptState.h" |
| +#include "core/dom/DOMArrayBuffer.h" |
| +#include "core/dom/DOMArrayBufferView.h" |
| +#include "core/mojo/MojoReadMessageOptions.h" |
| +#include "core/mojo/MojoReadMessageResult.h" |
| +#include "core/mojo/MojoWatcher.h" |
| +#include "core/mojo/MojoWriteMessageOptions.h" |
| + |
| +// Mojo messages typically do not contain many handles. In fact most |
| +// messages do not contain any handle. An inline capacity of 4 should avoid |
| +// heap allocation in vast majority of cases. |
| +static const size_t kHandleVectorInlineCapacity = 4; |
| + |
| +namespace blink { |
| + |
| +MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) { |
| + return new MojoHandle(std::move(handle)); |
| +} |
| + |
| +MojoHandle::MojoHandle(mojo::ScopedHandle handle) |
| + : m_handle(std::move(handle)) {} |
| + |
| +MojoResult MojoHandle::close() { |
| + return mojo::CloseRaw(m_handle.release()); |
|
jbroman
2017/01/10 20:46:30
Can this fail (in a case where the result will be
alokp
2017/01/11 21:53:01
I followed the cpp API - mojo::Handle::Close - and
|
| +} |
| + |
| +MojoWatcher* MojoHandle::watch(ScriptState* scriptState, |
| + MojoHandleSignals signals, |
| + MojoWatchCallback* callback) { |
| + return MojoWatcher::create(m_handle.get(), signals, callback, |
| + scriptState->getExecutionContext()); |
| +} |
| + |
| +MojoResult MojoHandle::writeMessage(const MojoWriteMessageOptions& options) { |
| + // MojoWriteMessage takes ownership of the handles, so release them here. |
| + Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles( |
| + options.handles().size()); |
| + std::transform( |
| + options.handles().begin(), options.handles().end(), rawHandles.begin(), |
| + [](MojoHandle* handle) { return handle->m_handle.release().value(); }); |
| + |
| + const void* bytes = nullptr; |
| + uint32_t numBytes = 0; |
| + if (options.buffer().isArrayBuffer()) { |
| + DOMArrayBuffer* array = options.buffer().getAsArrayBuffer(); |
| + bytes = array->data(); |
| + numBytes = array->byteLength(); |
| + } else { |
| + DOMArrayBufferView* view = options.buffer().getAsArrayBufferView(); |
| + bytes = view->baseAddress(); |
| + numBytes = view->byteLength(); |
| + } |
| + |
| + return MojoWriteMessage(m_handle->value(), bytes, numBytes, rawHandles.data(), |
| + rawHandles.size(), options.flags()); |
| +} |
| + |
| +void MojoHandle::readMessage(const MojoReadMessageOptions& options, |
| + MojoReadMessageResult& resultDict) { |
| + uint32_t numBytes = 0, numHandles = 0; |
| + MojoResult result = MojoReadMessage(m_handle->value(), nullptr, &numBytes, |
| + nullptr, &numHandles, options.flags()); |
| + if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| + resultDict.setResult(result); |
| + return; |
| + } |
| + |
| + DCHECK_GT(numBytes, 0u); |
| + DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1); |
| + Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(numHandles); |
| + result = MojoReadMessage(m_handle->value(), buffer->data(), &numBytes, |
| + rawHandles.data(), &numHandles, options.flags()); |
| + |
| + HeapVector<Member<MojoHandle>> handles(numHandles); |
| + for (size_t i = 0; i < numHandles; ++i) { |
| + handles[i] = |
| + MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); |
| + } |
| + |
| + resultDict.setResult(result); |
| + resultDict.setBuffer(buffer); |
| + resultDict.setHandles(handles); |
| +} |
| + |
| +} // namespace blink |