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/Transferables.h" | 7 #include "bindings/core/v8/Transferables.h" |
8 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
9 #include "bindings/core/v8/V8ArrayBufferView.h" | 9 #include "bindings/core/v8/V8ArrayBufferView.h" |
10 #include "bindings/core/v8/V8Blob.h" | 10 #include "bindings/core/v8/V8Blob.h" |
11 #include "bindings/core/v8/V8CompositorProxy.h" | 11 #include "bindings/core/v8/V8CompositorProxy.h" |
12 #include "bindings/core/v8/V8File.h" | 12 #include "bindings/core/v8/V8File.h" |
13 #include "bindings/core/v8/V8FileList.h" | 13 #include "bindings/core/v8/V8FileList.h" |
14 #include "bindings/core/v8/V8ImageBitmap.h" | 14 #include "bindings/core/v8/V8ImageBitmap.h" |
15 #include "bindings/core/v8/V8ImageData.h" | 15 #include "bindings/core/v8/V8ImageData.h" |
16 #include "bindings/core/v8/V8MessagePort.h" | 16 #include "bindings/core/v8/V8MessagePort.h" |
| 17 #include "bindings/core/v8/V8OffscreenCanvas.h" |
17 #include "bindings/core/v8/V8SharedArrayBuffer.h" | 18 #include "bindings/core/v8/V8SharedArrayBuffer.h" |
18 #include "core/dom/CompositorProxy.h" | 19 #include "core/dom/CompositorProxy.h" |
19 #include "core/dom/DOMDataView.h" | 20 #include "core/dom/DOMDataView.h" |
20 #include "core/dom/DOMSharedArrayBuffer.h" | 21 #include "core/dom/DOMSharedArrayBuffer.h" |
21 #include "core/dom/DOMTypedArray.h" | 22 #include "core/dom/DOMTypedArray.h" |
22 #include "core/fileapi/Blob.h" | 23 #include "core/fileapi/Blob.h" |
23 #include "core/fileapi/File.h" | 24 #include "core/fileapi/File.h" |
24 #include "core/fileapi/FileList.h" | 25 #include "core/fileapi/FileList.h" |
25 #include "platform/RuntimeEnabledFeatures.h" | 26 #include "platform/RuntimeEnabledFeatures.h" |
26 #include "public/platform/Platform.h" | 27 #include "public/platform/Platform.h" |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 append(ArrayBufferTransferTag); | 336 append(ArrayBufferTransferTag); |
336 doWriteUint32(index); | 337 doWriteUint32(index); |
337 } | 338 } |
338 | 339 |
339 void SerializedScriptValueWriter::writeTransferredImageBitmap(uint32_t index) | 340 void SerializedScriptValueWriter::writeTransferredImageBitmap(uint32_t index) |
340 { | 341 { |
341 append(ImageBitmapTransferTag); | 342 append(ImageBitmapTransferTag); |
342 doWriteUint32(index); | 343 doWriteUint32(index); |
343 } | 344 } |
344 | 345 |
| 346 void SerializedScriptValueWriter::writeTransferredOffscreenCanvas(uint32_t index
, uint32_t width, uint32_t height, uint32_t id) |
| 347 { |
| 348 append(OffscreenCanvasTransferTag); |
| 349 doWriteUint32(index); |
| 350 doWriteUint32(width); |
| 351 doWriteUint32(height); |
| 352 doWriteUint32(id); |
| 353 } |
| 354 |
345 void SerializedScriptValueWriter::writeTransferredSharedArrayBuffer(uint32_t ind
ex) | 355 void SerializedScriptValueWriter::writeTransferredSharedArrayBuffer(uint32_t ind
ex) |
346 { | 356 { |
347 ASSERT(RuntimeEnabledFeatures::sharedArrayBufferEnabled()); | 357 ASSERT(RuntimeEnabledFeatures::sharedArrayBufferEnabled()); |
348 append(SharedArrayBufferTransferTag); | 358 append(SharedArrayBufferTransferTag); |
349 doWriteUint32(index); | 359 doWriteUint32(index); |
350 } | 360 } |
351 | 361 |
352 void SerializedScriptValueWriter::writeObjectReference(uint32_t reference) | 362 void SerializedScriptValueWriter::writeObjectReference(uint32_t reference) |
353 { | 363 { |
354 append(ObjectReferenceTag); | 364 append(ObjectReferenceTag); |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 static v8::Local<v8::Object> toV8Object(ImageBitmap* impl, v8::Local<v8::Object>
creationContext, v8::Isolate* isolate) | 668 static v8::Local<v8::Object> toV8Object(ImageBitmap* impl, v8::Local<v8::Object>
creationContext, v8::Isolate* isolate) |
659 { | 669 { |
660 if (!impl) | 670 if (!impl) |
661 return v8::Local<v8::Object>(); | 671 return v8::Local<v8::Object>(); |
662 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); | 672 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); |
663 if (wrapper.IsEmpty()) | 673 if (wrapper.IsEmpty()) |
664 return v8::Local<v8::Object>(); | 674 return v8::Local<v8::Object>(); |
665 return wrapper.As<v8::Object>(); | 675 return wrapper.As<v8::Object>(); |
666 } | 676 } |
667 | 677 |
| 678 static v8::Local<v8::Object> toV8Object(OffscreenCanvas* impl, v8::Local<v8::Obj
ect> creationContext, v8::Isolate* isolate) |
| 679 { |
| 680 if (!impl) |
| 681 return v8::Local<v8::Object>(); |
| 682 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); |
| 683 if (wrapper.IsEmpty()) |
| 684 return v8::Local<v8::Object>(); |
| 685 return wrapper.As<v8::Object>(); |
| 686 } |
| 687 |
668 static v8::Local<v8::Object> toV8Object(DOMArrayBufferBase* impl, v8::Local<v8::
Object> creationContext, v8::Isolate* isolate) | 688 static v8::Local<v8::Object> toV8Object(DOMArrayBufferBase* impl, v8::Local<v8::
Object> creationContext, v8::Isolate* isolate) |
669 { | 689 { |
670 if (!impl) | 690 if (!impl) |
671 return v8::Local<v8::Object>(); | 691 return v8::Local<v8::Object>(); |
672 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); | 692 v8::Local<v8::Value> wrapper = toV8(impl, creationContext, isolate); |
673 if (wrapper.IsEmpty()) | 693 if (wrapper.IsEmpty()) |
674 return v8::Local<v8::Object>(); | 694 return v8::Local<v8::Object>(); |
675 return wrapper.As<v8::Object>(); | 695 return wrapper.As<v8::Object>(); |
676 } | 696 } |
677 | 697 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 if (!m_transferredArrayBuffers.contains(v8ArrayBuffer)) | 741 if (!m_transferredArrayBuffers.contains(v8ArrayBuffer)) |
722 m_transferredArrayBuffers.set(v8ArrayBuffer, i); | 742 m_transferredArrayBuffers.set(v8ArrayBuffer, i); |
723 } | 743 } |
724 | 744 |
725 const auto& imageBitmaps = transferables.imageBitmaps; | 745 const auto& imageBitmaps = transferables.imageBitmaps; |
726 for (size_t i = 0; i < imageBitmaps.size(); ++i) { | 746 for (size_t i = 0; i < imageBitmaps.size(); ++i) { |
727 v8::Local<v8::Object> v8ImageBitmap = toV8Object(imageBitmaps[i].get(),
creationContext, isolate()); | 747 v8::Local<v8::Object> v8ImageBitmap = toV8Object(imageBitmaps[i].get(),
creationContext, isolate()); |
728 if (!m_transferredImageBitmaps.contains(v8ImageBitmap)) | 748 if (!m_transferredImageBitmaps.contains(v8ImageBitmap)) |
729 m_transferredImageBitmaps.set(v8ImageBitmap, i); | 749 m_transferredImageBitmaps.set(v8ImageBitmap, i); |
730 } | 750 } |
| 751 |
| 752 const auto& offscreenCanvases = transferables.offscreenCanvases; |
| 753 for (size_t i = 0; i < offscreenCanvases.size(); ++i) { |
| 754 v8::Local<v8::Object> v8OffscreenCanvas = toV8Object(offscreenCanvases[i
].get(), creationContext, isolate()); |
| 755 if (!m_transferredOffscreenCanvas.contains(v8OffscreenCanvas)) |
| 756 m_transferredOffscreenCanvas.set(v8OffscreenCanvas, i); |
| 757 } |
731 } | 758 } |
732 | 759 |
733 ScriptValueSerializer::Status ScriptValueSerializer::serialize(v8::Local<v8::Val
ue> value) | 760 ScriptValueSerializer::Status ScriptValueSerializer::serialize(v8::Local<v8::Val
ue> value) |
734 { | 761 { |
735 v8::HandleScope scope(isolate()); | 762 v8::HandleScope scope(isolate()); |
736 m_writer.writeVersion(); | 763 m_writer.writeVersion(); |
737 StateBase* state = doSerialize(value, 0); | 764 StateBase* state = doSerialize(value, 0); |
738 while (state) | 765 while (state) |
739 state = state->advance(*this); | 766 state = state->advance(*this); |
740 return m_status; | 767 return m_status; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 m_writer.writeUint32(value.As<v8::Uint32>()->Value()); | 801 m_writer.writeUint32(value.As<v8::Uint32>()->Value()); |
775 } else if (value->IsNumber()) { | 802 } else if (value->IsNumber()) { |
776 m_writer.writeNumber(value.As<v8::Number>()->Value()); | 803 m_writer.writeNumber(value.As<v8::Number>()->Value()); |
777 } else if (value->IsString()) { | 804 } else if (value->IsString()) { |
778 writeString(value); | 805 writeString(value); |
779 } else if (value->IsObject()) { | 806 } else if (value->IsObject()) { |
780 v8::Local<v8::Object> jsObject = value.As<v8::Object>(); | 807 v8::Local<v8::Object> jsObject = value.As<v8::Object>(); |
781 | 808 |
782 uint32_t arrayBufferIndex; | 809 uint32_t arrayBufferIndex; |
783 uint32_t imageBitmapIndex; | 810 uint32_t imageBitmapIndex; |
| 811 uint32_t offscreenCanvasIndex; |
784 if (V8ArrayBufferView::hasInstance(value, isolate())) { | 812 if (V8ArrayBufferView::hasInstance(value, isolate())) { |
785 return writeAndGreyArrayBufferView(jsObject, next); | 813 return writeAndGreyArrayBufferView(jsObject, next); |
786 } else if (V8MessagePort::hasInstance(value, isolate())) { | 814 } else if (V8MessagePort::hasInstance(value, isolate())) { |
787 uint32_t messagePortIndex; | 815 uint32_t messagePortIndex; |
788 if (!m_transferredMessagePorts.tryGet(jsObject, &messagePortIndex)) | 816 if (!m_transferredMessagePorts.tryGet(jsObject, &messagePortIndex)) |
789 return handleError(DataCloneError, "A MessagePort could not be c
loned.", next); | 817 return handleError(DataCloneError, "A MessagePort could not be c
loned.", next); |
790 m_writer.writeTransferredMessagePort(messagePortIndex); | 818 m_writer.writeTransferredMessagePort(messagePortIndex); |
791 return nullptr; | 819 return nullptr; |
792 } else if (V8ArrayBuffer::hasInstance(value, isolate()) && m_transferred
ArrayBuffers.tryGet(jsObject, &arrayBufferIndex)) { | 820 } else if (V8ArrayBuffer::hasInstance(value, isolate()) && m_transferred
ArrayBuffers.tryGet(jsObject, &arrayBufferIndex)) { |
793 return writeTransferredArrayBuffer(value, arrayBufferIndex, next); | 821 return writeTransferredArrayBuffer(value, arrayBufferIndex, next); |
794 } else if (V8ImageBitmap::hasInstance(value, isolate()) && m_transferred
ImageBitmaps.tryGet(jsObject, &imageBitmapIndex)) { | 822 } else if (V8ImageBitmap::hasInstance(value, isolate()) && m_transferred
ImageBitmaps.tryGet(jsObject, &imageBitmapIndex)) { |
795 return writeTransferredImageBitmap(value, imageBitmapIndex, next); | 823 return writeTransferredImageBitmap(value, imageBitmapIndex, next); |
796 } else if (V8SharedArrayBuffer::hasInstance(value, isolate()) && m_trans
ferredArrayBuffers.tryGet(jsObject, &arrayBufferIndex)) { | 824 } else if (V8SharedArrayBuffer::hasInstance(value, isolate()) && m_trans
ferredArrayBuffers.tryGet(jsObject, &arrayBufferIndex)) { |
797 return writeTransferredSharedArrayBuffer(value, arrayBufferIndex, ne
xt); | 825 return writeTransferredSharedArrayBuffer(value, arrayBufferIndex, ne
xt); |
| 826 } else if (V8OffscreenCanvas::hasInstance(value, isolate()) && m_transfe
rredOffscreenCanvas.tryGet(jsObject, &offscreenCanvasIndex)) { |
| 827 return writeTransferredOffscreenCanvas(value, offscreenCanvasIndex,
next); |
798 } | 828 } |
799 | 829 |
800 greyObject(jsObject); | 830 greyObject(jsObject); |
801 if (value->IsDate()) { | 831 if (value->IsDate()) { |
802 m_writer.writeDate(value.As<v8::Date>()->ValueOf()); | 832 m_writer.writeDate(value.As<v8::Date>()->ValueOf()); |
803 } else if (value->IsStringObject()) { | 833 } else if (value->IsStringObject()) { |
804 writeStringObject(value); | 834 writeStringObject(value); |
805 } else if (value->IsNumberObject()) { | 835 } else if (value->IsNumberObject()) { |
806 writeNumberObject(value); | 836 writeNumberObject(value); |
807 } else if (value->IsBooleanObject()) { | 837 } else if (value->IsBooleanObject()) { |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 { | 1121 { |
1092 ImageBitmap* imageBitmap = V8ImageBitmap::toImpl(value.As<v8::Object>()); | 1122 ImageBitmap* imageBitmap = V8ImageBitmap::toImpl(value.As<v8::Object>()); |
1093 if (!imageBitmap) | 1123 if (!imageBitmap) |
1094 return 0; | 1124 return 0; |
1095 if (imageBitmap->isNeutered()) | 1125 if (imageBitmap->isNeutered()) |
1096 return handleError(DataCloneError, "An ImageBitmap is neutered and could
not be cloned.", next); | 1126 return handleError(DataCloneError, "An ImageBitmap is neutered and could
not be cloned.", next); |
1097 m_writer.writeTransferredImageBitmap(index); | 1127 m_writer.writeTransferredImageBitmap(index); |
1098 return 0; | 1128 return 0; |
1099 } | 1129 } |
1100 | 1130 |
| 1131 ScriptValueSerializer::StateBase* ScriptValueSerializer::writeTransferredOffscre
enCanvas(v8::Local<v8::Value> value, uint32_t index, ScriptValueSerializer::Stat
eBase* next) |
| 1132 { |
| 1133 OffscreenCanvas* offscreenCanvas = V8OffscreenCanvas::toImpl(value.As<v8::Ob
ject>()); |
| 1134 if (!offscreenCanvas) |
| 1135 return nullptr; |
| 1136 if (offscreenCanvas->isNeutered()) |
| 1137 return handleError(DataCloneError, "An OffscreenCanvas is detached and c
ould not be cloned.", next); |
| 1138 if (offscreenCanvas->renderingContext()) |
| 1139 return handleError(DataCloneError, "An OffscreenCanvas with a context co
uld not be cloned.", next); |
| 1140 m_writer.writeTransferredOffscreenCanvas(index, offscreenCanvas->width(), of
fscreenCanvas->height(), offscreenCanvas->getAssociatedCanvasId()); |
| 1141 return nullptr; |
| 1142 } |
| 1143 |
1101 ScriptValueSerializer::StateBase* ScriptValueSerializer::writeTransferredSharedA
rrayBuffer(v8::Local<v8::Value> value, uint32_t index, ScriptValueSerializer::St
ateBase* next) | 1144 ScriptValueSerializer::StateBase* ScriptValueSerializer::writeTransferredSharedA
rrayBuffer(v8::Local<v8::Value> value, uint32_t index, ScriptValueSerializer::St
ateBase* next) |
1102 { | 1145 { |
1103 ASSERT(RuntimeEnabledFeatures::sharedArrayBufferEnabled()); | 1146 ASSERT(RuntimeEnabledFeatures::sharedArrayBufferEnabled()); |
1104 DOMSharedArrayBuffer* sharedArrayBuffer = V8SharedArrayBuffer::toImpl(value.
As<v8::Object>()); | 1147 DOMSharedArrayBuffer* sharedArrayBuffer = V8SharedArrayBuffer::toImpl(value.
As<v8::Object>()); |
1105 if (!sharedArrayBuffer) | 1148 if (!sharedArrayBuffer) |
1106 return 0; | 1149 return 0; |
1107 m_writer.writeTransferredSharedArrayBuffer(index); | 1150 m_writer.writeTransferredSharedArrayBuffer(index); |
1108 return 0; | 1151 return 0; |
1109 } | 1152 } |
1110 | 1153 |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1434 case ImageBitmapTransferTag: { | 1477 case ImageBitmapTransferTag: { |
1435 if (!m_version) | 1478 if (!m_version) |
1436 return false; | 1479 return false; |
1437 uint32_t index; | 1480 uint32_t index; |
1438 if (!doReadUint32(&index)) | 1481 if (!doReadUint32(&index)) |
1439 return false; | 1482 return false; |
1440 if (!creator.tryGetTransferredImageBitmap(index, value)) | 1483 if (!creator.tryGetTransferredImageBitmap(index, value)) |
1441 return false; | 1484 return false; |
1442 break; | 1485 break; |
1443 } | 1486 } |
| 1487 case OffscreenCanvasTransferTag: { |
| 1488 if (!m_version) |
| 1489 return false; |
| 1490 uint32_t index, width, height, id; |
| 1491 if (!doReadUint32(&index)) |
| 1492 return false; |
| 1493 if (!doReadUint32(&width)) |
| 1494 return false; |
| 1495 if (!doReadUint32(&height)) |
| 1496 return false; |
| 1497 if (!doReadUint32(&id)) |
| 1498 return false; |
| 1499 if (!creator.tryGetTransferredOffscreenCanvas(index, width, height, id,
value)) |
| 1500 return false; |
| 1501 break; |
| 1502 } |
1444 case SharedArrayBufferTransferTag: { | 1503 case SharedArrayBufferTransferTag: { |
1445 if (!m_version) | 1504 if (!m_version) |
1446 return false; | 1505 return false; |
1447 uint32_t index; | 1506 uint32_t index; |
1448 if (!doReadUint32(&index)) | 1507 if (!doReadUint32(&index)) |
1449 return false; | 1508 return false; |
1450 if (!creator.tryGetTransferredSharedArrayBuffer(index, value)) | 1509 if (!creator.tryGetTransferredSharedArrayBuffer(index, value)) |
1451 return false; | 1510 return false; |
1452 break; | 1511 break; |
1453 } | 1512 } |
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2212 v8::Local<v8::Object> creationContext = m_reader.getScriptState()->conte
xt()->Global(); | 2271 v8::Local<v8::Object> creationContext = m_reader.getScriptState()->conte
xt()->Global(); |
2213 result = toV8(buffer, creationContext, isolate); | 2272 result = toV8(buffer, creationContext, isolate); |
2214 if (result.IsEmpty()) | 2273 if (result.IsEmpty()) |
2215 return false; | 2274 return false; |
2216 m_arrayBuffers[index] = result; | 2275 m_arrayBuffers[index] = result; |
2217 } | 2276 } |
2218 *object = result; | 2277 *object = result; |
2219 return true; | 2278 return true; |
2220 } | 2279 } |
2221 | 2280 |
| 2281 bool ScriptValueDeserializer::tryGetTransferredOffscreenCanvas(uint32_t index, u
int32_t width, uint32_t height, uint32_t id, v8::Local<v8::Value>* object) |
| 2282 { |
| 2283 OffscreenCanvas* offscreenCanvas = OffscreenCanvas::create(width, height); |
| 2284 offscreenCanvas->setAssociatedCanvasId(id); |
| 2285 *object = toV8(offscreenCanvas, m_reader.getScriptState()); |
| 2286 if ((*object).IsEmpty()) |
| 2287 return false; |
| 2288 return true; |
| 2289 } |
| 2290 |
2222 bool ScriptValueDeserializer::tryGetObjectFromObjectReference(uint32_t reference
, v8::Local<v8::Value>* object) | 2291 bool ScriptValueDeserializer::tryGetObjectFromObjectReference(uint32_t reference
, v8::Local<v8::Value>* object) |
2223 { | 2292 { |
2224 if (reference >= m_objectPool.size()) | 2293 if (reference >= m_objectPool.size()) |
2225 return false; | 2294 return false; |
2226 *object = m_objectPool[reference]; | 2295 *object = m_objectPool[reference]; |
2227 return object; | 2296 return object; |
2228 } | 2297 } |
2229 | 2298 |
2230 uint32_t ScriptValueDeserializer::objectReferenceCount() | 2299 uint32_t ScriptValueDeserializer::objectReferenceCount() |
2231 { | 2300 { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2290 return false; | 2359 return false; |
2291 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe
renceStack.size() - 1]; | 2360 uint32_t objectReference = m_openCompositeReferenceStack[m_openCompositeRefe
renceStack.size() - 1]; |
2292 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() -
1); | 2361 m_openCompositeReferenceStack.shrink(m_openCompositeReferenceStack.size() -
1); |
2293 if (objectReference >= m_objectPool.size()) | 2362 if (objectReference >= m_objectPool.size()) |
2294 return false; | 2363 return false; |
2295 *object = m_objectPool[objectReference]; | 2364 *object = m_objectPool[objectReference]; |
2296 return true; | 2365 return true; |
2297 } | 2366 } |
2298 | 2367 |
2299 } // namespace blink | 2368 } // namespace blink |
OLD | NEW |