OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
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 | |
13 * distribution. | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 | |
34 #include "core/inspector/InjectedScriptBase.h" | |
35 | |
36 #include "bindings/core/v8/ScriptFunctionCall.h" | |
37 #include "bindings/core/v8/V8Binding.h" | |
38 #include "core/inspector/InspectorInstrumentation.h" | |
39 #include "core/inspector/InspectorTraceEvents.h" | |
40 #include "platform/JSONValues.h" | |
41 #include "wtf/text/WTFString.h" | |
42 | |
43 using blink::TypeBuilder::Array; | |
44 using blink::TypeBuilder::Runtime::RemoteObject; | |
45 | |
46 namespace blink { | |
47 | |
48 PassRefPtr<JSONValue> toJSONValue(const ScriptValue& value) | |
49 { | |
50 ScriptState* scriptState = value.scriptState(); | |
51 ASSERT(scriptState->contextIsValid()); | |
52 ScriptState::Scope scope(scriptState); | |
53 NonThrowableExceptionState exceptionState; | |
54 return ScriptValue::to<JSONValuePtr>(scriptState->isolate(), value, exceptio
nState); | |
55 } | |
56 | |
57 static PassRefPtr<TypeBuilder::Debugger::ExceptionDetails> toExceptionDetails(Pa
ssRefPtr<JSONObject> object) | |
58 { | |
59 String text; | |
60 if (!object->getString("text", &text)) | |
61 return nullptr; | |
62 | |
63 RefPtr<TypeBuilder::Debugger::ExceptionDetails> exceptionDetails = TypeBuild
er::Debugger::ExceptionDetails::create().setText(text); | |
64 String url; | |
65 if (object->getString("url", &url)) | |
66 exceptionDetails->setUrl(url); | |
67 int line = 0; | |
68 if (object->getNumber("line", &line)) | |
69 exceptionDetails->setLine(line); | |
70 int column = 0; | |
71 if (object->getNumber("column", &column)) | |
72 exceptionDetails->setColumn(column); | |
73 int originScriptId = 0; | |
74 object->getNumber("scriptId", &originScriptId); | |
75 | |
76 RefPtr<JSONArray> stackTrace = object->getArray("stackTrace"); | |
77 if (stackTrace && stackTrace->length() > 0) { | |
78 RefPtr<TypeBuilder::Array<TypeBuilder::Console::CallFrame> > frames = Ty
peBuilder::Array<TypeBuilder::Console::CallFrame>::create(); | |
79 for (unsigned i = 0; i < stackTrace->length(); ++i) { | |
80 RefPtr<JSONObject> stackFrame = stackTrace->get(i)->asObject(); | |
81 int lineNumber = 0; | |
82 stackFrame->getNumber("lineNumber", &lineNumber); | |
83 int column = 0; | |
84 stackFrame->getNumber("column", &column); | |
85 int scriptId = 0; | |
86 stackFrame->getNumber("scriptId", &scriptId); | |
87 if (i == 0 && scriptId == originScriptId) | |
88 originScriptId = 0; | |
89 | |
90 String sourceURL; | |
91 stackFrame->getString("scriptNameOrSourceURL", &sourceURL); | |
92 String functionName; | |
93 stackFrame->getString("functionName", &functionName); | |
94 | |
95 RefPtr<TypeBuilder::Console::CallFrame> callFrame = TypeBuilder::Con
sole::CallFrame::create() | |
96 .setFunctionName(functionName) | |
97 .setScriptId(String::number(scriptId)) | |
98 .setUrl(sourceURL) | |
99 .setLineNumber(lineNumber) | |
100 .setColumnNumber(column); | |
101 | |
102 frames->addItem(callFrame.release()); | |
103 } | |
104 exceptionDetails->setStackTrace(frames.release()); | |
105 } | |
106 if (originScriptId) | |
107 exceptionDetails->setScriptId(String::number(originScriptId)); | |
108 return exceptionDetails.release(); | |
109 } | |
110 | |
111 InjectedScriptBase::InjectedScriptBase(const String& name) | |
112 : m_name(name) | |
113 , m_inspectedStateAccessCheck(0) | |
114 { | |
115 } | |
116 | |
117 InjectedScriptBase::InjectedScriptBase(const String& name, ScriptValue injectedS
criptObject, InspectedStateAccessCheck accessCheck) | |
118 : m_name(name) | |
119 , m_injectedScriptObject(injectedScriptObject) | |
120 , m_inspectedStateAccessCheck(accessCheck) | |
121 { | |
122 } | |
123 | |
124 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject, InspectedS
tateAccessCheck accessCheck) | |
125 { | |
126 m_injectedScriptObject = injectedScriptObject; | |
127 m_inspectedStateAccessCheck = accessCheck; | |
128 } | |
129 | |
130 bool InjectedScriptBase::canAccessInspectedWindow() const | |
131 { | |
132 ASSERT(!isEmpty()); | |
133 return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState()); | |
134 } | |
135 | |
136 const ScriptValue& InjectedScriptBase::injectedScriptObject() const | |
137 { | |
138 return m_injectedScriptObject; | |
139 } | |
140 | |
141 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall&
function, bool& hadException) const | |
142 { | |
143 ASSERT(!isEmpty()); | |
144 ExecutionContext* executionContext = m_injectedScriptObject.scriptState()->e
xecutionContext(); | |
145 ScriptState::Scope scope(m_injectedScriptObject.scriptState()); | |
146 v8::Local<v8::Function> functionObj = function.function(); | |
147 DevToolsFunctionInfo info(functionObj); | |
148 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFu
nction(executionContext, info); | |
149 | |
150 ScriptState* scriptState = m_injectedScriptObject.scriptState(); | |
151 bool evalIsDisabled = false; | |
152 if (scriptState) { | |
153 evalIsDisabled = !scriptState->evalEnabled(); | |
154 // Temporarily enable allow evals for inspector. | |
155 if (evalIsDisabled) | |
156 scriptState->setEvalEnabled(true); | |
157 } | |
158 | |
159 ScriptValue resultValue = function.call(hadException); | |
160 | |
161 if (evalIsDisabled) | |
162 scriptState->setEvalEnabled(false); | |
163 | |
164 InspectorInstrumentation::didCallFunction(cookie); | |
165 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update
Counters", TRACE_EVENT_SCOPE_THREAD, "data", InspectorUpdateCountersEvent::data(
)); | |
166 return resultValue; | |
167 } | |
168 | |
169 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue
>* result) | |
170 { | |
171 if (isEmpty() || !canAccessInspectedWindow()) { | |
172 *result = JSONValue::null(); | |
173 return; | |
174 } | |
175 | |
176 bool hadException = false; | |
177 ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException
); | |
178 | |
179 ASSERT(!hadException); | |
180 if (!hadException) { | |
181 *result = toJSONValue(resultValue); | |
182 if (!*result) | |
183 *result = JSONString::create(String::format("Object has too long ref
erence chain(must not be longer than %d)", JSONValue::maxDepth)); | |
184 } else { | |
185 *result = JSONString::create("Exception while making a call."); | |
186 } | |
187 } | |
188 | |
189 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCa
ll& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuil
der::OptOutput<bool>* wasThrown, RefPtr<TypeBuilder::Debugger::ExceptionDetails>
* exceptionDetails) | |
190 { | |
191 RefPtr<JSONValue> result; | |
192 makeCall(function, &result); | |
193 if (!result) { | |
194 *errorString = "Internal error: result value is empty"; | |
195 return; | |
196 } | |
197 if (result->type() == JSONValue::TypeString) { | |
198 result->asString(errorString); | |
199 ASSERT(errorString->length()); | |
200 return; | |
201 } | |
202 RefPtr<JSONObject> resultPair = result->asObject(); | |
203 if (!resultPair) { | |
204 *errorString = "Internal error: result is not an Object"; | |
205 return; | |
206 } | |
207 RefPtr<JSONObject> resultObj = resultPair->getObject("result"); | |
208 bool wasThrownVal = false; | |
209 if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) { | |
210 *errorString = "Internal error: result is not a pair of value and wasThr
own flag"; | |
211 return; | |
212 } | |
213 if (wasThrownVal) { | |
214 RefPtr<JSONObject> objectExceptionDetails = resultPair->getObject("excep
tionDetails"); | |
215 if (objectExceptionDetails) | |
216 *exceptionDetails = toExceptionDetails(objectExceptionDetails.releas
e()); | |
217 } | |
218 *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj); | |
219 *wasThrown = wasThrownVal; | |
220 } | |
221 | |
222 void InjectedScriptBase::makeCallWithExceptionDetails(ScriptFunctionCall& functi
on, RefPtr<JSONValue>* result, RefPtr<TypeBuilder::Debugger::ExceptionDetails>*
exceptionDetails) | |
223 { | |
224 ScriptState::Scope scope(injectedScriptObject().scriptState()); | |
225 v8::TryCatch tryCatch; | |
226 ScriptValue resultValue = function.callWithoutExceptionHandling(); | |
227 if (tryCatch.HasCaught()) { | |
228 v8::Local<v8::Message> message = tryCatch.Message(); | |
229 String text = !message.IsEmpty() ? toCoreStringWithUndefinedOrNullCheck(
message->Get()) : "Internal error"; | |
230 *exceptionDetails = TypeBuilder::Debugger::ExceptionDetails::create().se
tText(text); | |
231 } else { | |
232 *result = toJSONValue(resultValue); | |
233 if (!*result) | |
234 *result = JSONString::create(String::format("Object has too long ref
erence chain(must not be longer than %d)", JSONValue::maxDepth)); | |
235 } | |
236 } | |
237 | |
238 } // namespace blink | |
OLD | NEW |