| 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 "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
| 9 #include "bindings/core/v8/SerializationTag.h" | 9 #include "bindings/core/v8/SerializationTag.h" |
| 10 #include "bindings/core/v8/SerializedScriptValue.h" | 10 #include "bindings/core/v8/SerializedScriptValue.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 class DOMSharedArrayBuffer; | 25 class DOMSharedArrayBuffer; |
| 26 class File; | 26 class File; |
| 27 class FileList; | 27 class FileList; |
| 28 class ImageData; | 28 class ImageData; |
| 29 class StaticBitmapImage; | 29 class StaticBitmapImage; |
| 30 | 30 |
| 31 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; | 31 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; |
| 32 typedef Vector<RefPtr<StaticBitmapImage>, 1> ImageBitmapContentsArray; | 32 typedef Vector<RefPtr<StaticBitmapImage>, 1> ImageBitmapContentsArray; |
| 33 | 33 |
| 34 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. | 34 // V8ObjectMap is a map from V8 objects to arbitrary values of type T. |
| 35 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary wtf:
:HashMaps; | 35 // V8 objects (or handles to V8 objects) cannot be used as keys in ordinary |
| 36 // this class should be used instead. GCObject must be a subtype of v8::Object. | 36 // wtf::HashMaps; this class should be used instead. GCObject must be a subtype |
| 37 // of v8::Object. |
| 37 // Suggested usage: | 38 // Suggested usage: |
| 38 // V8ObjectMap<v8::Object, int> map; | 39 // V8ObjectMap<v8::Object, int> map; |
| 39 // v8::Local<v8::Object> obj = ...; | 40 // v8::Local<v8::Object> obj = ...; |
| 40 // map.set(obj, 42); | 41 // map.set(obj, 42); |
| 41 template <typename GCObject, typename T> | 42 template <typename GCObject, typename T> |
| 42 class V8ObjectMap { | 43 class V8ObjectMap { |
| 43 STACK_ALLOCATED(); | 44 STACK_ALLOCATED(); |
| 44 | 45 |
| 45 public: | 46 public: |
| 46 bool contains(const v8::Local<GCObject>& handle) { | 47 bool contains(const v8::Local<GCObject>& handle) { |
| 47 return m_map.contains(*handle); | 48 return m_map.contains(*handle); |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool tryGet(const v8::Local<GCObject>& handle, T* valueOut) { | 51 bool tryGet(const v8::Local<GCObject>& handle, T* valueOut) { |
| 51 typename HandleToT::iterator result = m_map.find(*handle); | 52 typename HandleToT::iterator result = m_map.find(*handle); |
| 52 if (result != m_map.end()) { | 53 if (result != m_map.end()) { |
| 53 *valueOut = result->value; | 54 *valueOut = result->value; |
| 54 return true; | 55 return true; |
| 55 } | 56 } |
| 56 return false; | 57 return false; |
| 57 } | 58 } |
| 58 | 59 |
| 59 void set(const v8::Local<GCObject>& handle, const T& value) { | 60 void set(const v8::Local<GCObject>& handle, const T& value) { |
| 60 m_map.set(*handle, value); | 61 m_map.set(*handle, value); |
| 61 } | 62 } |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 // This implementation uses GetIdentityHash(), which sets a hidden property on
the object containing | 65 // This implementation uses GetIdentityHash(), which sets a hidden property on |
| 65 // a random integer (or returns the one that had been previously set). This en
sures that the table | 66 // the object containing a random integer (or returns the one that had been |
| 66 // never needs to be rebuilt across garbage collections at the expense of doin
g additional allocation | 67 // previously set). This ensures that the table never needs to be rebuilt |
| 67 // and making more round trips into V8. Note that since GetIdentityHash() is d
efined only on | 68 // across garbage collections at the expense of doing additional allocation |
| 68 // v8::Objects, this V8ObjectMap cannot be used to map v8::Strings to T (becau
se the public V8 API | 69 // and making more round trips into V8. Note that since GetIdentityHash() is |
| 69 // considers a v8::String to be a v8::Primitive). | 70 // defined only on v8::Objects, this V8ObjectMap cannot be used to map |
| 71 // v8::Strings to T (because the public V8 API considers a v8::String to be a |
| 72 // v8::Primitive). |
| 70 | 73 |
| 71 // If V8 exposes a way to get at the address of the object held by a handle, t
hen we can produce | 74 // If V8 exposes a way to get at the address of the object held by a handle, |
| 72 // an alternate implementation that does not need to do any V8-side allocation
; however, it will | 75 // then we can produce an alternate implementation that does not need to do |
| 73 // need to rehash after every garbage collection because a key object may have
been moved. | 76 // any V8-side allocation; however, it will need to rehash after every garbage |
| 77 // collection because a key object may have been moved. |
| 74 template <typename G> | 78 template <typename G> |
| 75 struct V8HandlePtrHash { | 79 struct V8HandlePtrHash { |
| 76 STATIC_ONLY(V8HandlePtrHash); | 80 STATIC_ONLY(V8HandlePtrHash); |
| 77 static v8::Local<G> unsafeHandleFromRawValue(const G* value) { | 81 static v8::Local<G> unsafeHandleFromRawValue(const G* value) { |
| 78 const v8::Local<G>* handle = | 82 const v8::Local<G>* handle = |
| 79 reinterpret_cast<const v8::Local<G>*>(&value); | 83 reinterpret_cast<const v8::Local<G>*>(&value); |
| 80 return *handle; | 84 return *handle; |
| 81 } | 85 } |
| 82 | 86 |
| 83 static unsigned hash(const G* key) { | 87 static unsigned hash(const G* key) { |
| 84 return static_cast<unsigned>( | 88 return static_cast<unsigned>( |
| 85 unsafeHandleFromRawValue(key)->GetIdentityHash()); | 89 unsafeHandleFromRawValue(key)->GetIdentityHash()); |
| 86 } | 90 } |
| 87 static bool equal(const G* a, const G* b) { | 91 static bool equal(const G* a, const G* b) { |
| 88 return unsafeHandleFromRawValue(a) == unsafeHandleFromRawValue(b); | 92 return unsafeHandleFromRawValue(a) == unsafeHandleFromRawValue(b); |
| 89 } | 93 } |
| 90 // For HashArg. | 94 // For HashArg. |
| 91 static const bool safeToCompareToEmptyOrDeleted = false; | 95 static const bool safeToCompareToEmptyOrDeleted = false; |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 typedef WTF::HashMap<GCObject*, T, V8HandlePtrHash<GCObject>> HandleToT; | 98 typedef WTF::HashMap<GCObject*, T, V8HandlePtrHash<GCObject>> HandleToT; |
| 95 HandleToT m_map; | 99 HandleToT m_map; |
| 96 }; | 100 }; |
| 97 | 101 |
| 98 // SerializedScriptValueWriter is responsible for serializing primitive types an
d storing | 102 // SerializedScriptValueWriter is responsible for serializing primitive types |
| 99 // information used to reconstruct composite types. | 103 // and storing information used to reconstruct composite types. |
| 100 class CORE_EXPORT SerializedScriptValueWriter { | 104 class CORE_EXPORT SerializedScriptValueWriter { |
| 101 STACK_ALLOCATED(); | 105 STACK_ALLOCATED(); |
| 102 WTF_MAKE_NONCOPYABLE(SerializedScriptValueWriter); | 106 WTF_MAKE_NONCOPYABLE(SerializedScriptValueWriter); |
| 103 | 107 |
| 104 public: | 108 public: |
| 105 typedef UChar BufferValueType; | 109 typedef UChar BufferValueType; |
| 106 | 110 |
| 107 SerializedScriptValueWriter() : m_position(0) {} | 111 SerializedScriptValueWriter() : m_position(0) {} |
| 108 | 112 |
| 109 protected: | 113 protected: |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 v8::Local<v8::Array> m_entries; | 374 v8::Local<v8::Array> m_entries; |
| 371 uint32_t m_index; | 375 uint32_t m_index; |
| 372 uint32_t m_length; | 376 uint32_t m_length; |
| 373 }; | 377 }; |
| 374 | 378 |
| 375 typedef CollectionState<v8::Map> MapState; | 379 typedef CollectionState<v8::Map> MapState; |
| 376 typedef CollectionState<v8::Set> SetState; | 380 typedef CollectionState<v8::Set> SetState; |
| 377 | 381 |
| 378 virtual StateBase* doSerializeObject(v8::Local<v8::Object>, StateBase* next); | 382 virtual StateBase* doSerializeObject(v8::Local<v8::Object>, StateBase* next); |
| 379 | 383 |
| 380 // Marks object as having been visited by the serializer and assigns it a uniq
ue object reference ID. | 384 // Marks object as having been visited by the serializer and assigns it a |
| 381 // An object may only be greyed once. | 385 // unique object reference ID. An object may only be greyed once. |
| 382 void greyObject(const v8::Local<v8::Object>&); | 386 void greyObject(const v8::Local<v8::Object>&); |
| 383 | 387 |
| 384 StateBase* handleError(Status errorStatus, const String& message, StateBase*); | 388 StateBase* handleError(Status errorStatus, const String& message, StateBase*); |
| 385 | 389 |
| 386 SerializedScriptValueWriter& writer() { return m_writer; } | 390 SerializedScriptValueWriter& writer() { return m_writer; } |
| 387 | 391 |
| 388 private: | 392 private: |
| 389 StateBase* doSerialize(v8::Local<v8::Value>, StateBase* next); | 393 StateBase* doSerialize(v8::Local<v8::Value>, StateBase* next); |
| 390 StateBase* doSerializeArrayBuffer(v8::Local<v8::Value> arrayBuffer, | 394 StateBase* doSerializeArrayBuffer(v8::Local<v8::Value> arrayBuffer, |
| 391 StateBase* next); | 395 StateBase* next); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 BlobDataHandleMap* m_blobDataHandles; | 478 BlobDataHandleMap* m_blobDataHandles; |
| 475 | 479 |
| 476 // Counts of object types encountered while serializing the object graph. | 480 // Counts of object types encountered while serializing the object graph. |
| 477 int m_primitiveCount = 0; | 481 int m_primitiveCount = 0; |
| 478 int m_jsObjectCount = 0; | 482 int m_jsObjectCount = 0; |
| 479 int m_domWrapperCount = 0; | 483 int m_domWrapperCount = 0; |
| 480 }; | 484 }; |
| 481 | 485 |
| 482 class ScriptValueDeserializer; | 486 class ScriptValueDeserializer; |
| 483 | 487 |
| 484 // SerializedScriptValueReader is responsible for deserializing primitive types
and | 488 // SerializedScriptValueReader is responsible for deserializing primitive types |
| 485 // restoring information about saved objects of composite types. | 489 // and restoring information about saved objects of composite types. |
| 486 class CORE_EXPORT SerializedScriptValueReader { | 490 class CORE_EXPORT SerializedScriptValueReader { |
| 487 STACK_ALLOCATED(); | 491 STACK_ALLOCATED(); |
| 488 WTF_MAKE_NONCOPYABLE(SerializedScriptValueReader); | 492 WTF_MAKE_NONCOPYABLE(SerializedScriptValueReader); |
| 489 | 493 |
| 490 public: | 494 public: |
| 491 SerializedScriptValueReader(const uint8_t* buffer, | 495 SerializedScriptValueReader(const uint8_t* buffer, |
| 492 int length, | 496 int length, |
| 493 const WebBlobInfoArray* blobInfo, | 497 const WebBlobInfoArray* blobInfo, |
| 494 BlobDataHandleMap& blobDataHandles, | 498 BlobDataHandleMap& blobDataHandles, |
| 495 ScriptState* scriptState) | 499 ScriptState* scriptState) |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 ArrayBufferContentsArray* m_arrayBufferContents; | 676 ArrayBufferContentsArray* m_arrayBufferContents; |
| 673 ImageBitmapContentsArray* m_imageBitmapContents; | 677 ImageBitmapContentsArray* m_imageBitmapContents; |
| 674 Vector<v8::Local<v8::Value>> m_arrayBuffers; | 678 Vector<v8::Local<v8::Value>> m_arrayBuffers; |
| 675 Vector<v8::Local<v8::Value>> m_imageBitmaps; | 679 Vector<v8::Local<v8::Value>> m_imageBitmaps; |
| 676 uint32_t m_version; | 680 uint32_t m_version; |
| 677 }; | 681 }; |
| 678 | 682 |
| 679 } // namespace blink | 683 } // namespace blink |
| 680 | 684 |
| 681 #endif // ScriptValueSerializer_h | 685 #endif // ScriptValueSerializer_h |
| OLD | NEW |