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 #ifndef ScriptValueSerializer_h | 5 #ifndef ScriptValueSerializer_h |
6 #define ScriptValueSerializer_h | 6 #define ScriptValueSerializer_h |
7 | 7 |
8 #include "bindings/core/v8/SerializationTag.h" | 8 #include "bindings/core/v8/SerializationTag.h" |
9 #include "bindings/core/v8/SerializedScriptValue.h" | 9 #include "bindings/core/v8/SerializedScriptValue.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
11 #include "core/CoreExport.h" | 11 #include "core/CoreExport.h" |
12 #include "core/frame/ImageBitmap.h" | |
13 #include "wtf/ArrayBufferContents.h" | 12 #include "wtf/ArrayBufferContents.h" |
14 #include "wtf/HashMap.h" | 13 #include "wtf/HashMap.h" |
15 #include "wtf/Noncopyable.h" | 14 #include "wtf/Noncopyable.h" |
16 #include "wtf/Vector.h" | 15 #include "wtf/Vector.h" |
17 #include "wtf/text/WTFString.h" | 16 #include "wtf/text/WTFString.h" |
18 #include <v8.h> | 17 #include <v8.h> |
19 | 18 |
20 namespace blink { | 19 namespace blink { |
21 | 20 |
22 class CompositorProxy; | 21 class CompositorProxy; |
23 class DOMArrayBuffer; | 22 class DOMArrayBuffer; |
24 class DOMArrayBufferView; | 23 class DOMArrayBufferView; |
25 class DOMSharedArrayBuffer; | 24 class DOMSharedArrayBuffer; |
26 class File; | 25 class File; |
27 class FileList; | 26 class FileList; |
28 class ImageBitmap; | |
29 | 27 |
30 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; | 28 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; |
31 | 29 |
32 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. | 30 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. |
33 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary wtf:
:HashMaps; | 31 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary wtf:
:HashMaps; |
34 // this class should be used instead. GCObject must be a subtype of v8::Object. | 32 // this class should be used instead. GCObject must be a subtype of v8::Object. |
35 // Suggested usage: | 33 // Suggested usage: |
36 // V8ObjectMap<v8::Object, int> map; | 34 // V8ObjectMap<v8::Object, int> map; |
37 // v8::Local<v8::Object> obj = ...; | 35 // v8::Local<v8::Object> obj = ...; |
38 // map.set(obj, 42); | 36 // map.set(obj, 42); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 void writeFile(const File&); | 130 void writeFile(const File&); |
133 void writeFileIndex(int blobIndex); | 131 void writeFileIndex(int blobIndex); |
134 void writeFileList(const FileList&); | 132 void writeFileList(const FileList&); |
135 void writeFileListIndex(const Vector<int>& blobIndices); | 133 void writeFileListIndex(const Vector<int>& blobIndices); |
136 void writeArrayBuffer(const DOMArrayBuffer&); | 134 void writeArrayBuffer(const DOMArrayBuffer&); |
137 void writeArrayBufferView(const DOMArrayBufferView&); | 135 void writeArrayBufferView(const DOMArrayBufferView&); |
138 void writeImageData(uint32_t width, uint32_t height, const uint8_t* pixelDat
a, uint32_t pixelDataLength); | 136 void writeImageData(uint32_t width, uint32_t height, const uint8_t* pixelDat
a, uint32_t pixelDataLength); |
139 void writeRegExp(v8::Local<v8::String> pattern, v8::RegExp::Flags); | 137 void writeRegExp(v8::Local<v8::String> pattern, v8::RegExp::Flags); |
140 void writeTransferredMessagePort(uint32_t index); | 138 void writeTransferredMessagePort(uint32_t index); |
141 void writeTransferredArrayBuffer(uint32_t index); | 139 void writeTransferredArrayBuffer(uint32_t index); |
142 void writeTransferredImageBitmap(uint32_t index); | |
143 void writeTransferredSharedArrayBuffer(uint32_t index); | 140 void writeTransferredSharedArrayBuffer(uint32_t index); |
144 void writeObjectReference(uint32_t reference); | 141 void writeObjectReference(uint32_t reference); |
145 void writeObject(uint32_t numProperties); | 142 void writeObject(uint32_t numProperties); |
146 void writeSparseArray(uint32_t numProperties, uint32_t length); | 143 void writeSparseArray(uint32_t numProperties, uint32_t length); |
147 void writeDenseArray(uint32_t numProperties, uint32_t length); | 144 void writeDenseArray(uint32_t numProperties, uint32_t length); |
148 String takeWireString(); | 145 String takeWireString(); |
149 void writeReferenceCount(uint32_t numberOfReferences); | 146 void writeReferenceCount(uint32_t numberOfReferences); |
150 void writeGenerateFreshObject(); | 147 void writeGenerateFreshObject(); |
151 void writeGenerateFreshSparseArray(uint32_t length); | 148 void writeGenerateFreshSparseArray(uint32_t length); |
152 void writeGenerateFreshDenseArray(uint32_t length); | 149 void writeGenerateFreshDenseArray(uint32_t length); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 protected: | 195 protected: |
199 class StateBase; | 196 class StateBase; |
200 public: | 197 public: |
201 enum Status { | 198 enum Status { |
202 Success, | 199 Success, |
203 InputError, | 200 InputError, |
204 DataCloneError, | 201 DataCloneError, |
205 JSException | 202 JSException |
206 }; | 203 }; |
207 | 204 |
208 ScriptValueSerializer(SerializedScriptValueWriter&, MessagePortArray* messag
ePorts, ArrayBufferArray* arrayBuffers, ImageBitmapArray* imageBitmaps, WebBlobI
nfoArray*, BlobDataHandleMap& blobDataHandles, v8::TryCatch&, ScriptState*); | 205 ScriptValueSerializer(SerializedScriptValueWriter&, MessagePortArray* messag
ePorts, ArrayBufferArray* arrayBuffers, WebBlobInfoArray*, BlobDataHandleMap& bl
obDataHandles, v8::TryCatch&, ScriptState*); |
209 v8::Isolate* isolate() { return m_scriptState->isolate(); } | 206 v8::Isolate* isolate() { return m_scriptState->isolate(); } |
210 v8::Local<v8::Context> context() { return m_scriptState->context(); } | 207 v8::Local<v8::Context> context() { return m_scriptState->context(); } |
211 | 208 |
212 Status serialize(v8::Local<v8::Value>); | 209 Status serialize(v8::Local<v8::Value>); |
213 String errorMessage() { return m_errorMessage; } | 210 String errorMessage() { return m_errorMessage; } |
214 | 211 |
215 protected: | 212 protected: |
216 class StateBase { | 213 class StateBase { |
217 WTF_MAKE_FAST_ALLOCATED(StateBase); | 214 WTF_MAKE_FAST_ALLOCATED(StateBase); |
218 WTF_MAKE_NONCOPYABLE(StateBase); | 215 WTF_MAKE_NONCOPYABLE(StateBase); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 void writeBooleanObject(v8::Local<v8::Value>); | 394 void writeBooleanObject(v8::Local<v8::Value>); |
398 StateBase* writeBlob(v8::Local<v8::Value>, StateBase* next); | 395 StateBase* writeBlob(v8::Local<v8::Value>, StateBase* next); |
399 StateBase* writeCompositorProxy(v8::Local<v8::Value>, StateBase* next); | 396 StateBase* writeCompositorProxy(v8::Local<v8::Value>, StateBase* next); |
400 StateBase* writeFile(v8::Local<v8::Value>, StateBase* next); | 397 StateBase* writeFile(v8::Local<v8::Value>, StateBase* next); |
401 StateBase* writeFileList(v8::Local<v8::Value>, StateBase* next); | 398 StateBase* writeFileList(v8::Local<v8::Value>, StateBase* next); |
402 void writeImageData(v8::Local<v8::Value>); | 399 void writeImageData(v8::Local<v8::Value>); |
403 void writeRegExp(v8::Local<v8::Value>); | 400 void writeRegExp(v8::Local<v8::Value>); |
404 StateBase* writeAndGreyArrayBufferView(v8::Local<v8::Object>, StateBase* nex
t); | 401 StateBase* writeAndGreyArrayBufferView(v8::Local<v8::Object>, StateBase* nex
t); |
405 StateBase* writeArrayBuffer(v8::Local<v8::Value>, StateBase* next); | 402 StateBase* writeArrayBuffer(v8::Local<v8::Value>, StateBase* next); |
406 StateBase* writeTransferredArrayBuffer(v8::Local<v8::Value>, uint32_t index,
StateBase* next); | 403 StateBase* writeTransferredArrayBuffer(v8::Local<v8::Value>, uint32_t index,
StateBase* next); |
407 StateBase* writeTransferredImageBitmap(v8::Local<v8::Value>, uint32_t index,
StateBase* next); | |
408 StateBase* writeTransferredSharedArrayBuffer(v8::Local<v8::Value>, uint32_t
index, StateBase* next); | 404 StateBase* writeTransferredSharedArrayBuffer(v8::Local<v8::Value>, uint32_t
index, StateBase* next); |
409 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount); | 405 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount); |
410 | 406 |
411 StateBase* startArrayState(v8::Local<v8::Array>, StateBase* next); | 407 StateBase* startArrayState(v8::Local<v8::Array>, StateBase* next); |
412 StateBase* startObjectState(v8::Local<v8::Object>, StateBase* next); | 408 StateBase* startObjectState(v8::Local<v8::Object>, StateBase* next); |
413 StateBase* startMapState(v8::Local<v8::Map>, StateBase* next); | 409 StateBase* startMapState(v8::Local<v8::Map>, StateBase* next); |
414 StateBase* startSetState(v8::Local<v8::Set>, StateBase* next); | 410 StateBase* startSetState(v8::Local<v8::Set>, StateBase* next); |
415 | 411 |
416 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo
ng size, int* index); | 412 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo
ng size, int* index); |
417 bool appendFileInfo(const File*, int* index); | 413 bool appendFileInfo(const File*, int* index); |
(...skipping 12 matching lines...) Expand all Loading... |
430 RefPtr<ScriptState> m_scriptState; | 426 RefPtr<ScriptState> m_scriptState; |
431 SerializedScriptValueWriter& m_writer; | 427 SerializedScriptValueWriter& m_writer; |
432 v8::TryCatch& m_tryCatch; | 428 v8::TryCatch& m_tryCatch; |
433 int m_depth; | 429 int m_depth; |
434 Status m_status; | 430 Status m_status; |
435 String m_errorMessage; | 431 String m_errorMessage; |
436 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; | 432 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; |
437 ObjectPool m_objectPool; | 433 ObjectPool m_objectPool; |
438 ObjectPool m_transferredMessagePorts; | 434 ObjectPool m_transferredMessagePorts; |
439 ObjectPool m_transferredArrayBuffers; | 435 ObjectPool m_transferredArrayBuffers; |
440 ObjectPool m_transferredImageBitmaps; | |
441 uint32_t m_nextObjectReference; | 436 uint32_t m_nextObjectReference; |
442 WebBlobInfoArray* m_blobInfo; | 437 WebBlobInfoArray* m_blobInfo; |
443 BlobDataHandleMap& m_blobDataHandles; | 438 BlobDataHandleMap& m_blobDataHandles; |
444 }; | 439 }; |
445 | 440 |
446 // Interface used by SerializedScriptValueReader to create objects of composite
types. | 441 // Interface used by SerializedScriptValueReader to create objects of composite
types. |
447 class CORE_EXPORT ScriptValueCompositeCreator { | 442 class CORE_EXPORT ScriptValueCompositeCreator { |
448 STACK_ALLOCATED(); | 443 STACK_ALLOCATED(); |
449 WTF_MAKE_NONCOPYABLE(ScriptValueCompositeCreator); | 444 WTF_MAKE_NONCOPYABLE(ScriptValueCompositeCreator); |
450 public: | 445 public: |
451 ScriptValueCompositeCreator() { } | 446 ScriptValueCompositeCreator() { } |
452 virtual ~ScriptValueCompositeCreator() { } | 447 virtual ~ScriptValueCompositeCreator() { } |
453 | 448 |
454 virtual bool consumeTopOfStack(v8::Local<v8::Value>*) = 0; | 449 virtual bool consumeTopOfStack(v8::Local<v8::Value>*) = 0; |
455 virtual uint32_t objectReferenceCount() = 0; | 450 virtual uint32_t objectReferenceCount() = 0; |
456 virtual void pushObjectReference(const v8::Local<v8::Value>&) = 0; | 451 virtual void pushObjectReference(const v8::Local<v8::Value>&) = 0; |
457 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Local<v
8::Value>*) = 0; | 452 virtual bool tryGetObjectFromObjectReference(uint32_t reference, v8::Local<v
8::Value>*) = 0; |
458 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Local<v8::Valu
e>*) = 0; | 453 virtual bool tryGetTransferredMessagePort(uint32_t index, v8::Local<v8::Valu
e>*) = 0; |
459 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Local<v8::Valu
e>*) = 0; | 454 virtual bool tryGetTransferredArrayBuffer(uint32_t index, v8::Local<v8::Valu
e>*) = 0; |
460 virtual bool tryGetTransferredImageBitmap(uint32_t index, v8::Local<v8::Valu
e>*) = 0; | |
461 virtual bool tryGetTransferredSharedArrayBuffer(uint32_t index, v8::Local<v8
::Value>*) = 0; | 455 virtual bool tryGetTransferredSharedArrayBuffer(uint32_t index, v8::Local<v8
::Value>*) = 0; |
462 virtual bool newSparseArray(uint32_t length) = 0; | 456 virtual bool newSparseArray(uint32_t length) = 0; |
463 virtual bool newDenseArray(uint32_t length) = 0; | 457 virtual bool newDenseArray(uint32_t length) = 0; |
464 virtual bool newMap() = 0; | 458 virtual bool newMap() = 0; |
465 virtual bool newSet() = 0; | 459 virtual bool newSet() = 0; |
466 virtual bool newObject() = 0; | 460 virtual bool newObject() = 0; |
467 virtual bool completeObject(uint32_t numProperties, v8::Local<v8::Value>*) =
0; | 461 virtual bool completeObject(uint32_t numProperties, v8::Local<v8::Value>*) =
0; |
468 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Local<v8::Value>*) = 0; | 462 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8
::Local<v8::Value>*) = 0; |
469 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Local<v8::Value>*) = 0; | 463 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8:
:Local<v8::Value>*) = 0; |
470 virtual bool completeMap(uint32_t length, v8::Local<v8::Value>*) = 0; | 464 virtual bool completeMap(uint32_t length, v8::Local<v8::Value>*) = 0; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 unsigned m_position; | 564 unsigned m_position; |
571 uint32_t m_version; | 565 uint32_t m_version; |
572 const WebBlobInfoArray* m_blobInfo; | 566 const WebBlobInfoArray* m_blobInfo; |
573 const BlobDataHandleMap& m_blobDataHandles; | 567 const BlobDataHandleMap& m_blobDataHandles; |
574 }; | 568 }; |
575 | 569 |
576 class CORE_EXPORT ScriptValueDeserializer : public ScriptValueCompositeCreator { | 570 class CORE_EXPORT ScriptValueDeserializer : public ScriptValueCompositeCreator { |
577 STACK_ALLOCATED(); | 571 STACK_ALLOCATED(); |
578 WTF_MAKE_NONCOPYABLE(ScriptValueDeserializer); | 572 WTF_MAKE_NONCOPYABLE(ScriptValueDeserializer); |
579 public: | 573 public: |
580 ScriptValueDeserializer(SerializedScriptValueReader& reader, MessagePortArra
y* messagePorts, ArrayBufferContentsArray* arrayBufferContents, ImageBitmapArray
* imageBitmaps) | 574 ScriptValueDeserializer(SerializedScriptValueReader& reader, MessagePortArra
y* messagePorts, ArrayBufferContentsArray* arrayBufferContents) |
581 : m_reader(reader) | 575 : m_reader(reader) |
582 , m_transferredMessagePorts(messagePorts) | 576 , m_transferredMessagePorts(messagePorts) |
583 , m_arrayBufferContents(arrayBufferContents) | 577 , m_arrayBufferContents(arrayBufferContents) |
584 , m_imageBitmaps(imageBitmaps) | |
585 , m_arrayBuffers(arrayBufferContents ? arrayBufferContents->size() : 0) | 578 , m_arrayBuffers(arrayBufferContents ? arrayBufferContents->size() : 0) |
586 , m_version(0) | 579 , m_version(0) |
587 { | 580 { |
588 } | 581 } |
589 | 582 |
590 v8::Local<v8::Value> deserialize(); | 583 v8::Local<v8::Value> deserialize(); |
591 bool newSparseArray(uint32_t) override; | 584 bool newSparseArray(uint32_t) override; |
592 bool newDenseArray(uint32_t length) override; | 585 bool newDenseArray(uint32_t length) override; |
593 bool newMap() override; | 586 bool newMap() override; |
594 bool newSet() override; | 587 bool newSet() override; |
595 bool consumeTopOfStack(v8::Local<v8::Value>*) override; | 588 bool consumeTopOfStack(v8::Local<v8::Value>*) override; |
596 bool newObject() override; | 589 bool newObject() override; |
597 bool completeObject(uint32_t numProperties, v8::Local<v8::Value>*) override; | 590 bool completeObject(uint32_t numProperties, v8::Local<v8::Value>*) override; |
598 bool completeSparseArray(uint32_t numProperties, uint32_t length, v8::Local<
v8::Value>*) override; | 591 bool completeSparseArray(uint32_t numProperties, uint32_t length, v8::Local<
v8::Value>*) override; |
599 bool completeDenseArray(uint32_t numProperties, uint32_t length, v8::Local<v
8::Value>*) override; | 592 bool completeDenseArray(uint32_t numProperties, uint32_t length, v8::Local<v
8::Value>*) override; |
600 bool completeMap(uint32_t length, v8::Local<v8::Value>*) override; | 593 bool completeMap(uint32_t length, v8::Local<v8::Value>*) override; |
601 bool completeSet(uint32_t length, v8::Local<v8::Value>*) override; | 594 bool completeSet(uint32_t length, v8::Local<v8::Value>*) override; |
602 void pushObjectReference(const v8::Local<v8::Value>&) override; | 595 void pushObjectReference(const v8::Local<v8::Value>&) override; |
603 bool tryGetTransferredMessagePort(uint32_t index, v8::Local<v8::Value>*) ove
rride; | 596 bool tryGetTransferredMessagePort(uint32_t index, v8::Local<v8::Value>*) ove
rride; |
604 bool tryGetTransferredArrayBuffer(uint32_t index, v8::Local<v8::Value>*) ove
rride; | 597 bool tryGetTransferredArrayBuffer(uint32_t index, v8::Local<v8::Value>*) ove
rride; |
605 bool tryGetTransferredImageBitmap(uint32_t index, v8::Local<v8::Value>*) ove
rride; | |
606 bool tryGetTransferredSharedArrayBuffer(uint32_t index, v8::Local<v8::Value>
*) override; | 598 bool tryGetTransferredSharedArrayBuffer(uint32_t index, v8::Local<v8::Value>
*) override; |
607 bool tryGetObjectFromObjectReference(uint32_t reference, v8::Local<v8::Value
>*) override; | 599 bool tryGetObjectFromObjectReference(uint32_t reference, v8::Local<v8::Value
>*) override; |
608 uint32_t objectReferenceCount() override; | 600 uint32_t objectReferenceCount() override; |
609 | 601 |
610 protected: | 602 protected: |
611 SerializedScriptValueReader& reader() { return m_reader; } | 603 SerializedScriptValueReader& reader() { return m_reader; } |
612 virtual bool read(v8::Local<v8::Value>*); | 604 virtual bool read(v8::Local<v8::Value>*); |
613 | 605 |
614 private: | 606 private: |
615 bool initializeObject(v8::Local<v8::Object>, uint32_t numProperties, v8::Loc
al<v8::Value>*); | 607 bool initializeObject(v8::Local<v8::Object>, uint32_t numProperties, v8::Loc
al<v8::Value>*); |
616 bool doDeserialize(); | 608 bool doDeserialize(); |
617 void push(v8::Local<v8::Value> value) { m_stack.append(value); }; | 609 void push(v8::Local<v8::Value> value) { m_stack.append(value); }; |
618 void pop(unsigned length) | 610 void pop(unsigned length) |
619 { | 611 { |
620 ASSERT(length <= m_stack.size()); | 612 ASSERT(length <= m_stack.size()); |
621 m_stack.shrink(m_stack.size() - length); | 613 m_stack.shrink(m_stack.size() - length); |
622 } | 614 } |
623 unsigned stackDepth() const { return m_stack.size(); } | 615 unsigned stackDepth() const { return m_stack.size(); } |
624 | 616 |
625 v8::Local<v8::Value> element(unsigned index); | 617 v8::Local<v8::Value> element(unsigned index); |
626 void openComposite(const v8::Local<v8::Value>&); | 618 void openComposite(const v8::Local<v8::Value>&); |
627 bool closeComposite(v8::Local<v8::Value>*); | 619 bool closeComposite(v8::Local<v8::Value>*); |
628 | 620 |
629 SerializedScriptValueReader& m_reader; | 621 SerializedScriptValueReader& m_reader; |
630 Vector<v8::Local<v8::Value>> m_stack; | 622 Vector<v8::Local<v8::Value>> m_stack; |
631 Vector<v8::Local<v8::Value>> m_objectPool; | 623 Vector<v8::Local<v8::Value>> m_objectPool; |
632 Vector<uint32_t> m_openCompositeReferenceStack; | 624 Vector<uint32_t> m_openCompositeReferenceStack; |
633 // TODO(xidachen): Oilpan, turn these optional heap vector objects into prop
er fields. | |
634 MessagePortArray* m_transferredMessagePorts; | 625 MessagePortArray* m_transferredMessagePorts; |
635 ArrayBufferContentsArray* m_arrayBufferContents; | 626 ArrayBufferContentsArray* m_arrayBufferContents; |
636 ImageBitmapArray* m_imageBitmaps; | |
637 Vector<v8::Local<v8::Value>> m_arrayBuffers; | 627 Vector<v8::Local<v8::Value>> m_arrayBuffers; |
638 uint32_t m_version; | 628 uint32_t m_version; |
639 }; | 629 }; |
640 | 630 |
641 } // namespace blink | 631 } // namespace blink |
642 | 632 |
643 #endif // ScriptValueSerializer_h | 633 #endif // ScriptValueSerializer_h |
OLD | NEW |