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

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

Issue 2400563002: Adds Mojo IDL. (Closed)
Patch Set: addressed comments Created 3 years, 11 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 2016 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/MojoReadMessageOptions.h"
12 #include "core/mojo/MojoReadMessageResult.h"
13 #include "core/mojo/MojoWatcher.h"
14 #include "core/mojo/MojoWriteMessageOptions.h"
15
16 // Mojo messages typically do not contain many handles. In fact most
17 // messages do not contain any handle. An inline capacity of 4 should avoid
18 // heap allocation in vast majority of cases.
19 static const size_t kHandleVectorInlineCapacity = 4;
20
21 namespace blink {
22
23 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) {
24 return new MojoHandle(std::move(handle));
25 }
26
27 MojoHandle::MojoHandle(mojo::ScopedHandle handle)
28 : m_handle(std::move(handle)) {}
29
30 void MojoHandle::close() {
31 m_handle.reset();
32 }
33
34 MojoWatcher* MojoHandle::watch(ScriptState* scriptState,
35 const MojoWatchOptions& options,
36 MojoWatchCallback* callback) {
37 return MojoWatcher::create(m_handle.get(), options, callback,
38 scriptState->getExecutionContext());
39 }
40
41 MojoResult MojoHandle::writeMessage(const MojoWriteMessageOptions& options) {
42 // MojoWriteMessage takes ownership of the handles, so release them here.
43 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(
44 options.handles().size());
45 std::transform(
46 options.handles().begin(), options.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 (options.buffer().isArrayBuffer()) {
52 DOMArrayBuffer* array = options.buffer().getAsArrayBuffer();
53 bytes = array->data();
54 numBytes = array->byteLength();
55 } else {
56 DOMArrayBufferView* view = options.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 MojoReadMessageOptions& options,
66 MojoReadMessageResult& resultDict) {
67 MojoReadMessageFlags flags = MOJO_READ_MESSAGE_FLAG_NONE;
68 if (options.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 DCHECK_GT(numBytes, 0u);
80 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1);
81 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(numHandles);
82 result = MojoReadMessage(m_handle->value(), buffer->data(), &numBytes,
83 rawHandles.data(), &numHandles, flags);
84
85 HeapVector<Member<MojoHandle>> handles(numHandles);
86 for (size_t i = 0; i < numHandles; ++i) {
87 handles[i] =
88 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i])));
89 }
90
91 resultDict.setResult(result);
92 resultDict.setBuffer(buffer);
93 resultDict.setHandles(handles);
94 }
95
96 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698