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 #include "JSInjectedScriptHost.h"
35
36 #if ENABLE(INSPECTOR)
37
38 #if ENABLE(DATABASE)
39 #include "Database.h"
40 #include "JSDatabase.h"
41 #endif
42 #include "ExceptionCode.h"
43 #include "InjectedScriptHost.h"
44 #include "InspectorDebuggerAgent.h"
45 #include "InspectorValues.h"
46 #include "JSHTMLAllCollection.h"
47 #include "JSHTMLCollection.h"
48 #include "JSNode.h"
49 #include "JSNodeList.h"
50 #include "ScriptValue.h"
51 #if ENABLE(DOM_STORAGE)
52 #include "Storage.h"
53 #include "JSStorage.h"
54 #endif
55 #include <runtime/DateInstance.h>
56 #include <runtime/JSArray.h>
57 #include <runtime/JSLock.h>
58 #include <runtime/RegExpObject.h>
59
60 using namespace JSC;
61
62 namespace WebCore {
63
scriptValueAsNode(ScriptValue value)64 Node* InjectedScriptHost::scriptValueAsNode(ScriptValue value)
65 {
66 if (!value.isObject() || value.isNull())
67 return 0;
68 return toNode(value.jsValue());
69 }
70
nodeAsScriptValue(ScriptState * state,Node * node)71 ScriptValue InjectedScriptHost::nodeAsScriptValue(ScriptState* state, Node* node)
72 {
73 JSLock lock(SilenceAssertionsOnly);
74 return ScriptValue(state->globalData(), toJS(state, deprecatedGlobalObjectForPrototype(state), node));
75 }
76
inspectedNode(ExecState * exec)77 JSValue JSInjectedScriptHost::inspectedNode(ExecState* exec)
78 {
79 if (exec->argumentCount() < 1)
80 return jsUndefined();
81
82 Node* node = impl()->inspectedNode(exec->argument(0).toInt32(exec));
83 if (!node)
84 return jsUndefined();
85
86 JSLock lock(SilenceAssertionsOnly);
87 return toJS(exec, globalObject(), node);
88 }
89
internalConstructorName(ExecState * exec)90 JSValue JSInjectedScriptHost::internalConstructorName(ExecState* exec)
91 {
92 if (exec->argumentCount() < 1)
93 return jsUndefined();
94
95 UString result = exec->argument(0).toThisObject(exec)->className();
96 return jsString(exec, result);
97 }
98
isHTMLAllCollection(ExecState * exec)99 JSValue JSInjectedScriptHost::isHTMLAllCollection(ExecState* exec)
100 {
101 if (exec->argumentCount() < 1)
102 return jsUndefined();
103
104 JSValue value = exec->argument(0);
105 return jsBoolean(value.inherits(&JSHTMLAllCollection::s_info));
106 }
107
type(ExecState * exec)108 JSValue JSInjectedScriptHost::type(ExecState* exec)
109 {
110 if (exec->argumentCount() < 1)
111 return jsUndefined();
112
113 JSValue value = exec->argument(0);
114 if (value.isString())
115 return jsString(exec, String("string"));
116 if (value.inherits(&JSArray::s_info))
117 return jsString(exec, String("array"));
118 if (value.isBoolean())
119 return jsString(exec, String("boolean"));
120 if (value.isNumber())
121 return jsString(exec, String("number"));
122 if (value.inherits(&DateInstance::s_info))
123 return jsString(exec, String("date"));
124 if (value.inherits(&RegExpObject::s_info))
125 return jsString(exec, String("regexp"));
126 if (value.inherits(&JSNode::s_info))
127 return jsString(exec, String("node"));
128 if (value.inherits(&JSNodeList::s_info))
129 return jsString(exec, String("array"));
130 if (value.inherits(&JSHTMLCollection::s_info))
131 return jsString(exec, String("array"));
132 return jsUndefined();
133 }
134
inspect(ExecState * exec)135 JSValue JSInjectedScriptHost::inspect(ExecState* exec)
136 {
137 if (exec->argumentCount() >= 2) {
138 ScriptValue object(exec->globalData(), exec->argument(0));
139 ScriptValue hints(exec->globalData(), exec->argument(1));
140 impl()->inspectImpl(object.toInspectorValue(exec), hints.toInspectorValue(exec));
141 }
142 return jsUndefined();
143 }
144
databaseId(ExecState * exec)145 JSValue JSInjectedScriptHost::databaseId(ExecState* exec)
146 {
147 if (exec->argumentCount() < 1)
148 return jsUndefined();
149 #if ENABLE(DATABASE)
150 Database* database = toDatabase(exec->argument(0));
151 if (database)
152 return jsNumber(impl()->databaseIdImpl(database));
153 #endif
154 return jsUndefined();
155 }
156
storageId(ExecState * exec)157 JSValue JSInjectedScriptHost::storageId(ExecState* exec)
158 {
159 if (exec->argumentCount() < 1)
160 return jsUndefined();
161 #if ENABLE(DOM_STORAGE)
162 Storage* storage = toStorage(exec->argument(0));
163 if (storage)
164 return jsNumber(impl()->storageIdImpl(storage));
165 #endif
166 return jsUndefined();
167 }
168
169 } // namespace WebCore
170
171 #endif // ENABLE(INSPECTOR)
172