Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef SerializedScriptValue_h
     32 #define SerializedScriptValue_h
     33 
     34 #include "bindings/v8/ScriptValue.h"
     35 
     36 #include "wtf/HashMap.h"
     37 #include "wtf/ThreadSafeRefCounted.h"
     38 #include <v8.h>
     39 
     40 namespace WTF {
     41 
     42 class ArrayBuffer;
     43 class ArrayBufferContents;
     44 
     45 }
     46 
     47 namespace WebCore {
     48 
     49 class BlobDataHandle;
     50 class MessagePort;
     51 
     52 typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
     53 typedef Vector<RefPtr<WTF::ArrayBuffer>, 1> ArrayBufferArray;
     54 typedef HashMap<String, RefPtr<BlobDataHandle> > BlobDataHandleMap;
     55 
     56 class SerializedScriptValue FINAL : public ThreadSafeRefCounted<SerializedScriptValue> {
     57 public:
     58     // Increment this for each incompatible change to the wire format.
     59     // Version 2: Added StringUCharTag for UChar v8 strings.
     60     // Version 3: Switched to using uuids as blob data identifiers.
     61     // Version 4: Extended File serialization to be complete.
     62     static const uint32_t wireFormatVersion = 4;
     63 
     64     ~SerializedScriptValue();
     65 
     66     // If a serialization error occurs (e.g., cyclic input value) this
     67     // function returns an empty representation, schedules a V8 exception to
     68     // be thrown using v8::ThrowException(), and sets |didThrow|. In this case
     69     // the caller must not invoke any V8 operations until control returns to
     70     // V8. When serialization is successful, |didThrow| is false.
     71     static PassRefPtr<SerializedScriptValue> create(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, bool& didThrow, v8::Isolate*);
     72     static PassRefPtr<SerializedScriptValue> createFromWire(const String&);
     73     static PassRefPtr<SerializedScriptValue> createFromWireBytes(const Vector<uint8_t>&);
     74     static PassRefPtr<SerializedScriptValue> create(const String&);
     75     static PassRefPtr<SerializedScriptValue> create(const String&, v8::Isolate*);
     76     static PassRefPtr<SerializedScriptValue> create();
     77 
     78     static PassRefPtr<SerializedScriptValue> create(const ScriptValue&, bool& didThrow, ScriptState*);
     79 
     80     // Never throws exceptions.
     81     static PassRefPtr<SerializedScriptValue> createAndSwallowExceptions(v8::Handle<v8::Value>, v8::Isolate*);
     82 
     83     static PassRefPtr<SerializedScriptValue> nullValue();
     84     static PassRefPtr<SerializedScriptValue> nullValue(v8::Isolate*);
     85 
     86     String toWireString() const { return m_data; }
     87     void toWireBytes(Vector<char>&) const;
     88 
     89     // Deserializes the value (in the current context). Returns a null value in
     90     // case of failure.
     91     v8::Handle<v8::Value> deserialize(MessagePortArray* = 0);
     92     v8::Handle<v8::Value> deserialize(v8::Isolate*, MessagePortArray* = 0);
     93 
     94     // Only reflects the truth if the SSV was created by walking a v8 value, not reliable
     95     // if the SSV was created createdFromWire(data).
     96     bool containsBlobs() const { return !m_blobDataHandles.isEmpty(); }
     97 
     98     // Informs the V8 about external memory allocated and owned by this object. Large values should contribute
     99     // to GC counters to eventually trigger a GC, otherwise flood of postMessage() can cause OOM.
    100     // Ok to invoke multiple times (only adds memory once).
    101     // The memory registration is revoked automatically in destructor.
    102     void registerMemoryAllocatedWithCurrentScriptContext();
    103 
    104 private:
    105     enum StringDataMode {
    106         StringValue,
    107         WireData
    108     };
    109     enum ExceptionPolicy {
    110         ThrowExceptions,
    111         DoNotThrowExceptions
    112     };
    113     typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray;
    114 
    115     SerializedScriptValue();
    116     SerializedScriptValue(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, bool& didThrow, v8::Isolate*, ExceptionPolicy = ThrowExceptions);
    117     explicit SerializedScriptValue(const String& wireData);
    118 
    119     static PassOwnPtr<ArrayBufferContentsArray> transferArrayBuffers(ArrayBufferArray&, bool& didThrow, v8::Isolate*);
    120 
    121     String m_data;
    122     OwnPtr<ArrayBufferContentsArray> m_arrayBufferContentsArray;
    123     BlobDataHandleMap m_blobDataHandles;
    124     intptr_t m_externallyAllocatedMemory;
    125 };
    126 
    127 } // namespace WebCore
    128 
    129 #endif // SerializedScriptValue_h
    130