| 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..59a6d971746af2924061f4841f00d3bc6dbdd862
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/mojo/MojoHandle.cpp
|
| @@ -0,0 +1,96 @@
|
| +// 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)) {}
|
| +
|
| +void MojoHandle::close() {
|
| + m_handle.reset();
|
| +}
|
| +
|
| +MojoWatcher* MojoHandle::watch(ScriptState* scriptState,
|
| + const MojoWatchOptions& options,
|
| + MojoWatchCallback* callback) {
|
| + return MojoWatcher::create(m_handle.get(), options, 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(), MOJO_WRITE_MESSAGE_FLAG_NONE);
|
| +}
|
| +
|
| +void MojoHandle::readMessage(const MojoReadMessageOptions& options,
|
| + MojoReadMessageResult& resultDict) {
|
| + MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE;
|
| + if (options.mayDiscard())
|
| + flags |= MOJO_READ_MESSAGE_FLAG_MAY_DISCARD;
|
| +
|
| + uint32_t numBytes = 0, numHandles = 0;
|
| + MojoResult result = MojoReadMessage(m_handle->value(), nullptr, &numBytes,
|
| + nullptr, &numHandles, 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, 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
|
|
|