Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / SerializedScriptValue.h
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/core/v8/ScriptValue.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/ThreadSafeRefCounted.h"
37 #include <v8.h>
38
39 namespace WTF {
40
41 class ArrayBuffer;
42 class ArrayBufferContents;
43
44 }
45
46 namespace blink {
47
48 class BlobDataHandle;
49 class DOMArrayBuffer;
50 class ExceptionState;
51 class MessagePort;
52 class WebBlobInfo;
53
54 typedef WillBeHeapVector<RefPtrWillBeMember<MessagePort>, 1> MessagePortArray;
55 typedef Vector<RefPtr<DOMArrayBuffer>, 1> ArrayBufferArray;
56 typedef HashMap<String, RefPtr<BlobDataHandle> > BlobDataHandleMap;
57 typedef Vector<blink::WebBlobInfo> WebBlobInfoArray;
58
59 class SerializedScriptValue final : public ThreadSafeRefCounted<SerializedScriptValue> {
60 public:
61     // Increment this for each incompatible change to the wire format.
62     // Version 2: Added StringUCharTag for UChar v8 strings.
63     // Version 3: Switched to using uuids as blob data identifiers.
64     // Version 4: Extended File serialization to be complete.
65     // Version 5: Added CryptoKeyTag for Key objects.
66     // Version 6: Added indexed serialization for File, Blob, and FileList.
67     // Version 7: Extended File serialization with user visibility.
68     static const uint32_t wireFormatVersion = 7;
69
70     ~SerializedScriptValue();
71
72     // If a serialization error occurs (e.g., cyclic input value) this
73     // function returns an empty representation, schedules a V8 exception to
74     // be thrown using v8::ThrowException(), and sets |didThrow|. In this case
75     // the caller must not invoke any V8 operations until control returns to
76     // V8. When serialization is successful, |didThrow| is false.
77     static PassRefPtr<SerializedScriptValue> create(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, ExceptionState&, v8::Isolate*);
78     static PassRefPtr<SerializedScriptValue> createFromWire(const String&);
79     static PassRefPtr<SerializedScriptValue> createFromWireBytes(const Vector<uint8_t>&);
80     static PassRefPtr<SerializedScriptValue> create(const String&);
81     static PassRefPtr<SerializedScriptValue> create(const String&, v8::Isolate*);
82     static PassRefPtr<SerializedScriptValue> create();
83     static PassRefPtr<SerializedScriptValue> create(const ScriptValue&, WebBlobInfoArray*, ExceptionState&, v8::Isolate*);
84
85     // Never throws exceptions.
86     static PassRefPtr<SerializedScriptValue> createAndSwallowExceptions(v8::Isolate*, v8::Handle<v8::Value>);
87
88     static PassRefPtr<SerializedScriptValue> nullValue();
89
90     String toWireString() const { return m_data; }
91     void toWireBytes(Vector<char>&) const;
92
93     // Deserializes the value (in the current context). Returns a null value in
94     // case of failure.
95     v8::Handle<v8::Value> deserialize(MessagePortArray* = 0);
96     v8::Handle<v8::Value> deserialize(v8::Isolate*, MessagePortArray* = 0, const WebBlobInfoArray* = 0);
97
98     // Helper function which pulls the values out of a JS sequence and into a MessagePortArray.
99     // Also validates the elements per sections 4.1.13 and 4.1.15 of the WebIDL spec and section 8.3.3
100     // of the HTML5 spec and generates exceptions as appropriate.
101     // Returns true if the array was filled, or false if the passed value was not of an appropriate type.
102     static bool extractTransferables(v8::Isolate*, v8::Local<v8::Value>, int, MessagePortArray&, ArrayBufferArray&, ExceptionState&);
103
104     // Informs the V8 about external memory allocated and owned by this object. Large values should contribute
105     // to GC counters to eventually trigger a GC, otherwise flood of postMessage() can cause OOM.
106     // Ok to invoke multiple times (only adds memory once).
107     // The memory registration is revoked automatically in destructor.
108     void registerMemoryAllocatedWithCurrentScriptContext();
109
110 private:
111     enum StringDataMode {
112         StringValue,
113         WireData
114     };
115     typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray;
116
117     SerializedScriptValue();
118     SerializedScriptValue(v8::Handle<v8::Value>, MessagePortArray*, ArrayBufferArray*, WebBlobInfoArray*, ExceptionState&, v8::Isolate*);
119     explicit SerializedScriptValue(const String& wireData);
120
121     static PassOwnPtr<ArrayBufferContentsArray> transferArrayBuffers(v8::Isolate*, ArrayBufferArray&, ExceptionState&);
122
123     String m_data;
124     OwnPtr<ArrayBufferContentsArray> m_arrayBufferContentsArray;
125     BlobDataHandleMap m_blobDataHandles;
126     intptr_t m_externallyAllocatedMemory;
127 };
128
129 } // namespace blink
130
131 #endif // SerializedScriptValue_h