initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSInjectedScriptHostCustom.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4  * Copyright (C) 2010-2011 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *     * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "config.h"
34
35 #if ENABLE(INSPECTOR)
36
37 #include "JSInjectedScriptHost.h"
38
39 #if ENABLE(DATABASE)
40 #include "Database.h"
41 #include "JSDatabase.h"
42 #endif
43 #include "ExceptionCode.h"
44 #include "InjectedScriptHost.h"
45 #include "InspectorDebuggerAgent.h"
46 #include "InspectorValues.h"
47 #include "JSHTMLAllCollection.h"
48 #include "JSHTMLCollection.h"
49 #include "JSNode.h"
50 #include "JSNodeList.h"
51 #include "ScriptValue.h"
52 #if ENABLE(DOM_STORAGE)
53 #include "Storage.h"
54 #include "JSStorage.h"
55 #endif
56 #include <runtime/DateInstance.h>
57 #include <runtime/Error.h>
58 #include <runtime/JSArray.h>
59 #include <runtime/JSFunction.h>
60 #include <runtime/JSLock.h>
61 #include <runtime/RegExpObject.h>
62
63 using namespace JSC;
64
65 namespace WebCore {
66
67 Node* InjectedScriptHost::scriptValueAsNode(ScriptValue value)
68 {
69     if (!value.isObject() || value.isNull())
70         return 0;
71     return toNode(value.jsValue());
72 }
73
74 ScriptValue InjectedScriptHost::nodeAsScriptValue(ScriptState* state, Node* node)
75 {
76     JSLock lock(SilenceAssertionsOnly);
77     return ScriptValue(state->globalData(), toJS(state, deprecatedGlobalObjectForPrototype(state), node));
78 }
79
80 JSValue JSInjectedScriptHost::evaluate(ExecState* exec)
81 {
82     JSValue expression = exec->argument(0);
83     if (!expression.isString())
84         return throwError(exec, createError(exec, "String argument expected."));
85     JSGlobalObject* globalObject = exec->lexicalGlobalObject();
86     JSFunction* evalFunction = globalObject->evalFunction();
87     CallData callData;
88     CallType callType = evalFunction->getCallData(callData);
89     if (callType == CallTypeNone)
90         return jsUndefined();
91     MarkedArgumentBuffer args;
92     args.append(expression);
93
94     bool wasEvalEnabled = globalObject->evalEnabled();
95     globalObject->setEvalEnabled(true);
96     JSValue result = JSC::call(exec, evalFunction, callType, callData, exec->globalThisValue(), args);
97     globalObject->setEvalEnabled(wasEvalEnabled);
98
99     return result;
100 }
101
102 JSValue JSInjectedScriptHost::inspectedNode(ExecState* exec)
103 {
104     if (exec->argumentCount() < 1)
105         return jsUndefined();
106
107     Node* node = impl()->inspectedNode(exec->argument(0).toInt32(exec));
108     if (!node)
109         return jsUndefined();
110
111     JSLock lock(SilenceAssertionsOnly);
112     return toJS(exec, globalObject(), node);
113 }
114
115 JSValue JSInjectedScriptHost::internalConstructorName(ExecState* exec)
116 {
117     if (exec->argumentCount() < 1)
118         return jsUndefined();
119
120     UString result = exec->argument(0).toThisObject(exec)->className();
121     return jsString(exec, result);
122 }
123
124 JSValue JSInjectedScriptHost::isHTMLAllCollection(ExecState* exec)
125 {
126     if (exec->argumentCount() < 1)
127         return jsUndefined();
128
129     JSValue value = exec->argument(0);
130     return jsBoolean(value.inherits(&JSHTMLAllCollection::s_info));
131 }
132
133 JSValue JSInjectedScriptHost::type(ExecState* exec)
134 {
135     if (exec->argumentCount() < 1)
136         return jsUndefined();
137
138     JSValue value = exec->argument(0);
139     if (value.isString())
140         return jsString(exec, String("string"));
141     if (value.inherits(&JSArray::s_info))
142         return jsString(exec, String("array"));
143     if (value.isBoolean())
144         return jsString(exec, String("boolean"));
145     if (value.isNumber())
146         return jsString(exec, String("number"));
147     if (value.inherits(&DateInstance::s_info))
148         return jsString(exec, String("date"));
149     if (value.inherits(&RegExpObject::s_info))
150         return jsString(exec, String("regexp"));
151     if (value.inherits(&JSNode::s_info))
152         return jsString(exec, String("node"));
153     if (value.inherits(&JSNodeList::s_info))
154         return jsString(exec, String("array"));
155     if (value.inherits(&JSHTMLCollection::s_info))
156         return jsString(exec, String("array"));
157     return jsUndefined();
158 }
159
160 JSValue JSInjectedScriptHost::inspect(ExecState* exec)
161 {
162     if (exec->argumentCount() >= 2) {
163         ScriptValue object(exec->globalData(), exec->argument(0));
164         ScriptValue hints(exec->globalData(), exec->argument(1));
165         impl()->inspectImpl(object.toInspectorValue(exec), hints.toInspectorValue(exec));
166     }
167     return jsUndefined();
168 }
169
170 JSValue JSInjectedScriptHost::databaseId(ExecState* exec)
171 {
172     if (exec->argumentCount() < 1)
173         return jsUndefined();
174 #if ENABLE(DATABASE)
175     Database* database = toDatabase(exec->argument(0));
176     if (database)
177         return jsNumber(impl()->databaseIdImpl(database));
178 #endif
179     return jsUndefined();
180 }
181
182 JSValue JSInjectedScriptHost::storageId(ExecState* exec)
183 {
184     if (exec->argumentCount() < 1)
185         return jsUndefined();
186 #if ENABLE(DOM_STORAGE)
187     Storage* storage = toStorage(exec->argument(0));
188     if (storage)
189         return jsNumber(impl()->storageIdImpl(storage));
190 #endif
191     return jsUndefined();
192 }
193
194 } // namespace WebCore
195
196 #endif // ENABLE(INSPECTOR)