Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: src/inspector/V8ConsoleMessage.cpp

Issue 2292573002: [inspector] Initial import of v8_inspector. (Closed)
Patch Set: format the code, disable cpplint Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/inspector/V8ConsoleMessage.h ('k') | src/inspector/V8Debugger.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/inspector/V8ConsoleMessage.h"
6
7 #include "src/inspector/InspectedContext.h"
8 #include "src/inspector/StringUtil.h"
9 #include "src/inspector/V8ConsoleAgentImpl.h"
10 #include "src/inspector/V8InspectorImpl.h"
11 #include "src/inspector/V8InspectorSessionImpl.h"
12 #include "src/inspector/V8RuntimeAgentImpl.h"
13 #include "src/inspector/V8StackTraceImpl.h"
14 #include "src/inspector/protocol/Protocol.h"
15 #include "src/inspector/public/V8InspectorClient.h"
16
17 namespace v8_inspector {
18
19 namespace {
20
21 String16 consoleAPITypeValue(ConsoleAPIType type) {
22 switch (type) {
23 case ConsoleAPIType::kLog:
24 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log;
25 case ConsoleAPIType::kDebug:
26 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug;
27 case ConsoleAPIType::kInfo:
28 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Info;
29 case ConsoleAPIType::kError:
30 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Error;
31 case ConsoleAPIType::kWarning:
32 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Warning;
33 case ConsoleAPIType::kClear:
34 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Clear;
35 case ConsoleAPIType::kDir:
36 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dir;
37 case ConsoleAPIType::kDirXML:
38 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dirxml;
39 case ConsoleAPIType::kTable:
40 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Table;
41 case ConsoleAPIType::kTrace:
42 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Trace;
43 case ConsoleAPIType::kStartGroup:
44 return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroup;
45 case ConsoleAPIType::kStartGroupCollapsed:
46 return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroupCollapsed;
47 case ConsoleAPIType::kEndGroup:
48 return protocol::Runtime::ConsoleAPICalled::TypeEnum::EndGroup;
49 case ConsoleAPIType::kAssert:
50 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert;
51 case ConsoleAPIType::kTimeEnd:
52 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug;
53 case ConsoleAPIType::kCount:
54 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug;
55 }
56 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log;
57 }
58
59 const unsigned maxConsoleMessageCount = 1000;
60 const unsigned maxArrayItemsLimit = 10000;
61 const unsigned maxStackDepthLimit = 32;
62
63 class V8ValueStringBuilder {
64 public:
65 static String16 toString(v8::Local<v8::Value> value, v8::Isolate* isolate) {
66 V8ValueStringBuilder builder(isolate);
67 if (!builder.append(value)) return String16();
68 return builder.toString();
69 }
70
71 private:
72 enum {
73 IgnoreNull = 1 << 0,
74 IgnoreUndefined = 1 << 1,
75 };
76
77 V8ValueStringBuilder(v8::Isolate* isolate)
78 : m_arrayLimit(maxArrayItemsLimit),
79 m_isolate(isolate),
80 m_tryCatch(isolate) {}
81
82 bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) {
83 if (value.IsEmpty()) return true;
84 if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true;
85 if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true;
86 if (value->IsString()) return append(v8::Local<v8::String>::Cast(value));
87 if (value->IsStringObject())
88 return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf());
89 if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value));
90 if (value->IsSymbolObject())
91 return append(v8::Local<v8::SymbolObject>::Cast(value)->ValueOf());
92 if (value->IsNumberObject()) {
93 m_builder.append(String16::fromDoublePrecision6(
94 v8::Local<v8::NumberObject>::Cast(value)->ValueOf()));
95 return true;
96 }
97 if (value->IsBooleanObject()) {
98 m_builder.append(v8::Local<v8::BooleanObject>::Cast(value)->ValueOf()
99 ? "true"
100 : "false");
101 return true;
102 }
103 if (value->IsArray()) return append(v8::Local<v8::Array>::Cast(value));
104 if (value->IsProxy()) {
105 m_builder.append("[object Proxy]");
106 return true;
107 }
108 if (value->IsObject() && !value->IsDate() && !value->IsFunction() &&
109 !value->IsNativeError() && !value->IsRegExp()) {
110 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
111 v8::Local<v8::String> stringValue;
112 if (object->ObjectProtoToString(m_isolate->GetCurrentContext())
113 .ToLocal(&stringValue))
114 return append(stringValue);
115 }
116 v8::Local<v8::String> stringValue;
117 if (!value->ToString(m_isolate->GetCurrentContext()).ToLocal(&stringValue))
118 return false;
119 return append(stringValue);
120 }
121
122 bool append(v8::Local<v8::Array> array) {
123 for (const auto& it : m_visitedArrays) {
124 if (it == array) return true;
125 }
126 uint32_t length = array->Length();
127 if (length > m_arrayLimit) return false;
128 if (m_visitedArrays.size() > maxStackDepthLimit) return false;
129
130 bool result = true;
131 m_arrayLimit -= length;
132 m_visitedArrays.push_back(array);
133 for (uint32_t i = 0; i < length; ++i) {
134 if (i) m_builder.append(',');
135 if (!append(array->Get(i), IgnoreNull | IgnoreUndefined)) {
136 result = false;
137 break;
138 }
139 }
140 m_visitedArrays.pop_back();
141 return result;
142 }
143
144 bool append(v8::Local<v8::Symbol> symbol) {
145 m_builder.append("Symbol(");
146 bool result = append(symbol->Name(), IgnoreUndefined);
147 m_builder.append(')');
148 return result;
149 }
150
151 bool append(v8::Local<v8::String> string) {
152 if (m_tryCatch.HasCaught()) return false;
153 if (!string.IsEmpty()) m_builder.append(toProtocolString(string));
154 return true;
155 }
156
157 String16 toString() {
158 if (m_tryCatch.HasCaught()) return String16();
159 return m_builder.toString();
160 }
161
162 uint32_t m_arrayLimit;
163 v8::Isolate* m_isolate;
164 String16Builder m_builder;
165 std::vector<v8::Local<v8::Array>> m_visitedArrays;
166 v8::TryCatch m_tryCatch;
167 };
168
169 } // namespace
170
171 V8ConsoleMessage::V8ConsoleMessage(V8MessageOrigin origin, double timestamp,
172 const String16& message)
173 : m_origin(origin),
174 m_timestamp(timestamp),
175 m_message(message),
176 m_lineNumber(0),
177 m_columnNumber(0),
178 m_scriptId(0),
179 m_contextId(0),
180 m_type(ConsoleAPIType::kLog),
181 m_exceptionId(0),
182 m_revokedExceptionId(0) {}
183
184 V8ConsoleMessage::~V8ConsoleMessage() {}
185
186 void V8ConsoleMessage::setLocation(const String16& url, unsigned lineNumber,
187 unsigned columnNumber,
188 std::unique_ptr<V8StackTraceImpl> stackTrace,
189 int scriptId) {
190 m_url = url;
191 m_lineNumber = lineNumber;
192 m_columnNumber = columnNumber;
193 m_stackTrace = std::move(stackTrace);
194 m_scriptId = scriptId;
195 }
196
197 void V8ConsoleMessage::reportToFrontend(
198 protocol::Console::Frontend* frontend) const {
199 DCHECK(m_origin == V8MessageOrigin::kConsole);
200 String16 level = protocol::Console::ConsoleMessage::LevelEnum::Log;
201 if (m_type == ConsoleAPIType::kDebug || m_type == ConsoleAPIType::kCount ||
202 m_type == ConsoleAPIType::kTimeEnd)
203 level = protocol::Console::ConsoleMessage::LevelEnum::Debug;
204 else if (m_type == ConsoleAPIType::kError ||
205 m_type == ConsoleAPIType::kAssert)
206 level = protocol::Console::ConsoleMessage::LevelEnum::Error;
207 else if (m_type == ConsoleAPIType::kWarning)
208 level = protocol::Console::ConsoleMessage::LevelEnum::Warning;
209 else if (m_type == ConsoleAPIType::kInfo)
210 level = protocol::Console::ConsoleMessage::LevelEnum::Info;
211 std::unique_ptr<protocol::Console::ConsoleMessage> result =
212 protocol::Console::ConsoleMessage::create()
213 .setSource(protocol::Console::ConsoleMessage::SourceEnum::ConsoleApi)
214 .setLevel(level)
215 .setText(m_message)
216 .build();
217 result->setLine(static_cast<int>(m_lineNumber));
218 result->setColumn(static_cast<int>(m_columnNumber));
219 result->setUrl(m_url);
220 frontend->messageAdded(std::move(result));
221 }
222
223 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
224 V8ConsoleMessage::wrapArguments(V8InspectorSessionImpl* session,
225 bool generatePreview) const {
226 if (!m_arguments.size() || !m_contextId) return nullptr;
227 InspectedContext* inspectedContext =
228 session->inspector()->getContext(session->contextGroupId(), m_contextId);
229 if (!inspectedContext) return nullptr;
230
231 v8::Isolate* isolate = inspectedContext->isolate();
232 v8::HandleScope handles(isolate);
233 v8::Local<v8::Context> context = inspectedContext->context();
234
235 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> args =
236 protocol::Array<protocol::Runtime::RemoteObject>::create();
237 if (m_type == ConsoleAPIType::kTable && generatePreview) {
238 v8::Local<v8::Value> table = m_arguments[0]->Get(isolate);
239 v8::Local<v8::Value> columns = m_arguments.size() > 1
240 ? m_arguments[1]->Get(isolate)
241 : v8::Local<v8::Value>();
242 std::unique_ptr<protocol::Runtime::RemoteObject> wrapped =
243 session->wrapTable(context, table, columns);
244 if (wrapped)
245 args->addItem(std::move(wrapped));
246 else
247 args = nullptr;
248 } else {
249 for (size_t i = 0; i < m_arguments.size(); ++i) {
250 std::unique_ptr<protocol::Runtime::RemoteObject> wrapped =
251 session->wrapObject(context, m_arguments[i]->Get(isolate), "console",
252 generatePreview);
253 if (!wrapped) {
254 args = nullptr;
255 break;
256 }
257 args->addItem(std::move(wrapped));
258 }
259 }
260 return args;
261 }
262
263 void V8ConsoleMessage::reportToFrontend(protocol::Runtime::Frontend* frontend,
264 V8InspectorSessionImpl* session,
265 bool generatePreview) const {
266 if (m_origin == V8MessageOrigin::kException) {
267 std::unique_ptr<protocol::Runtime::RemoteObject> exception =
268 wrapException(session, generatePreview);
269 std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails =
270 protocol::Runtime::ExceptionDetails::create()
271 .setExceptionId(m_exceptionId)
272 .setText(exception ? m_message : m_detailedMessage)
273 .setLineNumber(m_lineNumber ? m_lineNumber - 1 : 0)
274 .setColumnNumber(m_columnNumber ? m_columnNumber - 1 : 0)
275 .build();
276 if (m_scriptId)
277 exceptionDetails->setScriptId(String16::fromInteger(m_scriptId));
278 if (!m_url.isEmpty()) exceptionDetails->setUrl(m_url);
279 if (m_stackTrace)
280 exceptionDetails->setStackTrace(m_stackTrace->buildInspectorObjectImpl());
281 if (m_contextId) exceptionDetails->setExecutionContextId(m_contextId);
282 if (exception) exceptionDetails->setException(std::move(exception));
283 frontend->exceptionThrown(m_timestamp, std::move(exceptionDetails));
284 return;
285 }
286 if (m_origin == V8MessageOrigin::kRevokedException) {
287 frontend->exceptionRevoked(m_message, m_revokedExceptionId);
288 return;
289 }
290 if (m_origin == V8MessageOrigin::kConsole) {
291 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
292 arguments = wrapArguments(session, generatePreview);
293 if (!arguments) {
294 arguments = protocol::Array<protocol::Runtime::RemoteObject>::create();
295 if (!m_message.isEmpty()) {
296 std::unique_ptr<protocol::Runtime::RemoteObject> messageArg =
297 protocol::Runtime::RemoteObject::create()
298 .setType(protocol::Runtime::RemoteObject::TypeEnum::String)
299 .build();
300 messageArg->setValue(protocol::StringValue::create(m_message));
301 arguments->addItem(std::move(messageArg));
302 }
303 }
304 frontend->consoleAPICalled(
305 consoleAPITypeValue(m_type), std::move(arguments), m_contextId,
306 m_timestamp,
307 m_stackTrace ? m_stackTrace->buildInspectorObjectImpl() : nullptr);
308 return;
309 }
310 NOTREACHED();
311 }
312
313 std::unique_ptr<protocol::Runtime::RemoteObject>
314 V8ConsoleMessage::wrapException(V8InspectorSessionImpl* session,
315 bool generatePreview) const {
316 if (!m_arguments.size() || !m_contextId) return nullptr;
317 DCHECK_EQ(1u, m_arguments.size());
318 InspectedContext* inspectedContext =
319 session->inspector()->getContext(session->contextGroupId(), m_contextId);
320 if (!inspectedContext) return nullptr;
321
322 v8::Isolate* isolate = inspectedContext->isolate();
323 v8::HandleScope handles(isolate);
324 // TODO(dgozman): should we use different object group?
325 return session->wrapObject(inspectedContext->context(),
326 m_arguments[0]->Get(isolate), "console",
327 generatePreview);
328 }
329
330 V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; }
331
332 ConsoleAPIType V8ConsoleMessage::type() const { return m_type; }
333
334 // static
335 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
336 double timestamp, ConsoleAPIType type,
337 const std::vector<v8::Local<v8::Value>>& arguments,
338 std::unique_ptr<V8StackTraceImpl> stackTrace, InspectedContext* context) {
339 std::unique_ptr<V8ConsoleMessage> message = wrapUnique(
340 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
341 if (stackTrace && !stackTrace->isEmpty()) {
342 message->m_url = toString16(stackTrace->topSourceURL());
343 message->m_lineNumber = stackTrace->topLineNumber();
344 message->m_columnNumber = stackTrace->topColumnNumber();
345 }
346 message->m_stackTrace = std::move(stackTrace);
347 message->m_type = type;
348 message->m_contextId = context->contextId();
349 for (size_t i = 0; i < arguments.size(); ++i)
350 message->m_arguments.push_back(wrapUnique(
351 new v8::Global<v8::Value>(context->isolate(), arguments.at(i))));
352 if (arguments.size())
353 message->m_message =
354 V8ValueStringBuilder::toString(arguments[0], context->isolate());
355
356 V8ConsoleAPIType clientType = V8ConsoleAPIType::kLog;
357 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
358 type == ConsoleAPIType::kTimeEnd)
359 clientType = V8ConsoleAPIType::kDebug;
360 else if (type == ConsoleAPIType::kError || type == ConsoleAPIType::kAssert)
361 clientType = V8ConsoleAPIType::kError;
362 else if (type == ConsoleAPIType::kWarning)
363 clientType = V8ConsoleAPIType::kWarning;
364 else if (type == ConsoleAPIType::kInfo)
365 clientType = V8ConsoleAPIType::kInfo;
366 else if (type == ConsoleAPIType::kClear)
367 clientType = V8ConsoleAPIType::kClear;
368 context->inspector()->client()->consoleAPIMessage(
369 context->contextGroupId(), clientType, toStringView(message->m_message),
370 toStringView(message->m_url), message->m_lineNumber,
371 message->m_columnNumber, message->m_stackTrace.get());
372
373 return message;
374 }
375
376 // static
377 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForException(
378 double timestamp, const String16& detailedMessage, const String16& url,
379 unsigned lineNumber, unsigned columnNumber,
380 std::unique_ptr<V8StackTraceImpl> stackTrace, int scriptId,
381 v8::Isolate* isolate, const String16& message, int contextId,
382 v8::Local<v8::Value> exception, unsigned exceptionId) {
383 std::unique_ptr<V8ConsoleMessage> consoleMessage = wrapUnique(
384 new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message));
385 consoleMessage->setLocation(url, lineNumber, columnNumber,
386 std::move(stackTrace), scriptId);
387 consoleMessage->m_exceptionId = exceptionId;
388 consoleMessage->m_detailedMessage = detailedMessage;
389 if (contextId && !exception.IsEmpty()) {
390 consoleMessage->m_contextId = contextId;
391 consoleMessage->m_arguments.push_back(
392 wrapUnique(new v8::Global<v8::Value>(isolate, exception)));
393 }
394 return consoleMessage;
395 }
396
397 // static
398 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException(
399 double timestamp, const String16& messageText,
400 unsigned revokedExceptionId) {
401 std::unique_ptr<V8ConsoleMessage> message = wrapUnique(new V8ConsoleMessage(
402 V8MessageOrigin::kRevokedException, timestamp, messageText));
403 message->m_revokedExceptionId = revokedExceptionId;
404 return message;
405 }
406
407 void V8ConsoleMessage::contextDestroyed(int contextId) {
408 if (contextId != m_contextId) return;
409 m_contextId = 0;
410 if (m_message.isEmpty()) m_message = "<message collected>";
411 Arguments empty;
412 m_arguments.swap(empty);
413 }
414
415 // ------------------------ V8ConsoleMessageStorage ----------------------------
416
417 V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector,
418 int contextGroupId)
419 : m_inspector(inspector),
420 m_contextGroupId(contextGroupId),
421 m_expiredCount(0) {}
422
423 V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); }
424
425 void V8ConsoleMessageStorage::addMessage(
426 std::unique_ptr<V8ConsoleMessage> message) {
427 if (message->type() == ConsoleAPIType::kClear) clear();
428
429 V8InspectorSessionImpl* session =
430 m_inspector->sessionForContextGroup(m_contextGroupId);
431 if (session) {
432 if (message->origin() == V8MessageOrigin::kConsole)
433 session->consoleAgent()->messageAdded(message.get());
434 session->runtimeAgent()->messageAdded(message.get());
435 }
436
437 DCHECK(m_messages.size() <= maxConsoleMessageCount);
438 if (m_messages.size() == maxConsoleMessageCount) {
439 ++m_expiredCount;
440 m_messages.pop_front();
441 }
442 m_messages.push_back(std::move(message));
443 }
444
445 void V8ConsoleMessageStorage::clear() {
446 m_messages.clear();
447 m_expiredCount = 0;
448 if (V8InspectorSessionImpl* session =
449 m_inspector->sessionForContextGroup(m_contextGroupId))
450 session->releaseObjectGroup("console");
451 }
452
453 void V8ConsoleMessageStorage::contextDestroyed(int contextId) {
454 for (size_t i = 0; i < m_messages.size(); ++i)
455 m_messages[i]->contextDestroyed(contextId);
456 }
457
458 } // namespace v8_inspector
OLDNEW
« no previous file with comments | « src/inspector/V8ConsoleMessage.h ('k') | src/inspector/V8Debugger.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698