| Index: third_party/WebKit/Source/core/mojo/Mojo.cpp
|
| diff --git a/third_party/WebKit/Source/core/mojo/Mojo.cpp b/third_party/WebKit/Source/core/mojo/Mojo.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..abfcffd8086e163f3dd0d43cbc1a0487fd197cc9
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/mojo/Mojo.cpp
|
| @@ -0,0 +1,133 @@
|
| +// 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/Mojo.h"
|
| +
|
| +#include "bindings/core/v8/MojoWatchCallback.h"
|
| +#include "bindings/core/v8/ScriptState.h"
|
| +#include "core/dom/DOMArrayBuffer.h"
|
| +#include "core/mojo/MojoCreateMessagePipeOptions.h"
|
| +#include "core/mojo/MojoCreateMessagePipeResult.h"
|
| +#include "core/mojo/MojoHandle.h"
|
| +#include "core/mojo/MojoMessagePipeHandle.h"
|
| +#include "core/mojo/MojoReadMessageResult.h"
|
| +#include "public/platform/Platform.h"
|
| +#include "public/platform/WebTaskRunner.h"
|
| +
|
| +static const unsigned kInvalidWatchId = 0;
|
| +
|
| +namespace blink {
|
| +
|
| +DEFINE_TRACE(Mojo::WatchEntry) {
|
| + visitor->trace(m_callback);
|
| +}
|
| +
|
| +Mojo* Mojo::create() {
|
| + return new Mojo(
|
| + Platform::current()->currentThread()->getWebTaskRunner()->clone());
|
| +}
|
| +
|
| +Mojo::Mojo(std::unique_ptr<WebTaskRunner> taskRunner)
|
| + : m_taskRunner(std::move(taskRunner)), m_nextWatchId(1) {}
|
| +
|
| +Mojo::~Mojo() {}
|
| +
|
| +unsigned Mojo::watch(ScriptState* scriptState,
|
| + MojoHandle* handle,
|
| + MojoHandleSignals signals,
|
| + MojoWatchCallback* callback) {
|
| + WatchEntry* entry = new WatchEntry(m_taskRunner.get());
|
| + std::unique_ptr<MojoHandleWatcher::ReadyCallback> readyCallback =
|
| + WTF::bind(&Mojo::watchCallback, wrapWeakPersistent(this), m_nextWatchId);
|
| + if (entry->m_watcher.start(handle->get(), signals,
|
| + std::move(readyCallback)) != MOJO_RESULT_OK) {
|
| + return kInvalidWatchId;
|
| + }
|
| +
|
| + entry->m_scriptState = scriptState;
|
| + entry->m_callback = callback;
|
| + m_watchMap.add(m_nextWatchId, entry);
|
| + return m_nextWatchId++;
|
| +}
|
| +
|
| +void Mojo::cancelWatch(unsigned watchId) {
|
| + WatchMap::iterator iter = m_watchMap.find(watchId);
|
| + if (iter != m_watchMap.end()) {
|
| + iter->value->m_watcher.cancel();
|
| + m_watchMap.remove(iter);
|
| + }
|
| +}
|
| +
|
| +void Mojo::createMessagePipe(const MojoCreateMessagePipeOptions& optionsDict,
|
| + MojoCreateMessagePipeResult& resultDict) {
|
| + ::MojoCreateMessagePipeOptions options = {0};
|
| + options.struct_size = sizeof(::MojoCreateMessagePipeOptions);
|
| + options.flags = optionsDict.flags();
|
| +
|
| + ::MojoHandle handle0, handle1;
|
| + MojoResult result = MojoCreateMessagePipe(&options, &handle0, &handle1);
|
| +
|
| + resultDict.setResult(result);
|
| + if (result == MOJO_RESULT_OK) {
|
| + resultDict.setHandle0(MojoMessagePipeHandle::create(handle0));
|
| + resultDict.setHandle1(MojoMessagePipeHandle::create(handle1));
|
| + }
|
| +}
|
| +
|
| +MojoResult Mojo::writeMessage(MojoMessagePipeHandle* pipe,
|
| + DOMArrayBuffer* buffer,
|
| + const HeapVector<Member<MojoHandle>>& handles,
|
| + MojoWriteMessageFlags flags) {
|
| + // MojoWriteMessage takes ownership of the handles, so release them here.
|
| + std::vector<::MojoHandle> rawHandles(handles.size());
|
| + for (size_t i = 0; i < handles.size(); ++i)
|
| + rawHandles[i] = handles[i]->release();
|
| +
|
| + return MojoWriteMessage(pipe->get(), buffer->data(), buffer->byteLength(),
|
| + rawHandles.data(), rawHandles.size(), flags);
|
| +}
|
| +
|
| +void Mojo::readMessage(MojoMessagePipeHandle* pipe,
|
| + MojoReadMessageFlags flags,
|
| + MojoReadMessageResult& resultDict) {
|
| + uint32_t numBytes = 0, numHandles = 0;
|
| + MojoResult result = MojoReadMessage(pipe->get(), 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);
|
| + std::vector<::MojoHandle> rawHandles(numHandles);
|
| + result = MojoReadMessage(pipe->get(), buffer->data(), &numBytes,
|
| + rawHandles.data(), &numHandles, flags);
|
| +
|
| + HeapVector<Member<MojoHandle>> handles(numHandles);
|
| + for (size_t i = 0; i < numHandles; ++i) {
|
| + handles[i] = new MojoHandle(rawHandles[i]);
|
| + }
|
| +
|
| + resultDict.setResult(result);
|
| + resultDict.setBuffer(buffer);
|
| + resultDict.setHandles(handles);
|
| +}
|
| +
|
| +DEFINE_TRACE(Mojo) {
|
| + visitor->trace(m_watchMap);
|
| +}
|
| +
|
| +void Mojo::watchCallback(unsigned watchId, MojoResult result) {
|
| + WatchMap::const_iterator iter = m_watchMap.find(watchId);
|
| + if (iter != m_watchMap.end()) {
|
| + const WatchEntry* entry = iter->value;
|
| + entry->m_callback->call(entry->m_scriptState.get(), nullptr, result);
|
| +
|
| + if (result != MOJO_RESULT_OK)
|
| + cancelWatch(watchId);
|
| + }
|
| +}
|
| +
|
| +} // namespace blink
|
|
|