2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "core/inspector/InjectedScriptBase.h"
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"
42 using blink::TypeBuilder::Array;
43 using blink::TypeBuilder::Runtime::RemoteObject;
47 static PassRefPtr<TypeBuilder::Debugger::ExceptionDetails> toExceptionDetails(PassRefPtr<JSONObject> object)
50 if (!object->getString("text", &text))
53 RefPtr<TypeBuilder::Debugger::ExceptionDetails> exceptionDetails = TypeBuilder::Debugger::ExceptionDetails::create().setText(text);
55 if (object->getString("url", &url))
56 exceptionDetails->setUrl(url);
58 if (object->getNumber("line", &line))
59 exceptionDetails->setLine(line);
61 if (object->getNumber("column", &column))
62 exceptionDetails->setColumn(column);
63 int originScriptId = 0;
64 object->getNumber("scriptId", &originScriptId);
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();
72 stackFrame->getNumber("lineNumber", &lineNumber);
74 stackFrame->getNumber("column", &column);
76 stackFrame->getNumber("scriptId", &scriptId);
77 if (i == 0 && scriptId == originScriptId)
81 stackFrame->getString("scriptNameOrSourceURL", &sourceURL);
83 stackFrame->getString("functionName", &functionName);
85 RefPtr<TypeBuilder::Console::CallFrame> callFrame = TypeBuilder::Console::CallFrame::create()
86 .setFunctionName(functionName)
87 .setScriptId(String::number(scriptId))
89 .setLineNumber(lineNumber)
90 .setColumnNumber(column);
92 frames->addItem(callFrame.release());
94 exceptionDetails->setStackTrace(frames.release());
97 exceptionDetails->setScriptId(String::number(originScriptId));
98 return exceptionDetails.release();
101 InjectedScriptBase::InjectedScriptBase(const String& name)
103 , m_inspectedStateAccessCheck(0)
107 InjectedScriptBase::InjectedScriptBase(const String& name, ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
109 , m_injectedScriptObject(injectedScriptObject)
110 , m_inspectedStateAccessCheck(accessCheck)
114 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
116 m_injectedScriptObject = injectedScriptObject;
117 m_inspectedStateAccessCheck = accessCheck;
120 bool InjectedScriptBase::canAccessInspectedWindow() const
123 return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
126 const ScriptValue& InjectedScriptBase::injectedScriptObject() const
128 return m_injectedScriptObject;
131 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall& function, bool& hadException) const
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);
140 ScriptState* scriptState = m_injectedScriptObject.scriptState();
141 bool evalIsDisabled = false;
143 evalIsDisabled = !scriptState->evalEnabled();
144 // Temporarily enable allow evals for inspector.
146 scriptState->setEvalEnabled(true);
149 ScriptValue resultValue = function.call(hadException);
152 scriptState->setEvalEnabled(false);
154 InspectorInstrumentation::didCallFunction(cookie);
155 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
159 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue>* result)
161 if (isEmpty() || !canAccessInspectedWindow()) {
162 *result = JSONValue::null();
166 bool hadException = false;
167 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
169 ASSERT(!hadException);
171 *result = resultValue.toJSONValue(m_injectedScriptObject.scriptState());
173 *result = JSONString::create(String::format("Object has too long reference chain(must not be longer than %d)", JSONValue::maxDepth));
175 *result = JSONString::create("Exception while making a call.");
179 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>* exceptionDetails)
181 RefPtr<JSONValue> result;
182 makeCall(function, &result);
184 *errorString = "Internal error: result value is empty";
187 if (result->type() == JSONValue::TypeString) {
188 result->asString(errorString);
189 ASSERT(errorString->length());
192 RefPtr<JSONObject> resultPair = result->asObject();
194 *errorString = "Internal error: result is not an Object";
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";
204 RefPtr<JSONObject> objectExceptionDetails = resultPair->getObject("exceptionDetails");
205 if (objectExceptionDetails)
206 *exceptionDetails = toExceptionDetails(objectExceptionDetails.release());
208 *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
209 *wasThrown = wasThrownVal;