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

Side by Side Diff: Source/bindings/v8/SerializedScriptValue.cpp

Issue 18590006: Blob support for IDB [Blink] (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: small cleanup Created 7 years 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 #include "bindings/v8/custom/V8Uint8ArrayCustom.h" 54 #include "bindings/v8/custom/V8Uint8ArrayCustom.h"
55 #include "bindings/v8/custom/V8Uint8ClampedArrayCustom.h" 55 #include "bindings/v8/custom/V8Uint8ClampedArrayCustom.h"
56 #include "core/dom/ExceptionCode.h" 56 #include "core/dom/ExceptionCode.h"
57 #include "core/dom/MessagePort.h" 57 #include "core/dom/MessagePort.h"
58 #include "core/fileapi/Blob.h" 58 #include "core/fileapi/Blob.h"
59 #include "core/fileapi/File.h" 59 #include "core/fileapi/File.h"
60 #include "core/fileapi/FileList.h" 60 #include "core/fileapi/FileList.h"
61 #include "core/html/ImageData.h" 61 #include "core/html/ImageData.h"
62 #include "core/html/canvas/DataView.h" 62 #include "core/html/canvas/DataView.h"
63 #include "platform/SharedBuffer.h" 63 #include "platform/SharedBuffer.h"
64 #include "platform/blob/BlobInfo.h"
64 #include "wtf/ArrayBuffer.h" 65 #include "wtf/ArrayBuffer.h"
65 #include "wtf/ArrayBufferContents.h" 66 #include "wtf/ArrayBufferContents.h"
66 #include "wtf/ArrayBufferView.h" 67 #include "wtf/ArrayBufferView.h"
67 #include "wtf/Assertions.h" 68 #include "wtf/Assertions.h"
68 #include "wtf/ByteOrder.h" 69 #include "wtf/ByteOrder.h"
69 #include "wtf/Float32Array.h" 70 #include "wtf/Float32Array.h"
70 #include "wtf/Float64Array.h" 71 #include "wtf/Float64Array.h"
71 #include "wtf/Int16Array.h" 72 #include "wtf/Int16Array.h"
72 #include "wtf/Int32Array.h" 73 #include "wtf/Int32Array.h"
73 #include "wtf/Int8Array.h" 74 #include "wtf/Int8Array.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 NullTag = '0', // -> <null> 180 NullTag = '0', // -> <null>
180 TrueTag = 'T', // -> <true> 181 TrueTag = 'T', // -> <true>
181 FalseTag = 'F', // -> <false> 182 FalseTag = 'F', // -> <false>
182 StringTag = 'S', // string:RawString -> string 183 StringTag = 'S', // string:RawString -> string
183 StringUCharTag = 'c', // string:RawUCharString -> string 184 StringUCharTag = 'c', // string:RawUCharString -> string
184 Int32Tag = 'I', // value:ZigZag-encoded int32 -> Integer 185 Int32Tag = 'I', // value:ZigZag-encoded int32 -> Integer
185 Uint32Tag = 'U', // value:uint32_t -> Integer 186 Uint32Tag = 'U', // value:uint32_t -> Integer
186 DateTag = 'D', // value:double -> Date (ref) 187 DateTag = 'D', // value:double -> Date (ref)
187 MessagePortTag = 'M', // index:int -> MessagePort. Fills the result with tra nsferred MessagePort. 188 MessagePortTag = 'M', // index:int -> MessagePort. Fills the result with tra nsferred MessagePort.
188 NumberTag = 'N', // value:double -> Number 189 NumberTag = 'N', // value:double -> Number
189 BlobTag = 'b', // url:WebCoreString, type:WebCoreString, size:uint64_t -> Bl ob (ref) 190 BlobTag = 'b', // uuid:WebCoreString, type:WebCoreString, size:uint64_t -> B lob (ref)
190 FileTag = 'f', // file:RawFile -> File (ref) 191 FileTag = 'f', // file:RawFile -> File (ref)
191 DOMFileSystemTag = 'd', // type:int32_t, name:WebCoreString, url:WebCoreStri ng -> FileSystem (ref) 192 DOMFileSystemTag = 'd', // type:int32_t, name:WebCoreString, uuid:WebCoreStr ing -> FileSystem (ref)
192 FileListTag = 'l', // length:uint32_t, files:RawFile[length] -> FileList (re f) 193 FileListTag = 'l', // length:uint32_t, files:RawFile[length] -> FileList (re f)
193 ImageDataTag = '#', // width:uint32_t, height:uint32_t, pixelDataLength:uint 32_t, data:byte[pixelDataLength] -> ImageData (ref) 194 ImageDataTag = '#', // width:uint32_t, height:uint32_t, pixelDataLength:uint 32_t, data:byte[pixelDataLength] -> ImageData (ref)
194 ObjectTag = '{', // numProperties:uint32_t -> pops the last object from the open stack; 195 ObjectTag = '{', // numProperties:uint32_t -> pops the last object from the open stack;
195 // fills it with the last numProp erties name,value pairs pushed onto the deserialization stack 196 // fills it with the last numProp erties name,value pairs pushed onto the deserialization stack
196 SparseArrayTag = '@', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack; 197 SparseArrayTag = '@', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack;
197 // fills it with the last numProperties name,value pairs pushed onto the deserialization st ack 198 // fills it with the last numProperties name,value pairs pushed onto the deserialization st ack
198 DenseArrayTag = '$', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack; 199 DenseArrayTag = '$', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack;
199 // fills it with the last length elements and numProperties name,value pairs pushed onto des erialization stack 200 // fills it with the last length elements and numProperties name,value pairs pushed onto des erialization stack
200 RegExpTag = 'R', // pattern:RawString, flags:uint32_t -> RegExp (ref) 201 RegExpTag = 'R', // pattern:RawString, flags:uint32_t -> RegExp (ref)
201 ArrayBufferTag = 'B', // byteLength:uint32_t, data:byte[byteLength] -> Array Buffer (ref) 202 ArrayBufferTag = 'B', // byteLength:uint32_t, data:byte[byteLength] -> Array Buffer (ref)
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 } 386 }
386 387
387 void writeBlob(const String& uuid, const String& type, unsigned long long si ze) 388 void writeBlob(const String& uuid, const String& type, unsigned long long si ze)
388 { 389 {
389 append(BlobTag); 390 append(BlobTag);
390 doWriteWebCoreString(uuid); 391 doWriteWebCoreString(uuid);
391 doWriteWebCoreString(type); 392 doWriteWebCoreString(type);
392 doWriteUint64(size); 393 doWriteUint64(size);
393 } 394 }
394 395
396 void writeBlob(int blobIndex)
397 {
398 append(BlobTag);
399 doWriteUint32(blobIndex);
400 }
401
395 void writeDOMFileSystem(int type, const String& name, const String& url) 402 void writeDOMFileSystem(int type, const String& name, const String& url)
396 { 403 {
397 append(DOMFileSystemTag); 404 append(DOMFileSystemTag);
398 doWriteUint32(type); 405 doWriteUint32(type);
399 doWriteWebCoreString(name); 406 doWriteWebCoreString(name);
400 doWriteWebCoreString(url); 407 doWriteWebCoreString(url);
401 } 408 }
402 409
403 void writeFile(const File& file) 410 void writeFile(const File& file)
404 { 411 {
405 append(FileTag); 412 append(FileTag);
406 doWriteFile(file); 413 doWriteFile(file);
407 } 414 }
408 415
416 void writeFile(int blobIndex)
417 {
418 append(FileTag);
419 doWriteUint32(blobIndex);
420 }
421
422 // Note: We could dedupe the files in this list, but it's probably
423 // pretty rare that there are any duplicates.
409 void writeFileList(const FileList& fileList) 424 void writeFileList(const FileList& fileList)
410 { 425 {
411 append(FileListTag); 426 append(FileListTag);
412 uint32_t length = fileList.length(); 427 uint32_t length = fileList.length();
413 doWriteUint32(length); 428 doWriteUint32(length);
414 for (unsigned i = 0; i < length; ++i) 429 for (unsigned i = 0; i < length; ++i)
415 doWriteFile(*fileList.item(i)); 430 doWriteFile(*fileList.item(i));
416 } 431 }
417 432
433 void writeFileList(const Vector<int>& blobIndices)
434 {
435 append(FileListTag);
436 uint32_t length = blobIndices.size();
437 doWriteUint32(length);
438 for (unsigned i = 0; i < length; ++i) {
439 doWriteUint32(blobIndices[i]);
440 }
441 }
442
418 void writeArrayBuffer(const ArrayBuffer& arrayBuffer) 443 void writeArrayBuffer(const ArrayBuffer& arrayBuffer)
419 { 444 {
420 append(ArrayBufferTag); 445 append(ArrayBufferTag);
421 doWriteArrayBuffer(arrayBuffer); 446 doWriteArrayBuffer(arrayBuffer);
422 } 447 }
423 448
424 void writeArrayBufferView(const ArrayBufferView& arrayBufferView) 449 void writeArrayBufferView(const ArrayBufferView& arrayBufferView)
425 { 450 {
426 append(ArrayBufferViewTag); 451 append(ArrayBufferViewTag);
427 #ifndef NDEBUG 452 #ifndef NDEBUG
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 public: 711 public:
687 enum Status { 712 enum Status {
688 Success, 713 Success,
689 InputError, 714 InputError,
690 DataCloneError, 715 DataCloneError,
691 InvalidStateError, 716 InvalidStateError,
692 JSException, 717 JSException,
693 JSFailure 718 JSFailure
694 }; 719 };
695 720
696 Serializer(Writer& writer, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, BlobDataHandleMap& blobDataHandles, v8::TryCatch& tryCatch, v8::I solate* isolate) 721 Serializer(Writer& writer, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, Vector<BlobInfo>* blobInfo, BlobDataHandleMap& blobDataHandles, v 8::TryCatch& tryCatch, v8::Isolate* isolate)
697 : m_writer(writer) 722 : m_writer(writer)
698 , m_tryCatch(tryCatch) 723 , m_tryCatch(tryCatch)
699 , m_depth(0) 724 , m_depth(0)
700 , m_execDepth(0) 725 , m_execDepth(0)
701 , m_status(Success) 726 , m_status(Success)
702 , m_nextObjectReference(0) 727 , m_nextObjectReference(0)
728 , m_blobInfo(blobInfo)
703 , m_blobDataHandles(blobDataHandles) 729 , m_blobDataHandles(blobDataHandles)
704 , m_isolate(isolate) 730 , m_isolate(isolate)
705 { 731 {
706 ASSERT(!tryCatch.HasCaught()); 732 ASSERT(!tryCatch.HasCaught());
707 if (messagePorts) { 733 if (messagePorts) {
708 for (size_t i = 0; i < messagePorts->size(); i++) 734 for (size_t i = 0; i < messagePorts->size(); i++)
709 m_transferredMessagePorts.set(toV8Object(messagePorts->at(i).get (), m_writer.getIsolate()), i); 735 m_transferredMessagePorts.set(toV8Object(messagePorts->at(i).get (), m_writer.getIsolate()), i);
710 } 736 }
711 if (arrayBuffers) { 737 if (arrayBuffers) {
712 for (size_t i = 0; i < arrayBuffers->size(); i++) { 738 for (size_t i = 0; i < arrayBuffers->size(); i++) {
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 { 1090 {
1065 v8::Handle<v8::BooleanObject> booleanObject = value.As<v8::BooleanObject >(); 1091 v8::Handle<v8::BooleanObject> booleanObject = value.As<v8::BooleanObject >();
1066 m_writer.writeBooleanObject(booleanObject->BooleanValue()); 1092 m_writer.writeBooleanObject(booleanObject->BooleanValue());
1067 } 1093 }
1068 1094
1069 void writeBlob(v8::Handle<v8::Value> value) 1095 void writeBlob(v8::Handle<v8::Value> value)
1070 { 1096 {
1071 Blob* blob = V8Blob::toNative(value.As<v8::Object>()); 1097 Blob* blob = V8Blob::toNative(value.As<v8::Object>());
1072 if (!blob) 1098 if (!blob)
1073 return; 1099 return;
1074 m_writer.writeBlob(blob->uuid(), blob->type(), blob->size()); 1100 int blobIndex = -1;
1075 m_blobDataHandles.add(blob->uuid(), blob->blobDataHandle()); 1101 m_blobDataHandles.add(blob->uuid(), blob->blobDataHandle());
1102 if (appendBlobInfo(blob->uuid(), blob->type(), blob->size(), &blobIndex) ) {
1103 ASSERT(blobIndex >= 0);
1104 m_writer.writeBlob(blobIndex);
1105 } else {
1106 m_writer.writeBlob(blob->uuid(), blob->type(), blob->size());
1107 }
1076 } 1108 }
1077 1109
1078 StateBase* writeDOMFileSystem(v8::Handle<v8::Value> value, StateBase* next) 1110 StateBase* writeDOMFileSystem(v8::Handle<v8::Value> value, StateBase* next)
1079 { 1111 {
1080 DOMFileSystem* fs = V8DOMFileSystem::toNative(value.As<v8::Object>()); 1112 DOMFileSystem* fs = V8DOMFileSystem::toNative(value.As<v8::Object>());
1081 if (!fs) 1113 if (!fs)
1082 return 0; 1114 return 0;
1083 if (!fs->clonable()) 1115 if (!fs->clonable())
1084 return handleError(DataCloneError, next); 1116 return handleError(DataCloneError, next);
1085 m_writer.writeDOMFileSystem(fs->type(), fs->name(), fs->rootURL().string ()); 1117 m_writer.writeDOMFileSystem(fs->type(), fs->name(), fs->rootURL().string ());
1086 return 0; 1118 return 0;
1087 } 1119 }
1088 1120
1089 void writeFile(v8::Handle<v8::Value> value) 1121 void writeFile(v8::Handle<v8::Value> value)
1090 { 1122 {
1091 File* file = V8File::toNative(value.As<v8::Object>()); 1123 File* file = V8File::toNative(value.As<v8::Object>());
1092 if (!file) 1124 if (!file)
1093 return; 1125 return;
1094 m_writer.writeFile(*file); 1126 int blobIndex = -1;
1095 m_blobDataHandles.add(file->uuid(), file->blobDataHandle()); 1127 m_blobDataHandles.add(file->uuid(), file->blobDataHandle());
1128 if (appendFileInfo(file->uuid(), file->path(), file->name(), file->type( ), &blobIndex)) {
1129 ASSERT(blobIndex >= 0);
1130 m_writer.writeFile(blobIndex);
1131 } else {
1132 m_writer.writeFile(*file);
1133 }
1096 } 1134 }
1097 1135
1098 void writeFileList(v8::Handle<v8::Value> value) 1136 void writeFileList(v8::Handle<v8::Value> value)
1099 { 1137 {
1100 FileList* fileList = V8FileList::toNative(value.As<v8::Object>()); 1138 FileList* fileList = V8FileList::toNative(value.As<v8::Object>());
1101 if (!fileList) 1139 if (!fileList)
1102 return; 1140 return;
1103 m_writer.writeFileList(*fileList);
1104 unsigned length = fileList->length(); 1141 unsigned length = fileList->length();
1105 for (unsigned i = 0; i < length; ++i) 1142 Vector<int> blobIndices;
1106 m_blobDataHandles.add(fileList->item(i)->uuid(), fileList->item(i)-> blobDataHandle()); 1143 bool useBlobIndices = false;
1144 for (unsigned i = 0; i < length; ++i) {
1145 int blobIndex = -1;
1146 const File* file = fileList->item(i);
1147 m_blobDataHandles.add(file->uuid(), file->blobDataHandle());
1148 if (appendFileInfo(file->uuid(), file->path(), file->name(), file->t ype(), &blobIndex)) {
1149 ASSERT(!i || useBlobIndices);
1150 ASSERT(blobIndex >= 0);
1151 useBlobIndices = true;
1152 blobIndices.append(blobIndex);
1153 }
1154 }
1155 if (useBlobIndices) {
1156 m_writer.writeFileList(blobIndices);
1157 } else {
1158 m_writer.writeFileList(*fileList);
1159 }
1107 } 1160 }
1108 1161
1109 void writeImageData(v8::Handle<v8::Value> value) 1162 void writeImageData(v8::Handle<v8::Value> value)
1110 { 1163 {
1111 ImageData* imageData = V8ImageData::toNative(value.As<v8::Object>()); 1164 ImageData* imageData = V8ImageData::toNative(value.As<v8::Object>());
1112 if (!imageData) 1165 if (!imageData)
1113 return; 1166 return;
1114 Uint8ClampedArray* pixelArray = imageData->data(); 1167 Uint8ClampedArray* pixelArray = imageData->data();
1115 m_writer.writeImageData(imageData->width(), imageData->height(), pixelAr ray->data(), pixelArray->length()); 1168 m_writer.writeImageData(imageData->width(), imageData->height(), pixelAr ray->data(), pixelArray->length());
1116 } 1169 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 1260
1208 // Marks object as having been visited by the serializer and assigns it a un ique object reference ID. 1261 // Marks object as having been visited by the serializer and assigns it a un ique object reference ID.
1209 // An object may only be greyed once. 1262 // An object may only be greyed once.
1210 void greyObject(const v8::Handle<v8::Object>& object) 1263 void greyObject(const v8::Handle<v8::Object>& object)
1211 { 1264 {
1212 ASSERT(!m_objectPool.contains(object)); 1265 ASSERT(!m_objectPool.contains(object));
1213 uint32_t objectReference = m_nextObjectReference++; 1266 uint32_t objectReference = m_nextObjectReference++;
1214 m_objectPool.set(object, objectReference); 1267 m_objectPool.set(object, objectReference);
1215 } 1268 }
1216 1269
1270 bool appendBlobInfo(const String& uuid, const String& type, unsigned long lo ng size, int* index)
1271 {
1272 if (!m_blobInfo)
1273 return false;
1274 *index = m_blobInfo->size();
1275 m_blobInfo->append(BlobInfo(uuid, type, size));
1276 return true;
1277 }
1278
1279 bool appendFileInfo(const String& uuid, const String& filePath, const String & fileName, const String& type, int* index)
1280 {
1281 if (!m_blobInfo)
1282 return false;
1283 *index = m_blobInfo->size();
1284 m_blobInfo->append(BlobInfo(uuid, filePath, fileName, type));
1285 return true;
1286 }
1287
1217 Writer& m_writer; 1288 Writer& m_writer;
1218 v8::TryCatch& m_tryCatch; 1289 v8::TryCatch& m_tryCatch;
1219 int m_depth; 1290 int m_depth;
1220 int m_execDepth; 1291 int m_execDepth;
1221 Status m_status; 1292 Status m_status;
1222 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool; 1293 typedef V8ObjectMap<v8::Object, uint32_t> ObjectPool;
1223 ObjectPool m_objectPool; 1294 ObjectPool m_objectPool;
1224 ObjectPool m_transferredMessagePorts; 1295 ObjectPool m_transferredMessagePorts;
1225 ObjectPool m_transferredArrayBuffers; 1296 ObjectPool m_transferredArrayBuffers;
1226 uint32_t m_nextObjectReference; 1297 uint32_t m_nextObjectReference;
1298 Vector<BlobInfo>* m_blobInfo;
1227 BlobDataHandleMap& m_blobDataHandles; 1299 BlobDataHandleMap& m_blobDataHandles;
1228 v8::Isolate* m_isolate; 1300 v8::Isolate* m_isolate;
1229 }; 1301 };
1230 1302
1231 Serializer::StateBase* Serializer::doSerializeImpl(v8::Handle<v8::Value> value, StateBase* next) 1303 Serializer::StateBase* Serializer::doSerializeImpl(v8::Handle<v8::Value> value, StateBase* next)
1232 { 1304 {
1233 m_writer.writeReferenceCount(m_nextObjectReference); 1305 m_writer.writeReferenceCount(m_nextObjectReference);
1234 uint32_t objectReference; 1306 uint32_t objectReference;
1235 uint32_t arrayBufferIndex; 1307 uint32_t arrayBufferIndex;
1236 WrapperWorldType currentWorldType = worldType(m_isolate); 1308 WrapperWorldType currentWorldType = worldType(m_isolate);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 virtual bool newObject() = 0; 1404 virtual bool newObject() = 0;
1333 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*) = 0; 1405 virtual bool completeObject(uint32_t numProperties, v8::Handle<v8::Value>*) = 0;
1334 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8 ::Handle<v8::Value>*) = 0; 1406 virtual bool completeSparseArray(uint32_t numProperties, uint32_t length, v8 ::Handle<v8::Value>*) = 0;
1335 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8: :Handle<v8::Value>*) = 0; 1407 virtual bool completeDenseArray(uint32_t numProperties, uint32_t length, v8: :Handle<v8::Value>*) = 0;
1336 }; 1408 };
1337 1409
1338 // Reader is responsible for deserializing primitive types and 1410 // Reader is responsible for deserializing primitive types and
1339 // restoring information about saved objects of composite types. 1411 // restoring information about saved objects of composite types.
1340 class Reader { 1412 class Reader {
1341 public: 1413 public:
1342 Reader(const uint8_t* buffer, int length, v8::Isolate* isolate, const BlobD ataHandleMap& blobDataHandles) 1414 Reader(const uint8_t* buffer, int length, v8::Isolate* isolate, const Vector <BlobInfo>* blobInfo, BlobDataHandleMap& blobDataHandles)
1343 : m_buffer(buffer) 1415 : m_buffer(buffer)
1344 , m_length(length) 1416 , m_length(length)
1345 , m_position(0) 1417 , m_position(0)
1346 , m_version(0) 1418 , m_version(0)
1347 , m_isolate(isolate) 1419 , m_isolate(isolate)
1420 , m_blobInfo(blobInfo)
1348 , m_blobDataHandles(blobDataHandles) 1421 , m_blobDataHandles(blobDataHandles)
1349 { 1422 {
1350 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1)); 1423 ASSERT(!(reinterpret_cast<size_t>(buffer) & 1));
1351 ASSERT(length >= 0); 1424 ASSERT(length >= 0);
1352 } 1425 }
1353 1426
1354 bool isEof() const { return m_position >= m_length; } 1427 bool isEof() const { return m_position >= m_length; }
1355 1428
1356 bool read(v8::Handle<v8::Value>* value, CompositeCreator& creator) 1429 bool read(v8::Handle<v8::Value>* value, CompositeCreator& creator)
1357 { 1430 {
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
1853 return true; 1926 return true;
1854 } 1927 }
1855 1928
1856 bool readBlob(v8::Handle<v8::Value>* value) 1929 bool readBlob(v8::Handle<v8::Value>* value)
1857 { 1930 {
1858 if (m_version < 3) 1931 if (m_version < 3)
1859 return false; 1932 return false;
1860 String uuid; 1933 String uuid;
1861 String type; 1934 String type;
1862 uint64_t size; 1935 uint64_t size;
1863 if (!readWebCoreString(&uuid)) 1936 if (m_blobInfo) {
1864 return false; 1937 uint32_t index;
1865 if (!readWebCoreString(&type)) 1938 if (!doReadUint32(&index) || index >= m_blobInfo->size())
1866 return false; 1939 return false;
1867 if (!doReadUint64(&size)) 1940 const BlobInfo& info = (*m_blobInfo)[index];
1868 return false; 1941 uuid = info.uuid();
1942 type = info.type();
1943 size = info.size();
1944 } else {
1945 if (!readWebCoreString(&uuid))
1946 return false;
1947 if (!readWebCoreString(&type))
1948 return false;
1949 if (!doReadUint64(&size))
1950 return false;
1951 }
1869 RefPtr<Blob> blob = Blob::create(getOrCreateBlobDataHandle(uuid, type, s ize)); 1952 RefPtr<Blob> blob = Blob::create(getOrCreateBlobDataHandle(uuid, type, s ize));
1870 *value = toV8(blob.release(), v8::Handle<v8::Object>(), m_isolate); 1953 *value = toV8(blob.release(), v8::Handle<v8::Object>(), m_isolate);
1871 return true; 1954 return true;
1872 } 1955 }
1873 1956
1874 bool readDOMFileSystem(v8::Handle<v8::Value>* value) 1957 bool readDOMFileSystem(v8::Handle<v8::Value>* value)
1875 { 1958 {
1876 uint32_t type; 1959 uint32_t type;
1877 String name; 1960 String name;
1878 String url; 1961 String url;
1879 if (!doReadUint32(&type)) 1962 if (!doReadUint32(&type))
1880 return false; 1963 return false;
1881 if (!readWebCoreString(&name)) 1964 if (!readWebCoreString(&name))
1882 return false; 1965 return false;
1883 if (!readWebCoreString(&url)) 1966 if (!readWebCoreString(&url))
1884 return false; 1967 return false;
1885 RefPtr<DOMFileSystem> fs = DOMFileSystem::create(getExecutionContext(), name, static_cast<WebCore::FileSystemType>(type), KURL(ParsedURLString, url)); 1968 RefPtr<DOMFileSystem> fs = DOMFileSystem::create(getExecutionContext(), name, static_cast<WebCore::FileSystemType>(type), KURL(ParsedURLString, url));
1886 *value = toV8(fs.release(), v8::Handle<v8::Object>(), m_isolate); 1969 *value = toV8(fs.release(), v8::Handle<v8::Object>(), m_isolate);
1887 return true; 1970 return true;
1888 } 1971 }
1889 1972
1890 bool readFile(v8::Handle<v8::Value>* value) 1973 bool readFile(v8::Handle<v8::Value>* value)
1891 { 1974 {
1892 RefPtr<File> file = doReadFileHelper(); 1975 RefPtr<File> file = readSingleFile();
1893 if (!file) 1976 if (!file)
1894 return false; 1977 return false;
1978
1895 *value = toV8(file.release(), v8::Handle<v8::Object>(), m_isolate); 1979 *value = toV8(file.release(), v8::Handle<v8::Object>(), m_isolate);
1896 return true; 1980 return true;
1897 } 1981 }
1898 1982
1899 bool readFileList(v8::Handle<v8::Value>* value) 1983 bool readFileList(v8::Handle<v8::Value>* value)
1900 { 1984 {
1901 if (m_version < 3) 1985 if (m_version < 3)
1902 return false; 1986 return false;
1903 uint32_t length; 1987 uint32_t length;
1904 if (!doReadUint32(&length)) 1988 if (!doReadUint32(&length))
1905 return false; 1989 return false;
1906 RefPtr<FileList> fileList = FileList::create(); 1990 RefPtr<FileList> fileList = FileList::create();
1907 for (unsigned i = 0; i < length; ++i) { 1991 for (unsigned i = 0; i < length; ++i) {
1908 RefPtr<File> file = doReadFileHelper(); 1992 RefPtr<File> file = readSingleFile();
1909 if (!file) 1993 if (!file)
1910 return false; 1994 return false;
1911 fileList->append(file.release()); 1995 fileList->append(file.get());
1912 } 1996 }
1913 *value = toV8(fileList.release(), v8::Handle<v8::Object>(), m_isolate); 1997 *value = toV8(fileList.release(), v8::Handle<v8::Object>(), m_isolate);
1914 return true; 1998 return true;
1915 } 1999 }
1916 2000
1917 PassRefPtr<File> doReadFileHelper() 2001 PassRefPtr<File> readSingleFile()
1918 { 2002 {
1919 if (m_version < 3) 2003 RefPtr<File> file;
1920 return 0; 2004 if (m_blobInfo) {
2005 uint32_t index;
2006 if (!doReadUint32(&index) || index >= m_blobInfo->size())
2007 return file;
2008 const BlobInfo& info = (*m_blobInfo)[index];
2009 file = File::create(info.filePath(), info.fileName(), getOrCreateBlo bDataHandle(info.uuid(), info.type(), info.size()), info.lastModified(), info.si ze());
2010 return file;
2011 }
1921 String path; 2012 String path;
1922 String uuid; 2013 String uuid;
1923 String type; 2014 String type;
1924 if (!readWebCoreString(&path)) 2015 if (!readWebCoreString(&path))
1925 return 0; 2016 return file;
1926 if (!readWebCoreString(&uuid)) 2017 if (!readWebCoreString(&uuid))
1927 return 0; 2018 return file;
1928 if (!readWebCoreString(&type)) 2019 if (!readWebCoreString(&type))
1929 return 0; 2020 return file;
1930 return File::create(path, getOrCreateBlobDataHandle(uuid, type)); 2021 return File::create(path, getOrCreateBlobDataHandle(uuid, type));
1931 } 2022 }
1932 2023
1933 template<class T> 2024 template<class T>
1934 bool doReadUintHelper(T* value) 2025 bool doReadUintHelper(T* value)
1935 { 2026 {
1936 *value = 0; 2027 *value = 0;
1937 uint8_t currentByte; 2028 uint8_t currentByte;
1938 int shift = 0; 2029 int shift = 0;
1939 do { 2030 do {
(...skipping 30 matching lines...) Expand all
1970 { 2061 {
1971 // The containing ssv may have a BDH for this uuid if this ssv is just b eing 2062 // The containing ssv may have a BDH for this uuid if this ssv is just b eing
1972 // passed from main to worker thread (for example). We use those values when creating 2063 // passed from main to worker thread (for example). We use those values when creating
1973 // the new blob instead of cons'ing up a new BDH. 2064 // the new blob instead of cons'ing up a new BDH.
1974 // 2065 //
1975 // FIXME: Maybe we should require that it work that way where the ssv mu st have a BDH for any 2066 // FIXME: Maybe we should require that it work that way where the ssv mu st have a BDH for any
1976 // blobs it comes across during deserialization. Would require callers t o explicitly populate 2067 // blobs it comes across during deserialization. Would require callers t o explicitly populate
1977 // the collection of BDH's for blobs to work, which would encourage life times to be considered 2068 // the collection of BDH's for blobs to work, which would encourage life times to be considered
1978 // when passing ssv's around cross process. At present, we get 'lucky' i n some cases because 2069 // when passing ssv's around cross process. At present, we get 'lucky' i n some cases because
1979 // the blob in the src process happens to still exist at the time the de st process is deserializing. 2070 // the blob in the src process happens to still exist at the time the de st process is deserializing.
1980 // For example in sharedWorker.postMesssage(...). 2071 // For example in sharedWorker.postMessage(...).
1981 BlobDataHandleMap::const_iterator it = m_blobDataHandles.find(uuid); 2072 BlobDataHandleMap::const_iterator it = m_blobDataHandles.find(uuid);
1982 if (it != m_blobDataHandles.end()) { 2073 if (it != m_blobDataHandles.end()) {
1983 // make assertions about type and size? 2074 // make assertions about type and size?
1984 return it->value; 2075 return it->value;
1985 } 2076 }
1986 return BlobDataHandle::create(uuid, type, size); 2077 return BlobDataHandle::create(uuid, type, size);
1987 } 2078 }
1988 2079
1989 const uint8_t* m_buffer; 2080 const uint8_t* m_buffer;
1990 const unsigned m_length; 2081 const unsigned m_length;
1991 unsigned m_position; 2082 unsigned m_position;
1992 uint32_t m_version; 2083 uint32_t m_version;
1993 v8::Isolate* m_isolate; 2084 v8::Isolate* m_isolate;
1994 const BlobDataHandleMap& m_blobDataHandles; 2085 const Vector<BlobInfo>* m_blobInfo;
2086 BlobDataHandleMap m_blobDataHandles;
1995 }; 2087 };
1996 2088
1997 2089
1998 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; 2090 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray;
1999 2091
2000 class Deserializer : public CompositeCreator { 2092 class Deserializer : public CompositeCreator {
2001 public: 2093 public:
2002 Deserializer(Reader& reader, MessagePortArray* messagePorts, ArrayBufferCont entsArray* arrayBufferContents) 2094 Deserializer(Reader& reader, MessagePortArray* messagePorts, ArrayBufferCont entsArray* arrayBufferContents)
2003 : m_reader(reader) 2095 : m_reader(reader)
2004 , m_transferredMessagePorts(messagePorts) 2096 , m_transferredMessagePorts(messagePorts)
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
2247 MessagePortArray* m_transferredMessagePorts; 2339 MessagePortArray* m_transferredMessagePorts;
2248 ArrayBufferContentsArray* m_arrayBufferContents; 2340 ArrayBufferContentsArray* m_arrayBufferContents;
2249 Vector<v8::Handle<v8::Object> > m_arrayBuffers; 2341 Vector<v8::Handle<v8::Object> > m_arrayBuffers;
2250 uint32_t m_version; 2342 uint32_t m_version;
2251 }; 2343 };
2252 2344
2253 } // namespace 2345 } // namespace
2254 2346
2255 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(v8::Handle<v8::V alue> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, boo l& didThrow, v8::Isolate* isolate) 2347 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(v8::Handle<v8::V alue> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, boo l& didThrow, v8::Isolate* isolate)
2256 { 2348 {
2257 return adoptRef(new SerializedScriptValue(value, messagePorts, arrayBuffers, didThrow, isolate)); 2349 return adoptRef(new SerializedScriptValue(value, messagePorts, arrayBuffers, 0, didThrow, isolate));
2350 }
2351
2352 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(v8::Handle<v8::V alue> value, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, Vec tor<BlobInfo>* blobInfo, bool& didThrow, v8::Isolate* isolate)
2353 {
2354 return adoptRef(new SerializedScriptValue(value, messagePorts, arrayBuffers, blobInfo, didThrow, isolate));
2258 } 2355 }
2259 2356
2260 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createAndSwallowExcepti ons(v8::Handle<v8::Value> value, v8::Isolate* isolate) 2357 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createAndSwallowExcepti ons(v8::Handle<v8::Value> value, v8::Isolate* isolate)
2261 { 2358 {
2262 bool didThrow; 2359 bool didThrow;
2263 return adoptRef(new SerializedScriptValue(value, 0, 0, didThrow, isolate, Do NotThrowExceptions)); 2360 return adoptRef(new SerializedScriptValue(value, 0, 0, 0, didThrow, isolate, DoNotThrowExceptions));
2264 } 2361 }
2265 2362
2266 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const ScriptValu e& value, bool& didThrow, ScriptState* state) 2363 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const ScriptValu e& value, Vector<BlobInfo>* blobInfo, bool& didThrow, ScriptState* state)
2267 { 2364 {
2268 ScriptScope scope(state); 2365 ScriptScope scope(state);
2269 return adoptRef(new SerializedScriptValue(value.v8Value(), 0, 0, didThrow, s tate->isolate())); 2366 return adoptRef(new SerializedScriptValue(value.v8Value(), 0, 0, blobInfo, d idThrow, state->isolate()));
2270 } 2367 }
2271 2368
2272 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWire(const St ring& data) 2369 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWire(const St ring& data)
2273 { 2370 {
2274 return adoptRef(new SerializedScriptValue(data)); 2371 return adoptRef(new SerializedScriptValue(data));
2275 } 2372 }
2276 2373
2277 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWireBytes(con st Vector<uint8_t>& data) 2374 PassRefPtr<SerializedScriptValue> SerializedScriptValue::createFromWireBytes(con st Vector<uint8_t>& data)
2278 { 2375 {
2279 // Decode wire data from big endian to host byte order. 2376 // Decode wire data from big endian to host byte order.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2396 return nullptr; 2493 return nullptr;
2397 } 2494 }
2398 2495
2399 neuterBinding(arrayBuffers[i].get()); 2496 neuterBinding(arrayBuffers[i].get());
2400 for (size_t j = 0; j < neuteredViews.size(); j++) 2497 for (size_t j = 0; j < neuteredViews.size(); j++)
2401 neuterBinding(neuteredViews[j].get()); 2498 neuterBinding(neuteredViews[j].get());
2402 } 2499 }
2403 return contents.release(); 2500 return contents.release();
2404 } 2501 }
2405 2502
2406 SerializedScriptValue::SerializedScriptValue(v8::Handle<v8::Value> value, Messag ePortArray* messagePorts, ArrayBufferArray* arrayBuffers, bool& didThrow, v8::Is olate* isolate, ExceptionPolicy policy) 2503 SerializedScriptValue::SerializedScriptValue(v8::Handle<v8::Value> value, Messag ePortArray* messagePorts, ArrayBufferArray* arrayBuffers, Vector<BlobInfo>* blob Info, bool& didThrow, v8::Isolate* isolate, ExceptionPolicy policy)
2407 : m_externallyAllocatedMemory(0) 2504 : m_externallyAllocatedMemory(0)
2408 { 2505 {
2409 didThrow = false; 2506 didThrow = false;
2410 Writer writer(isolate); 2507 Writer writer(isolate);
2411 Serializer::Status status; 2508 Serializer::Status status;
2412 { 2509 {
2413 v8::TryCatch tryCatch; 2510 v8::TryCatch tryCatch;
2414 Serializer serializer(writer, messagePorts, arrayBuffers, m_blobDataHand les, tryCatch, isolate); 2511 Serializer serializer(writer, messagePorts, arrayBuffers, blobInfo, m_bl obDataHandles, tryCatch, isolate);
2415 status = serializer.serialize(value); 2512 status = serializer.serialize(value);
2416 if (status == Serializer::JSException) { 2513 if (status == Serializer::JSException) {
2417 didThrow = true; 2514 didThrow = true;
2418 // If there was a JS exception thrown, re-throw it. 2515 // If there was a JS exception thrown, re-throw it.
2419 if (policy == ThrowExceptions) 2516 if (policy == ThrowExceptions)
2420 tryCatch.ReThrow(); 2517 tryCatch.ReThrow();
2421 return; 2518 return;
2422 } 2519 }
2423 } 2520 }
2424 switch (status) { 2521 switch (status) {
(...skipping 30 matching lines...) Expand all
2455 } 2552 }
2456 2553
2457 SerializedScriptValue::SerializedScriptValue(const String& wireData) 2554 SerializedScriptValue::SerializedScriptValue(const String& wireData)
2458 : m_externallyAllocatedMemory(0) 2555 : m_externallyAllocatedMemory(0)
2459 { 2556 {
2460 m_data = wireData.isolatedCopy(); 2557 m_data = wireData.isolatedCopy();
2461 } 2558 }
2462 2559
2463 v8::Handle<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messa gePorts) 2560 v8::Handle<v8::Value> SerializedScriptValue::deserialize(MessagePortArray* messa gePorts)
2464 { 2561 {
2465 return deserialize(v8::Isolate::GetCurrent(), messagePorts); 2562 return deserialize(v8::Isolate::GetCurrent(), messagePorts, 0);
2466 } 2563 }
2467 2564
2468 v8::Handle<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, M essagePortArray* messagePorts) 2565 v8::Handle<v8::Value> SerializedScriptValue::deserialize(v8::Isolate* isolate, M essagePortArray* messagePorts, const Vector<BlobInfo>* blobInfo)
2469 { 2566 {
2470 if (!m_data.impl()) 2567 if (!m_data.impl())
2471 return v8NullWithCheck(isolate); 2568 return v8NullWithCheck(isolate);
2472 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); 2569 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes);
2473 m_data.ensure16Bit(); 2570 m_data.ensure16Bit();
2474 // FIXME: SerializedScriptValue shouldn't use String for its underlying 2571 // FIXME: SerializedScriptValue shouldn't use String for its underlying
2475 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The 2572 // storage. Instead, it should use SharedBuffer or Vector<uint8_t>. The
2476 // information stored in m_data isn't even encoded in UTF-16. Instead, 2573 // information stored in m_data isn't even encoded in UTF-16. Instead,
2477 // unicode characters are encoded as UTF-8 with two code units per UChar. 2574 // unicode characters are encoded as UTF-8 with two code units per UChar.
2478 Reader reader(reinterpret_cast<const uint8_t*>(m_data.impl()->characters16() ), 2 * m_data.length(), isolate, m_blobDataHandles); 2575 Reader reader(reinterpret_cast<const uint8_t*>(m_data.impl()->characters16() ), 2 * m_data.length(), isolate, blobInfo, m_blobDataHandles);
2479 Deserializer deserializer(reader, messagePorts, m_arrayBufferContentsArray.g et()); 2576 Deserializer deserializer(reader, messagePorts, m_arrayBufferContentsArray.g et());
2480 2577
2481 // deserialize() can run arbitrary script (e.g., setters), which could resul t in |this| being destroyed. 2578 // deserialize() can run arbitrary script (e.g., setters), which could resul t in |this| being destroyed.
2482 // Holding a RefPtr ensures we are alive (along with our internal data) thro ughout the operation. 2579 // Holding a RefPtr ensures we are alive (along with our internal data) thro ughout the operation.
2483 RefPtr<SerializedScriptValue> protect(this); 2580 RefPtr<SerializedScriptValue> protect(this);
2484 return deserializer.deserialize(); 2581 return deserializer.deserialize();
2485 } 2582 }
2486 2583
2487 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() 2584 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext()
2488 { 2585 {
2489 if (m_externallyAllocatedMemory) 2586 if (m_externallyAllocatedMemory)
2490 return; 2587 return;
2491 m_externallyAllocatedMemory = static_cast<intptr_t>(m_data.length()); 2588 m_externallyAllocatedMemory = static_cast<intptr_t>(m_data.length());
2492 v8::V8::AdjustAmountOfExternalAllocatedMemory(m_externallyAllocatedMemory); 2589 v8::V8::AdjustAmountOfExternalAllocatedMemory(m_externallyAllocatedMemory);
2493 } 2590 }
2494 2591
2495 SerializedScriptValue::~SerializedScriptValue() 2592 SerializedScriptValue::~SerializedScriptValue()
2496 { 2593 {
2497 // If the allocated memory was not registered before, then this class is lik ely 2594 // If the allocated memory was not registered before, then this class is lik ely
2498 // used in a context other then Worker's onmessage environment and the prese nce of 2595 // used in a context other then Worker's onmessage environment and the prese nce of
2499 // current v8 context is not guaranteed. Avoid calling v8 then. 2596 // current v8 context is not guaranteed. Avoid calling v8 then.
2500 if (m_externallyAllocatedMemory) { 2597 if (m_externallyAllocatedMemory) {
2501 ASSERT(v8::Isolate::GetCurrent()); 2598 ASSERT(v8::Isolate::GetCurrent());
2502 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo ry); 2599 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo ry);
2503 } 2600 }
2504 } 2601 }
2505 2602
2506 } // namespace WebCore 2603 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698