1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
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
14 * distribution.
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.
18 *
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.
30 */
31
32#include "config.h"
33#include "InjectedScriptBase.h"
34
35#include "DebuggerEvalEnabler.h"
36#include "JSCInlines.h"
37#include "JSGlobalObject.h"
38#include "JSLock.h"
39#include "JSNativeStdFunction.h"
40#include "NativeStdFunctionCell.h"
41#include "ScriptFunctionCall.h"
42#include <wtf/JSONValues.h>
43#include <wtf/text/StringConcatenateNumbers.h>
44
45namespace Inspector {
46
47InjectedScriptBase::InjectedScriptBase(const String& name)
48 : m_name(name)
49{
50}
51
52InjectedScriptBase::InjectedScriptBase(const String& name, Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
53 : m_name(name)
54 , m_injectedScriptObject(injectedScriptObject)
55 , m_environment(environment)
56{
57}
58
59InjectedScriptBase::~InjectedScriptBase()
60{
61}
62
63bool InjectedScriptBase::hasAccessToInspectedScriptState() const
64{
65 return m_environment && m_environment->canAccessInspectedScriptState(m_injectedScriptObject.scriptState());
66}
67
68const Deprecated::ScriptObject& InjectedScriptBase::injectedScriptObject() const
69{
70 return m_injectedScriptObject;
71}
72
73JSC::JSValue InjectedScriptBase::callFunctionWithEvalEnabled(Deprecated::ScriptFunctionCall& function, bool& hadException) const
74{
75 JSC::ExecState* scriptState = m_injectedScriptObject.scriptState();
76 JSC::DebuggerEvalEnabler evalEnabler(scriptState);
77 return function.call(hadException);
78}
79
80Ref<JSON::Value> InjectedScriptBase::makeCall(Deprecated::ScriptFunctionCall& function)
81{
82 if (hasNoValue() || !hasAccessToInspectedScriptState())
83 return JSON::Value::null();
84
85 bool hadException = false;
86 auto resultJSValue = callFunctionWithEvalEnabled(function, hadException);
87
88 ASSERT(!hadException);
89 if (hadException)
90 return JSON::Value::create("Exception while making a call.");
91
92 RefPtr<JSON::Value> resultJSONValue = toInspectorValue(*m_injectedScriptObject.scriptState(), resultJSValue);
93 if (!resultJSONValue)
94 return JSON::Value::create(makeString("Object has too long reference chain (must not be longer than ", JSON::Value::maxDepth, ')'));
95
96 return resultJSONValue.releaseNonNull();
97}
98
99void InjectedScriptBase::makeEvalCall(ErrorString& errorString, Deprecated::ScriptFunctionCall& function, RefPtr<Protocol::Runtime::RemoteObject>& out_resultObject, Optional<bool>& out_wasThrown, Optional<int>& out_savedResultIndex)
100{
101 checkCallResult(errorString, makeCall(function), out_resultObject, out_wasThrown, out_savedResultIndex);
102}
103
104void InjectedScriptBase::makeAsyncCall(Deprecated::ScriptFunctionCall& function, AsyncCallCallback&& callback)
105{
106 if (hasNoValue() || !hasAccessToInspectedScriptState()) {
107 checkAsyncCallResult(JSON::Value::null(), callback);
108 return;
109 }
110
111 auto* scriptState = m_injectedScriptObject.scriptState();
112 JSC::VM& vm = scriptState->vm();
113
114 JSC::JSNativeStdFunction* jsFunction;
115
116 {
117 JSC::JSLockHolder locker(vm);
118
119 jsFunction = JSC::JSNativeStdFunction::create(vm, scriptState->lexicalGlobalObject(), 1, String(), [&, callback = WTFMove(callback)] (JSC::ExecState* exec) {
120 if (!exec)
121 checkAsyncCallResult(JSON::Value::create("Exception while making a call."), callback);
122 else if (auto resultJSONValue = toInspectorValue(*exec, exec->argument(0)))
123 checkAsyncCallResult(resultJSONValue, callback);
124 else
125 checkAsyncCallResult(JSON::Value::create(makeString("Object has too long reference chain (must not be longer than ", JSON::Value::maxDepth, ')')), callback);
126 return JSC::JSValue::encode(JSC::jsUndefined());
127 });
128 }
129
130 function.appendArgument(JSC::JSValue(jsFunction));
131
132 bool hadException = false;
133 auto resultJSValue = callFunctionWithEvalEnabled(function, hadException);
134 ASSERT_UNUSED(resultJSValue, resultJSValue.isUndefined());
135
136 ASSERT(!hadException);
137 if (hadException) {
138 // Since `callback` is moved above, we can't call it if there's an exception while trying to
139 // execute the `JSNativeStdFunction` inside InjectedScriptSource.js.
140 jsFunction->nativeStdFunctionCell()->function()(nullptr);
141 }
142}
143
144void InjectedScriptBase::checkCallResult(ErrorString& errorString, RefPtr<JSON::Value> result, RefPtr<Protocol::Runtime::RemoteObject>& out_resultObject, Optional<bool>& out_wasThrown, Optional<int>& out_savedResultIndex)
145{
146 if (!result) {
147 errorString = "Internal error: result value is empty"_s;
148 return;
149 }
150
151 if (result->type() == JSON::Value::Type::String) {
152 result->asString(errorString);
153 ASSERT(errorString.length());
154 return;
155 }
156
157 RefPtr<JSON::Object> resultTuple;
158 if (!result->asObject(resultTuple)) {
159 errorString = "Internal error: result is not an Object"_s;
160 return;
161 }
162
163 RefPtr<JSON::Object> resultObject;
164 if (!resultTuple->getObject("result"_s, resultObject)) {
165 errorString = "Internal error: result is not a pair of value and wasThrown flag"_s;
166 return;
167 }
168
169 bool wasThrown = false;
170 if (!resultTuple->getBoolean("wasThrown"_s, wasThrown)) {
171 errorString = "Internal error: result is not a pair of value and wasThrown flag"_s;
172 return;
173 }
174
175 out_resultObject = BindingTraits<Protocol::Runtime::RemoteObject>::runtimeCast(resultObject);
176
177 if (wasThrown)
178 out_wasThrown = wasThrown;
179
180 int savedResultIndex;
181 if (resultTuple->getInteger("savedResultIndex"_s, savedResultIndex))
182 out_savedResultIndex = savedResultIndex;
183}
184
185void InjectedScriptBase::checkAsyncCallResult(RefPtr<JSON::Value> result, const AsyncCallCallback& callback)
186{
187 ErrorString errorString;
188 RefPtr<Protocol::Runtime::RemoteObject> resultObject;
189 Optional<bool> wasThrown;
190 Optional<int> savedResultIndex;
191
192 checkCallResult(errorString, result, resultObject, wasThrown, savedResultIndex);
193
194 callback(errorString, WTFMove(resultObject), wasThrown, savedResultIndex);
195}
196
197} // namespace Inspector
198
199