Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ScriptValueSerializer.h

Issue 2101953002: [Refactoring] Clean up code for serializing ScriptValues (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 STACK_ALLOCATED(); 103 STACK_ALLOCATED();
104 WTF_MAKE_NONCOPYABLE(SerializedScriptValueWriter); 104 WTF_MAKE_NONCOPYABLE(SerializedScriptValueWriter);
105 public: 105 public:
106 typedef UChar BufferValueType; 106 typedef UChar BufferValueType;
107 107
108 SerializedScriptValueWriter() 108 SerializedScriptValueWriter()
109 : m_position(0) 109 : m_position(0)
110 { 110 {
111 } 111 }
112 112
113 // TODO(peria): Protect this mehtod.
114 String takeWireString();
115
116 protected: 113 protected:
117 friend class ScriptValueSerializer; 114 friend class ScriptValueSerializer;
118 115
116 String takeWireString();
117
119 // Write functions for primitive types. 118 // Write functions for primitive types.
120 void writeUndefined(); 119 void writeUndefined();
121 void writeNull(); 120 void writeNull();
122 void writeTrue(); 121 void writeTrue();
123 void writeFalse(); 122 void writeFalse();
124 void writeBooleanObject(bool value); 123 void writeBooleanObject(bool value);
125 void writeOneByteString(v8::Local<v8::String>&); 124 void writeOneByteString(v8::Local<v8::String>&);
126 void writeUCharString(v8::Local<v8::String>&); 125 void writeUCharString(v8::Local<v8::String>&);
127 void writeStringObject(const char* data, int length); 126 void writeStringObject(const char* data, int length);
128 void writeWebCoreString(const String&); 127 void writeWebCoreString(const String&);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 protected: 204 protected:
206 class StateBase; 205 class StateBase;
207 enum class Status { 206 enum class Status {
208 Success, 207 Success,
209 InputError, 208 InputError,
210 DataCloneError, 209 DataCloneError,
211 JSException 210 JSException
212 }; 211 };
213 212
214 public: 213 public:
215 ScriptValueSerializer(SerializedScriptValueWriter&, const Transferables*, We bBlobInfoArray*, ScriptState*); 214 ScriptValueSerializer(SerializedScriptValueWriter&, WebBlobInfoArray*, Scrip tState*);
216 v8::Isolate* isolate() { return m_scriptState->isolate(); } 215 v8::Isolate* isolate() { return m_scriptState->isolate(); }
217 v8::Local<v8::Context> context() { return m_scriptState->context(); } 216 v8::Local<v8::Context> context() { return m_scriptState->context(); }
218 217
219 PassRefPtr<SerializedScriptValue> serialize(v8::Local<v8::Value>, Transferab les*, ExceptionState&); 218 PassRefPtr<SerializedScriptValue> serialize(v8::Local<v8::Value>, Transferab les*, ExceptionState&);
220 String errorMessage() { return m_errorMessage; }
221 219
222 static String serializeWTFString(const String&); 220 static String serializeWTFString(const String&);
223 static String serializeNullValue(); 221 static String serializeNullValue();
224 222
225 protected: 223 protected:
226 class StateBase { 224 class StateBase {
227 USING_FAST_MALLOC(StateBase); 225 USING_FAST_MALLOC(StateBase);
228 WTF_MAKE_NONCOPYABLE(StateBase); 226 WTF_MAKE_NONCOPYABLE(StateBase);
229 public: 227 public:
230 virtual ~StateBase() { } 228 virtual ~StateBase() { }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 359
362 private: 360 private:
363 v8::Local<v8::Array> m_entries; 361 v8::Local<v8::Array> m_entries;
364 uint32_t m_index; 362 uint32_t m_index;
365 uint32_t m_length; 363 uint32_t m_length;
366 }; 364 };
367 365
368 typedef CollectionState<v8::Map> MapState; 366 typedef CollectionState<v8::Map> MapState;
369 typedef CollectionState<v8::Set> SetState; 367 typedef CollectionState<v8::Set> SetState;
370 368
371 // Functions used by serialization states.
372 virtual StateBase* doSerializeObject(v8::Local<v8::Object>, StateBase* next) ; 369 virtual StateBase* doSerializeObject(v8::Local<v8::Object>, StateBase* next) ;
373 370
371 // Marks object as having been visited by the serializer and assigns it a un ique object reference ID.
372 // An object may only be greyed once.
373 void greyObject(const v8::Local<v8::Object>&);
374
375 StateBase* handleError(Status errorStatus, const String& message, StateBase* );
376
377 SerializedScriptValueWriter& writer() { return m_writer; }
378
374 private: 379 private:
375 StateBase* doSerialize(v8::Local<v8::Value>, StateBase* next); 380 StateBase* doSerialize(v8::Local<v8::Value>, StateBase* next);
376 StateBase* doSerializeArrayBuffer(v8::Local<v8::Value> arrayBuffer, StateBas e* next); 381 StateBase* doSerializeArrayBuffer(v8::Local<v8::Value> arrayBuffer, StateBas e* next);
377 void transferData(Transferables*, ExceptionState&, SerializedScriptValue*); 382 void transferData(Transferables*, ExceptionState&, SerializedScriptValue*);
378 383
379 StateBase* checkException(StateBase*); 384 StateBase* checkException(StateBase*);
380 StateBase* writeObject(uint32_t numProperties, StateBase*); 385 StateBase* writeObject(uint32_t numProperties, StateBase*);
381 StateBase* writeSparseArray(uint32_t numProperties, uint32_t length, StateBa se*); 386 StateBase* writeSparseArray(uint32_t numProperties, uint32_t length, StateBa se*);
382 StateBase* writeDenseArray(uint32_t numProperties, uint32_t length, StateBas e*); 387 StateBase* writeDenseArray(uint32_t numProperties, uint32_t length, StateBas e*);
383 388
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount); 423 static bool shouldSerializeDensely(uint32_t length, uint32_t propertyCount);
419 424
420 StateBase* startArrayState(v8::Local<v8::Array>, StateBase* next); 425 StateBase* startArrayState(v8::Local<v8::Array>, StateBase* next);
421 StateBase* startObjectState(v8::Local<v8::Object>, StateBase* next); 426 StateBase* startObjectState(v8::Local<v8::Object>, StateBase* next);
422 StateBase* startMapState(v8::Local<v8::Map>, StateBase* next); 427 StateBase* startMapState(v8::Local<v8::Map>, StateBase* next);
423 StateBase* startSetState(v8::Local<v8::Set>, StateBase* next); 428 StateBase* startSetState(v8::Local<v8::Set>, StateBase* next);
424 429
425 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo ng size, int* index); 430 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo ng size, int* index);
426 bool appendFileInfo(const File*, int* index); 431 bool appendFileInfo(const File*, int* index);
427 432
428 protected:
429 // Marks object as having been visited by the serializer and assigns it a un ique object reference ID.
430 // An object may only be greyed once.
431 void greyObject(const v8::Local<v8::Object>&);
432
433 StateBase* handleError(Status errorStatus, const String& message, StateBase* );
434
435 SerializedScriptValueWriter& writer() { return m_writer; }
436 uint32_t nextObjectReference() const { return m_nextObjectReference; }
437
438 private:
439
440 void copyTransferables(const Transferables&); 433 void copyTransferables(const Transferables&);
441 434
442 RefPtr<ScriptState> m_scriptState; 435 RefPtr<ScriptState> m_scriptState;
443 SerializedScriptValueWriter& m_writer; 436 SerializedScriptValueWriter& m_writer;
444 v8::TryCatch m_tryCatch; 437 v8::TryCatch m_tryCatch;
445 int m_depth; 438 int m_depth;
446 Status m_status; 439 Status m_status;
447 String m_errorMessage; 440 String m_errorMessage;
448 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; 441 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool;
449 ObjectPool m_objectPool; 442 ObjectPool m_objectPool;
(...skipping 24 matching lines...) Expand all
474 , m_blobDataHandles(blobDataHandles) 467 , m_blobDataHandles(blobDataHandles)
475 { 468 {
476 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1)); 469 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1));
477 ASSERT(length >= 0); 470 ASSERT(length >= 0);
478 } 471 }
479 472
480 bool isEof() const { return m_position >= m_length; } 473 bool isEof() const { return m_position >= m_length; }
481 474
482 ScriptState* getScriptState() const { return m_scriptState.get(); } 475 ScriptState* getScriptState() const { return m_scriptState.get(); }
483 476
477 virtual bool read(v8::Local<v8::Value>*, ScriptValueDeserializer&);
478 bool readVersion(uint32_t& version);
479 void setVersion(uint32_t);
480
484 protected: 481 protected:
485 v8::Isolate* isolate() const { return m_scriptState->isolate(); } 482 v8::Isolate* isolate() const { return m_scriptState->isolate(); }
486 v8::Local<v8::Context> context() const { return m_scriptState->context(); } 483 v8::Local<v8::Context> context() const { return m_scriptState->context(); }
487 unsigned length() const { return m_length; } 484 unsigned length() const { return m_length; }
488 unsigned position() const { return m_position; } 485 unsigned position() const { return m_position; }
489 486
490 const uint8_t* allocate(uint32_t size) 487 const uint8_t* allocate(uint32_t size)
491 { 488 {
492 const uint8_t* allocated = m_buffer + m_position; 489 const uint8_t* allocated = m_buffer + m_position;
493 m_position += size; 490 m_position += size;
494 return allocated; 491 return allocated;
495 } 492 }
496 493
497 public:
498 virtual bool read(v8::Local<v8::Value>*, ScriptValueDeserializer&);
499 bool readVersion(uint32_t& version);
500 void setVersion(uint32_t);
501
502 protected:
503 bool readWithTag(SerializationTag, v8::Local<v8::Value>*, ScriptValueDeseria lizer&); 494 bool readWithTag(SerializationTag, v8::Local<v8::Value>*, ScriptValueDeseria lizer&);
504 495
505 bool readTag(SerializationTag*); 496 bool readTag(SerializationTag*);
506 bool readWebCoreString(String*); 497 bool readWebCoreString(String*);
507 bool readUint32(v8::Local<v8::Value>*); 498 bool readUint32(v8::Local<v8::Value>*);
508 499
509 bool doReadUint32(uint32_t* value); 500 bool doReadUint32(uint32_t* value);
510 501
511 private: 502 private:
512 void undoReadTag(); 503 void undoReadTag();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 *value |= ((currentByte & SerializedScriptValue::varIntMask) << shif t); 536 *value |= ((currentByte & SerializedScriptValue::varIntMask) << shif t);
546 shift += SerializedScriptValue::varIntShift; 537 shift += SerializedScriptValue::varIntShift;
547 } while (currentByte & (1 << SerializedScriptValue::varIntShift)); 538 } while (currentByte & (1 << SerializedScriptValue::varIntShift));
548 return true; 539 return true;
549 } 540 }
550 541
551 bool doReadUint64(uint64_t* value); 542 bool doReadUint64(uint64_t* value);
552 bool doReadNumber(double* number); 543 bool doReadNumber(double* number);
553 PassRefPtr<BlobDataHandle> getOrCreateBlobDataHandle(const String& uuid, con st String& type, long long size = -1); 544 PassRefPtr<BlobDataHandle> getOrCreateBlobDataHandle(const String& uuid, con st String& type, long long size = -1);
554 545
555 private:
556 RefPtr<ScriptState> m_scriptState; 546 RefPtr<ScriptState> m_scriptState;
557 const uint8_t* m_buffer; 547 const uint8_t* m_buffer;
558 const unsigned m_length; 548 const unsigned m_length;
559 unsigned m_position; 549 unsigned m_position;
560 uint32_t m_version; 550 uint32_t m_version;
561 const WebBlobInfoArray* m_blobInfo; 551 const WebBlobInfoArray* m_blobInfo;
562 const BlobDataHandleMap& m_blobDataHandles; 552 const BlobDataHandleMap& m_blobDataHandles;
563 }; 553 };
564 554
565 class CORE_EXPORT ScriptValueDeserializer { 555 class CORE_EXPORT ScriptValueDeserializer {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 ArrayBufferContentsArray* m_arrayBufferContents; 615 ArrayBufferContentsArray* m_arrayBufferContents;
626 ImageBitmapContentsArray* m_imageBitmapContents; 616 ImageBitmapContentsArray* m_imageBitmapContents;
627 Vector<v8::Local<v8::Value>> m_arrayBuffers; 617 Vector<v8::Local<v8::Value>> m_arrayBuffers;
628 Vector<v8::Local<v8::Value>> m_imageBitmaps; 618 Vector<v8::Local<v8::Value>> m_imageBitmaps;
629 uint32_t m_version; 619 uint32_t m_version;
630 }; 620 };
631 621
632 } // namespace blink 622 } // namespace blink
633 623
634 #endif // ScriptValueSerializer_h 624 #endif // ScriptValueSerializer_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698