1/*
2 * Copyright (C) 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#include "config.h"
32#include "ScriptObject.h"
33
34#include "ScriptScope.h"
35#include "ScriptState.h"
36
37#include "Document.h"
38#include "Frame.h"
39#include "V8Binding.h"
40#include "V8InjectedScriptHost.h"
41#include "V8InspectorBackend.h"
42#include "V8InspectorFrontendHost.h"
43#include "V8Proxy.h"
44
45#include <v8.h>
46
47namespace WebCore {
48
49ScriptObject::ScriptObject(ScriptState* scriptState, v8::Handle<v8::Object> v8Object)
50    : ScriptValue(v8Object)
51    , m_scriptState(scriptState)
52{
53}
54
55v8::Local<v8::Object> ScriptObject::v8Object() const
56{
57    ASSERT(v8Value()->IsObject());
58    return v8::Local<v8::Object>(v8::Object::Cast(*v8Value()));
59}
60
61bool ScriptObject::set(const String& name, const String& value)
62{
63    ScriptScope scope(m_scriptState);
64    v8Object()->Set(v8String(name), v8String(value));
65    return scope.success();
66}
67
68bool ScriptObject::set(const char* name, const ScriptObject& value)
69{
70    if (value.scriptState() != m_scriptState) {
71        ASSERT_NOT_REACHED();
72        return false;
73    }
74    ScriptScope scope(m_scriptState);
75    v8Object()->Set(v8::String::New(name), value.v8Value());
76    return scope.success();
77}
78
79bool ScriptObject::set(const char* name, const String& value)
80{
81    ScriptScope scope(m_scriptState);
82    v8Object()->Set(v8::String::New(name), v8String(value));
83    return scope.success();
84}
85
86bool ScriptObject::set(const char* name, double value)
87{
88    ScriptScope scope(m_scriptState);
89    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
90    return scope.success();
91}
92
93bool ScriptObject::set(const char* name, long value)
94{
95    ScriptScope scope(m_scriptState);
96    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
97    return scope.success();
98}
99
100bool ScriptObject::set(const char* name, long long value)
101{
102    ScriptScope scope(m_scriptState);
103    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
104    return scope.success();
105}
106
107bool ScriptObject::set(const char* name, int value)
108{
109    ScriptScope scope(m_scriptState);
110    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
111    return scope.success();
112}
113
114bool ScriptObject::set(const char* name, unsigned value)
115{
116    ScriptScope scope(m_scriptState);
117    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
118    return scope.success();
119}
120
121bool ScriptObject::set(const char* name, unsigned long value)
122{
123    ScriptScope scope(m_scriptState);
124    v8Object()->Set(v8::String::New(name), v8::Number::New(value));
125    return scope.success();
126}
127
128bool ScriptObject::set(const char* name, bool value)
129{
130    ScriptScope scope(m_scriptState);
131    v8Object()->Set(v8::String::New(name), v8Boolean(value));
132    return scope.success();
133}
134
135ScriptObject ScriptObject::createNew(ScriptState* scriptState)
136{
137    ScriptScope scope(scriptState);
138    return ScriptObject(scriptState, v8::Object::New());
139}
140
141bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, const ScriptObject& value)
142{
143    ScriptScope scope(scriptState);
144    scope.global()->Set(v8::String::New(name), value.v8Value());
145    return scope.success();
146}
147
148#if ENABLE(INSPECTOR)
149bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorBackend* value)
150{
151    ScriptScope scope(scriptState);
152    scope.global()->Set(v8::String::New(name), toV8(value));
153    return scope.success();
154}
155
156bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorFrontendHost* value)
157{
158    ScriptScope scope(scriptState);
159    scope.global()->Set(v8::String::New(name), toV8(value));
160    return scope.success();
161}
162
163bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InjectedScriptHost* value)
164{
165    ScriptScope scope(scriptState);
166    scope.global()->Set(v8::String::New(name), toV8(value));
167    return scope.success();
168}
169#endif
170
171bool ScriptGlobalObject::get(ScriptState* scriptState, const char* name, ScriptObject& value)
172{
173    ScriptScope scope(scriptState);
174    v8::Local<v8::Value> v8Value = scope.global()->Get(v8::String::New(name));
175    if (v8Value.IsEmpty())
176        return false;
177
178    if (!v8Value->IsObject())
179        return false;
180
181    value = ScriptObject(scriptState, v8::Handle<v8::Object>(v8::Object::Cast(*v8Value)));
182    return true;
183}
184
185bool ScriptGlobalObject::remove(ScriptState* scriptState, const char* name)
186{
187    ScriptScope scope(scriptState);
188    return scope.global()->Delete(v8::String::New(name));
189}
190
191} // namespace WebCore
192