2 * Copyright (C) 2010, 2011 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.
32 #include "InjectedScript.h"
37 #include "InjectedScriptHost.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorValues.h"
41 #include "PlatformString.h"
42 #include "ScriptFunctionCall.h"
46 InjectedScript::InjectedScript()
47 : m_inspectedStateAccessCheck(0)
51 InjectedScript::InjectedScript(ScriptObject injectedScriptObject, InspectedStateAccessCheck accessCheck)
52 : m_injectedScriptObject(injectedScriptObject)
53 , m_inspectedStateAccessCheck(accessCheck)
57 void InjectedScript::evaluate(ErrorString* errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, RefPtr<InspectorObject>* result, bool* wasThrown)
59 ScriptFunctionCall function(m_injectedScriptObject, "evaluate");
60 function.appendArgument(expression);
61 function.appendArgument(objectGroup);
62 function.appendArgument(includeCommandLineAPI);
63 function.appendArgument(returnByValue);
64 makeEvalCall(errorString, function, result, wasThrown);
67 void InjectedScript::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, RefPtr<InspectorObject>* result, bool* wasThrown)
69 ScriptFunctionCall function(m_injectedScriptObject, "callFunctionOn");
70 function.appendArgument(objectId);
71 function.appendArgument(expression);
72 function.appendArgument(arguments);
73 function.appendArgument(returnByValue);
74 makeEvalCall(errorString, function, result, wasThrown);
77 void InjectedScript::evaluateOnCallFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, RefPtr<InspectorObject>* result, bool* wasThrown)
79 ScriptFunctionCall function(m_injectedScriptObject, "evaluateOnCallFrame");
80 function.appendArgument(callFrames);
81 function.appendArgument(callFrameId);
82 function.appendArgument(expression);
83 function.appendArgument(objectGroup);
84 function.appendArgument(includeCommandLineAPI);
85 function.appendArgument(returnByValue);
86 makeEvalCall(errorString, function, result, wasThrown);
89 void InjectedScript::getProperties(ErrorString* errorString, const String& objectId, bool ownProperties, RefPtr<InspectorArray>* properties)
91 ScriptFunctionCall function(m_injectedScriptObject, "getProperties");
92 function.appendArgument(objectId);
93 function.appendArgument(ownProperties);
95 RefPtr<InspectorValue> result;
96 makeCall(function, &result);
97 if (!result || result->type() != InspectorValue::TypeArray) {
98 *errorString = "Internal error";
101 *properties = result->asArray();
104 Node* InjectedScript::nodeForObjectId(const String& objectId)
106 if (hasNoValue() || !canAccessInspectedWindow())
109 ScriptFunctionCall function(m_injectedScriptObject, "nodeForObjectId");
110 function.appendArgument(objectId);
112 bool hadException = false;
113 ScriptValue resultValue = function.call(hadException);
114 ASSERT(!hadException);
116 return InjectedScriptHost::scriptValueAsNode(resultValue);
119 void InjectedScript::releaseObject(const String& objectId)
121 ScriptFunctionCall function(m_injectedScriptObject, "releaseObject");
122 function.appendArgument(objectId);
123 RefPtr<InspectorValue> result;
124 makeCall(function, &result);
127 #if ENABLE(JAVASCRIPT_DEBUGGER)
128 PassRefPtr<InspectorArray> InjectedScript::wrapCallFrames(const ScriptValue& callFrames)
130 ASSERT(!hasNoValue());
131 ScriptFunctionCall function(m_injectedScriptObject, "wrapCallFrames");
132 function.appendArgument(callFrames);
133 ScriptValue callFramesValue = function.call();
134 RefPtr<InspectorValue> result = callFramesValue.toInspectorValue(m_injectedScriptObject.scriptState());
135 if (result->type() == InspectorValue::TypeArray)
136 return result->asArray();
137 return InspectorArray::create();
141 PassRefPtr<InspectorObject> InjectedScript::wrapObject(ScriptValue value, const String& groupName)
143 ASSERT(!hasNoValue());
144 ScriptFunctionCall wrapFunction(m_injectedScriptObject, "wrapObject");
145 wrapFunction.appendArgument(value);
146 wrapFunction.appendArgument(groupName);
147 wrapFunction.appendArgument(canAccessInspectedWindow());
148 bool hadException = false;
149 ScriptValue r = wrapFunction.call(hadException);
151 RefPtr<InspectorObject> result = InspectorObject::create();
152 result->setString("description", "<exception>");
155 return r.toInspectorValue(m_injectedScriptObject.scriptState())->asObject();
158 PassRefPtr<InspectorObject> InjectedScript::wrapNode(Node* node, const String& groupName)
160 return wrapObject(nodeAsScriptValue(node), groupName);
163 void InjectedScript::inspectNode(Node* node)
165 ASSERT(!hasNoValue());
166 ScriptFunctionCall function(m_injectedScriptObject, "inspectNode");
167 function.appendArgument(nodeAsScriptValue(node));
168 RefPtr<InspectorValue> result;
169 makeCall(function, &result);
172 void InjectedScript::releaseObjectGroup(const String& objectGroup)
174 ASSERT(!hasNoValue());
175 ScriptFunctionCall releaseFunction(m_injectedScriptObject, "releaseObjectGroup");
176 releaseFunction.appendArgument(objectGroup);
177 releaseFunction.call();
180 bool InjectedScript::canAccessInspectedWindow()
182 return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
185 void InjectedScript::makeCall(ScriptFunctionCall& function, RefPtr<InspectorValue>* result)
187 if (hasNoValue() || !canAccessInspectedWindow()) {
188 *result = InspectorValue::null();
192 bool hadException = false;
193 ScriptValue resultValue = function.call(hadException);
195 ASSERT(!hadException);
197 *result = resultValue.toInspectorValue(m_injectedScriptObject.scriptState());
199 *result = InspectorString::create(String::format("Object has too long reference chain(must not be longer than %d)", InspectorValue::maxDepth));
201 *result = InspectorString::create("Exception while making a call.");
204 void InjectedScript::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<InspectorObject>* objectResult, bool* wasThrown)
206 RefPtr<InspectorValue> result;
207 makeCall(function, &result);
209 *errorString = "Internal error: result value is empty";
212 if (result->type() == InspectorValue::TypeString) {
213 result->asString(errorString);
216 RefPtr<InspectorObject> resultPair = result->asObject();
218 *errorString = "Internal error: result is not an Object";
221 RefPtr<InspectorObject> resultObj = resultPair->getObject("result");
222 bool wasThrownVal = false;
223 if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
224 *errorString = "Internal error: result is not a pair of value and wasThrown flag";
227 *objectResult = resultObj;
228 *wasThrown = wasThrownVal;
231 ScriptValue InjectedScript::nodeAsScriptValue(Node* node)
233 return InjectedScriptHost::nodeAsScriptValue(m_injectedScriptObject.scriptState(), node);
236 } // namespace WebCore
238 #endif // ENABLE(INSPECTOR)