OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ScriptValueSerializer.h" | 5 #include "bindings/core/v8/ScriptValueSerializer.h" |
6 | 6 |
7 #include "bindings/core/v8/Transferable.h" | 7 #include "bindings/core/v8/Transferables.h" |
8 #include "bindings/core/v8/TransferableArrayBuffer.h" | |
9 #include "bindings/core/v8/TransferableImageBitmap.h" | |
10 #include "bindings/core/v8/TransferableMessagePort.h" | |
11 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
12 #include "bindings/core/v8/V8ArrayBufferView.h" | 9 #include "bindings/core/v8/V8ArrayBufferView.h" |
13 #include "bindings/core/v8/V8Blob.h" | 10 #include "bindings/core/v8/V8Blob.h" |
14 #include "bindings/core/v8/V8CompositorProxy.h" | 11 #include "bindings/core/v8/V8CompositorProxy.h" |
15 #include "bindings/core/v8/V8File.h" | 12 #include "bindings/core/v8/V8File.h" |
16 #include "bindings/core/v8/V8FileList.h" | 13 #include "bindings/core/v8/V8FileList.h" |
17 #include "bindings/core/v8/V8ImageBitmap.h" | 14 #include "bindings/core/v8/V8ImageBitmap.h" |
18 #include "bindings/core/v8/V8ImageData.h" | 15 #include "bindings/core/v8/V8ImageData.h" |
19 #include "bindings/core/v8/V8MessagePort.h" | 16 #include "bindings/core/v8/V8MessagePort.h" |
20 #include "bindings/core/v8/V8SharedArrayBuffer.h" | 17 #include "bindings/core/v8/V8SharedArrayBuffer.h" |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 // HTML5 structured clone algorithm. | 679 // HTML5 structured clone algorithm. |
683 static bool isHostObject(v8::Local<v8::Object> object) | 680 static bool isHostObject(v8::Local<v8::Object> object) |
684 { | 681 { |
685 // If the object has any internal fields, then we won't be able to serialize
or deserialize | 682 // If the object has any internal fields, then we won't be able to serialize
or deserialize |
686 // them; conveniently, this is also a quick way to detect DOM wrapper object
s, because | 683 // them; conveniently, this is also a quick way to detect DOM wrapper object
s, because |
687 // the mechanism for these relies on data stored in these fields. We should | 684 // the mechanism for these relies on data stored in these fields. We should |
688 // catch external array data as a special case. | 685 // catch external array data as a special case. |
689 return object->InternalFieldCount(); | 686 return object->InternalFieldCount(); |
690 } | 687 } |
691 | 688 |
692 ScriptValueSerializer::ScriptValueSerializer(SerializedScriptValueWriter& writer
, TransferableArray* transferables, WebBlobInfoArray* blobInfo, BlobDataHandleMa
p& blobDataHandles, v8::TryCatch& tryCatch, ScriptState* scriptState) | 689 ScriptValueSerializer::ScriptValueSerializer(SerializedScriptValueWriter& writer
, const Transferables* transferables, WebBlobInfoArray* blobInfo, BlobDataHandle
Map& blobDataHandles, v8::TryCatch& tryCatch, ScriptState* scriptState) |
693 : m_scriptState(scriptState) | 690 : m_scriptState(scriptState) |
694 , m_writer(writer) | 691 , m_writer(writer) |
695 , m_tryCatch(tryCatch) | 692 , m_tryCatch(tryCatch) |
696 , m_depth(0) | 693 , m_depth(0) |
697 , m_status(Success) | 694 , m_status(Success) |
698 , m_nextObjectReference(0) | 695 , m_nextObjectReference(0) |
699 , m_blobInfo(blobInfo) | 696 , m_blobInfo(blobInfo) |
700 , m_blobDataHandles(blobDataHandles) | 697 , m_blobDataHandles(blobDataHandles) |
701 { | 698 { |
702 ASSERT(!tryCatch.HasCaught()); | 699 ASSERT(!tryCatch.HasCaught()); |
| 700 if (transferables) |
| 701 copyTransferables(*transferables); |
| 702 } |
| 703 |
| 704 void ScriptValueSerializer::copyTransferables(const Transferables& transferables
) |
| 705 { |
703 v8::Local<v8::Object> creationContext = m_scriptState->context()->Global(); | 706 v8::Local<v8::Object> creationContext = m_scriptState->context()->Global(); |
704 if (!transferables) | 707 |
705 return; | 708 // Also kept in separate ObjectPools, iterate and copy the contents |
706 if (auto* messagePorts = TransferableMessagePort::get(*transferables)) { | 709 // of each kind of transferable vector. |
707 for (size_t i = 0; i < messagePorts->getArray().size(); i++) { | 710 |
708 v8::Local<v8::Object> v8MessagePort = toV8Object(messagePorts->getAr
ray().at(i).get(), creationContext, isolate()); | 711 const auto& messagePorts = transferables.messagePorts; |
709 m_transferredMessagePorts.set(v8MessagePort, i); | 712 for (size_t i = 0; i < messagePorts.size(); ++i) { |
710 } | 713 v8::Local<v8::Object> v8MessagePort = toV8Object(messagePorts[i].get(),
creationContext, isolate()); |
| 714 m_transferredMessagePorts.set(v8MessagePort, i); |
711 } | 715 } |
712 if (auto* arrayBuffers = TransferableArrayBuffer::get(*transferables)) { | 716 |
713 for (size_t i = 0; i < arrayBuffers->getArray().size(); i++) { | 717 const auto& arrayBuffers = transferables.arrayBuffers; |
714 v8::Local<v8::Object> v8ArrayBuffer = toV8Object(arrayBuffers->getAr
ray().at(i).get(), creationContext, isolate()); | 718 for (size_t i = 0; i < arrayBuffers.size(); ++i) { |
715 // Coalesce multiple occurences of the same buffer to the first inde
x. | 719 v8::Local<v8::Object> v8ArrayBuffer = toV8Object(arrayBuffers[i].get(),
creationContext, isolate()); |
716 if (!m_transferredArrayBuffers.contains(v8ArrayBuffer)) | 720 // Coalesce multiple occurences of the same buffer to the first index. |
717 m_transferredArrayBuffers.set(v8ArrayBuffer, i); | 721 if (!m_transferredArrayBuffers.contains(v8ArrayBuffer)) |
718 } | 722 m_transferredArrayBuffers.set(v8ArrayBuffer, i); |
719 } | 723 } |
720 if (auto* imageBitmaps = TransferableImageBitmap::get(*transferables)) { | 724 |
721 for (size_t i = 0; i < imageBitmaps->getArray().size(); i++) { | 725 const auto& imageBitmaps = transferables.imageBitmaps; |
722 v8::Local<v8::Object> v8ImageBitmap = toV8Object(imageBitmaps->getAr
ray().at(i).get(), creationContext, isolate()); | 726 for (size_t i = 0; i < imageBitmaps.size(); ++i) { |
723 if (!m_transferredImageBitmaps.contains(v8ImageBitmap)) | 727 v8::Local<v8::Object> v8ImageBitmap = toV8Object(imageBitmaps[i].get(),
creationContext, isolate()); |
724 m_transferredImageBitmaps.set(v8ImageBitmap, i); | 728 if (!m_transferredImageBitmaps.contains(v8ImageBitmap)) |
725 } | 729 m_transferredImageBitmaps.set(v8ImageBitmap, i); |
726 } | 730 } |
727 } | 731 } |
728 | 732 |
729 ScriptValueSerializer::Status ScriptValueSerializer::serialize(v8::Local<v8::Val
ue> value) | 733 ScriptValueSerializer::Status ScriptValueSerializer::serialize(v8::Local<v8::Val
ue> value) |
730 { | 734 { |
731 v8::HandleScope scope(isolate()); | 735 v8::HandleScope scope(isolate()); |
732 m_writer.writeVersion(); | 736 m_writer.writeVersion(); |
733 StateBase* state = doSerialize(value, 0); | 737 StateBase* state = doSerialize(value, 0); |
734 while (state) | 738 while (state) |
735 state = state->advance(*this); | 739 state = state->advance(*this); |
(...skipping 1550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2286 return false; | 2290 return false; |
2287 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe
renceStack.size() - 1]; | 2291 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe
renceStack.size() - 1]; |
2288 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() -
1); | 2292 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() -
1); |
2289 if (objectReference >= m_objectPool.size()) | 2293 if (objectReference >= m_objectPool.size()) |
2290 return false; | 2294 return false; |
2291 *object = m_objectPool[objectReference]; | 2295 *object = m_objectPool[objectReference]; |
2292 return true; | 2296 return true; |
2293 } | 2297 } |
2294 | 2298 |
2295 } // namespace blink | 2299 } // namespace blink |
OLD | NEW |