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