2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "core/inspector/InjectedScript.h"
36 #include "bindings/core/v8/ScriptFunctionCall.h"
37 #include "core/inspector/InjectedScriptHost.h"
38 #include "platform/JSONValues.h"
39 #include "wtf/text/WTFString.h"
41 using blink::TypeBuilder::Array;
42 using blink::TypeBuilder::Debugger::CallFrame;
43 using blink::TypeBuilder::Debugger::CollectionEntry;
44 using blink::TypeBuilder::Debugger::FunctionDetails;
45 using blink::TypeBuilder::Runtime::PropertyDescriptor;
46 using blink::TypeBuilder::Runtime::InternalPropertyDescriptor;
47 using blink::TypeBuilder::Runtime::RemoteObject;
51 InjectedScript::InjectedScript()
52 : InjectedScriptBase("InjectedScript")
56 InjectedScript::InjectedScript(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
57 : InjectedScriptBase("InjectedScript", injectedScriptObject, accessCheck)
61 void InjectedScript::evaluate(ErrorString* errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>* exceptionDetails)
63 ScriptFunctionCall function(injectedScriptObject(), "evaluate");
64 function.appendArgument(expression);
65 function.appendArgument(objectGroup);
66 function.appendArgument(includeCommandLineAPI);
67 function.appendArgument(returnByValue);
68 function.appendArgument(generatePreview);
69 makeEvalCall(errorString, function, result, wasThrown, exceptionDetails);
72 void InjectedScript::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
74 ScriptFunctionCall function(injectedScriptObject(), "callFunctionOn");
75 function.appendArgument(objectId);
76 function.appendArgument(expression);
77 function.appendArgument(arguments);
78 function.appendArgument(returnByValue);
79 function.appendArgument(generatePreview);
80 makeEvalCall(errorString, function, result, wasThrown);
83 void InjectedScript::evaluateOnCallFrame(ErrorString* errorString, const ScriptValue& callFrames, const Vector<ScriptValue>& asyncCallStacks, const String& callFrameId, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>* exceptionDetails)
85 ScriptFunctionCall function(injectedScriptObject(), "evaluateOnCallFrame");
86 function.appendArgument(callFrames);
87 function.appendArgument(asyncCallStacks);
88 function.appendArgument(callFrameId);
89 function.appendArgument(expression);
90 function.appendArgument(objectGroup);
91 function.appendArgument(includeCommandLineAPI);
92 function.appendArgument(returnByValue);
93 function.appendArgument(generatePreview);
94 makeEvalCall(errorString, function, result, wasThrown, exceptionDetails);
97 void InjectedScript::restartFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, RefPtr<JSONObject>* result)
99 ScriptFunctionCall function(injectedScriptObject(), "restartFrame");
100 function.appendArgument(callFrames);
101 function.appendArgument(callFrameId);
102 RefPtr<JSONValue> resultValue;
103 makeCall(function, &resultValue);
105 if (resultValue->type() == JSONValue::TypeString) {
106 resultValue->asString(errorString);
109 if (resultValue->type() == JSONValue::TypeObject) {
110 *result = resultValue->asObject();
114 *errorString = "Internal error";
117 void InjectedScript::getStepInPositions(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, RefPtr<Array<TypeBuilder::Debugger::Location> >& positions)
119 ScriptFunctionCall function(injectedScriptObject(), "getStepInPositions");
120 function.appendArgument(callFrames);
121 function.appendArgument(callFrameId);
122 RefPtr<JSONValue> resultValue;
123 makeCall(function, &resultValue);
125 if (resultValue->type() == JSONValue::TypeString) {
126 resultValue->asString(errorString);
129 if (resultValue->type() == JSONValue::TypeArray) {
130 positions = Array<TypeBuilder::Debugger::Location>::runtimeCast(resultValue);
134 *errorString = "Internal error";
137 void InjectedScript::setVariableValue(ErrorString* errorString, const ScriptValue& callFrames, const String* callFrameIdOpt, const String* functionObjectIdOpt, int scopeNumber, const String& variableName, const String& newValueStr)
139 ScriptFunctionCall function(injectedScriptObject(), "setVariableValue");
140 if (callFrameIdOpt) {
141 function.appendArgument(callFrames);
142 function.appendArgument(*callFrameIdOpt);
144 function.appendArgument(false);
145 function.appendArgument(false);
147 if (functionObjectIdOpt)
148 function.appendArgument(*functionObjectIdOpt);
150 function.appendArgument(false);
151 function.appendArgument(scopeNumber);
152 function.appendArgument(variableName);
153 function.appendArgument(newValueStr);
154 RefPtr<JSONValue> resultValue;
155 makeCall(function, &resultValue);
157 *errorString = "Internal error";
160 if (resultValue->type() == JSONValue::TypeString) {
161 resultValue->asString(errorString);
167 void InjectedScript::getFunctionDetails(ErrorString* errorString, const String& functionId, RefPtr<FunctionDetails>* result)
169 ScriptFunctionCall function(injectedScriptObject(), "getFunctionDetails");
170 function.appendArgument(functionId);
171 RefPtr<JSONValue> resultValue;
172 makeCall(function, &resultValue);
173 if (!resultValue || resultValue->type() != JSONValue::TypeObject) {
174 if (!resultValue->asString(errorString))
175 *errorString = "Internal error";
178 *result = FunctionDetails::runtimeCast(resultValue);
181 void InjectedScript::getCollectionEntries(ErrorString* errorString, const String& objectId, RefPtr<Array<CollectionEntry> >* result)
183 ScriptFunctionCall function(injectedScriptObject(), "getCollectionEntries");
184 function.appendArgument(objectId);
185 RefPtr<JSONValue> resultValue;
186 makeCall(function, &resultValue);
187 if (!resultValue || resultValue->type() != JSONValue::TypeArray) {
188 if (!resultValue->asString(errorString))
189 *errorString = "Internal error";
192 *result = Array<CollectionEntry>::runtimeCast(resultValue);
195 void InjectedScript::getProperties(ErrorString* errorString, const String& objectId, bool ownProperties, bool accessorPropertiesOnly, RefPtr<Array<PropertyDescriptor> >* properties)
197 ScriptFunctionCall function(injectedScriptObject(), "getProperties");
198 function.appendArgument(objectId);
199 function.appendArgument(ownProperties);
200 function.appendArgument(accessorPropertiesOnly);
202 RefPtr<JSONValue> result;
203 makeCall(function, &result);
204 if (!result || result->type() != JSONValue::TypeArray) {
205 *errorString = "Internal error";
208 *properties = Array<PropertyDescriptor>::runtimeCast(result);
211 void InjectedScript::getInternalProperties(ErrorString* errorString, const String& objectId, RefPtr<Array<InternalPropertyDescriptor> >* properties)
213 ScriptFunctionCall function(injectedScriptObject(), "getInternalProperties");
214 function.appendArgument(objectId);
216 RefPtr<JSONValue> result;
217 makeCall(function, &result);
218 if (!result || result->type() != JSONValue::TypeArray) {
219 *errorString = "Internal error";
222 RefPtr<Array<InternalPropertyDescriptor> > array = Array<InternalPropertyDescriptor>::runtimeCast(result);
223 if (array->length() > 0)
227 Node* InjectedScript::nodeForObjectId(const String& objectId)
229 if (isEmpty() || !canAccessInspectedWindow())
232 ScriptFunctionCall function(injectedScriptObject(), "nodeForObjectId");
233 function.appendArgument(objectId);
235 bool hadException = false;
236 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
237 ASSERT(!hadException);
239 return InjectedScriptHost::scriptValueAsNode(scriptState(), resultValue);
242 void InjectedScript::releaseObject(const String& objectId)
244 ScriptFunctionCall function(injectedScriptObject(), "releaseObject");
245 function.appendArgument(objectId);
246 RefPtr<JSONValue> result;
247 makeCall(function, &result);
250 PassRefPtr<Array<CallFrame> > InjectedScript::wrapCallFrames(const ScriptValue& callFrames, int asyncOrdinal)
253 ScriptFunctionCall function(injectedScriptObject(), "wrapCallFrames");
254 function.appendArgument(callFrames);
255 function.appendArgument(asyncOrdinal);
256 bool hadException = false;
257 ScriptValue callFramesValue = callFunctionWithEvalEnabled(function, hadException);
258 ASSERT(!hadException);
259 RefPtr<JSONValue> result = callFramesValue.toJSONValue(scriptState());
260 if (result && result->type() == JSONValue::TypeArray)
261 return Array<CallFrame>::runtimeCast(result);
262 return Array<CallFrame>::create();
265 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapObject(const ScriptValue& value, const String& groupName, bool generatePreview) const
268 ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapObject");
269 wrapFunction.appendArgument(value);
270 wrapFunction.appendArgument(groupName);
271 wrapFunction.appendArgument(canAccessInspectedWindow());
272 wrapFunction.appendArgument(generatePreview);
273 bool hadException = false;
274 ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
277 RefPtr<JSONObject> rawResult = r.toJSONValue(scriptState())->asObject();
278 return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
281 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapTable(const ScriptValue& table, const ScriptValue& columns) const
284 ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapTable");
285 wrapFunction.appendArgument(canAccessInspectedWindow());
286 wrapFunction.appendArgument(table);
287 if (columns.isEmpty())
288 wrapFunction.appendArgument(false);
290 wrapFunction.appendArgument(columns);
291 bool hadException = false;
292 ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
295 RefPtr<JSONObject> rawResult = r.toJSONValue(scriptState())->asObject();
296 return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
299 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapNode(Node* node, const String& groupName)
301 return wrapObject(nodeAsScriptValue(node), groupName);
304 ScriptValue InjectedScript::findObjectById(const String& objectId) const
307 ScriptFunctionCall function(injectedScriptObject(), "findObjectById");
308 function.appendArgument(objectId);
310 bool hadException = false;
311 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
312 ASSERT(!hadException);
316 void InjectedScript::inspectNode(Node* node)
319 ScriptFunctionCall function(injectedScriptObject(), "inspectNode");
320 function.appendArgument(nodeAsScriptValue(node));
321 RefPtr<JSONValue> result;
322 makeCall(function, &result);
325 void InjectedScript::releaseObjectGroup(const String& objectGroup)
328 ScriptFunctionCall releaseFunction(injectedScriptObject(), "releaseObjectGroup");
329 releaseFunction.appendArgument(objectGroup);
330 bool hadException = false;
331 callFunctionWithEvalEnabled(releaseFunction, hadException);
332 ASSERT(!hadException);
335 ScriptValue InjectedScript::nodeAsScriptValue(Node* node)
337 return InjectedScriptHost::nodeAsScriptValue(scriptState(), node);