15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2009, 2010 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions of source code must retain the above copyright 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions in binary form must reproduce the above 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission. 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef SerializedScriptValue_h 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define SerializedScriptValue_h 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ScriptValue.h" 351e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "wtf/HashMap.h" 363c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch#include "wtf/ThreadSafeRefCounted.h" 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <v8.h> 385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)namespace WTF { 4081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 4181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)class ArrayBuffer; 4281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)class ArrayBufferContents; 4381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 4481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)} 4581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 46c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 481e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class BlobDataHandle; 4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class ExceptionState; 505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class MessagePort; 517242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass WebBlobInfo; 525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 53197021e6b966cfb06891637935ef33fff06433d1Ben Murdochtypedef WillBeHeapVector<RefPtrWillBeMember<MessagePort>, 1> MessagePortArray; 545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)typedef Vector<RefPtr<WTF::ArrayBuffer>, 1> ArrayBufferArray; 551e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)typedef HashMap<String, RefPtr<BlobDataHandle> > BlobDataHandleMap; 566f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdochtypedef Vector<blink::WebBlobInfo> WebBlobInfoArray; 575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 58f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)class SerializedScriptValue FINAL : public ThreadSafeRefCounted<SerializedScriptValue> { 595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public: 601e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) // Increment this for each incompatible change to the wire format. 611e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) // Version 2: Added StringUCharTag for UChar v8 strings. 621e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) // Version 3: Switched to using uuids as blob data identifiers. 6309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // Version 4: Extended File serialization to be complete. 6407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch // Version 5: Added CryptoKeyTag for Key objects. 656f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch // Version 6: Added indexed serialization for File, Blob, and FileList. 66c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) // Version 7: Extended File serialization with user visibility. 67c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) static const uint32_t wireFormatVersion = 7; 681e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) 69f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) ~SerializedScriptValue(); 705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // If a serialization error occurs (e.g., cyclic input value) this 725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // function returns an empty representation, schedules a V8 exception to 735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // be thrown using v8::ThrowException(), and sets |didThrow|. In this case 745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // the caller must not invoke any V8 operations until control returns to 755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // V8. When serialization is successful, |didThrow| is false. 7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) static PassRefPtr<SerializedScriptValue> create(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, ExceptionState&, v8::Isolate*); 77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static PassRefPtr<SerializedScriptValue> createFromWire(const String&); 78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static PassRefPtr<SerializedScriptValue> createFromWireBytes(const Vector<uint8_t>&); 79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static PassRefPtr<SerializedScriptValue> create(const String&); 80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static PassRefPtr<SerializedScriptValue> create(const String&, v8::Isolate*); 815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static PassRefPtr<SerializedScriptValue> create(); 8210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch static PassRefPtr<SerializedScriptValue> create(const ScriptValue&, WebBlobInfoArray*, ExceptionState&, v8::Isolate*); 8351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) 847757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch // Never throws exceptions. 857757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch static PassRefPtr<SerializedScriptValue> createAndSwallowExceptions(v8::Handle<v8::Value>, v8::Isolate*); 867757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 87926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static PassRefPtr<SerializedScriptValue> nullValue(); 88926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String toWireString() const { return m_data; } 90591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch void toWireBytes(Vector<char>&) const; 915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Deserializes the value (in the current context). Returns a null value in 935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // case of failure. 94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) v8::Handle<v8::Value> deserialize(MessagePortArray* = 0); 956f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch v8::Handle<v8::Value> deserialize(v8::Isolate*, MessagePortArray* = 0, const WebBlobInfoArray* = 0); 965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) // Helper function which pulls the values out of a JS sequence and into a MessagePortArray. 98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) // Also validates the elements per sections 4.1.13 and 4.1.15 of the WebIDL spec and section 8.3.3 99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) // of the HTML5 spec and generates exceptions as appropriate. 100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) // Returns true if the array was filled, or false if the passed value was not of an appropriate type. 101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) static bool extractTransferables(v8::Local<v8::Value>, int, MessagePortArray&, ArrayBufferArray&, ExceptionState&, v8::Isolate*); 102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles) 1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Informs the V8 about external memory allocated and owned by this object. Large values should contribute 1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // to GC counters to eventually trigger a GC, otherwise flood of postMessage() can cause OOM. 1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Ok to invoke multiple times (only adds memory once). 1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // The memory registration is revoked automatically in destructor. 1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void registerMemoryAllocatedWithCurrentScriptContext(); 1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private: 1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) enum StringDataMode { 1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) StringValue, 1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WireData 1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) }; 1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; 1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) SerializedScriptValue(); 1176f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch SerializedScriptValue(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, WebBlobInfoArray*, ExceptionState&, v8::Isolate*); 1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) explicit SerializedScriptValue(const String& wireData); 1195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 12009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) static PassOwnPtr<ArrayBufferContentsArray> transferArrayBuffers(ArrayBufferArray&, ExceptionState&, v8::Isolate*); 1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String m_data; 1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) OwnPtr<ArrayBufferContentsArray> m_arrayBufferContentsArray; 1241e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) BlobDataHandleMap m_blobDataHandles; 1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) intptr_t m_externallyAllocatedMemory; 1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 128c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // SerializedScriptValue_h 131