| 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(result == MOJO_RESULT_OK ? numBytes : 0); |
| 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(result == MOJO_RESULT_OK ? numBytes : 0); |
| 147 } |
| 148 |
| 149 void MojoHandle::readData(ArrayBufferOrArrayBufferView& buffer, |
| 150 const MojoReadDataOptions& optionsDict, |
| 151 MojoReadDataResult& resultDict) { |
| 152 MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_NONE; |
| 153 if (optionsDict.allOrNone()) |
| 154 flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE; |
| 155 if (optionsDict.peek()) |
| 156 flags |= MOJO_READ_DATA_FLAG_PEEK; |
| 157 |
| 158 void* elements = nullptr; |
| 159 unsigned numBytes = 0; |
| 160 if (buffer.isArrayBuffer()) { |
| 161 DOMArrayBuffer* array = buffer.getAsArrayBuffer(); |
| 162 elements = array->data(); |
| 163 numBytes = array->byteLength(); |
| 164 } else { |
| 165 DOMArrayBufferView* view = buffer.getAsArrayBufferView(); |
| 166 elements = view->baseAddress(); |
| 167 numBytes = view->byteLength(); |
| 168 } |
| 169 |
| 170 MojoResult result = |
| 171 MojoReadData(m_handle->value(), elements, &numBytes, flags); |
| 172 resultDict.setResult(result); |
| 173 resultDict.setNumBytes(result == MOJO_RESULT_OK ? numBytes : 0); |
| 174 } |
| 175 |
| 98 void MojoHandle::mapBuffer(unsigned offset, | 176 void MojoHandle::mapBuffer(unsigned offset, |
| 99 unsigned numBytes, | 177 unsigned numBytes, |
| 100 MojoMapBufferResult& resultDict) { | 178 MojoMapBufferResult& resultDict) { |
| 101 void* data = nullptr; | 179 void* data = nullptr; |
| 102 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data, | 180 MojoResult result = MojoMapBuffer(m_handle->value(), offset, numBytes, &data, |
| 103 MOJO_MAP_BUFFER_FLAG_NONE); | 181 MOJO_MAP_BUFFER_FLAG_NONE); |
| 104 resultDict.setResult(result); | 182 resultDict.setResult(result); |
| 105 if (result == MOJO_RESULT_OK) { | 183 if (result == MOJO_RESULT_OK) { |
| 106 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) { | 184 WTF::ArrayBufferContents::DataHandle dataHandle(data, [](void* buffer) { |
| 107 MojoResult result = MojoUnmapBuffer(buffer); | 185 MojoResult result = MojoUnmapBuffer(buffer); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 124 mojo::Handle handle; | 202 mojo::Handle handle; |
| 125 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options, | 203 MojoResult result = MojoDuplicateBufferHandle(m_handle->value(), &options, |
| 126 handle.mutable_value()); | 204 handle.mutable_value()); |
| 127 resultDict.setResult(result); | 205 resultDict.setResult(result); |
| 128 if (result == MOJO_RESULT_OK) { | 206 if (result == MOJO_RESULT_OK) { |
| 129 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle))); | 207 resultDict.setHandle(MojoHandle::create(mojo::MakeScopedHandle(handle))); |
| 130 } | 208 } |
| 131 } | 209 } |
| 132 | 210 |
| 133 } // namespace blink | 211 } // namespace blink |
| OLD | NEW |