Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InjectedScript.cpp
1 /*
2  * Copyright (C) 2012 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
33
34 #include "core/inspector/InjectedScript.h"
35
36 #include "bindings/core/v8/ScriptFunctionCall.h"
37 #include "core/inspector/InjectedScriptHost.h"
38 #include "platform/JSONValues.h"
39 #include "wtf/text/WTFString.h"
40
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;
48
49 namespace blink {
50
51 InjectedScript::InjectedScript()
52     : InjectedScriptBase("InjectedScript")
53 {
54 }
55
56 InjectedScript::InjectedScript(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
57     : InjectedScriptBase("InjectedScript", injectedScriptObject, accessCheck)
58 {
59 }
60
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)
62 {
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);
70 }
71
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)
73 {
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);
81 }
82
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)
84 {
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);
95 }
96
97 void InjectedScript::restartFrame(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, RefPtr<JSONObject>* result)
98 {
99     ScriptFunctionCall function(injectedScriptObject(), "restartFrame");
100     function.appendArgument(callFrames);
101     function.appendArgument(callFrameId);
102     RefPtr<JSONValue> resultValue;
103     makeCall(function, &resultValue);
104     if (resultValue) {
105         if (resultValue->type() == JSONValue::TypeString) {
106             resultValue->asString(errorString);
107             return;
108         }
109         if (resultValue->type() == JSONValue::TypeObject) {
110             *result = resultValue->asObject();
111             return;
112         }
113     }
114     *errorString = "Internal error";
115 }
116
117 void InjectedScript::getStepInPositions(ErrorString* errorString, const ScriptValue& callFrames, const String& callFrameId, RefPtr<Array<TypeBuilder::Debugger::Location> >& positions)
118 {
119     ScriptFunctionCall function(injectedScriptObject(), "getStepInPositions");
120     function.appendArgument(callFrames);
121     function.appendArgument(callFrameId);
122     RefPtr<JSONValue> resultValue;
123     makeCall(function, &resultValue);
124     if (resultValue) {
125         if (resultValue->type() == JSONValue::TypeString) {
126             resultValue->asString(errorString);
127             return;
128         }
129         if (resultValue->type() == JSONValue::TypeArray) {
130             positions = Array<TypeBuilder::Debugger::Location>::runtimeCast(resultValue);
131             return;
132         }
133     }
134     *errorString = "Internal error";
135 }
136
137 void InjectedScript::setVariableValue(ErrorString* errorString, const ScriptValue& callFrames, const String* callFrameIdOpt, const String* functionObjectIdOpt, int scopeNumber, const String& variableName, const String& newValueStr)
138 {
139     ScriptFunctionCall function(injectedScriptObject(), "setVariableValue");
140     if (callFrameIdOpt) {
141         function.appendArgument(callFrames);
142         function.appendArgument(*callFrameIdOpt);
143     } else {
144         function.appendArgument(false);
145         function.appendArgument(false);
146     }
147     if (functionObjectIdOpt)
148         function.appendArgument(*functionObjectIdOpt);
149     else
150         function.appendArgument(false);
151     function.appendArgument(scopeNumber);
152     function.appendArgument(variableName);
153     function.appendArgument(newValueStr);
154     RefPtr<JSONValue> resultValue;
155     makeCall(function, &resultValue);
156     if (!resultValue) {
157         *errorString = "Internal error";
158         return;
159     }
160     if (resultValue->type() == JSONValue::TypeString) {
161         resultValue->asString(errorString);
162         return;
163     }
164     // Normal return.
165 }
166
167 void InjectedScript::getFunctionDetails(ErrorString* errorString, const String& functionId, RefPtr<FunctionDetails>* result)
168 {
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";
176         return;
177     }
178     *result = FunctionDetails::runtimeCast(resultValue);
179 }
180
181 void InjectedScript::getCollectionEntries(ErrorString* errorString, const String& objectId, RefPtr<Array<CollectionEntry> >* result)
182 {
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";
190         return;
191     }
192     *result = Array<CollectionEntry>::runtimeCast(resultValue);
193 }
194
195 void InjectedScript::getProperties(ErrorString* errorString, const String& objectId, bool ownProperties, bool accessorPropertiesOnly, RefPtr<Array<PropertyDescriptor> >* properties)
196 {
197     ScriptFunctionCall function(injectedScriptObject(), "getProperties");
198     function.appendArgument(objectId);
199     function.appendArgument(ownProperties);
200     function.appendArgument(accessorPropertiesOnly);
201
202     RefPtr<JSONValue> result;
203     makeCall(function, &result);
204     if (!result || result->type() != JSONValue::TypeArray) {
205         *errorString = "Internal error";
206         return;
207     }
208     *properties = Array<PropertyDescriptor>::runtimeCast(result);
209 }
210
211 void InjectedScript::getInternalProperties(ErrorString* errorString, const String& objectId, RefPtr<Array<InternalPropertyDescriptor> >* properties)
212 {
213     ScriptFunctionCall function(injectedScriptObject(), "getInternalProperties");
214     function.appendArgument(objectId);
215
216     RefPtr<JSONValue> result;
217     makeCall(function, &result);
218     if (!result || result->type() != JSONValue::TypeArray) {
219         *errorString = "Internal error";
220         return;
221     }
222     RefPtr<Array<InternalPropertyDescriptor> > array = Array<InternalPropertyDescriptor>::runtimeCast(result);
223     if (array->length() > 0)
224         *properties = array;
225 }
226
227 Node* InjectedScript::nodeForObjectId(const String& objectId)
228 {
229     if (isEmpty() || !canAccessInspectedWindow())
230         return 0;
231
232     ScriptFunctionCall function(injectedScriptObject(), "nodeForObjectId");
233     function.appendArgument(objectId);
234
235     bool hadException = false;
236     ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
237     ASSERT(!hadException);
238
239     return InjectedScriptHost::scriptValueAsNode(scriptState(), resultValue);
240 }
241
242 void InjectedScript::releaseObject(const String& objectId)
243 {
244     ScriptFunctionCall function(injectedScriptObject(), "releaseObject");
245     function.appendArgument(objectId);
246     RefPtr<JSONValue> result;
247     makeCall(function, &result);
248 }
249
250 PassRefPtr<Array<CallFrame> > InjectedScript::wrapCallFrames(const ScriptValue& callFrames, int asyncOrdinal)
251 {
252     ASSERT(!isEmpty());
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();
263 }
264
265 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapObject(const ScriptValue& value, const String& groupName, bool generatePreview) const
266 {
267     ASSERT(!isEmpty());
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);
275     if (hadException)
276         return nullptr;
277     RefPtr<JSONObject> rawResult = r.toJSONValue(scriptState())->asObject();
278     return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
279 }
280
281 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapTable(const ScriptValue& table, const ScriptValue& columns) const
282 {
283     ASSERT(!isEmpty());
284     ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapTable");
285     wrapFunction.appendArgument(canAccessInspectedWindow());
286     wrapFunction.appendArgument(table);
287     if (columns.isEmpty())
288         wrapFunction.appendArgument(false);
289     else
290         wrapFunction.appendArgument(columns);
291     bool hadException = false;
292     ScriptValue r = callFunctionWithEvalEnabled(wrapFunction, hadException);
293     if (hadException)
294         return nullptr;
295     RefPtr<JSONObject> rawResult = r.toJSONValue(scriptState())->asObject();
296     return TypeBuilder::Runtime::RemoteObject::runtimeCast(rawResult);
297 }
298
299 PassRefPtr<TypeBuilder::Runtime::RemoteObject> InjectedScript::wrapNode(Node* node, const String& groupName)
300 {
301     return wrapObject(nodeAsScriptValue(node), groupName);
302 }
303
304 ScriptValue InjectedScript::findObjectById(const String& objectId) const
305 {
306     ASSERT(!isEmpty());
307     ScriptFunctionCall function(injectedScriptObject(), "findObjectById");
308     function.appendArgument(objectId);
309
310     bool hadException = false;
311     ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
312     ASSERT(!hadException);
313     return resultValue;
314 }
315
316 void InjectedScript::inspectNode(Node* node)
317 {
318     ASSERT(!isEmpty());
319     ScriptFunctionCall function(injectedScriptObject(), "inspectNode");
320     function.appendArgument(nodeAsScriptValue(node));
321     RefPtr<JSONValue> result;
322     makeCall(function, &result);
323 }
324
325 void InjectedScript::releaseObjectGroup(const String& objectGroup)
326 {
327     ASSERT(!isEmpty());
328     ScriptFunctionCall releaseFunction(injectedScriptObject(), "releaseObjectGroup");
329     releaseFunction.appendArgument(objectGroup);
330     bool hadException = false;
331     callFunctionWithEvalEnabled(releaseFunction, hadException);
332     ASSERT(!hadException);
333 }
334
335 ScriptValue InjectedScript::nodeAsScriptValue(Node* node)
336 {
337     return InjectedScriptHost::nodeAsScriptValue(scriptState(), node);
338 }
339
340 } // namespace blink
341