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/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 WebCore::TypeBuilder::Runtime::RemoteObject;
46 InjectedScriptBase::InjectedScriptBase(const String& name)
48 , m_inspectedStateAccessCheck(0)
52 InjectedScriptBase::InjectedScriptBase(const String& name, ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
54 , m_injectedScriptObject(injectedScriptObject)
55 , m_inspectedStateAccessCheck(accessCheck)
59 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
61 m_injectedScriptObject = injectedScriptObject;
62 m_inspectedStateAccessCheck = accessCheck;
65 bool InjectedScriptBase::canAccessInspectedWindow() const
68 return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
71 const ScriptValue& InjectedScriptBase::injectedScriptObject() const
73 return m_injectedScriptObject;
76 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall& function, bool& hadException) const
79 ExecutionContext* executionContext = m_injectedScriptObject.scriptState()->executionContext();
80 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall", "data", InspectorFunctionCallEvent::data(executionContext, 0, name(), 1));
81 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
82 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
83 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(executionContext, 0, name(), 1);
85 ScriptState* scriptState = m_injectedScriptObject.scriptState();
86 bool evalIsDisabled = false;
88 evalIsDisabled = !scriptState->evalEnabled();
89 // Temporarily enable allow evals for inspector.
91 scriptState->setEvalEnabled(true);
94 ScriptValue resultValue = function.call(hadException);
97 scriptState->setEvalEnabled(false);
99 InspectorInstrumentation::didCallFunction(cookie);
100 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
104 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue>* result)
106 if (isEmpty() || !canAccessInspectedWindow()) {
107 *result = JSONValue::null();
111 bool hadException = false;
112 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
114 ASSERT(!hadException);
116 *result = resultValue.toJSONValue(m_injectedScriptObject.scriptState());
118 *result = JSONString::create(String::format("Object has too long reference chain(must not be longer than %d)", JSONValue::maxDepth));
120 *result = JSONString::create("Exception while making a call.");
124 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown)
126 RefPtr<JSONValue> result;
127 makeCall(function, &result);
129 *errorString = "Internal error: result value is empty";
132 if (result->type() == JSONValue::TypeString) {
133 result->asString(errorString);
134 ASSERT(errorString->length());
137 RefPtr<JSONObject> resultPair = result->asObject();
139 *errorString = "Internal error: result is not an Object";
142 RefPtr<JSONObject> resultObj = resultPair->getObject("result");
143 bool wasThrownVal = false;
144 if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
145 *errorString = "Internal error: result is not a pair of value and wasThrown flag";
148 *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
149 *wasThrown = wasThrownVal;
152 } // namespace WebCore