1/*
2 * Copyright (C) 2008, 2009 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 ScriptValue_h
32#define ScriptValue_h
33
34#include "bindings/v8/ScriptState.h"
35#include "bindings/v8/SharedPersistent.h"
36#include <v8.h>
37#include "wtf/PassRefPtr.h"
38#include "wtf/RefPtr.h"
39#include "wtf/Vector.h"
40#include "wtf/text/WTFString.h"
41
42#ifndef NDEBUG
43#include "bindings/v8/V8GCController.h"
44#endif
45
46namespace WTF {
47class ArrayBuffer;
48}
49
50namespace WebCore {
51
52class JSONValue;
53class MessagePort;
54class SerializedScriptValue;
55typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
56typedef Vector<RefPtr<WTF::ArrayBuffer>, 1> ArrayBufferArray;
57
58class ScriptValue {
59public:
60    ScriptValue() { }
61    virtual ~ScriptValue();
62
63    ScriptValue(v8::Handle<v8::Value> value)
64        : m_value(value.IsEmpty() ? 0 : SharedPersistent<v8::Value>::create(value))
65    {
66    }
67
68    ScriptValue(const ScriptValue& value)
69        : m_value(value.m_value)
70    {
71    }
72
73    static ScriptValue createNull() { return ScriptValue(v8::Null()); }
74    static ScriptValue createBoolean(bool b) { return ScriptValue(b ? v8::True() : v8::False()); }
75
76    ScriptValue& operator=(const ScriptValue& value)
77    {
78        if (this != &value)
79            m_value = value.m_value;
80        return *this;
81    }
82
83    bool operator==(const ScriptValue& value) const
84    {
85        if (hasNoValue())
86            return value.hasNoValue();
87        if (value.hasNoValue())
88            return false;
89        return *m_value == *value.m_value;
90    }
91
92    bool isEqual(ScriptState*, const ScriptValue& value) const
93    {
94        return operator==(value);
95    }
96
97    // Note: This creates a new local Handle; not to be used in cases where is
98    // is an efficiency problem.
99    bool isFunction() const
100    {
101        ASSERT(!hasNoValue());
102        v8::Handle<v8::Value> value = v8Value();
103        return !value.IsEmpty() && value->IsFunction();
104    }
105
106    bool operator!=(const ScriptValue& value) const
107    {
108        return !operator==(value);
109    }
110
111    // Note: This creates a new local Handle; not to be used in cases where is
112    // is an efficiency problem.
113    bool isNull() const
114    {
115        ASSERT(!hasNoValue());
116        v8::Handle<v8::Value> value = v8Value();
117        return !value.IsEmpty() && value->IsNull();
118    }
119
120    // Note: This creates a new local Handle; not to be used in cases where is
121    // is an efficiency problem.
122    bool isUndefined() const
123    {
124        ASSERT(!hasNoValue());
125        v8::Handle<v8::Value> value = v8Value();
126        return !value.IsEmpty() && value->IsUndefined();
127    }
128
129    // Note: This creates a new local Handle; not to be used in cases where is
130    // is an efficiency problem.
131    bool isObject() const
132    {
133        ASSERT(!hasNoValue());
134        v8::Handle<v8::Value> value = v8Value();
135        return !value.IsEmpty() && value->IsObject();
136    }
137
138    bool hasNoValue() const
139    {
140        return !m_value.get() || m_value->isEmpty();
141    }
142
143    PassRefPtr<SerializedScriptValue> serialize(ScriptState*);
144    PassRefPtr<SerializedScriptValue> serialize(ScriptState*, MessagePortArray*, ArrayBufferArray*, bool&);
145    static ScriptValue deserialize(ScriptState*, SerializedScriptValue*);
146
147    void clear()
148    {
149        m_value = 0;
150    }
151
152    v8::Handle<v8::Value> v8Value() const
153    {
154        return m_value.get() ? m_value->newLocal(v8::Isolate::GetCurrent()) : v8::Handle<v8::Value>();
155    }
156
157    bool getString(ScriptState* scriptState, String& result) const { return getString(result, scriptState->isolate()); }
158    bool getString(String& result) const { return getString(result, v8::Isolate::GetCurrent()); }
159    bool getString(String& result, v8::Isolate*) const;
160    String toString(ScriptState*) const;
161
162    PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
163
164private:
165    RefPtr<SharedPersistent<v8::Value> > m_value;
166};
167
168} // namespace WebCore
169
170#endif // ScriptValue_h
171