Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Unified Diff: third_party/WebKit/Source/core/mojo/Mojo.cpp

Issue 2400563002: Adds Mojo IDL. (Closed)
Patch Set: fixed (most) tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..2a00c3eec3a7832a21dd61e488fc04e2c97de2f5
--- /dev/null
+++ b/third_party/WebKit/Source/core/mojo/Mojo.cpp
@@ -0,0 +1,101 @@
+// 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/ArrayBufferOrArrayBufferView.h"
+#include "core/dom/DOMArrayBuffer.h"
+#include "core/dom/DOMArrayBufferView.h"
+#include "core/mojo/MojoCreateMessagePipeOptions.h"
+#include "core/mojo/MojoCreateMessagePipeResult.h"
+#include "core/mojo/MojoHandle.h"
+#include "core/mojo/MojoReadMessageResult.h"
+#include "core/mojo/MojoWatcher.h"
+
+namespace blink {
+
+Mojo* Mojo::create() {
+ return new Mojo();
+}
+
+MojoWatcher* Mojo::watch(ScriptState* scriptState,
+ MojoHandle* handle,
+ MojoHandleSignals signals,
+ MojoWatchCallback* callback) {
+ return MojoWatcher::create(handle->get(), signals, scriptState, callback);
+}
+
+void Mojo::createMessagePipe(const MojoCreateMessagePipeOptions& optionsDict,
+ MojoCreateMessagePipeResult& resultDict) {
+ ::MojoCreateMessagePipeOptions options = {0};
+ options.struct_size = sizeof(::MojoCreateMessagePipeOptions);
+ options.flags = optionsDict.flags();
+
+ mojo::ScopedMessagePipeHandle handle0, handle1;
+ MojoResult result = mojo::CreateMessagePipe(&options, &handle0, &handle1);
+
+ resultDict.setResult(result);
+ if (result == MOJO_RESULT_OK) {
+ resultDict.setHandle0(
+ MojoHandle::create(mojo::ScopedHandle::From(std::move(handle0))));
+ resultDict.setHandle1(
+ MojoHandle::create(mojo::ScopedHandle::From(std::move(handle1))));
+ }
+}
+
+MojoResult Mojo::writeMessage(MojoHandle* pipe,
+ ArrayBufferOrArrayBufferView& buffer,
+ const HeapVector<Member<MojoHandle>>& handles,
+ MojoWriteMessageFlags flags) {
+ // MojoWriteMessage takes ownership of the handles, so release them here.
+ 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.
+ 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.
+ rawHandles[i] = handles[i]->release().value();
+
+ const void* bytes = nullptr;
+ uint32_t numBytes = 0;
+ if (buffer.isArrayBuffer()) {
+ DOMArrayBuffer* array = buffer.getAsArrayBuffer();
+ bytes = array->data();
+ numBytes = array->byteLength();
+ } else {
esprehn 2016/10/25 03:34:06 We should really add an abstraction over this for
+ DCHECK(buffer.isArrayBufferView());
+ DOMArrayBufferView* view = buffer.getAsArrayBufferView();
+ bytes = view->baseAddress();
+ numBytes = view->byteLength();
+ }
+
+ return MojoWriteMessage(pipe->value(), bytes, numBytes, rawHandles.data(),
+ rawHandles.size(), flags);
+}
+
+void Mojo::readMessage(MojoHandle* pipe,
+ MojoReadMessageFlags flags,
+ MojoReadMessageResult& resultDict) {
+ uint32_t numBytes = 0, numHandles = 0;
+ MojoResult result = MojoReadMessage(pipe->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);
+ 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
+ result = MojoReadMessage(pipe->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

Powered by Google App Engine
This is Rietveld 408576698