Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InjectedScriptBase.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/InjectedScriptBase.h"
35
36 #include "bindings/core/v8/ScriptFunctionCall.h"
37 #include "core/inspector/InspectorInstrumentation.h"
38 #include "core/inspector/InspectorTraceEvents.h"
39 #include "platform/JSONValues.h"
40 #include "wtf/text/WTFString.h"
41
42 using blink::TypeBuilder::Array;
43 using blink::TypeBuilder::Runtime::RemoteObject;
44
45 namespace blink {
46
47 static PassRefPtr<TypeBuilder::Debugger::ExceptionDetails> toExceptionDetails(PassRefPtr<JSONObject> object)
48 {
49     String text;
50     if (!object->getString("text", &text))
51         return nullptr;
52
53     RefPtr<TypeBuilder::Debugger::ExceptionDetails> exceptionDetails = TypeBuilder::Debugger::ExceptionDetails::create().setText(text);
54     String url;
55     if (object->getString("url", &url))
56         exceptionDetails->setUrl(url);
57     int line = 0;
58     if (object->getNumber("line", &line))
59         exceptionDetails->setLine(line);
60     int column = 0;
61     if (object->getNumber("column", &column))
62         exceptionDetails->setColumn(column);
63     int originScriptId = 0;
64     object->getNumber("scriptId", &originScriptId);
65
66     RefPtr<JSONArray> stackTrace = object->getArray("stackTrace");
67     if (stackTrace && stackTrace->length() > 0) {
68         RefPtr<TypeBuilder::Array<TypeBuilder::Console::CallFrame> > frames = TypeBuilder::Array<TypeBuilder::Console::CallFrame>::create();
69         for (unsigned i = 0; i < stackTrace->length(); ++i) {
70             RefPtr<JSONObject> stackFrame = stackTrace->get(i)->asObject();
71             int lineNumber = 0;
72             stackFrame->getNumber("lineNumber", &lineNumber);
73             int column = 0;
74             stackFrame->getNumber("column", &column);
75             int scriptId = 0;
76             stackFrame->getNumber("scriptId", &scriptId);
77             if (i == 0 && scriptId == originScriptId)
78                 originScriptId = 0;
79
80             String sourceURL;
81             stackFrame->getString("scriptNameOrSourceURL", &sourceURL);
82             String functionName;
83             stackFrame->getString("functionName", &functionName);
84
85             RefPtr<TypeBuilder::Console::CallFrame> callFrame = TypeBuilder::Console::CallFrame::create()
86                 .setFunctionName(functionName)
87                 .setScriptId(String::number(scriptId))
88                 .setUrl(sourceURL)
89                 .setLineNumber(lineNumber)
90                 .setColumnNumber(column);
91
92             frames->addItem(callFrame.release());
93         }
94         exceptionDetails->setStackTrace(frames.release());
95     }
96     if (originScriptId)
97         exceptionDetails->setScriptId(String::number(originScriptId));
98     return exceptionDetails.release();
99 }
100
101 InjectedScriptBase::InjectedScriptBase(const String& name)
102     : m_name(name)
103     , m_inspectedStateAccessCheck(0)
104 {
105 }
106
107 InjectedScriptBase::InjectedScriptBase(const String& name, ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
108     : m_name(name)
109     , m_injectedScriptObject(injectedScriptObject)
110     , m_inspectedStateAccessCheck(accessCheck)
111 {
112 }
113
114 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
115 {
116     m_injectedScriptObject = injectedScriptObject;
117     m_inspectedStateAccessCheck = accessCheck;
118 }
119
120 bool InjectedScriptBase::canAccessInspectedWindow() const
121 {
122     ASSERT(!isEmpty());
123     return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
124 }
125
126 const ScriptValue& InjectedScriptBase::injectedScriptObject() const
127 {
128     return m_injectedScriptObject;
129 }
130
131 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall& function, bool& hadException) const
132 {
133     ASSERT(!isEmpty());
134     ExecutionContext* executionContext = m_injectedScriptObject.scriptState()->executionContext();
135     TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall", "data", InspectorFunctionCallEvent::data(executionContext, 0, name(), 1));
136     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
137     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
138     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(executionContext, 0, name(), 1);
139
140     ScriptState* scriptState = m_injectedScriptObject.scriptState();
141     bool evalIsDisabled = false;
142     if (scriptState) {
143         evalIsDisabled = !scriptState->evalEnabled();
144         // Temporarily enable allow evals for inspector.
145         if (evalIsDisabled)
146             scriptState->setEvalEnabled(true);
147     }
148
149     ScriptValue resultValue = function.call(hadException);
150
151     if (evalIsDisabled)
152         scriptState->setEvalEnabled(false);
153
154     InspectorInstrumentation::didCallFunction(cookie);
155     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
156     return resultValue;
157 }
158
159 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue>* result)
160 {
161     if (isEmpty() || !canAccessInspectedWindow()) {
162         *result = JSONValue::null();
163         return;
164     }
165
166     bool hadException = false;
167     ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
168
169     ASSERT(!hadException);
170     if (!hadException) {
171         *result = resultValue.toJSONValue(m_injectedScriptObject.scriptState());
172         if (!*result)
173             *result = JSONString::create(String::format("Object has too long reference chain(must not be longer than %d)", JSONValue::maxDepth));
174     } else {
175         *result = JSONString::create("Exception while making a call.");
176     }
177 }
178
179 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>* exceptionDetails)
180 {
181     RefPtr<JSONValue> result;
182     makeCall(function, &result);
183     if (!result) {
184         *errorString = "Internal error: result value is empty";
185         return;
186     }
187     if (result->type() == JSONValue::TypeString) {
188         result->asString(errorString);
189         ASSERT(errorString->length());
190         return;
191     }
192     RefPtr<JSONObject> resultPair = result->asObject();
193     if (!resultPair) {
194         *errorString = "Internal error: result is not an Object";
195         return;
196     }
197     RefPtr<JSONObject> resultObj = resultPair->getObject("result");
198     bool wasThrownVal = false;
199     if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
200         *errorString = "Internal error: result is not a pair of value and wasThrown flag";
201         return;
202     }
203     if (wasThrownVal) {
204         RefPtr<JSONObject> objectExceptionDetails = resultPair->getObject("exceptionDetails");
205         if (objectExceptionDetails)
206             *exceptionDetails = toExceptionDetails(objectExceptionDetails.release());
207     }
208     *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
209     *wasThrown = wasThrownVal;
210 }
211
212 } // namespace blink
213