|
1 /* |
|
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> |
|
4 * Copyright (C) 2010 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 #include "Console.h" |
|
39 #include "JSMainThreadExecState.h" |
|
40 #if ENABLE(DATABASE) |
|
41 #include "Database.h" |
|
42 #include "JSDatabase.h" |
|
43 #endif |
|
44 #include "ExceptionCode.h" |
|
45 #include "Frame.h" |
|
46 #include "FrameLoader.h" |
|
47 #include "InjectedScript.h" |
|
48 #include "InjectedScriptHost.h" |
|
49 #include "InspectorController.h" |
|
50 #include "InspectorResource.h" |
|
51 #include "JSDOMWindow.h" |
|
52 #include "JSDOMWindowCustom.h" |
|
53 #include "JSNode.h" |
|
54 #include "JSRange.h" |
|
55 #include "Node.h" |
|
56 #include "Page.h" |
|
57 #if ENABLE(DOM_STORAGE) |
|
58 #include "SerializedScriptValue.h" |
|
59 #include "Storage.h" |
|
60 #include "JSStorage.h" |
|
61 #endif |
|
62 #include "TextIterator.h" |
|
63 #include "VisiblePosition.h" |
|
64 #include <parser/SourceCode.h> |
|
65 #include <runtime/JSArray.h> |
|
66 #include <runtime/JSLock.h> |
|
67 #include <wtf/RefPtr.h> |
|
68 #include <wtf/Vector.h> |
|
69 |
|
70 #if ENABLE(JAVASCRIPT_DEBUGGER) |
|
71 #include "JavaScriptCallFrame.h" |
|
72 #include "JSJavaScriptCallFrame.h" |
|
73 #include "ScriptDebugServer.h" |
|
74 #endif |
|
75 |
|
76 using namespace JSC; |
|
77 |
|
78 namespace WebCore { |
|
79 |
|
80 ScriptObject InjectedScriptHost::createInjectedScript(const String& source, ScriptState* scriptState, long id) |
|
81 { |
|
82 SourceCode sourceCode = makeSource(stringToUString(source)); |
|
83 JSLock lock(SilenceAssertionsOnly); |
|
84 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject()); |
|
85 JSValue globalThisValue = scriptState->globalThisValue(); |
|
86 Completion comp = JSMainThreadExecState::evaluate(scriptState, globalObject->globalScopeChain(), sourceCode, globalThisValue); |
|
87 if (comp.complType() != JSC::Normal && comp.complType() != JSC::ReturnValue) |
|
88 return ScriptObject(); |
|
89 JSValue functionValue = comp.value(); |
|
90 CallData callData; |
|
91 CallType callType = getCallData(functionValue, callData); |
|
92 if (callType == CallTypeNone) |
|
93 return ScriptObject(); |
|
94 |
|
95 MarkedArgumentBuffer args; |
|
96 args.append(toJS(scriptState, globalObject, this)); |
|
97 args.append(globalThisValue); |
|
98 args.append(jsNumber(scriptState, id)); |
|
99 args.append(jsString(scriptState, String("JSC"))); |
|
100 JSValue result = JSC::call(scriptState, functionValue, callType, callData, globalThisValue, args); |
|
101 if (result.isObject()) |
|
102 return ScriptObject(scriptState, result.getObject()); |
|
103 return ScriptObject(); |
|
104 } |
|
105 |
|
106 #if ENABLE(DATABASE) |
|
107 JSValue JSInjectedScriptHost::databaseForId(ExecState* exec) |
|
108 { |
|
109 if (exec->argumentCount() < 1) |
|
110 return jsUndefined(); |
|
111 |
|
112 InspectorController* ic = impl()->inspectorController(); |
|
113 if (!ic) |
|
114 return jsUndefined(); |
|
115 |
|
116 Database* database = impl()->databaseForId(exec->argument(0).toInt32(exec)); |
|
117 if (!database) |
|
118 return jsUndefined(); |
|
119 return toJS(exec, database); |
|
120 } |
|
121 #endif |
|
122 |
|
123 #if ENABLE(JAVASCRIPT_DEBUGGER) |
|
124 JSValue JSInjectedScriptHost::currentCallFrame(ExecState* exec) |
|
125 { |
|
126 JavaScriptCallFrame* callFrame = ScriptDebugServer::shared().currentCallFrame(); |
|
127 if (!callFrame || !callFrame->isValid()) |
|
128 return jsUndefined(); |
|
129 |
|
130 JSLock lock(SilenceAssertionsOnly); |
|
131 return toJS(exec, callFrame); |
|
132 } |
|
133 #endif |
|
134 |
|
135 JSValue JSInjectedScriptHost::nodeForId(ExecState* exec) |
|
136 { |
|
137 if (exec->argumentCount() < 1) |
|
138 return jsUndefined(); |
|
139 |
|
140 Node* node = impl()->nodeForId(exec->argument(0).toInt32(exec)); |
|
141 if (!node) |
|
142 return jsUndefined(); |
|
143 |
|
144 InspectorController* ic = impl()->inspectorController(); |
|
145 if (!ic) |
|
146 return jsUndefined(); |
|
147 |
|
148 JSLock lock(SilenceAssertionsOnly); |
|
149 return toJS(exec, node); |
|
150 } |
|
151 |
|
152 JSValue JSInjectedScriptHost::pushNodePathToFrontend(ExecState* exec) |
|
153 { |
|
154 if (exec->argumentCount() < 3) |
|
155 return jsUndefined(); |
|
156 |
|
157 Node* node = toNode(exec->argument(0)); |
|
158 if (!node) |
|
159 return jsUndefined(); |
|
160 |
|
161 bool withChildren = exec->argument(1).toBoolean(exec); |
|
162 bool selectInUI = exec->argument(2).toBoolean(exec); |
|
163 return jsNumber(exec, impl()->pushNodePathToFrontend(node, withChildren, selectInUI)); |
|
164 } |
|
165 |
|
166 #if ENABLE(DATABASE) |
|
167 JSValue JSInjectedScriptHost::selectDatabase(ExecState* exec) |
|
168 { |
|
169 if (exec->argumentCount() < 1) |
|
170 return jsUndefined(); |
|
171 |
|
172 Database* database = toDatabase(exec->argument(0)); |
|
173 if (database) |
|
174 impl()->selectDatabase(database); |
|
175 return jsUndefined(); |
|
176 } |
|
177 #endif |
|
178 |
|
179 #if ENABLE(DOM_STORAGE) |
|
180 JSValue JSInjectedScriptHost::selectDOMStorage(ExecState* exec) |
|
181 { |
|
182 if (exec->argumentCount() < 1) |
|
183 return jsUndefined(); |
|
184 InspectorController* ic = impl()->inspectorController(); |
|
185 if (!ic) |
|
186 return jsUndefined(); |
|
187 |
|
188 Storage* storage = toStorage(exec->argument(0)); |
|
189 if (storage) |
|
190 impl()->selectDOMStorage(storage); |
|
191 return jsUndefined(); |
|
192 } |
|
193 #endif |
|
194 |
|
195 JSValue JSInjectedScriptHost::reportDidDispatchOnInjectedScript(ExecState* exec) |
|
196 { |
|
197 if (exec->argumentCount() < 3) |
|
198 return jsUndefined(); |
|
199 |
|
200 if (!exec->argument(0).isInt32()) |
|
201 return jsUndefined(); |
|
202 int callId = exec->argument(0).asInt32(); |
|
203 |
|
204 RefPtr<SerializedScriptValue> result(SerializedScriptValue::create(exec, exec->argument(1))); |
|
205 |
|
206 bool isException; |
|
207 if (!exec->argument(2).getBoolean(isException)) |
|
208 return jsUndefined(); |
|
209 impl()->reportDidDispatchOnInjectedScript(callId, result.get(), isException); |
|
210 return jsUndefined(); |
|
211 } |
|
212 |
|
213 InjectedScript InjectedScriptHost::injectedScriptFor(ScriptState* scriptState) |
|
214 { |
|
215 JSLock lock(SilenceAssertionsOnly); |
|
216 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject()); |
|
217 JSObject* injectedScript = globalObject->injectedScript(); |
|
218 if (injectedScript) |
|
219 return InjectedScript(ScriptObject(scriptState, injectedScript)); |
|
220 |
|
221 ASSERT(!m_injectedScriptSource.isEmpty()); |
|
222 pair<long, ScriptObject> injectedScriptObject = injectScript(m_injectedScriptSource, scriptState); |
|
223 globalObject->setInjectedScript(injectedScriptObject.second.jsObject()); |
|
224 InjectedScript result(injectedScriptObject.second); |
|
225 m_idToInjectedScript.set(injectedScriptObject.first, result); |
|
226 return result; |
|
227 } |
|
228 |
|
229 bool InjectedScriptHost::canAccessInspectedWindow(ScriptState* scriptState) |
|
230 { |
|
231 JSLock lock(SilenceAssertionsOnly); |
|
232 JSDOMWindow* inspectedWindow = toJSDOMWindow(scriptState->lexicalGlobalObject()); |
|
233 if (!inspectedWindow) |
|
234 return false; |
|
235 return inspectedWindow->allowsAccessFromNoErrorMessage(scriptState); |
|
236 } |
|
237 |
|
238 } // namespace WebCore |
|
239 |
|
240 #endif // ENABLE(INSPECTOR) |