OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "bindings/core/v8/serialization/V8ScriptValueSerializer.h" | 5 #include "bindings/core/v8/serialization/V8ScriptValueSerializer.h" |
6 | 6 |
7 #include "bindings/core/v8/ToV8.h" | 7 #include "bindings/core/v8/ToV8.h" |
8 #include "bindings/core/v8/V8Blob.h" | 8 #include "bindings/core/v8/V8Blob.h" |
9 #include "bindings/core/v8/V8CompositorProxy.h" | 9 #include "bindings/core/v8/V8CompositorProxy.h" |
10 #include "bindings/core/v8/V8File.h" | 10 #include "bindings/core/v8/V8File.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 // for both parts was always equal. | 36 // for both parts was always equal. |
37 // | 37 // |
38 // See also V8ScriptValueDeserializer.cpp. | 38 // See also V8ScriptValueDeserializer.cpp. |
39 // | 39 // |
40 // This version number must be incremented whenever any incompatible changes are | 40 // This version number must be incremented whenever any incompatible changes are |
41 // made to how Blink writes data. Purely V8-side changes do not require an | 41 // made to how Blink writes data. Purely V8-side changes do not require an |
42 // adjustment to this value. | 42 // adjustment to this value. |
43 static const uint32_t kLatestVersion = 16; | 43 static const uint32_t kLatestVersion = 16; |
44 | 44 |
45 V8ScriptValueSerializer::V8ScriptValueSerializer( | 45 V8ScriptValueSerializer::V8ScriptValueSerializer( |
46 RefPtr<ScriptState> scriptState) | 46 RefPtr<ScriptState> scriptState, |
| 47 const Options& options) |
47 : m_scriptState(std::move(scriptState)), | 48 : m_scriptState(std::move(scriptState)), |
48 m_serializedScriptValue(SerializedScriptValue::create()), | 49 m_serializedScriptValue(SerializedScriptValue::create()), |
49 m_serializer(m_scriptState->isolate(), this) { | 50 m_serializer(m_scriptState->isolate(), this), |
50 } | 51 m_transferables(options.transferables), |
| 52 m_blobInfoArray(options.blobInfo) {} |
51 | 53 |
52 RefPtr<SerializedScriptValue> V8ScriptValueSerializer::serialize( | 54 RefPtr<SerializedScriptValue> V8ScriptValueSerializer::serialize( |
53 v8::Local<v8::Value> value, | 55 v8::Local<v8::Value> value, |
54 Transferables* transferables, | |
55 ExceptionState& exceptionState) { | 56 ExceptionState& exceptionState) { |
56 #if DCHECK_IS_ON() | 57 #if DCHECK_IS_ON() |
57 DCHECK(!m_serializeInvoked); | 58 DCHECK(!m_serializeInvoked); |
58 m_serializeInvoked = true; | 59 m_serializeInvoked = true; |
59 #endif | 60 #endif |
60 DCHECK(m_serializedScriptValue); | 61 DCHECK(m_serializedScriptValue); |
61 AutoReset<const ExceptionState*> reset(&m_exceptionState, &exceptionState); | 62 AutoReset<const ExceptionState*> reset(&m_exceptionState, &exceptionState); |
62 | 63 |
63 // Prepare to transfer the provided transferables. | 64 // Prepare to transfer the provided transferables. |
64 prepareTransfer(transferables, exceptionState); | 65 prepareTransfer(exceptionState); |
65 if (exceptionState.hadException()) | 66 if (exceptionState.hadException()) |
66 return nullptr; | 67 return nullptr; |
67 | 68 |
68 // Write out the file header. | 69 // Write out the file header. |
69 writeTag(VersionTag); | 70 writeTag(VersionTag); |
70 writeUint32(kLatestVersion); | 71 writeUint32(kLatestVersion); |
71 m_serializer.WriteHeader(); | 72 m_serializer.WriteHeader(); |
72 | 73 |
73 // Serialize the value and handle errors. | 74 // Serialize the value and handle errors. |
74 v8::TryCatch tryCatch(m_scriptState->isolate()); | 75 v8::TryCatch tryCatch(m_scriptState->isolate()); |
(...skipping 11 matching lines...) Expand all Loading... |
86 if (exceptionState.hadException()) | 87 if (exceptionState.hadException()) |
87 return nullptr; | 88 return nullptr; |
88 | 89 |
89 // Finalize the results. | 90 // Finalize the results. |
90 std::pair<uint8_t*, size_t> buffer = m_serializer.Release(); | 91 std::pair<uint8_t*, size_t> buffer = m_serializer.Release(); |
91 m_serializedScriptValue->setData( | 92 m_serializedScriptValue->setData( |
92 SerializedScriptValue::DataBufferPtr(buffer.first), buffer.second); | 93 SerializedScriptValue::DataBufferPtr(buffer.first), buffer.second); |
93 return std::move(m_serializedScriptValue); | 94 return std::move(m_serializedScriptValue); |
94 } | 95 } |
95 | 96 |
96 void V8ScriptValueSerializer::prepareTransfer(Transferables* transferables, | 97 void V8ScriptValueSerializer::prepareTransfer(ExceptionState& exceptionState) { |
97 ExceptionState& exceptionState) { | 98 if (!m_transferables) |
98 if (!transferables) | |
99 return; | 99 return; |
100 m_transferables = transferables; | |
101 | 100 |
102 // Transfer array buffers. | 101 // Transfer array buffers. |
103 for (uint32_t i = 0; i < transferables->arrayBuffers.size(); i++) { | 102 for (uint32_t i = 0; i < m_transferables->arrayBuffers.size(); i++) { |
104 DOMArrayBufferBase* arrayBuffer = transferables->arrayBuffers[i].get(); | 103 DOMArrayBufferBase* arrayBuffer = m_transferables->arrayBuffers[i].get(); |
105 if (!arrayBuffer->isShared()) { | 104 if (!arrayBuffer->isShared()) { |
106 v8::Local<v8::Value> wrapper = ToV8(arrayBuffer, m_scriptState.get()); | 105 v8::Local<v8::Value> wrapper = ToV8(arrayBuffer, m_scriptState.get()); |
107 m_serializer.TransferArrayBuffer( | 106 m_serializer.TransferArrayBuffer( |
108 i, v8::Local<v8::ArrayBuffer>::Cast(wrapper)); | 107 i, v8::Local<v8::ArrayBuffer>::Cast(wrapper)); |
109 } else { | 108 } else { |
110 exceptionState.throwDOMException( | 109 exceptionState.throwDOMException( |
111 DataCloneError, "SharedArrayBuffer can not be in transfer list."); | 110 DataCloneError, "SharedArrayBuffer can not be in transfer list."); |
112 return; | 111 return; |
113 } | 112 } |
114 } | 113 } |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 *actualSize = WTF::Partitions::bufferActualSize(size); | 426 *actualSize = WTF::Partitions::bufferActualSize(size); |
428 return WTF::Partitions::bufferRealloc(oldBuffer, *actualSize, | 427 return WTF::Partitions::bufferRealloc(oldBuffer, *actualSize, |
429 "SerializedScriptValue buffer"); | 428 "SerializedScriptValue buffer"); |
430 } | 429 } |
431 | 430 |
432 void V8ScriptValueSerializer::FreeBufferMemory(void* buffer) { | 431 void V8ScriptValueSerializer::FreeBufferMemory(void* buffer) { |
433 return WTF::Partitions::bufferFree(buffer); | 432 return WTF::Partitions::bufferFree(buffer); |
434 } | 433 } |
435 | 434 |
436 } // namespace blink | 435 } // namespace blink |
OLD | NEW |