2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "InjectedScriptBase.h"
37 #include "InspectorValues.h"
38 #include "JSCInlines.h"
39 #include "JSGlobalObject.h"
40 #include "ScriptFunctionCall.h"
41 #include <wtf/text/WTFString.h>
45 InjectedScriptBase::InjectedScriptBase(const String
& name
)
47 , m_environment(nullptr)
51 InjectedScriptBase::InjectedScriptBase(const String
& name
, Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
)
53 , m_injectedScriptObject(injectedScriptObject
)
54 , m_environment(environment
)
58 InjectedScriptBase::~InjectedScriptBase()
62 void InjectedScriptBase::initialize(Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
)
64 m_injectedScriptObject
= injectedScriptObject
;
65 m_environment
= environment
;
68 bool InjectedScriptBase::hasAccessToInspectedScriptState() const
70 return m_environment
&& m_environment
->canAccessInspectedScriptState(m_injectedScriptObject
.scriptState());
73 const Deprecated::ScriptObject
& InjectedScriptBase::injectedScriptObject() const
75 return m_injectedScriptObject
;
78 Deprecated::ScriptValue
InjectedScriptBase::callFunctionWithEvalEnabled(Deprecated::ScriptFunctionCall
& function
, bool& hadException
) const
81 m_environment
->willCallInjectedScriptFunction(m_injectedScriptObject
.scriptState(), name(), 1);
83 JSC::ExecState
* scriptState
= m_injectedScriptObject
.scriptState();
84 bool evalIsDisabled
= false;
86 evalIsDisabled
= !scriptState
->lexicalGlobalObject()->evalEnabled();
87 // Temporarily enable allow evals for inspector.
89 scriptState
->lexicalGlobalObject()->setEvalEnabled(true);
92 Deprecated::ScriptValue resultValue
= function
.call(hadException
);
95 scriptState
->lexicalGlobalObject()->setEvalEnabled(false);
98 m_environment
->didCallInjectedScriptFunction(m_injectedScriptObject
.scriptState());
103 void InjectedScriptBase::makeCall(Deprecated::ScriptFunctionCall
& function
, RefPtr
<InspectorValue
>* result
)
105 if (hasNoValue() || !hasAccessToInspectedScriptState()) {
106 *result
= InspectorValue::null();
110 bool hadException
= false;
111 Deprecated::ScriptValue resultValue
= callFunctionWithEvalEnabled(function
, hadException
);
113 ASSERT(!hadException
);
115 *result
= resultValue
.toInspectorValue(m_injectedScriptObject
.scriptState());
117 *result
= InspectorString::create(String::format("Object has too long reference chain (must not be longer than %d)", InspectorValue::maxDepth
));
119 *result
= InspectorString::create("Exception while making a call.");
122 void InjectedScriptBase::makeEvalCall(ErrorString
* errorString
, Deprecated::ScriptFunctionCall
& function
, RefPtr
<TypeBuilder::Runtime::RemoteObject
>* objectResult
, TypeBuilder::OptOutput
<bool>* wasThrown
)
124 RefPtr
<InspectorValue
> result
;
125 makeCall(function
, &result
);
127 *errorString
= ASCIILiteral("Internal error: result value is empty");
131 if (result
->type() == InspectorValue::TypeString
) {
132 result
->asString(errorString
);
133 ASSERT(errorString
->length());
137 RefPtr
<InspectorObject
> resultPair
= result
->asObject();
139 *errorString
= ASCIILiteral("Internal error: result is not an Object");
143 RefPtr
<InspectorObject
> resultObj
= resultPair
->getObject(ASCIILiteral("result"));
144 bool wasThrownVal
= false;
145 if (!resultObj
|| !resultPair
->getBoolean(ASCIILiteral("wasThrown"), &wasThrownVal
)) {
146 *errorString
= ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
150 *objectResult
= TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj
);
151 *wasThrown
= wasThrownVal
;
154 } // namespace Inspector
156 #endif // ENABLE(INSPECTOR)