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"
35 #include "DebuggerEvalEnabler.h"
36 #include "InspectorValues.h"
37 #include "JSCInlines.h"
38 #include "JSGlobalObject.h"
39 #include "ScriptFunctionCall.h"
40 #include <wtf/text/WTFString.h>
44 InjectedScriptBase::InjectedScriptBase(const String
& name
)
46 , m_environment(nullptr)
50 InjectedScriptBase::InjectedScriptBase(const String
& name
, Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
)
52 , m_injectedScriptObject(injectedScriptObject
)
53 , m_environment(environment
)
57 InjectedScriptBase::~InjectedScriptBase()
61 void InjectedScriptBase::initialize(Deprecated::ScriptObject injectedScriptObject
, InspectorEnvironment
* environment
)
63 m_injectedScriptObject
= injectedScriptObject
;
64 m_environment
= environment
;
67 bool InjectedScriptBase::hasAccessToInspectedScriptState() const
69 return m_environment
&& m_environment
->canAccessInspectedScriptState(m_injectedScriptObject
.scriptState());
72 const Deprecated::ScriptObject
& InjectedScriptBase::injectedScriptObject() const
74 return m_injectedScriptObject
;
77 Deprecated::ScriptValue
InjectedScriptBase::callFunctionWithEvalEnabled(Deprecated::ScriptFunctionCall
& function
, bool& hadException
) const
80 m_environment
->willCallInjectedScriptFunction(m_injectedScriptObject
.scriptState(), name(), 1);
82 JSC::ExecState
* scriptState
= m_injectedScriptObject
.scriptState();
83 Deprecated::ScriptValue resultValue
;
86 JSC::DebuggerEvalEnabler
evalEnabler(scriptState
);
87 resultValue
= function
.call(hadException
);
91 m_environment
->didCallInjectedScriptFunction(m_injectedScriptObject
.scriptState());
96 void InjectedScriptBase::makeCall(Deprecated::ScriptFunctionCall
& function
, RefPtr
<InspectorValue
>* result
)
98 if (hasNoValue() || !hasAccessToInspectedScriptState()) {
99 *result
= InspectorValue::null();
103 bool hadException
= false;
104 Deprecated::ScriptValue resultValue
= callFunctionWithEvalEnabled(function
, hadException
);
106 ASSERT(!hadException
);
108 *result
= resultValue
.toInspectorValue(m_injectedScriptObject
.scriptState());
110 *result
= InspectorString::create(String::format("Object has too long reference chain (must not be longer than %d)", InspectorValue::maxDepth
));
112 *result
= InspectorString::create("Exception while making a call.");
115 void InjectedScriptBase::makeEvalCall(ErrorString
& errorString
, Deprecated::ScriptFunctionCall
& function
, RefPtr
<Protocol::Runtime::RemoteObject
>* objectResult
, Protocol::OptOutput
<bool>* wasThrown
, Protocol::OptOutput
<int>* savedResultIndex
)
117 RefPtr
<InspectorValue
> result
;
118 makeCall(function
, &result
);
120 errorString
= ASCIILiteral("Internal error: result value is empty");
124 if (result
->type() == InspectorValue::Type::String
) {
125 result
->asString(errorString
);
126 ASSERT(errorString
.length());
130 RefPtr
<InspectorObject
> resultTuple
;
131 if (!result
->asObject(resultTuple
)) {
132 errorString
= ASCIILiteral("Internal error: result is not an Object");
136 RefPtr
<InspectorObject
> resultObject
;
137 if (!resultTuple
->getObject(ASCIILiteral("result"), resultObject
)) {
138 errorString
= ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
142 bool wasThrownValue
= false;
143 if (!resultTuple
->getBoolean(ASCIILiteral("wasThrown"), wasThrownValue
)) {
144 errorString
= ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
148 *objectResult
= BindingTraits
<Protocol::Runtime::RemoteObject
>::runtimeCast(resultObject
);
149 *wasThrown
= wasThrownValue
;
151 if (savedResultIndex
) {
153 if (resultTuple
->getInteger(ASCIILiteral("savedResultIndex"), savedIndex
))
154 *savedResultIndex
= savedIndex
;
158 } // namespace Inspector