1 /*
2 * Copyright (C) 2010, 2011 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 "InjectedScript.h"
33
34 #if ENABLE(INSPECTOR)
35
36 #include "Frame.h"
37 #include "InjectedScriptHost.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorValues.h"
40 #include "Node.h"
41 #include "PlatformString.h"
42 #include "ScriptFunctionCall.h"
43
44 namespace WebCore {
45
InjectedScript()46 InjectedScript::InjectedScript()
47 : m_inspectedStateAccessCheck(0)
48 {
49 }
50
InjectedScript(ScriptObject injectedScriptObject,InspectedStateAccessCheck accessCheck)51 InjectedScript::InjectedScript(ScriptObject injectedScriptObject, InspectedStateAccessCheck accessCheck)
52 : m_injectedScriptObject(injectedScriptObject)
53 , m_inspectedStateAccessCheck(accessCheck)
54 {
55 }
56
evaluate(ErrorString * errorString,const String & expression,const String & objectGroup,bool includeCommandLineAPI,RefPtr<InspectorObject> * result,bool * wasThrown)57 void InjectedScript::evaluate(ErrorString* errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, RefPtr<InspectorObject>* result, bool* wasThrown)
58 {
59 ScriptFunctionCall function(m_injectedScriptObject, "evaluate");
60 function.appendArgument(expression);
61 function.appendArgument(objectGroup);
62 function.appendArgument(includeCommandLineAPI);
63 makeEvalCall(errorString, function, result, wasThrown);
64 }
65
evaluateOn(ErrorString * errorString,const String & objectId,const String & expression,RefPtr<InspectorObject> * result,bool * wasThrown)66 void InjectedScript::evaluateOn(ErrorString* errorString, const String& objectId, const String& expression, RefPtr<InspectorObject>* result, bool* wasThrown)
67 {
68 ScriptFunctionCall function(m_injectedScriptObject, "evaluateOn");
69 function.appendArgument(objectId);
70 function.appendArgument(expression);
71 makeEvalCall(errorString, function, result, wasThrown);
72 }
73
evaluateOnCallFrame(ErrorString * errorString,const ScriptValue & callFrames,const String & callFrameId,const String & expression,const String & objectGroup,bool includeCommandLineAPI,RefPtr<InspectorObject> * result,bool * wasThrown)74 void InjectedScript::evaluateOnCallFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, const String& expression, const String& objectGroup, bool includeCommandLineAPI, RefPtr<InspectorObject>* result, bool* wasThrown)
75 {
76 ScriptFunctionCall function(m_injectedScriptObject, "evaluateOnCallFrame");
77 function.appendArgument(callFrames);
78 function.appendArgument(callFrameId);
79 function.appendArgument(expression);
80 function.appendArgument(objectGroup);
81 function.appendArgument(includeCommandLineAPI);
82 makeEvalCall(errorString, function, result, wasThrown);
83 }
84
getProperties(ErrorString * errorString,const String & objectId,bool ignoreHasOwnProperty,RefPtr<InspectorArray> * properties)85 void InjectedScript::getProperties(ErrorString* errorString, const String& objectId, bool ignoreHasOwnProperty, RefPtr<InspectorArray>* properties)
86 {
87 ScriptFunctionCall function(m_injectedScriptObject, "getProperties");
88 function.appendArgument(objectId);
89 function.appendArgument(ignoreHasOwnProperty);
90
91 RefPtr<InspectorValue> result;
92 makeCall(function, &result);
93 if (!result || result->type() != InspectorValue::TypeArray) {
94 *errorString = "Internal error";
95 return;
96 }
97 *properties = result->asArray();
98 }
99
nodeForObjectId(const String & objectId)100 Node* InjectedScript::nodeForObjectId(const String& objectId)
101 {
102 if (hasNoValue() || !canAccessInspectedWindow())
103 return 0;
104
105 ScriptFunctionCall function(m_injectedScriptObject, "nodeForObjectId");
106 function.appendArgument(objectId);
107
108 bool hadException = false;
109 ScriptValue resultValue = function.call(hadException);
110 ASSERT(!hadException);
111
112 return InjectedScriptHost::scriptValueAsNode(resultValue);
113 }
114
setPropertyValue(ErrorString * errorString,const String & objectId,const String & propertyName,const String & expression)115 void InjectedScript::setPropertyValue(ErrorString* errorString, const String& objectId, const String& propertyName, const String& expression)
116 {
117 ScriptFunctionCall function(m_injectedScriptObject, "setPropertyValue");
118 function.appendArgument(objectId);
119 function.appendArgument(propertyName);
120 function.appendArgument(expression);
121 RefPtr<InspectorValue> result;
122 makeCall(function, &result);
123 result->asString(errorString);
124 }
125
releaseObject(const String & objectId)126 void InjectedScript::releaseObject(const String& objectId)
127 {
128 ScriptFunctionCall function(m_injectedScriptObject, "releaseObject");
129 function.appendArgument(objectId);
130 RefPtr<InspectorValue> result;
131 makeCall(function, &result);
132 }
133
134 #if ENABLE(JAVASCRIPT_DEBUGGER)
wrapCallFrames(const ScriptValue & callFrames)135 PassRefPtr<InspectorArray> InjectedScript::wrapCallFrames(const ScriptValue& callFrames)
136 {
137 ASSERT(!hasNoValue());
138 ScriptFunctionCall function(m_injectedScriptObject, "wrapCallFrames");
139 function.appendArgument(callFrames);
140 ScriptValue callFramesValue = function.call();
141 RefPtr<InspectorValue> result = callFramesValue.toInspectorValue(m_injectedScriptObject.scriptState());
142 if (result->type() == InspectorValue::TypeArray)
143 return result->asArray();
144 return InspectorArray::create();
145 }
146 #endif
147
wrapObject(ScriptValue value,const String & groupName)148 PassRefPtr<InspectorObject> InjectedScript::wrapObject(ScriptValue value, const String& groupName)
149 {
150 ASSERT(!hasNoValue());
151 ScriptFunctionCall wrapFunction(m_injectedScriptObject, "wrapObject");
152 wrapFunction.appendArgument(value);
153 wrapFunction.appendArgument(groupName);
154 wrapFunction.appendArgument(canAccessInspectedWindow());
155 bool hadException = false;
156 ScriptValue r = wrapFunction.call(hadException);
157 if (hadException) {
158 RefPtr<InspectorObject> result = InspectorObject::create();
159 result->setString("description", "<exception>");
160 return result;
161 }
162 return r.toInspectorValue(m_injectedScriptObject.scriptState())->asObject();
163 }
164
wrapNode(Node * node)165 PassRefPtr<InspectorObject> InjectedScript::wrapNode(Node* node)
166 {
167 return wrapObject(nodeAsScriptValue(node), "");
168 }
169
inspectNode(Node * node)170 void InjectedScript::inspectNode(Node* node)
171 {
172 ASSERT(!hasNoValue());
173 ScriptFunctionCall function(m_injectedScriptObject, "inspectNode");
174 function.appendArgument(nodeAsScriptValue(node));
175 RefPtr<InspectorValue> result;
176 makeCall(function, &result);
177 }
178
releaseObjectGroup(const String & objectGroup)179 void InjectedScript::releaseObjectGroup(const String& objectGroup)
180 {
181 ASSERT(!hasNoValue());
182 ScriptFunctionCall releaseFunction(m_injectedScriptObject, "releaseObjectGroup");
183 releaseFunction.appendArgument(objectGroup);
184 releaseFunction.call();
185 }
186
canAccessInspectedWindow()187 bool InjectedScript::canAccessInspectedWindow()
188 {
189 return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
190 }
191
makeCall(ScriptFunctionCall & function,RefPtr<InspectorValue> * result)192 void InjectedScript::makeCall(ScriptFunctionCall& function, RefPtr<InspectorValue>* result)
193 {
194 if (hasNoValue() || !canAccessInspectedWindow()) {
195 *result = InspectorValue::null();
196 return;
197 }
198
199 bool hadException = false;
200 ScriptValue resultValue = function.call(hadException);
201
202 ASSERT(!hadException);
203 if (!hadException)
204 *result = resultValue.toInspectorValue(m_injectedScriptObject.scriptState());
205 else
206 *result = InspectorString::create("Exception while making a call.");
207 }
208
makeEvalCall(ErrorString * errorString,ScriptFunctionCall & function,RefPtr<InspectorObject> * objectResult,bool * wasThrown)209 void InjectedScript::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<InspectorObject>* objectResult, bool* wasThrown)
210 {
211 RefPtr<InspectorValue> result;
212 makeCall(function, &result);
213 if (!result) {
214 *errorString = "Internal error: result value is empty";
215 return;
216 }
217 if (result->type() == InspectorValue::TypeString) {
218 result->asString(errorString);
219 return;
220 }
221 RefPtr<InspectorObject> resultPair = result->asObject();
222 if (!resultPair) {
223 *errorString = "Internal error: result is not an Object";
224 return;
225 }
226 RefPtr<InspectorObject> resultObj = resultPair->getObject("result");
227 bool wasThrownVal = false;
228 if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
229 *errorString = "Internal error: result is not a pair of value and wasThrown flag";
230 return;
231 }
232 *objectResult = resultObj;
233 *wasThrown = wasThrownVal;
234 }
235
nodeAsScriptValue(Node * node)236 ScriptValue InjectedScript::nodeAsScriptValue(Node* node)
237 {
238 return InjectedScriptHost::nodeAsScriptValue(m_injectedScriptObject.scriptState(), node);
239 }
240
241 } // namespace WebCore
242
243 #endif // ENABLE(INSPECTOR)
244