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

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 MojoResult MojoHandle::close() {
31 return mojo::CloseRaw(m_handle.release());
jbroman 2017/01/10 20:46:30 Can this fail (in a case where the result will be
alokp 2017/01/11 21:53:01 I followed the cpp API - mojo::Handle::Close - and
32 }
33
34 MojoWatcher* MojoHandle::watch(ScriptState* scriptState,
35 MojoHandleSignals signals,
36 MojoWatchCallback* callback) {
37 return MojoWatcher::create(m_handle.get(), signals, 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(), options.flags());
63 }
64
65 void MojoHandle::readMessage(const MojoReadMessageOptions& options,
66 MojoReadMessageResult& resultDict) {
67 uint32_t numBytes = 0, numHandles = 0;
68 MojoResult result = MojoReadMessage(m_handle->value(), nullptr, &numBytes,
69 nullptr, &numHandles, options.flags());
70 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) {
71 resultDict.setResult(result);
72 return;
73 }
74
75 DCHECK_GT(numBytes, 0u);
76 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1);
77 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(numHandles);
78 result = MojoReadMessage(m_handle->value(), buffer->data(), &numBytes,
79 rawHandles.data(), &numHandles, options.flags());
80
81 HeapVector<Member<MojoHandle>> handles(numHandles);
82 for (size_t i = 0; i < numHandles; ++i) {
83 handles[i] =
84 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i])));
85 }
86
87 resultDict.setResult(result);
88 resultDict.setBuffer(buffer);
89 resultDict.setHandles(handles);
90 }
91
92 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698