| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/mojo/MojoHandle.h" | 5 #include "core/mojo/MojoHandle.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" | 7 #include "bindings/core/v8/ArrayBufferOrArrayBufferView.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/dom/DOMArrayBuffer.h" | 9 #include "core/dom/DOMArrayBuffer.h" |
| 10 #include "core/dom/DOMArrayBufferView.h" | 10 #include "core/dom/DOMArrayBufferView.h" |
| 11 #include "core/mojo/MojoCreateSharedBufferResult.h" | 11 #include "core/mojo/MojoCreateSharedBufferResult.h" |
| 12 #include "core/mojo/MojoDiscardDataOptions.h" |
| 12 #include "core/mojo/MojoDuplicateBufferHandleOptions.h" | 13 #include "core/mojo/MojoDuplicateBufferHandleOptions.h" |
| 13 #include "core/mojo/MojoMapBufferResult.h" | 14 #include "core/mojo/MojoMapBufferResult.h" |
| 15 #include "core/mojo/MojoReadDataOptions.h" |
| 16 #include "core/mojo/MojoReadDataResult.h" |
| 14 #include "core/mojo/MojoReadMessageFlags.h" | 17 #include "core/mojo/MojoReadMessageFlags.h" |
| 15 #include "core/mojo/MojoReadMessageResult.h" | 18 #include "core/mojo/MojoReadMessageResult.h" |
| 16 #include "core/mojo/MojoWatcher.h" | 19 #include "core/mojo/MojoWatcher.h" |
| 20 #include "core/mojo/MojoWriteDataOptions.h" |
| 21 #include "core/mojo/MojoWriteDataResult.h" |
| 17 | 22 |
| 18 // Mojo messages typically do not contain many handles. In fact most | 23 // Mojo messages typically do not contain many handles. In fact most |
| 19 // messages do not contain any handle. An inline capacity of 4 should avoid | 24 // messages do not contain any handle. An inline capacity of 4 should avoid |
| 20 // heap allocation in vast majority of cases. | 25 // heap allocation in vast majority of cases. |
| 21 static const size_t kHandleVectorInlineCapacity = 4; | 26 static const size_t kHandleVectorInlineCapacity = 4; |
| 22 | 27 |
| 23 namespace blink { | 28 namespace blink { |
| 24 | 29 |
| 25 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) { | 30 MojoHandle* MojoHandle::create(mojo::ScopedHandle handle) { |
| 26 return new MojoHandle(std::move(handle)); | 31 return new MojoHandle(std::move(handle)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 for (size_t i = 0; i < numHandles; ++i) { | 93 for (size_t i = 0; i < numHandles; ++i) { |
| 89 handles[i] = | 94 handles[i] = |
| 90 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); | 95 MojoHandle::create(mojo::MakeScopedHandle(mojo::Handle(rawHandles[i]))); |
| 91 } | 96 } |
| 92 | 97 |
| 93 resultDict.setResult(result); | 98 resultDict.setResult(result); |
| 94 resultDict.setBuffer(buffer); | 99 resultDict.setBuffer(buffer); |
| 95 resultDict.setHandles(handles); | 100 resultDict.setHandles(handles); |
| 96 } | 101 } |
| 97 | 102 |
| 103 void MojoHandle::writeData(const ArrayBufferOrArrayBufferView& buffer, |
| 104 const MojoWriteDataOptions& optionsDict, |
| 105 MojoWriteDataResult& resultDict) { |
| 106 MojoWriteDataFlags flags = MOJO_WRITE_DATA_FLAG_NONE; |
| 107 if (optionsDict.allOrNone()) |
| 108 flags |= MOJO_WRITE_DATA_FLAG_ALL_OR_NONE; |
| 109 |
| 110 const void* elements = nullptr; |
| 111 uint32_t numBytes = 0; |
| 112 if (buffer.isArrayBuffer()) { |
| 113 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); |
| 114 elements = array->data(); |
| 115 numBytes = array->byteLength(); |
| 116 } else { |
| 117 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); |
| 118 elements = view->baseAddress(); |
| 119 numBytes = view->byteLength(); |
| 120 } |
| 121 |
| 122 MojoResult result = |
| 123 MojoWriteData(m_handle->value(), elements, &numBytes, flags); |
| 124 resultDict.setResult(result); |
| 125 resultDict.setNumBytes(numBytes); |
| 126 } |
| 127 |
| 128 void MojoHandle::queryData(MojoReadDataResult& resultDict) { |
| 129 uint32_t numBytes = 0; |
| 130 MojoResult result = MojoReadData(m_handle->value(), nullptr, &numBytes, |
| 131 MOJO_READ_DATA_FLAG_QUERY); |
| 132 resultDict.setResult(result); |
| 133 resultDict.setNumBytes(numBytes); |
| 134 } |
| 135 |
| 136 void MojoHandle::discardData(unsigned numBytes, |
| 137 const MojoDiscardDataOptions& optionsDict, |
| 138 MojoReadDataResult& resultDict) { |
| 139 MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_DISCARD; |
| 140 if (optionsDict.allOrNone()) |
| 141 flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE; |
| 142 |
| 143 MojoResult result = |
| 144 MojoReadData(m_handle->value(), nullptr, &numBytes, flags); |
| 145 resultDict.setResult(result); |
| 146 resultDict.setNumBytes(numBytes); |
| 147 } |
| 148 |
| 149 void MojoHandle::readData(unsigned numBytes, |
| 150 ArrayBufferOrArrayBufferView& buffer, |
| 151 const MojoReadDataOptions& optionsDict, |
| 152 MojoReadDataResult& resultDict) { |
| 153 void* elements = nullptr; |
| 154 unsigned bufferLength = 0; |
| 155 if (buffer.isArrayBuffer()) { |
| 156 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); |
| 157 elements = array->data(); |
| 158 bufferLength = array->byteLength(); |
| 159 } else { |
| 160 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); |
| 161 elements = view->baseAddress(); |
| 162 bufferLength = view->byteLength(); |
| 163 } |
| 164 |
| 165 MojoResult result = MOJO_RESULT_OK; |
| 166 if (numBytes > bufferLength) { |
| 167 result = MOJO_RESULT_INVALID_ARGUMENT; |
| 168 numBytes = 0; |
| 169 } else { |
| 170 MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_NONE; |
| 171 if (optionsDict.allOrNone()) |
| 172 flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE; |
| 173 if (optionsDict.peek()) |
| 174 flags |= MOJO_READ_DATA_FLAG_PEEK; |
| 175 result = MojoReadData(m_handle->value(), elements, &numBytes, flags); |
| 176 } |
| 177 |
| 178 resultDict.setResult(result); |
| 179 resultDict.setNumBytes(numBytes); |
| 180 } |
| 181 |
| 98 void MojoHandle::mapBuffer(unsigned offset, | 182 void MojoHandle::mapBuffer(unsigned offset, |
| 99 unsigned numBytes, | 183 unsigned numBytes, |
| 100 MojoMapBufferResult& resultDict) { | 184 MojoMapBufferResult& resultDict) { |
| 101 void* data = nullptr; | 185 void* data = nullptr; |
| 102 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data, | 186 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data, |
| 103 MOJO_MAP_BUFFER_FLAG_NONE); | 187 MOJO_MAP_BUFFER_FLAG_NONE); |
| 104 resultDict.setResult(result); | 188 resultDict.setResult(result); |
| 105 if (result == MOJO_RESULT_OK) { | 189 if (result == MOJO_RESULT_OK) { |
| 106 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) { | 190 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) { |
| 107 MojoResult result = MojoUnmapBuffer(buffer); | 191 MojoResult result = MojoUnmapBuffer(buffer); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 124 mojo::Handle handle; | 208 mojo::Handle handle; |
| 125 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options, | 209 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options, |
| 126 handle.mutable_value()); | 210 handle.mutable_value()); |
| 127 resultDict.setResult(result); | 211 resultDict.setResult(result); |
| 128 if (result == MOJO_RESULT_OK) { | 212 if (result == MOJO_RESULT_OK) { |
| 129 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle))); | 213 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle))); |
| 130 } | 214 } |
| 131 } | 215 } |
| 132 | 216 |
| 133 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |