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

Side by Side Diff: third_party/WebKit/Source/core/mojo/MojoHandle.cpp

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

Powered by Google App Engine
This is Rietveld 408576698