2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "InjectedScript.h"
37 #include "InspectorValues.h"
38 #include "JSCInlines.h"
39 #include "ScriptFunctionCall.h"
40 #include "ScriptObject.h"
41 #include <wtf/text/WTFString.h>
43 using Inspector::TypeBuilder::Array
;
47 InjectedScript::InjectedScript()
48 : InjectedScriptBase(ASCIILiteral("InjectedScript"))
52 InjectedScript::InjectedScript(Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
)
53 : InjectedScriptBase(ASCIILiteral("InjectedScript"), injectedScriptObject
, environment
)
57 InjectedScript::~InjectedScript()
61 void InjectedScript::evaluate(ErrorString
* errorString
, const String
& expression
, const String
& objectGroup
, bool includeCommandLineAPI
, bool returnByValue
, bool generatePreview
, RefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
>* result
, Inspector::TypeBuilder::OptOutput
<bool>* wasThrown
)
63 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("evaluate"), inspectorEnvironment()->functionCallHandler());
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
);
72 void InjectedScript::callFunctionOn(ErrorString
* errorString
, const String
& objectId
, const String
& expression
, const String
& arguments
, bool returnByValue
, bool generatePreview
, RefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
>* result
, Inspector::TypeBuilder::OptOutput
<bool>* wasThrown
)
74 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("callFunctionOn"), inspectorEnvironment()->functionCallHandler());
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 Deprecated::ScriptValue
& callFrames
, const String
& callFrameId
, const String
& expression
, const String
& objectGroup
, bool includeCommandLineAPI
, bool returnByValue
, bool generatePreview
, RefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
>* result
, Inspector::TypeBuilder::OptOutput
<bool>* wasThrown
)
85 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("evaluateOnCallFrame"), inspectorEnvironment()->functionCallHandler());
86 function
.appendArgument(callFrames
);
87 function
.appendArgument(callFrameId
);
88 function
.appendArgument(expression
);
89 function
.appendArgument(objectGroup
);
90 function
.appendArgument(includeCommandLineAPI
);
91 function
.appendArgument(returnByValue
);
92 function
.appendArgument(generatePreview
);
93 makeEvalCall(errorString
, function
, result
, wasThrown
);
96 void InjectedScript::getFunctionDetails(ErrorString
* errorString
, const String
& functionId
, RefPtr
<Inspector::TypeBuilder::Debugger::FunctionDetails
>* result
)
98 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("getFunctionDetails"), inspectorEnvironment()->functionCallHandler());
99 function
.appendArgument(functionId
);
101 RefPtr
<InspectorValue
> resultValue
;
102 makeCall(function
, &resultValue
);
103 if (!resultValue
|| resultValue
->type() != InspectorValue::TypeObject
) {
104 if (!resultValue
->asString(errorString
))
105 *errorString
= ASCIILiteral("Internal error");
109 *result
= Inspector::TypeBuilder::Debugger::FunctionDetails::runtimeCast(resultValue
);
112 void InjectedScript::getProperties(ErrorString
* errorString
, const String
& objectId
, bool ownProperties
, bool ownAndGetterProperties
, RefPtr
<Array
<Inspector::TypeBuilder::Runtime::PropertyDescriptor
>>* properties
)
114 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("getProperties"), inspectorEnvironment()->functionCallHandler());
115 function
.appendArgument(objectId
);
116 function
.appendArgument(ownProperties
);
117 function
.appendArgument(ownAndGetterProperties
);
119 RefPtr
<InspectorValue
> result
;
120 makeCall(function
, &result
);
121 if (!result
|| result
->type() != InspectorValue::TypeArray
) {
122 *errorString
= ASCIILiteral("Internal error");
126 *properties
= Array
<Inspector::TypeBuilder::Runtime::PropertyDescriptor
>::runtimeCast(result
);
129 void InjectedScript::getInternalProperties(ErrorString
* errorString
, const String
& objectId
, RefPtr
<Array
<Inspector::TypeBuilder::Runtime::InternalPropertyDescriptor
>>* properties
)
131 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("getInternalProperties"), inspectorEnvironment()->functionCallHandler());
132 function
.appendArgument(objectId
);
134 RefPtr
<InspectorValue
> result
;
135 makeCall(function
, &result
);
136 if (!result
|| result
->type() != InspectorValue::TypeArray
) {
137 *errorString
= ASCIILiteral("Internal error");
141 RefPtr
<Array
<Inspector::TypeBuilder::Runtime::InternalPropertyDescriptor
>> array
= Array
<Inspector::TypeBuilder::Runtime::InternalPropertyDescriptor
>::runtimeCast(result
);
142 if (array
->length() > 0)
146 PassRefPtr
<Array
<Inspector::TypeBuilder::Debugger::CallFrame
>> InjectedScript::wrapCallFrames(const Deprecated::ScriptValue
& callFrames
)
148 ASSERT(!hasNoValue());
149 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("wrapCallFrames"), inspectorEnvironment()->functionCallHandler());
150 function
.appendArgument(callFrames
);
152 bool hadException
= false;
153 Deprecated::ScriptValue callFramesValue
= callFunctionWithEvalEnabled(function
, hadException
);
154 ASSERT(!hadException
);
155 RefPtr
<InspectorValue
> result
= callFramesValue
.toInspectorValue(scriptState());
156 if (result
->type() == InspectorValue::TypeArray
)
157 return Array
<Inspector::TypeBuilder::Debugger::CallFrame
>::runtimeCast(result
);
159 return Array
<Inspector::TypeBuilder::Debugger::CallFrame
>::create();
162 PassRefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
> InjectedScript::wrapObject(const Deprecated::ScriptValue
& value
, const String
& groupName
, bool generatePreview
) const
164 ASSERT(!hasNoValue());
165 Deprecated::ScriptFunctionCall
wrapFunction(injectedScriptObject(), ASCIILiteral("wrapObject"), inspectorEnvironment()->functionCallHandler());
166 wrapFunction
.appendArgument(value
);
167 wrapFunction
.appendArgument(groupName
);
168 wrapFunction
.appendArgument(hasAccessToInspectedScriptState());
169 wrapFunction
.appendArgument(generatePreview
);
171 bool hadException
= false;
172 Deprecated::ScriptValue r
= callFunctionWithEvalEnabled(wrapFunction
, hadException
);
176 RefPtr
<InspectorObject
> rawResult
= r
.toInspectorValue(scriptState())->asObject();
177 return Inspector::TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult
);
180 PassRefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
> InjectedScript::wrapTable(const Deprecated::ScriptValue
& table
, const Deprecated::ScriptValue
& columns
) const
182 ASSERT(!hasNoValue());
183 Deprecated::ScriptFunctionCall
wrapFunction(injectedScriptObject(), ASCIILiteral("wrapTable"), inspectorEnvironment()->functionCallHandler());
184 wrapFunction
.appendArgument(hasAccessToInspectedScriptState());
185 wrapFunction
.appendArgument(table
);
186 if (columns
.hasNoValue())
187 wrapFunction
.appendArgument(false);
189 wrapFunction
.appendArgument(columns
);
191 bool hadException
= false;
192 Deprecated::ScriptValue r
= callFunctionWithEvalEnabled(wrapFunction
, hadException
);
196 RefPtr
<InspectorObject
> rawResult
= r
.toInspectorValue(scriptState())->asObject();
197 return Inspector::TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult
);
200 Deprecated::ScriptValue
InjectedScript::findObjectById(const String
& objectId
) const
202 ASSERT(!hasNoValue());
203 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("findObjectById"), inspectorEnvironment()->functionCallHandler());
204 function
.appendArgument(objectId
);
206 bool hadException
= false;
207 Deprecated::ScriptValue resultValue
= callFunctionWithEvalEnabled(function
, hadException
);
208 ASSERT(!hadException
);
213 void InjectedScript::inspectObject(Deprecated::ScriptValue value
)
215 ASSERT(!hasNoValue());
216 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("inspectObject"), inspectorEnvironment()->functionCallHandler());
217 function
.appendArgument(value
);
218 RefPtr
<InspectorValue
> result
;
219 makeCall(function
, &result
);
222 void InjectedScript::releaseObject(const String
& objectId
)
224 Deprecated::ScriptFunctionCall
function(injectedScriptObject(), ASCIILiteral("releaseObject"), inspectorEnvironment()->functionCallHandler());
225 function
.appendArgument(objectId
);
226 RefPtr
<InspectorValue
> result
;
227 makeCall(function
, &result
);
230 void InjectedScript::releaseObjectGroup(const String
& objectGroup
)
232 ASSERT(!hasNoValue());
233 Deprecated::ScriptFunctionCall
releaseFunction(injectedScriptObject(), ASCIILiteral("releaseObjectGroup"), inspectorEnvironment()->functionCallHandler());
234 releaseFunction
.appendArgument(objectGroup
);
236 bool hadException
= false;
237 callFunctionWithEvalEnabled(releaseFunction
, hadException
);
238 ASSERT(!hadException
);
241 } // namespace Inspector
243 #endif // ENABLE(INSPECTOR)