OLD | NEW |
---|---|
(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/Mojo.h" | |
6 | |
7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" | |
8 #include "core/dom/DOMArrayBuffer.h" | |
9 #include "core/dom/DOMArrayBufferView.h" | |
10 #include "core/mojo/MojoCreateMessagePipeOptions.h" | |
11 #include "core/mojo/MojoCreateMessagePipeResult.h" | |
12 #include "core/mojo/MojoHandle.h" | |
13 #include "core/mojo/MojoReadMessageResult.h" | |
14 #include "core/mojo/MojoWatcher.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 Mojo* Mojo::create() { | |
24 return new Mojo(); | |
25 } | |
26 | |
27 MojoWatcher* Mojo::watch(ScriptState* scriptState, | |
28 MojoHandle* handle, | |
29 MojoHandleSignals signals, | |
30 MojoWatchCallback* callback) { | |
31 return MojoWatcher::create(handle->get(), signals, scriptState, callback); | |
32 } | |
33 | |
34 void Mojo::createMessagePipe(const MojoCreateMessagePipeOptions& optionsDict, | |
35 MojoCreateMessagePipeResult& resultDict) { | |
36 ::MojoCreateMessagePipeOptions options = {0}; | |
37 options.struct_size = sizeof(::MojoCreateMessagePipeOptions); | |
38 options.flags = optionsDict.flags(); | |
39 | |
40 mojo::ScopedMessagePipeHandle handle0, handle1; | |
41 MojoResult result = mojo::CreateMessagePipe(&options, &handle0, &handle1); | |
42 | |
43 resultDict.setResult(result); | |
44 if (result == MOJO_RESULT_OK) { | |
45 resultDict.setHandle0( | |
46 MojoHandle::create(mojo::ScopedHandle::From(std::move(handle0)))); | |
47 resultDict.setHandle1( | |
48 MojoHandle::create(mojo::ScopedHandle::From(std::move(handle1)))); | |
49 } | |
50 } | |
51 | |
52 MojoResult Mojo::writeMessage(MojoHandle* pipe, | |
53 ArrayBufferOrArrayBufferView& buffer, | |
54 const HeapVector<Member<MojoHandle>>& handles, | |
55 MojoWriteMessageFlags flags) { | |
56 // MojoWriteMessage takes ownership of the handles, so release them here. | |
57 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(handles.size()); | |
58 for (auto& handle : handles) | |
59 rawHandles.append(handle->release().value()); | |
jbroman
2016/12/20 23:21:57
I don't think this does what you want. This create
alokp
2017/01/09 23:33:09
good catch - done.
| |
60 | |
61 const void* bytes = nullptr; | |
62 uint32_t numBytes = 0; | |
63 if (buffer.isArrayBuffer()) { | |
64 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); | |
65 bytes = array->data(); | |
66 numBytes = array->byteLength(); | |
67 } else { | |
68 DCHECK(buffer.isArrayBufferView()); | |
jbroman
2016/12/20 23:21:57
For what it's worth, this is already DCHECKed insi
alokp
2017/01/09 23:33:09
Removed redundant DCHECK
| |
69 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); | |
70 bytes = view->baseAddress(); | |
71 numBytes = view->byteLength(); | |
72 } | |
73 | |
74 return MojoWriteMessage(pipe->value(), bytes, numBytes, rawHandles.data(), | |
75 rawHandles.size(), flags); | |
jbroman
2016/12/20 23:21:57
Here and elsewhere: this exposes access to pass ar
alokp
2017/01/09 23:33:09
This will only be used for trusted script. I would
jbroman
2017/01/10 20:46:30
I'm not a security engineer, but yes, I think that
alokp
2017/01/11 21:53:01
Acknowledged. I will add tsepez@ as reviewer once
| |
76 } | |
77 | |
78 void Mojo::readMessage(MojoHandle* pipe, | |
79 MojoReadMessageFlags flags, | |
80 MojoReadMessageResult& resultDict) { | |
81 uint32_t numBytes = 0, numHandles = 0; | |
82 MojoResult result = MojoReadMessage(pipe->value(), nullptr, &numBytes, | |
83 nullptr, &numHandles, flags); | |
84 if (result != MOJO_RESULT_RESOURCE_EXHAUSTED) { | |
85 resultDict.setResult(result); | |
86 return; | |
87 } | |
88 | |
89 DCHECK_GT(numBytes, 0u); | |
90 DOMArrayBuffer* buffer = DOMArrayBuffer::createUninitialized(numBytes, 1); | |
91 Vector<::MojoHandle, kHandleVectorInlineCapacity> rawHandles(numHandles); | |
92 result = MojoReadMessage(pipe->value(), buffer->data(), &numBytes, | |
93 rawHandles.data(), &numHandles, flags); | |
94 | |
95 HeapVector<Member<MojoHandle>> handles(numHandles); | |
96 for (size_t i = 0; i < numHandles; ++i) { | |
97 handles[i] = | |
98 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); | |
99 } | |
100 | |
101 resultDict.setResult(result); | |
102 resultDict.setBuffer(buffer); | |
103 resultDict.setHandles(handles); | |
104 } | |
105 | |
106 } // namespace blink | |
OLD | NEW |