61ddc0cb054520d9b2e46dc78b56b165974d1718
[WebKit-https.git] / Source / JavaScriptCore / inspector / InjectedScript.cpp
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
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
14  * distribution.
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.
18  *
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.
30  */
31
32 #include "config.h"
33 #include "InjectedScript.h"
34
35 #if ENABLE(INSPECTOR)
36
37 #include "InspectorValues.h"
38 #include "JSCInlines.h"
39 #include "ScriptFunctionCall.h"
40 #include "ScriptObject.h"
41 #include <wtf/text/WTFString.h>
42
43 using Inspector::Protocol::Array;
44
45 namespace Inspector {
46
47 InjectedScript::InjectedScript()
48     : InjectedScriptBase(ASCIILiteral("InjectedScript"))
49 {
50 }
51
52 InjectedScript::InjectedScript(Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
53     : InjectedScriptBase(ASCIILiteral("InjectedScript"), injectedScriptObject, environment)
54 {
55 }
56
57 InjectedScript::~InjectedScript()
58 {
59 }
60
61 void InjectedScript::evaluate(ErrorString& errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<Inspector::Protocol::Runtime::RemoteObject>* result, Inspector::Protocol::OptOutput<bool>* wasThrown)
62 {
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);
70 }
71
72 void InjectedScript::callFunctionOn(ErrorString& errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, bool generatePreview, RefPtr<Inspector::Protocol::Runtime::RemoteObject>* result, Inspector::Protocol::OptOutput<bool>* wasThrown)
73 {
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);
81 }
82
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::Protocol::Runtime::RemoteObject>* result, Inspector::Protocol::OptOutput<bool>* wasThrown)
84 {
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);
94 }
95
96 void InjectedScript::getFunctionDetails(ErrorString& errorString, const String& functionId, RefPtr<Inspector::Protocol::Debugger::FunctionDetails>* result)
97 {
98     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("getFunctionDetails"), inspectorEnvironment()->functionCallHandler());
99     function.appendArgument(functionId);
100
101     RefPtr<InspectorValue> resultValue;
102     makeCall(function, &resultValue);
103     if (!resultValue || resultValue->type() != InspectorValue::Type::Object) {
104         if (!resultValue->asString(errorString))
105             errorString = ASCIILiteral("Internal error");
106         return;
107     }
108
109     *result = BindingTraits<Inspector::Protocol::Debugger::FunctionDetails>::runtimeCast(resultValue);
110 }
111
112 void InjectedScript::getProperties(ErrorString& errorString, const String& objectId, bool ownProperties, RefPtr<Array<Inspector::Protocol::Runtime::PropertyDescriptor>>* properties)
113 {
114     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("getProperties"), inspectorEnvironment()->functionCallHandler());
115     function.appendArgument(objectId);
116     function.appendArgument(ownProperties);
117
118     RefPtr<InspectorValue> result;
119     makeCall(function, &result);
120     if (!result || result->type() != InspectorValue::Type::Array) {
121         errorString = ASCIILiteral("Internal error");
122         return;
123     }
124
125     *properties = BindingTraits<Array<Inspector::Protocol::Runtime::PropertyDescriptor>>::runtimeCast(result);
126 }
127
128 void InjectedScript::getInternalProperties(ErrorString& errorString, const String& objectId, RefPtr<Array<Inspector::Protocol::Runtime::InternalPropertyDescriptor>>* properties)
129 {
130     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("getInternalProperties"), inspectorEnvironment()->functionCallHandler());
131     function.appendArgument(objectId);
132
133     RefPtr<InspectorValue> result;
134     makeCall(function, &result);
135     if (!result || result->type() != InspectorValue::Type::Array) {
136         errorString = ASCIILiteral("Internal error");
137         return;
138     }
139
140     auto array = BindingTraits<Array<Inspector::Protocol::Runtime::InternalPropertyDescriptor>>::runtimeCast(result);
141     if (array->length() > 0)
142         *properties = array;
143 }
144
145 PassRefPtr<Array<Inspector::Protocol::Debugger::CallFrame>> InjectedScript::wrapCallFrames(const Deprecated::ScriptValue& callFrames)
146 {
147     ASSERT(!hasNoValue());
148     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("wrapCallFrames"), inspectorEnvironment()->functionCallHandler());
149     function.appendArgument(callFrames);
150
151     bool hadException = false;
152     Deprecated::ScriptValue callFramesValue = callFunctionWithEvalEnabled(function, hadException);
153     ASSERT(!hadException);
154     RefPtr<InspectorValue> result = callFramesValue.toInspectorValue(scriptState());
155     if (result->type() == InspectorValue::Type::Array)
156         return BindingTraits<Array<Inspector::Protocol::Debugger::CallFrame>>::runtimeCast(result);
157
158     return Array<Inspector::Protocol::Debugger::CallFrame>::create();
159 }
160
161 PassRefPtr<Inspector::Protocol::Runtime::RemoteObject> InjectedScript::wrapObject(const Deprecated::ScriptValue& value, const String& groupName, bool generatePreview) const
162 {
163     ASSERT(!hasNoValue());
164     Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), ASCIILiteral("wrapObject"), inspectorEnvironment()->functionCallHandler());
165     wrapFunction.appendArgument(value);
166     wrapFunction.appendArgument(groupName);
167     wrapFunction.appendArgument(hasAccessToInspectedScriptState());
168     wrapFunction.appendArgument(generatePreview);
169
170     bool hadException = false;
171     Deprecated::ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
172     if (hadException)
173         return nullptr;
174
175     RefPtr<InspectorObject> resultObject;
176     bool castSucceeded = r.toInspectorValue(scriptState())->asObject(resultObject);
177     ASSERT_UNUSED(castSucceeded, castSucceeded);
178
179     return BindingTraits<Inspector::Protocol::Runtime::RemoteObject>::runtimeCast(resultObject);
180 }
181
182 PassRefPtr<Inspector::Protocol::Runtime::RemoteObject> InjectedScript::wrapTable(const Deprecated::ScriptValue& table, const Deprecated::ScriptValue& columns) const
183 {
184     ASSERT(!hasNoValue());
185     Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), ASCIILiteral("wrapTable"), inspectorEnvironment()->functionCallHandler());
186     wrapFunction.appendArgument(hasAccessToInspectedScriptState());
187     wrapFunction.appendArgument(table);
188     if (columns.hasNoValue())
189         wrapFunction.appendArgument(false);
190     else
191         wrapFunction.appendArgument(columns);
192
193     bool hadException = false;
194     Deprecated::ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
195     if (hadException)
196         return nullptr;
197
198     RefPtr<InspectorObject> resultObject;
199     bool castSucceeded = r.toInspectorValue(scriptState())->asObject(resultObject);
200     ASSERT_UNUSED(castSucceeded, castSucceeded);
201
202     return BindingTraits<Inspector::Protocol::Runtime::RemoteObject>::runtimeCast(resultObject);
203 }
204
205 Deprecated::ScriptValue InjectedScript::findObjectById(const String& objectId) const
206 {
207     ASSERT(!hasNoValue());
208     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("findObjectById"), inspectorEnvironment()->functionCallHandler());
209     function.appendArgument(objectId);
210
211     bool hadException = false;
212     Deprecated::ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
213     ASSERT(!hadException);
214
215     return resultValue;
216 }
217
218 void InjectedScript::inspectObject(Deprecated::ScriptValue value)
219 {
220     ASSERT(!hasNoValue());
221     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("inspectObject"), inspectorEnvironment()->functionCallHandler());
222     function.appendArgument(value);
223     RefPtr<InspectorValue> result;
224     makeCall(function, &result);
225 }
226
227 void InjectedScript::releaseObject(const String& objectId)
228 {
229     Deprecated::ScriptFunctionCall function(injectedScriptObject(), ASCIILiteral("releaseObject"), inspectorEnvironment()->functionCallHandler());
230     function.appendArgument(objectId);
231     RefPtr<InspectorValue> result;
232     makeCall(function, &result);
233 }
234
235 void InjectedScript::releaseObjectGroup(const String& objectGroup)
236 {
237     ASSERT(!hasNoValue());
238     Deprecated::ScriptFunctionCall releaseFunction(injectedScriptObject(), ASCIILiteral("releaseObjectGroup"), inspectorEnvironment()->functionCallHandler());
239     releaseFunction.appendArgument(objectGroup);
240
241     bool hadException = false;
242     callFunctionWithEvalEnabled(releaseFunction, hadException);
243     ASSERT(!hadException);
244 }
245
246 } // namespace Inspector
247
248 #endif // ENABLE(INSPECTOR)